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