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 | |
| 19 | namespace art { |
| 20 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 21 | class 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 Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 29 | void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 30 | void VisitNullCheck(HNullCheck* instruction) OVERRIDE; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame^] | 31 | void VisitArrayLength(HArrayLength* instruction) OVERRIDE; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 34 | void InstructionSimplifier::Run() { |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 35 | InstructionSimplifierVisitor visitor(graph_); |
| 36 | visitor.VisitInsertionOrder(); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 39 | void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) { |
| 40 | HInstruction* obj = null_check->InputAt(0); |
| 41 | if (!obj->CanBeNull()) { |
| 42 | null_check->ReplaceWith(obj); |
| 43 | null_check->GetBlock()->RemoveInstruction(null_check); |
| 44 | } |
| 45 | } |
| 46 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 47 | void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 48 | HBasicBlock* block = check->GetBlock(); |
| 49 | // Currently always keep the suspend check at entry. |
| 50 | if (block->IsEntryBlock()) return; |
| 51 | |
| 52 | // Currently always keep suspend checks at loop entry. |
| 53 | if (block->IsLoopHeader() && block->GetFirstInstruction() == check) { |
| 54 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | // Remove the suspend check that was added at build time for the baseline |
| 59 | // compiler. |
| 60 | block->RemoveInstruction(check); |
| 61 | } |
| 62 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 63 | void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 64 | HInstruction* input1 = equal->InputAt(0); |
| 65 | HInstruction* input2 = equal->InputAt(1); |
| 66 | if (input1->GetType() == Primitive::kPrimBoolean && input2->IsIntConstant()) { |
| 67 | if (input2->AsIntConstant()->GetValue() == 1) { |
| 68 | // Replace (bool_value == 1) with bool_value |
| 69 | equal->ReplaceWith(equal->InputAt(0)); |
| 70 | equal->GetBlock()->RemoveInstruction(equal); |
| 71 | } else { |
Nicolas Geoffray | fa93b50 | 2015-01-21 15:44:16 +0000 | [diff] [blame] | 72 | // We should replace (bool_value == 0) with !bool_value, but we unfortunately |
| 73 | // do not have such instruction. |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 74 | DCHECK_EQ(input2->AsIntConstant()->GetValue(), 0); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame^] | 79 | void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) { |
| 80 | HInstruction* input = instruction->InputAt(0); |
| 81 | // If the array is a NewArray with constant size, replace the array length |
| 82 | // with the constant instruction. This helps the bounds check elimination phase. |
| 83 | if (input->IsNewArray()) { |
| 84 | input = input->InputAt(0); |
| 85 | if (input->IsIntConstant()) { |
| 86 | instruction->ReplaceWith(input); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 91 | void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) { |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 92 | HInstruction* value = instruction->GetValue(); |
| 93 | if (value->GetType() != Primitive::kPrimNot) return; |
| 94 | |
| 95 | if (value->IsArrayGet()) { |
| 96 | if (value->AsArrayGet()->GetArray() == instruction->GetArray()) { |
| 97 | // If the code is just swapping elements in the array, no need for a type check. |
| 98 | instruction->ClearNeedsTypeCheck(); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 103 | void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) { |
| 104 | if (instruction->GetResultType() == instruction->GetInputType()) { |
| 105 | // Remove the instruction if it's converting to the same type. |
| 106 | instruction->ReplaceWith(instruction->GetInput()); |
| 107 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 108 | } |
| 109 | } |
| 110 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 111 | } // namespace art |