blob: 67ee83c9dd46242aae0f48c538074aff12a4b53f [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 Geoffray0846a8f2018-09-12 15:21:07 +010019#include "base/arena_bit_vector.h"
20#include "base/bit_vector-inl.h"
Andreas Gampe85f1c572018-11-21 13:52:48 -080021#include "base/logging.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010022#include "data_type-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070023#include "dex/bytecode_utils.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080024#include "mirror/class-inl.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010025#include "nodes.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000026#include "reference_type_propagation.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080027#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000028#include "ssa_phi_elimination.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010029
Vladimir Marko0a516052019-10-14 13:00:44 +000030namespace art {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010031
Calin Juravlea4f88312015-04-16 12:57:19 +010032void SsaBuilder::FixNullConstantType() {
33 // The order doesn't matter here.
Vladimir Marko2c45bc92016-10-25 16:54:12 +010034 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
35 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Calin Juravlea4f88312015-04-16 12:57:19 +010036 HInstruction* equality_instr = it.Current();
37 if (!equality_instr->IsEqual() && !equality_instr->IsNotEqual()) {
38 continue;
39 }
40 HInstruction* left = equality_instr->InputAt(0);
41 HInstruction* right = equality_instr->InputAt(1);
Nicolas Geoffray51d400d2015-06-15 09:01:08 +010042 HInstruction* int_operand = nullptr;
Calin Juravlea4f88312015-04-16 12:57:19 +010043
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010044 if ((left->GetType() == DataType::Type::kReference) &&
45 (right->GetType() == DataType::Type::kInt32)) {
Nicolas Geoffray51d400d2015-06-15 09:01:08 +010046 int_operand = right;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010047 } else if ((right->GetType() == DataType::Type::kReference) &&
48 (left->GetType() == DataType::Type::kInt32)) {
Nicolas Geoffray51d400d2015-06-15 09:01:08 +010049 int_operand = left;
Calin Juravlea4f88312015-04-16 12:57:19 +010050 } else {
51 continue;
52 }
53
54 // If we got here, we are comparing against a reference and the int constant
55 // should be replaced with a null constant.
Nicolas Geoffray51d400d2015-06-15 09:01:08 +010056 // Both type propagation and redundant phi elimination ensure `int_operand`
57 // can only be the 0 constant.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +000058 DCHECK(int_operand->IsIntConstant()) << int_operand->DebugName();
Nicolas Geoffray51d400d2015-06-15 09:01:08 +010059 DCHECK_EQ(0, int_operand->AsIntConstant()->GetValue());
David Brazdildee58d62016-04-07 09:54:26 +000060 equality_instr->ReplaceInput(graph_->GetNullConstant(), int_operand == right ? 1 : 0);
Calin Juravlea4f88312015-04-16 12:57:19 +010061 }
62 }
63}
64
65void SsaBuilder::EquivalentPhisCleanup() {
66 // The order doesn't matter here.
Vladimir Marko2c45bc92016-10-25 16:54:12 +010067 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
68 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Calin Juravlea4f88312015-04-16 12:57:19 +010069 HPhi* phi = it.Current()->AsPhi();
70 HPhi* next = phi->GetNextEquivalentPhiWithSameType();
71 if (next != nullptr) {
David Brazdil4833f5a2015-12-16 10:37:39 +000072 // Make sure we do not replace a live phi with a dead phi. A live phi
73 // has been handled by the type propagation phase, unlike a dead phi.
Nicolas Geoffray4230e182015-06-29 14:34:46 +010074 if (next->IsLive()) {
75 phi->ReplaceWith(next);
David Brazdil4833f5a2015-12-16 10:37:39 +000076 phi->SetDead();
Nicolas Geoffray4230e182015-06-29 14:34:46 +010077 } else {
78 next->ReplaceWith(phi);
79 }
Calin Juravlea4f88312015-04-16 12:57:19 +010080 DCHECK(next->GetNextEquivalentPhiWithSameType() == nullptr)
81 << "More then one phi equivalent with type " << phi->GetType()
82 << " found for phi" << phi->GetId();
83 }
84 }
85 }
86}
87
David Brazdil4833f5a2015-12-16 10:37:39 +000088void SsaBuilder::FixEnvironmentPhis() {
Vladimir Marko2c45bc92016-10-25 16:54:12 +010089 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000090 for (HInstructionIterator it_phis(block->GetPhis()); !it_phis.Done(); it_phis.Advance()) {
91 HPhi* phi = it_phis.Current()->AsPhi();
92 // If the phi is not dead, or has no environment uses, there is nothing to do.
93 if (!phi->IsDead() || !phi->HasEnvironmentUses()) continue;
94 HInstruction* next = phi->GetNext();
David Brazdild0180f92015-09-22 14:39:58 +010095 if (!phi->IsVRegEquivalentOf(next)) continue;
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000096 if (next->AsPhi()->IsDead()) {
97 // If the phi equivalent is dead, check if there is another one.
98 next = next->GetNext();
David Brazdild0180f92015-09-22 14:39:58 +010099 if (!phi->IsVRegEquivalentOf(next)) continue;
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000100 // There can be at most two phi equivalents.
David Brazdild0180f92015-09-22 14:39:58 +0100101 DCHECK(!phi->IsVRegEquivalentOf(next->GetNext()));
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000102 if (next->AsPhi()->IsDead()) continue;
103 }
104 // We found a live phi equivalent. Update the environment uses of `phi` with it.
105 phi->ReplaceWith(next);
106 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000107 }
David Brazdil4833f5a2015-12-16 10:37:39 +0000108}
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000109
David Brazdil4833f5a2015-12-16 10:37:39 +0000110static void AddDependentInstructionsToWorklist(HInstruction* instruction,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100111 ScopedArenaVector<HPhi*>* worklist) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000112 // If `instruction` is a dead phi, type conflict was just identified. All its
113 // live phi users, and transitively users of those users, therefore need to be
114 // marked dead/conflicting too, so we add them to the worklist. Otherwise we
115 // add users whose type does not match and needs to be updated.
116 bool add_all_live_phis = instruction->IsPhi() && instruction->AsPhi()->IsDead();
Vladimir Marko46817b82016-03-29 12:21:58 +0100117 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
118 HInstruction* user = use.GetUser();
David Brazdil4833f5a2015-12-16 10:37:39 +0000119 if (user->IsPhi() && user->AsPhi()->IsLive()) {
120 if (add_all_live_phis || user->GetType() != instruction->GetType()) {
121 worklist->push_back(user->AsPhi());
122 }
123 }
124 }
125}
126
127// Find a candidate primitive type for `phi` by merging the type of its inputs.
128// Return false if conflict is identified.
129static bool TypePhiFromInputs(HPhi* phi) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100130 DataType::Type common_type = phi->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +0000131
Vladimir Marko372f10e2016-05-17 16:30:10 +0100132 for (HInstruction* input : phi->GetInputs()) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000133 if (input->IsPhi() && input->AsPhi()->IsDead()) {
134 // Phis are constructed live so if an input is a dead phi, it must have
135 // been made dead due to type conflict. Mark this phi conflicting too.
136 return false;
137 }
138
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100139 DataType::Type input_type = HPhi::ToPhiType(input->GetType());
David Brazdil4833f5a2015-12-16 10:37:39 +0000140 if (common_type == input_type) {
141 // No change in type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100142 } else if (DataType::Is64BitType(common_type) != DataType::Is64BitType(input_type)) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000143 // Types are of different sizes, e.g. int vs. long. Must be a conflict.
144 return false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100145 } else if (DataType::IsIntegralType(common_type)) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000146 // Previous inputs were integral, this one is not but is of the same size.
147 // This does not imply conflict since some bytecode instruction types are
148 // ambiguous. TypeInputsOfPhi will either type them or detect a conflict.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100149 DCHECK(DataType::IsFloatingPointType(input_type) ||
150 input_type == DataType::Type::kReference);
David Brazdil4833f5a2015-12-16 10:37:39 +0000151 common_type = input_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100152 } else if (DataType::IsIntegralType(input_type)) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000153 // Input is integral, common type is not. Same as in the previous case, if
154 // there is a conflict, it will be detected during TypeInputsOfPhi.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100155 DCHECK(DataType::IsFloatingPointType(common_type) ||
156 common_type == DataType::Type::kReference);
David Brazdil4833f5a2015-12-16 10:37:39 +0000157 } else {
158 // Combining float and reference types. Clearly a conflict.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100159 DCHECK(
160 (common_type == DataType::Type::kFloat32 && input_type == DataType::Type::kReference) ||
161 (common_type == DataType::Type::kReference && input_type == DataType::Type::kFloat32));
David Brazdil4833f5a2015-12-16 10:37:39 +0000162 return false;
163 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000164 }
165
David Brazdil4833f5a2015-12-16 10:37:39 +0000166 // We have found a candidate type for the phi. Set it and return true. We may
167 // still discover conflict whilst typing the individual inputs in TypeInputsOfPhi.
168 phi->SetType(common_type);
169 return true;
170}
David Brazdild9510df2015-11-04 23:30:22 +0000171
David Brazdil4833f5a2015-12-16 10:37:39 +0000172// Replace inputs of `phi` to match its type. Return false if conflict is identified.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100173bool SsaBuilder::TypeInputsOfPhi(HPhi* phi, ScopedArenaVector<HPhi*>* worklist) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100174 DataType::Type common_type = phi->GetType();
175 if (DataType::IsIntegralType(common_type)) {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +0100176 // We do not need to retype ambiguous inputs because they are always constructed
177 // with the integral type candidate.
David Brazdil4833f5a2015-12-16 10:37:39 +0000178 if (kIsDebugBuild) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100179 for (HInstruction* input : phi->GetInputs()) {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +0100180 DCHECK(HPhi::ToPhiType(input->GetType()) == common_type);
David Brazdil4833f5a2015-12-16 10:37:39 +0000181 }
182 }
183 // Inputs did not need to be replaced, hence no conflict. Report success.
184 return true;
185 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100186 DCHECK(common_type == DataType::Type::kReference ||
187 DataType::IsFloatingPointType(common_type));
Vladimir Markoe9004912016-06-16 16:50:52 +0100188 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100189 for (size_t i = 0; i < inputs.size(); ++i) {
190 HInstruction* input = inputs[i];
David Brazdil4833f5a2015-12-16 10:37:39 +0000191 if (input->GetType() != common_type) {
192 // Input type does not match phi's type. Try to retype the input or
193 // generate a suitably typed equivalent.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100194 HInstruction* equivalent = (common_type == DataType::Type::kReference)
David Brazdil4833f5a2015-12-16 10:37:39 +0000195 ? GetReferenceTypeEquivalent(input)
196 : GetFloatOrDoubleEquivalent(input, common_type);
197 if (equivalent == nullptr) {
198 // Input could not be typed. Report conflict.
199 return false;
200 }
201 // Make sure the input did not change its type and we do not need to
202 // update its users.
203 DCHECK_NE(input, equivalent);
204
205 phi->ReplaceInput(equivalent, i);
206 if (equivalent->IsPhi()) {
207 worklist->push_back(equivalent->AsPhi());
208 }
209 }
210 }
211 // All inputs either matched the type of the phi or we successfully replaced
212 // them with a suitable equivalent. Report success.
213 return true;
214 }
215}
216
217// Attempt to set the primitive type of `phi` to match its inputs. Return whether
218// it was changed by the algorithm or not.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100219bool SsaBuilder::UpdatePrimitiveType(HPhi* phi, ScopedArenaVector<HPhi*>* worklist) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000220 DCHECK(phi->IsLive());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100221 DataType::Type original_type = phi->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +0000222
223 // Try to type the phi in two stages:
224 // (1) find a candidate type for the phi by merging types of all its inputs,
225 // (2) try to type the phi's inputs to that candidate type.
226 // Either of these stages may detect a type conflict and fail, in which case
227 // we immediately abort.
228 if (!TypePhiFromInputs(phi) || !TypeInputsOfPhi(phi, worklist)) {
229 // Conflict detected. Mark the phi dead and return true because it changed.
230 phi->SetDead();
231 return true;
232 }
233
234 // Return true if the type of the phi has changed.
235 return phi->GetType() != original_type;
236}
237
238void SsaBuilder::RunPrimitiveTypePropagation() {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100239 ScopedArenaVector<HPhi*> worklist(local_allocator_->Adapter(kArenaAllocGraphBuilder));
David Brazdil4833f5a2015-12-16 10:37:39 +0000240
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100241 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 if (block->IsLoopHeader()) {
243 for (HInstructionIterator phi_it(block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
244 HPhi* phi = phi_it.Current()->AsPhi();
245 if (phi->IsLive()) {
246 worklist.push_back(phi);
247 }
248 }
249 } else {
250 for (HInstructionIterator phi_it(block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
251 // Eagerly compute the type of the phi, for quicker convergence. Note
252 // that we don't need to add users to the worklist because we are
253 // doing a reverse post-order visit, therefore either the phi users are
254 // non-loop phi and will be visited later in the visit, or are loop-phis,
255 // and they are already in the work list.
256 HPhi* phi = phi_it.Current()->AsPhi();
257 if (phi->IsLive()) {
258 UpdatePrimitiveType(phi, &worklist);
259 }
260 }
261 }
262 }
263
264 ProcessPrimitiveTypePropagationWorklist(&worklist);
265 EquivalentPhisCleanup();
266}
267
Vladimir Marko69d310e2017-10-09 14:12:23 +0100268void SsaBuilder::ProcessPrimitiveTypePropagationWorklist(ScopedArenaVector<HPhi*>* worklist) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000269 // Process worklist
270 while (!worklist->empty()) {
271 HPhi* phi = worklist->back();
272 worklist->pop_back();
273 // The phi could have been made dead as a result of conflicts while in the
274 // worklist. If it is now dead, there is no point in updating its type.
275 if (phi->IsLive() && UpdatePrimitiveType(phi, worklist)) {
276 AddDependentInstructionsToWorklist(phi, worklist);
277 }
278 }
279}
280
281static HArrayGet* FindFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100282 DataType::Type type = aget->GetType();
283 DCHECK(DataType::IsIntOrLongType(type));
David Brazdildee58d62016-04-07 09:54:26 +0000284 HInstruction* next = aget->GetNext();
285 if (next != nullptr && next->IsArrayGet()) {
286 HArrayGet* next_aget = next->AsArrayGet();
287 if (next_aget->IsEquivalentOf(aget)) {
288 return next_aget;
289 }
290 }
291 return nullptr;
David Brazdil4833f5a2015-12-16 10:37:39 +0000292}
293
294static HArrayGet* CreateFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100295 DataType::Type type = aget->GetType();
296 DCHECK(DataType::IsIntOrLongType(type));
David Brazdil4833f5a2015-12-16 10:37:39 +0000297 DCHECK(FindFloatOrDoubleEquivalentOfArrayGet(aget) == nullptr);
298
Vladimir Markoca6fff82017-10-03 14:49:14 +0100299 HArrayGet* equivalent = new (aget->GetBlock()->GetGraph()->GetAllocator()) HArrayGet(
David Brazdil4833f5a2015-12-16 10:37:39 +0000300 aget->GetArray(),
301 aget->GetIndex(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100302 type == DataType::Type::kInt32 ? DataType::Type::kFloat32 : DataType::Type::kFloat64,
David Brazdil4833f5a2015-12-16 10:37:39 +0000303 aget->GetDexPc());
304 aget->GetBlock()->InsertInstructionAfter(equivalent, aget);
305 return equivalent;
306}
307
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100308static DataType::Type GetPrimitiveArrayComponentType(HInstruction* array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700309 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil15693bf2015-12-16 10:30:45 +0000310 ReferenceTypeInfo array_type = array->GetReferenceTypeInfo();
David Brazdil4833f5a2015-12-16 10:37:39 +0000311 DCHECK(array_type.IsPrimitiveArrayClass());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100312 return DataTypeFromPrimitive(
313 array_type.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
David Brazdil4833f5a2015-12-16 10:37:39 +0000314}
315
David Brazdil15693bf2015-12-16 10:30:45 +0000316bool SsaBuilder::FixAmbiguousArrayOps() {
317 if (ambiguous_agets_.empty() && ambiguous_asets_.empty()) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000318 return true;
319 }
320
321 // The wrong ArrayGet equivalent may still have Phi uses coming from ArraySet
322 // uses (because they are untyped) and environment uses (if --debuggable).
323 // After resolving all ambiguous ArrayGets, we will re-run primitive type
324 // propagation on the Phis which need to be updated.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100325 ScopedArenaVector<HPhi*> worklist(local_allocator_->Adapter(kArenaAllocGraphBuilder));
David Brazdil4833f5a2015-12-16 10:37:39 +0000326
327 {
328 ScopedObjectAccess soa(Thread::Current());
329
330 for (HArrayGet* aget_int : ambiguous_agets_) {
David Brazdil15693bf2015-12-16 10:30:45 +0000331 HInstruction* array = aget_int->GetArray();
332 if (!array->GetReferenceTypeInfo().IsPrimitiveArrayClass()) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000333 // RTP did not type the input array. Bail.
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +0000334 VLOG(compiler) << "Not compiled: Could not infer an array type for array operation at "
335 << aget_int->GetDexPc();
David Brazdil4833f5a2015-12-16 10:37:39 +0000336 return false;
337 }
338
339 HArrayGet* aget_float = FindFloatOrDoubleEquivalentOfArrayGet(aget_int);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100340 DataType::Type array_type = GetPrimitiveArrayComponentType(array);
341 DCHECK_EQ(DataType::Is64BitType(aget_int->GetType()), DataType::Is64BitType(array_type));
David Brazdil15693bf2015-12-16 10:30:45 +0000342
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100343 if (DataType::IsIntOrLongType(array_type)) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 if (aget_float != nullptr) {
345 // There is a float/double equivalent. We must replace it and re-run
346 // primitive type propagation on all dependent instructions.
347 aget_float->ReplaceWith(aget_int);
348 aget_float->GetBlock()->RemoveInstruction(aget_float);
349 AddDependentInstructionsToWorklist(aget_int, &worklist);
350 }
351 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100352 DCHECK(DataType::IsFloatingPointType(array_type));
David Brazdil4833f5a2015-12-16 10:37:39 +0000353 if (aget_float == nullptr) {
354 // This is a float/double ArrayGet but there were no typed uses which
355 // would create the typed equivalent. Create it now.
356 aget_float = CreateFloatOrDoubleEquivalentOfArrayGet(aget_int);
357 }
358 // Replace the original int/long instruction. Note that it may have phi
359 // uses, environment uses, as well as real uses (from untyped ArraySets).
360 // We need to re-run primitive type propagation on its dependent instructions.
361 aget_int->ReplaceWith(aget_float);
362 aget_int->GetBlock()->RemoveInstruction(aget_int);
363 AddDependentInstructionsToWorklist(aget_float, &worklist);
364 }
365 }
David Brazdil4833f5a2015-12-16 10:37:39 +0000366
David Brazdil15693bf2015-12-16 10:30:45 +0000367 // Set a flag stating that types of ArrayGets have been resolved. Requesting
368 // equivalent of the wrong type with GetFloatOrDoubleEquivalentOfArrayGet
369 // will fail from now on.
370 agets_fixed_ = true;
371
372 for (HArraySet* aset : ambiguous_asets_) {
373 HInstruction* array = aset->GetArray();
374 if (!array->GetReferenceTypeInfo().IsPrimitiveArrayClass()) {
375 // RTP did not type the input array. Bail.
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +0000376 VLOG(compiler) << "Not compiled: Could not infer an array type for array operation at "
377 << aset->GetDexPc();
David Brazdil15693bf2015-12-16 10:30:45 +0000378 return false;
379 }
380
381 HInstruction* value = aset->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100382 DataType::Type value_type = value->GetType();
383 DataType::Type array_type = GetPrimitiveArrayComponentType(array);
384 DCHECK_EQ(DataType::Is64BitType(value_type), DataType::Is64BitType(array_type));
David Brazdil15693bf2015-12-16 10:30:45 +0000385
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100386 if (DataType::IsFloatingPointType(array_type)) {
387 if (!DataType::IsFloatingPointType(value_type)) {
388 DCHECK(DataType::IsIntegralType(value_type));
David Brazdil15693bf2015-12-16 10:30:45 +0000389 // Array elements are floating-point but the value has not been replaced
390 // with its floating-point equivalent. The replacement must always
391 // succeed in code validated by the verifier.
392 HInstruction* equivalent = GetFloatOrDoubleEquivalent(value, array_type);
393 DCHECK(equivalent != nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -0800394 aset->ReplaceInput(equivalent, /* index= */ 2);
David Brazdil15693bf2015-12-16 10:30:45 +0000395 if (equivalent->IsPhi()) {
396 // Returned equivalent is a phi which may not have had its inputs
397 // replaced yet. We need to run primitive type propagation on it.
398 worklist.push_back(equivalent->AsPhi());
399 }
400 }
Aart Bik18b36ab2016-04-13 16:41:35 -0700401 // Refine the side effects of this floating point aset. Note that we do this even if
402 // no replacement occurs, since the right-hand-side may have been corrected already.
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100403 aset->SetSideEffects(HArraySet::ComputeSideEffects(aset->GetComponentType()));
David Brazdil15693bf2015-12-16 10:30:45 +0000404 } else {
405 // Array elements are integral and the value assigned to it initially
406 // was integral too. Nothing to do.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100407 DCHECK(DataType::IsIntegralType(array_type));
408 DCHECK(DataType::IsIntegralType(value_type));
David Brazdil15693bf2015-12-16 10:30:45 +0000409 }
410 }
411 }
David Brazdil4833f5a2015-12-16 10:37:39 +0000412
413 if (!worklist.empty()) {
414 ProcessPrimitiveTypePropagationWorklist(&worklist);
415 EquivalentPhisCleanup();
416 }
417
418 return true;
419}
420
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100421bool SsaBuilder::HasAliasInEnvironments(HInstruction* instruction) {
422 ScopedArenaHashSet<size_t> seen_users(
423 local_allocator_->Adapter(kArenaAllocGraphBuilder));
Vladimir Marko46817b82016-03-29 12:21:58 +0100424 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
425 DCHECK(use.GetUser() != nullptr);
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100426 size_t id = use.GetUser()->GetHolder()->GetId();
427 if (seen_users.find(id) != seen_users.end()) {
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +0000428 return true;
429 }
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100430 seen_users.insert(id);
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +0000431 }
432 return false;
433}
434
Nicolas Geoffray639f2792018-09-25 00:39:48 +0100435bool SsaBuilder::ReplaceUninitializedStringPhis() {
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100436 for (HInvoke* invoke : uninitialized_string_phis_) {
437 HInstruction* str = invoke->InputAt(invoke->InputCount() - 1);
438 if (str->IsPhi()) {
439 // If after redundant phi and dead phi elimination, it's still a phi that feeds
440 // the invoke, then we must be compiling a method with irreducible loops. Just bail.
441 DCHECK(graph_->HasIrreducibleLoops());
Nicolas Geoffray639f2792018-09-25 00:39:48 +0100442 return false;
443 }
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100444 DCHECK(str->IsNewInstance());
445 AddUninitializedString(str->AsNewInstance());
446 str->ReplaceUsesDominatedBy(invoke, invoke);
447 str->ReplaceEnvUsesDominatedBy(invoke, invoke);
448 invoke->RemoveInputAt(invoke->InputCount() - 1);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100449 }
Nicolas Geoffray639f2792018-09-25 00:39:48 +0100450 return true;
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100451}
452
David Brazdil65902e82016-01-15 09:35:13 +0000453void SsaBuilder::RemoveRedundantUninitializedStrings() {
David Brazdildee58d62016-04-07 09:54:26 +0000454 if (graph_->IsDebuggable()) {
David Brazdil65902e82016-01-15 09:35:13 +0000455 // Do not perform the optimization for consistency with the interpreter
456 // which always allocates an object for new-instance of String.
457 return;
458 }
459
460 for (HNewInstance* new_instance : uninitialized_strings_) {
Aart Bikeda31402016-03-24 15:38:56 -0700461 DCHECK(new_instance->IsInBlock());
David Brazdildee58d62016-04-07 09:54:26 +0000462 DCHECK(new_instance->IsStringAlloc());
463
David Brazdil65902e82016-01-15 09:35:13 +0000464 // Replace NewInstance of String with NullConstant if not used prior to
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100465 // calling StringFactory. We check for alias environments in case of deoptimization.
466 // The interpreter is expected to skip null check on the `this` argument of the
467 // StringFactory call.
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +0000468 if (!new_instance->HasNonEnvironmentUses() && !HasAliasInEnvironments(new_instance)) {
David Brazdildee58d62016-04-07 09:54:26 +0000469 new_instance->ReplaceWith(graph_->GetNullConstant());
David Brazdil65902e82016-01-15 09:35:13 +0000470 new_instance->GetBlock()->RemoveInstruction(new_instance);
471
472 // Remove LoadClass if not needed any more.
Nicolas Geoffray5e08e362016-02-15 15:56:11 +0000473 HInstruction* input = new_instance->InputAt(0);
474 HLoadClass* load_class = nullptr;
475
476 // If the class was not present in the dex cache at the point of building
477 // the graph, the builder inserted a HClinitCheck in between. Since the String
478 // class is always initialized at the point of running Java code, we can remove
479 // that check.
480 if (input->IsClinitCheck()) {
481 load_class = input->InputAt(0)->AsLoadClass();
482 input->ReplaceWith(load_class);
483 input->GetBlock()->RemoveInstruction(input);
484 } else {
485 load_class = input->AsLoadClass();
486 DCHECK(new_instance->IsStringAlloc());
487 DCHECK(!load_class->NeedsAccessCheck()) << "String class is always accessible";
488 }
David Brazdil65902e82016-01-15 09:35:13 +0000489 DCHECK(load_class != nullptr);
David Brazdil65902e82016-01-15 09:35:13 +0000490 if (!load_class->HasUses()) {
Nicolas Geoffray5e08e362016-02-15 15:56:11 +0000491 // Even if the HLoadClass needs access check, we can remove it, as we know the
492 // String class does not need it.
David Brazdil65902e82016-01-15 09:35:13 +0000493 load_class->GetBlock()->RemoveInstruction(load_class);
494 }
495 }
496 }
497}
498
Nicolas Geoffray7cfc8f52019-08-07 10:41:09 +0100499static bool HasPhiEquivalentAtLoopEntry(HGraph* graph) {
500 // Phi equivalents for a dex register do not work with OSR, as the phis will
501 // receive two different stack slots but only one is recorded in the stack
502 // map.
503 for (HBasicBlock* block : graph->GetReversePostOrder()) {
504 if (block->IsLoopHeader()) {
505 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
506 if (it.Current()->AsPhi()->HasEquivalentPhi()) {
507 return true;
508 }
509 }
510 }
511 }
512 return false;
513}
514
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000515GraphAnalysisResult SsaBuilder::BuildSsa() {
David Brazdildee58d62016-04-07 09:54:26 +0000516 DCHECK(!graph_->IsInSsaForm());
David Brazdilbadd8262016-02-02 16:28:56 +0000517
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100518 // Propagate types of phis. At this point, phis are typed void in the general
David Brazdil4833f5a2015-12-16 10:37:39 +0000519 // case, or float/double/reference if we created an equivalent phi. So we need
520 // to propagate the types across phis to give them a correct type. If a type
521 // conflict is detected in this stage, the phi is marked dead.
522 RunPrimitiveTypePropagation();
523
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100524 // Now that the correct primitive types have been assigned, we can get rid
David Brazdil4833f5a2015-12-16 10:37:39 +0000525 // of redundant phis. Note that we cannot do this phase before type propagation,
526 // otherwise we could get rid of phi equivalents, whose presence is a requirement
527 // for the type propagation phase. Note that this is to satisfy statement (a)
528 // of the SsaBuilder (see ssa_builder.h).
David Brazdildee58d62016-04-07 09:54:26 +0000529 SsaRedundantPhiElimination(graph_).Run();
David Brazdil4833f5a2015-12-16 10:37:39 +0000530
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100531 // Fix the type for null constants which are part of an equality comparison.
David Brazdil4833f5a2015-12-16 10:37:39 +0000532 // We need to do this after redundant phi elimination, to ensure the only cases
533 // that we can see are reference comparison against 0. The redundant phi
534 // elimination ensures we do not see a phi taking two 0 constants in a HEqual
535 // or HNotEqual.
536 FixNullConstantType();
537
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100538 // Compute type of reference type instructions. The pass assumes that
David Brazdil4833f5a2015-12-16 10:37:39 +0000539 // NullConstant has been fixed up.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000540 ReferenceTypePropagation(graph_,
541 class_loader_,
542 dex_cache_,
Andreas Gampe3db70682018-12-26 15:12:03 -0800543 /* is_first_run= */ true).Run();
David Brazdil4833f5a2015-12-16 10:37:39 +0000544
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100545 // HInstructionBuilder duplicated ArrayGet instructions with ambiguous type
David Brazdildee58d62016-04-07 09:54:26 +0000546 // (int/float or long/double) and marked ArraySets with ambiguous input type.
547 // Now that RTP computed the type of the array input, the ambiguity can be
548 // resolved and the correct equivalents kept.
David Brazdil15693bf2015-12-16 10:30:45 +0000549 if (!FixAmbiguousArrayOps()) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000550 return kAnalysisFailAmbiguousArrayOp;
David Brazdil4833f5a2015-12-16 10:37:39 +0000551 }
552
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100553 // Mark dead phis. This will mark phis which are not used by instructions
David Brazdil4833f5a2015-12-16 10:37:39 +0000554 // or other live phis. If compiling as debuggable code, phis will also be kept
555 // live if they have an environment use.
David Brazdildee58d62016-04-07 09:54:26 +0000556 SsaDeadPhiElimination dead_phi_elimimation(graph_);
David Brazdil4833f5a2015-12-16 10:37:39 +0000557 dead_phi_elimimation.MarkDeadPhis();
558
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100559 // Make sure environments use the right phi equivalent: a phi marked dead
David Brazdil4833f5a2015-12-16 10:37:39 +0000560 // can have a phi equivalent that is not dead. In that case we have to replace
561 // it with the live equivalent because deoptimization and try/catch rely on
562 // environments containing values of all live vregs at that point. Note that
563 // there can be multiple phis for the same Dex register that are live
564 // (for example when merging constants), in which case it is okay for the
565 // environments to just reference one.
566 FixEnvironmentPhis();
567
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100568 // Now that the right phis are used for the environments, we can eliminate
David Brazdil4833f5a2015-12-16 10:37:39 +0000569 // phis we do not need. Regardless of the debuggable status, this phase is
570 /// necessary for statement (b) of the SsaBuilder (see ssa_builder.h), as well
571 // as for the code generation, which does not deal with phis of conflicting
572 // input types.
573 dead_phi_elimimation.EliminateDeadPhis();
574
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100575 // Replace Phis that feed in a String.<init> during instruction building. We
576 // run this after redundant and dead phi elimination to make sure the phi will have
577 // been replaced by the actual allocation. Only with an irreducible loop
578 // a phi can still be the input, in which case we bail.
579 if (!ReplaceUninitializedStringPhis()) {
580 return kAnalysisFailIrreducibleLoopAndStringInit;
581 }
582
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100583 // HInstructionBuidler replaced uses of NewInstances of String with the
David Brazdildee58d62016-04-07 09:54:26 +0000584 // results of their corresponding StringFactory calls. Unless the String
585 // objects are used before they are initialized, they can be replaced with
586 // NullConstant. Note that this optimization is valid only if unsimplified
587 // code does not use the uninitialized value because we assume execution can
588 // be deoptimized at any safepoint. We must therefore perform it before any
589 // other optimizations.
David Brazdil65902e82016-01-15 09:35:13 +0000590 RemoveRedundantUninitializedStrings();
591
Nicolas Geoffray7cfc8f52019-08-07 10:41:09 +0100592 if (graph_->IsCompilingOsr() && HasPhiEquivalentAtLoopEntry(graph_)) {
593 return kAnalysisFailPhiEquivalentInOsr;
594 }
595
David Brazdildee58d62016-04-07 09:54:26 +0000596 graph_->SetInSsaForm();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000597 return kAnalysisSuccess;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100598}
599
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100600/**
601 * Constants in the Dex format are not typed. So the builder types them as
602 * integers, but when doing the SSA form, we might realize the constant
603 * is used for floating point operations. We create a floating-point equivalent
604 * constant to make the operations correctly typed.
605 */
David Brazdil8d5b8b22015-03-24 10:51:52 +0000606HFloatConstant* SsaBuilder::GetFloatEquivalent(HIntConstant* constant) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100607 // We place the floating point constant next to this constant.
608 HFloatConstant* result = constant->GetNext()->AsFloatConstant();
609 if (result == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000610 float value = bit_cast<float, int32_t>(constant->GetValue());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100611 result = new (graph_->GetAllocator()) HFloatConstant(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100612 constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext());
David Brazdildee58d62016-04-07 09:54:26 +0000613 graph_->CacheFloatConstant(result);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100614 } else {
615 // If there is already a constant with the expected type, we know it is
616 // the floating point equivalent of this constant.
Roland Levillainda4d79b2015-03-24 14:36:11 +0000617 DCHECK_EQ((bit_cast<int32_t, float>(result->GetValue())), constant->GetValue());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100618 }
619 return result;
620}
621
622/**
623 * Wide constants in the Dex format are not typed. So the builder types them as
624 * longs, but when doing the SSA form, we might realize the constant
625 * is used for floating point operations. We create a floating-point equivalent
626 * constant to make the operations correctly typed.
627 */
David Brazdil8d5b8b22015-03-24 10:51:52 +0000628HDoubleConstant* SsaBuilder::GetDoubleEquivalent(HLongConstant* constant) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100629 // We place the floating point constant next to this constant.
630 HDoubleConstant* result = constant->GetNext()->AsDoubleConstant();
631 if (result == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000632 double value = bit_cast<double, int64_t>(constant->GetValue());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100633 result = new (graph_->GetAllocator()) HDoubleConstant(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100634 constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext());
David Brazdildee58d62016-04-07 09:54:26 +0000635 graph_->CacheDoubleConstant(result);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100636 } else {
637 // If there is already a constant with the expected type, we know it is
638 // the floating point equivalent of this constant.
Roland Levillainda4d79b2015-03-24 14:36:11 +0000639 DCHECK_EQ((bit_cast<int64_t, double>(result->GetValue())), constant->GetValue());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100640 }
641 return result;
642}
643
644/**
645 * Because of Dex format, we might end up having the same phi being
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000646 * used for non floating point operations and floating point / reference operations.
647 * Because we want the graph to be correctly typed (and thereafter avoid moves between
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100648 * floating point registers and core registers), we need to create a copy of the
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000649 * phi with a floating point / reference type.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100650 */
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100651HPhi* SsaBuilder::GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, DataType::Type type) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000652 DCHECK(phi->IsLive()) << "Cannot get equivalent of a dead phi since it would create a live one.";
653
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000654 // We place the floating point /reference phi next to this phi.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100655 HInstruction* next = phi->GetNext();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000656 if (next != nullptr
657 && next->AsPhi()->GetRegNumber() == phi->GetRegNumber()
658 && next->GetType() != type) {
659 // Move to the next phi to see if it is the one we are looking for.
660 next = next->GetNext();
661 }
662
663 if (next == nullptr
664 || (next->AsPhi()->GetRegNumber() != phi->GetRegNumber())
665 || (next->GetType() != type)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100666 ArenaAllocator* allocator = graph_->GetAllocator();
Vladimir Markoe9004912016-06-16 16:50:52 +0100667 HInputsRef inputs = phi->GetInputs();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100668 HPhi* new_phi = new (allocator) HPhi(allocator, phi->GetRegNumber(), inputs.size(), type);
Vladimir Marko372f10e2016-05-17 16:30:10 +0100669 // Copy the inputs. Note that the graph may not be correctly typed
670 // by doing this copy, but the type propagation phase will fix it.
671 ArrayRef<HUserRecord<HInstruction*>> new_input_records = new_phi->GetInputRecords();
672 for (size_t i = 0; i < inputs.size(); ++i) {
673 new_input_records[i] = HUserRecord<HInstruction*>(inputs[i]);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100674 }
675 phi->GetBlock()->InsertPhiAfter(new_phi, phi);
David Brazdil4833f5a2015-12-16 10:37:39 +0000676 DCHECK(new_phi->IsLive());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100677 return new_phi;
678 } else {
David Brazdil4833f5a2015-12-16 10:37:39 +0000679 // An existing equivalent was found. If it is dead, conflict was previously
680 // identified and we return nullptr instead.
David Brazdil809d70f2015-11-19 10:29:39 +0000681 HPhi* next_phi = next->AsPhi();
682 DCHECK_EQ(next_phi->GetType(), type);
David Brazdil4833f5a2015-12-16 10:37:39 +0000683 return next_phi->IsLive() ? next_phi : nullptr;
David Brazdild9510df2015-11-04 23:30:22 +0000684 }
685}
686
David Brazdil4833f5a2015-12-16 10:37:39 +0000687HArrayGet* SsaBuilder::GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100688 DCHECK(DataType::IsIntegralType(aget->GetType()));
David Brazdil4833f5a2015-12-16 10:37:39 +0000689
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100690 if (!DataType::IsIntOrLongType(aget->GetType())) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000691 // Cannot type boolean, char, byte, short to float/double.
692 return nullptr;
693 }
694
695 DCHECK(ContainsElement(ambiguous_agets_, aget));
696 if (agets_fixed_) {
697 // This used to be an ambiguous ArrayGet but its type has been resolved to
698 // int/long. Requesting a float/double equivalent should lead to a conflict.
699 if (kIsDebugBuild) {
700 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100701 DCHECK(DataType::IsIntOrLongType(GetPrimitiveArrayComponentType(aget->GetArray())));
David Brazdil4833f5a2015-12-16 10:37:39 +0000702 }
703 return nullptr;
704 } else {
705 // This is an ambiguous ArrayGet which has not been resolved yet. Return an
706 // equivalent float/double instruction to use until it is resolved.
707 HArrayGet* equivalent = FindFloatOrDoubleEquivalentOfArrayGet(aget);
708 return (equivalent == nullptr) ? CreateFloatOrDoubleEquivalentOfArrayGet(aget) : equivalent;
709 }
710}
711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100712HInstruction* SsaBuilder::GetFloatOrDoubleEquivalent(HInstruction* value, DataType::Type type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100713 if (value->IsArrayGet()) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000714 return GetFloatOrDoubleEquivalentOfArrayGet(value->AsArrayGet());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100715 } else if (value->IsLongConstant()) {
716 return GetDoubleEquivalent(value->AsLongConstant());
717 } else if (value->IsIntConstant()) {
718 return GetFloatEquivalent(value->AsIntConstant());
719 } else if (value->IsPhi()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000720 return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), type);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100721 } else {
David Brazdil4833f5a2015-12-16 10:37:39 +0000722 return nullptr;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100723 }
724}
725
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000726HInstruction* SsaBuilder::GetReferenceTypeEquivalent(HInstruction* value) {
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000727 if (value->IsIntConstant() && value->AsIntConstant()->GetValue() == 0) {
David Brazdildee58d62016-04-07 09:54:26 +0000728 return graph_->GetNullConstant();
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000729 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100730 return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), DataType::Type::kReference);
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000731 } else {
732 return nullptr;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000733 }
734}
735
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100736} // namespace art