xref: /aosp_15_r20/art/compiler/optimizing/reference_type_propagation.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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_cache-inl.h"
28 #include "handle_scope-inl.h"
29 #include "mirror/class-inl.h"
30 #include "mirror/dex_cache.h"
31 #include "scoped_thread_state_change-inl.h"
32 
33 namespace art HIDDEN {
34 
FindDexCacheWithHint(Thread * self,const DexFile & dex_file,Handle<mirror::DexCache> hint_dex_cache)35 static inline ObjPtr<mirror::DexCache> FindDexCacheWithHint(
36     Thread* self, const DexFile& dex_file, Handle<mirror::DexCache> hint_dex_cache)
37     REQUIRES_SHARED(Locks::mutator_lock_) {
38   if (LIKELY(hint_dex_cache->GetDexFile() == &dex_file)) {
39     return hint_dex_cache.Get();
40   } else {
41     return Runtime::Current()->GetClassLinker()->FindDexCache(self, dex_file);
42   }
43 }
44 
45 class ReferenceTypePropagation::RTPVisitor final : public HGraphDelegateVisitor {
46  public:
RTPVisitor(HGraph * graph,Handle<mirror::DexCache> hint_dex_cache,bool is_first_run)47   RTPVisitor(HGraph* graph, Handle<mirror::DexCache> hint_dex_cache, bool is_first_run)
48       : HGraphDelegateVisitor(graph),
49         hint_dex_cache_(hint_dex_cache),
50         allocator_(graph->GetArenaStack()),
51         worklist_(allocator_.Adapter(kArenaAllocReferenceTypePropagation)),
52         is_first_run_(is_first_run) {
53     worklist_.reserve(kDefaultWorklistSize);
54   }
55 
56   void VisitDeoptimize(HDeoptimize* deopt) override;
57   void VisitNewInstance(HNewInstance* new_instance) override;
58   void VisitLoadClass(HLoadClass* load_class) override;
59   void VisitInstanceOf(HInstanceOf* load_class) override;
60   void VisitClinitCheck(HClinitCheck* clinit_check) override;
61   void VisitLoadMethodHandle(HLoadMethodHandle* instr) override;
62   void VisitLoadMethodType(HLoadMethodType* instr) override;
63   void VisitLoadString(HLoadString* instr) override;
64   void VisitLoadException(HLoadException* instr) override;
65   void VisitNewArray(HNewArray* instr) override;
66   void VisitParameterValue(HParameterValue* instr) override;
67   void VisitInstanceFieldGet(HInstanceFieldGet* instr) override;
68   void VisitStaticFieldGet(HStaticFieldGet* instr) override;
69   void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* instr) override;
70   void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* instr) override;
71   void VisitInvoke(HInvoke* instr) override;
72   void VisitArrayGet(HArrayGet* instr) override;
73   void VisitCheckCast(HCheckCast* instr) override;
74   void VisitBoundType(HBoundType* instr) override;
75   void VisitNullCheck(HNullCheck* instr) override;
76   void VisitPhi(HPhi* phi) override;
77 
78   void VisitBasicBlock(HBasicBlock* block) override;
79   void ProcessWorklist();
80 
81  private:
82   void UpdateFieldAccessTypeInfo(HInstruction* instr, const FieldInfo& info);
83   void SetClassAsTypeInfo(HInstruction* instr, ObjPtr<mirror::Class> klass, bool is_exact)
84       REQUIRES_SHARED(Locks::mutator_lock_);
85   void BoundTypeForIfNotNull(HBasicBlock* block);
86   static void BoundTypeForIfInstanceOf(HBasicBlock* block);
87   static bool UpdateNullability(HInstruction* instr);
88   static void UpdateBoundType(HBoundType* bound_type) REQUIRES_SHARED(Locks::mutator_lock_);
89   void UpdateArrayGet(HArrayGet* instr) REQUIRES_SHARED(Locks::mutator_lock_);
90   void UpdatePhi(HPhi* phi) REQUIRES_SHARED(Locks::mutator_lock_);
91   bool UpdateReferenceTypeInfo(HInstruction* instr);
92   void UpdateReferenceTypeInfo(HInstruction* instr,
93                                dex::TypeIndex type_idx,
94                                const DexFile& dex_file,
95                                bool is_exact);
96 
97   // Returns true if this is an instruction we might need to recursively update.
98   // The types are (live) Phi, BoundType, ArrayGet, and NullCheck
99   static constexpr bool IsUpdateable(const HInstruction* instr);
100   void AddToWorklist(HInstruction* instruction);
101   void AddDependentInstructionsToWorklist(HInstruction* instruction);
102 
GetHandleCache()103   HandleCache* GetHandleCache() {
104     return GetGraph()->GetHandleCache();
105   }
106 
107   static constexpr size_t kDefaultWorklistSize = 8;
108 
109   Handle<mirror::DexCache> hint_dex_cache_;
110 
111   // Use local allocator for allocating memory.
112   ScopedArenaAllocator allocator_;
113   ScopedArenaVector<HInstruction*> worklist_;
114   const bool is_first_run_;
115 
116   friend class ReferenceTypePropagation;
117 };
118 
ReferenceTypePropagation(HGraph * graph,Handle<mirror::DexCache> hint_dex_cache,bool is_first_run,const char * name)119 ReferenceTypePropagation::ReferenceTypePropagation(HGraph* graph,
120                                                    Handle<mirror::DexCache> hint_dex_cache,
121                                                    bool is_first_run,
122                                                    const char* name)
123     : HOptimization(graph, name), hint_dex_cache_(hint_dex_cache), is_first_run_(is_first_run) {}
124 
Visit(HInstruction * instruction)125 void ReferenceTypePropagation::Visit(HInstruction* instruction) {
126   RTPVisitor visitor(graph_, hint_dex_cache_, is_first_run_);
127   instruction->Accept(&visitor);
128 }
129 
Visit(ArrayRef<HInstruction * const> instructions)130 void ReferenceTypePropagation::Visit(ArrayRef<HInstruction* const> instructions) {
131   RTPVisitor visitor(graph_, hint_dex_cache_, is_first_run_);
132   for (HInstruction* instruction : instructions) {
133     if (instruction->IsPhi()) {
134       // Need to force phis to recalculate null-ness.
135       instruction->AsPhi()->SetCanBeNull(false);
136     }
137   }
138   for (HInstruction* instruction : instructions) {
139     instruction->Accept(&visitor);
140     // We don't know if the instruction list is ordered in the same way normal
141     // visiting would be so we need to process every instruction manually.
142     if (RTPVisitor::IsUpdateable(instruction)) {
143       visitor.AddToWorklist(instruction);
144     }
145   }
146   visitor.ProcessWorklist();
147 }
148 
149 // Check if we should create a bound type for the given object at the specified
150 // position. Because of inlining and the fact we run RTP more than once and we
151 // might have a HBoundType already. If we do, we should not create a new one.
152 // In this case we also assert that there are no other uses of the object (except
153 // 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)154 static bool ShouldCreateBoundType(HInstruction* position,
155                                   HInstruction* obj,
156                                   ReferenceTypeInfo upper_bound,
157                                   HInstruction* dominator_instr,
158                                   HBasicBlock* dominator_block)
159     REQUIRES_SHARED(Locks::mutator_lock_) {
160   // If the position where we should insert the bound type is not already a
161   // a bound type then we need to create one.
162   if (position == nullptr || !position->IsBoundType()) {
163     return true;
164   }
165 
166   HBoundType* existing_bound_type = position->AsBoundType();
167   if (existing_bound_type->GetUpperBound().IsSupertypeOf(upper_bound)) {
168     if (kIsDebugBuild) {
169       // Check that the existing HBoundType dominates all the uses.
170       for (const HUseListNode<HInstruction*>& use : obj->GetUses()) {
171         HInstruction* user = use.GetUser();
172         if (dominator_instr != nullptr) {
173           DCHECK(!dominator_instr->StrictlyDominates(user)
174               || user == existing_bound_type
175               || existing_bound_type->StrictlyDominates(user));
176         } else if (dominator_block != nullptr) {
177           DCHECK(!dominator_block->Dominates(user->GetBlock())
178               || user == existing_bound_type
179               || existing_bound_type->StrictlyDominates(user));
180         }
181       }
182     }
183   } else {
184     // TODO: if the current bound type is a refinement we could update the
185     // existing_bound_type with the a new upper limit. However, we also need to
186     // update its users and have access to the work list.
187   }
188   return false;
189 }
190 
191 // Helper method to bound the type of `receiver` for all instructions dominated
192 // by `start_block`, or `start_instruction` if `start_block` is null. The new
193 // bound type will have its upper bound be `class_rti`.
BoundTypeIn(HInstruction * receiver,HBasicBlock * start_block,HInstruction * start_instruction,const ReferenceTypeInfo & class_rti)194 static void BoundTypeIn(HInstruction* receiver,
195                         HBasicBlock* start_block,
196                         HInstruction* start_instruction,
197                         const ReferenceTypeInfo& class_rti) {
198   // We only need to bound the type if we have uses in the relevant block.
199   // So start with null and create the HBoundType lazily, only if it's needed.
200   HBoundType* bound_type = nullptr;
201   DCHECK(!receiver->IsLoadClass()) << "We should not replace HLoadClass instructions";
202   const HUseList<HInstruction*>& uses = receiver->GetUses();
203   for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
204     HInstruction* user = it->GetUser();
205     size_t index = it->GetIndex();
206     // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput().
207     ++it;
208     bool dominates = (start_instruction != nullptr)
209         ? start_instruction->StrictlyDominates(user)
210         : start_block->Dominates(user->GetBlock());
211     if (!dominates) {
212       continue;
213     }
214     if (bound_type == nullptr) {
215       ScopedObjectAccess soa(Thread::Current());
216       HInstruction* insert_point = (start_instruction != nullptr)
217           ? start_instruction->GetNext()
218           : start_block->GetFirstInstruction();
219       if (ShouldCreateBoundType(
220             insert_point, receiver, class_rti, start_instruction, start_block)) {
221         bound_type = new (receiver->GetBlock()->GetGraph()->GetAllocator()) HBoundType(receiver);
222         bound_type->SetUpperBound(class_rti, /* can_be_null= */ false);
223         start_block->InsertInstructionBefore(bound_type, insert_point);
224         // To comply with the RTP algorithm, don't type the bound type just yet, it will
225         // be handled in RTPVisitor::VisitBoundType.
226       } else {
227         // We already have a bound type on the position we would need to insert
228         // the new one. The existing bound type should dominate all the users
229         // (dchecked) so there's no need to continue.
230         break;
231       }
232     }
233     user->ReplaceInput(bound_type, index);
234   }
235   // If the receiver is a null check, also bound the type of the actual
236   // receiver.
237   if (receiver->IsNullCheck()) {
238     BoundTypeIn(receiver->InputAt(0), start_block, start_instruction, class_rti);
239   }
240 }
241 
242 // Recognize the patterns:
243 // if (obj.shadow$_klass_ == Foo.class) ...
244 // deoptimize if (obj.shadow$_klass_ == Foo.class)
BoundTypeForClassCheck(HInstruction * check)245 static void BoundTypeForClassCheck(HInstruction* check) {
246   if (!check->IsIf() && !check->IsDeoptimize()) {
247     return;
248   }
249   HInstruction* compare = check->InputAt(0);
250   if (!compare->IsEqual() && !compare->IsNotEqual()) {
251     return;
252   }
253   HInstruction* input_one = compare->InputAt(0);
254   HInstruction* input_two = compare->InputAt(1);
255   HLoadClass* load_class = input_one->IsLoadClass()
256       ? input_one->AsLoadClass()
257       : input_two->AsLoadClassOrNull();
258   if (load_class == nullptr) {
259     return;
260   }
261 
262   ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
263   if (!class_rti.IsValid()) {
264     // We have loaded an unresolved class. Don't bother bounding the type.
265     return;
266   }
267 
268   HInstruction* field_get = (load_class == input_one) ? input_two : input_one;
269   if (!field_get->IsInstanceFieldGet()) {
270     return;
271   }
272   HInstruction* receiver = field_get->InputAt(0);
273   ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo();
274   if (receiver_type.IsExact()) {
275     // If we already know the receiver type, don't bother updating its users.
276     return;
277   }
278 
279   {
280     ScopedObjectAccess soa(Thread::Current());
281     ArtField* field = GetClassRoot<mirror::Object>()->GetInstanceField(0);
282     DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
283     if (field_get->GetFieldInfo().GetField() != field) {
284       return;
285     }
286   }
287 
288   if (check->IsIf()) {
289     HBasicBlock* trueBlock = compare->IsEqual()
290         ? check->AsIf()->IfTrueSuccessor()
291         : check->AsIf()->IfFalseSuccessor();
292     BoundTypeIn(receiver, trueBlock, /* start_instruction= */ nullptr, class_rti);
293   } else {
294     DCHECK(check->IsDeoptimize());
295     if (compare->IsEqual() && check->AsDeoptimize()->GuardsAnInput()) {
296       check->SetReferenceTypeInfo(class_rti);
297     }
298   }
299 }
300 
Run()301 bool ReferenceTypePropagation::Run() {
302   DCHECK(Thread::Current() != nullptr)
303       << "ReferenceTypePropagation requires the use of Thread::Current(). Make sure you have a "
304       << "Runtime initialized before calling this optimization pass";
305   RTPVisitor visitor(graph_, hint_dex_cache_, is_first_run_);
306 
307   // To properly propagate type info we need to visit in the dominator-based order.
308   // Reverse post order guarantees a node's dominators are visited first.
309   // We take advantage of this order in `VisitBasicBlock`.
310   for (HBasicBlock* block : graph_->GetReversePostOrder()) {
311     visitor.VisitBasicBlock(block);
312   }
313 
314   visitor.ProcessWorklist();
315   return true;
316 }
317 
VisitBasicBlock(HBasicBlock * block)318 void ReferenceTypePropagation::RTPVisitor::VisitBasicBlock(HBasicBlock* block) {
319   // Handle Phis first as there might be instructions in the same block who depend on them.
320   VisitPhis(block);
321 
322   // Handle instructions. Since RTP may add HBoundType instructions just after the
323   // last visited instruction, use `HInstructionIteratorHandleChanges` iterator.
324   VisitNonPhiInstructions(block);
325 
326   // Add extra nodes to bound types.
327   BoundTypeForIfNotNull(block);
328   BoundTypeForIfInstanceOf(block);
329   BoundTypeForClassCheck(block->GetLastInstruction());
330 }
331 
BoundTypeForIfNotNull(HBasicBlock * block)332 void ReferenceTypePropagation::RTPVisitor::BoundTypeForIfNotNull(HBasicBlock* block) {
333   HIf* ifInstruction = block->GetLastInstruction()->AsIfOrNull();
334   if (ifInstruction == nullptr) {
335     return;
336   }
337   HInstruction* ifInput = ifInstruction->InputAt(0);
338   if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) {
339     return;
340   }
341   HInstruction* input0 = ifInput->InputAt(0);
342   HInstruction* input1 = ifInput->InputAt(1);
343   HInstruction* obj = nullptr;
344 
345   if (input1->IsNullConstant()) {
346     obj = input0;
347   } else if (input0->IsNullConstant()) {
348     obj = input1;
349   } else {
350     return;
351   }
352 
353   if (!obj->CanBeNull() || obj->IsNullConstant()) {
354     // Null check is dead code and will be removed by DCE.
355     return;
356   }
357   DCHECK(!obj->IsLoadClass()) << "We should not replace HLoadClass instructions";
358 
359   // We only need to bound the type if we have uses in the relevant block.
360   // So start with null and create the HBoundType lazily, only if it's needed.
361   HBasicBlock* notNullBlock = ifInput->IsNotEqual()
362       ? ifInstruction->IfTrueSuccessor()
363       : ifInstruction->IfFalseSuccessor();
364 
365   ReferenceTypeInfo object_rti =
366       ReferenceTypeInfo::Create(GetHandleCache()->GetObjectClassHandle(), /* is_exact= */ false);
367 
368   BoundTypeIn(obj, notNullBlock, /* start_instruction= */ nullptr, object_rti);
369 }
370 
371 // Returns true if one of the patterns below has been recognized. If so, the
372 // InstanceOf instruction together with the true branch of `ifInstruction` will
373 // be returned using the out parameters.
374 // Recognized patterns:
375 //   (1) patterns equivalent to `if (obj instanceof X)`
376 //     (a) InstanceOf -> Equal to 1 -> If
377 //     (b) InstanceOf -> NotEqual to 0 -> If
378 //     (c) InstanceOf -> If
379 //   (2) patterns equivalent to `if (!(obj instanceof X))`
380 //     (a) InstanceOf -> Equal to 0 -> If
381 //     (b) InstanceOf -> NotEqual to 1 -> If
382 //     (c) InstanceOf -> BooleanNot -> If
MatchIfInstanceOf(HIf * ifInstruction,HInstanceOf ** instanceOf,HBasicBlock ** trueBranch)383 static bool MatchIfInstanceOf(HIf* ifInstruction,
384                               /* out */ HInstanceOf** instanceOf,
385                               /* out */ HBasicBlock** trueBranch) {
386   HInstruction* input = ifInstruction->InputAt(0);
387 
388   if (input->IsEqual()) {
389     HInstruction* rhs = input->AsEqual()->GetConstantRight();
390     if (rhs != nullptr) {
391       HInstruction* lhs = input->AsEqual()->GetLeastConstantLeft();
392       if (lhs->IsInstanceOf() && rhs->IsIntConstant()) {
393         if (rhs->AsIntConstant()->IsTrue()) {
394           // Case (1a)
395           *trueBranch = ifInstruction->IfTrueSuccessor();
396         } else if (rhs->AsIntConstant()->IsFalse()) {
397           // Case (2a)
398           *trueBranch = ifInstruction->IfFalseSuccessor();
399         } else {
400           // Sometimes we see a comparison of instance-of with a constant which is neither 0 nor 1.
401           // In those cases, we cannot do the match if+instance-of.
402           return false;
403         }
404         *instanceOf = lhs->AsInstanceOf();
405         return true;
406       }
407     }
408   } else if (input->IsNotEqual()) {
409     HInstruction* rhs = input->AsNotEqual()->GetConstantRight();
410     if (rhs != nullptr) {
411       HInstruction* lhs = input->AsNotEqual()->GetLeastConstantLeft();
412       if (lhs->IsInstanceOf() && rhs->IsIntConstant()) {
413         if (rhs->AsIntConstant()->IsFalse()) {
414           // Case (1b)
415           *trueBranch = ifInstruction->IfTrueSuccessor();
416         } else if (rhs->AsIntConstant()->IsTrue()) {
417           // Case (2b)
418           *trueBranch = ifInstruction->IfFalseSuccessor();
419         } else {
420           // Sometimes we see a comparison of instance-of with a constant which is neither 0 nor 1.
421           // In those cases, we cannot do the match if+instance-of.
422           return false;
423         }
424         *instanceOf = lhs->AsInstanceOf();
425         return true;
426       }
427     }
428   } else if (input->IsInstanceOf()) {
429     // Case (1c)
430     *instanceOf = input->AsInstanceOf();
431     *trueBranch = ifInstruction->IfTrueSuccessor();
432     return true;
433   } else if (input->IsBooleanNot()) {
434     HInstruction* not_input = input->InputAt(0);
435     if (not_input->IsInstanceOf()) {
436       // Case (2c)
437       *instanceOf = not_input->AsInstanceOf();
438       *trueBranch = ifInstruction->IfFalseSuccessor();
439       return true;
440     }
441   }
442 
443   return false;
444 }
445 
446 // Detects if `block` is the True block for the pattern
447 // `if (x instanceof ClassX) { }`
448 // If that's the case insert an HBoundType instruction to bound the type of `x`
449 // to `ClassX` in the scope of the dominated blocks.
BoundTypeForIfInstanceOf(HBasicBlock * block)450 void ReferenceTypePropagation::RTPVisitor::BoundTypeForIfInstanceOf(HBasicBlock* block) {
451   HIf* ifInstruction = block->GetLastInstruction()->AsIfOrNull();
452   if (ifInstruction == nullptr) {
453     return;
454   }
455 
456   // Try to recognize common `if (instanceof)` and `if (!instanceof)` patterns.
457   HInstanceOf* instanceOf = nullptr;
458   HBasicBlock* instanceOfTrueBlock = nullptr;
459   if (!MatchIfInstanceOf(ifInstruction, &instanceOf, &instanceOfTrueBlock)) {
460     return;
461   }
462 
463   ReferenceTypeInfo class_rti = instanceOf->GetTargetClassRTI();
464   if (!class_rti.IsValid()) {
465     // We have loaded an unresolved class. Don't bother bounding the type.
466     return;
467   }
468 
469   HInstruction* obj = instanceOf->InputAt(0);
470   if (obj->GetReferenceTypeInfo().IsExact() && !obj->IsPhi()) {
471     // This method is being called while doing a fixed-point calculation
472     // over phis. Non-phis instruction whose type is already known do
473     // not need to be bound to another type.
474     // Not that this also prevents replacing `HLoadClass` with a `HBoundType`.
475     // `HCheckCast` and `HInstanceOf` expect a `HLoadClass` as a second
476     // input.
477     return;
478   }
479 
480   {
481     ScopedObjectAccess soa(Thread::Current());
482     if (!class_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes()) {
483       class_rti = ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact= */ false);
484     }
485   }
486   BoundTypeIn(obj, instanceOfTrueBlock, /* start_instruction= */ nullptr, class_rti);
487 }
488 
SetClassAsTypeInfo(HInstruction * instr,ObjPtr<mirror::Class> klass,bool is_exact)489 void ReferenceTypePropagation::RTPVisitor::SetClassAsTypeInfo(HInstruction* instr,
490                                                               ObjPtr<mirror::Class> klass,
491                                                               bool is_exact) {
492   if (instr->IsInvokeStaticOrDirect() && instr->AsInvokeStaticOrDirect()->IsStringInit()) {
493     // Calls to String.<init> are replaced with a StringFactory.
494     if (kIsDebugBuild) {
495       HInvokeStaticOrDirect* invoke = instr->AsInvokeStaticOrDirect();
496       ClassLinker* cl = Runtime::Current()->GetClassLinker();
497       Thread* self = Thread::Current();
498       StackHandleScope<2> hs(self);
499       const DexFile& dex_file = *invoke->GetResolvedMethodReference().dex_file;
500       uint32_t dex_method_index = invoke->GetResolvedMethodReference().index;
501       Handle<mirror::DexCache> dex_cache(
502           hs.NewHandle(FindDexCacheWithHint(self, dex_file, hint_dex_cache_)));
503       // Use a null loader, the target method is in a boot classpath dex file.
504       Handle<mirror::ClassLoader> loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
505       ArtMethod* method = cl->ResolveMethodId(dex_method_index, dex_cache, loader);
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 
FixUpSelectType(HSelect * select,HandleCache * handle_cache)752 void ReferenceTypePropagation::FixUpSelectType(HSelect* select, HandleCache* handle_cache) {
753   ReferenceTypeInfo false_rti = select->GetFalseValue()->GetReferenceTypeInfo();
754   ReferenceTypeInfo true_rti = select->GetTrueValue()->GetReferenceTypeInfo();
755   ReferenceTypeInfo rti = ReferenceTypeInfo::CreateInvalid();
756   ScopedObjectAccess soa(Thread::Current());
757   select->SetReferenceTypeInfo(MergeTypes(false_rti, true_rti, handle_cache));
758 }
759 
MergeTypes(const ReferenceTypeInfo & a,const ReferenceTypeInfo & b,HandleCache * handle_cache)760 ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a,
761                                                        const ReferenceTypeInfo& b,
762                                                        HandleCache* handle_cache) {
763   if (!b.IsValid()) {
764     return a;
765   }
766   if (!a.IsValid()) {
767     return b;
768   }
769 
770   bool is_exact = a.IsExact() && b.IsExact();
771   ReferenceTypeInfo::TypeHandle result_type_handle;
772   ReferenceTypeInfo::TypeHandle a_type_handle = a.GetTypeHandle();
773   ReferenceTypeInfo::TypeHandle b_type_handle = b.GetTypeHandle();
774   bool a_is_interface = a_type_handle->IsInterface();
775   bool b_is_interface = b_type_handle->IsInterface();
776 
777   if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) {
778     result_type_handle = a_type_handle;
779   } else if (a.IsSupertypeOf(b)) {
780     result_type_handle = a_type_handle;
781     is_exact = false;
782   } else if (b.IsSupertypeOf(a)) {
783     result_type_handle = b_type_handle;
784     is_exact = false;
785   } else if (!a_is_interface && !b_is_interface) {
786     result_type_handle =
787         handle_cache->NewHandle(a_type_handle->GetCommonSuperClass(b_type_handle));
788     is_exact = false;
789   } else {
790     // This can happen if:
791     //    - both types are interfaces. TODO(calin): implement
792     //    - one is an interface, the other a class, and the type does not implement the interface
793     //      e.g:
794     //        void foo(Interface i, boolean cond) {
795     //          Object o = cond ? i : new Object();
796     //        }
797     result_type_handle = handle_cache->GetObjectClassHandle();
798     is_exact = false;
799   }
800 
801   return ReferenceTypeInfo::Create(result_type_handle, is_exact);
802 }
803 
UpdateArrayGet(HArrayGet * instr)804 void ReferenceTypePropagation::RTPVisitor::UpdateArrayGet(HArrayGet* instr) {
805   DCHECK_EQ(DataType::Type::kReference, instr->GetType());
806 
807   ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
808   if (!parent_rti.IsValid()) {
809     return;
810   }
811 
812   Handle<mirror::Class> handle = parent_rti.GetTypeHandle();
813   if (handle->IsObjectArrayClass() && IsAdmissible(handle->GetComponentType())) {
814     ReferenceTypeInfo::TypeHandle component_handle =
815         GetHandleCache()->NewHandle(handle->GetComponentType());
816     bool is_exact = component_handle->CannotBeAssignedFromOtherTypes();
817     instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(component_handle, is_exact));
818   } else {
819     // We don't know what the parent actually is, so we fallback to object.
820     instr->SetReferenceTypeInfo(GetGraph()->GetInexactObjectRti());
821   }
822 }
823 
UpdateReferenceTypeInfo(HInstruction * instr)824 bool ReferenceTypePropagation::RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr) {
825   ScopedObjectAccess soa(Thread::Current());
826 
827   ReferenceTypeInfo previous_rti = instr->GetReferenceTypeInfo();
828   if (instr->IsBoundType()) {
829     UpdateBoundType(instr->AsBoundType());
830   } else if (instr->IsPhi()) {
831     UpdatePhi(instr->AsPhi());
832   } else if (instr->IsNullCheck()) {
833     ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
834     if (parent_rti.IsValid()) {
835       instr->SetReferenceTypeInfo(parent_rti);
836     }
837   } else if (instr->IsArrayGet()) {
838     // TODO: consider if it's worth "looking back" and binding the input object
839     // to an array type.
840     UpdateArrayGet(instr->AsArrayGet());
841   } else {
842     LOG(FATAL) << "Invalid instruction (should not get here)";
843   }
844 
845   return !previous_rti.IsEqual(instr->GetReferenceTypeInfo());
846 }
847 
VisitInvoke(HInvoke * instr)848 void ReferenceTypePropagation::RTPVisitor::VisitInvoke(HInvoke* instr) {
849   if (instr->GetType() != DataType::Type::kReference) {
850     return;
851   }
852 
853   ScopedObjectAccess soa(Thread::Current());
854   // FIXME: Treat InvokePolymorphic separately, as we can get a more specific return type from
855   // protoId than the one obtained from the resolved method.
856   ArtMethod* method = instr->GetResolvedMethod();
857   ObjPtr<mirror::Class> klass = (method == nullptr) ? nullptr : method->LookupResolvedReturnType();
858   SetClassAsTypeInfo(instr, klass, /* is_exact= */ false);
859 }
860 
VisitArrayGet(HArrayGet * instr)861 void ReferenceTypePropagation::RTPVisitor::VisitArrayGet(HArrayGet* instr) {
862   if (instr->GetType() != DataType::Type::kReference) {
863     return;
864   }
865 
866   ScopedObjectAccess soa(Thread::Current());
867   UpdateArrayGet(instr);
868   if (!instr->GetReferenceTypeInfo().IsValid()) {
869     worklist_.push_back(instr);
870   }
871 }
872 
UpdateBoundType(HBoundType * instr)873 void ReferenceTypePropagation::RTPVisitor::UpdateBoundType(HBoundType* instr) {
874   ReferenceTypeInfo input_rti = instr->InputAt(0)->GetReferenceTypeInfo();
875   if (!input_rti.IsValid()) {
876     return;  // No new info yet.
877   }
878 
879   ReferenceTypeInfo upper_bound_rti = instr->GetUpperBound();
880   if (upper_bound_rti.IsExact()) {
881     instr->SetReferenceTypeInfo(upper_bound_rti);
882   } else if (upper_bound_rti.IsSupertypeOf(input_rti)) {
883     // input is more specific.
884     instr->SetReferenceTypeInfo(input_rti);
885   } else {
886     // upper_bound is more specific or unrelated.
887     // Note that the object might then be exact, and we know the code dominated by this
888     // bound type is dead. To not confuse potential other optimizations, we mark
889     // the bound as non-exact.
890     instr->SetReferenceTypeInfo(
891         ReferenceTypeInfo::Create(upper_bound_rti.GetTypeHandle(), /* is_exact= */ false));
892   }
893 }
894 
895 // NullConstant inputs are ignored during merging as they do not provide any useful information.
896 // If all the inputs are NullConstants then the type of the phi will be set to Object.
UpdatePhi(HPhi * instr)897 void ReferenceTypePropagation::RTPVisitor::UpdatePhi(HPhi* instr) {
898   DCHECK(instr->IsLive());
899 
900   HInputsRef inputs = instr->GetInputs();
901   size_t first_input_index_not_null = 0;
902   while (first_input_index_not_null < inputs.size() &&
903          inputs[first_input_index_not_null]->IsNullConstant()) {
904     first_input_index_not_null++;
905   }
906   if (first_input_index_not_null == inputs.size()) {
907     // All inputs are NullConstants, set the type to object.
908     // This may happen in the presence of inlining.
909     instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
910     return;
911   }
912 
913   ReferenceTypeInfo new_rti = instr->InputAt(first_input_index_not_null)->GetReferenceTypeInfo();
914 
915   if (new_rti.IsValid() && new_rti.IsObjectClass() && !new_rti.IsExact()) {
916     // Early return if we are Object and inexact.
917     instr->SetReferenceTypeInfo(new_rti);
918     return;
919   }
920 
921   for (size_t i = first_input_index_not_null + 1; i < inputs.size(); i++) {
922     if (inputs[i]->IsNullConstant()) {
923       continue;
924     }
925     new_rti = MergeTypes(new_rti, inputs[i]->GetReferenceTypeInfo(), GetHandleCache());
926     if (new_rti.IsValid() && new_rti.IsObjectClass()) {
927       if (!new_rti.IsExact()) {
928         break;
929       } else {
930         continue;
931       }
932     }
933   }
934 
935   if (new_rti.IsValid()) {
936     instr->SetReferenceTypeInfo(new_rti);
937   }
938 }
939 
IsUpdateable(const HInstruction * instr)940 constexpr bool ReferenceTypePropagation::RTPVisitor::IsUpdateable(const HInstruction* instr) {
941   return (instr->IsPhi() && instr->AsPhi()->IsLive()) ||
942          instr->IsBoundType() ||
943          instr->IsNullCheck() ||
944          instr->IsArrayGet();
945 }
946 
947 // Re-computes and updates the nullability of the instruction. Returns whether or
948 // not the nullability was changed.
UpdateNullability(HInstruction * instr)949 bool ReferenceTypePropagation::RTPVisitor::UpdateNullability(HInstruction* instr) {
950   DCHECK(IsUpdateable(instr));
951 
952   if (!instr->IsPhi() && !instr->IsBoundType()) {
953     return false;
954   }
955 
956   bool existing_can_be_null = instr->CanBeNull();
957   if (instr->IsPhi()) {
958     HPhi* phi = instr->AsPhi();
959     bool new_can_be_null = false;
960     for (HInstruction* input : phi->GetInputs()) {
961       if (input->CanBeNull()) {
962         new_can_be_null = true;
963         break;
964       }
965     }
966     phi->SetCanBeNull(new_can_be_null);
967   } else if (instr->IsBoundType()) {
968     HBoundType* bound_type = instr->AsBoundType();
969     bound_type->SetCanBeNull(instr->InputAt(0)->CanBeNull() && bound_type->GetUpperCanBeNull());
970   }
971   return existing_can_be_null != instr->CanBeNull();
972 }
973 
ProcessWorklist()974 void ReferenceTypePropagation::RTPVisitor::ProcessWorklist() {
975   while (!worklist_.empty()) {
976     HInstruction* instruction = worklist_.back();
977     worklist_.pop_back();
978     bool updated_nullability = UpdateNullability(instruction);
979     bool updated_reference_type = UpdateReferenceTypeInfo(instruction);
980     if (updated_nullability || updated_reference_type) {
981       AddDependentInstructionsToWorklist(instruction);
982     }
983   }
984 }
985 
AddToWorklist(HInstruction * instruction)986 void ReferenceTypePropagation::RTPVisitor::AddToWorklist(HInstruction* instruction) {
987   DCHECK_EQ(instruction->GetType(), DataType::Type::kReference)
988       << instruction->DebugName() << ":" << instruction->GetType();
989   worklist_.push_back(instruction);
990 }
991 
AddDependentInstructionsToWorklist(HInstruction * instruction)992 void ReferenceTypePropagation::RTPVisitor::AddDependentInstructionsToWorklist(
993     HInstruction* instruction) {
994   for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
995     HInstruction* user = use.GetUser();
996     if ((user->IsPhi() && user->AsPhi()->IsLive())
997        || user->IsBoundType()
998        || user->IsNullCheck()
999        || (user->IsArrayGet() && (user->GetType() == DataType::Type::kReference))) {
1000       AddToWorklist(user);
1001     }
1002   }
1003 }
1004 
1005 }  // namespace art
1006