blob: 3d6606b8dc1c8f7272bee378aa9e536f60877351 [file] [log] [blame]
Calin Juravle10e244f2015-01-26 18:54:32 +00001/*
2 * Copyright (C) 2015 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 "reference_type_propagation.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "class_linker-inl.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000020#include "mirror/class-inl.h"
21#include "mirror/dex_cache.h"
22#include "scoped_thread_state_change.h"
23
Calin Juravle10e244f2015-01-26 18:54:32 +000024namespace art {
25
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010026class RTPVisitor : public HGraphDelegateVisitor {
27 public:
28 RTPVisitor(HGraph* graph, StackHandleScopeCollection* handles)
29 : HGraphDelegateVisitor(graph),
30 handles_(handles) {}
31
32 void VisitNewInstance(HNewInstance* new_instance) OVERRIDE;
33 void VisitLoadClass(HLoadClass* load_class) OVERRIDE;
34 void VisitNewArray(HNewArray* instr) OVERRIDE;
35 void UpdateFieldAccessTypeInfo(HInstruction* instr, const FieldInfo& info);
36 void SetClassAsTypeInfo(HInstruction* instr, mirror::Class* klass, bool is_exact);
37 void VisitInstanceFieldGet(HInstanceFieldGet* instr) OVERRIDE;
38 void VisitStaticFieldGet(HStaticFieldGet* instr) OVERRIDE;
39 void VisitInvoke(HInvoke* instr) OVERRIDE;
Guillaume "Vermeille" Sanchez72a5eb52015-06-02 17:39:45 +010040 void VisitArrayGet(HArrayGet* instr) OVERRIDE;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010041 void UpdateReferenceTypeInfo(HInstruction* instr,
42 uint16_t type_idx,
43 const DexFile& dex_file,
44 bool is_exact);
45
46 private:
47 StackHandleScopeCollection* handles_;
48};
49
Calin Juravleacf735c2015-02-12 15:25:22 +000050void ReferenceTypePropagation::Run() {
51 // To properly propagate type info we need to visit in the dominator-based order.
Calin Juravle10e244f2015-01-26 18:54:32 +000052 // Reverse post order guarantees a node's dominators are visited first.
53 // We take advantage of this order in `VisitBasicBlock`.
Calin Juravle6c0c4f22015-06-12 15:40:42 +000054 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Calin Juravle10e244f2015-01-26 18:54:32 +000055 VisitBasicBlock(it.Current());
56 }
57 ProcessWorklist();
58}
59
Calin Juravleb1498f62015-02-16 13:13:29 +000060void ReferenceTypePropagation::VisitBasicBlock(HBasicBlock* block) {
61 // TODO: handle other instructions that give type info
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010062 // (array accesses)
Calin Juravle10e244f2015-01-26 18:54:32 +000063
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010064 RTPVisitor visitor(graph_, handles_);
Calin Juravleb1498f62015-02-16 13:13:29 +000065 // Initialize exact types first for faster convergence.
66 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
67 HInstruction* instr = it.Current();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010068 instr->Accept(&visitor);
Calin Juravleb1498f62015-02-16 13:13:29 +000069 }
70
71 // Handle Phis.
72 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
73 VisitPhi(it.Current()->AsPhi());
74 }
75
76 // Add extra nodes to bound types.
Calin Juravle61d544b2015-02-23 16:46:57 +000077 BoundTypeForIfNotNull(block);
Calin Juravleb1498f62015-02-16 13:13:29 +000078 BoundTypeForIfInstanceOf(block);
Calin Juravle10e244f2015-01-26 18:54:32 +000079}
80
Calin Juravle61d544b2015-02-23 16:46:57 +000081void ReferenceTypePropagation::BoundTypeForIfNotNull(HBasicBlock* block) {
Calin Juravleb3306642015-04-20 18:30:42 +010082 HIf* ifInstruction = block->GetLastInstruction()->AsIf();
83 if (ifInstruction == nullptr) {
Calin Juravle61d544b2015-02-23 16:46:57 +000084 return;
85 }
Calin Juravleb3306642015-04-20 18:30:42 +010086 HInstruction* ifInput = ifInstruction->InputAt(0);
Calin Juravle61d544b2015-02-23 16:46:57 +000087 if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) {
88 return;
89 }
90 HInstruction* input0 = ifInput->InputAt(0);
91 HInstruction* input1 = ifInput->InputAt(1);
Calin Juravleedad8ad2015-04-23 14:34:33 +010092 HInstruction* obj = nullptr;
Calin Juravle61d544b2015-02-23 16:46:57 +000093
Calin Juravleedad8ad2015-04-23 14:34:33 +010094 if (input1->IsNullConstant()) {
Calin Juravle61d544b2015-02-23 16:46:57 +000095 obj = input0;
Calin Juravleedad8ad2015-04-23 14:34:33 +010096 } else if (input0->IsNullConstant()) {
Calin Juravle61d544b2015-02-23 16:46:57 +000097 obj = input1;
98 } else {
99 return;
100 }
101
Calin Juravleb3306642015-04-20 18:30:42 +0100102 // We only need to bound the type if we have uses in the relevant block.
103 // So start with null and create the HBoundType lazily, only if it's needed.
104 HBoundType* bound_type = nullptr;
Calin Juravle61d544b2015-02-23 16:46:57 +0000105 HBasicBlock* notNullBlock = ifInput->IsNotEqual()
Calin Juravleb3306642015-04-20 18:30:42 +0100106 ? ifInstruction->IfTrueSuccessor()
107 : ifInstruction->IfFalseSuccessor();
108
Calin Juravle61d544b2015-02-23 16:46:57 +0000109 for (HUseIterator<HInstruction*> it(obj->GetUses()); !it.Done(); it.Advance()) {
110 HInstruction* user = it.Current()->GetUser();
111 if (notNullBlock->Dominates(user->GetBlock())) {
Calin Juravleb3306642015-04-20 18:30:42 +0100112 if (bound_type == nullptr) {
Calin Juravle6c0c4f22015-06-12 15:40:42 +0000113 bound_type = new (graph_->GetArena()) HBoundType(obj, ReferenceTypeInfo::CreateTop(false));
Calin Juravleb3306642015-04-20 18:30:42 +0100114 notNullBlock->InsertInstructionBefore(bound_type, notNullBlock->GetFirstInstruction());
115 }
Calin Juravle61d544b2015-02-23 16:46:57 +0000116 user->ReplaceInput(bound_type, it.Current()->GetIndex());
117 }
118 }
119}
120
Calin Juravleb1498f62015-02-16 13:13:29 +0000121// Detects if `block` is the True block for the pattern
122// `if (x instanceof ClassX) { }`
123// If that's the case insert an HBoundType instruction to bound the type of `x`
124// to `ClassX` in the scope of the dominated blocks.
125void ReferenceTypePropagation::BoundTypeForIfInstanceOf(HBasicBlock* block) {
Calin Juravleb3306642015-04-20 18:30:42 +0100126 HIf* ifInstruction = block->GetLastInstruction()->AsIf();
127 if (ifInstruction == nullptr) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000128 return;
129 }
Calin Juravleb3306642015-04-20 18:30:42 +0100130 HInstruction* ifInput = ifInstruction->InputAt(0);
131 HInstruction* instanceOf = nullptr;
132 HBasicBlock* instanceOfTrueBlock = nullptr;
David Brazdil0d13fee2015-04-17 14:52:19 +0100133
Calin Juravleb3306642015-04-20 18:30:42 +0100134 // The instruction simplifier has transformed:
135 // - `if (a instanceof A)` into an HIf with an HInstanceOf input
136 // - `if (!(a instanceof A)` into an HIf with an HBooleanNot input (which in turn
137 // has an HInstanceOf input)
138 // So we should not see the usual HEqual here.
David Brazdil0d13fee2015-04-17 14:52:19 +0100139 if (ifInput->IsInstanceOf()) {
140 instanceOf = ifInput;
Calin Juravleb3306642015-04-20 18:30:42 +0100141 instanceOfTrueBlock = ifInstruction->IfTrueSuccessor();
David Brazdil0d13fee2015-04-17 14:52:19 +0100142 } else if (ifInput->IsBooleanNot() && ifInput->InputAt(0)->IsInstanceOf()) {
143 instanceOf = ifInput->InputAt(0);
Calin Juravleb3306642015-04-20 18:30:42 +0100144 instanceOfTrueBlock = ifInstruction->IfFalseSuccessor();
David Brazdil0d13fee2015-04-17 14:52:19 +0100145 } else {
Calin Juravleb1498f62015-02-16 13:13:29 +0000146 return;
Calin Juravleacf735c2015-02-12 15:25:22 +0000147 }
148
Calin Juravleb3306642015-04-20 18:30:42 +0100149 // We only need to bound the type if we have uses in the relevant block.
150 // So start with null and create the HBoundType lazily, only if it's needed.
151 HBoundType* bound_type = nullptr;
152
Calin Juravleb1498f62015-02-16 13:13:29 +0000153 HInstruction* obj = instanceOf->InputAt(0);
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100154 if (obj->GetReferenceTypeInfo().IsExact() && !obj->IsPhi()) {
155 // This method is being called while doing a fixed-point calculation
156 // over phis. Non-phis instruction whose type is already known do
157 // not need to be bound to another type.
158 // Not that this also prevents replacing `HLoadClass` with a `HBoundType`.
159 // `HCheckCast` and `HInstanceOf` expect a `HLoadClass` as a second
160 // input.
161 return;
162 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000163 for (HUseIterator<HInstruction*> it(obj->GetUses()); !it.Done(); it.Advance()) {
164 HInstruction* user = it.Current()->GetUser();
165 if (instanceOfTrueBlock->Dominates(user->GetBlock())) {
Calin Juravleb3306642015-04-20 18:30:42 +0100166 if (bound_type == nullptr) {
167 HLoadClass* load_class = instanceOf->InputAt(1)->AsLoadClass();
168
169 ReferenceTypeInfo obj_rti = obj->GetReferenceTypeInfo();
170 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
Calin Juravle6c0c4f22015-06-12 15:40:42 +0000171 bound_type = new (graph_->GetArena()) HBoundType(obj, class_rti);
Calin Juravleb3306642015-04-20 18:30:42 +0100172
173 // Narrow the type as much as possible.
174 {
175 ScopedObjectAccess soa(Thread::Current());
176 if (!load_class->IsResolved() || class_rti.IsSupertypeOf(obj_rti)) {
177 bound_type->SetReferenceTypeInfo(obj_rti);
178 } else {
179 bound_type->SetReferenceTypeInfo(
180 ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false));
181 }
182 }
183
184 instanceOfTrueBlock->InsertInstructionBefore(
185 bound_type, instanceOfTrueBlock->GetFirstInstruction());
186 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000187 user->ReplaceInput(bound_type, it.Current()->GetIndex());
188 }
189 }
Calin Juravleacf735c2015-02-12 15:25:22 +0000190}
191
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100192void RTPVisitor::SetClassAsTypeInfo(HInstruction* instr,
193 mirror::Class* klass,
194 bool is_exact) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100195 if (klass != nullptr) {
196 ScopedObjectAccess soa(Thread::Current());
197 MutableHandle<mirror::Class> handle = handles_->NewHandle(klass);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100198 is_exact = is_exact || klass->IsFinal();
199 instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(handle, is_exact));
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100200 }
201}
202
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100203void RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr,
204 uint16_t type_idx,
205 const DexFile& dex_file,
206 bool is_exact) {
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +0100207 DCHECK_EQ(instr->GetType(), Primitive::kPrimNot);
208
Calin Juravleacf735c2015-02-12 15:25:22 +0000209 ScopedObjectAccess soa(Thread::Current());
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +0100210 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
Calin Juravleacf735c2015-02-12 15:25:22 +0000211 // Get type from dex cache assuming it was populated by the verifier.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100212 SetClassAsTypeInfo(instr, dex_cache->GetResolvedType(type_idx), is_exact);
Calin Juravleacf735c2015-02-12 15:25:22 +0000213}
214
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100215void RTPVisitor::VisitNewInstance(HNewInstance* instr) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100216 UpdateReferenceTypeInfo(instr, instr->GetTypeIndex(), instr->GetDexFile(), /* is_exact */ true);
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +0100217}
218
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100219void RTPVisitor::VisitNewArray(HNewArray* instr) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100220 UpdateReferenceTypeInfo(instr, instr->GetTypeIndex(), instr->GetDexFile(), /* is_exact */ true);
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +0100221}
222
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100223void RTPVisitor::UpdateFieldAccessTypeInfo(HInstruction* instr,
224 const FieldInfo& info) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100225 // The field index is unknown only during tests.
226 if (instr->GetType() != Primitive::kPrimNot || info.GetFieldIndex() == kUnknownFieldIndex) {
227 return;
228 }
229
230 ScopedObjectAccess soa(Thread::Current());
231 ClassLinker* cl = Runtime::Current()->GetClassLinker();
232 mirror::DexCache* dex_cache = cl->FindDexCache(info.GetDexFile());
233 ArtField* field = cl->GetResolvedField(info.GetFieldIndex(), dex_cache);
Calin Juravle183617a2015-06-18 18:38:29 +0100234 if (field != nullptr) {
235 mirror::Class* klass = field->GetType<false>();
236 SetClassAsTypeInfo(instr, klass, /* is_exact */ false);
237 }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100238}
239
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100240void RTPVisitor::VisitInstanceFieldGet(HInstanceFieldGet* instr) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100241 UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
242}
243
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100244void RTPVisitor::VisitStaticFieldGet(HStaticFieldGet* instr) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100245 UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
246}
247
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100248void RTPVisitor::VisitLoadClass(HLoadClass* instr) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000249 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100250 mirror::DexCache* dex_cache =
251 Runtime::Current()->GetClassLinker()->FindDexCache(instr->GetDexFile());
Calin Juravleacf735c2015-02-12 15:25:22 +0000252 // Get type from dex cache assuming it was populated by the verifier.
253 mirror::Class* resolved_class = dex_cache->GetResolvedType(instr->GetTypeIndex());
254 if (resolved_class != nullptr) {
255 Handle<mirror::Class> handle = handles_->NewHandle(resolved_class);
Calin Juravleb1498f62015-02-16 13:13:29 +0000256 instr->SetLoadedClassRTI(ReferenceTypeInfo::Create(handle, /* is_exact */ true));
Calin Juravleacf735c2015-02-12 15:25:22 +0000257 }
258 Handle<mirror::Class> class_handle = handles_->NewHandle(mirror::Class::GetJavaLangClass());
Calin Juravleb1498f62015-02-16 13:13:29 +0000259 instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(class_handle, /* is_exact */ true));
Calin Juravleacf735c2015-02-12 15:25:22 +0000260}
Calin Juravle10e244f2015-01-26 18:54:32 +0000261
Calin Juravleb1498f62015-02-16 13:13:29 +0000262void ReferenceTypePropagation::VisitPhi(HPhi* phi) {
263 if (phi->GetType() != Primitive::kPrimNot) {
264 return;
Calin Juravleacf735c2015-02-12 15:25:22 +0000265 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000266
267 if (phi->GetBlock()->IsLoopHeader()) {
268 // Set the initial type for the phi. Use the non back edge input for reaching
269 // a fixed point faster.
270 AddToWorklist(phi);
271 phi->SetCanBeNull(phi->InputAt(0)->CanBeNull());
272 phi->SetReferenceTypeInfo(phi->InputAt(0)->GetReferenceTypeInfo());
Calin Juravle10e244f2015-01-26 18:54:32 +0000273 } else {
Calin Juravleb1498f62015-02-16 13:13:29 +0000274 // Eagerly compute the type of the phi, for quicker convergence. Note
275 // that we don't need to add users to the worklist because we are
276 // doing a reverse post-order visit, therefore either the phi users are
277 // non-loop phi and will be visited later in the visit, or are loop-phis,
278 // and they are already in the work list.
279 UpdateNullability(phi);
280 UpdateReferenceTypeInfo(phi);
281 }
282}
283
284ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a,
285 const ReferenceTypeInfo& b) {
286 bool is_exact = a.IsExact() && b.IsExact();
287 bool is_top = a.IsTop() || b.IsTop();
288 Handle<mirror::Class> type_handle;
289
290 if (!is_top) {
291 if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) {
292 type_handle = a.GetTypeHandle();
293 } else if (a.IsSupertypeOf(b)) {
294 type_handle = a.GetTypeHandle();
295 is_exact = false;
296 } else if (b.IsSupertypeOf(a)) {
297 type_handle = b.GetTypeHandle();
298 is_exact = false;
299 } else {
300 // TODO: Find a common super class.
301 is_top = true;
302 is_exact = false;
303 }
304 }
305
306 return is_top
307 ? ReferenceTypeInfo::CreateTop(is_exact)
308 : ReferenceTypeInfo::Create(type_handle, is_exact);
309}
310
311bool ReferenceTypePropagation::UpdateReferenceTypeInfo(HInstruction* instr) {
312 ScopedObjectAccess soa(Thread::Current());
313
314 ReferenceTypeInfo previous_rti = instr->GetReferenceTypeInfo();
315 if (instr->IsBoundType()) {
316 UpdateBoundType(instr->AsBoundType());
317 } else if (instr->IsPhi()) {
318 UpdatePhi(instr->AsPhi());
319 } else {
320 LOG(FATAL) << "Invalid instruction (should not get here)";
321 }
322
323 return !previous_rti.IsEqual(instr->GetReferenceTypeInfo());
324}
325
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100326void RTPVisitor::VisitInvoke(HInvoke* instr) {
327 if (instr->GetType() != Primitive::kPrimNot) {
328 return;
329 }
330
331 ScopedObjectAccess soa(Thread::Current());
332 ClassLinker* cl = Runtime::Current()->GetClassLinker();
333 mirror::DexCache* dex_cache = cl->FindDexCache(instr->GetDexFile());
334 ArtMethod* method = dex_cache->GetResolvedMethod(
335 instr->GetDexMethodIndex(), cl->GetImagePointerSize());
Calin Juravle183617a2015-06-18 18:38:29 +0100336 if (method != nullptr) {
337 mirror::Class* klass = method->GetReturnType(false);
338 SetClassAsTypeInfo(instr, klass, /* is_exact */ false);
339 }
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100340}
341
Guillaume "Vermeille" Sanchez72a5eb52015-06-02 17:39:45 +0100342void RTPVisitor::VisitArrayGet(HArrayGet* instr) {
343 if (instr->GetType() != Primitive::kPrimNot) {
344 return;
345 }
346
347 HInstruction* parent = instr->InputAt(0);
348 ScopedObjectAccess soa(Thread::Current());
349 Handle<mirror::Class> handle = parent->GetReferenceTypeInfo().GetTypeHandle();
350 if (handle.GetReference() != nullptr && handle->IsObjectArrayClass()) {
351 SetClassAsTypeInfo(instr, handle->GetComponentType(), /* is_exact */ false);
352 }
353}
354
Calin Juravleb1498f62015-02-16 13:13:29 +0000355void ReferenceTypePropagation::UpdateBoundType(HBoundType* instr) {
356 ReferenceTypeInfo new_rti = instr->InputAt(0)->GetReferenceTypeInfo();
357 // Be sure that we don't go over the bounded type.
358 ReferenceTypeInfo bound_rti = instr->GetBoundType();
359 if (!bound_rti.IsSupertypeOf(new_rti)) {
360 new_rti = bound_rti;
361 }
362 instr->SetReferenceTypeInfo(new_rti);
363}
364
365void ReferenceTypePropagation::UpdatePhi(HPhi* instr) {
366 ReferenceTypeInfo new_rti = instr->InputAt(0)->GetReferenceTypeInfo();
367 if (new_rti.IsTop() && !new_rti.IsExact()) {
368 // Early return if we are Top and inexact.
369 instr->SetReferenceTypeInfo(new_rti);
370 return;
371 }
372 for (size_t i = 1; i < instr->InputCount(); i++) {
373 new_rti = MergeTypes(new_rti, instr->InputAt(i)->GetReferenceTypeInfo());
374 if (new_rti.IsTop()) {
375 if (!new_rti.IsExact()) {
376 break;
377 } else {
378 continue;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000379 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000380 }
381 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000382 instr->SetReferenceTypeInfo(new_rti);
383}
384
385// Re-computes and updates the nullability of the instruction. Returns whether or
386// not the nullability was changed.
387bool ReferenceTypePropagation::UpdateNullability(HInstruction* instr) {
388 DCHECK(instr->IsPhi() || instr->IsBoundType());
389
390 if (!instr->IsPhi()) {
391 return false;
392 }
393
394 HPhi* phi = instr->AsPhi();
395 bool existing_can_be_null = phi->CanBeNull();
396 bool new_can_be_null = false;
397 for (size_t i = 0; i < phi->InputCount(); i++) {
398 new_can_be_null |= phi->InputAt(i)->CanBeNull();
399 }
400 phi->SetCanBeNull(new_can_be_null);
401
402 return existing_can_be_null != new_can_be_null;
Calin Juravle10e244f2015-01-26 18:54:32 +0000403}
404
405void ReferenceTypePropagation::ProcessWorklist() {
406 while (!worklist_.IsEmpty()) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000407 HInstruction* instruction = worklist_.Pop();
Calin Juravleacf735c2015-02-12 15:25:22 +0000408 if (UpdateNullability(instruction) || UpdateReferenceTypeInfo(instruction)) {
Calin Juravle10e244f2015-01-26 18:54:32 +0000409 AddDependentInstructionsToWorklist(instruction);
410 }
411 }
412}
413
Calin Juravleb1498f62015-02-16 13:13:29 +0000414void ReferenceTypePropagation::AddToWorklist(HInstruction* instruction) {
415 DCHECK_EQ(instruction->GetType(), Primitive::kPrimNot) << instruction->GetType();
Calin Juravle10e244f2015-01-26 18:54:32 +0000416 worklist_.Add(instruction);
417}
418
Calin Juravleb1498f62015-02-16 13:13:29 +0000419void ReferenceTypePropagation::AddDependentInstructionsToWorklist(HInstruction* instruction) {
Calin Juravle10e244f2015-01-26 18:54:32 +0000420 for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000421 HInstruction* user = it.Current()->GetUser();
422 if (user->IsPhi() || user->IsBoundType()) {
423 AddToWorklist(user);
Calin Juravle10e244f2015-01-26 18:54:32 +0000424 }
425 }
426}
Calin Juravle10e244f2015-01-26 18:54:32 +0000427} // namespace art