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; |
| 29 | }; |
| 30 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 31 | void InstructionSimplifier::Run() { |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 32 | InstructionSimplifierVisitor visitor(graph_); |
| 33 | visitor.VisitInsertionOrder(); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 36 | void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 37 | HBasicBlock* block = check->GetBlock(); |
| 38 | // Currently always keep the suspend check at entry. |
| 39 | if (block->IsEntryBlock()) return; |
| 40 | |
| 41 | // Currently always keep suspend checks at loop entry. |
| 42 | if (block->IsLoopHeader() && block->GetFirstInstruction() == check) { |
| 43 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | // Remove the suspend check that was added at build time for the baseline |
| 48 | // compiler. |
| 49 | block->RemoveInstruction(check); |
| 50 | } |
| 51 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 52 | void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 53 | HInstruction* input1 = equal->InputAt(0); |
| 54 | HInstruction* input2 = equal->InputAt(1); |
| 55 | if (input1->GetType() == Primitive::kPrimBoolean && input2->IsIntConstant()) { |
| 56 | if (input2->AsIntConstant()->GetValue() == 1) { |
| 57 | // Replace (bool_value == 1) with bool_value |
| 58 | equal->ReplaceWith(equal->InputAt(0)); |
| 59 | equal->GetBlock()->RemoveInstruction(equal); |
| 60 | } else { |
| 61 | // Replace (bool_value == 0) with !bool_value |
| 62 | DCHECK_EQ(input2->AsIntConstant()->GetValue(), 0); |
| 63 | equal->GetBlock()->ReplaceAndRemoveInstructionWith( |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 64 | equal, new (GetGraph()->GetArena()) HNot(Primitive::kPrimBoolean, input1)); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 69 | void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) { |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 70 | HInstruction* value = instruction->GetValue(); |
| 71 | if (value->GetType() != Primitive::kPrimNot) return; |
| 72 | |
| 73 | if (value->IsArrayGet()) { |
| 74 | if (value->AsArrayGet()->GetArray() == instruction->GetArray()) { |
| 75 | // If the code is just swapping elements in the array, no need for a type check. |
| 76 | instruction->ClearNeedsTypeCheck(); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 81 | } // namespace art |