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