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