blob: ae1e3699993d496731af78a7df68c62466a1b2a7 [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
David Brazdil86ea7ee2016-02-16 09:26:07 +000019#include "bytecode_utils.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010020#include "nodes.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000021#include "reference_type_propagation.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000022#include "ssa_phi_elimination.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010023
24namespace art {
25
Calin Juravlea4f88312015-04-16 12:57:19 +010026void SsaBuilder::FixNullConstantType() {
27 // The order doesn't matter here.
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
29 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Calin Juravlea4f88312015-04-16 12:57:19 +010030 HInstruction* equality_instr = it.Current();
31 if (!equality_instr->IsEqual() && !equality_instr->IsNotEqual()) {
32 continue;
33 }
34 HInstruction* left = equality_instr->InputAt(0);
35 HInstruction* right = equality_instr->InputAt(1);
Nicolas Geoffray51d400d2015-06-15 09:01:08 +010036 HInstruction* int_operand = nullptr;
Calin Juravlea4f88312015-04-16 12:57:19 +010037
Nicolas Geoffray51d400d2015-06-15 09:01:08 +010038 if ((left->GetType() == Primitive::kPrimNot) && (right->GetType() == Primitive::kPrimInt)) {
39 int_operand = right;
40 } else if ((right->GetType() == Primitive::kPrimNot)
41 && (left->GetType() == Primitive::kPrimInt)) {
42 int_operand = left;
Calin Juravlea4f88312015-04-16 12:57:19 +010043 } else {
44 continue;
45 }
46
47 // If we got here, we are comparing against a reference and the int constant
48 // should be replaced with a null constant.
Nicolas Geoffray51d400d2015-06-15 09:01:08 +010049 // Both type propagation and redundant phi elimination ensure `int_operand`
50 // can only be the 0 constant.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +000051 DCHECK(int_operand->IsIntConstant()) << int_operand->DebugName();
Nicolas Geoffray51d400d2015-06-15 09:01:08 +010052 DCHECK_EQ(0, int_operand->AsIntConstant()->GetValue());
David Brazdildee58d62016-04-07 09:54:26 +000053 equality_instr->ReplaceInput(graph_->GetNullConstant(), int_operand == right ? 1 : 0);
Calin Juravlea4f88312015-04-16 12:57:19 +010054 }
55 }
56}
57
58void SsaBuilder::EquivalentPhisCleanup() {
59 // The order doesn't matter here.
Vladimir Marko2c45bc92016-10-25 16:54:12 +010060 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
61 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Calin Juravlea4f88312015-04-16 12:57:19 +010062 HPhi* phi = it.Current()->AsPhi();
63 HPhi* next = phi->GetNextEquivalentPhiWithSameType();
64 if (next != nullptr) {
David Brazdil4833f5a2015-12-16 10:37:39 +000065 // Make sure we do not replace a live phi with a dead phi. A live phi
66 // has been handled by the type propagation phase, unlike a dead phi.
Nicolas Geoffray4230e182015-06-29 14:34:46 +010067 if (next->IsLive()) {
68 phi->ReplaceWith(next);
David Brazdil4833f5a2015-12-16 10:37:39 +000069 phi->SetDead();
Nicolas Geoffray4230e182015-06-29 14:34:46 +010070 } else {
71 next->ReplaceWith(phi);
72 }
Calin Juravlea4f88312015-04-16 12:57:19 +010073 DCHECK(next->GetNextEquivalentPhiWithSameType() == nullptr)
74 << "More then one phi equivalent with type " << phi->GetType()
75 << " found for phi" << phi->GetId();
76 }
77 }
78 }
79}
80
David Brazdil4833f5a2015-12-16 10:37:39 +000081void SsaBuilder::FixEnvironmentPhis() {
Vladimir Marko2c45bc92016-10-25 16:54:12 +010082 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000083 for (HInstructionIterator it_phis(block->GetPhis()); !it_phis.Done(); it_phis.Advance()) {
84 HPhi* phi = it_phis.Current()->AsPhi();
85 // If the phi is not dead, or has no environment uses, there is nothing to do.
86 if (!phi->IsDead() || !phi->HasEnvironmentUses()) continue;
87 HInstruction* next = phi->GetNext();
David Brazdild0180f92015-09-22 14:39:58 +010088 if (!phi->IsVRegEquivalentOf(next)) continue;
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000089 if (next->AsPhi()->IsDead()) {
90 // If the phi equivalent is dead, check if there is another one.
91 next = next->GetNext();
David Brazdild0180f92015-09-22 14:39:58 +010092 if (!phi->IsVRegEquivalentOf(next)) continue;
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000093 // There can be at most two phi equivalents.
David Brazdild0180f92015-09-22 14:39:58 +010094 DCHECK(!phi->IsVRegEquivalentOf(next->GetNext()));
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000095 if (next->AsPhi()->IsDead()) continue;
96 }
97 // We found a live phi equivalent. Update the environment uses of `phi` with it.
98 phi->ReplaceWith(next);
99 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000100 }
David Brazdil4833f5a2015-12-16 10:37:39 +0000101}
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000102
David Brazdil4833f5a2015-12-16 10:37:39 +0000103static void AddDependentInstructionsToWorklist(HInstruction* instruction,
104 ArenaVector<HPhi*>* worklist) {
105 // If `instruction` is a dead phi, type conflict was just identified. All its
106 // live phi users, and transitively users of those users, therefore need to be
107 // marked dead/conflicting too, so we add them to the worklist. Otherwise we
108 // add users whose type does not match and needs to be updated.
109 bool add_all_live_phis = instruction->IsPhi() && instruction->AsPhi()->IsDead();
Vladimir Marko46817b82016-03-29 12:21:58 +0100110 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
111 HInstruction* user = use.GetUser();
David Brazdil4833f5a2015-12-16 10:37:39 +0000112 if (user->IsPhi() && user->AsPhi()->IsLive()) {
113 if (add_all_live_phis || user->GetType() != instruction->GetType()) {
114 worklist->push_back(user->AsPhi());
115 }
116 }
117 }
118}
119
120// Find a candidate primitive type for `phi` by merging the type of its inputs.
121// Return false if conflict is identified.
122static bool TypePhiFromInputs(HPhi* phi) {
123 Primitive::Type common_type = phi->GetType();
124
Vladimir Marko372f10e2016-05-17 16:30:10 +0100125 for (HInstruction* input : phi->GetInputs()) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000126 if (input->IsPhi() && input->AsPhi()->IsDead()) {
127 // Phis are constructed live so if an input is a dead phi, it must have
128 // been made dead due to type conflict. Mark this phi conflicting too.
129 return false;
130 }
131
132 Primitive::Type input_type = HPhi::ToPhiType(input->GetType());
133 if (common_type == input_type) {
134 // No change in type.
David Brazdild87f3ea2016-01-04 15:55:10 +0000135 } else if (Primitive::Is64BitType(common_type) != Primitive::Is64BitType(input_type)) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000136 // Types are of different sizes, e.g. int vs. long. Must be a conflict.
137 return false;
138 } else if (Primitive::IsIntegralType(common_type)) {
139 // Previous inputs were integral, this one is not but is of the same size.
140 // This does not imply conflict since some bytecode instruction types are
141 // ambiguous. TypeInputsOfPhi will either type them or detect a conflict.
142 DCHECK(Primitive::IsFloatingPointType(input_type) || input_type == Primitive::kPrimNot);
143 common_type = input_type;
144 } else if (Primitive::IsIntegralType(input_type)) {
145 // Input is integral, common type is not. Same as in the previous case, if
146 // there is a conflict, it will be detected during TypeInputsOfPhi.
147 DCHECK(Primitive::IsFloatingPointType(common_type) || common_type == Primitive::kPrimNot);
148 } else {
149 // Combining float and reference types. Clearly a conflict.
150 DCHECK((common_type == Primitive::kPrimFloat && input_type == Primitive::kPrimNot) ||
151 (common_type == Primitive::kPrimNot && input_type == Primitive::kPrimFloat));
152 return false;
153 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000154 }
155
David Brazdil4833f5a2015-12-16 10:37:39 +0000156 // We have found a candidate type for the phi. Set it and return true. We may
157 // still discover conflict whilst typing the individual inputs in TypeInputsOfPhi.
158 phi->SetType(common_type);
159 return true;
160}
David Brazdild9510df2015-11-04 23:30:22 +0000161
David Brazdil4833f5a2015-12-16 10:37:39 +0000162// Replace inputs of `phi` to match its type. Return false if conflict is identified.
163bool SsaBuilder::TypeInputsOfPhi(HPhi* phi, ArenaVector<HPhi*>* worklist) {
164 Primitive::Type common_type = phi->GetType();
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +0100165 if (Primitive::IsIntegralType(common_type)) {
166 // We do not need to retype ambiguous inputs because they are always constructed
167 // with the integral type candidate.
David Brazdil4833f5a2015-12-16 10:37:39 +0000168 if (kIsDebugBuild) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100169 for (HInstruction* input : phi->GetInputs()) {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +0100170 DCHECK(HPhi::ToPhiType(input->GetType()) == common_type);
David Brazdil4833f5a2015-12-16 10:37:39 +0000171 }
172 }
173 // Inputs did not need to be replaced, hence no conflict. Report success.
174 return true;
175 } else {
176 DCHECK(common_type == Primitive::kPrimNot || Primitive::IsFloatingPointType(common_type));
Vladimir Markoe9004912016-06-16 16:50:52 +0100177 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100178 for (size_t i = 0; i < inputs.size(); ++i) {
179 HInstruction* input = inputs[i];
David Brazdil4833f5a2015-12-16 10:37:39 +0000180 if (input->GetType() != common_type) {
181 // Input type does not match phi's type. Try to retype the input or
182 // generate a suitably typed equivalent.
183 HInstruction* equivalent = (common_type == Primitive::kPrimNot)
184 ? GetReferenceTypeEquivalent(input)
185 : GetFloatOrDoubleEquivalent(input, common_type);
186 if (equivalent == nullptr) {
187 // Input could not be typed. Report conflict.
188 return false;
189 }
190 // Make sure the input did not change its type and we do not need to
191 // update its users.
192 DCHECK_NE(input, equivalent);
193
194 phi->ReplaceInput(equivalent, i);
195 if (equivalent->IsPhi()) {
196 worklist->push_back(equivalent->AsPhi());
197 }
198 }
199 }
200 // All inputs either matched the type of the phi or we successfully replaced
201 // them with a suitable equivalent. Report success.
202 return true;
203 }
204}
205
206// Attempt to set the primitive type of `phi` to match its inputs. Return whether
207// it was changed by the algorithm or not.
208bool SsaBuilder::UpdatePrimitiveType(HPhi* phi, ArenaVector<HPhi*>* worklist) {
209 DCHECK(phi->IsLive());
210 Primitive::Type original_type = phi->GetType();
211
212 // Try to type the phi in two stages:
213 // (1) find a candidate type for the phi by merging types of all its inputs,
214 // (2) try to type the phi's inputs to that candidate type.
215 // Either of these stages may detect a type conflict and fail, in which case
216 // we immediately abort.
217 if (!TypePhiFromInputs(phi) || !TypeInputsOfPhi(phi, worklist)) {
218 // Conflict detected. Mark the phi dead and return true because it changed.
219 phi->SetDead();
220 return true;
221 }
222
223 // Return true if the type of the phi has changed.
224 return phi->GetType() != original_type;
225}
226
227void SsaBuilder::RunPrimitiveTypePropagation() {
Vladimir Marko3ea5a972016-05-09 20:23:34 +0100228 ArenaVector<HPhi*> worklist(graph_->GetArena()->Adapter(kArenaAllocGraphBuilder));
David Brazdil4833f5a2015-12-16 10:37:39 +0000229
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100230 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 if (block->IsLoopHeader()) {
232 for (HInstructionIterator phi_it(block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
233 HPhi* phi = phi_it.Current()->AsPhi();
234 if (phi->IsLive()) {
235 worklist.push_back(phi);
236 }
237 }
238 } else {
239 for (HInstructionIterator phi_it(block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
240 // Eagerly compute the type of the phi, for quicker convergence. Note
241 // that we don't need to add users to the worklist because we are
242 // doing a reverse post-order visit, therefore either the phi users are
243 // non-loop phi and will be visited later in the visit, or are loop-phis,
244 // and they are already in the work list.
245 HPhi* phi = phi_it.Current()->AsPhi();
246 if (phi->IsLive()) {
247 UpdatePrimitiveType(phi, &worklist);
248 }
249 }
250 }
251 }
252
253 ProcessPrimitiveTypePropagationWorklist(&worklist);
254 EquivalentPhisCleanup();
255}
256
257void SsaBuilder::ProcessPrimitiveTypePropagationWorklist(ArenaVector<HPhi*>* worklist) {
258 // Process worklist
259 while (!worklist->empty()) {
260 HPhi* phi = worklist->back();
261 worklist->pop_back();
262 // The phi could have been made dead as a result of conflicts while in the
263 // worklist. If it is now dead, there is no point in updating its type.
264 if (phi->IsLive() && UpdatePrimitiveType(phi, worklist)) {
265 AddDependentInstructionsToWorklist(phi, worklist);
266 }
267 }
268}
269
270static HArrayGet* FindFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) {
271 Primitive::Type type = aget->GetType();
272 DCHECK(Primitive::IsIntOrLongType(type));
David Brazdildee58d62016-04-07 09:54:26 +0000273 HInstruction* next = aget->GetNext();
274 if (next != nullptr && next->IsArrayGet()) {
275 HArrayGet* next_aget = next->AsArrayGet();
276 if (next_aget->IsEquivalentOf(aget)) {
277 return next_aget;
278 }
279 }
280 return nullptr;
David Brazdil4833f5a2015-12-16 10:37:39 +0000281}
282
283static HArrayGet* CreateFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) {
284 Primitive::Type type = aget->GetType();
285 DCHECK(Primitive::IsIntOrLongType(type));
286 DCHECK(FindFloatOrDoubleEquivalentOfArrayGet(aget) == nullptr);
287
288 HArrayGet* equivalent = new (aget->GetBlock()->GetGraph()->GetArena()) HArrayGet(
289 aget->GetArray(),
290 aget->GetIndex(),
291 type == Primitive::kPrimInt ? Primitive::kPrimFloat : Primitive::kPrimDouble,
292 aget->GetDexPc());
293 aget->GetBlock()->InsertInstructionAfter(equivalent, aget);
294 return equivalent;
295}
296
David Brazdil15693bf2015-12-16 10:30:45 +0000297static Primitive::Type GetPrimitiveArrayComponentType(HInstruction* array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700298 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil15693bf2015-12-16 10:30:45 +0000299 ReferenceTypeInfo array_type = array->GetReferenceTypeInfo();
David Brazdil4833f5a2015-12-16 10:37:39 +0000300 DCHECK(array_type.IsPrimitiveArrayClass());
David Brazdil15693bf2015-12-16 10:30:45 +0000301 return array_type.GetTypeHandle()->GetComponentType()->GetPrimitiveType();
David Brazdil4833f5a2015-12-16 10:37:39 +0000302}
303
David Brazdil15693bf2015-12-16 10:30:45 +0000304bool SsaBuilder::FixAmbiguousArrayOps() {
305 if (ambiguous_agets_.empty() && ambiguous_asets_.empty()) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000306 return true;
307 }
308
309 // The wrong ArrayGet equivalent may still have Phi uses coming from ArraySet
310 // uses (because they are untyped) and environment uses (if --debuggable).
311 // After resolving all ambiguous ArrayGets, we will re-run primitive type
312 // propagation on the Phis which need to be updated.
Vladimir Marko3ea5a972016-05-09 20:23:34 +0100313 ArenaVector<HPhi*> worklist(graph_->GetArena()->Adapter(kArenaAllocGraphBuilder));
David Brazdil4833f5a2015-12-16 10:37:39 +0000314
315 {
316 ScopedObjectAccess soa(Thread::Current());
317
318 for (HArrayGet* aget_int : ambiguous_agets_) {
David Brazdil15693bf2015-12-16 10:30:45 +0000319 HInstruction* array = aget_int->GetArray();
320 if (!array->GetReferenceTypeInfo().IsPrimitiveArrayClass()) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000321 // RTP did not type the input array. Bail.
322 return false;
323 }
324
325 HArrayGet* aget_float = FindFloatOrDoubleEquivalentOfArrayGet(aget_int);
David Brazdil15693bf2015-12-16 10:30:45 +0000326 Primitive::Type array_type = GetPrimitiveArrayComponentType(array);
327 DCHECK_EQ(Primitive::Is64BitType(aget_int->GetType()), Primitive::Is64BitType(array_type));
328
329 if (Primitive::IsIntOrLongType(array_type)) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000330 if (aget_float != nullptr) {
331 // There is a float/double equivalent. We must replace it and re-run
332 // primitive type propagation on all dependent instructions.
333 aget_float->ReplaceWith(aget_int);
334 aget_float->GetBlock()->RemoveInstruction(aget_float);
335 AddDependentInstructionsToWorklist(aget_int, &worklist);
336 }
337 } else {
David Brazdil15693bf2015-12-16 10:30:45 +0000338 DCHECK(Primitive::IsFloatingPointType(array_type));
David Brazdil4833f5a2015-12-16 10:37:39 +0000339 if (aget_float == nullptr) {
340 // This is a float/double ArrayGet but there were no typed uses which
341 // would create the typed equivalent. Create it now.
342 aget_float = CreateFloatOrDoubleEquivalentOfArrayGet(aget_int);
343 }
344 // Replace the original int/long instruction. Note that it may have phi
345 // uses, environment uses, as well as real uses (from untyped ArraySets).
346 // We need to re-run primitive type propagation on its dependent instructions.
347 aget_int->ReplaceWith(aget_float);
348 aget_int->GetBlock()->RemoveInstruction(aget_int);
349 AddDependentInstructionsToWorklist(aget_float, &worklist);
350 }
351 }
David Brazdil4833f5a2015-12-16 10:37:39 +0000352
David Brazdil15693bf2015-12-16 10:30:45 +0000353 // Set a flag stating that types of ArrayGets have been resolved. Requesting
354 // equivalent of the wrong type with GetFloatOrDoubleEquivalentOfArrayGet
355 // will fail from now on.
356 agets_fixed_ = true;
357
358 for (HArraySet* aset : ambiguous_asets_) {
359 HInstruction* array = aset->GetArray();
360 if (!array->GetReferenceTypeInfo().IsPrimitiveArrayClass()) {
361 // RTP did not type the input array. Bail.
362 return false;
363 }
364
365 HInstruction* value = aset->GetValue();
366 Primitive::Type value_type = value->GetType();
367 Primitive::Type array_type = GetPrimitiveArrayComponentType(array);
368 DCHECK_EQ(Primitive::Is64BitType(value_type), Primitive::Is64BitType(array_type));
369
370 if (Primitive::IsFloatingPointType(array_type)) {
371 if (!Primitive::IsFloatingPointType(value_type)) {
372 DCHECK(Primitive::IsIntegralType(value_type));
373 // Array elements are floating-point but the value has not been replaced
374 // with its floating-point equivalent. The replacement must always
375 // succeed in code validated by the verifier.
376 HInstruction* equivalent = GetFloatOrDoubleEquivalent(value, array_type);
377 DCHECK(equivalent != nullptr);
378 aset->ReplaceInput(equivalent, /* input_index */ 2);
379 if (equivalent->IsPhi()) {
380 // Returned equivalent is a phi which may not have had its inputs
381 // replaced yet. We need to run primitive type propagation on it.
382 worklist.push_back(equivalent->AsPhi());
383 }
384 }
Aart Bik18b36ab2016-04-13 16:41:35 -0700385 // Refine the side effects of this floating point aset. Note that we do this even if
386 // no replacement occurs, since the right-hand-side may have been corrected already.
387 aset->ComputeSideEffects();
David Brazdil15693bf2015-12-16 10:30:45 +0000388 } else {
389 // Array elements are integral and the value assigned to it initially
390 // was integral too. Nothing to do.
391 DCHECK(Primitive::IsIntegralType(array_type));
392 DCHECK(Primitive::IsIntegralType(value_type));
393 }
394 }
395 }
David Brazdil4833f5a2015-12-16 10:37:39 +0000396
397 if (!worklist.empty()) {
398 ProcessPrimitiveTypePropagationWorklist(&worklist);
399 EquivalentPhisCleanup();
400 }
401
402 return true;
403}
404
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +0000405static bool HasAliasInEnvironments(HInstruction* instruction) {
Vladimir Marko46817b82016-03-29 12:21:58 +0100406 HEnvironment* last_user = nullptr;
407 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
408 DCHECK(use.GetUser() != nullptr);
409 // Note: The first comparison (== null) always fails.
410 if (use.GetUser() == last_user) {
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +0000411 return true;
412 }
Vladimir Marko46817b82016-03-29 12:21:58 +0100413 last_user = use.GetUser();
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +0000414 }
415
416 if (kIsDebugBuild) {
417 // Do a quadratic search to ensure same environment uses are next
418 // to each other.
Vladimir Marko46817b82016-03-29 12:21:58 +0100419 const HUseList<HEnvironment*>& env_uses = instruction->GetEnvUses();
420 for (auto current = env_uses.begin(), end = env_uses.end(); current != end; ++current) {
421 auto next = current;
422 for (++next; next != end; ++next) {
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +0000423 DCHECK(next->GetUser() != current->GetUser());
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +0000424 }
425 }
426 }
427 return false;
428}
429
David Brazdil65902e82016-01-15 09:35:13 +0000430void SsaBuilder::RemoveRedundantUninitializedStrings() {
David Brazdildee58d62016-04-07 09:54:26 +0000431 if (graph_->IsDebuggable()) {
David Brazdil65902e82016-01-15 09:35:13 +0000432 // Do not perform the optimization for consistency with the interpreter
433 // which always allocates an object for new-instance of String.
434 return;
435 }
436
437 for (HNewInstance* new_instance : uninitialized_strings_) {
Aart Bikeda31402016-03-24 15:38:56 -0700438 DCHECK(new_instance->IsInBlock());
David Brazdildee58d62016-04-07 09:54:26 +0000439 DCHECK(new_instance->IsStringAlloc());
440
David Brazdil65902e82016-01-15 09:35:13 +0000441 // Replace NewInstance of String with NullConstant if not used prior to
442 // calling StringFactory. In case of deoptimization, the interpreter is
443 // expected to skip null check on the `this` argument of the StringFactory call.
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +0000444 if (!new_instance->HasNonEnvironmentUses() && !HasAliasInEnvironments(new_instance)) {
David Brazdildee58d62016-04-07 09:54:26 +0000445 new_instance->ReplaceWith(graph_->GetNullConstant());
David Brazdil65902e82016-01-15 09:35:13 +0000446 new_instance->GetBlock()->RemoveInstruction(new_instance);
447
448 // Remove LoadClass if not needed any more.
Nicolas Geoffray5e08e362016-02-15 15:56:11 +0000449 HInstruction* input = new_instance->InputAt(0);
450 HLoadClass* load_class = nullptr;
451
452 // If the class was not present in the dex cache at the point of building
453 // the graph, the builder inserted a HClinitCheck in between. Since the String
454 // class is always initialized at the point of running Java code, we can remove
455 // that check.
456 if (input->IsClinitCheck()) {
457 load_class = input->InputAt(0)->AsLoadClass();
458 input->ReplaceWith(load_class);
459 input->GetBlock()->RemoveInstruction(input);
460 } else {
461 load_class = input->AsLoadClass();
462 DCHECK(new_instance->IsStringAlloc());
463 DCHECK(!load_class->NeedsAccessCheck()) << "String class is always accessible";
464 }
David Brazdil65902e82016-01-15 09:35:13 +0000465 DCHECK(load_class != nullptr);
David Brazdil65902e82016-01-15 09:35:13 +0000466 if (!load_class->HasUses()) {
Nicolas Geoffray5e08e362016-02-15 15:56:11 +0000467 // Even if the HLoadClass needs access check, we can remove it, as we know the
468 // String class does not need it.
David Brazdil65902e82016-01-15 09:35:13 +0000469 load_class->GetBlock()->RemoveInstruction(load_class);
470 }
471 }
472 }
473}
474
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000475GraphAnalysisResult SsaBuilder::BuildSsa() {
David Brazdildee58d62016-04-07 09:54:26 +0000476 DCHECK(!graph_->IsInSsaForm());
David Brazdilbadd8262016-02-02 16:28:56 +0000477
David Brazdildee58d62016-04-07 09:54:26 +0000478 // 1) Propagate types of phis. At this point, phis are typed void in the general
David Brazdil4833f5a2015-12-16 10:37:39 +0000479 // case, or float/double/reference if we created an equivalent phi. So we need
480 // to propagate the types across phis to give them a correct type. If a type
481 // conflict is detected in this stage, the phi is marked dead.
482 RunPrimitiveTypePropagation();
483
David Brazdildee58d62016-04-07 09:54:26 +0000484 // 2) Now that the correct primitive types have been assigned, we can get rid
David Brazdil4833f5a2015-12-16 10:37:39 +0000485 // of redundant phis. Note that we cannot do this phase before type propagation,
486 // otherwise we could get rid of phi equivalents, whose presence is a requirement
487 // for the type propagation phase. Note that this is to satisfy statement (a)
488 // of the SsaBuilder (see ssa_builder.h).
David Brazdildee58d62016-04-07 09:54:26 +0000489 SsaRedundantPhiElimination(graph_).Run();
David Brazdil4833f5a2015-12-16 10:37:39 +0000490
David Brazdildee58d62016-04-07 09:54:26 +0000491 // 3) Fix the type for null constants which are part of an equality comparison.
David Brazdil4833f5a2015-12-16 10:37:39 +0000492 // We need to do this after redundant phi elimination, to ensure the only cases
493 // that we can see are reference comparison against 0. The redundant phi
494 // elimination ensures we do not see a phi taking two 0 constants in a HEqual
495 // or HNotEqual.
496 FixNullConstantType();
497
David Brazdildee58d62016-04-07 09:54:26 +0000498 // 4) Compute type of reference type instructions. The pass assumes that
David Brazdil4833f5a2015-12-16 10:37:39 +0000499 // NullConstant has been fixed up.
Vladimir Marko456307a2016-04-19 14:12:13 +0000500 ReferenceTypePropagation(graph_, dex_cache_, handles_, /* is_first_run */ true).Run();
David Brazdil4833f5a2015-12-16 10:37:39 +0000501
David Brazdildee58d62016-04-07 09:54:26 +0000502 // 5) HInstructionBuilder duplicated ArrayGet instructions with ambiguous type
503 // (int/float or long/double) and marked ArraySets with ambiguous input type.
504 // Now that RTP computed the type of the array input, the ambiguity can be
505 // resolved and the correct equivalents kept.
David Brazdil15693bf2015-12-16 10:30:45 +0000506 if (!FixAmbiguousArrayOps()) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000507 return kAnalysisFailAmbiguousArrayOp;
David Brazdil4833f5a2015-12-16 10:37:39 +0000508 }
509
David Brazdildee58d62016-04-07 09:54:26 +0000510 // 6) Mark dead phis. This will mark phis which are not used by instructions
David Brazdil4833f5a2015-12-16 10:37:39 +0000511 // or other live phis. If compiling as debuggable code, phis will also be kept
512 // live if they have an environment use.
David Brazdildee58d62016-04-07 09:54:26 +0000513 SsaDeadPhiElimination dead_phi_elimimation(graph_);
David Brazdil4833f5a2015-12-16 10:37:39 +0000514 dead_phi_elimimation.MarkDeadPhis();
515
David Brazdildee58d62016-04-07 09:54:26 +0000516 // 7) Make sure environments use the right phi equivalent: a phi marked dead
David Brazdil4833f5a2015-12-16 10:37:39 +0000517 // can have a phi equivalent that is not dead. In that case we have to replace
518 // it with the live equivalent because deoptimization and try/catch rely on
519 // environments containing values of all live vregs at that point. Note that
520 // there can be multiple phis for the same Dex register that are live
521 // (for example when merging constants), in which case it is okay for the
522 // environments to just reference one.
523 FixEnvironmentPhis();
524
David Brazdildee58d62016-04-07 09:54:26 +0000525 // 8) Now that the right phis are used for the environments, we can eliminate
David Brazdil4833f5a2015-12-16 10:37:39 +0000526 // phis we do not need. Regardless of the debuggable status, this phase is
527 /// necessary for statement (b) of the SsaBuilder (see ssa_builder.h), as well
528 // as for the code generation, which does not deal with phis of conflicting
529 // input types.
530 dead_phi_elimimation.EliminateDeadPhis();
531
David Brazdildee58d62016-04-07 09:54:26 +0000532 // 9) HInstructionBuidler replaced uses of NewInstances of String with the
533 // results of their corresponding StringFactory calls. Unless the String
534 // objects are used before they are initialized, they can be replaced with
535 // NullConstant. Note that this optimization is valid only if unsimplified
536 // code does not use the uninitialized value because we assume execution can
537 // be deoptimized at any safepoint. We must therefore perform it before any
538 // other optimizations.
David Brazdil65902e82016-01-15 09:35:13 +0000539 RemoveRedundantUninitializedStrings();
540
David Brazdildee58d62016-04-07 09:54:26 +0000541 graph_->SetInSsaForm();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000542 return kAnalysisSuccess;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100543}
544
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100545/**
546 * Constants in the Dex format are not typed. So the builder types them as
547 * integers, but when doing the SSA form, we might realize the constant
548 * is used for floating point operations. We create a floating-point equivalent
549 * constant to make the operations correctly typed.
550 */
David Brazdil8d5b8b22015-03-24 10:51:52 +0000551HFloatConstant* SsaBuilder::GetFloatEquivalent(HIntConstant* constant) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100552 // We place the floating point constant next to this constant.
553 HFloatConstant* result = constant->GetNext()->AsFloatConstant();
554 if (result == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000555 float value = bit_cast<float, int32_t>(constant->GetValue());
556 result = new (graph_->GetArena()) HFloatConstant(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100557 constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext());
David Brazdildee58d62016-04-07 09:54:26 +0000558 graph_->CacheFloatConstant(result);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100559 } else {
560 // If there is already a constant with the expected type, we know it is
561 // the floating point equivalent of this constant.
Roland Levillainda4d79b2015-03-24 14:36:11 +0000562 DCHECK_EQ((bit_cast<int32_t, float>(result->GetValue())), constant->GetValue());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100563 }
564 return result;
565}
566
567/**
568 * Wide constants in the Dex format are not typed. So the builder types them as
569 * longs, but when doing the SSA form, we might realize the constant
570 * is used for floating point operations. We create a floating-point equivalent
571 * constant to make the operations correctly typed.
572 */
David Brazdil8d5b8b22015-03-24 10:51:52 +0000573HDoubleConstant* SsaBuilder::GetDoubleEquivalent(HLongConstant* constant) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100574 // We place the floating point constant next to this constant.
575 HDoubleConstant* result = constant->GetNext()->AsDoubleConstant();
576 if (result == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000577 double value = bit_cast<double, int64_t>(constant->GetValue());
578 result = new (graph_->GetArena()) HDoubleConstant(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100579 constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext());
David Brazdildee58d62016-04-07 09:54:26 +0000580 graph_->CacheDoubleConstant(result);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100581 } else {
582 // If there is already a constant with the expected type, we know it is
583 // the floating point equivalent of this constant.
Roland Levillainda4d79b2015-03-24 14:36:11 +0000584 DCHECK_EQ((bit_cast<int64_t, double>(result->GetValue())), constant->GetValue());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100585 }
586 return result;
587}
588
589/**
590 * Because of Dex format, we might end up having the same phi being
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000591 * used for non floating point operations and floating point / reference operations.
592 * Because we want the graph to be correctly typed (and thereafter avoid moves between
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100593 * floating point registers and core registers), we need to create a copy of the
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000594 * phi with a floating point / reference type.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100595 */
David Brazdil8d5b8b22015-03-24 10:51:52 +0000596HPhi* SsaBuilder::GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000597 DCHECK(phi->IsLive()) << "Cannot get equivalent of a dead phi since it would create a live one.";
598
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000599 // We place the floating point /reference phi next to this phi.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100600 HInstruction* next = phi->GetNext();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000601 if (next != nullptr
602 && next->AsPhi()->GetRegNumber() == phi->GetRegNumber()
603 && next->GetType() != type) {
604 // Move to the next phi to see if it is the one we are looking for.
605 next = next->GetNext();
606 }
607
608 if (next == nullptr
609 || (next->AsPhi()->GetRegNumber() != phi->GetRegNumber())
610 || (next->GetType() != type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000611 ArenaAllocator* allocator = graph_->GetArena();
Vladimir Markoe9004912016-06-16 16:50:52 +0100612 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100613 HPhi* new_phi =
614 new (allocator) HPhi(allocator, phi->GetRegNumber(), inputs.size(), type);
615 // Copy the inputs. Note that the graph may not be correctly typed
616 // by doing this copy, but the type propagation phase will fix it.
617 ArrayRef<HUserRecord<HInstruction*>> new_input_records = new_phi->GetInputRecords();
618 for (size_t i = 0; i < inputs.size(); ++i) {
619 new_input_records[i] = HUserRecord<HInstruction*>(inputs[i]);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100620 }
621 phi->GetBlock()->InsertPhiAfter(new_phi, phi);
David Brazdil4833f5a2015-12-16 10:37:39 +0000622 DCHECK(new_phi->IsLive());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100623 return new_phi;
624 } else {
David Brazdil4833f5a2015-12-16 10:37:39 +0000625 // An existing equivalent was found. If it is dead, conflict was previously
626 // identified and we return nullptr instead.
David Brazdil809d70f2015-11-19 10:29:39 +0000627 HPhi* next_phi = next->AsPhi();
628 DCHECK_EQ(next_phi->GetType(), type);
David Brazdil4833f5a2015-12-16 10:37:39 +0000629 return next_phi->IsLive() ? next_phi : nullptr;
David Brazdild9510df2015-11-04 23:30:22 +0000630 }
631}
632
David Brazdil4833f5a2015-12-16 10:37:39 +0000633HArrayGet* SsaBuilder::GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) {
634 DCHECK(Primitive::IsIntegralType(aget->GetType()));
635
636 if (!Primitive::IsIntOrLongType(aget->GetType())) {
637 // Cannot type boolean, char, byte, short to float/double.
638 return nullptr;
639 }
640
641 DCHECK(ContainsElement(ambiguous_agets_, aget));
642 if (agets_fixed_) {
643 // This used to be an ambiguous ArrayGet but its type has been resolved to
644 // int/long. Requesting a float/double equivalent should lead to a conflict.
645 if (kIsDebugBuild) {
646 ScopedObjectAccess soa(Thread::Current());
David Brazdil15693bf2015-12-16 10:30:45 +0000647 DCHECK(Primitive::IsIntOrLongType(GetPrimitiveArrayComponentType(aget->GetArray())));
David Brazdil4833f5a2015-12-16 10:37:39 +0000648 }
649 return nullptr;
650 } else {
651 // This is an ambiguous ArrayGet which has not been resolved yet. Return an
652 // equivalent float/double instruction to use until it is resolved.
653 HArrayGet* equivalent = FindFloatOrDoubleEquivalentOfArrayGet(aget);
654 return (equivalent == nullptr) ? CreateFloatOrDoubleEquivalentOfArrayGet(aget) : equivalent;
655 }
656}
657
658HInstruction* SsaBuilder::GetFloatOrDoubleEquivalent(HInstruction* value, Primitive::Type type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100659 if (value->IsArrayGet()) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000660 return GetFloatOrDoubleEquivalentOfArrayGet(value->AsArrayGet());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100661 } else if (value->IsLongConstant()) {
662 return GetDoubleEquivalent(value->AsLongConstant());
663 } else if (value->IsIntConstant()) {
664 return GetFloatEquivalent(value->AsIntConstant());
665 } else if (value->IsPhi()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000666 return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), type);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100667 } else {
David Brazdil4833f5a2015-12-16 10:37:39 +0000668 return nullptr;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100669 }
670}
671
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000672HInstruction* SsaBuilder::GetReferenceTypeEquivalent(HInstruction* value) {
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000673 if (value->IsIntConstant() && value->AsIntConstant()->GetValue() == 0) {
David Brazdildee58d62016-04-07 09:54:26 +0000674 return graph_->GetNullConstant();
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000675 } else if (value->IsPhi()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000676 return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), Primitive::kPrimNot);
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000677 } else {
678 return nullptr;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000679 }
680}
681
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682} // namespace art