blob: 827b5913afdffad635ffa11fbe51a6b30f6bd0a5 [file] [log] [blame]
David Brazdil74eb1b22015-12-14 11:44:01 +00001/*
2 * Copyright (C) 2016 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 "select_generator.h"
18
19namespace art {
20
21static constexpr size_t kMaxInstructionsInBranch = 1u;
22
Mads Ager16e52892017-07-14 13:11:37 +020023HSelectGenerator::HSelectGenerator(HGraph* graph,
24 VariableSizedHandleScope* handles,
25 OptimizingCompilerStats* stats)
26 : HOptimization(graph, kSelectGeneratorPassName, stats),
27 handle_scope_(handles) {
28}
29
30// Returns true if `block` has only one predecessor, ends with a Goto
31// or a Return and contains at most `kMaxInstructionsInBranch` other
32// movable instruction with no side-effects.
David Brazdil74eb1b22015-12-14 11:44:01 +000033static bool IsSimpleBlock(HBasicBlock* block) {
34 if (block->GetPredecessors().size() != 1u) {
35 return false;
36 }
37 DCHECK(block->GetPhis().IsEmpty());
38
39 size_t num_instructions = 0u;
40 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
41 HInstruction* instruction = it.Current();
42 if (instruction->IsControlFlow()) {
Mads Ager16e52892017-07-14 13:11:37 +020043 if (num_instructions > kMaxInstructionsInBranch) {
44 return false;
45 }
46 return instruction->IsGoto() || instruction->IsReturn();
David Brazdil74eb1b22015-12-14 11:44:01 +000047 } else if (instruction->CanBeMoved() && !instruction->HasSideEffects()) {
48 num_instructions++;
49 } else {
50 return false;
51 }
52 }
53
54 LOG(FATAL) << "Unreachable";
55 UNREACHABLE();
56}
57
Mads Ager16e52892017-07-14 13:11:37 +020058// Returns true if 'block1' and 'block2' are empty and merge into the
59// same single successor.
David Brazdil74eb1b22015-12-14 11:44:01 +000060static bool BlocksMergeTogether(HBasicBlock* block1, HBasicBlock* block2) {
61 return block1->GetSingleSuccessor() == block2->GetSingleSuccessor();
62}
63
64// Returns nullptr if `block` has either no phis or there is more than one phi
65// with different inputs at `index1` and `index2`. Otherwise returns that phi.
66static HPhi* GetSingleChangedPhi(HBasicBlock* block, size_t index1, size_t index2) {
67 DCHECK_NE(index1, index2);
68
69 HPhi* select_phi = nullptr;
70 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
71 HPhi* phi = it.Current()->AsPhi();
72 if (phi->InputAt(index1) != phi->InputAt(index2)) {
73 if (select_phi == nullptr) {
74 // First phi with different inputs for the two indices found.
75 select_phi = phi;
76 } else {
77 // More than one phis has different inputs for the two indices.
78 return nullptr;
79 }
80 }
81 }
82 return select_phi;
83}
84
85void HSelectGenerator::Run() {
86 // Iterate in post order in the unlikely case that removing one occurrence of
87 // the selection pattern empties a branch block of another occurrence.
88 // Otherwise the order does not matter.
Vladimir Marko2c45bc92016-10-25 16:54:12 +010089 for (HBasicBlock* block : graph_->GetPostOrder()) {
David Brazdil74eb1b22015-12-14 11:44:01 +000090 if (!block->EndsWithIf()) continue;
91
92 // Find elements of the diamond pattern.
93 HIf* if_instruction = block->GetLastInstruction()->AsIf();
94 HBasicBlock* true_block = if_instruction->IfTrueSuccessor();
95 HBasicBlock* false_block = if_instruction->IfFalseSuccessor();
96 DCHECK_NE(true_block, false_block);
97 if (!IsSimpleBlock(true_block) ||
98 !IsSimpleBlock(false_block) ||
99 !BlocksMergeTogether(true_block, false_block)) {
100 continue;
101 }
102 HBasicBlock* merge_block = true_block->GetSingleSuccessor();
103
104 // If the branches are not empty, move instructions in front of the If.
105 // TODO(dbrazdil): This puts an instruction between If and its condition.
106 // Implement moving of conditions to first users if possible.
Mads Ager16e52892017-07-14 13:11:37 +0200107 if (!true_block->IsSingleGoto() && !true_block->IsSingleReturn()) {
David Brazdild6c205e2016-06-07 14:20:52 +0100108 true_block->GetFirstInstruction()->MoveBefore(if_instruction);
David Brazdil74eb1b22015-12-14 11:44:01 +0000109 }
Mads Ager16e52892017-07-14 13:11:37 +0200110 if (!false_block->IsSingleGoto() && !false_block->IsSingleReturn()) {
David Brazdild6c205e2016-06-07 14:20:52 +0100111 false_block->GetFirstInstruction()->MoveBefore(if_instruction);
David Brazdil74eb1b22015-12-14 11:44:01 +0000112 }
Mads Ager16e52892017-07-14 13:11:37 +0200113 DCHECK(true_block->IsSingleGoto() || true_block->IsSingleReturn());
114 DCHECK(false_block->IsSingleGoto() || false_block->IsSingleReturn());
David Brazdil74eb1b22015-12-14 11:44:01 +0000115
116 // Find the resulting true/false values.
117 size_t predecessor_index_true = merge_block->GetPredecessorIndexOf(true_block);
118 size_t predecessor_index_false = merge_block->GetPredecessorIndexOf(false_block);
119 DCHECK_NE(predecessor_index_true, predecessor_index_false);
120
Mads Ager16e52892017-07-14 13:11:37 +0200121 bool both_successors_return = true_block->IsSingleReturn() && false_block->IsSingleReturn();
David Brazdil74eb1b22015-12-14 11:44:01 +0000122 HPhi* phi = GetSingleChangedPhi(merge_block, predecessor_index_true, predecessor_index_false);
Mads Ager16e52892017-07-14 13:11:37 +0200123
124 HInstruction* true_value = nullptr;
125 HInstruction* false_value = nullptr;
126 if (both_successors_return) {
127 true_value = true_block->GetFirstInstruction()->InputAt(0);
128 false_value = false_block->GetFirstInstruction()->InputAt(0);
129 } else if (phi != nullptr) {
130 true_value = phi->InputAt(predecessor_index_true);
131 false_value = phi->InputAt(predecessor_index_false);
132 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +0000133 continue;
134 }
Mads Ager16e52892017-07-14 13:11:37 +0200135 DCHECK(both_successors_return || phi != nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +0000136
137 // Create the Select instruction and insert it in front of the If.
138 HSelect* select = new (graph_->GetArena()) HSelect(if_instruction->InputAt(0),
139 true_value,
140 false_value,
141 if_instruction->GetDexPc());
Mads Ager16e52892017-07-14 13:11:37 +0200142 if (both_successors_return) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 if (true_value->GetType() == DataType::Type::kReference) {
144 DCHECK(false_value->GetType() == DataType::Type::kReference);
Mads Ager16e52892017-07-14 13:11:37 +0200145 ReferenceTypePropagation::FixUpInstructionType(select, handle_scope_);
146 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147 } else if (phi->GetType() == DataType::Type::kReference) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000148 select->SetReferenceTypeInfo(phi->GetReferenceTypeInfo());
149 }
150 block->InsertInstructionBefore(select, if_instruction);
151
Mads Ager16e52892017-07-14 13:11:37 +0200152 // Remove the true branch which removes the corresponding Phi
153 // input if needed. If left only with the false branch, the Phi is
154 // automatically removed.
155 if (both_successors_return) {
156 false_block->GetFirstInstruction()->ReplaceInput(select, 0);
157 } else {
158 phi->ReplaceInput(select, predecessor_index_false);
159 }
160
David Brazdil74eb1b22015-12-14 11:44:01 +0000161 bool only_two_predecessors = (merge_block->GetPredecessors().size() == 2u);
162 true_block->DisconnectAndDelete();
David Brazdil74eb1b22015-12-14 11:44:01 +0000163
164 // Merge remaining blocks which are now connected with Goto.
165 DCHECK_EQ(block->GetSingleSuccessor(), false_block);
166 block->MergeWith(false_block);
Mads Ager16e52892017-07-14 13:11:37 +0200167 if (!both_successors_return && only_two_predecessors) {
168 DCHECK_EQ(only_two_predecessors, phi->GetBlock() == nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +0000169 DCHECK_EQ(block->GetSingleSuccessor(), merge_block);
170 block->MergeWith(merge_block);
171 }
172
Igor Murashkin1e065a52017-08-09 13:20:34 -0700173 MaybeRecordStat(stats_, MethodCompilationStat::kSelectGenerated);
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +0100174
David Brazdil74eb1b22015-12-14 11:44:01 +0000175 // No need to update dominance information, as we are simplifying
176 // a simple diamond shape, where the join block is merged with the
177 // entry block. Any following blocks would have had the join block
178 // as a dominator, and `MergeWith` handles changing that to the
179 // entry block.
180 }
181}
182
183} // namespace art