Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1 | /* |
| 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 Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame^] | 19 | #include "mirror/class-inl.h" |
| 20 | #include "scoped_thread_state_change.h" |
| 21 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 24 | class InstructionSimplifierVisitor : public HGraphVisitor { |
| 25 | public: |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame^] | 26 | InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats) |
| 27 | : HGraphVisitor(graph), stats_(stats) {} |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 28 | |
| 29 | private: |
| 30 | void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE; |
| 31 | void VisitEqual(HEqual* equal) OVERRIDE; |
| 32 | void VisitArraySet(HArraySet* equal) OVERRIDE; |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 33 | void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 34 | void VisitNullCheck(HNullCheck* instruction) OVERRIDE; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 35 | void VisitArrayLength(HArrayLength* instruction) OVERRIDE; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame^] | 36 | void VisitCheckCast(HCheckCast* instruction) OVERRIDE; |
| 37 | |
| 38 | OptimizingCompilerStats* stats_; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 41 | void InstructionSimplifier::Run() { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame^] | 42 | InstructionSimplifierVisitor visitor(graph_, stats_); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 43 | visitor.VisitInsertionOrder(); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 46 | void 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 Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame^] | 51 | if (stats_ != nullptr) { |
| 52 | stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void 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 Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 76 | void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 77 | 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 Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 92 | void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 93 | 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 Geoffray | fa93b50 | 2015-01-21 15:44:16 +0000 | [diff] [blame] | 101 | // We should replace (bool_value == 0) with !bool_value, but we unfortunately |
| 102 | // do not have such instruction. |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 103 | DCHECK_EQ(input2->AsIntConstant()->GetValue(), 0); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 108 | void 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 Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 120 | void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) { |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 121 | 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 Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 132 | void 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 Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 140 | } // namespace art |