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