blob: 17c8f337ca115b4ad65231fcb67e4a9de4c7c2f3 [file] [log] [blame]
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instruction_simplifier.h"
18
19namespace art {
20
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000021class InstructionSimplifierVisitor : public HGraphVisitor {
22 public:
23 explicit InstructionSimplifierVisitor(HGraph* graph) : HGraphVisitor(graph) {}
24
25 private:
26 void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE;
27 void VisitEqual(HEqual* equal) OVERRIDE;
28 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000029 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000030 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000031};
32
Nicolas Geoffray3c049742014-09-24 18:10:46 +010033void InstructionSimplifier::Run() {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000034 InstructionSimplifierVisitor visitor(graph_);
35 visitor.VisitInsertionOrder();
Nicolas Geoffray3c049742014-09-24 18:10:46 +010036}
37
Calin Juravle10e244f2015-01-26 18:54:32 +000038void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
39 HInstruction* obj = null_check->InputAt(0);
40 if (!obj->CanBeNull()) {
41 null_check->ReplaceWith(obj);
42 null_check->GetBlock()->RemoveInstruction(null_check);
43 }
44}
45
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000046void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +010047 HBasicBlock* block = check->GetBlock();
48 // Currently always keep the suspend check at entry.
49 if (block->IsEntryBlock()) return;
50
51 // Currently always keep suspend checks at loop entry.
52 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
53 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
54 return;
55 }
56
57 // Remove the suspend check that was added at build time for the baseline
58 // compiler.
59 block->RemoveInstruction(check);
60}
61
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000062void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010063 HInstruction* input1 = equal->InputAt(0);
64 HInstruction* input2 = equal->InputAt(1);
65 if (input1->GetType() == Primitive::kPrimBoolean && input2->IsIntConstant()) {
66 if (input2->AsIntConstant()->GetValue() == 1) {
67 // Replace (bool_value == 1) with bool_value
68 equal->ReplaceWith(equal->InputAt(0));
69 equal->GetBlock()->RemoveInstruction(equal);
70 } else {
Nicolas Geoffrayfa93b502015-01-21 15:44:16 +000071 // We should replace (bool_value == 0) with !bool_value, but we unfortunately
72 // do not have such instruction.
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010073 DCHECK_EQ(input2->AsIntConstant()->GetValue(), 0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010074 }
75 }
76}
77
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000078void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000079 HInstruction* value = instruction->GetValue();
80 if (value->GetType() != Primitive::kPrimNot) return;
81
82 if (value->IsArrayGet()) {
83 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
84 // If the code is just swapping elements in the array, no need for a type check.
85 instruction->ClearNeedsTypeCheck();
86 }
87 }
88}
89
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000090void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
91 if (instruction->GetResultType() == instruction->GetInputType()) {
92 // Remove the instruction if it's converting to the same type.
93 instruction->ReplaceWith(instruction->GetInput());
94 instruction->GetBlock()->RemoveInstruction(instruction);
95 }
96}
97
Nicolas Geoffray3c049742014-09-24 18:10:46 +010098} // namespace art