blob: 49ca44331de783ff8a07b750d8fe894608a9b060 [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;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000030};
31
Nicolas Geoffray3c049742014-09-24 18:10:46 +010032void InstructionSimplifier::Run() {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000033 InstructionSimplifierVisitor visitor(graph_);
34 visitor.VisitInsertionOrder();
Nicolas Geoffray3c049742014-09-24 18:10:46 +010035}
36
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000037void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +010038 HBasicBlock* block = check->GetBlock();
39 // Currently always keep the suspend check at entry.
40 if (block->IsEntryBlock()) return;
41
42 // Currently always keep suspend checks at loop entry.
43 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
44 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
45 return;
46 }
47
48 // Remove the suspend check that was added at build time for the baseline
49 // compiler.
50 block->RemoveInstruction(check);
51}
52
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000053void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010054 HInstruction* input1 = equal->InputAt(0);
55 HInstruction* input2 = equal->InputAt(1);
56 if (input1->GetType() == Primitive::kPrimBoolean && input2->IsIntConstant()) {
57 if (input2->AsIntConstant()->GetValue() == 1) {
58 // Replace (bool_value == 1) with bool_value
59 equal->ReplaceWith(equal->InputAt(0));
60 equal->GetBlock()->RemoveInstruction(equal);
61 } else {
62 // Replace (bool_value == 0) with !bool_value
63 DCHECK_EQ(input2->AsIntConstant()->GetValue(), 0);
64 equal->GetBlock()->ReplaceAndRemoveInstructionWith(
Roland Levillain1cc5f2512014-10-22 18:06:21 +010065 equal, new (GetGraph()->GetArena()) HNot(Primitive::kPrimBoolean, input1));
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010066 }
67 }
68}
69
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000070void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000071 HInstruction* value = instruction->GetValue();
72 if (value->GetType() != Primitive::kPrimNot) return;
73
74 if (value->IsArrayGet()) {
75 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
76 // If the code is just swapping elements in the array, no need for a type check.
77 instruction->ClearNeedsTypeCheck();
78 }
79 }
80}
81
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000082void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
83 if (instruction->GetResultType() == instruction->GetInputType()) {
84 // Remove the instruction if it's converting to the same type.
85 instruction->ReplaceWith(instruction->GetInput());
86 instruction->GetBlock()->RemoveInstruction(instruction);
87 }
88}
89
Nicolas Geoffray3c049742014-09-24 18:10:46 +010090} // namespace art