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