blob: cd4391d2db7a0ee309488afa85dd084123bcf16d [file] [log] [blame]
Calin Juravle10e244f2015-01-26 18:54:32 +00001/*
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
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "class_linker-inl.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000020#include "mirror/class-inl.h"
21#include "mirror/dex_cache.h"
22#include "scoped_thread_state_change.h"
23
Calin Juravle10e244f2015-01-26 18:54:32 +000024namespace art {
25
Vladimir Marko7d1fbf32016-01-26 15:01:12 +000026static inline ReferenceTypeInfo::TypeHandle GetRootHandle(StackHandleScopeCollection* handles,
27 ClassLinker::ClassRoot class_root,
28 ReferenceTypeInfo::TypeHandle* cache) {
29 if (!ReferenceTypeInfo::IsValidHandle(*cache)) {
30 // Mutator lock is required for NewHandle.
31 ClassLinker* linker = Runtime::Current()->GetClassLinker();
32 ScopedObjectAccess soa(Thread::Current());
33 *cache = handles->NewHandle(linker->GetClassRoot(class_root));
34 }
35 return *cache;
36}
37
38ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetObjectClassHandle() {
39 return GetRootHandle(handles_, ClassLinker::kJavaLangObject, &object_class_handle_);
40}
41
42ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetClassClassHandle() {
43 return GetRootHandle(handles_, ClassLinker::kJavaLangClass, &class_class_handle_);
44}
45
46ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetStringClassHandle() {
47 return GetRootHandle(handles_, ClassLinker::kJavaLangString, &string_class_handle_);
48}
49
50ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetThrowableClassHandle() {
51 return GetRootHandle(handles_, ClassLinker::kJavaLangThrowable, &throwable_class_handle_);
52}
53
54class ReferenceTypePropagation::RTPVisitor : public HGraphDelegateVisitor {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010055 public:
Calin Juravle2e768302015-07-28 14:41:11 +000056 RTPVisitor(HGraph* graph,
Vladimir Marko7d1fbf32016-01-26 15:01:12 +000057 HandleCache* handle_cache,
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +000058 ArenaVector<HInstruction*>* worklist,
59 bool is_first_run)
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010060 : HGraphDelegateVisitor(graph),
Vladimir Marko7d1fbf32016-01-26 15:01:12 +000061 handle_cache_(handle_cache),
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +000062 worklist_(worklist),
63 is_first_run_(is_first_run) {}
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010064
65 void VisitNewInstance(HNewInstance* new_instance) OVERRIDE;
66 void VisitLoadClass(HLoadClass* load_class) OVERRIDE;
Calin Juravle2e768302015-07-28 14:41:11 +000067 void VisitClinitCheck(HClinitCheck* clinit_check) OVERRIDE;
68 void VisitLoadString(HLoadString* instr) OVERRIDE;
David Brazdilbbd733e2015-08-18 17:48:17 +010069 void VisitLoadException(HLoadException* instr) OVERRIDE;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010070 void VisitNewArray(HNewArray* instr) OVERRIDE;
Calin Juravle2e768302015-07-28 14:41:11 +000071 void VisitParameterValue(HParameterValue* instr) OVERRIDE;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010072 void UpdateFieldAccessTypeInfo(HInstruction* instr, const FieldInfo& info);
73 void SetClassAsTypeInfo(HInstruction* instr, mirror::Class* klass, bool is_exact);
74 void VisitInstanceFieldGet(HInstanceFieldGet* instr) OVERRIDE;
75 void VisitStaticFieldGet(HStaticFieldGet* instr) OVERRIDE;
Calin Juravlee460d1d2015-09-29 04:52:17 +010076 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* instr) OVERRIDE;
77 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* instr) OVERRIDE;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010078 void VisitInvoke(HInvoke* instr) OVERRIDE;
Guillaume "Vermeille" Sanchez72a5eb52015-06-02 17:39:45 +010079 void VisitArrayGet(HArrayGet* instr) OVERRIDE;
Calin Juravlea5ae3c32015-07-28 14:40:50 +000080 void VisitCheckCast(HCheckCast* instr) OVERRIDE;
David Brazdilf5552582015-12-27 13:36:12 +000081 void VisitBoundType(HBoundType* instr) OVERRIDE;
Calin Juravle2e768302015-07-28 14:41:11 +000082 void VisitNullCheck(HNullCheck* instr) OVERRIDE;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010083 void UpdateReferenceTypeInfo(HInstruction* instr,
84 uint16_t type_idx,
85 const DexFile& dex_file,
86 bool is_exact);
87
88 private:
Vladimir Marko7d1fbf32016-01-26 15:01:12 +000089 HandleCache* handle_cache_;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010090 ArenaVector<HInstruction*>* worklist_;
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +000091 const bool is_first_run_;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010092};
93
Calin Juravle2e768302015-07-28 14:41:11 +000094ReferenceTypePropagation::ReferenceTypePropagation(HGraph* graph,
95 StackHandleScopeCollection* handles,
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +000096 bool is_first_run,
Calin Juravle2e768302015-07-28 14:41:11 +000097 const char* name)
98 : HOptimization(graph, name),
Vladimir Marko7d1fbf32016-01-26 15:01:12 +000099 handle_cache_(handles),
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000100 worklist_(graph->GetArena()->Adapter(kArenaAllocReferenceTypePropagation)),
101 is_first_run_(is_first_run) {
Calin Juravle2e768302015-07-28 14:41:11 +0000102}
103
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000104void ReferenceTypePropagation::ValidateTypes() {
105 // TODO: move this to the graph checker.
Calin Juravle2e768302015-07-28 14:41:11 +0000106 if (kIsDebugBuild) {
Calin Juravle2e768302015-07-28 14:41:11 +0000107 ScopedObjectAccess soa(Thread::Current());
108 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
109 HBasicBlock* block = it.Current();
110 for (HInstructionIterator iti(block->GetInstructions()); !iti.Done(); iti.Advance()) {
111 HInstruction* instr = iti.Current();
112 if (instr->GetType() == Primitive::kPrimNot) {
113 DCHECK(instr->GetReferenceTypeInfo().IsValid())
114 << "Invalid RTI for instruction: " << instr->DebugName();
115 if (instr->IsBoundType()) {
116 DCHECK(instr->AsBoundType()->GetUpperBound().IsValid());
117 } else if (instr->IsLoadClass()) {
Calin Juravle98893e12015-10-02 21:05:03 +0100118 HLoadClass* cls = instr->AsLoadClass();
119 DCHECK(cls->GetReferenceTypeInfo().IsExact());
120 DCHECK(!cls->GetLoadedClassRTI().IsValid() || cls->GetLoadedClassRTI().IsExact());
Calin Juravle2e768302015-07-28 14:41:11 +0000121 } else if (instr->IsNullCheck()) {
122 DCHECK(instr->GetReferenceTypeInfo().IsEqual(instr->InputAt(0)->GetReferenceTypeInfo()))
123 << "NullCheck " << instr->GetReferenceTypeInfo()
124 << "Input(0) " << instr->InputAt(0)->GetReferenceTypeInfo();
125 }
126 }
127 }
128 }
129 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000130}
131
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000132void ReferenceTypePropagation::Visit(HInstruction* instruction) {
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000133 RTPVisitor visitor(graph_, &handle_cache_, &worklist_, is_first_run_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000134 instruction->Accept(&visitor);
135}
136
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000137void ReferenceTypePropagation::Run() {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000138 worklist_.reserve(kDefaultWorklistSize);
139
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000140 // To properly propagate type info we need to visit in the dominator-based order.
141 // Reverse post order guarantees a node's dominators are visited first.
142 // We take advantage of this order in `VisitBasicBlock`.
143 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
144 VisitBasicBlock(it.Current());
145 }
146
147 ProcessWorklist();
148 ValidateTypes();
149}
150
Calin Juravleb1498f62015-02-16 13:13:29 +0000151void ReferenceTypePropagation::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000152 RTPVisitor visitor(graph_, &handle_cache_, &worklist_, is_first_run_);
Calin Juravle2e768302015-07-28 14:41:11 +0000153 // Handle Phis first as there might be instructions in the same block who depend on them.
154 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
155 VisitPhi(it.Current()->AsPhi());
156 }
Calin Juravlebeba9302015-07-08 15:57:18 +0000157
Calin Juravle2e768302015-07-28 14:41:11 +0000158 // Handle instructions.
Calin Juravleb1498f62015-02-16 13:13:29 +0000159 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
160 HInstruction* instr = it.Current();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100161 instr->Accept(&visitor);
Calin Juravleb1498f62015-02-16 13:13:29 +0000162 }
163
Calin Juravleb1498f62015-02-16 13:13:29 +0000164 // Add extra nodes to bound types.
Calin Juravle61d544b2015-02-23 16:46:57 +0000165 BoundTypeForIfNotNull(block);
Calin Juravleb1498f62015-02-16 13:13:29 +0000166 BoundTypeForIfInstanceOf(block);
Calin Juravle10e244f2015-01-26 18:54:32 +0000167}
168
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000169// Check if we should create a bound type for the given object at the specified
170// position. Because of inlining and the fact we run RTP more than once and we
171// might have a HBoundType already. If we do, we should not create a new one.
172// In this case we also assert that there are no other uses of the object (except
173// the bound type) dominated by the specified dominator_instr or dominator_block.
174static bool ShouldCreateBoundType(HInstruction* position,
175 HInstruction* obj,
176 ReferenceTypeInfo upper_bound,
177 HInstruction* dominator_instr,
178 HBasicBlock* dominator_block)
179 SHARED_REQUIRES(Locks::mutator_lock_) {
180 // If the position where we should insert the bound type is not already a
181 // a bound type then we need to create one.
182 if (position == nullptr || !position->IsBoundType()) {
183 return true;
184 }
185
186 HBoundType* existing_bound_type = position->AsBoundType();
187 if (existing_bound_type->GetUpperBound().IsSupertypeOf(upper_bound)) {
188 if (kIsDebugBuild) {
189 // Check that the existing HBoundType dominates all the uses.
Vladimir Marko46817b82016-03-29 12:21:58 +0100190 for (const HUseListNode<HInstruction*>& use : obj->GetUses()) {
191 HInstruction* user = use.GetUser();
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000192 if (dominator_instr != nullptr) {
193 DCHECK(!dominator_instr->StrictlyDominates(user)
194 || user == existing_bound_type
195 || existing_bound_type->StrictlyDominates(user));
196 } else if (dominator_block != nullptr) {
197 DCHECK(!dominator_block->Dominates(user->GetBlock())
198 || user == existing_bound_type
199 || existing_bound_type->StrictlyDominates(user));
200 }
201 }
202 }
203 } else {
204 // TODO: if the current bound type is a refinement we could update the
205 // existing_bound_type with the a new upper limit. However, we also need to
206 // update its users and have access to the work list.
207 }
208 return false;
209}
210
Calin Juravle61d544b2015-02-23 16:46:57 +0000211void ReferenceTypePropagation::BoundTypeForIfNotNull(HBasicBlock* block) {
Calin Juravleb3306642015-04-20 18:30:42 +0100212 HIf* ifInstruction = block->GetLastInstruction()->AsIf();
213 if (ifInstruction == nullptr) {
Calin Juravle61d544b2015-02-23 16:46:57 +0000214 return;
215 }
Calin Juravleb3306642015-04-20 18:30:42 +0100216 HInstruction* ifInput = ifInstruction->InputAt(0);
Calin Juravle61d544b2015-02-23 16:46:57 +0000217 if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) {
218 return;
219 }
220 HInstruction* input0 = ifInput->InputAt(0);
221 HInstruction* input1 = ifInput->InputAt(1);
Calin Juravleedad8ad2015-04-23 14:34:33 +0100222 HInstruction* obj = nullptr;
Calin Juravle61d544b2015-02-23 16:46:57 +0000223
Calin Juravleedad8ad2015-04-23 14:34:33 +0100224 if (input1->IsNullConstant()) {
Calin Juravle61d544b2015-02-23 16:46:57 +0000225 obj = input0;
Calin Juravleedad8ad2015-04-23 14:34:33 +0100226 } else if (input0->IsNullConstant()) {
Calin Juravle61d544b2015-02-23 16:46:57 +0000227 obj = input1;
228 } else {
229 return;
230 }
231
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +0100232 if (!obj->CanBeNull() || obj->IsNullConstant()) {
233 // Null check is dead code and will be removed by DCE.
234 return;
235 }
236 DCHECK(!obj->IsLoadClass()) << "We should not replace HLoadClass instructions";
237
Calin Juravleb3306642015-04-20 18:30:42 +0100238 // We only need to bound the type if we have uses in the relevant block.
239 // So start with null and create the HBoundType lazily, only if it's needed.
240 HBoundType* bound_type = nullptr;
Calin Juravle61d544b2015-02-23 16:46:57 +0000241 HBasicBlock* notNullBlock = ifInput->IsNotEqual()
Calin Juravleb3306642015-04-20 18:30:42 +0100242 ? ifInstruction->IfTrueSuccessor()
243 : ifInstruction->IfFalseSuccessor();
244
Vladimir Marko46817b82016-03-29 12:21:58 +0100245 const HUseList<HInstruction*>& uses = obj->GetUses();
246 for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
247 HInstruction* user = it->GetUser();
248 size_t index = it->GetIndex();
249 // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput().
250 ++it;
Calin Juravle61d544b2015-02-23 16:46:57 +0000251 if (notNullBlock->Dominates(user->GetBlock())) {
Calin Juravleb3306642015-04-20 18:30:42 +0100252 if (bound_type == nullptr) {
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000253 ScopedObjectAccess soa(Thread::Current());
254 HInstruction* insert_point = notNullBlock->GetFirstInstruction();
Calin Juravle2e768302015-07-28 14:41:11 +0000255 ReferenceTypeInfo object_rti = ReferenceTypeInfo::Create(
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000256 handle_cache_.GetObjectClassHandle(), /* is_exact */ true);
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000257 if (ShouldCreateBoundType(insert_point, obj, object_rti, nullptr, notNullBlock)) {
David Brazdilf5552582015-12-27 13:36:12 +0000258 bound_type = new (graph_->GetArena()) HBoundType(obj);
259 bound_type->SetUpperBound(object_rti, /* bound_can_be_null */ false);
Calin Juravle2e768302015-07-28 14:41:11 +0000260 if (obj->GetReferenceTypeInfo().IsValid()) {
261 bound_type->SetReferenceTypeInfo(obj->GetReferenceTypeInfo());
262 }
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000263 notNullBlock->InsertInstructionBefore(bound_type, insert_point);
264 } else {
265 // We already have a bound type on the position we would need to insert
266 // the new one. The existing bound type should dominate all the users
267 // (dchecked) so there's no need to continue.
268 break;
269 }
Calin Juravleb3306642015-04-20 18:30:42 +0100270 }
Vladimir Marko46817b82016-03-29 12:21:58 +0100271 user->ReplaceInput(bound_type, index);
Calin Juravle61d544b2015-02-23 16:46:57 +0000272 }
273 }
274}
275
David Brazdild9510df2015-11-04 23:30:22 +0000276// Returns true if one of the patterns below has been recognized. If so, the
277// InstanceOf instruction together with the true branch of `ifInstruction` will
278// be returned using the out parameters.
279// Recognized patterns:
280// (1) patterns equivalent to `if (obj instanceof X)`
281// (a) InstanceOf -> Equal to 1 -> If
282// (b) InstanceOf -> NotEqual to 0 -> If
283// (c) InstanceOf -> If
284// (2) patterns equivalent to `if (!(obj instanceof X))`
285// (a) InstanceOf -> Equal to 0 -> If
286// (b) InstanceOf -> NotEqual to 1 -> If
287// (c) InstanceOf -> BooleanNot -> If
288static bool MatchIfInstanceOf(HIf* ifInstruction,
289 /* out */ HInstanceOf** instanceOf,
290 /* out */ HBasicBlock** trueBranch) {
291 HInstruction* input = ifInstruction->InputAt(0);
292
293 if (input->IsEqual()) {
294 HInstruction* rhs = input->AsEqual()->GetConstantRight();
295 if (rhs != nullptr) {
296 HInstruction* lhs = input->AsEqual()->GetLeastConstantLeft();
297 if (lhs->IsInstanceOf() && rhs->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000298 if (rhs->AsIntConstant()->IsTrue()) {
David Brazdild9510df2015-11-04 23:30:22 +0000299 // Case (1a)
300 *trueBranch = ifInstruction->IfTrueSuccessor();
301 } else {
302 // Case (2a)
Roland Levillain1a653882016-03-18 18:05:57 +0000303 DCHECK(rhs->AsIntConstant()->IsFalse()) << rhs->AsIntConstant()->GetValue();
David Brazdild9510df2015-11-04 23:30:22 +0000304 *trueBranch = ifInstruction->IfFalseSuccessor();
305 }
306 *instanceOf = lhs->AsInstanceOf();
307 return true;
308 }
309 }
310 } else if (input->IsNotEqual()) {
311 HInstruction* rhs = input->AsNotEqual()->GetConstantRight();
312 if (rhs != nullptr) {
313 HInstruction* lhs = input->AsNotEqual()->GetLeastConstantLeft();
314 if (lhs->IsInstanceOf() && rhs->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000315 if (rhs->AsIntConstant()->IsFalse()) {
David Brazdild9510df2015-11-04 23:30:22 +0000316 // Case (1b)
317 *trueBranch = ifInstruction->IfTrueSuccessor();
318 } else {
319 // Case (2b)
Roland Levillain1a653882016-03-18 18:05:57 +0000320 DCHECK(rhs->AsIntConstant()->IsTrue()) << rhs->AsIntConstant()->GetValue();
David Brazdild9510df2015-11-04 23:30:22 +0000321 *trueBranch = ifInstruction->IfFalseSuccessor();
322 }
323 *instanceOf = lhs->AsInstanceOf();
324 return true;
325 }
326 }
327 } else if (input->IsInstanceOf()) {
328 // Case (1c)
329 *instanceOf = input->AsInstanceOf();
330 *trueBranch = ifInstruction->IfTrueSuccessor();
331 return true;
332 } else if (input->IsBooleanNot()) {
333 HInstruction* not_input = input->InputAt(0);
334 if (not_input->IsInstanceOf()) {
335 // Case (2c)
336 *instanceOf = not_input->AsInstanceOf();
337 *trueBranch = ifInstruction->IfFalseSuccessor();
338 return true;
339 }
340 }
341
342 return false;
343}
344
Calin Juravleb1498f62015-02-16 13:13:29 +0000345// Detects if `block` is the True block for the pattern
346// `if (x instanceof ClassX) { }`
347// If that's the case insert an HBoundType instruction to bound the type of `x`
348// to `ClassX` in the scope of the dominated blocks.
349void ReferenceTypePropagation::BoundTypeForIfInstanceOf(HBasicBlock* block) {
Calin Juravleb3306642015-04-20 18:30:42 +0100350 HIf* ifInstruction = block->GetLastInstruction()->AsIf();
351 if (ifInstruction == nullptr) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000352 return;
353 }
David Brazdil0d13fee2015-04-17 14:52:19 +0100354
David Brazdild9510df2015-11-04 23:30:22 +0000355 // Try to recognize common `if (instanceof)` and `if (!instanceof)` patterns.
356 HInstanceOf* instanceOf = nullptr;
357 HBasicBlock* instanceOfTrueBlock = nullptr;
358 if (!MatchIfInstanceOf(ifInstruction, &instanceOf, &instanceOfTrueBlock)) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000359 return;
Calin Juravleacf735c2015-02-12 15:25:22 +0000360 }
361
Calin Juravle98893e12015-10-02 21:05:03 +0100362 HLoadClass* load_class = instanceOf->InputAt(1)->AsLoadClass();
363 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
364 {
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000365 ScopedObjectAccess soa(Thread::Current());
Calin Juravle98893e12015-10-02 21:05:03 +0100366 if (!class_rti.IsValid()) {
367 // He have loaded an unresolved class. Don't bother bounding the type.
368 return;
369 }
370 }
Calin Juravleb3306642015-04-20 18:30:42 +0100371 // We only need to bound the type if we have uses in the relevant block.
372 // So start with null and create the HBoundType lazily, only if it's needed.
373 HBoundType* bound_type = nullptr;
374
Calin Juravleb1498f62015-02-16 13:13:29 +0000375 HInstruction* obj = instanceOf->InputAt(0);
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100376 if (obj->GetReferenceTypeInfo().IsExact() && !obj->IsPhi()) {
377 // This method is being called while doing a fixed-point calculation
378 // over phis. Non-phis instruction whose type is already known do
379 // not need to be bound to another type.
380 // Not that this also prevents replacing `HLoadClass` with a `HBoundType`.
381 // `HCheckCast` and `HInstanceOf` expect a `HLoadClass` as a second
382 // input.
383 return;
384 }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +0100385 DCHECK(!obj->IsLoadClass()) << "We should not replace HLoadClass instructions";
Vladimir Marko46817b82016-03-29 12:21:58 +0100386 const HUseList<HInstruction*>& uses = obj->GetUses();
387 for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
388 HInstruction* user = it->GetUser();
389 size_t index = it->GetIndex();
390 // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput().
391 ++it;
Calin Juravleb1498f62015-02-16 13:13:29 +0000392 if (instanceOfTrueBlock->Dominates(user->GetBlock())) {
Calin Juravleb3306642015-04-20 18:30:42 +0100393 if (bound_type == nullptr) {
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000394 ScopedObjectAccess soa(Thread::Current());
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000395 HInstruction* insert_point = instanceOfTrueBlock->GetFirstInstruction();
396 if (ShouldCreateBoundType(insert_point, obj, class_rti, nullptr, instanceOfTrueBlock)) {
David Brazdilf5552582015-12-27 13:36:12 +0000397 bound_type = new (graph_->GetArena()) HBoundType(obj);
398 bound_type->SetUpperBound(class_rti, /* InstanceOf fails for null. */ false);
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000399 instanceOfTrueBlock->InsertInstructionBefore(bound_type, insert_point);
400 } else {
401 // We already have a bound type on the position we would need to insert
402 // the new one. The existing bound type should dominate all the users
403 // (dchecked) so there's no need to continue.
404 break;
Calin Juravleb3306642015-04-20 18:30:42 +0100405 }
Calin Juravleb3306642015-04-20 18:30:42 +0100406 }
Vladimir Marko46817b82016-03-29 12:21:58 +0100407 user->ReplaceInput(bound_type, index);
Calin Juravleb1498f62015-02-16 13:13:29 +0000408 }
409 }
Calin Juravleacf735c2015-02-12 15:25:22 +0000410}
411
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000412void ReferenceTypePropagation::RTPVisitor::SetClassAsTypeInfo(HInstruction* instr,
413 mirror::Class* klass,
414 bool is_exact) {
Calin Juravle2e768302015-07-28 14:41:11 +0000415 if (instr->IsInvokeStaticOrDirect() && instr->AsInvokeStaticOrDirect()->IsStringInit()) {
416 // Calls to String.<init> are replaced with a StringFactory.
417 if (kIsDebugBuild) {
Nicolas Geoffrayc89715c2015-10-28 12:06:25 +0000418 HInvoke* invoke = instr->AsInvoke();
Calin Juravle2e768302015-07-28 14:41:11 +0000419 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Nicolas Geoffrayc89715c2015-10-28 12:06:25 +0000420 ScopedObjectAccess soa(Thread::Current());
421 StackHandleScope<2> hs(soa.Self());
422 Handle<mirror::DexCache> dex_cache(
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000423 hs.NewHandle(cl->FindDexCache(soa.Self(), invoke->GetDexFile(), false)));
Nicolas Geoffrayc89715c2015-10-28 12:06:25 +0000424 // Use a null loader. We should probably use the compiling method's class loader,
425 // but then we would need to pass it to RTPVisitor just for this debug check. Since
426 // the method is from the String class, the null loader is good enough.
427 Handle<mirror::ClassLoader> loader;
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800428 ArtMethod* method = cl->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Nicolas Geoffrayc89715c2015-10-28 12:06:25 +0000429 invoke->GetDexFile(), invoke->GetDexMethodIndex(), dex_cache, loader, nullptr, kDirect);
Calin Juravle2e768302015-07-28 14:41:11 +0000430 DCHECK(method != nullptr);
431 mirror::Class* declaring_class = method->GetDeclaringClass();
432 DCHECK(declaring_class != nullptr);
433 DCHECK(declaring_class->IsStringClass())
434 << "Expected String class: " << PrettyDescriptor(declaring_class);
435 DCHECK(method->IsConstructor())
436 << "Expected String.<init>: " << PrettyMethod(method);
437 }
438 instr->SetReferenceTypeInfo(
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000439 ReferenceTypeInfo::Create(handle_cache_->GetStringClassHandle(), /* is_exact */ true));
Calin Juravle2e768302015-07-28 14:41:11 +0000440 } else if (klass != nullptr) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100441 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000442 ReferenceTypeInfo::TypeHandle handle = handle_cache_->NewHandle(klass);
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000443 is_exact = is_exact || handle->CannotBeAssignedFromOtherTypes();
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100444 instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(handle, is_exact));
Calin Juravle2e768302015-07-28 14:41:11 +0000445 } else {
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000446 instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100447 }
448}
449
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000450void ReferenceTypePropagation::RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr,
451 uint16_t type_idx,
452 const DexFile& dex_file,
453 bool is_exact) {
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +0100454 DCHECK_EQ(instr->GetType(), Primitive::kPrimNot);
455
Calin Juravleacf735c2015-02-12 15:25:22 +0000456 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000457 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(
458 soa.Self(), dex_file, false);
Calin Juravleacf735c2015-02-12 15:25:22 +0000459 // Get type from dex cache assuming it was populated by the verifier.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100460 SetClassAsTypeInfo(instr, dex_cache->GetResolvedType(type_idx), is_exact);
Calin Juravleacf735c2015-02-12 15:25:22 +0000461}
462
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000463void ReferenceTypePropagation::RTPVisitor::VisitNewInstance(HNewInstance* instr) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100464 UpdateReferenceTypeInfo(instr, instr->GetTypeIndex(), instr->GetDexFile(), /* is_exact */ true);
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +0100465}
466
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000467void ReferenceTypePropagation::RTPVisitor::VisitNewArray(HNewArray* instr) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100468 UpdateReferenceTypeInfo(instr, instr->GetTypeIndex(), instr->GetDexFile(), /* is_exact */ true);
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +0100469}
470
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000471static mirror::Class* GetClassFromDexCache(Thread* self, const DexFile& dex_file, uint16_t type_idx)
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000472 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000473 mirror::DexCache* dex_cache =
474 Runtime::Current()->GetClassLinker()->FindDexCache(self, dex_file, /* allow_failure */ true);
475 if (dex_cache == nullptr) {
476 // Dex cache could not be found. This should only happen during gtests.
477 return nullptr;
478 }
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000479 // Get type from dex cache assuming it was populated by the verifier.
480 return dex_cache->GetResolvedType(type_idx);
481}
482
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000483void ReferenceTypePropagation::RTPVisitor::VisitParameterValue(HParameterValue* instr) {
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000484 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700485 // We check if the existing type is valid: the inliner may have set it.
486 if (instr->GetType() == Primitive::kPrimNot && !instr->GetReferenceTypeInfo().IsValid()) {
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000487 mirror::Class* resolved_class =
488 GetClassFromDexCache(soa.Self(), instr->GetDexFile(), instr->GetTypeIndex());
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000489 SetClassAsTypeInfo(instr, resolved_class, /* is_exact */ false);
Calin Juravle2e768302015-07-28 14:41:11 +0000490 }
491}
492
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000493void ReferenceTypePropagation::RTPVisitor::UpdateFieldAccessTypeInfo(HInstruction* instr,
494 const FieldInfo& info) {
David Brazdild9510df2015-11-04 23:30:22 +0000495 if (instr->GetType() != Primitive::kPrimNot) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100496 return;
497 }
498
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000499 ScopedObjectAccess soa(Thread::Current());
David Brazdild9510df2015-11-04 23:30:22 +0000500 mirror::Class* klass = nullptr;
501
502 // The field index is unknown only during tests.
503 if (info.GetFieldIndex() != kUnknownFieldIndex) {
504 ClassLinker* cl = Runtime::Current()->GetClassLinker();
505 ArtField* field = cl->GetResolvedField(info.GetFieldIndex(), info.GetDexCache().Get());
506 // TODO: There are certain cases where we can't resolve the field.
507 // b/21914925 is open to keep track of a repro case for this issue.
508 if (field != nullptr) {
509 klass = field->GetType<false>();
510 }
511 }
512
Calin Juravle2e768302015-07-28 14:41:11 +0000513 SetClassAsTypeInfo(instr, klass, /* is_exact */ false);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100514}
515
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000516void ReferenceTypePropagation::RTPVisitor::VisitInstanceFieldGet(HInstanceFieldGet* instr) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100517 UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
518}
519
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000520void ReferenceTypePropagation::RTPVisitor::VisitStaticFieldGet(HStaticFieldGet* instr) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100521 UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
522}
523
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000524void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedInstanceFieldGet(
525 HUnresolvedInstanceFieldGet* instr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100526 // TODO: Use descriptor to get the actual type.
527 if (instr->GetFieldType() == Primitive::kPrimNot) {
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000528 instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100529 }
530}
531
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000532void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedStaticFieldGet(
533 HUnresolvedStaticFieldGet* instr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100534 // TODO: Use descriptor to get the actual type.
535 if (instr->GetFieldType() == Primitive::kPrimNot) {
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000536 instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100537 }
538}
539
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000540void ReferenceTypePropagation::RTPVisitor::VisitLoadClass(HLoadClass* instr) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000541 ScopedObjectAccess soa(Thread::Current());
Calin Juravleacf735c2015-02-12 15:25:22 +0000542 // Get type from dex cache assuming it was populated by the verifier.
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000543 mirror::Class* resolved_class =
544 GetClassFromDexCache(soa.Self(), instr->GetDexFile(), instr->GetTypeIndex());
Calin Juravle98893e12015-10-02 21:05:03 +0100545 if (resolved_class != nullptr) {
546 instr->SetLoadedClassRTI(ReferenceTypeInfo::Create(
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000547 handle_cache_->NewHandle(resolved_class), /* is_exact */ true));
Calin Juravle98893e12015-10-02 21:05:03 +0100548 }
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000549 instr->SetReferenceTypeInfo(
550 ReferenceTypeInfo::Create(handle_cache_->GetClassClassHandle(), /* is_exact */ true));
Calin Juravle2e768302015-07-28 14:41:11 +0000551}
552
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000553void ReferenceTypePropagation::RTPVisitor::VisitClinitCheck(HClinitCheck* instr) {
Calin Juravle2e768302015-07-28 14:41:11 +0000554 instr->SetReferenceTypeInfo(instr->InputAt(0)->GetReferenceTypeInfo());
555}
556
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000557void ReferenceTypePropagation::RTPVisitor::VisitLoadString(HLoadString* instr) {
558 instr->SetReferenceTypeInfo(
559 ReferenceTypeInfo::Create(handle_cache_->GetStringClassHandle(), /* is_exact */ true));
Calin Juravle2e768302015-07-28 14:41:11 +0000560}
561
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000562void ReferenceTypePropagation::RTPVisitor::VisitLoadException(HLoadException* instr) {
David Brazdilbbd733e2015-08-18 17:48:17 +0100563 DCHECK(instr->GetBlock()->IsCatchBlock());
564 TryCatchInformation* catch_info = instr->GetBlock()->GetTryCatchInformation();
565
566 if (catch_info->IsCatchAllTypeIndex()) {
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000567 instr->SetReferenceTypeInfo(
568 ReferenceTypeInfo::Create(handle_cache_->GetThrowableClassHandle(), /* is_exact */ false));
David Brazdilbbd733e2015-08-18 17:48:17 +0100569 } else {
570 UpdateReferenceTypeInfo(instr,
571 catch_info->GetCatchTypeIndex(),
572 catch_info->GetCatchDexFile(),
573 /* is_exact */ false);
574 }
575}
576
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000577void ReferenceTypePropagation::RTPVisitor::VisitNullCheck(HNullCheck* instr) {
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000578 ScopedObjectAccess soa(Thread::Current());
Calin Juravle2e768302015-07-28 14:41:11 +0000579 ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
David Brazdilfe860702015-12-02 09:06:57 +0000580 if (parent_rti.IsValid()) {
581 instr->SetReferenceTypeInfo(parent_rti);
582 }
Calin Juravle2e768302015-07-28 14:41:11 +0000583}
584
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000585void ReferenceTypePropagation::RTPVisitor::VisitBoundType(HBoundType* instr) {
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000586 ScopedObjectAccess soa(Thread::Current());
587
David Brazdilf5552582015-12-27 13:36:12 +0000588 ReferenceTypeInfo class_rti = instr->GetUpperBound();
589 if (class_rti.IsValid()) {
590 // Narrow the type as much as possible.
591 HInstruction* obj = instr->InputAt(0);
592 ReferenceTypeInfo obj_rti = obj->GetReferenceTypeInfo();
593 if (class_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes()) {
594 instr->SetReferenceTypeInfo(
595 ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ true));
David Brazdil744a1c62015-12-28 10:53:34 +0000596 } else if (obj_rti.IsValid()) {
597 if (class_rti.IsSupertypeOf(obj_rti)) {
598 // Object type is more specific.
599 instr->SetReferenceTypeInfo(obj_rti);
600 } else {
601 // Upper bound is more specific.
602 instr->SetReferenceTypeInfo(
603 ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false));
604 }
David Brazdilf5552582015-12-27 13:36:12 +0000605 } else {
David Brazdil744a1c62015-12-28 10:53:34 +0000606 // Object not typed yet. Leave BoundType untyped for now rather than
607 // assign the type conservatively.
David Brazdilf5552582015-12-27 13:36:12 +0000608 }
609 instr->SetCanBeNull(obj->CanBeNull() && instr->GetUpperCanBeNull());
610 } else {
611 // The owner of the BoundType was already visited. If the class is unresolved,
612 // the BoundType should have been removed from the data flow and this method
613 // should remove it from the graph.
614 DCHECK(!instr->HasUses());
615 instr->GetBlock()->RemoveInstruction(instr);
616 }
617}
618
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000619void ReferenceTypePropagation::RTPVisitor::VisitCheckCast(HCheckCast* check_cast) {
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000620 ScopedObjectAccess soa(Thread::Current());
621
Calin Juravle98893e12015-10-02 21:05:03 +0100622 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
623 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
David Brazdilf5552582015-12-27 13:36:12 +0000624 HBoundType* bound_type = check_cast->GetNext()->AsBoundType();
625 if (bound_type == nullptr || bound_type->GetUpperBound().IsValid()) {
626 // The next instruction is not an uninitialized BoundType. This must be
627 // an RTP pass after SsaBuilder and we do not need to do anything.
628 return;
Calin Juravle98893e12015-10-02 21:05:03 +0100629 }
David Brazdilf5552582015-12-27 13:36:12 +0000630 DCHECK_EQ(bound_type->InputAt(0), check_cast->InputAt(0));
631
632 if (class_rti.IsValid()) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000633 DCHECK(is_first_run_);
David Brazdilf5552582015-12-27 13:36:12 +0000634 // This is the first run of RTP and class is resolved.
635 bound_type->SetUpperBound(class_rti, /* CheckCast succeeds for nulls. */ true);
636 } else {
637 // This is the first run of RTP and class is unresolved. Remove the binding.
638 // The instruction itself is removed in VisitBoundType so as to not
639 // invalidate HInstructionIterator.
640 bound_type->ReplaceWith(bound_type->InputAt(0));
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000641 }
642}
643
Calin Juravleb1498f62015-02-16 13:13:29 +0000644void ReferenceTypePropagation::VisitPhi(HPhi* phi) {
David Brazdild9510df2015-11-04 23:30:22 +0000645 if (phi->IsDead() || phi->GetType() != Primitive::kPrimNot) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000646 return;
Calin Juravleacf735c2015-02-12 15:25:22 +0000647 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000648
649 if (phi->GetBlock()->IsLoopHeader()) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000650 if (!is_first_run_ && graph_->IsCompilingOsr()) {
651 // Don't update the type of a loop phi when compiling OSR: we may have done
652 // speculative optimizations dominating that phi, that do not hold at the
653 // point the interpreter jumps to that loop header.
654 return;
655 }
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000656 ScopedObjectAccess soa(Thread::Current());
Calin Juravleb1498f62015-02-16 13:13:29 +0000657 // Set the initial type for the phi. Use the non back edge input for reaching
658 // a fixed point faster.
David Brazdilfe860702015-12-02 09:06:57 +0000659 HInstruction* first_input = phi->InputAt(0);
660 ReferenceTypeInfo first_input_rti = first_input->GetReferenceTypeInfo();
661 if (first_input_rti.IsValid() && !first_input->IsNullConstant()) {
662 phi->SetCanBeNull(first_input->CanBeNull());
663 phi->SetReferenceTypeInfo(first_input_rti);
664 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000665 AddToWorklist(phi);
Calin Juravle10e244f2015-01-26 18:54:32 +0000666 } else {
Calin Juravleb1498f62015-02-16 13:13:29 +0000667 // Eagerly compute the type of the phi, for quicker convergence. Note
668 // that we don't need to add users to the worklist because we are
669 // doing a reverse post-order visit, therefore either the phi users are
670 // non-loop phi and will be visited later in the visit, or are loop-phis,
671 // and they are already in the work list.
672 UpdateNullability(phi);
673 UpdateReferenceTypeInfo(phi);
674 }
675}
676
677ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a,
678 const ReferenceTypeInfo& b) {
Calin Juravle2e768302015-07-28 14:41:11 +0000679 if (!b.IsValid()) {
680 return a;
681 }
682 if (!a.IsValid()) {
683 return b;
Calin Juravle20e60712015-07-01 18:41:04 +0100684 }
685
Calin Juravle2e768302015-07-28 14:41:11 +0000686 bool is_exact = a.IsExact() && b.IsExact();
Calin Juravle52503d82015-11-11 16:58:31 +0000687 ReferenceTypeInfo::TypeHandle result_type_handle;
688 ReferenceTypeInfo::TypeHandle a_type_handle = a.GetTypeHandle();
689 ReferenceTypeInfo::TypeHandle b_type_handle = b.GetTypeHandle();
690 bool a_is_interface = a_type_handle->IsInterface();
691 bool b_is_interface = b_type_handle->IsInterface();
Calin Juravle2e768302015-07-28 14:41:11 +0000692
693 if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) {
Calin Juravle52503d82015-11-11 16:58:31 +0000694 result_type_handle = a_type_handle;
Calin Juravle2e768302015-07-28 14:41:11 +0000695 } else if (a.IsSupertypeOf(b)) {
Calin Juravle52503d82015-11-11 16:58:31 +0000696 result_type_handle = a_type_handle;
Calin Juravle2e768302015-07-28 14:41:11 +0000697 is_exact = false;
698 } else if (b.IsSupertypeOf(a)) {
Calin Juravle52503d82015-11-11 16:58:31 +0000699 result_type_handle = b_type_handle;
700 is_exact = false;
701 } else if (!a_is_interface && !b_is_interface) {
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000702 result_type_handle =
703 handle_cache_.NewHandle(a_type_handle->GetCommonSuperClass(b_type_handle));
Calin Juravle2e768302015-07-28 14:41:11 +0000704 is_exact = false;
705 } else {
Calin Juravle52503d82015-11-11 16:58:31 +0000706 // This can happen if:
707 // - both types are interfaces. TODO(calin): implement
708 // - one is an interface, the other a class, and the type does not implement the interface
709 // e.g:
710 // void foo(Interface i, boolean cond) {
711 // Object o = cond ? i : new Object();
712 // }
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000713 result_type_handle = handle_cache_.GetObjectClassHandle();
Calin Juravle2e768302015-07-28 14:41:11 +0000714 is_exact = false;
715 }
716
Calin Juravle52503d82015-11-11 16:58:31 +0000717 return ReferenceTypeInfo::Create(result_type_handle, is_exact);
Calin Juravle2e768302015-07-28 14:41:11 +0000718}
719
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000720void ReferenceTypePropagation::UpdateArrayGet(HArrayGet* instr, HandleCache* handle_cache) {
Calin Juravle2e768302015-07-28 14:41:11 +0000721 DCHECK_EQ(Primitive::kPrimNot, instr->GetType());
722
723 ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
David Brazdilfe860702015-12-02 09:06:57 +0000724 if (!parent_rti.IsValid()) {
725 return;
726 }
Calin Juravle2e768302015-07-28 14:41:11 +0000727
728 Handle<mirror::Class> handle = parent_rti.GetTypeHandle();
729 if (handle->IsObjectArrayClass()) {
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000730 ReferenceTypeInfo::TypeHandle component_handle =
731 handle_cache->NewHandle(handle->GetComponentType());
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000732 bool is_exact = component_handle->CannotBeAssignedFromOtherTypes();
733 instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(component_handle, is_exact));
Calin Juravle2e768302015-07-28 14:41:11 +0000734 } else {
735 // We don't know what the parent actually is, so we fallback to object.
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000736 instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
Calin Juravle2e768302015-07-28 14:41:11 +0000737 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000738}
739
740bool ReferenceTypePropagation::UpdateReferenceTypeInfo(HInstruction* instr) {
741 ScopedObjectAccess soa(Thread::Current());
742
743 ReferenceTypeInfo previous_rti = instr->GetReferenceTypeInfo();
744 if (instr->IsBoundType()) {
745 UpdateBoundType(instr->AsBoundType());
746 } else if (instr->IsPhi()) {
747 UpdatePhi(instr->AsPhi());
Calin Juravle2e768302015-07-28 14:41:11 +0000748 } else if (instr->IsNullCheck()) {
749 ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
750 if (parent_rti.IsValid()) {
751 instr->SetReferenceTypeInfo(parent_rti);
752 }
753 } else if (instr->IsArrayGet()) {
David Brazdilfe860702015-12-02 09:06:57 +0000754 // TODO: consider if it's worth "looking back" and binding the input object
Calin Juravle2e768302015-07-28 14:41:11 +0000755 // to an array type.
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000756 UpdateArrayGet(instr->AsArrayGet(), &handle_cache_);
Calin Juravleb1498f62015-02-16 13:13:29 +0000757 } else {
758 LOG(FATAL) << "Invalid instruction (should not get here)";
759 }
760
761 return !previous_rti.IsEqual(instr->GetReferenceTypeInfo());
762}
763
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000764void ReferenceTypePropagation::RTPVisitor::VisitInvoke(HInvoke* instr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100765 if (instr->GetType() != Primitive::kPrimNot) {
766 return;
767 }
768
769 ScopedObjectAccess soa(Thread::Current());
770 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Nicolas Geoffray34d9b042016-04-15 17:01:11 +0000771 mirror::DexCache* dex_cache = cl->FindDexCache(soa.Self(), instr->GetDexFile());
Vladimir Marko05792b92015-08-03 11:56:49 +0100772 size_t pointer_size = cl->GetImagePointerSize();
773 ArtMethod* method = dex_cache->GetResolvedMethod(instr->GetDexMethodIndex(), pointer_size);
774 mirror::Class* klass = (method == nullptr) ? nullptr : method->GetReturnType(false, pointer_size);
Calin Juravle2e768302015-07-28 14:41:11 +0000775 SetClassAsTypeInfo(instr, klass, /* is_exact */ false);
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100776}
777
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000778void ReferenceTypePropagation::RTPVisitor::VisitArrayGet(HArrayGet* instr) {
Guillaume "Vermeille" Sanchez72a5eb52015-06-02 17:39:45 +0100779 if (instr->GetType() != Primitive::kPrimNot) {
780 return;
781 }
David Brazdilfe860702015-12-02 09:06:57 +0000782
Guillaume "Vermeille" Sanchez72a5eb52015-06-02 17:39:45 +0100783 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000784 UpdateArrayGet(instr, handle_cache_);
Calin Juravle2e768302015-07-28 14:41:11 +0000785 if (!instr->GetReferenceTypeInfo().IsValid()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100786 worklist_->push_back(instr);
Guillaume "Vermeille" Sanchez72a5eb52015-06-02 17:39:45 +0100787 }
788}
789
Calin Juravleb1498f62015-02-16 13:13:29 +0000790void ReferenceTypePropagation::UpdateBoundType(HBoundType* instr) {
791 ReferenceTypeInfo new_rti = instr->InputAt(0)->GetReferenceTypeInfo();
Calin Juravle2e768302015-07-28 14:41:11 +0000792 if (!new_rti.IsValid()) {
793 return; // No new info yet.
794 }
795
796 // Make sure that we don't go over the bounded type.
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000797 ReferenceTypeInfo upper_bound_rti = instr->GetUpperBound();
798 if (!upper_bound_rti.IsSupertypeOf(new_rti)) {
799 new_rti = upper_bound_rti;
Calin Juravleb1498f62015-02-16 13:13:29 +0000800 }
801 instr->SetReferenceTypeInfo(new_rti);
802}
803
Calin Juravle617bd922015-11-11 14:59:46 +0000804// NullConstant inputs are ignored during merging as they do not provide any useful information.
805// If all the inputs are NullConstants then the type of the phi will be set to Object.
Calin Juravleb1498f62015-02-16 13:13:29 +0000806void ReferenceTypePropagation::UpdatePhi(HPhi* instr) {
David Brazdild9510df2015-11-04 23:30:22 +0000807 DCHECK(instr->IsLive());
808
Calin Juravle617bd922015-11-11 14:59:46 +0000809 size_t input_count = instr->InputCount();
810 size_t first_input_index_not_null = 0;
811 while (first_input_index_not_null < input_count &&
812 instr->InputAt(first_input_index_not_null)->IsNullConstant()) {
813 first_input_index_not_null++;
814 }
815 if (first_input_index_not_null == input_count) {
816 // All inputs are NullConstants, set the type to object.
817 // This may happen in the presence of inlining.
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000818 instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
Calin Juravle617bd922015-11-11 14:59:46 +0000819 return;
820 }
821
822 ReferenceTypeInfo new_rti = instr->InputAt(first_input_index_not_null)->GetReferenceTypeInfo();
823
Calin Juravle2e768302015-07-28 14:41:11 +0000824 if (new_rti.IsValid() && new_rti.IsObjectClass() && !new_rti.IsExact()) {
825 // Early return if we are Object and inexact.
Calin Juravleb1498f62015-02-16 13:13:29 +0000826 instr->SetReferenceTypeInfo(new_rti);
827 return;
828 }
Calin Juravle617bd922015-11-11 14:59:46 +0000829
830 for (size_t i = first_input_index_not_null + 1; i < input_count; i++) {
831 if (instr->InputAt(i)->IsNullConstant()) {
832 continue;
833 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000834 new_rti = MergeTypes(new_rti, instr->InputAt(i)->GetReferenceTypeInfo());
Calin Juravle2e768302015-07-28 14:41:11 +0000835 if (new_rti.IsValid() && new_rti.IsObjectClass()) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000836 if (!new_rti.IsExact()) {
837 break;
838 } else {
839 continue;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000840 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000841 }
842 }
David Brazdilfe860702015-12-02 09:06:57 +0000843
844 if (new_rti.IsValid()) {
845 instr->SetReferenceTypeInfo(new_rti);
846 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000847}
848
849// Re-computes and updates the nullability of the instruction. Returns whether or
850// not the nullability was changed.
851bool ReferenceTypePropagation::UpdateNullability(HInstruction* instr) {
David Brazdild9510df2015-11-04 23:30:22 +0000852 DCHECK((instr->IsPhi() && instr->AsPhi()->IsLive())
Calin Juravle2e768302015-07-28 14:41:11 +0000853 || instr->IsBoundType()
854 || instr->IsNullCheck()
855 || instr->IsArrayGet());
856
857 if (!instr->IsPhi() && !instr->IsBoundType()) {
858 return false;
859 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000860
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000861 bool existing_can_be_null = instr->CanBeNull();
862 if (instr->IsPhi()) {
863 HPhi* phi = instr->AsPhi();
864 bool new_can_be_null = false;
865 for (size_t i = 0; i < phi->InputCount(); i++) {
866 if (phi->InputAt(i)->CanBeNull()) {
867 new_can_be_null = true;
868 break;
869 }
870 }
871 phi->SetCanBeNull(new_can_be_null);
872 } else if (instr->IsBoundType()) {
873 HBoundType* bound_type = instr->AsBoundType();
874 bound_type->SetCanBeNull(instr->InputAt(0)->CanBeNull() && bound_type->GetUpperCanBeNull());
Calin Juravleb1498f62015-02-16 13:13:29 +0000875 }
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000876 return existing_can_be_null != instr->CanBeNull();
Calin Juravle10e244f2015-01-26 18:54:32 +0000877}
878
879void ReferenceTypePropagation::ProcessWorklist() {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100880 while (!worklist_.empty()) {
881 HInstruction* instruction = worklist_.back();
882 worklist_.pop_back();
Calin Juravle12617592015-10-16 16:28:46 +0100883 bool updated_nullability = UpdateNullability(instruction);
884 bool updated_reference_type = UpdateReferenceTypeInfo(instruction);
885 if (updated_nullability || updated_reference_type) {
Calin Juravle10e244f2015-01-26 18:54:32 +0000886 AddDependentInstructionsToWorklist(instruction);
887 }
888 }
889}
890
Calin Juravleb1498f62015-02-16 13:13:29 +0000891void ReferenceTypePropagation::AddToWorklist(HInstruction* instruction) {
Calin Juravle2e768302015-07-28 14:41:11 +0000892 DCHECK_EQ(instruction->GetType(), Primitive::kPrimNot)
893 << instruction->DebugName() << ":" << instruction->GetType();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100894 worklist_.push_back(instruction);
Calin Juravle10e244f2015-01-26 18:54:32 +0000895}
896
Calin Juravleb1498f62015-02-16 13:13:29 +0000897void ReferenceTypePropagation::AddDependentInstructionsToWorklist(HInstruction* instruction) {
Vladimir Marko46817b82016-03-29 12:21:58 +0100898 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
899 HInstruction* user = use.GetUser();
David Brazdild9510df2015-11-04 23:30:22 +0000900 if ((user->IsPhi() && user->AsPhi()->IsLive())
Calin Juravle2e768302015-07-28 14:41:11 +0000901 || user->IsBoundType()
902 || user->IsNullCheck()
903 || (user->IsArrayGet() && (user->GetType() == Primitive::kPrimNot))) {
Calin Juravlebeba9302015-07-08 15:57:18 +0000904 AddToWorklist(user);
Calin Juravle10e244f2015-01-26 18:54:32 +0000905 }
906 }
907}
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000908
Calin Juravle10e244f2015-01-26 18:54:32 +0000909} // namespace art