blob: ecf9fa28a77988e458560847675eafe3d70c62cf [file] [log] [blame]
David Brazdil46e2a392015-03-16 17:31:52 +00001/*
2 * Copyright (C) 2015 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 "boolean_simplifier.h"
18
19namespace art {
20
21static bool EndsWithAnIf(HBasicBlock* block) {
22 return block->GetLastInstruction()->IsIf();
23}
24
25static bool HasSinglePhi(HBasicBlock* block) {
26 return !block->GetPhis().IsEmpty()
27 && block->GetFirstPhi()->GetNext() == nullptr;
28}
29
30// Returns true if 'block1' and 'block2' are empty, merge into the same single
31// successor and the successor can only be reached from them.
32static bool BlocksDoMergeTogether(HBasicBlock* block1, HBasicBlock* block2) {
33 if (!block1->IsSingleGoto() || !block2->IsSingleGoto()) return false;
34 HBasicBlock* succ1 = block1->GetSuccessors().Get(0);
35 HBasicBlock* succ2 = block2->GetSuccessors().Get(0);
36 return succ1 == succ2 && succ1->GetPredecessors().Size() == 2u;
37}
38
39// Returns true if the outcome of the branching matches the boolean value of
40// the branching condition.
41static bool PreservesCondition(HInstruction* input_true, HInstruction* input_false) {
42 return input_true->IsIntConstant() && input_true->AsIntConstant()->GetValue() == 1
43 && input_false->IsIntConstant() && input_false->AsIntConstant()->GetValue() == 0;
44}
45
46// Returns true if the outcome of the branching is exactly opposite of the
47// boolean value of the branching condition.
48static bool NegatesCondition(HInstruction* input_true, HInstruction* input_false) {
49 return input_true->IsIntConstant() && input_true->AsIntConstant()->GetValue() == 0
50 && input_false->IsIntConstant() && input_false->AsIntConstant()->GetValue() == 1;
51}
52
53// Returns an instruction with the opposite boolean value from 'cond'.
54static HInstruction* GetOppositeCondition(HInstruction* cond) {
55 HGraph* graph = cond->GetBlock()->GetGraph();
56 ArenaAllocator* allocator = graph->GetArena();
57
58 if (cond->IsCondition()) {
59 HInstruction* lhs = cond->InputAt(0);
60 HInstruction* rhs = cond->InputAt(1);
61 if (cond->IsEqual()) {
62 return new (allocator) HNotEqual(lhs, rhs);
63 } else if (cond->IsNotEqual()) {
64 return new (allocator) HEqual(lhs, rhs);
65 } else if (cond->IsLessThan()) {
66 return new (allocator) HGreaterThanOrEqual(lhs, rhs);
67 } else if (cond->IsLessThanOrEqual()) {
68 return new (allocator) HGreaterThan(lhs, rhs);
69 } else if (cond->IsGreaterThan()) {
70 return new (allocator) HLessThanOrEqual(lhs, rhs);
71 } else if (cond->IsGreaterThanOrEqual()) {
72 return new (allocator) HLessThan(lhs, rhs);
73 }
74 } else if (cond->IsIntConstant()) {
75 int32_t value = cond->AsIntConstant()->GetValue();
76 if (value == 0) {
77 return graph->GetIntConstant1();
78 } else {
79 DCHECK_EQ(value, 1);
80 return graph->GetIntConstant0();
81 }
82 }
83
84 LOG(FATAL) << "Instruction " << cond->DebugName() << " used as a condition";
85 UNREACHABLE();
86}
87
88void HBooleanSimplifier::Run() {
89 // Iterate in post order in the unlikely case that removing one occurrence of
90 // the pattern empties a branch block of another occurrence. Otherwise the
91 // order does not matter.
92 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
93 HBasicBlock* block = it.Current();
94 if (!EndsWithAnIf(block)) continue;
95
96 // Find elements of the pattern.
97 HIf* if_instruction = block->GetLastInstruction()->AsIf();
98 HBasicBlock* true_block = if_instruction->IfTrueSuccessor();
99 HBasicBlock* false_block = if_instruction->IfFalseSuccessor();
100 if (!BlocksDoMergeTogether(true_block, false_block)) {
101 continue;
102 }
103 HBasicBlock* merge_block = true_block->GetSuccessors().Get(0);
104 if (!HasSinglePhi(merge_block)) {
105 continue;
106 }
107 HPhi* phi = merge_block->GetFirstPhi()->AsPhi();
108 HInstruction* true_value = phi->InputAt(merge_block->GetPredecessorIndexOf(true_block));
109 HInstruction* false_value = phi->InputAt(merge_block->GetPredecessorIndexOf(false_block));
110
111 // Check if the selection negates/preserves the value of the condition and
112 // if so, generate a suitable replacement instruction.
113 HInstruction* if_condition = if_instruction->InputAt(0);
114 HInstruction* replacement;
115 if (NegatesCondition(true_value, false_value)) {
116 replacement = GetOppositeCondition(if_condition);
117 if (replacement->GetBlock() == nullptr) {
118 block->InsertInstructionBefore(replacement, if_instruction);
119 }
120 } else if (PreservesCondition(true_value, false_value)) {
121 replacement = if_condition;
122 } else {
123 continue;
124 }
125
126 // Replace the selection outcome with the new instruction.
127 phi->ReplaceWith(replacement);
128 merge_block->RemovePhi(phi);
129
130 // Link the start/end blocks and remove empty branches.
131 graph_->MergeEmptyBranches(block, merge_block);
132
133 // Remove the original condition if it is now unused.
134 if (!if_condition->HasUses()) {
135 if_condition->GetBlock()->RemoveInstruction(if_condition);
136 }
137 }
138}
139
140} // namespace art