blob: fb11d763206587df7347707e6a096d0f459c5b06 [file] [log] [blame]
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001/*
2 * Copyright (C) 2014 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 "ssa_builder.h"
Nicolas Geoffray184d6402014-06-09 14:06:02 +010018
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010019#include "nodes.h"
Calin Juravle10e244f2015-01-26 18:54:32 +000020#include "primitive_type_propagation.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000021#include "ssa_phi_elimination.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010022
23namespace art {
24
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000025/**
26 * A debuggable application may require to reviving phis, to ensure their
27 * associated DEX register is available to a debugger. This class implements
28 * the logic for statement (c) of the SsaBuilder (see ssa_builder.h). It
29 * also makes sure that phis with incompatible input types are not revived
30 * (statement (b) of the SsaBuilder).
31 *
32 * This phase must be run after detecting dead phis through the
33 * DeadPhiElimination phase, and before deleting the dead phis.
34 */
35class DeadPhiHandling : public ValueObject {
36 public:
37 explicit DeadPhiHandling(HGraph* graph)
Vladimir Marko71bf8092015-09-15 15:33:14 +010038 : graph_(graph), worklist_(graph->GetArena()->Adapter(kArenaAllocSsaBuilder)) {
39 worklist_.reserve(kDefaultWorklistSize);
40 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000041
42 void Run();
43
44 private:
45 void VisitBasicBlock(HBasicBlock* block);
46 void ProcessWorklist();
47 void AddToWorklist(HPhi* phi);
48 void AddDependentInstructionsToWorklist(HPhi* phi);
49 bool UpdateType(HPhi* phi);
50
51 HGraph* const graph_;
Vladimir Marko71bf8092015-09-15 15:33:14 +010052 ArenaVector<HPhi*> worklist_;
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000053
54 static constexpr size_t kDefaultWorklistSize = 8;
55
56 DISALLOW_COPY_AND_ASSIGN(DeadPhiHandling);
57};
58
59bool DeadPhiHandling::UpdateType(HPhi* phi) {
David Brazdilb7013152015-09-17 16:47:21 +010060 if (phi->IsDead()) {
61 // Phi was rendered dead while waiting in the worklist because it was replaced
62 // with an equivalent.
63 return false;
64 }
65
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000066 Primitive::Type existing = phi->GetType();
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000067
68 bool conflict = false;
69 Primitive::Type new_type = existing;
70 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
71 HInstruction* input = phi->InputAt(i);
72 if (input->IsPhi() && input->AsPhi()->IsDead()) {
73 // We are doing a reverse post order visit of the graph, reviving
74 // phis that have environment uses and updating their types. If an
75 // input is a phi, and it is dead (because its input types are
76 // conflicting), this phi must be marked dead as well.
77 conflict = true;
78 break;
79 }
80 Primitive::Type input_type = HPhi::ToPhiType(input->GetType());
81
82 // The only acceptable transitions are:
83 // - From void to typed: first time we update the type of this phi.
84 // - From int to reference (or reference to int): the phi has to change
85 // to reference type. If the integer input cannot be converted to a
86 // reference input, the phi will remain dead.
87 if (new_type == Primitive::kPrimVoid) {
88 new_type = input_type;
89 } else if (new_type == Primitive::kPrimNot && input_type == Primitive::kPrimInt) {
90 HInstruction* equivalent = SsaBuilder::GetReferenceTypeEquivalent(input);
91 if (equivalent == nullptr) {
92 conflict = true;
93 break;
94 } else {
95 phi->ReplaceInput(equivalent, i);
96 if (equivalent->IsPhi()) {
97 DCHECK_EQ(equivalent->GetType(), Primitive::kPrimNot);
98 // We created a new phi, but that phi has the same inputs as the old phi. We
99 // add it to the worklist to ensure its inputs can also be converted to reference.
100 // If not, it will remain dead, and the algorithm will make the current phi dead
101 // as well.
102 equivalent->AsPhi()->SetLive();
103 AddToWorklist(equivalent->AsPhi());
104 }
105 }
106 } else if (new_type == Primitive::kPrimInt && input_type == Primitive::kPrimNot) {
107 new_type = Primitive::kPrimNot;
108 // Start over, we may request reference equivalents for the inputs of the phi.
109 i = -1;
110 } else if (new_type != input_type) {
111 conflict = true;
112 break;
113 }
114 }
115
116 if (conflict) {
117 phi->SetType(Primitive::kPrimVoid);
118 phi->SetDead();
119 return true;
David Brazdilb7013152015-09-17 16:47:21 +0100120 } else if (existing == new_type) {
121 return false;
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000122 }
David Brazdilb7013152015-09-17 16:47:21 +0100123
124 DCHECK(phi->IsLive());
125 phi->SetType(new_type);
126
127 // There might exist a `new_type` equivalent of `phi` already. In that case,
128 // we replace the equivalent with the, now live, `phi`.
129 HPhi* equivalent = phi->GetNextEquivalentPhiWithSameType();
130 if (equivalent != nullptr) {
131 // There cannot be more than two equivalents with the same type.
132 DCHECK(equivalent->GetNextEquivalentPhiWithSameType() == nullptr);
133 // If doing fix-point iteration, the equivalent might be in `worklist_`.
134 // Setting it dead will make UpdateType skip it.
135 equivalent->SetDead();
136 equivalent->ReplaceWith(phi);
137 }
138
139 return true;
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000140}
141
142void DeadPhiHandling::VisitBasicBlock(HBasicBlock* block) {
143 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
144 HPhi* phi = it.Current()->AsPhi();
145 if (phi->IsDead() && phi->HasEnvironmentUses()) {
146 phi->SetLive();
147 if (block->IsLoopHeader()) {
David Brazdil1d0a03c2015-09-28 14:11:09 +0100148 // Give a type to the loop phi to guarantee convergence of the algorithm.
149 // Note that the dead phi may already have a type if it is an equivalent
150 // generated for a typed LoadLocal. In that case we do not change the
151 // type because it could lead to an unsupported PrimNot/Float/Double ->
152 // PrimInt/Long transition and create same type equivalents.
153 if (phi->GetType() == Primitive::kPrimVoid) {
154 phi->SetType(phi->InputAt(0)->GetType());
155 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000156 AddToWorklist(phi);
157 } else {
158 // Because we are doing a reverse post order visit, all inputs of
159 // this phi have been visited and therefore had their (initial) type set.
160 UpdateType(phi);
161 }
162 }
163 }
164}
165
166void DeadPhiHandling::ProcessWorklist() {
Vladimir Marko71bf8092015-09-15 15:33:14 +0100167 while (!worklist_.empty()) {
168 HPhi* instruction = worklist_.back();
169 worklist_.pop_back();
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000170 // Note that the same equivalent phi can be added multiple times in the work list, if
171 // used by multiple phis. The first call to `UpdateType` will know whether the phi is
172 // dead or live.
173 if (instruction->IsLive() && UpdateType(instruction)) {
174 AddDependentInstructionsToWorklist(instruction);
175 }
176 }
177}
178
179void DeadPhiHandling::AddToWorklist(HPhi* instruction) {
180 DCHECK(instruction->IsLive());
Vladimir Marko71bf8092015-09-15 15:33:14 +0100181 worklist_.push_back(instruction);
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000182}
183
184void DeadPhiHandling::AddDependentInstructionsToWorklist(HPhi* instruction) {
185 for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
186 HPhi* phi = it.Current()->GetUser()->AsPhi();
187 if (phi != nullptr && !phi->IsDead()) {
188 AddToWorklist(phi);
189 }
190 }
191}
192
193void DeadPhiHandling::Run() {
194 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
195 VisitBasicBlock(it.Current());
196 }
197 ProcessWorklist();
198}
199
Calin Juravlea4f88312015-04-16 12:57:19 +0100200void SsaBuilder::FixNullConstantType() {
201 // The order doesn't matter here.
202 for (HReversePostOrderIterator itb(*GetGraph()); !itb.Done(); itb.Advance()) {
203 for (HInstructionIterator it(itb.Current()->GetInstructions()); !it.Done(); it.Advance()) {
204 HInstruction* equality_instr = it.Current();
205 if (!equality_instr->IsEqual() && !equality_instr->IsNotEqual()) {
206 continue;
207 }
208 HInstruction* left = equality_instr->InputAt(0);
209 HInstruction* right = equality_instr->InputAt(1);
Nicolas Geoffray51d400d2015-06-15 09:01:08 +0100210 HInstruction* int_operand = nullptr;
Calin Juravlea4f88312015-04-16 12:57:19 +0100211
Nicolas Geoffray51d400d2015-06-15 09:01:08 +0100212 if ((left->GetType() == Primitive::kPrimNot) && (right->GetType() == Primitive::kPrimInt)) {
213 int_operand = right;
214 } else if ((right->GetType() == Primitive::kPrimNot)
215 && (left->GetType() == Primitive::kPrimInt)) {
216 int_operand = left;
Calin Juravlea4f88312015-04-16 12:57:19 +0100217 } else {
218 continue;
219 }
220
221 // If we got here, we are comparing against a reference and the int constant
222 // should be replaced with a null constant.
Nicolas Geoffray51d400d2015-06-15 09:01:08 +0100223 // Both type propagation and redundant phi elimination ensure `int_operand`
224 // can only be the 0 constant.
225 DCHECK(int_operand->IsIntConstant());
226 DCHECK_EQ(0, int_operand->AsIntConstant()->GetValue());
227 equality_instr->ReplaceInput(GetGraph()->GetNullConstant(), int_operand == right ? 1 : 0);
Calin Juravlea4f88312015-04-16 12:57:19 +0100228 }
229 }
230}
231
232void SsaBuilder::EquivalentPhisCleanup() {
233 // The order doesn't matter here.
234 for (HReversePostOrderIterator itb(*GetGraph()); !itb.Done(); itb.Advance()) {
235 for (HInstructionIterator it(itb.Current()->GetPhis()); !it.Done(); it.Advance()) {
236 HPhi* phi = it.Current()->AsPhi();
237 HPhi* next = phi->GetNextEquivalentPhiWithSameType();
238 if (next != nullptr) {
Nicolas Geoffray4230e182015-06-29 14:34:46 +0100239 // Make sure we do not replace a live phi with a dead phi. A live phi has been
240 // handled by the type propagation phase, unlike a dead phi.
241 if (next->IsLive()) {
242 phi->ReplaceWith(next);
243 } else {
244 next->ReplaceWith(phi);
245 }
Calin Juravlea4f88312015-04-16 12:57:19 +0100246 DCHECK(next->GetNextEquivalentPhiWithSameType() == nullptr)
247 << "More then one phi equivalent with type " << phi->GetType()
248 << " found for phi" << phi->GetId();
249 }
250 }
251 }
252}
253
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100254void SsaBuilder::BuildSsa() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100255 // 1) Visit in reverse post order. We need to have all predecessors of a block visited
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100256 // (with the exception of loops) in order to create the right environment for that
257 // block. For loops, we create phis whose inputs will be set in 2).
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100258 for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) {
259 VisitBasicBlock(it.Current());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100260 }
261
262 // 2) Set inputs of loop phis.
Vladimir Marko71bf8092015-09-15 15:33:14 +0100263 for (HBasicBlock* block : loop_headers_) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100264 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100265 HPhi* phi = it.Current()->AsPhi();
Vladimir Marko60584552015-09-03 13:35:12 +0000266 for (HBasicBlock* predecessor : block->GetPredecessors()) {
267 HInstruction* input = ValueOfLocal(predecessor, phi->GetRegNumber());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100268 phi->AddInput(input);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100269 }
270 }
271 }
272
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000273 // 3) Mark dead phis. This will mark phis that are only used by environments:
Nicolas Geoffray31596742014-11-24 15:28:45 +0000274 // at the DEX level, the type of these phis does not need to be consistent, but
275 // our code generator will complain if the inputs of a phi do not have the same
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000276 // type. The marking allows the type propagation to know which phis it needs
277 // to handle. We mark but do not eliminate: the elimination will be done in
Nicolas Geoffrayb59dba02015-03-11 18:13:21 +0000278 // step 9).
279 SsaDeadPhiElimination dead_phis_for_type_propagation(GetGraph());
280 dead_phis_for_type_propagation.MarkDeadPhis();
Nicolas Geoffray31596742014-11-24 15:28:45 +0000281
282 // 4) Propagate types of phis. At this point, phis are typed void in the general
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000283 // case, or float/double/reference when we created an equivalent phi. So we
Nicolas Geoffray31596742014-11-24 15:28:45 +0000284 // need to propagate the types across phis to give them a correct type.
Calin Juravle10e244f2015-01-26 18:54:32 +0000285 PrimitiveTypePropagation type_propagation(GetGraph());
Nicolas Geoffray184d6402014-06-09 14:06:02 +0100286 type_propagation.Run();
287
Nicolas Geoffray51d400d2015-06-15 09:01:08 +0100288 // 5) When creating equivalent phis we copy the inputs of the original phi which
289 // may be improperly typed. This was fixed during the type propagation in 4) but
Calin Juravlea4f88312015-04-16 12:57:19 +0100290 // as a result we may end up with two equivalent phis with the same type for
291 // the same dex register. This pass cleans them up.
292 EquivalentPhisCleanup();
293
Nicolas Geoffray51d400d2015-06-15 09:01:08 +0100294 // 6) Mark dead phis again. Step 4) may have introduced new phis.
295 // Step 5) might enable the death of new phis.
Nicolas Geoffrayb59dba02015-03-11 18:13:21 +0000296 SsaDeadPhiElimination dead_phis(GetGraph());
297 dead_phis.MarkDeadPhis();
298
Nicolas Geoffray51d400d2015-06-15 09:01:08 +0100299 // 7) Now that the graph is correctly typed, we can get rid of redundant phis.
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000300 // Note that we cannot do this phase before type propagation, otherwise
301 // we could get rid of phi equivalents, whose presence is a requirement for the
302 // type propagation phase. Note that this is to satisfy statement (a) of the
303 // SsaBuilder (see ssa_builder.h).
304 SsaRedundantPhiElimination redundant_phi(GetGraph());
305 redundant_phi.Run();
306
Nicolas Geoffray51d400d2015-06-15 09:01:08 +0100307 // 8) Fix the type for null constants which are part of an equality comparison.
308 // We need to do this after redundant phi elimination, to ensure the only cases
309 // that we can see are reference comparison against 0. The redundant phi
310 // elimination ensures we do not see a phi taking two 0 constants in a HEqual
311 // or HNotEqual.
312 FixNullConstantType();
313
Calin Juravlea4f88312015-04-16 12:57:19 +0100314 // 9) Make sure environments use the right phi "equivalent": a phi marked dead
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000315 // can have a phi equivalent that is not dead. We must therefore update
316 // all environment uses of the dead phi to use its equivalent. Note that there
317 // can be multiple phis for the same Dex register that are live (for example
318 // when merging constants), in which case it is OK for the environments
319 // to just reference one.
320 for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) {
321 HBasicBlock* block = it.Current();
322 for (HInstructionIterator it_phis(block->GetPhis()); !it_phis.Done(); it_phis.Advance()) {
323 HPhi* phi = it_phis.Current()->AsPhi();
324 // If the phi is not dead, or has no environment uses, there is nothing to do.
325 if (!phi->IsDead() || !phi->HasEnvironmentUses()) continue;
326 HInstruction* next = phi->GetNext();
David Brazdild0180f92015-09-22 14:39:58 +0100327 if (!phi->IsVRegEquivalentOf(next)) continue;
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000328 if (next->AsPhi()->IsDead()) {
329 // If the phi equivalent is dead, check if there is another one.
330 next = next->GetNext();
David Brazdild0180f92015-09-22 14:39:58 +0100331 if (!phi->IsVRegEquivalentOf(next)) continue;
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000332 // There can be at most two phi equivalents.
David Brazdild0180f92015-09-22 14:39:58 +0100333 DCHECK(!phi->IsVRegEquivalentOf(next->GetNext()));
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000334 if (next->AsPhi()->IsDead()) continue;
335 }
336 // We found a live phi equivalent. Update the environment uses of `phi` with it.
337 phi->ReplaceWith(next);
338 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000339 }
340
Calin Juravlea4f88312015-04-16 12:57:19 +0100341 // 10) Deal with phis to guarantee liveness of phis in case of a debuggable
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000342 // application. This is for satisfying statement (c) of the SsaBuilder
343 // (see ssa_builder.h).
344 if (GetGraph()->IsDebuggable()) {
345 DeadPhiHandling dead_phi_handler(GetGraph());
346 dead_phi_handler.Run();
347 }
348
Calin Juravlea4f88312015-04-16 12:57:19 +0100349 // 11) Now that the right phis are used for the environments, and we
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000350 // have potentially revive dead phis in case of a debuggable application,
351 // we can eliminate phis we do not need. Regardless of the debuggable status,
352 // this phase is necessary for statement (b) of the SsaBuilder (see ssa_builder.h),
353 // as well as for the code generation, which does not deal with phis of conflicting
354 // input types.
355 dead_phis.EliminateDeadPhis();
356
Calin Juravlea4f88312015-04-16 12:57:19 +0100357 // 12) Clear locals.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100358 for (HInstructionIterator it(GetGraph()->GetEntryBlock()->GetInstructions());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100359 !it.Done();
360 it.Advance()) {
361 HInstruction* current = it.Current();
Roland Levillain476df552014-10-09 17:51:36 +0100362 if (current->IsLocal()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100363 current->GetBlock()->RemoveInstruction(current);
364 }
365 }
366}
367
David Brazdileead0712015-09-18 14:58:57 +0100368ArenaVector<HInstruction*>* SsaBuilder::GetLocalsFor(HBasicBlock* block) {
369 DCHECK_LT(block->GetBlockId(), locals_for_.size());
370 ArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
371 const size_t vregs = GetGraph()->GetNumberOfVRegs();
372 if (locals->empty() && vregs != 0u) {
373 locals->resize(vregs, nullptr);
374
375 if (block->IsCatchBlock()) {
376 ArenaAllocator* arena = GetGraph()->GetArena();
377 // We record incoming inputs of catch phis at throwing instructions and
378 // must therefore eagerly create the phis. Phis for undefined vregs will
379 // be deleted when the first throwing instruction with the vreg undefined
380 // is encountered. Unused phis will be removed by dead phi analysis.
381 for (size_t i = 0; i < vregs; ++i) {
382 // No point in creating the catch phi if it is already undefined at
383 // the first throwing instruction.
384 if ((*current_locals_)[i] != nullptr) {
385 HPhi* phi = new (arena) HPhi(arena, i, 0, Primitive::kPrimVoid);
386 block->AddPhi(phi);
387 (*locals)[i] = phi;
388 }
389 }
390 }
391 }
392 return locals;
393}
394
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100395HInstruction* SsaBuilder::ValueOfLocal(HBasicBlock* block, size_t local) {
Vladimir Marko71bf8092015-09-15 15:33:14 +0100396 ArenaVector<HInstruction*>* locals = GetLocalsFor(block);
397 DCHECK_LT(local, locals->size());
398 return (*locals)[local];
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100399}
400
401void SsaBuilder::VisitBasicBlock(HBasicBlock* block) {
402 current_locals_ = GetLocalsFor(block);
403
David Brazdilffee3d32015-07-06 11:48:53 +0100404 if (block->IsCatchBlock()) {
405 // Catch phis were already created and inputs collected from throwing sites.
David Brazdild0180f92015-09-22 14:39:58 +0100406 if (kIsDebugBuild) {
407 // Make sure there was at least one throwing instruction which initialized
408 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
409 // visited already (from HTryBoundary scoping and reverse post order).
410 bool throwing_instruction_found = false;
411 bool catch_block_visited = false;
412 for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) {
413 HBasicBlock* current = it.Current();
414 if (current == block) {
415 catch_block_visited = true;
416 } else if (current->IsTryBlock() &&
417 current->GetTryCatchInformation()->GetTryEntry().HasExceptionHandler(*block)) {
418 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
419 throwing_instruction_found |= current->HasThrowingInstructions();
420 }
421 }
422 DCHECK(throwing_instruction_found) << "No instructions throwing into a live catch block.";
423 }
David Brazdilffee3d32015-07-06 11:48:53 +0100424 } else if (block->IsLoopHeader()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100425 // If the block is a loop header, we know we only have visited the pre header
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100426 // because we are visiting in reverse post order. We create phis for all initialized
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100427 // locals from the pre header. Their inputs will be populated at the end of
428 // the analysis.
Vladimir Marko71bf8092015-09-15 15:33:14 +0100429 for (size_t local = 0; local < current_locals_->size(); ++local) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100430 HInstruction* incoming = ValueOfLocal(block->GetLoopInformation()->GetPreHeader(), local);
431 if (incoming != nullptr) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100432 HPhi* phi = new (GetGraph()->GetArena()) HPhi(
433 GetGraph()->GetArena(), local, 0, Primitive::kPrimVoid);
434 block->AddPhi(phi);
Vladimir Marko71bf8092015-09-15 15:33:14 +0100435 (*current_locals_)[local] = phi;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100436 }
437 }
438 // Save the loop header so that the last phase of the analysis knows which
439 // blocks need to be updated.
Vladimir Marko71bf8092015-09-15 15:33:14 +0100440 loop_headers_.push_back(block);
Vladimir Marko60584552015-09-03 13:35:12 +0000441 } else if (block->GetPredecessors().size() > 0) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100442 // All predecessors have already been visited because we are visiting in reverse post order.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100443 // We merge the values of all locals, creating phis if those values differ.
Vladimir Marko71bf8092015-09-15 15:33:14 +0100444 for (size_t local = 0; local < current_locals_->size(); ++local) {
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +0100445 bool one_predecessor_has_no_value = false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100446 bool is_different = false;
Vladimir Marko60584552015-09-03 13:35:12 +0000447 HInstruction* value = ValueOfLocal(block->GetPredecessor(0), local);
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +0100448
Vladimir Marko60584552015-09-03 13:35:12 +0000449 for (HBasicBlock* predecessor : block->GetPredecessors()) {
450 HInstruction* current = ValueOfLocal(predecessor, local);
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +0100451 if (current == nullptr) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100452 one_predecessor_has_no_value = true;
453 break;
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +0100454 } else if (current != value) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100455 is_different = true;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100456 }
457 }
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +0100458
459 if (one_predecessor_has_no_value) {
460 // If one predecessor has no value for this local, we trust the verifier has
461 // successfully checked that there is a store dominating any read after this block.
462 continue;
463 }
464
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100465 if (is_different) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100466 HPhi* phi = new (GetGraph()->GetArena()) HPhi(
Vladimir Marko60584552015-09-03 13:35:12 +0000467 GetGraph()->GetArena(), local, block->GetPredecessors().size(), Primitive::kPrimVoid);
468 for (size_t i = 0; i < block->GetPredecessors().size(); i++) {
469 HInstruction* pred_value = ValueOfLocal(block->GetPredecessor(i), local);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800470 phi->SetRawInputAt(i, pred_value);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 }
472 block->AddPhi(phi);
473 value = phi;
474 }
Vladimir Marko71bf8092015-09-15 15:33:14 +0100475 (*current_locals_)[local] = value;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100476 }
477 }
478
479 // Visit all instructions. The instructions of interest are:
480 // - HLoadLocal: replace them with the current value of the local.
481 // - HStoreLocal: update current value of the local and remove the instruction.
482 // - Instructions that require an environment: populate their environment
483 // with the current values of the locals.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100484 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100485 it.Current()->Accept(this);
486 }
487}
488
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100489/**
490 * Constants in the Dex format are not typed. So the builder types them as
491 * integers, but when doing the SSA form, we might realize the constant
492 * is used for floating point operations. We create a floating-point equivalent
493 * constant to make the operations correctly typed.
494 */
David Brazdil8d5b8b22015-03-24 10:51:52 +0000495HFloatConstant* SsaBuilder::GetFloatEquivalent(HIntConstant* constant) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100496 // We place the floating point constant next to this constant.
497 HFloatConstant* result = constant->GetNext()->AsFloatConstant();
498 if (result == nullptr) {
499 HGraph* graph = constant->GetBlock()->GetGraph();
500 ArenaAllocator* allocator = graph->GetArena();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000501 result = new (allocator) HFloatConstant(bit_cast<float, int32_t>(constant->GetValue()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100502 constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000503 graph->CacheFloatConstant(result);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100504 } else {
505 // If there is already a constant with the expected type, we know it is
506 // the floating point equivalent of this constant.
Roland Levillainda4d79b2015-03-24 14:36:11 +0000507 DCHECK_EQ((bit_cast<int32_t, float>(result->GetValue())), constant->GetValue());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100508 }
509 return result;
510}
511
512/**
513 * Wide constants in the Dex format are not typed. So the builder types them as
514 * longs, but when doing the SSA form, we might realize the constant
515 * is used for floating point operations. We create a floating-point equivalent
516 * constant to make the operations correctly typed.
517 */
David Brazdil8d5b8b22015-03-24 10:51:52 +0000518HDoubleConstant* SsaBuilder::GetDoubleEquivalent(HLongConstant* constant) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100519 // We place the floating point constant next to this constant.
520 HDoubleConstant* result = constant->GetNext()->AsDoubleConstant();
521 if (result == nullptr) {
522 HGraph* graph = constant->GetBlock()->GetGraph();
523 ArenaAllocator* allocator = graph->GetArena();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000524 result = new (allocator) HDoubleConstant(bit_cast<double, int64_t>(constant->GetValue()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100525 constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext());
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000526 graph->CacheDoubleConstant(result);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100527 } else {
528 // If there is already a constant with the expected type, we know it is
529 // the floating point equivalent of this constant.
Roland Levillainda4d79b2015-03-24 14:36:11 +0000530 DCHECK_EQ((bit_cast<int64_t, double>(result->GetValue())), constant->GetValue());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100531 }
532 return result;
533}
534
535/**
536 * Because of Dex format, we might end up having the same phi being
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000537 * used for non floating point operations and floating point / reference operations.
538 * Because we want the graph to be correctly typed (and thereafter avoid moves between
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100539 * floating point registers and core registers), we need to create a copy of the
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000540 * phi with a floating point / reference type.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100541 */
David Brazdil8d5b8b22015-03-24 10:51:52 +0000542HPhi* SsaBuilder::GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000543 // We place the floating point /reference phi next to this phi.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100544 HInstruction* next = phi->GetNext();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000545 if (next != nullptr
546 && next->AsPhi()->GetRegNumber() == phi->GetRegNumber()
547 && next->GetType() != type) {
548 // Move to the next phi to see if it is the one we are looking for.
549 next = next->GetNext();
550 }
551
552 if (next == nullptr
553 || (next->AsPhi()->GetRegNumber() != phi->GetRegNumber())
554 || (next->GetType() != type)) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100555 ArenaAllocator* allocator = phi->GetBlock()->GetGraph()->GetArena();
556 HPhi* new_phi = new (allocator) HPhi(allocator, phi->GetRegNumber(), phi->InputCount(), type);
557 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
558 // Copy the inputs. Note that the graph may not be correctly typed by doing this copy,
559 // but the type propagation phase will fix it.
560 new_phi->SetRawInputAt(i, phi->InputAt(i));
561 }
562 phi->GetBlock()->InsertPhiAfter(new_phi, phi);
563 return new_phi;
564 } else {
Nicolas Geoffray21cc7982014-11-17 17:50:33 +0000565 DCHECK_EQ(next->GetType(), type);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100566 return next->AsPhi();
567 }
568}
569
570HInstruction* SsaBuilder::GetFloatOrDoubleEquivalent(HInstruction* user,
571 HInstruction* value,
572 Primitive::Type type) {
573 if (value->IsArrayGet()) {
574 // The verifier has checked that values in arrays cannot be used for both
575 // floating point and non-floating point operations. It is therefore safe to just
576 // change the type of the operation.
577 value->AsArrayGet()->SetType(type);
578 return value;
579 } else if (value->IsLongConstant()) {
580 return GetDoubleEquivalent(value->AsLongConstant());
581 } else if (value->IsIntConstant()) {
582 return GetFloatEquivalent(value->AsIntConstant());
583 } else if (value->IsPhi()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000584 return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), type);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100585 } else {
586 // For other instructions, we assume the verifier has checked that the dex format is correctly
587 // typed and the value in a dex register will not be used for both floating point and
588 // non-floating point operations. So the only reason an instruction would want a floating
589 // point equivalent is for an unused phi that will be removed by the dead phi elimination phase.
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100590 DCHECK(user->IsPhi()) << "is actually " << user->DebugName() << " (" << user->GetId() << ")";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100591 return value;
592 }
593}
594
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000595HInstruction* SsaBuilder::GetReferenceTypeEquivalent(HInstruction* value) {
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000596 if (value->IsIntConstant() && value->AsIntConstant()->GetValue() == 0) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000597 return value->GetBlock()->GetGraph()->GetNullConstant();
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000598 } else if (value->IsPhi()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000599 return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), Primitive::kPrimNot);
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000600 } else {
601 return nullptr;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000602 }
603}
604
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100605void SsaBuilder::VisitLoadLocal(HLoadLocal* load) {
Vladimir Marko71bf8092015-09-15 15:33:14 +0100606 DCHECK_LT(load->GetLocal()->GetRegNumber(), current_locals_->size());
607 HInstruction* value = (*current_locals_)[load->GetLocal()->GetRegNumber()];
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000608 // If the operation requests a specific type, we make sure its input is of that type.
609 if (load->GetType() != value->GetType()) {
610 if (load->GetType() == Primitive::kPrimFloat || load->GetType() == Primitive::kPrimDouble) {
611 value = GetFloatOrDoubleEquivalent(load, value, load->GetType());
612 } else if (load->GetType() == Primitive::kPrimNot) {
613 value = GetReferenceTypeEquivalent(value);
614 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100615 }
616 load->ReplaceWith(value);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100617 load->GetBlock()->RemoveInstruction(load);
618}
619
620void SsaBuilder::VisitStoreLocal(HStoreLocal* store) {
Vladimir Marko71bf8092015-09-15 15:33:14 +0100621 DCHECK_LT(store->GetLocal()->GetRegNumber(), current_locals_->size());
622 (*current_locals_)[store->GetLocal()->GetRegNumber()] = store->InputAt(1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100623 store->GetBlock()->RemoveInstruction(store);
624}
625
626void SsaBuilder::VisitInstruction(HInstruction* instruction) {
David Brazdilffee3d32015-07-06 11:48:53 +0100627 if (instruction->NeedsEnvironment()) {
628 HEnvironment* environment = new (GetGraph()->GetArena()) HEnvironment(
629 GetGraph()->GetArena(),
Vladimir Marko71bf8092015-09-15 15:33:14 +0100630 current_locals_->size(),
David Brazdilffee3d32015-07-06 11:48:53 +0100631 GetGraph()->GetDexFile(),
632 GetGraph()->GetMethodIdx(),
633 instruction->GetDexPc(),
634 GetGraph()->GetInvokeType(),
635 instruction);
636 environment->CopyFrom(*current_locals_);
637 instruction->SetRawEnvironment(environment);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100638 }
David Brazdilffee3d32015-07-06 11:48:53 +0100639
640 // If in a try block, propagate values of locals into catch blocks.
David Brazdilec16f792015-08-19 15:04:01 +0100641 if (instruction->CanThrowIntoCatchBlock()) {
642 const HTryBoundary& try_entry =
643 instruction->GetBlock()->GetTryCatchInformation()->GetTryEntry();
644 for (HExceptionHandlerIterator it(try_entry); !it.Done(); it.Advance()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100645 HBasicBlock* catch_block = it.Current();
646 ArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
Vladimir Marko71bf8092015-09-15 15:33:14 +0100647 DCHECK_EQ(handler_locals->size(), current_locals_->size());
David Brazdil3eaa32f2015-09-18 10:58:32 +0100648 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
649 HInstruction* handler_value = (*handler_locals)[vreg];
650 if (handler_value == nullptr) {
651 // Vreg was undefined at a previously encountered throwing instruction
652 // and the catch phi was deleted. Do not record the local value.
653 continue;
654 }
655 DCHECK(handler_value->IsPhi());
656
657 HInstruction* local_value = (*current_locals_)[vreg];
658 if (local_value == nullptr) {
659 // This is the first instruction throwing into `catch_block` where
660 // `vreg` is undefined. Delete the catch phi.
661 catch_block->RemovePhi(handler_value->AsPhi());
662 (*handler_locals)[vreg] = nullptr;
663 } else {
664 // Vreg has been defined at all instructions throwing into `catch_block`
665 // encountered so far. Record the local value in the catch phi.
666 handler_value->AsPhi()->AddInput(local_value);
David Brazdilffee3d32015-07-06 11:48:53 +0100667 }
668 }
669 }
670 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100671}
672
Nicolas Geoffray421e9f92014-11-11 18:21:53 +0000673void SsaBuilder::VisitTemporary(HTemporary* temp) {
674 // Temporaries are only used by the baseline register allocator.
675 temp->GetBlock()->RemoveInstruction(temp);
676}
677
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100678} // namespace art