blob: 3f52bdd13c1465067b3e6e68e0f34174d35d11c6 [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,
Aart Bik2ca10eb2017-11-15 15:17:53 -080027 OptimizingCompilerStats* stats,
28 const char* name)
29 : HOptimization(graph, name, stats),
Mads Ager16e52892017-07-14 13:11:37 +020030 handle_scope_(handles) {
31}
32
33// Returns true if `block` has only one predecessor, ends with a Goto
34// or a Return and contains at most `kMaxInstructionsInBranch` other
35// movable instruction with no side-effects.
David Brazdil74eb1b22015-12-14 11:44:01 +000036static bool IsSimpleBlock(HBasicBlock* block) {
37 if (block->GetPredecessors().size() != 1u) {
38 return false;
39 }
40 DCHECK(block->GetPhis().IsEmpty());
41
42 size_t num_instructions = 0u;
43 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
44 HInstruction* instruction = it.Current();
45 if (instruction->IsControlFlow()) {
Mads Ager16e52892017-07-14 13:11:37 +020046 return instruction->IsGoto() || instruction->IsReturn();
David Brazdil74eb1b22015-12-14 11:44:01 +000047 } else if (instruction->CanBeMoved() && !instruction->HasSideEffects()) {
Aart Bik1d746de2018-03-28 16:30:02 -070048 if (instruction->IsSelect() &&
49 instruction->AsSelect()->GetCondition()->GetBlock() == block) {
50 // Count one HCondition and HSelect in the same block as a single instruction.
51 // This enables finding nested selects.
52 continue;
53 } else if (++num_instructions > kMaxInstructionsInBranch) {
54 return false; // bail as soon as we exceed number of allowed instructions
55 }
David Brazdil74eb1b22015-12-14 11:44:01 +000056 } else {
57 return false;
58 }
59 }
60
61 LOG(FATAL) << "Unreachable";
62 UNREACHABLE();
63}
64
Mads Ager16e52892017-07-14 13:11:37 +020065// Returns true if 'block1' and 'block2' are empty and merge into the
66// same single successor.
David Brazdil74eb1b22015-12-14 11:44:01 +000067static bool BlocksMergeTogether(HBasicBlock* block1, HBasicBlock* block2) {
68 return block1->GetSingleSuccessor() == block2->GetSingleSuccessor();
69}
70
71// Returns nullptr if `block` has either no phis or there is more than one phi
72// with different inputs at `index1` and `index2`. Otherwise returns that phi.
73static HPhi* GetSingleChangedPhi(HBasicBlock* block, size_t index1, size_t index2) {
74 DCHECK_NE(index1, index2);
75
76 HPhi* select_phi = nullptr;
77 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
78 HPhi* phi = it.Current()->AsPhi();
79 if (phi->InputAt(index1) != phi->InputAt(index2)) {
80 if (select_phi == nullptr) {
81 // First phi with different inputs for the two indices found.
82 select_phi = phi;
83 } else {
84 // More than one phis has different inputs for the two indices.
85 return nullptr;
86 }
87 }
88 }
89 return select_phi;
90}
91
92void HSelectGenerator::Run() {
93 // Iterate in post order in the unlikely case that removing one occurrence of
94 // the selection pattern empties a branch block of another occurrence.
95 // Otherwise the order does not matter.
Vladimir Marko2c45bc92016-10-25 16:54:12 +010096 for (HBasicBlock* block : graph_->GetPostOrder()) {
David Brazdil74eb1b22015-12-14 11:44:01 +000097 if (!block->EndsWithIf()) continue;
98
99 // Find elements of the diamond pattern.
100 HIf* if_instruction = block->GetLastInstruction()->AsIf();
101 HBasicBlock* true_block = if_instruction->IfTrueSuccessor();
102 HBasicBlock* false_block = if_instruction->IfFalseSuccessor();
103 DCHECK_NE(true_block, false_block);
Aart Bik1d746de2018-03-28 16:30:02 -0700104
David Brazdil74eb1b22015-12-14 11:44:01 +0000105 if (!IsSimpleBlock(true_block) ||
106 !IsSimpleBlock(false_block) ||
107 !BlocksMergeTogether(true_block, false_block)) {
108 continue;
109 }
110 HBasicBlock* merge_block = true_block->GetSingleSuccessor();
111
112 // If the branches are not empty, move instructions in front of the If.
113 // TODO(dbrazdil): This puts an instruction between If and its condition.
114 // Implement moving of conditions to first users if possible.
Aart Bik1d746de2018-03-28 16:30:02 -0700115 while (!true_block->IsSingleGoto() && !true_block->IsSingleReturn()) {
David Brazdild6c205e2016-06-07 14:20:52 +0100116 true_block->GetFirstInstruction()->MoveBefore(if_instruction);
David Brazdil74eb1b22015-12-14 11:44:01 +0000117 }
Aart Bik1d746de2018-03-28 16:30:02 -0700118 while (!false_block->IsSingleGoto() && !false_block->IsSingleReturn()) {
David Brazdild6c205e2016-06-07 14:20:52 +0100119 false_block->GetFirstInstruction()->MoveBefore(if_instruction);
David Brazdil74eb1b22015-12-14 11:44:01 +0000120 }
Mads Ager16e52892017-07-14 13:11:37 +0200121 DCHECK(true_block->IsSingleGoto() || true_block->IsSingleReturn());
122 DCHECK(false_block->IsSingleGoto() || false_block->IsSingleReturn());
David Brazdil74eb1b22015-12-14 11:44:01 +0000123
124 // Find the resulting true/false values.
125 size_t predecessor_index_true = merge_block->GetPredecessorIndexOf(true_block);
126 size_t predecessor_index_false = merge_block->GetPredecessorIndexOf(false_block);
127 DCHECK_NE(predecessor_index_true, predecessor_index_false);
128
Mads Ager16e52892017-07-14 13:11:37 +0200129 bool both_successors_return = true_block->IsSingleReturn() && false_block->IsSingleReturn();
David Brazdil74eb1b22015-12-14 11:44:01 +0000130 HPhi* phi = GetSingleChangedPhi(merge_block, predecessor_index_true, predecessor_index_false);
Mads Ager16e52892017-07-14 13:11:37 +0200131
132 HInstruction* true_value = nullptr;
133 HInstruction* false_value = nullptr;
134 if (both_successors_return) {
135 true_value = true_block->GetFirstInstruction()->InputAt(0);
136 false_value = false_block->GetFirstInstruction()->InputAt(0);
137 } else if (phi != nullptr) {
138 true_value = phi->InputAt(predecessor_index_true);
139 false_value = phi->InputAt(predecessor_index_false);
140 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +0000141 continue;
142 }
Mads Ager16e52892017-07-14 13:11:37 +0200143 DCHECK(both_successors_return || phi != nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +0000144
145 // Create the Select instruction and insert it in front of the If.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100146 HSelect* select = new (graph_->GetAllocator()) HSelect(if_instruction->InputAt(0),
147 true_value,
148 false_value,
149 if_instruction->GetDexPc());
Mads Ager16e52892017-07-14 13:11:37 +0200150 if (both_successors_return) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100151 if (true_value->GetType() == DataType::Type::kReference) {
152 DCHECK(false_value->GetType() == DataType::Type::kReference);
Mads Ager16e52892017-07-14 13:11:37 +0200153 ReferenceTypePropagation::FixUpInstructionType(select, handle_scope_);
154 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100155 } else if (phi->GetType() == DataType::Type::kReference) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000156 select->SetReferenceTypeInfo(phi->GetReferenceTypeInfo());
157 }
158 block->InsertInstructionBefore(select, if_instruction);
159
Mads Ager16e52892017-07-14 13:11:37 +0200160 // Remove the true branch which removes the corresponding Phi
161 // input if needed. If left only with the false branch, the Phi is
162 // automatically removed.
163 if (both_successors_return) {
164 false_block->GetFirstInstruction()->ReplaceInput(select, 0);
165 } else {
166 phi->ReplaceInput(select, predecessor_index_false);
167 }
168
David Brazdil74eb1b22015-12-14 11:44:01 +0000169 bool only_two_predecessors = (merge_block->GetPredecessors().size() == 2u);
170 true_block->DisconnectAndDelete();
David Brazdil74eb1b22015-12-14 11:44:01 +0000171
172 // Merge remaining blocks which are now connected with Goto.
173 DCHECK_EQ(block->GetSingleSuccessor(), false_block);
174 block->MergeWith(false_block);
Mads Ager16e52892017-07-14 13:11:37 +0200175 if (!both_successors_return && only_two_predecessors) {
176 DCHECK_EQ(only_two_predecessors, phi->GetBlock() == nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +0000177 DCHECK_EQ(block->GetSingleSuccessor(), merge_block);
178 block->MergeWith(merge_block);
179 }
180
Igor Murashkin1e065a52017-08-09 13:20:34 -0700181 MaybeRecordStat(stats_, MethodCompilationStat::kSelectGenerated);
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +0100182
David Brazdil74eb1b22015-12-14 11:44:01 +0000183 // No need to update dominance information, as we are simplifying
184 // a simple diamond shape, where the join block is merged with the
185 // entry block. Any following blocks would have had the join block
186 // as a dominator, and `MergeWith` handles changing that to the
187 // entry block.
188 }
189}
190
191} // namespace art