1 /*
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 
19 #include "art_field-inl.h"
20 #include "art_method-inl.h"
21 #include "base/arena_allocator.h"
22 #include "base/pointer_size.h"
23 #include "base/scoped_arena_allocator.h"
24 #include "base/scoped_arena_containers.h"
25 #include "class_linker-inl.h"
26 #include "class_root-inl.h"
27 #include "handle_scope-inl.h"
28 #include "mirror/class-inl.h"
29 #include "mirror/dex_cache.h"
30 #include "scoped_thread_state_change-inl.h"
31 
32 namespace art HIDDEN {
33 
FindDexCacheWithHint(Thread * self,const DexFile & dex_file,Handle<mirror::DexCache> hint_dex_cache)34 static inline ObjPtr<mirror::DexCache> FindDexCacheWithHint(
35     Thread* self, const DexFile& dex_file, Handle<mirror::DexCache> hint_dex_cache)
36     REQUIRES_SHARED(Locks::mutator_lock_) {
37   if (LIKELY(hint_dex_cache->GetDexFile() == &dex_file)) {
38     return hint_dex_cache.Get();
39   } else {
40     return Runtime::Current()->GetClassLinker()->FindDexCache(self, dex_file);
41   }
42 }
43 
44 class ReferenceTypePropagation::RTPVisitor final : public HGraphDelegateVisitor {
45  public:
RTPVisitor(HGraph * graph,Handle<mirror::DexCache> hint_dex_cache,bool is_first_run)46   RTPVisitor(HGraph* graph, Handle<mirror::DexCache> hint_dex_cache, bool is_first_run)
47       : HGraphDelegateVisitor(graph),
48         hint_dex_cache_(hint_dex_cache),
49         allocator_(graph->GetArenaStack()),
50         worklist_(allocator_.Adapter(kArenaAllocReferenceTypePropagation)),
51         is_first_run_(is_first_run) {
52     worklist_.reserve(kDefaultWorklistSize);
53   }
54 
55   void VisitDeoptimize(HDeoptimize* deopt) override;
56   void VisitNewInstance(HNewInstance* new_instance) override;
57   void VisitLoadClass(HLoadClass* load_class) override;
58   void VisitInstanceOf(HInstanceOf* load_class) override;
59   void VisitClinitCheck(HClinitCheck* clinit_check) override;
60   void VisitLoadMethodHandle(HLoadMethodHandle* instr) override;
61   void VisitLoadMethodType(HLoadMethodType* instr) override;
62   void VisitLoadString(HLoadString* instr) override;
63   void VisitLoadException(HLoadException* instr) override;
64   void VisitNewArray(HNewArray* instr) override;
65   void VisitParameterValue(HParameterValue* instr) override;
66   void VisitInstanceFieldGet(HInstanceFieldGet* instr) override;
67   void VisitStaticFieldGet(HStaticFieldGet* instr) override;
68   void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* instr) override;
69   void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* instr) override;
70   void VisitInvoke(HInvoke* instr) override;
71   void VisitArrayGet(HArrayGet* instr) override;
72   void VisitCheckCast(HCheckCast* instr) override;
73   void VisitBoundType(HBoundType* instr) override;
74   void VisitNullCheck(HNullCheck* instr) override;
75   void VisitPhi(HPhi* phi) override;
76 
77   void VisitBasicBlock(HBasicBlock* block) override;
78   void ProcessWorklist();
79 
80  private:
81   void UpdateFieldAccessTypeInfo(HInstruction* instr, const FieldInfo& info);
82   void SetClassAsTypeInfo(HInstruction* instr, ObjPtr<mirror::Class> klass, bool is_exact)
83       REQUIRES_SHARED(Locks::mutator_lock_);
84   void BoundTypeForIfNotNull(HBasicBlock* block);
85   static void BoundTypeForIfInstanceOf(HBasicBlock* block);
86   static bool UpdateNullability(HInstruction* instr);
87   static void UpdateBoundType(HBoundType* bound_type) REQUIRES_SHARED(Locks::mutator_lock_);
88   void UpdateArrayGet(HArrayGet* instr) REQUIRES_SHARED(Locks::mutator_lock_);
89   void UpdatePhi(HPhi* phi) REQUIRES_SHARED(Locks::mutator_lock_);
90   bool UpdateReferenceTypeInfo(HInstruction* instr);
91   void UpdateReferenceTypeInfo(HInstruction* instr,
92                                dex::TypeIndex type_idx,
93                                const DexFile& dex_file,
94                                bool is_exact);
95 
96   // Returns true if this is an instruction we might need to recursively update.
97   // The types are (live) Phi, BoundType, ArrayGet, and NullCheck
98   static constexpr bool IsUpdateable(const HInstruction* instr);
99   void AddToWorklist(HInstruction* instruction);
100   void AddDependentInstructionsToWorklist(HInstruction* instruction);
101 
GetHandleCache()102   HandleCache* GetHandleCache() {
103     return GetGraph()->GetHandleCache();
104   }
105 
106   static constexpr size_t kDefaultWorklistSize = 8;
107 
108   Handle<mirror::DexCache> hint_dex_cache_;
109 
110   // Use local allocator for allocating memory.
111   ScopedArenaAllocator allocator_;
112   ScopedArenaVector<HInstruction*> worklist_;
113   const bool is_first_run_;
114 
115   friend class ReferenceTypePropagation;
116 };
117 
ReferenceTypePropagation(HGraph * graph,Handle<mirror::DexCache> hint_dex_cache,bool is_first_run,const char * name)118 ReferenceTypePropagation::ReferenceTypePropagation(HGraph* graph,
119                                                    Handle<mirror::DexCache> hint_dex_cache,
120                                                    bool is_first_run,
121                                                    const char* name)
122     : HOptimization(graph, name), hint_dex_cache_(hint_dex_cache), is_first_run_(is_first_run) {}
123 
Visit(HInstruction * instruction)124 void ReferenceTypePropagation::Visit(HInstruction* instruction) {
125   RTPVisitor visitor(graph_, hint_dex_cache_, is_first_run_);
126   instruction->Accept(&visitor);
127 }
128 
Visit(ArrayRef<HInstruction * const> instructions)129 void ReferenceTypePropagation::Visit(ArrayRef<HInstruction* const> instructions) {
130   RTPVisitor visitor(graph_, hint_dex_cache_, is_first_run_);
131   for (HInstruction* instruction : instructions) {
132     if (instruction->IsPhi()) {
133       // Need to force phis to recalculate null-ness.
134       instruction->AsPhi()->SetCanBeNull(false);
135     }
136   }
137   for (HInstruction* instruction : instructions) {
138     instruction->Accept(&visitor);
139     // We don't know if the instruction list is ordered in the same way normal
140     // visiting would be so we need to process every instruction manually.
141     if (RTPVisitor::IsUpdateable(instruction)) {
142       visitor.AddToWorklist(instruction);
143     }
144   }
145   visitor.ProcessWorklist();
146 }
147 
148 // Check if we should create a bound type for the given object at the specified
149 // position. Because of inlining and the fact we run RTP more than once and we
150 // might have a HBoundType already. If we do, we should not create a new one.
151 // In this case we also assert that there are no other uses of the object (except
152 // the bound type) dominated by the specified dominator_instr or dominator_block.
ShouldCreateBoundType(HInstruction * position,HInstruction * obj,ReferenceTypeInfo upper_bound,HInstruction * dominator_instr,HBasicBlock * dominator_block)153 static bool ShouldCreateBoundType(HInstruction* position,
154                                   HInstruction* obj,
155                                   ReferenceTypeInfo upper_bound,
156                                   HInstruction* dominator_instr,
157                                   HBasicBlock* dominator_block)
158     REQUIRES_SHARED(Locks::mutator_lock_) {
159   // If the position where we should insert the bound type is not already a
160   // a bound type then we need to create one.
161   if (position == nullptr || !position->IsBoundType()) {
162     return true;
163   }
164 
165   HBoundType* existing_bound_type = position->AsBoundType();
166   if (existing_bound_type->GetUpperBound().IsSupertypeOf(upper_bound)) {
167     if (kIsDebugBuild) {
168       // Check that the existing HBoundType dominates all the uses.
169       for (const HUseListNode<HInstruction*>& use : obj->GetUses()) {
170         HInstruction* user = use.GetUser();
171         if (dominator_instr != nullptr) {
172           DCHECK(!dominator_instr->StrictlyDominates(user)
173               || user == existing_bound_type
174               || existing_bound_type->StrictlyDominates(user));
175         } else if (dominator_block != nullptr) {
176           DCHECK(!dominator_block->Dominates(user->GetBlock())
177               || user == existing_bound_type
178               || existing_bound_type->StrictlyDominates(user));
179         }
180       }
181     }
182   } else {
183     // TODO: if the current bound type is a refinement we could update the
184     // existing_bound_type with the a new upper limit. However, we also need to
185     // update its users and have access to the work list.
186   }
187   return false;
188 }
189 
190 // Helper method to bound the type of `receiver` for all instructions dominated
191 // by `start_block`, or `start_instruction` if `start_block` is null. The new
192 // bound type will have its upper bound be `class_rti`.
BoundTypeIn(HInstruction * receiver,HBasicBlock * start_block,HInstruction * start_instruction,const ReferenceTypeInfo & class_rti)193 static void BoundTypeIn(HInstruction* receiver,
194                         HBasicBlock* start_block,
195                         HInstruction* start_instruction,
196                         const ReferenceTypeInfo& class_rti) {
197   // We only need to bound the type if we have uses in the relevant block.
198   // So start with null and create the HBoundType lazily, only if it's needed.
199   HBoundType* bound_type = nullptr;
200   DCHECK(!receiver->IsLoadClass()) << "We should not replace HLoadClass instructions";
201   const HUseList<HInstruction*>& uses = receiver->GetUses();
202   for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
203     HInstruction* user = it->GetUser();
204     size_t index = it->GetIndex();
205     // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput().
206     ++it;
207     bool dominates = (start_instruction != nullptr)
208         ? start_instruction->StrictlyDominates(user)
209         : start_block->Dominates(user->GetBlock());
210     if (!dominates) {
211       continue;
212     }
213     if (bound_type == nullptr) {
214       ScopedObjectAccess soa(Thread::Current());
215       HInstruction* insert_point = (start_instruction != nullptr)
216           ? start_instruction->GetNext()
217           : start_block->GetFirstInstruction();
218       if (ShouldCreateBoundType(
219             insert_point, receiver, class_rti, start_instruction, start_block)) {
220         bound_type = new (receiver->GetBlock()->GetGraph()->GetAllocator()) HBoundType(receiver);
221         bound_type->SetUpperBound(class_rti, /* can_be_null= */ false);
222         start_block->InsertInstructionBefore(bound_type, insert_point);
223         // To comply with the RTP algorithm, don't type the bound type just yet, it will
224         // be handled in RTPVisitor::VisitBoundType.
225       } else {
226         // We already have a bound type on the position we would need to insert
227         // the new one. The existing bound type should dominate all the users
228         // (dchecked) so there's no need to continue.
229         break;
230       }
231     }
232     user->ReplaceInput(bound_type, index);
233   }
234   // If the receiver is a null check, also bound the type of the actual
235   // receiver.
236   if (receiver->IsNullCheck()) {
237     BoundTypeIn(receiver->InputAt(0), start_block, start_instruction, class_rti);
238   }
239 }
240 
241 // Recognize the patterns:
242 // if (obj.shadow$_klass_ == Foo.class) ...
243 // deoptimize if (obj.shadow$_klass_ == Foo.class)
BoundTypeForClassCheck(HInstruction * check)244 static void BoundTypeForClassCheck(HInstruction* check) {
245   if (!check->IsIf() && !check->IsDeoptimize()) {
246     return;
247   }
248   HInstruction* compare = check->InputAt(0);
249   if (!compare->IsEqual() && !compare->IsNotEqual()) {
250     return;
251   }
252   HInstruction* input_one = compare->InputAt(0);
253   HInstruction* input_two = compare->InputAt(1);
254   HLoadClass* load_class = input_one->IsLoadClass()
255       ? input_one->AsLoadClass()
256       : input_two->AsLoadClassOrNull();
257   if (load_class == nullptr) {
258     return;
259   }
260 
261   ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
262   if (!class_rti.IsValid()) {
263     // We have loaded an unresolved class. Don't bother bounding the type.
264     return;
265   }
266 
267   HInstruction* field_get = (load_class == input_one) ? input_two : input_one;
268   if (!field_get->IsInstanceFieldGet()) {
269     return;
270   }
271   HInstruction* receiver = field_get->InputAt(0);
272   ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo();
273   if (receiver_type.IsExact()) {
274     // If we already know the receiver type, don't bother updating its users.
275     return;
276   }
277 
278   {
279     ScopedObjectAccess soa(Thread::Current());
280     ArtField* field = GetClassRoot<mirror::Object>()->GetInstanceField(0);
281     DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
282     if (field_get->GetFieldInfo().GetField() != field) {
283       return;
284     }
285   }
286 
287   if (check->IsIf()) {
288     HBasicBlock* trueBlock = compare->IsEqual()
289         ? check->AsIf()->IfTrueSuccessor()
290         : check->AsIf()->IfFalseSuccessor();
291     BoundTypeIn(receiver, trueBlock, /* start_instruction= */ nullptr, class_rti);
292   } else {
293     DCHECK(check->IsDeoptimize());
294     if (compare->IsEqual() && check->AsDeoptimize()->GuardsAnInput()) {
295       check->SetReferenceTypeInfo(class_rti);
296     }
297   }
298 }
299 
Run()300 bool ReferenceTypePropagation::Run() {
301   DCHECK(Thread::Current() != nullptr)
302       << "ReferenceTypePropagation requires the use of Thread::Current(). Make sure you have a "
303       << "Runtime initialized before calling this optimization pass";
304   RTPVisitor visitor(graph_, hint_dex_cache_, is_first_run_);
305 
306   // To properly propagate type info we need to visit in the dominator-based order.
307   // Reverse post order guarantees a node's dominators are visited first.
308   // We take advantage of this order in `VisitBasicBlock`.
309   for (HBasicBlock* block : graph_->GetReversePostOrder()) {
310     visitor.VisitBasicBlock(block);
311   }
312 
313   visitor.ProcessWorklist();
314   return true;
315 }
316 
VisitBasicBlock(HBasicBlock * block)317 void ReferenceTypePropagation::RTPVisitor::VisitBasicBlock(HBasicBlock* block) {
318   // Handle Phis first as there might be instructions in the same block who depend on them.
319   VisitPhis(block);
320 
321   // Handle instructions. Since RTP may add HBoundType instructions just after the
322   // last visited instruction, use `HInstructionIteratorHandleChanges` iterator.
323   VisitNonPhiInstructions(block);
324 
325   // Add extra nodes to bound types.
326   BoundTypeForIfNotNull(block);
327   BoundTypeForIfInstanceOf(block);
328   BoundTypeForClassCheck(block->GetLastInstruction());
329 }
330 
BoundTypeForIfNotNull(HBasicBlock * block)331 void ReferenceTypePropagation::RTPVisitor::BoundTypeForIfNotNull(HBasicBlock* block) {
332   HIf* ifInstruction = block->GetLastInstruction()->AsIfOrNull();
333   if (ifInstruction == nullptr) {
334     return;
335   }
336   HInstruction* ifInput = ifInstruction->InputAt(0);
337   if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) {
338     return;
339   }
340   HInstruction* input0 = ifInput->InputAt(0);
341   HInstruction* input1 = ifInput->InputAt(1);
342   HInstruction* obj = nullptr;
343 
344   if (input1->IsNullConstant()) {
345     obj = input0;
346   } else if (input0->IsNullConstant()) {
347     obj = input1;
348   } else {
349     return;
350   }
351 
352   if (!obj->CanBeNull() || obj->IsNullConstant()) {
353     // Null check is dead code and will be removed by DCE.
354     return;
355   }
356   DCHECK(!obj->IsLoadClass()) << "We should not replace HLoadClass instructions";
357 
358   // We only need to bound the type if we have uses in the relevant block.
359   // So start with null and create the HBoundType lazily, only if it's needed.
360   HBasicBlock* notNullBlock = ifInput->IsNotEqual()
361       ? ifInstruction->IfTrueSuccessor()
362       : ifInstruction->IfFalseSuccessor();
363 
364   ReferenceTypeInfo object_rti =
365       ReferenceTypeInfo::Create(GetHandleCache()->GetObjectClassHandle(), /* is_exact= */ false);
366 
367   BoundTypeIn(obj, notNullBlock, /* start_instruction= */ nullptr, object_rti);
368 }
369 
370 // Returns true if one of the patterns below has been recognized. If so, the
371 // InstanceOf instruction together with the true branch of `ifInstruction` will
372 // be returned using the out parameters.
373 // Recognized patterns:
374 //   (1) patterns equivalent to `if (obj instanceof X)`
375 //     (a) InstanceOf -> Equal to 1 -> If
376 //     (b) InstanceOf -> NotEqual to 0 -> If
377 //     (c) InstanceOf -> If
378 //   (2) patterns equivalent to `if (!(obj instanceof X))`
379 //     (a) InstanceOf -> Equal to 0 -> If
380 //     (b) InstanceOf -> NotEqual to 1 -> If
381 //     (c) InstanceOf -> BooleanNot -> If
MatchIfInstanceOf(HIf * ifInstruction,HInstanceOf ** instanceOf,HBasicBlock ** trueBranch)382 static bool MatchIfInstanceOf(HIf* ifInstruction,
383                               /* out */ HInstanceOf** instanceOf,
384                               /* out */ HBasicBlock** trueBranch) {
385   HInstruction* input = ifInstruction->InputAt(0);
386 
387   if (input->IsEqual()) {
388     HInstruction* rhs = input->AsEqual()->GetConstantRight();
389     if (rhs != nullptr) {
390       HInstruction* lhs = input->AsEqual()->GetLeastConstantLeft();
391       if (lhs->IsInstanceOf() && rhs->IsIntConstant()) {
392         if (rhs->AsIntConstant()->IsTrue()) {
393           // Case (1a)
394           *trueBranch = ifInstruction->IfTrueSuccessor();
395         } else if (rhs->AsIntConstant()->IsFalse()) {
396           // Case (2a)
397           *trueBranch = ifInstruction->IfFalseSuccessor();
398         } else {
399           // Sometimes we see a comparison of instance-of with a constant which is neither 0 nor 1.
400           // In those cases, we cannot do the match if+instance-of.
401           return false;
402         }
403         *instanceOf = lhs->AsInstanceOf();
404         return true;
405       }
406     }
407   } else if (input->IsNotEqual()) {
408     HInstruction* rhs = input->AsNotEqual()->GetConstantRight();
409     if (rhs != nullptr) {
410       HInstruction* lhs = input->AsNotEqual()->GetLeastConstantLeft();
411       if (lhs->IsInstanceOf() && rhs->IsIntConstant()) {
412         if (rhs->AsIntConstant()->IsFalse()) {
413           // Case (1b)
414           *trueBranch = ifInstruction->IfTrueSuccessor();
415         } else if (rhs->AsIntConstant()->IsTrue()) {
416           // Case (2b)
417           *trueBranch = ifInstruction->IfFalseSuccessor();
418         } else {
419           // Sometimes we see a comparison of instance-of with a constant which is neither 0 nor 1.
420           // In those cases, we cannot do the match if+instance-of.
421           return false;
422         }
423         *instanceOf = lhs->AsInstanceOf();
424         return true;
425       }
426     }
427   } else if (input->IsInstanceOf()) {
428     // Case (1c)
429     *instanceOf = input->AsInstanceOf();
430     *trueBranch = ifInstruction->IfTrueSuccessor();
431     return true;
432   } else if (input->IsBooleanNot()) {
433     HInstruction* not_input = input->InputAt(0);
434     if (not_input->IsInstanceOf()) {
435       // Case (2c)
436       *instanceOf = not_input->AsInstanceOf();
437       *trueBranch = ifInstruction->IfFalseSuccessor();
438       return true;
439     }
440   }
441 
442   return false;
443 }
444 
445 // Detects if `block` is the True block for the pattern
446 // `if (x instanceof ClassX) { }`
447 // If that's the case insert an HBoundType instruction to bound the type of `x`
448 // to `ClassX` in the scope of the dominated blocks.
BoundTypeForIfInstanceOf(HBasicBlock * block)449 void ReferenceTypePropagation::RTPVisitor::BoundTypeForIfInstanceOf(HBasicBlock* block) {
450   HIf* ifInstruction = block->GetLastInstruction()->AsIfOrNull();
451   if (ifInstruction == nullptr) {
452     return;
453   }
454 
455   // Try to recognize common `if (instanceof)` and `if (!instanceof)` patterns.
456   HInstanceOf* instanceOf = nullptr;
457   HBasicBlock* instanceOfTrueBlock = nullptr;
458   if (!MatchIfInstanceOf(ifInstruction, &instanceOf, &instanceOfTrueBlock)) {
459     return;
460   }
461 
462   ReferenceTypeInfo class_rti = instanceOf->GetTargetClassRTI();
463   if (!class_rti.IsValid()) {
464     // We have loaded an unresolved class. Don't bother bounding the type.
465     return;
466   }
467 
468   HInstruction* obj = instanceOf->InputAt(0);
469   if (obj->GetReferenceTypeInfo().IsExact() && !obj->IsPhi()) {
470     // This method is being called while doing a fixed-point calculation
471     // over phis. Non-phis instruction whose type is already known do
472     // not need to be bound to another type.
473     // Not that this also prevents replacing `HLoadClass` with a `HBoundType`.
474     // `HCheckCast` and `HInstanceOf` expect a `HLoadClass` as a second
475     // input.
476     return;
477   }
478 
479   {
480     ScopedObjectAccess soa(Thread::Current());
481     if (!class_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes()) {
482       class_rti = ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact= */ false);
483     }
484   }
485   BoundTypeIn(obj, instanceOfTrueBlock, /* start_instruction= */ nullptr, class_rti);
486 }
487 
SetClassAsTypeInfo(HInstruction * instr,ObjPtr<mirror::Class> klass,bool is_exact)488 void ReferenceTypePropagation::RTPVisitor::SetClassAsTypeInfo(HInstruction* instr,
489                                                               ObjPtr<mirror::Class> klass,
490                                                               bool is_exact) {
491   if (instr->IsInvokeStaticOrDirect() && instr->AsInvokeStaticOrDirect()->IsStringInit()) {
492     // Calls to String.<init> are replaced with a StringFactory.
493     if (kIsDebugBuild) {
494       HInvokeStaticOrDirect* invoke = instr->AsInvokeStaticOrDirect();
495       ClassLinker* cl = Runtime::Current()->GetClassLinker();
496       Thread* self = Thread::Current();
497       StackHandleScope<2> hs(self);
498       const DexFile& dex_file = *invoke->GetResolvedMethodReference().dex_file;
499       uint32_t dex_method_index = invoke->GetResolvedMethodReference().index;
500       Handle<mirror::DexCache> dex_cache(
501           hs.NewHandle(FindDexCacheWithHint(self, dex_file, hint_dex_cache_)));
502       // Use a null loader, the target method is in a boot classpath dex file.
503       Handle<mirror::ClassLoader> loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
504       ArtMethod* method = cl->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
505           dex_method_index, dex_cache, loader, /* referrer= */ nullptr, kDirect);
506       DCHECK(method != nullptr);
507       ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
508       DCHECK(declaring_class != nullptr);
509       DCHECK(declaring_class->IsStringClass())
510           << "Expected String class: " << declaring_class->PrettyDescriptor();
511       DCHECK(method->IsConstructor())
512           << "Expected String.<init>: " << method->PrettyMethod();
513     }
514     instr->SetReferenceTypeInfo(
515         ReferenceTypeInfo::Create(GetHandleCache()->GetStringClassHandle(), /* is_exact= */ true));
516   } else if (IsAdmissible(klass)) {
517     ReferenceTypeInfo::TypeHandle handle = GetHandleCache()->NewHandle(klass);
518     is_exact = is_exact || handle->CannotBeAssignedFromOtherTypes();
519     instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(handle, is_exact));
520   } else {
521     instr->SetReferenceTypeInfo(GetGraph()->GetInexactObjectRti());
522   }
523 }
524 
VisitDeoptimize(HDeoptimize * instr)525 void ReferenceTypePropagation::RTPVisitor::VisitDeoptimize(HDeoptimize* instr) {
526   BoundTypeForClassCheck(instr);
527 }
528 
UpdateReferenceTypeInfo(HInstruction * instr,dex::TypeIndex type_idx,const DexFile & dex_file,bool is_exact)529 void ReferenceTypePropagation::RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr,
530                                                                    dex::TypeIndex type_idx,
531                                                                    const DexFile& dex_file,
532                                                                    bool is_exact) {
533   DCHECK_EQ(instr->GetType(), DataType::Type::kReference);
534 
535   ScopedObjectAccess soa(Thread::Current());
536   StackHandleScope<2> hs(soa.Self());
537   Handle<mirror::DexCache> dex_cache =
538       hs.NewHandle(FindDexCacheWithHint(soa.Self(), dex_file, hint_dex_cache_));
539   Handle<mirror::ClassLoader> loader = hs.NewHandle(dex_cache->GetClassLoader());
540   ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->ResolveType(
541       type_idx, dex_cache, loader);
542   DCHECK_EQ(klass == nullptr, soa.Self()->IsExceptionPending());
543   soa.Self()->ClearException();  // Clean up the exception left by type resolution if any.
544   SetClassAsTypeInfo(instr, klass, is_exact);
545 }
546 
VisitNewInstance(HNewInstance * instr)547 void ReferenceTypePropagation::RTPVisitor::VisitNewInstance(HNewInstance* instr) {
548   ScopedObjectAccess soa(Thread::Current());
549   SetClassAsTypeInfo(instr, instr->GetLoadClass()->GetClass().Get(), /* is_exact= */ true);
550 }
551 
VisitNewArray(HNewArray * instr)552 void ReferenceTypePropagation::RTPVisitor::VisitNewArray(HNewArray* instr) {
553   ScopedObjectAccess soa(Thread::Current());
554   SetClassAsTypeInfo(instr, instr->GetLoadClass()->GetClass().Get(), /* is_exact= */ true);
555 }
556 
VisitParameterValue(HParameterValue * instr)557 void ReferenceTypePropagation::RTPVisitor::VisitParameterValue(HParameterValue* instr) {
558   // We check if the existing type is valid: the inliner may have set it.
559   if (instr->GetType() == DataType::Type::kReference && !instr->GetReferenceTypeInfo().IsValid()) {
560     UpdateReferenceTypeInfo(instr,
561                             instr->GetTypeIndex(),
562                             instr->GetDexFile(),
563                             /* is_exact= */ false);
564   }
565 }
566 
UpdateFieldAccessTypeInfo(HInstruction * instr,const FieldInfo & info)567 void ReferenceTypePropagation::RTPVisitor::UpdateFieldAccessTypeInfo(HInstruction* instr,
568                                                                      const FieldInfo& info) {
569   if (instr->GetType() != DataType::Type::kReference) {
570     return;
571   }
572 
573   ScopedObjectAccess soa(Thread::Current());
574   ObjPtr<mirror::Class> klass;
575 
576   // The field is unknown only during tests.
577   if (info.GetField() != nullptr) {
578     klass = info.GetField()->LookupResolvedType();
579   }
580 
581   SetClassAsTypeInfo(instr, klass, /* is_exact= */ false);
582 }
583 
VisitInstanceFieldGet(HInstanceFieldGet * instr)584 void ReferenceTypePropagation::RTPVisitor::VisitInstanceFieldGet(HInstanceFieldGet* instr) {
585   UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
586 }
587 
VisitStaticFieldGet(HStaticFieldGet * instr)588 void ReferenceTypePropagation::RTPVisitor::VisitStaticFieldGet(HStaticFieldGet* instr) {
589   UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
590 }
591 
VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet * instr)592 void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedInstanceFieldGet(
593     HUnresolvedInstanceFieldGet* instr) {
594   // TODO: Use descriptor to get the actual type.
595   if (instr->GetFieldType() == DataType::Type::kReference) {
596     instr->SetReferenceTypeInfo(GetGraph()->GetInexactObjectRti());
597   }
598 }
599 
VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet * instr)600 void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedStaticFieldGet(
601     HUnresolvedStaticFieldGet* instr) {
602   // TODO: Use descriptor to get the actual type.
603   if (instr->GetFieldType() == DataType::Type::kReference) {
604     instr->SetReferenceTypeInfo(GetGraph()->GetInexactObjectRti());
605   }
606 }
607 
VisitLoadClass(HLoadClass * instr)608 void ReferenceTypePropagation::RTPVisitor::VisitLoadClass(HLoadClass* instr) {
609   ScopedObjectAccess soa(Thread::Current());
610   if (IsAdmissible(instr->GetClass().Get())) {
611     instr->SetValidLoadedClassRTI();
612   }
613   instr->SetReferenceTypeInfo(
614       ReferenceTypeInfo::Create(GetHandleCache()->GetClassClassHandle(), /* is_exact= */ true));
615 }
616 
VisitInstanceOf(HInstanceOf * instr)617 void ReferenceTypePropagation::RTPVisitor::VisitInstanceOf(HInstanceOf* instr) {
618   ScopedObjectAccess soa(Thread::Current());
619   if (IsAdmissible(instr->GetClass().Get())) {
620     instr->SetValidTargetClassRTI();
621   }
622 }
623 
VisitClinitCheck(HClinitCheck * instr)624 void ReferenceTypePropagation::RTPVisitor::VisitClinitCheck(HClinitCheck* instr) {
625   instr->SetReferenceTypeInfo(instr->InputAt(0)->GetReferenceTypeInfo());
626 }
627 
VisitLoadMethodHandle(HLoadMethodHandle * instr)628 void ReferenceTypePropagation::RTPVisitor::VisitLoadMethodHandle(HLoadMethodHandle* instr) {
629   instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
630       GetHandleCache()->GetMethodHandleClassHandle(), /* is_exact= */ true));
631 }
632 
VisitLoadMethodType(HLoadMethodType * instr)633 void ReferenceTypePropagation::RTPVisitor::VisitLoadMethodType(HLoadMethodType* instr) {
634   instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
635       GetHandleCache()->GetMethodTypeClassHandle(), /* is_exact= */ true));
636 }
637 
VisitLoadString(HLoadString * instr)638 void ReferenceTypePropagation::RTPVisitor::VisitLoadString(HLoadString* instr) {
639   instr->SetReferenceTypeInfo(
640       ReferenceTypeInfo::Create(GetHandleCache()->GetStringClassHandle(), /* is_exact= */ true));
641 }
642 
VisitLoadException(HLoadException * instr)643 void ReferenceTypePropagation::RTPVisitor::VisitLoadException(HLoadException* instr) {
644   DCHECK(instr->GetBlock()->IsCatchBlock());
645   TryCatchInformation* catch_info = instr->GetBlock()->GetTryCatchInformation();
646 
647   if (catch_info->IsValidTypeIndex()) {
648     UpdateReferenceTypeInfo(instr,
649                             catch_info->GetCatchTypeIndex(),
650                             catch_info->GetCatchDexFile(),
651                             /* is_exact= */ false);
652   } else {
653     instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
654         GetHandleCache()->GetThrowableClassHandle(), /* is_exact= */ false));
655   }
656 }
657 
VisitNullCheck(HNullCheck * instr)658 void ReferenceTypePropagation::RTPVisitor::VisitNullCheck(HNullCheck* instr) {
659   ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
660   if (parent_rti.IsValid()) {
661     instr->SetReferenceTypeInfo(parent_rti);
662   }
663 }
664 
VisitBoundType(HBoundType * instr)665 void ReferenceTypePropagation::RTPVisitor::VisitBoundType(HBoundType* instr) {
666   ReferenceTypeInfo class_rti = instr->GetUpperBound();
667   if (class_rti.IsValid()) {
668     ScopedObjectAccess soa(Thread::Current());
669     // Narrow the type as much as possible.
670     HInstruction* obj = instr->InputAt(0);
671     ReferenceTypeInfo obj_rti = obj->GetReferenceTypeInfo();
672     if (class_rti.IsExact()) {
673       instr->SetReferenceTypeInfo(class_rti);
674     } else if (obj_rti.IsValid()) {
675       if (class_rti.IsSupertypeOf(obj_rti)) {
676         // Object type is more specific.
677         instr->SetReferenceTypeInfo(obj_rti);
678       } else {
679         // Upper bound is more specific, or unrelated to the object's type.
680         // Note that the object might then be exact, and we know the code dominated by this
681         // bound type is dead. To not confuse potential other optimizations, we mark
682         // the bound as non-exact.
683         instr->SetReferenceTypeInfo(
684             ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact= */ false));
685       }
686     } else {
687       // Object not typed yet. Leave BoundType untyped for now rather than
688       // assign the type conservatively.
689     }
690     instr->SetCanBeNull(obj->CanBeNull() && instr->GetUpperCanBeNull());
691   } else {
692     // The owner of the BoundType was already visited. If the class is unresolved,
693     // the BoundType should have been removed from the data flow and this method
694     // should remove it from the graph.
695     DCHECK(!instr->HasUses());
696     instr->GetBlock()->RemoveInstruction(instr);
697   }
698 }
699 
VisitCheckCast(HCheckCast * check_cast)700 void ReferenceTypePropagation::RTPVisitor::VisitCheckCast(HCheckCast* check_cast) {
701   HBoundType* bound_type = check_cast->GetNext()->AsBoundTypeOrNull();
702   if (bound_type == nullptr || bound_type->GetUpperBound().IsValid()) {
703     // The next instruction is not an uninitialized BoundType. This must be
704     // an RTP pass after SsaBuilder and we do not need to do anything.
705     return;
706   }
707   DCHECK_EQ(bound_type->InputAt(0), check_cast->InputAt(0));
708 
709   ScopedObjectAccess soa(Thread::Current());
710   Handle<mirror::Class> klass = check_cast->GetClass();
711   if (IsAdmissible(klass.Get())) {
712     DCHECK(is_first_run_);
713     check_cast->SetValidTargetClassRTI();
714     // This is the first run of RTP and class is resolved.
715     bool is_exact = klass->CannotBeAssignedFromOtherTypes();
716     bound_type->SetUpperBound(ReferenceTypeInfo::Create(klass, is_exact),
717                               /* CheckCast succeeds for nulls. */ true);
718   } else {
719     // This is the first run of RTP and class is unresolved. Remove the binding.
720     // The instruction itself is removed in VisitBoundType so as to not
721     // invalidate HInstructionIterator.
722     bound_type->ReplaceWith(bound_type->InputAt(0));
723   }
724 }
725 
VisitPhi(HPhi * phi)726 void ReferenceTypePropagation::RTPVisitor::VisitPhi(HPhi* phi) {
727   if (phi->IsDead() || phi->GetType() != DataType::Type::kReference) {
728     return;
729   }
730 
731   if (phi->GetBlock()->IsLoopHeader()) {
732     // Set the initial type for the phi. Use the non back edge input for reaching
733     // a fixed point faster.
734     HInstruction* first_input = phi->InputAt(0);
735     ReferenceTypeInfo first_input_rti = first_input->GetReferenceTypeInfo();
736     if (first_input_rti.IsValid() && !first_input->IsNullConstant()) {
737       phi->SetCanBeNull(first_input->CanBeNull());
738       phi->SetReferenceTypeInfo(first_input_rti);
739     }
740     AddToWorklist(phi);
741   } else {
742     // Eagerly compute the type of the phi, for quicker convergence. Note
743     // that we don't need to add users to the worklist because we are
744     // doing a reverse post-order visit, therefore either the phi users are
745     // non-loop phi and will be visited later in the visit, or are loop-phis,
746     // and they are already in the work list.
747     UpdateNullability(phi);
748     UpdateReferenceTypeInfo(phi);
749   }
750 }
751 
FixUpInstructionType(HInstruction * instruction,HandleCache * handle_cache)752 void ReferenceTypePropagation::FixUpInstructionType(HInstruction* instruction,
753                                                     HandleCache* handle_cache) {
754   if (instruction->IsSelect()) {
755     ScopedObjectAccess soa(Thread::Current());
756     HSelect* select = instruction->AsSelect();
757     ReferenceTypeInfo false_rti = select->GetFalseValue()->GetReferenceTypeInfo();
758     ReferenceTypeInfo true_rti = select->GetTrueValue()->GetReferenceTypeInfo();
759     select->SetReferenceTypeInfo(MergeTypes(false_rti, true_rti, handle_cache));
760   } else {
761     LOG(FATAL) << "Invalid instruction in FixUpInstructionType";
762   }
763 }
764 
MergeTypes(const ReferenceTypeInfo & a,const ReferenceTypeInfo & b,HandleCache * handle_cache)765 ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a,
766                                                        const ReferenceTypeInfo& b,
767                                                        HandleCache* handle_cache) {
768   if (!b.IsValid()) {
769     return a;
770   }
771   if (!a.IsValid()) {
772     return b;
773   }
774 
775   bool is_exact = a.IsExact() && b.IsExact();
776   ReferenceTypeInfo::TypeHandle result_type_handle;
777   ReferenceTypeInfo::TypeHandle a_type_handle = a.GetTypeHandle();
778   ReferenceTypeInfo::TypeHandle b_type_handle = b.GetTypeHandle();
779   bool a_is_interface = a_type_handle->IsInterface();
780   bool b_is_interface = b_type_handle->IsInterface();
781 
782   if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) {
783     result_type_handle = a_type_handle;
784   } else if (a.IsSupertypeOf(b)) {
785     result_type_handle = a_type_handle;
786     is_exact = false;
787   } else if (b.IsSupertypeOf(a)) {
788     result_type_handle = b_type_handle;
789     is_exact = false;
790   } else if (!a_is_interface && !b_is_interface) {
791     result_type_handle =
792         handle_cache->NewHandle(a_type_handle->GetCommonSuperClass(b_type_handle));
793     is_exact = false;
794   } else {
795     // This can happen if:
796     //    - both types are interfaces. TODO(calin): implement
797     //    - one is an interface, the other a class, and the type does not implement the interface
798     //      e.g:
799     //        void foo(Interface i, boolean cond) {
800     //          Object o = cond ? i : new Object();
801     //        }
802     result_type_handle = handle_cache->GetObjectClassHandle();
803     is_exact = false;
804   }
805 
806   return ReferenceTypeInfo::Create(result_type_handle, is_exact);
807 }
808 
UpdateArrayGet(HArrayGet * instr)809 void ReferenceTypePropagation::RTPVisitor::UpdateArrayGet(HArrayGet* instr) {
810   DCHECK_EQ(DataType::Type::kReference, instr->GetType());
811 
812   ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
813   if (!parent_rti.IsValid()) {
814     return;
815   }
816 
817   Handle<mirror::Class> handle = parent_rti.GetTypeHandle();
818   if (handle->IsObjectArrayClass() && IsAdmissible(handle->GetComponentType())) {
819     ReferenceTypeInfo::TypeHandle component_handle =
820         GetHandleCache()->NewHandle(handle->GetComponentType());
821     bool is_exact = component_handle->CannotBeAssignedFromOtherTypes();
822     instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(component_handle, is_exact));
823   } else {
824     // We don't know what the parent actually is, so we fallback to object.
825     instr->SetReferenceTypeInfo(GetGraph()->GetInexactObjectRti());
826   }
827 }
828 
UpdateReferenceTypeInfo(HInstruction * instr)829 bool ReferenceTypePropagation::RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr) {
830   ScopedObjectAccess soa(Thread::Current());
831 
832   ReferenceTypeInfo previous_rti = instr->GetReferenceTypeInfo();
833   if (instr->IsBoundType()) {
834     UpdateBoundType(instr->AsBoundType());
835   } else if (instr->IsPhi()) {
836     UpdatePhi(instr->AsPhi());
837   } else if (instr->IsNullCheck()) {
838     ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
839     if (parent_rti.IsValid()) {
840       instr->SetReferenceTypeInfo(parent_rti);
841     }
842   } else if (instr->IsArrayGet()) {
843     // TODO: consider if it's worth "looking back" and binding the input object
844     // to an array type.
845     UpdateArrayGet(instr->AsArrayGet());
846   } else {
847     LOG(FATAL) << "Invalid instruction (should not get here)";
848   }
849 
850   return !previous_rti.IsEqual(instr->GetReferenceTypeInfo());
851 }
852 
VisitInvoke(HInvoke * instr)853 void ReferenceTypePropagation::RTPVisitor::VisitInvoke(HInvoke* instr) {
854   if (instr->GetType() != DataType::Type::kReference) {
855     return;
856   }
857 
858   ScopedObjectAccess soa(Thread::Current());
859   // FIXME: Treat InvokePolymorphic separately, as we can get a more specific return type from
860   // protoId than the one obtained from the resolved method.
861   ArtMethod* method = instr->GetResolvedMethod();
862   ObjPtr<mirror::Class> klass = (method == nullptr) ? nullptr : method->LookupResolvedReturnType();
863   SetClassAsTypeInfo(instr, klass, /* is_exact= */ false);
864 }
865 
VisitArrayGet(HArrayGet * instr)866 void ReferenceTypePropagation::RTPVisitor::VisitArrayGet(HArrayGet* instr) {
867   if (instr->GetType() != DataType::Type::kReference) {
868     return;
869   }
870 
871   ScopedObjectAccess soa(Thread::Current());
872   UpdateArrayGet(instr);
873   if (!instr->GetReferenceTypeInfo().IsValid()) {
874     worklist_.push_back(instr);
875   }
876 }
877 
UpdateBoundType(HBoundType * instr)878 void ReferenceTypePropagation::RTPVisitor::UpdateBoundType(HBoundType* instr) {
879   ReferenceTypeInfo input_rti = instr->InputAt(0)->GetReferenceTypeInfo();
880   if (!input_rti.IsValid()) {
881     return;  // No new info yet.
882   }
883 
884   ReferenceTypeInfo upper_bound_rti = instr->GetUpperBound();
885   if (upper_bound_rti.IsExact()) {
886     instr->SetReferenceTypeInfo(upper_bound_rti);
887   } else if (upper_bound_rti.IsSupertypeOf(input_rti)) {
888     // input is more specific.
889     instr->SetReferenceTypeInfo(input_rti);
890   } else {
891     // upper_bound is more specific or unrelated.
892     // Note that the object might then be exact, and we know the code dominated by this
893     // bound type is dead. To not confuse potential other optimizations, we mark
894     // the bound as non-exact.
895     instr->SetReferenceTypeInfo(
896         ReferenceTypeInfo::Create(upper_bound_rti.GetTypeHandle(), /* is_exact= */ false));
897   }
898 }
899 
900 // NullConstant inputs are ignored during merging as they do not provide any useful information.
901 // If all the inputs are NullConstants then the type of the phi will be set to Object.
UpdatePhi(HPhi * instr)902 void ReferenceTypePropagation::RTPVisitor::UpdatePhi(HPhi* instr) {
903   DCHECK(instr->IsLive());
904 
905   HInputsRef inputs = instr->GetInputs();
906   size_t first_input_index_not_null = 0;
907   while (first_input_index_not_null < inputs.size() &&
908          inputs[first_input_index_not_null]->IsNullConstant()) {
909     first_input_index_not_null++;
910   }
911   if (first_input_index_not_null == inputs.size()) {
912     // All inputs are NullConstants, set the type to object.
913     // This may happen in the presence of inlining.
914     instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
915     return;
916   }
917 
918   ReferenceTypeInfo new_rti = instr->InputAt(first_input_index_not_null)->GetReferenceTypeInfo();
919 
920   if (new_rti.IsValid() && new_rti.IsObjectClass() && !new_rti.IsExact()) {
921     // Early return if we are Object and inexact.
922     instr->SetReferenceTypeInfo(new_rti);
923     return;
924   }
925 
926   for (size_t i = first_input_index_not_null + 1; i < inputs.size(); i++) {
927     if (inputs[i]->IsNullConstant()) {
928       continue;
929     }
930     new_rti = MergeTypes(new_rti, inputs[i]->GetReferenceTypeInfo(), GetHandleCache());
931     if (new_rti.IsValid() && new_rti.IsObjectClass()) {
932       if (!new_rti.IsExact()) {
933         break;
934       } else {
935         continue;
936       }
937     }
938   }
939 
940   if (new_rti.IsValid()) {
941     instr->SetReferenceTypeInfo(new_rti);
942   }
943 }
944 
IsUpdateable(const HInstruction * instr)945 constexpr bool ReferenceTypePropagation::RTPVisitor::IsUpdateable(const HInstruction* instr) {
946   return (instr->IsPhi() && instr->AsPhi()->IsLive()) ||
947          instr->IsBoundType() ||
948          instr->IsNullCheck() ||
949          instr->IsArrayGet();
950 }
951 
952 // Re-computes and updates the nullability of the instruction. Returns whether or
953 // not the nullability was changed.
UpdateNullability(HInstruction * instr)954 bool ReferenceTypePropagation::RTPVisitor::UpdateNullability(HInstruction* instr) {
955   DCHECK(IsUpdateable(instr));
956 
957   if (!instr->IsPhi() && !instr->IsBoundType()) {
958     return false;
959   }
960 
961   bool existing_can_be_null = instr->CanBeNull();
962   if (instr->IsPhi()) {
963     HPhi* phi = instr->AsPhi();
964     bool new_can_be_null = false;
965     for (HInstruction* input : phi->GetInputs()) {
966       if (input->CanBeNull()) {
967         new_can_be_null = true;
968         break;
969       }
970     }
971     phi->SetCanBeNull(new_can_be_null);
972   } else if (instr->IsBoundType()) {
973     HBoundType* bound_type = instr->AsBoundType();
974     bound_type->SetCanBeNull(instr->InputAt(0)->CanBeNull() && bound_type->GetUpperCanBeNull());
975   }
976   return existing_can_be_null != instr->CanBeNull();
977 }
978 
ProcessWorklist()979 void ReferenceTypePropagation::RTPVisitor::ProcessWorklist() {
980   while (!worklist_.empty()) {
981     HInstruction* instruction = worklist_.back();
982     worklist_.pop_back();
983     bool updated_nullability = UpdateNullability(instruction);
984     bool updated_reference_type = UpdateReferenceTypeInfo(instruction);
985     if (updated_nullability || updated_reference_type) {
986       AddDependentInstructionsToWorklist(instruction);
987     }
988   }
989 }
990 
AddToWorklist(HInstruction * instruction)991 void ReferenceTypePropagation::RTPVisitor::AddToWorklist(HInstruction* instruction) {
992   DCHECK_EQ(instruction->GetType(), DataType::Type::kReference)
993       << instruction->DebugName() << ":" << instruction->GetType();
994   worklist_.push_back(instruction);
995 }
996 
AddDependentInstructionsToWorklist(HInstruction * instruction)997 void ReferenceTypePropagation::RTPVisitor::AddDependentInstructionsToWorklist(
998     HInstruction* instruction) {
999   for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
1000     HInstruction* user = use.GetUser();
1001     if ((user->IsPhi() && user->AsPhi()->IsLive())
1002        || user->IsBoundType()
1003        || user->IsNullCheck()
1004        || (user->IsArrayGet() && (user->GetType() == DataType::Type::kReference))) {
1005       AddToWorklist(user);
1006     }
1007   }
1008 }
1009 
1010 }  // namespace art
1011