blob: fd99070780031f5f9ebcef68553e5b4607fbd2b8 [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
Calin Juravleacf735c2015-02-12 15:25:22 +000019#include "mirror/class-inl.h"
20#include "scoped_thread_state_change.h"
21
Nicolas Geoffray3c049742014-09-24 18:10:46 +010022namespace art {
23
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000024class InstructionSimplifierVisitor : public HGraphVisitor {
25 public:
Calin Juravleacf735c2015-02-12 15:25:22 +000026 InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
27 : HGraphVisitor(graph), stats_(stats) {}
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000028
29 private:
30 void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE;
31 void VisitEqual(HEqual* equal) OVERRIDE;
32 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000033 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000034 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080035 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000036 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
37
38 OptimizingCompilerStats* stats_;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000039};
40
Nicolas Geoffray3c049742014-09-24 18:10:46 +010041void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +000042 InstructionSimplifierVisitor visitor(graph_, stats_);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000043 visitor.VisitInsertionOrder();
Nicolas Geoffray3c049742014-09-24 18:10:46 +010044}
45
Calin Juravle10e244f2015-01-26 18:54:32 +000046void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
47 HInstruction* obj = null_check->InputAt(0);
48 if (!obj->CanBeNull()) {
49 null_check->ReplaceWith(obj);
50 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +000051 if (stats_ != nullptr) {
52 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
53 }
54 }
55}
56
57void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
58 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
59 if (!load_class->IsResolved()) {
60 // If the class couldn't be resolve it's not safe to compare against it. It's
61 // default type would be Top which might be wider that the actual class type
62 // and thus producing wrong results.
63 return;
64 }
65 ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo();
66 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
67 ScopedObjectAccess soa(Thread::Current());
68 if (class_rti.IsSupertypeOf(obj_rti)) {
69 check_cast->GetBlock()->RemoveInstruction(check_cast);
70 if (stats_ != nullptr) {
71 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
72 }
Calin Juravle10e244f2015-01-26 18:54:32 +000073 }
74}
75
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000076void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +010077 HBasicBlock* block = check->GetBlock();
78 // Currently always keep the suspend check at entry.
79 if (block->IsEntryBlock()) return;
80
81 // Currently always keep suspend checks at loop entry.
82 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
83 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
84 return;
85 }
86
87 // Remove the suspend check that was added at build time for the baseline
88 // compiler.
89 block->RemoveInstruction(check);
90}
91
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000092void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010093 HInstruction* input1 = equal->InputAt(0);
94 HInstruction* input2 = equal->InputAt(1);
95 if (input1->GetType() == Primitive::kPrimBoolean && input2->IsIntConstant()) {
96 if (input2->AsIntConstant()->GetValue() == 1) {
97 // Replace (bool_value == 1) with bool_value
98 equal->ReplaceWith(equal->InputAt(0));
99 equal->GetBlock()->RemoveInstruction(equal);
100 } else {
Nicolas Geoffrayfa93b502015-01-21 15:44:16 +0000101 // We should replace (bool_value == 0) with !bool_value, but we unfortunately
102 // do not have such instruction.
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100103 DCHECK_EQ(input2->AsIntConstant()->GetValue(), 0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100104 }
105 }
106}
107
Mingyao Yang0304e182015-01-30 16:41:29 -0800108void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
109 HInstruction* input = instruction->InputAt(0);
110 // If the array is a NewArray with constant size, replace the array length
111 // with the constant instruction. This helps the bounds check elimination phase.
112 if (input->IsNewArray()) {
113 input = input->InputAt(0);
114 if (input->IsIntConstant()) {
115 instruction->ReplaceWith(input);
116 }
117 }
118}
119
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000120void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000121 HInstruction* value = instruction->GetValue();
122 if (value->GetType() != Primitive::kPrimNot) return;
123
124 if (value->IsArrayGet()) {
125 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
126 // If the code is just swapping elements in the array, no need for a type check.
127 instruction->ClearNeedsTypeCheck();
128 }
129 }
130}
131
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000132void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
133 if (instruction->GetResultType() == instruction->GetInputType()) {
134 // Remove the instruction if it's converting to the same type.
135 instruction->ReplaceWith(instruction->GetInput());
136 instruction->GetBlock()->RemoveInstruction(instruction);
137 }
138}
139
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100140} // namespace art