• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/enums.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 VisitPredicatedInstanceFieldGet(HPredicatedInstanceFieldGet* 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->AsLoadClass();
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() && !field_get->IsPredicatedInstanceFieldGet()) {
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   for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
321     VisitPhi(it.Current()->AsPhi());
322   }
323 
324   // Handle instructions. Since RTP may add HBoundType instructions just after the
325   // last visited instruction, use `HInstructionIteratorHandleChanges` iterator.
326   for (HInstructionIteratorHandleChanges it(block->GetInstructions()); !it.Done(); it.Advance()) {
327     HInstruction* instr = it.Current();
328     instr->Accept(this);
329   }
330 
331   // Add extra nodes to bound types.
332   BoundTypeForIfNotNull(block);
333   BoundTypeForIfInstanceOf(block);
334   BoundTypeForClassCheck(block->GetLastInstruction());
335 }
336 
BoundTypeForIfNotNull(HBasicBlock * block)337 void ReferenceTypePropagation::RTPVisitor::BoundTypeForIfNotNull(HBasicBlock* block) {
338   HIf* ifInstruction = block->GetLastInstruction()->AsIf();
339   if (ifInstruction == nullptr) {
340     return;
341   }
342   HInstruction* ifInput = ifInstruction->InputAt(0);
343   if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) {
344     return;
345   }
346   HInstruction* input0 = ifInput->InputAt(0);
347   HInstruction* input1 = ifInput->InputAt(1);
348   HInstruction* obj = nullptr;
349 
350   if (input1->IsNullConstant()) {
351     obj = input0;
352   } else if (input0->IsNullConstant()) {
353     obj = input1;
354   } else {
355     return;
356   }
357 
358   if (!obj->CanBeNull() || obj->IsNullConstant()) {
359     // Null check is dead code and will be removed by DCE.
360     return;
361   }
362   DCHECK(!obj->IsLoadClass()) << "We should not replace HLoadClass instructions";
363 
364   // We only need to bound the type if we have uses in the relevant block.
365   // So start with null and create the HBoundType lazily, only if it's needed.
366   HBasicBlock* notNullBlock = ifInput->IsNotEqual()
367       ? ifInstruction->IfTrueSuccessor()
368       : ifInstruction->IfFalseSuccessor();
369 
370   ReferenceTypeInfo object_rti =
371       ReferenceTypeInfo::Create(GetHandleCache()->GetObjectClassHandle(), /* is_exact= */ false);
372 
373   BoundTypeIn(obj, notNullBlock, /* start_instruction= */ nullptr, object_rti);
374 }
375 
376 // Returns true if one of the patterns below has been recognized. If so, the
377 // InstanceOf instruction together with the true branch of `ifInstruction` will
378 // be returned using the out parameters.
379 // Recognized patterns:
380 //   (1) patterns equivalent to `if (obj instanceof X)`
381 //     (a) InstanceOf -> Equal to 1 -> If
382 //     (b) InstanceOf -> NotEqual to 0 -> If
383 //     (c) InstanceOf -> If
384 //   (2) patterns equivalent to `if (!(obj instanceof X))`
385 //     (a) InstanceOf -> Equal to 0 -> If
386 //     (b) InstanceOf -> NotEqual to 1 -> If
387 //     (c) InstanceOf -> BooleanNot -> If
MatchIfInstanceOf(HIf * ifInstruction,HInstanceOf ** instanceOf,HBasicBlock ** trueBranch)388 static bool MatchIfInstanceOf(HIf* ifInstruction,
389                               /* out */ HInstanceOf** instanceOf,
390                               /* out */ HBasicBlock** trueBranch) {
391   HInstruction* input = ifInstruction->InputAt(0);
392 
393   if (input->IsEqual()) {
394     HInstruction* rhs = input->AsEqual()->GetConstantRight();
395     if (rhs != nullptr) {
396       HInstruction* lhs = input->AsEqual()->GetLeastConstantLeft();
397       if (lhs->IsInstanceOf() && rhs->IsIntConstant()) {
398         if (rhs->AsIntConstant()->IsTrue()) {
399           // Case (1a)
400           *trueBranch = ifInstruction->IfTrueSuccessor();
401         } else if (rhs->AsIntConstant()->IsFalse()) {
402           // Case (2a)
403           *trueBranch = ifInstruction->IfFalseSuccessor();
404         } else {
405           // Sometimes we see a comparison of instance-of with a constant which is neither 0 nor 1.
406           // In those cases, we cannot do the match if+instance-of.
407           return false;
408         }
409         *instanceOf = lhs->AsInstanceOf();
410         return true;
411       }
412     }
413   } else if (input->IsNotEqual()) {
414     HInstruction* rhs = input->AsNotEqual()->GetConstantRight();
415     if (rhs != nullptr) {
416       HInstruction* lhs = input->AsNotEqual()->GetLeastConstantLeft();
417       if (lhs->IsInstanceOf() && rhs->IsIntConstant()) {
418         if (rhs->AsIntConstant()->IsFalse()) {
419           // Case (1b)
420           *trueBranch = ifInstruction->IfTrueSuccessor();
421         } else if (rhs->AsIntConstant()->IsTrue()) {
422           // Case (2b)
423           *trueBranch = ifInstruction->IfFalseSuccessor();
424         } else {
425           // Sometimes we see a comparison of instance-of with a constant which is neither 0 nor 1.
426           // In those cases, we cannot do the match if+instance-of.
427           return false;
428         }
429         *instanceOf = lhs->AsInstanceOf();
430         return true;
431       }
432     }
433   } else if (input->IsInstanceOf()) {
434     // Case (1c)
435     *instanceOf = input->AsInstanceOf();
436     *trueBranch = ifInstruction->IfTrueSuccessor();
437     return true;
438   } else if (input->IsBooleanNot()) {
439     HInstruction* not_input = input->InputAt(0);
440     if (not_input->IsInstanceOf()) {
441       // Case (2c)
442       *instanceOf = not_input->AsInstanceOf();
443       *trueBranch = ifInstruction->IfFalseSuccessor();
444       return true;
445     }
446   }
447 
448   return false;
449 }
450 
451 // Detects if `block` is the True block for the pattern
452 // `if (x instanceof ClassX) { }`
453 // If that's the case insert an HBoundType instruction to bound the type of `x`
454 // to `ClassX` in the scope of the dominated blocks.
BoundTypeForIfInstanceOf(HBasicBlock * block)455 void ReferenceTypePropagation::RTPVisitor::BoundTypeForIfInstanceOf(HBasicBlock* block) {
456   HIf* ifInstruction = block->GetLastInstruction()->AsIf();
457   if (ifInstruction == nullptr) {
458     return;
459   }
460 
461   // Try to recognize common `if (instanceof)` and `if (!instanceof)` patterns.
462   HInstanceOf* instanceOf = nullptr;
463   HBasicBlock* instanceOfTrueBlock = nullptr;
464   if (!MatchIfInstanceOf(ifInstruction, &instanceOf, &instanceOfTrueBlock)) {
465     return;
466   }
467 
468   ReferenceTypeInfo class_rti = instanceOf->GetTargetClassRTI();
469   if (!class_rti.IsValid()) {
470     // We have loaded an unresolved class. Don't bother bounding the type.
471     return;
472   }
473 
474   HInstruction* obj = instanceOf->InputAt(0);
475   if (obj->GetReferenceTypeInfo().IsExact() && !obj->IsPhi()) {
476     // This method is being called while doing a fixed-point calculation
477     // over phis. Non-phis instruction whose type is already known do
478     // not need to be bound to another type.
479     // Not that this also prevents replacing `HLoadClass` with a `HBoundType`.
480     // `HCheckCast` and `HInstanceOf` expect a `HLoadClass` as a second
481     // input.
482     return;
483   }
484 
485   {
486     ScopedObjectAccess soa(Thread::Current());
487     if (!class_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes()) {
488       class_rti = ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact= */ false);
489     }
490   }
491   BoundTypeIn(obj, instanceOfTrueBlock, /* start_instruction= */ nullptr, class_rti);
492 }
493 
SetClassAsTypeInfo(HInstruction * instr,ObjPtr<mirror::Class> klass,bool is_exact)494 void ReferenceTypePropagation::RTPVisitor::SetClassAsTypeInfo(HInstruction* instr,
495                                                               ObjPtr<mirror::Class> klass,
496                                                               bool is_exact) {
497   if (instr->IsInvokeStaticOrDirect() && instr->AsInvokeStaticOrDirect()->IsStringInit()) {
498     // Calls to String.<init> are replaced with a StringFactory.
499     if (kIsDebugBuild) {
500       HInvokeStaticOrDirect* invoke = instr->AsInvokeStaticOrDirect();
501       ClassLinker* cl = Runtime::Current()->GetClassLinker();
502       Thread* self = Thread::Current();
503       StackHandleScope<2> hs(self);
504       const DexFile& dex_file = *invoke->GetResolvedMethodReference().dex_file;
505       uint32_t dex_method_index = invoke->GetResolvedMethodReference().index;
506       Handle<mirror::DexCache> dex_cache(
507           hs.NewHandle(FindDexCacheWithHint(self, dex_file, hint_dex_cache_)));
508       // Use a null loader, the target method is in a boot classpath dex file.
509       Handle<mirror::ClassLoader> loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
510       ArtMethod* method = cl->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
511           dex_method_index, dex_cache, loader, /* referrer= */ nullptr, kDirect);
512       DCHECK(method != nullptr);
513       ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
514       DCHECK(declaring_class != nullptr);
515       DCHECK(declaring_class->IsStringClass())
516           << "Expected String class: " << declaring_class->PrettyDescriptor();
517       DCHECK(method->IsConstructor())
518           << "Expected String.<init>: " << method->PrettyMethod();
519     }
520     instr->SetReferenceTypeInfo(
521         ReferenceTypeInfo::Create(GetHandleCache()->GetStringClassHandle(), /* is_exact= */ true));
522   } else if (IsAdmissible(klass)) {
523     ReferenceTypeInfo::TypeHandle handle = GetHandleCache()->NewHandle(klass);
524     is_exact = is_exact || handle->CannotBeAssignedFromOtherTypes();
525     instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(handle, is_exact));
526   } else {
527     instr->SetReferenceTypeInfo(GetGraph()->GetInexactObjectRti());
528   }
529 }
530 
VisitDeoptimize(HDeoptimize * instr)531 void ReferenceTypePropagation::RTPVisitor::VisitDeoptimize(HDeoptimize* instr) {
532   BoundTypeForClassCheck(instr);
533 }
534 
UpdateReferenceTypeInfo(HInstruction * instr,dex::TypeIndex type_idx,const DexFile & dex_file,bool is_exact)535 void ReferenceTypePropagation::RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr,
536                                                                    dex::TypeIndex type_idx,
537                                                                    const DexFile& dex_file,
538                                                                    bool is_exact) {
539   DCHECK_EQ(instr->GetType(), DataType::Type::kReference);
540 
541   ScopedObjectAccess soa(Thread::Current());
542   ObjPtr<mirror::DexCache> dex_cache = FindDexCacheWithHint(soa.Self(), dex_file, hint_dex_cache_);
543   ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
544       type_idx, dex_cache, dex_cache->GetClassLoader());
545   SetClassAsTypeInfo(instr, klass, is_exact);
546 }
547 
VisitNewInstance(HNewInstance * instr)548 void ReferenceTypePropagation::RTPVisitor::VisitNewInstance(HNewInstance* instr) {
549   ScopedObjectAccess soa(Thread::Current());
550   SetClassAsTypeInfo(instr, instr->GetLoadClass()->GetClass().Get(), /* is_exact= */ true);
551 }
552 
VisitNewArray(HNewArray * instr)553 void ReferenceTypePropagation::RTPVisitor::VisitNewArray(HNewArray* instr) {
554   ScopedObjectAccess soa(Thread::Current());
555   SetClassAsTypeInfo(instr, instr->GetLoadClass()->GetClass().Get(), /* is_exact= */ true);
556 }
557 
VisitParameterValue(HParameterValue * instr)558 void ReferenceTypePropagation::RTPVisitor::VisitParameterValue(HParameterValue* instr) {
559   // We check if the existing type is valid: the inliner may have set it.
560   if (instr->GetType() == DataType::Type::kReference && !instr->GetReferenceTypeInfo().IsValid()) {
561     UpdateReferenceTypeInfo(instr,
562                             instr->GetTypeIndex(),
563                             instr->GetDexFile(),
564                             /* is_exact= */ false);
565   }
566 }
567 
UpdateFieldAccessTypeInfo(HInstruction * instr,const FieldInfo & info)568 void ReferenceTypePropagation::RTPVisitor::UpdateFieldAccessTypeInfo(HInstruction* instr,
569                                                                      const FieldInfo& info) {
570   if (instr->GetType() != DataType::Type::kReference) {
571     return;
572   }
573 
574   ScopedObjectAccess soa(Thread::Current());
575   ObjPtr<mirror::Class> klass;
576 
577   // The field is unknown only during tests.
578   if (info.GetField() != nullptr) {
579     klass = info.GetField()->LookupResolvedType();
580   }
581 
582   SetClassAsTypeInfo(instr, klass, /* is_exact= */ false);
583 }
584 
VisitPredicatedInstanceFieldGet(HPredicatedInstanceFieldGet * instr)585 void ReferenceTypePropagation::RTPVisitor::VisitPredicatedInstanceFieldGet(
586     HPredicatedInstanceFieldGet* instr) {
587   UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
588 }
589 
VisitInstanceFieldGet(HInstanceFieldGet * instr)590 void ReferenceTypePropagation::RTPVisitor::VisitInstanceFieldGet(HInstanceFieldGet* instr) {
591   UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
592 }
593 
VisitStaticFieldGet(HStaticFieldGet * instr)594 void ReferenceTypePropagation::RTPVisitor::VisitStaticFieldGet(HStaticFieldGet* instr) {
595   UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
596 }
597 
VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet * instr)598 void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedInstanceFieldGet(
599     HUnresolvedInstanceFieldGet* instr) {
600   // TODO: Use descriptor to get the actual type.
601   if (instr->GetFieldType() == DataType::Type::kReference) {
602     instr->SetReferenceTypeInfo(GetGraph()->GetInexactObjectRti());
603   }
604 }
605 
VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet * instr)606 void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedStaticFieldGet(
607     HUnresolvedStaticFieldGet* instr) {
608   // TODO: Use descriptor to get the actual type.
609   if (instr->GetFieldType() == DataType::Type::kReference) {
610     instr->SetReferenceTypeInfo(GetGraph()->GetInexactObjectRti());
611   }
612 }
613 
VisitLoadClass(HLoadClass * instr)614 void ReferenceTypePropagation::RTPVisitor::VisitLoadClass(HLoadClass* instr) {
615   ScopedObjectAccess soa(Thread::Current());
616   if (IsAdmissible(instr->GetClass().Get())) {
617     instr->SetValidLoadedClassRTI();
618   }
619   instr->SetReferenceTypeInfo(
620       ReferenceTypeInfo::Create(GetHandleCache()->GetClassClassHandle(), /* is_exact= */ true));
621 }
622 
VisitInstanceOf(HInstanceOf * instr)623 void ReferenceTypePropagation::RTPVisitor::VisitInstanceOf(HInstanceOf* instr) {
624   ScopedObjectAccess soa(Thread::Current());
625   if (IsAdmissible(instr->GetClass().Get())) {
626     instr->SetValidTargetClassRTI();
627   }
628 }
629 
VisitClinitCheck(HClinitCheck * instr)630 void ReferenceTypePropagation::RTPVisitor::VisitClinitCheck(HClinitCheck* instr) {
631   instr->SetReferenceTypeInfo(instr->InputAt(0)->GetReferenceTypeInfo());
632 }
633 
VisitLoadMethodHandle(HLoadMethodHandle * instr)634 void ReferenceTypePropagation::RTPVisitor::VisitLoadMethodHandle(HLoadMethodHandle* instr) {
635   instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
636       GetHandleCache()->GetMethodHandleClassHandle(), /* is_exact= */ true));
637 }
638 
VisitLoadMethodType(HLoadMethodType * instr)639 void ReferenceTypePropagation::RTPVisitor::VisitLoadMethodType(HLoadMethodType* instr) {
640   instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
641       GetHandleCache()->GetMethodTypeClassHandle(), /* is_exact= */ true));
642 }
643 
VisitLoadString(HLoadString * instr)644 void ReferenceTypePropagation::RTPVisitor::VisitLoadString(HLoadString* instr) {
645   instr->SetReferenceTypeInfo(
646       ReferenceTypeInfo::Create(GetHandleCache()->GetStringClassHandle(), /* is_exact= */ true));
647 }
648 
VisitLoadException(HLoadException * instr)649 void ReferenceTypePropagation::RTPVisitor::VisitLoadException(HLoadException* instr) {
650   DCHECK(instr->GetBlock()->IsCatchBlock());
651   TryCatchInformation* catch_info = instr->GetBlock()->GetTryCatchInformation();
652 
653   if (catch_info->IsValidTypeIndex()) {
654     UpdateReferenceTypeInfo(instr,
655                             catch_info->GetCatchTypeIndex(),
656                             catch_info->GetCatchDexFile(),
657                             /* is_exact= */ false);
658   } else {
659     instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
660         GetHandleCache()->GetThrowableClassHandle(), /* is_exact= */ false));
661   }
662 }
663 
VisitNullCheck(HNullCheck * instr)664 void ReferenceTypePropagation::RTPVisitor::VisitNullCheck(HNullCheck* instr) {
665   ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
666   if (parent_rti.IsValid()) {
667     instr->SetReferenceTypeInfo(parent_rti);
668   }
669 }
670 
VisitBoundType(HBoundType * instr)671 void ReferenceTypePropagation::RTPVisitor::VisitBoundType(HBoundType* instr) {
672   ReferenceTypeInfo class_rti = instr->GetUpperBound();
673   if (class_rti.IsValid()) {
674     ScopedObjectAccess soa(Thread::Current());
675     // Narrow the type as much as possible.
676     HInstruction* obj = instr->InputAt(0);
677     ReferenceTypeInfo obj_rti = obj->GetReferenceTypeInfo();
678     if (class_rti.IsExact()) {
679       instr->SetReferenceTypeInfo(class_rti);
680     } else if (obj_rti.IsValid()) {
681       if (class_rti.IsSupertypeOf(obj_rti)) {
682         // Object type is more specific.
683         instr->SetReferenceTypeInfo(obj_rti);
684       } else {
685         // Upper bound is more specific, or unrelated to the object's type.
686         // Note that the object might then be exact, and we know the code dominated by this
687         // bound type is dead. To not confuse potential other optimizations, we mark
688         // the bound as non-exact.
689         instr->SetReferenceTypeInfo(
690             ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact= */ false));
691       }
692     } else {
693       // Object not typed yet. Leave BoundType untyped for now rather than
694       // assign the type conservatively.
695     }
696     instr->SetCanBeNull(obj->CanBeNull() && instr->GetUpperCanBeNull());
697   } else {
698     // The owner of the BoundType was already visited. If the class is unresolved,
699     // the BoundType should have been removed from the data flow and this method
700     // should remove it from the graph.
701     DCHECK(!instr->HasUses());
702     instr->GetBlock()->RemoveInstruction(instr);
703   }
704 }
705 
VisitCheckCast(HCheckCast * check_cast)706 void ReferenceTypePropagation::RTPVisitor::VisitCheckCast(HCheckCast* check_cast) {
707   HBoundType* bound_type = check_cast->GetNext()->AsBoundType();
708   if (bound_type == nullptr || bound_type->GetUpperBound().IsValid()) {
709     // The next instruction is not an uninitialized BoundType. This must be
710     // an RTP pass after SsaBuilder and we do not need to do anything.
711     return;
712   }
713   DCHECK_EQ(bound_type->InputAt(0), check_cast->InputAt(0));
714 
715   ScopedObjectAccess soa(Thread::Current());
716   Handle<mirror::Class> klass = check_cast->GetClass();
717   if (IsAdmissible(klass.Get())) {
718     DCHECK(is_first_run_);
719     check_cast->SetValidTargetClassRTI();
720     // This is the first run of RTP and class is resolved.
721     bool is_exact = klass->CannotBeAssignedFromOtherTypes();
722     bound_type->SetUpperBound(ReferenceTypeInfo::Create(klass, is_exact),
723                               /* CheckCast succeeds for nulls. */ true);
724   } else {
725     // This is the first run of RTP and class is unresolved. Remove the binding.
726     // The instruction itself is removed in VisitBoundType so as to not
727     // invalidate HInstructionIterator.
728     bound_type->ReplaceWith(bound_type->InputAt(0));
729   }
730 }
731 
VisitPhi(HPhi * phi)732 void ReferenceTypePropagation::RTPVisitor::VisitPhi(HPhi* phi) {
733   if (phi->IsDead() || phi->GetType() != DataType::Type::kReference) {
734     return;
735   }
736 
737   if (phi->GetBlock()->IsLoopHeader()) {
738     // Set the initial type for the phi. Use the non back edge input for reaching
739     // a fixed point faster.
740     HInstruction* first_input = phi->InputAt(0);
741     ReferenceTypeInfo first_input_rti = first_input->GetReferenceTypeInfo();
742     if (first_input_rti.IsValid() && !first_input->IsNullConstant()) {
743       phi->SetCanBeNull(first_input->CanBeNull());
744       phi->SetReferenceTypeInfo(first_input_rti);
745     }
746     AddToWorklist(phi);
747   } else {
748     // Eagerly compute the type of the phi, for quicker convergence. Note
749     // that we don't need to add users to the worklist because we are
750     // doing a reverse post-order visit, therefore either the phi users are
751     // non-loop phi and will be visited later in the visit, or are loop-phis,
752     // and they are already in the work list.
753     UpdateNullability(phi);
754     UpdateReferenceTypeInfo(phi);
755   }
756 }
757 
FixUpInstructionType(HInstruction * instruction,HandleCache * handle_cache)758 void ReferenceTypePropagation::FixUpInstructionType(HInstruction* instruction,
759                                                     HandleCache* handle_cache) {
760   if (instruction->IsSelect()) {
761     ScopedObjectAccess soa(Thread::Current());
762     HSelect* select = instruction->AsSelect();
763     ReferenceTypeInfo false_rti = select->GetFalseValue()->GetReferenceTypeInfo();
764     ReferenceTypeInfo true_rti = select->GetTrueValue()->GetReferenceTypeInfo();
765     select->SetReferenceTypeInfo(MergeTypes(false_rti, true_rti, handle_cache));
766   } else {
767     LOG(FATAL) << "Invalid instruction in FixUpInstructionType";
768   }
769 }
770 
MergeTypes(const ReferenceTypeInfo & a,const ReferenceTypeInfo & b,HandleCache * handle_cache)771 ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a,
772                                                        const ReferenceTypeInfo& b,
773                                                        HandleCache* handle_cache) {
774   if (!b.IsValid()) {
775     return a;
776   }
777   if (!a.IsValid()) {
778     return b;
779   }
780 
781   bool is_exact = a.IsExact() && b.IsExact();
782   ReferenceTypeInfo::TypeHandle result_type_handle;
783   ReferenceTypeInfo::TypeHandle a_type_handle = a.GetTypeHandle();
784   ReferenceTypeInfo::TypeHandle b_type_handle = b.GetTypeHandle();
785   bool a_is_interface = a_type_handle->IsInterface();
786   bool b_is_interface = b_type_handle->IsInterface();
787 
788   if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) {
789     result_type_handle = a_type_handle;
790   } else if (a.IsSupertypeOf(b)) {
791     result_type_handle = a_type_handle;
792     is_exact = false;
793   } else if (b.IsSupertypeOf(a)) {
794     result_type_handle = b_type_handle;
795     is_exact = false;
796   } else if (!a_is_interface && !b_is_interface) {
797     result_type_handle =
798         handle_cache->NewHandle(a_type_handle->GetCommonSuperClass(b_type_handle));
799     is_exact = false;
800   } else {
801     // This can happen if:
802     //    - both types are interfaces. TODO(calin): implement
803     //    - one is an interface, the other a class, and the type does not implement the interface
804     //      e.g:
805     //        void foo(Interface i, boolean cond) {
806     //          Object o = cond ? i : new Object();
807     //        }
808     result_type_handle = handle_cache->GetObjectClassHandle();
809     is_exact = false;
810   }
811 
812   return ReferenceTypeInfo::Create(result_type_handle, is_exact);
813 }
814 
UpdateArrayGet(HArrayGet * instr)815 void ReferenceTypePropagation::RTPVisitor::UpdateArrayGet(HArrayGet* instr) {
816   DCHECK_EQ(DataType::Type::kReference, instr->GetType());
817 
818   ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
819   if (!parent_rti.IsValid()) {
820     return;
821   }
822 
823   Handle<mirror::Class> handle = parent_rti.GetTypeHandle();
824   if (handle->IsObjectArrayClass() && IsAdmissible(handle->GetComponentType())) {
825     ReferenceTypeInfo::TypeHandle component_handle =
826         GetHandleCache()->NewHandle(handle->GetComponentType());
827     bool is_exact = component_handle->CannotBeAssignedFromOtherTypes();
828     instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(component_handle, is_exact));
829   } else {
830     // We don't know what the parent actually is, so we fallback to object.
831     instr->SetReferenceTypeInfo(GetGraph()->GetInexactObjectRti());
832   }
833 }
834 
UpdateReferenceTypeInfo(HInstruction * instr)835 bool ReferenceTypePropagation::RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr) {
836   ScopedObjectAccess soa(Thread::Current());
837 
838   ReferenceTypeInfo previous_rti = instr->GetReferenceTypeInfo();
839   if (instr->IsBoundType()) {
840     UpdateBoundType(instr->AsBoundType());
841   } else if (instr->IsPhi()) {
842     UpdatePhi(instr->AsPhi());
843   } else if (instr->IsNullCheck()) {
844     ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
845     if (parent_rti.IsValid()) {
846       instr->SetReferenceTypeInfo(parent_rti);
847     }
848   } else if (instr->IsArrayGet()) {
849     // TODO: consider if it's worth "looking back" and binding the input object
850     // to an array type.
851     UpdateArrayGet(instr->AsArrayGet());
852   } else {
853     LOG(FATAL) << "Invalid instruction (should not get here)";
854   }
855 
856   return !previous_rti.IsEqual(instr->GetReferenceTypeInfo());
857 }
858 
VisitInvoke(HInvoke * instr)859 void ReferenceTypePropagation::RTPVisitor::VisitInvoke(HInvoke* instr) {
860   if (instr->GetType() != DataType::Type::kReference) {
861     return;
862   }
863 
864   ScopedObjectAccess soa(Thread::Current());
865   // FIXME: Treat InvokePolymorphic separately, as we can get a more specific return type from
866   // protoId than the one obtained from the resolved method.
867   ArtMethod* method = instr->GetResolvedMethod();
868   ObjPtr<mirror::Class> klass = (method == nullptr) ? nullptr : method->LookupResolvedReturnType();
869   SetClassAsTypeInfo(instr, klass, /* is_exact= */ false);
870 }
871 
VisitArrayGet(HArrayGet * instr)872 void ReferenceTypePropagation::RTPVisitor::VisitArrayGet(HArrayGet* instr) {
873   if (instr->GetType() != DataType::Type::kReference) {
874     return;
875   }
876 
877   ScopedObjectAccess soa(Thread::Current());
878   UpdateArrayGet(instr);
879   if (!instr->GetReferenceTypeInfo().IsValid()) {
880     worklist_.push_back(instr);
881   }
882 }
883 
UpdateBoundType(HBoundType * instr)884 void ReferenceTypePropagation::RTPVisitor::UpdateBoundType(HBoundType* instr) {
885   ReferenceTypeInfo input_rti = instr->InputAt(0)->GetReferenceTypeInfo();
886   if (!input_rti.IsValid()) {
887     return;  // No new info yet.
888   }
889 
890   ReferenceTypeInfo upper_bound_rti = instr->GetUpperBound();
891   if (upper_bound_rti.IsExact()) {
892     instr->SetReferenceTypeInfo(upper_bound_rti);
893   } else if (upper_bound_rti.IsSupertypeOf(input_rti)) {
894     // input is more specific.
895     instr->SetReferenceTypeInfo(input_rti);
896   } else {
897     // upper_bound is more specific or unrelated.
898     // Note that the object might then be exact, and we know the code dominated by this
899     // bound type is dead. To not confuse potential other optimizations, we mark
900     // the bound as non-exact.
901     instr->SetReferenceTypeInfo(
902         ReferenceTypeInfo::Create(upper_bound_rti.GetTypeHandle(), /* is_exact= */ false));
903   }
904 }
905 
906 // NullConstant inputs are ignored during merging as they do not provide any useful information.
907 // If all the inputs are NullConstants then the type of the phi will be set to Object.
UpdatePhi(HPhi * instr)908 void ReferenceTypePropagation::RTPVisitor::UpdatePhi(HPhi* instr) {
909   DCHECK(instr->IsLive());
910 
911   HInputsRef inputs = instr->GetInputs();
912   size_t first_input_index_not_null = 0;
913   while (first_input_index_not_null < inputs.size() &&
914          inputs[first_input_index_not_null]->IsNullConstant()) {
915     first_input_index_not_null++;
916   }
917   if (first_input_index_not_null == inputs.size()) {
918     // All inputs are NullConstants, set the type to object.
919     // This may happen in the presence of inlining.
920     instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
921     return;
922   }
923 
924   ReferenceTypeInfo new_rti = instr->InputAt(first_input_index_not_null)->GetReferenceTypeInfo();
925 
926   if (new_rti.IsValid() && new_rti.IsObjectClass() && !new_rti.IsExact()) {
927     // Early return if we are Object and inexact.
928     instr->SetReferenceTypeInfo(new_rti);
929     return;
930   }
931 
932   for (size_t i = first_input_index_not_null + 1; i < inputs.size(); i++) {
933     if (inputs[i]->IsNullConstant()) {
934       continue;
935     }
936     new_rti = MergeTypes(new_rti, inputs[i]->GetReferenceTypeInfo(), GetHandleCache());
937     if (new_rti.IsValid() && new_rti.IsObjectClass()) {
938       if (!new_rti.IsExact()) {
939         break;
940       } else {
941         continue;
942       }
943     }
944   }
945 
946   if (new_rti.IsValid()) {
947     instr->SetReferenceTypeInfo(new_rti);
948   }
949 }
950 
IsUpdateable(const HInstruction * instr)951 constexpr bool ReferenceTypePropagation::RTPVisitor::IsUpdateable(const HInstruction* instr) {
952   return (instr->IsPhi() && instr->AsPhi()->IsLive()) ||
953          instr->IsBoundType() ||
954          instr->IsNullCheck() ||
955          instr->IsArrayGet();
956 }
957 
958 // Re-computes and updates the nullability of the instruction. Returns whether or
959 // not the nullability was changed.
UpdateNullability(HInstruction * instr)960 bool ReferenceTypePropagation::RTPVisitor::UpdateNullability(HInstruction* instr) {
961   DCHECK(IsUpdateable(instr));
962 
963   if (!instr->IsPhi() && !instr->IsBoundType()) {
964     return false;
965   }
966 
967   bool existing_can_be_null = instr->CanBeNull();
968   if (instr->IsPhi()) {
969     HPhi* phi = instr->AsPhi();
970     bool new_can_be_null = false;
971     for (HInstruction* input : phi->GetInputs()) {
972       if (input->CanBeNull()) {
973         new_can_be_null = true;
974         break;
975       }
976     }
977     phi->SetCanBeNull(new_can_be_null);
978   } else if (instr->IsBoundType()) {
979     HBoundType* bound_type = instr->AsBoundType();
980     bound_type->SetCanBeNull(instr->InputAt(0)->CanBeNull() && bound_type->GetUpperCanBeNull());
981   }
982   return existing_can_be_null != instr->CanBeNull();
983 }
984 
ProcessWorklist()985 void ReferenceTypePropagation::RTPVisitor::ProcessWorklist() {
986   while (!worklist_.empty()) {
987     HInstruction* instruction = worklist_.back();
988     worklist_.pop_back();
989     bool updated_nullability = UpdateNullability(instruction);
990     bool updated_reference_type = UpdateReferenceTypeInfo(instruction);
991     if (updated_nullability || updated_reference_type) {
992       AddDependentInstructionsToWorklist(instruction);
993     }
994   }
995 }
996 
AddToWorklist(HInstruction * instruction)997 void ReferenceTypePropagation::RTPVisitor::AddToWorklist(HInstruction* instruction) {
998   DCHECK_EQ(instruction->GetType(), DataType::Type::kReference)
999       << instruction->DebugName() << ":" << instruction->GetType();
1000   worklist_.push_back(instruction);
1001 }
1002 
AddDependentInstructionsToWorklist(HInstruction * instruction)1003 void ReferenceTypePropagation::RTPVisitor::AddDependentInstructionsToWorklist(
1004     HInstruction* instruction) {
1005   for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
1006     HInstruction* user = use.GetUser();
1007     if ((user->IsPhi() && user->AsPhi()->IsLive())
1008        || user->IsBoundType()
1009        || user->IsNullCheck()
1010        || (user->IsArrayGet() && (user->GetType() == DataType::Type::kReference))) {
1011       AddToWorklist(user);
1012     }
1013   }
1014 }
1015 
1016 }  // namespace art
1017