blob: 77ec9a6285c614628dc7828392b92a18d47c8143 [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
Vladimir Marko009d1662017-10-10 13:21:15 +010019#include "reference_type_propagation.h"
20
David Brazdil74eb1b22015-12-14 11:44:01 +000021namespace art {
22
23static constexpr size_t kMaxInstructionsInBranch = 1u;
24
Mads Ager16e52892017-07-14 13:11:37 +020025HSelectGenerator::HSelectGenerator(HGraph* graph,
26 VariableSizedHandleScope* handles,
27 OptimizingCompilerStats* stats)
28 : HOptimization(graph, kSelectGeneratorPassName, stats),
29 handle_scope_(handles) {
30}
31
32// Returns true if `block` has only one predecessor, ends with a Goto
33// or a Return and contains at most `kMaxInstructionsInBranch` other
34// movable instruction with no side-effects.
David Brazdil74eb1b22015-12-14 11:44:01 +000035static bool IsSimpleBlock(HBasicBlock* block) {
36 if (block->GetPredecessors().size() != 1u) {
37 return false;
38 }
39 DCHECK(block->GetPhis().IsEmpty());
40
41 size_t num_instructions = 0u;
42 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
43 HInstruction* instruction = it.Current();
44 if (instruction->IsControlFlow()) {
Mads Ager16e52892017-07-14 13:11:37 +020045 if (num_instructions > kMaxInstructionsInBranch) {
46 return false;
47 }
48 return instruction->IsGoto() || instruction->IsReturn();
David Brazdil74eb1b22015-12-14 11:44:01 +000049 } else if (instruction->CanBeMoved() && !instruction->HasSideEffects()) {
50 num_instructions++;
51 } else {
52 return false;
53 }
54 }
55
56 LOG(FATAL) << "Unreachable";
57 UNREACHABLE();
58}
59
Mads Ager16e52892017-07-14 13:11:37 +020060// Returns true if 'block1' and 'block2' are empty and merge into the
61// same single successor.
David Brazdil74eb1b22015-12-14 11:44:01 +000062static bool BlocksMergeTogether(HBasicBlock* block1, HBasicBlock* block2) {
63 return block1->GetSingleSuccessor() == block2->GetSingleSuccessor();
64}
65
66// Returns nullptr if `block` has either no phis or there is more than one phi
67// with different inputs at `index1` and `index2`. Otherwise returns that phi.
68static HPhi* GetSingleChangedPhi(HBasicBlock* block, size_t index1, size_t index2) {
69 DCHECK_NE(index1, index2);
70
71 HPhi* select_phi = nullptr;
72 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
73 HPhi* phi = it.Current()->AsPhi();
74 if (phi->InputAt(index1) != phi->InputAt(index2)) {
75 if (select_phi == nullptr) {
76 // First phi with different inputs for the two indices found.
77 select_phi = phi;
78 } else {
79 // More than one phis has different inputs for the two indices.
80 return nullptr;
81 }
82 }
83 }
84 return select_phi;
85}
86
87void HSelectGenerator::Run() {
88 // Iterate in post order in the unlikely case that removing one occurrence of
89 // the selection pattern empties a branch block of another occurrence.
90 // Otherwise the order does not matter.
Vladimir Marko2c45bc92016-10-25 16:54:12 +010091 for (HBasicBlock* block : graph_->GetPostOrder()) {
David Brazdil74eb1b22015-12-14 11:44:01 +000092 if (!block->EndsWithIf()) continue;
93
94 // Find elements of the diamond pattern.
95 HIf* if_instruction = block->GetLastInstruction()->AsIf();
96 HBasicBlock* true_block = if_instruction->IfTrueSuccessor();
97 HBasicBlock* false_block = if_instruction->IfFalseSuccessor();
98 DCHECK_NE(true_block, false_block);
99 if (!IsSimpleBlock(true_block) ||
100 !IsSimpleBlock(false_block) ||
101 !BlocksMergeTogether(true_block, false_block)) {
102 continue;
103 }
104 HBasicBlock* merge_block = true_block->GetSingleSuccessor();
105
106 // If the branches are not empty, move instructions in front of the If.
107 // TODO(dbrazdil): This puts an instruction between If and its condition.
108 // Implement moving of conditions to first users if possible.
Mads Ager16e52892017-07-14 13:11:37 +0200109 if (!true_block->IsSingleGoto() && !true_block->IsSingleReturn()) {
David Brazdild6c205e2016-06-07 14:20:52 +0100110 true_block->GetFirstInstruction()->MoveBefore(if_instruction);
David Brazdil74eb1b22015-12-14 11:44:01 +0000111 }
Mads Ager16e52892017-07-14 13:11:37 +0200112 if (!false_block->IsSingleGoto() && !false_block->IsSingleReturn()) {
David Brazdild6c205e2016-06-07 14:20:52 +0100113 false_block->GetFirstInstruction()->MoveBefore(if_instruction);
David Brazdil74eb1b22015-12-14 11:44:01 +0000114 }
Mads Ager16e52892017-07-14 13:11:37 +0200115 DCHECK(true_block->IsSingleGoto() || true_block->IsSingleReturn());
116 DCHECK(false_block->IsSingleGoto() || false_block->IsSingleReturn());
David Brazdil74eb1b22015-12-14 11:44:01 +0000117
118 // Find the resulting true/false values.
119 size_t predecessor_index_true = merge_block->GetPredecessorIndexOf(true_block);
120 size_t predecessor_index_false = merge_block->GetPredecessorIndexOf(false_block);
121 DCHECK_NE(predecessor_index_true, predecessor_index_false);
122
Mads Ager16e52892017-07-14 13:11:37 +0200123 bool both_successors_return = true_block->IsSingleReturn() && false_block->IsSingleReturn();
David Brazdil74eb1b22015-12-14 11:44:01 +0000124 HPhi* phi = GetSingleChangedPhi(merge_block, predecessor_index_true, predecessor_index_false);
Mads Ager16e52892017-07-14 13:11:37 +0200125
126 HInstruction* true_value = nullptr;
127 HInstruction* false_value = nullptr;
128 if (both_successors_return) {
129 true_value = true_block->GetFirstInstruction()->InputAt(0);
130 false_value = false_block->GetFirstInstruction()->InputAt(0);
131 } else if (phi != nullptr) {
132 true_value = phi->InputAt(predecessor_index_true);
133 false_value = phi->InputAt(predecessor_index_false);
134 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +0000135 continue;
136 }
Mads Ager16e52892017-07-14 13:11:37 +0200137 DCHECK(both_successors_return || phi != nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +0000138
139 // Create the Select instruction and insert it in front of the If.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100140 HSelect* select = new (graph_->GetAllocator()) HSelect(if_instruction->InputAt(0),
141 true_value,
142 false_value,
143 if_instruction->GetDexPc());
Mads Ager16e52892017-07-14 13:11:37 +0200144 if (both_successors_return) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100145 if (true_value->GetType() == DataType::Type::kReference) {
146 DCHECK(false_value->GetType() == DataType::Type::kReference);
Mads Ager16e52892017-07-14 13:11:37 +0200147 ReferenceTypePropagation::FixUpInstructionType(select, handle_scope_);
148 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100149 } else if (phi->GetType() == DataType::Type::kReference) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000150 select->SetReferenceTypeInfo(phi->GetReferenceTypeInfo());
151 }
152 block->InsertInstructionBefore(select, if_instruction);
153
Mads Ager16e52892017-07-14 13:11:37 +0200154 // Remove the true branch which removes the corresponding Phi
155 // input if needed. If left only with the false branch, the Phi is
156 // automatically removed.
157 if (both_successors_return) {
158 false_block->GetFirstInstruction()->ReplaceInput(select, 0);
159 } else {
160 phi->ReplaceInput(select, predecessor_index_false);
161 }
162
David Brazdil74eb1b22015-12-14 11:44:01 +0000163 bool only_two_predecessors = (merge_block->GetPredecessors().size() == 2u);
164 true_block->DisconnectAndDelete();
David Brazdil74eb1b22015-12-14 11:44:01 +0000165
166 // Merge remaining blocks which are now connected with Goto.
167 DCHECK_EQ(block->GetSingleSuccessor(), false_block);
168 block->MergeWith(false_block);
Mads Ager16e52892017-07-14 13:11:37 +0200169 if (!both_successors_return && only_two_predecessors) {
170 DCHECK_EQ(only_two_predecessors, phi->GetBlock() == nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +0000171 DCHECK_EQ(block->GetSingleSuccessor(), merge_block);
172 block->MergeWith(merge_block);
173 }
174
Igor Murashkin1e065a52017-08-09 13:20:34 -0700175 MaybeRecordStat(stats_, MethodCompilationStat::kSelectGenerated);
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +0100176
David Brazdil74eb1b22015-12-14 11:44:01 +0000177 // No need to update dominance information, as we are simplifying
178 // a simple diamond shape, where the join block is merged with the
179 // entry block. Any following blocks would have had the join block
180 // as a dominator, and `MergeWith` handles changing that to the
181 // entry block.
182 }
183}
184
185} // namespace art