blob: f5064c3057c87384bad447594892b787a8a4ac40 [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
Andreas Gampea1d2f952017-04-20 22:53:58 -070019#include "art_field-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080020#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "class_linker-inl.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000023#include "mirror/class-inl.h"
24#include "mirror/dex_cache.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026
Calin Juravle10e244f2015-01-26 18:54:32 +000027namespace art {
28
Vladimir Markocd556b02017-02-03 11:47:34 +000029static inline ObjPtr<mirror::DexCache> FindDexCacheWithHint(
30 Thread* self, const DexFile& dex_file, Handle<mirror::DexCache> hint_dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070031 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko456307a2016-04-19 14:12:13 +000032 if (LIKELY(hint_dex_cache->GetDexFile() == &dex_file)) {
33 return hint_dex_cache.Get();
34 } else {
35 return Runtime::Current()->GetClassLinker()->FindDexCache(self, dex_file);
36 }
37}
38
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070039static inline ReferenceTypeInfo::TypeHandle GetRootHandle(VariableSizedHandleScope* handles,
Vladimir Marko7d1fbf32016-01-26 15:01:12 +000040 ClassLinker::ClassRoot class_root,
41 ReferenceTypeInfo::TypeHandle* cache) {
42 if (!ReferenceTypeInfo::IsValidHandle(*cache)) {
43 // Mutator lock is required for NewHandle.
44 ClassLinker* linker = Runtime::Current()->GetClassLinker();
45 ScopedObjectAccess soa(Thread::Current());
46 *cache = handles->NewHandle(linker->GetClassRoot(class_root));
47 }
48 return *cache;
49}
50
51ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetObjectClassHandle() {
52 return GetRootHandle(handles_, ClassLinker::kJavaLangObject, &object_class_handle_);
53}
54
55ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetClassClassHandle() {
56 return GetRootHandle(handles_, ClassLinker::kJavaLangClass, &class_class_handle_);
57}
58
59ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetStringClassHandle() {
60 return GetRootHandle(handles_, ClassLinker::kJavaLangString, &string_class_handle_);
61}
62
63ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetThrowableClassHandle() {
64 return GetRootHandle(handles_, ClassLinker::kJavaLangThrowable, &throwable_class_handle_);
65}
66
67class ReferenceTypePropagation::RTPVisitor : public HGraphDelegateVisitor {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010068 public:
Calin Juravle2e768302015-07-28 14:41:11 +000069 RTPVisitor(HGraph* graph,
Vladimir Marko8d6768d2017-03-14 10:13:21 +000070 Handle<mirror::ClassLoader> class_loader,
Vladimir Marko456307a2016-04-19 14:12:13 +000071 Handle<mirror::DexCache> hint_dex_cache,
Vladimir Marko7d1fbf32016-01-26 15:01:12 +000072 HandleCache* handle_cache,
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +000073 ArenaVector<HInstruction*>* worklist,
74 bool is_first_run)
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010075 : HGraphDelegateVisitor(graph),
Vladimir Marko8d6768d2017-03-14 10:13:21 +000076 class_loader_(class_loader),
Vladimir Marko456307a2016-04-19 14:12:13 +000077 hint_dex_cache_(hint_dex_cache),
Vladimir Marko7d1fbf32016-01-26 15:01:12 +000078 handle_cache_(handle_cache),
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +000079 worklist_(worklist),
80 is_first_run_(is_first_run) {}
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010081
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +000082 void VisitDeoptimize(HDeoptimize* deopt) OVERRIDE;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010083 void VisitNewInstance(HNewInstance* new_instance) OVERRIDE;
84 void VisitLoadClass(HLoadClass* load_class) OVERRIDE;
Calin Juravle2e768302015-07-28 14:41:11 +000085 void VisitClinitCheck(HClinitCheck* clinit_check) OVERRIDE;
86 void VisitLoadString(HLoadString* instr) OVERRIDE;
David Brazdilbbd733e2015-08-18 17:48:17 +010087 void VisitLoadException(HLoadException* instr) OVERRIDE;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010088 void VisitNewArray(HNewArray* instr) OVERRIDE;
Calin Juravle2e768302015-07-28 14:41:11 +000089 void VisitParameterValue(HParameterValue* instr) OVERRIDE;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010090 void UpdateFieldAccessTypeInfo(HInstruction* instr, const FieldInfo& info);
Mathieu Chartier3398c782016-09-30 10:27:43 -070091 void SetClassAsTypeInfo(HInstruction* instr, ObjPtr<mirror::Class> klass, bool is_exact)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 REQUIRES_SHARED(Locks::mutator_lock_);
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010093 void VisitInstanceFieldGet(HInstanceFieldGet* instr) OVERRIDE;
94 void VisitStaticFieldGet(HStaticFieldGet* instr) OVERRIDE;
Calin Juravlee460d1d2015-09-29 04:52:17 +010095 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* instr) OVERRIDE;
96 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* instr) OVERRIDE;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +010097 void VisitInvoke(HInvoke* instr) OVERRIDE;
Guillaume "Vermeille" Sanchez72a5eb52015-06-02 17:39:45 +010098 void VisitArrayGet(HArrayGet* instr) OVERRIDE;
Calin Juravlea5ae3c32015-07-28 14:40:50 +000099 void VisitCheckCast(HCheckCast* instr) OVERRIDE;
David Brazdilf5552582015-12-27 13:36:12 +0000100 void VisitBoundType(HBoundType* instr) OVERRIDE;
Calin Juravle2e768302015-07-28 14:41:11 +0000101 void VisitNullCheck(HNullCheck* instr) OVERRIDE;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100102 void UpdateReferenceTypeInfo(HInstruction* instr,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800103 dex::TypeIndex type_idx,
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100104 const DexFile& dex_file,
105 bool is_exact);
106
107 private:
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000108 Handle<mirror::ClassLoader> class_loader_;
Vladimir Marko456307a2016-04-19 14:12:13 +0000109 Handle<mirror::DexCache> hint_dex_cache_;
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000110 HandleCache* handle_cache_;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100111 ArenaVector<HInstruction*>* worklist_;
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000112 const bool is_first_run_;
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100113};
114
Calin Juravle2e768302015-07-28 14:41:11 +0000115ReferenceTypePropagation::ReferenceTypePropagation(HGraph* graph,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000116 Handle<mirror::ClassLoader> class_loader,
Vladimir Marko456307a2016-04-19 14:12:13 +0000117 Handle<mirror::DexCache> hint_dex_cache,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700118 VariableSizedHandleScope* handles,
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000119 bool is_first_run,
Calin Juravle2e768302015-07-28 14:41:11 +0000120 const char* name)
121 : HOptimization(graph, name),
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000122 class_loader_(class_loader),
Vladimir Marko456307a2016-04-19 14:12:13 +0000123 hint_dex_cache_(hint_dex_cache),
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000124 handle_cache_(handles),
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000125 worklist_(graph->GetArena()->Adapter(kArenaAllocReferenceTypePropagation)),
126 is_first_run_(is_first_run) {
Calin Juravle2e768302015-07-28 14:41:11 +0000127}
128
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000129void ReferenceTypePropagation::ValidateTypes() {
130 // TODO: move this to the graph checker.
Calin Juravle2e768302015-07-28 14:41:11 +0000131 if (kIsDebugBuild) {
Calin Juravle2e768302015-07-28 14:41:11 +0000132 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100133 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
Calin Juravle2e768302015-07-28 14:41:11 +0000134 for (HInstructionIterator iti(block->GetInstructions()); !iti.Done(); iti.Advance()) {
135 HInstruction* instr = iti.Current();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 if (instr->GetType() == DataType::Type::kReference) {
Calin Juravle2e768302015-07-28 14:41:11 +0000137 DCHECK(instr->GetReferenceTypeInfo().IsValid())
138 << "Invalid RTI for instruction: " << instr->DebugName();
139 if (instr->IsBoundType()) {
140 DCHECK(instr->AsBoundType()->GetUpperBound().IsValid());
141 } else if (instr->IsLoadClass()) {
Calin Juravle98893e12015-10-02 21:05:03 +0100142 HLoadClass* cls = instr->AsLoadClass();
143 DCHECK(cls->GetReferenceTypeInfo().IsExact());
144 DCHECK(!cls->GetLoadedClassRTI().IsValid() || cls->GetLoadedClassRTI().IsExact());
Calin Juravle2e768302015-07-28 14:41:11 +0000145 } else if (instr->IsNullCheck()) {
146 DCHECK(instr->GetReferenceTypeInfo().IsEqual(instr->InputAt(0)->GetReferenceTypeInfo()))
147 << "NullCheck " << instr->GetReferenceTypeInfo()
148 << "Input(0) " << instr->InputAt(0)->GetReferenceTypeInfo();
149 }
150 }
151 }
152 }
153 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000154}
155
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000156void ReferenceTypePropagation::Visit(HInstruction* instruction) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000157 RTPVisitor visitor(graph_,
158 class_loader_,
159 hint_dex_cache_,
160 &handle_cache_,
161 &worklist_,
162 is_first_run_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000163 instruction->Accept(&visitor);
164}
165
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000166// Check if we should create a bound type for the given object at the specified
167// position. Because of inlining and the fact we run RTP more than once and we
168// might have a HBoundType already. If we do, we should not create a new one.
169// In this case we also assert that there are no other uses of the object (except
170// the bound type) dominated by the specified dominator_instr or dominator_block.
171static bool ShouldCreateBoundType(HInstruction* position,
172 HInstruction* obj,
173 ReferenceTypeInfo upper_bound,
174 HInstruction* dominator_instr,
175 HBasicBlock* dominator_block)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700176 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000177 // If the position where we should insert the bound type is not already a
178 // a bound type then we need to create one.
179 if (position == nullptr || !position->IsBoundType()) {
180 return true;
181 }
182
183 HBoundType* existing_bound_type = position->AsBoundType();
184 if (existing_bound_type->GetUpperBound().IsSupertypeOf(upper_bound)) {
185 if (kIsDebugBuild) {
186 // Check that the existing HBoundType dominates all the uses.
Vladimir Marko46817b82016-03-29 12:21:58 +0100187 for (const HUseListNode<HInstruction*>& use : obj->GetUses()) {
188 HInstruction* user = use.GetUser();
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000189 if (dominator_instr != nullptr) {
190 DCHECK(!dominator_instr->StrictlyDominates(user)
191 || user == existing_bound_type
192 || existing_bound_type->StrictlyDominates(user));
193 } else if (dominator_block != nullptr) {
194 DCHECK(!dominator_block->Dominates(user->GetBlock())
195 || user == existing_bound_type
196 || existing_bound_type->StrictlyDominates(user));
197 }
198 }
199 }
200 } else {
201 // TODO: if the current bound type is a refinement we could update the
202 // existing_bound_type with the a new upper limit. However, we also need to
203 // update its users and have access to the work list.
204 }
205 return false;
206}
207
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000208// Helper method to bound the type of `receiver` for all instructions dominated
209// by `start_block`, or `start_instruction` if `start_block` is null. The new
210// bound type will have its upper bound be `class_rti`.
211static void BoundTypeIn(HInstruction* receiver,
212 HBasicBlock* start_block,
213 HInstruction* start_instruction,
214 const ReferenceTypeInfo& class_rti) {
215 // We only need to bound the type if we have uses in the relevant block.
216 // So start with null and create the HBoundType lazily, only if it's needed.
217 HBoundType* bound_type = nullptr;
218 DCHECK(!receiver->IsLoadClass()) << "We should not replace HLoadClass instructions";
219 const HUseList<HInstruction*>& uses = receiver->GetUses();
220 for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
221 HInstruction* user = it->GetUser();
222 size_t index = it->GetIndex();
223 // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput().
224 ++it;
225 bool dominates = (start_instruction != nullptr)
226 ? start_instruction->StrictlyDominates(user)
227 : start_block->Dominates(user->GetBlock());
228 if (!dominates) {
229 continue;
230 }
231 if (bound_type == nullptr) {
232 ScopedObjectAccess soa(Thread::Current());
233 HInstruction* insert_point = (start_instruction != nullptr)
234 ? start_instruction->GetNext()
235 : start_block->GetFirstInstruction();
236 if (ShouldCreateBoundType(
237 insert_point, receiver, class_rti, start_instruction, start_block)) {
238 bound_type = new (receiver->GetBlock()->GetGraph()->GetArena()) HBoundType(receiver);
239 bound_type->SetUpperBound(class_rti, /* bound_can_be_null */ false);
240 start_block->InsertInstructionBefore(bound_type, insert_point);
241 // To comply with the RTP algorithm, don't type the bound type just yet, it will
242 // be handled in RTPVisitor::VisitBoundType.
243 } else {
244 // We already have a bound type on the position we would need to insert
245 // the new one. The existing bound type should dominate all the users
246 // (dchecked) so there's no need to continue.
247 break;
248 }
249 }
250 user->ReplaceInput(bound_type, index);
251 }
252 // If the receiver is a null check, also bound the type of the actual
253 // receiver.
254 if (receiver->IsNullCheck()) {
255 BoundTypeIn(receiver->InputAt(0), start_block, start_instruction, class_rti);
256 }
257}
258
259// Recognize the patterns:
260// if (obj.shadow$_klass_ == Foo.class) ...
261// deoptimize if (obj.shadow$_klass_ == Foo.class)
262static void BoundTypeForClassCheck(HInstruction* check) {
263 if (!check->IsIf() && !check->IsDeoptimize()) {
264 return;
265 }
266 HInstruction* compare = check->InputAt(0);
267 if (!compare->IsEqual() && !compare->IsNotEqual()) {
268 return;
269 }
270 HInstruction* input_one = compare->InputAt(0);
271 HInstruction* input_two = compare->InputAt(1);
272 HLoadClass* load_class = input_one->IsLoadClass()
273 ? input_one->AsLoadClass()
274 : input_two->AsLoadClass();
275 if (load_class == nullptr) {
276 return;
277 }
278
279 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
280 if (!class_rti.IsValid()) {
281 // We have loaded an unresolved class. Don't bother bounding the type.
282 return;
283 }
284
285 HInstanceFieldGet* field_get = (load_class == input_one)
286 ? input_two->AsInstanceFieldGet()
287 : input_one->AsInstanceFieldGet();
288 if (field_get == nullptr) {
289 return;
290 }
291 HInstruction* receiver = field_get->InputAt(0);
292 ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo();
293 if (receiver_type.IsExact()) {
294 // If we already know the receiver type, don't bother updating its users.
295 return;
296 }
297
298 {
299 ScopedObjectAccess soa(Thread::Current());
300 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
301 ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0);
302 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
303 if (field_get->GetFieldInfo().GetField() != field) {
304 return;
305 }
306 }
307
308 if (check->IsIf()) {
Nicolas Geoffraycd9f8572017-01-16 15:08:56 +0000309 HBasicBlock* trueBlock = compare->IsEqual()
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000310 ? check->AsIf()->IfTrueSuccessor()
311 : check->AsIf()->IfFalseSuccessor();
312 BoundTypeIn(receiver, trueBlock, /* start_instruction */ nullptr, class_rti);
313 } else {
314 DCHECK(check->IsDeoptimize());
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +0000315 if (compare->IsEqual() && check->AsDeoptimize()->GuardsAnInput()) {
316 check->SetReferenceTypeInfo(class_rti);
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000317 }
318 }
319}
320
321void ReferenceTypePropagation::Run() {
322 worklist_.reserve(kDefaultWorklistSize);
323
324 // To properly propagate type info we need to visit in the dominator-based order.
325 // Reverse post order guarantees a node's dominators are visited first.
326 // We take advantage of this order in `VisitBasicBlock`.
327 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
328 VisitBasicBlock(block);
329 }
330
331 ProcessWorklist();
332 ValidateTypes();
333}
334
335void ReferenceTypePropagation::VisitBasicBlock(HBasicBlock* block) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000336 RTPVisitor visitor(graph_,
337 class_loader_,
338 hint_dex_cache_,
339 &handle_cache_,
340 &worklist_,
341 is_first_run_);
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000342 // Handle Phis first as there might be instructions in the same block who depend on them.
343 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
344 VisitPhi(it.Current()->AsPhi());
345 }
346
347 // Handle instructions. Since RTP may add HBoundType instructions just after the
348 // last visited instruction, use `HInstructionIteratorHandleChanges` iterator.
349 for (HInstructionIteratorHandleChanges it(block->GetInstructions()); !it.Done(); it.Advance()) {
350 HInstruction* instr = it.Current();
351 instr->Accept(&visitor);
352 }
353
354 // Add extra nodes to bound types.
355 BoundTypeForIfNotNull(block);
356 BoundTypeForIfInstanceOf(block);
357 BoundTypeForClassCheck(block->GetLastInstruction());
358}
359
Calin Juravle61d544b2015-02-23 16:46:57 +0000360void ReferenceTypePropagation::BoundTypeForIfNotNull(HBasicBlock* block) {
Calin Juravleb3306642015-04-20 18:30:42 +0100361 HIf* ifInstruction = block->GetLastInstruction()->AsIf();
362 if (ifInstruction == nullptr) {
Calin Juravle61d544b2015-02-23 16:46:57 +0000363 return;
364 }
Calin Juravleb3306642015-04-20 18:30:42 +0100365 HInstruction* ifInput = ifInstruction->InputAt(0);
Calin Juravle61d544b2015-02-23 16:46:57 +0000366 if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) {
367 return;
368 }
369 HInstruction* input0 = ifInput->InputAt(0);
370 HInstruction* input1 = ifInput->InputAt(1);
Calin Juravleedad8ad2015-04-23 14:34:33 +0100371 HInstruction* obj = nullptr;
Calin Juravle61d544b2015-02-23 16:46:57 +0000372
Calin Juravleedad8ad2015-04-23 14:34:33 +0100373 if (input1->IsNullConstant()) {
Calin Juravle61d544b2015-02-23 16:46:57 +0000374 obj = input0;
Calin Juravleedad8ad2015-04-23 14:34:33 +0100375 } else if (input0->IsNullConstant()) {
Calin Juravle61d544b2015-02-23 16:46:57 +0000376 obj = input1;
377 } else {
378 return;
379 }
380
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +0100381 if (!obj->CanBeNull() || obj->IsNullConstant()) {
382 // Null check is dead code and will be removed by DCE.
383 return;
384 }
385 DCHECK(!obj->IsLoadClass()) << "We should not replace HLoadClass instructions";
386
Calin Juravleb3306642015-04-20 18:30:42 +0100387 // We only need to bound the type if we have uses in the relevant block.
388 // So start with null and create the HBoundType lazily, only if it's needed.
Calin Juravle61d544b2015-02-23 16:46:57 +0000389 HBasicBlock* notNullBlock = ifInput->IsNotEqual()
Calin Juravleb3306642015-04-20 18:30:42 +0100390 ? ifInstruction->IfTrueSuccessor()
391 : ifInstruction->IfFalseSuccessor();
392
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000393 ReferenceTypeInfo object_rti = ReferenceTypeInfo::Create(
394 handle_cache_.GetObjectClassHandle(), /* is_exact */ false);
395
396 BoundTypeIn(obj, notNullBlock, /* start_instruction */ nullptr, object_rti);
Calin Juravle61d544b2015-02-23 16:46:57 +0000397}
398
David Brazdild9510df2015-11-04 23:30:22 +0000399// Returns true if one of the patterns below has been recognized. If so, the
400// InstanceOf instruction together with the true branch of `ifInstruction` will
401// be returned using the out parameters.
402// Recognized patterns:
403// (1) patterns equivalent to `if (obj instanceof X)`
404// (a) InstanceOf -> Equal to 1 -> If
405// (b) InstanceOf -> NotEqual to 0 -> If
406// (c) InstanceOf -> If
407// (2) patterns equivalent to `if (!(obj instanceof X))`
408// (a) InstanceOf -> Equal to 0 -> If
409// (b) InstanceOf -> NotEqual to 1 -> If
410// (c) InstanceOf -> BooleanNot -> If
411static bool MatchIfInstanceOf(HIf* ifInstruction,
412 /* out */ HInstanceOf** instanceOf,
413 /* out */ HBasicBlock** trueBranch) {
414 HInstruction* input = ifInstruction->InputAt(0);
415
416 if (input->IsEqual()) {
417 HInstruction* rhs = input->AsEqual()->GetConstantRight();
418 if (rhs != nullptr) {
419 HInstruction* lhs = input->AsEqual()->GetLeastConstantLeft();
420 if (lhs->IsInstanceOf() && rhs->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000421 if (rhs->AsIntConstant()->IsTrue()) {
David Brazdild9510df2015-11-04 23:30:22 +0000422 // Case (1a)
423 *trueBranch = ifInstruction->IfTrueSuccessor();
424 } else {
425 // Case (2a)
Roland Levillain1a653882016-03-18 18:05:57 +0000426 DCHECK(rhs->AsIntConstant()->IsFalse()) << rhs->AsIntConstant()->GetValue();
David Brazdild9510df2015-11-04 23:30:22 +0000427 *trueBranch = ifInstruction->IfFalseSuccessor();
428 }
429 *instanceOf = lhs->AsInstanceOf();
430 return true;
431 }
432 }
433 } else if (input->IsNotEqual()) {
434 HInstruction* rhs = input->AsNotEqual()->GetConstantRight();
435 if (rhs != nullptr) {
436 HInstruction* lhs = input->AsNotEqual()->GetLeastConstantLeft();
437 if (lhs->IsInstanceOf() && rhs->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000438 if (rhs->AsIntConstant()->IsFalse()) {
David Brazdild9510df2015-11-04 23:30:22 +0000439 // Case (1b)
440 *trueBranch = ifInstruction->IfTrueSuccessor();
441 } else {
442 // Case (2b)
Roland Levillain1a653882016-03-18 18:05:57 +0000443 DCHECK(rhs->AsIntConstant()->IsTrue()) << rhs->AsIntConstant()->GetValue();
David Brazdild9510df2015-11-04 23:30:22 +0000444 *trueBranch = ifInstruction->IfFalseSuccessor();
445 }
446 *instanceOf = lhs->AsInstanceOf();
447 return true;
448 }
449 }
450 } else if (input->IsInstanceOf()) {
451 // Case (1c)
452 *instanceOf = input->AsInstanceOf();
453 *trueBranch = ifInstruction->IfTrueSuccessor();
454 return true;
455 } else if (input->IsBooleanNot()) {
456 HInstruction* not_input = input->InputAt(0);
457 if (not_input->IsInstanceOf()) {
458 // Case (2c)
459 *instanceOf = not_input->AsInstanceOf();
460 *trueBranch = ifInstruction->IfFalseSuccessor();
461 return true;
462 }
463 }
464
465 return false;
466}
467
Calin Juravleb1498f62015-02-16 13:13:29 +0000468// Detects if `block` is the True block for the pattern
469// `if (x instanceof ClassX) { }`
470// If that's the case insert an HBoundType instruction to bound the type of `x`
471// to `ClassX` in the scope of the dominated blocks.
472void ReferenceTypePropagation::BoundTypeForIfInstanceOf(HBasicBlock* block) {
Calin Juravleb3306642015-04-20 18:30:42 +0100473 HIf* ifInstruction = block->GetLastInstruction()->AsIf();
474 if (ifInstruction == nullptr) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000475 return;
476 }
David Brazdil0d13fee2015-04-17 14:52:19 +0100477
David Brazdild9510df2015-11-04 23:30:22 +0000478 // Try to recognize common `if (instanceof)` and `if (!instanceof)` patterns.
479 HInstanceOf* instanceOf = nullptr;
480 HBasicBlock* instanceOfTrueBlock = nullptr;
481 if (!MatchIfInstanceOf(ifInstruction, &instanceOf, &instanceOfTrueBlock)) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000482 return;
Calin Juravleacf735c2015-02-12 15:25:22 +0000483 }
484
Calin Juravle98893e12015-10-02 21:05:03 +0100485 HLoadClass* load_class = instanceOf->InputAt(1)->AsLoadClass();
486 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000487 if (!class_rti.IsValid()) {
488 // He have loaded an unresolved class. Don't bother bounding the type.
489 return;
Calin Juravle98893e12015-10-02 21:05:03 +0100490 }
Calin Juravleb3306642015-04-20 18:30:42 +0100491
Calin Juravleb1498f62015-02-16 13:13:29 +0000492 HInstruction* obj = instanceOf->InputAt(0);
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100493 if (obj->GetReferenceTypeInfo().IsExact() && !obj->IsPhi()) {
494 // This method is being called while doing a fixed-point calculation
495 // over phis. Non-phis instruction whose type is already known do
496 // not need to be bound to another type.
497 // Not that this also prevents replacing `HLoadClass` with a `HBoundType`.
498 // `HCheckCast` and `HInstanceOf` expect a `HLoadClass` as a second
499 // input.
500 return;
501 }
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000502
503 {
504 ScopedObjectAccess soa(Thread::Current());
505 if (!class_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes()) {
506 class_rti = ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false);
Calin Juravleb1498f62015-02-16 13:13:29 +0000507 }
508 }
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000509 BoundTypeIn(obj, instanceOfTrueBlock, /* start_instruction */ nullptr, class_rti);
Calin Juravleacf735c2015-02-12 15:25:22 +0000510}
511
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000512void ReferenceTypePropagation::RTPVisitor::SetClassAsTypeInfo(HInstruction* instr,
Mathieu Chartier3398c782016-09-30 10:27:43 -0700513 ObjPtr<mirror::Class> klass,
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000514 bool is_exact) {
Calin Juravle2e768302015-07-28 14:41:11 +0000515 if (instr->IsInvokeStaticOrDirect() && instr->AsInvokeStaticOrDirect()->IsStringInit()) {
516 // Calls to String.<init> are replaced with a StringFactory.
517 if (kIsDebugBuild) {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000518 HInvokeStaticOrDirect* invoke = instr->AsInvokeStaticOrDirect();
Calin Juravle2e768302015-07-28 14:41:11 +0000519 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko62977ff2016-04-20 15:06:31 +0100520 Thread* self = Thread::Current();
521 StackHandleScope<2> hs(self);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000522 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
Nicolas Geoffrayc89715c2015-10-28 12:06:25 +0000523 Handle<mirror::DexCache> dex_cache(
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000524 hs.NewHandle(FindDexCacheWithHint(self, dex_file, hint_dex_cache_)));
Nicolas Geoffrayc89715c2015-10-28 12:06:25 +0000525 // Use a null loader. We should probably use the compiling method's class loader,
526 // but then we would need to pass it to RTPVisitor just for this debug check. Since
527 // the method is from the String class, the null loader is good enough.
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100528 Handle<mirror::ClassLoader> loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
Vladimir Markoba118822017-06-12 15:41:56 +0100529 ArtMethod* method = cl->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000530 dex_file, invoke->GetDexMethodIndex(), dex_cache, loader, nullptr, kDirect);
Calin Juravle2e768302015-07-28 14:41:11 +0000531 DCHECK(method != nullptr);
532 mirror::Class* declaring_class = method->GetDeclaringClass();
533 DCHECK(declaring_class != nullptr);
534 DCHECK(declaring_class->IsStringClass())
David Sehr709b0702016-10-13 09:12:37 -0700535 << "Expected String class: " << declaring_class->PrettyDescriptor();
Calin Juravle2e768302015-07-28 14:41:11 +0000536 DCHECK(method->IsConstructor())
David Sehr709b0702016-10-13 09:12:37 -0700537 << "Expected String.<init>: " << method->PrettyMethod();
Calin Juravle2e768302015-07-28 14:41:11 +0000538 }
539 instr->SetReferenceTypeInfo(
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000540 ReferenceTypeInfo::Create(handle_cache_->GetStringClassHandle(), /* is_exact */ true));
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700541 } else if (IsAdmissible(klass.Ptr())) {
Aart Bikf417ff42016-04-25 12:51:37 -0700542 ReferenceTypeInfo::TypeHandle handle = handle_cache_->NewHandle(klass);
543 is_exact = is_exact || handle->CannotBeAssignedFromOtherTypes();
544 instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(handle, is_exact));
Calin Juravle2e768302015-07-28 14:41:11 +0000545 } else {
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000546 instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100547 }
548}
549
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000550void ReferenceTypePropagation::RTPVisitor::VisitDeoptimize(HDeoptimize* instr) {
551 BoundTypeForClassCheck(instr);
552}
553
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000554void ReferenceTypePropagation::RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800555 dex::TypeIndex type_idx,
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000556 const DexFile& dex_file,
557 bool is_exact) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100558 DCHECK_EQ(instr->GetType(), DataType::Type::kReference);
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +0100559
Calin Juravleacf735c2015-02-12 15:25:22 +0000560 ScopedObjectAccess soa(Thread::Current());
Vladimir Markocd556b02017-02-03 11:47:34 +0000561 ObjPtr<mirror::DexCache> dex_cache = FindDexCacheWithHint(soa.Self(), dex_file, hint_dex_cache_);
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000562 ObjPtr<mirror::Class> klass =
563 ClassLinker::LookupResolvedType(type_idx, dex_cache, class_loader_.Get());
564 SetClassAsTypeInfo(instr, klass, is_exact);
Calin Juravleacf735c2015-02-12 15:25:22 +0000565}
566
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000567void ReferenceTypePropagation::RTPVisitor::VisitNewInstance(HNewInstance* instr) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000568 ScopedObjectAccess soa(Thread::Current());
569 SetClassAsTypeInfo(instr, instr->GetLoadClass()->GetClass().Get(), /* is_exact */ true);
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +0100570}
571
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000572void ReferenceTypePropagation::RTPVisitor::VisitNewArray(HNewArray* instr) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000573 ScopedObjectAccess soa(Thread::Current());
574 SetClassAsTypeInfo(instr, instr->GetLoadClass()->GetClass().Get(), /* is_exact */ true);
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +0100575}
576
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000577void ReferenceTypePropagation::RTPVisitor::VisitParameterValue(HParameterValue* instr) {
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700578 // We check if the existing type is valid: the inliner may have set it.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100579 if (instr->GetType() == DataType::Type::kReference && !instr->GetReferenceTypeInfo().IsValid()) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000580 UpdateReferenceTypeInfo(instr,
581 instr->GetTypeIndex(),
582 instr->GetDexFile(),
583 /* is_exact */ false);
Calin Juravle2e768302015-07-28 14:41:11 +0000584 }
585}
586
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000587void ReferenceTypePropagation::RTPVisitor::UpdateFieldAccessTypeInfo(HInstruction* instr,
588 const FieldInfo& info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100589 if (instr->GetType() != DataType::Type::kReference) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100590 return;
591 }
592
Vladimir Marko62977ff2016-04-20 15:06:31 +0100593 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier3398c782016-09-30 10:27:43 -0700594 ObjPtr<mirror::Class> klass;
David Brazdild9510df2015-11-04 23:30:22 +0000595
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000596 // The field is unknown only during tests.
597 if (info.GetField() != nullptr) {
598 klass = info.GetField()->GetType<false>();
David Brazdild9510df2015-11-04 23:30:22 +0000599 }
600
Calin Juravle2e768302015-07-28 14:41:11 +0000601 SetClassAsTypeInfo(instr, klass, /* is_exact */ false);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100602}
603
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000604void ReferenceTypePropagation::RTPVisitor::VisitInstanceFieldGet(HInstanceFieldGet* instr) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100605 UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
606}
607
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000608void ReferenceTypePropagation::RTPVisitor::VisitStaticFieldGet(HStaticFieldGet* instr) {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100609 UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo());
610}
611
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000612void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedInstanceFieldGet(
613 HUnresolvedInstanceFieldGet* instr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100614 // TODO: Use descriptor to get the actual type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100615 if (instr->GetFieldType() == DataType::Type::kReference) {
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000616 instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100617 }
618}
619
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000620void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedStaticFieldGet(
621 HUnresolvedStaticFieldGet* instr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100622 // TODO: Use descriptor to get the actual type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100623 if (instr->GetFieldType() == DataType::Type::kReference) {
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000624 instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100625 }
626}
627
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000628void ReferenceTypePropagation::RTPVisitor::VisitLoadClass(HLoadClass* instr) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000629 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000630 Handle<mirror::Class> resolved_class = instr->GetClass();
631 if (IsAdmissible(resolved_class.Get())) {
Calin Juravle98893e12015-10-02 21:05:03 +0100632 instr->SetLoadedClassRTI(ReferenceTypeInfo::Create(
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000633 resolved_class, /* is_exact */ true));
Calin Juravle98893e12015-10-02 21:05:03 +0100634 }
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000635 instr->SetReferenceTypeInfo(
636 ReferenceTypeInfo::Create(handle_cache_->GetClassClassHandle(), /* is_exact */ true));
Calin Juravle2e768302015-07-28 14:41:11 +0000637}
638
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000639void ReferenceTypePropagation::RTPVisitor::VisitClinitCheck(HClinitCheck* instr) {
Calin Juravle2e768302015-07-28 14:41:11 +0000640 instr->SetReferenceTypeInfo(instr->InputAt(0)->GetReferenceTypeInfo());
641}
642
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000643void ReferenceTypePropagation::RTPVisitor::VisitLoadString(HLoadString* instr) {
644 instr->SetReferenceTypeInfo(
645 ReferenceTypeInfo::Create(handle_cache_->GetStringClassHandle(), /* is_exact */ true));
Calin Juravle2e768302015-07-28 14:41:11 +0000646}
647
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000648void ReferenceTypePropagation::RTPVisitor::VisitLoadException(HLoadException* instr) {
David Brazdilbbd733e2015-08-18 17:48:17 +0100649 DCHECK(instr->GetBlock()->IsCatchBlock());
650 TryCatchInformation* catch_info = instr->GetBlock()->GetTryCatchInformation();
651
652 if (catch_info->IsCatchAllTypeIndex()) {
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000653 instr->SetReferenceTypeInfo(
654 ReferenceTypeInfo::Create(handle_cache_->GetThrowableClassHandle(), /* is_exact */ false));
David Brazdilbbd733e2015-08-18 17:48:17 +0100655 } else {
656 UpdateReferenceTypeInfo(instr,
657 catch_info->GetCatchTypeIndex(),
658 catch_info->GetCatchDexFile(),
659 /* is_exact */ false);
660 }
661}
662
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000663void ReferenceTypePropagation::RTPVisitor::VisitNullCheck(HNullCheck* instr) {
Calin Juravle2e768302015-07-28 14:41:11 +0000664 ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
David Brazdilfe860702015-12-02 09:06:57 +0000665 if (parent_rti.IsValid()) {
666 instr->SetReferenceTypeInfo(parent_rti);
667 }
Calin Juravle2e768302015-07-28 14:41:11 +0000668}
669
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000670void ReferenceTypePropagation::RTPVisitor::VisitBoundType(HBoundType* instr) {
David Brazdilf5552582015-12-27 13:36:12 +0000671 ReferenceTypeInfo class_rti = instr->GetUpperBound();
672 if (class_rti.IsValid()) {
Vladimir Marko456307a2016-04-19 14:12:13 +0000673 ScopedObjectAccess soa(Thread::Current());
David Brazdilf5552582015-12-27 13:36:12 +0000674 // Narrow the type as much as possible.
675 HInstruction* obj = instr->InputAt(0);
676 ReferenceTypeInfo obj_rti = obj->GetReferenceTypeInfo();
Nicolas Geoffray8c3794c2016-12-20 09:25:42 +0000677 if (class_rti.IsExact()) {
678 instr->SetReferenceTypeInfo(class_rti);
David Brazdil744a1c62015-12-28 10:53:34 +0000679 } else if (obj_rti.IsValid()) {
680 if (class_rti.IsSupertypeOf(obj_rti)) {
681 // Object type is more specific.
682 instr->SetReferenceTypeInfo(obj_rti);
683 } else {
Nicolas Geoffray8c3794c2016-12-20 09:25:42 +0000684 // Upper bound is more specific, or unrelated to the object's type.
685 // Note that the object might then be exact, and we know the code dominated by this
686 // bound type is dead. To not confuse potential other optimizations, we mark
687 // the bound as non-exact.
David Brazdil744a1c62015-12-28 10:53:34 +0000688 instr->SetReferenceTypeInfo(
689 ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false));
690 }
David Brazdilf5552582015-12-27 13:36:12 +0000691 } else {
David Brazdil744a1c62015-12-28 10:53:34 +0000692 // Object not typed yet. Leave BoundType untyped for now rather than
693 // assign the type conservatively.
David Brazdilf5552582015-12-27 13:36:12 +0000694 }
695 instr->SetCanBeNull(obj->CanBeNull() && instr->GetUpperCanBeNull());
696 } else {
697 // The owner of the BoundType was already visited. If the class is unresolved,
698 // the BoundType should have been removed from the data flow and this method
699 // should remove it from the graph.
700 DCHECK(!instr->HasUses());
701 instr->GetBlock()->RemoveInstruction(instr);
702 }
703}
704
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000705void ReferenceTypePropagation::RTPVisitor::VisitCheckCast(HCheckCast* check_cast) {
Calin Juravle98893e12015-10-02 21:05:03 +0100706 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
707 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
David Brazdilf5552582015-12-27 13:36:12 +0000708 HBoundType* bound_type = check_cast->GetNext()->AsBoundType();
709 if (bound_type == nullptr || bound_type->GetUpperBound().IsValid()) {
710 // The next instruction is not an uninitialized BoundType. This must be
711 // an RTP pass after SsaBuilder and we do not need to do anything.
712 return;
Calin Juravle98893e12015-10-02 21:05:03 +0100713 }
David Brazdilf5552582015-12-27 13:36:12 +0000714 DCHECK_EQ(bound_type->InputAt(0), check_cast->InputAt(0));
715
716 if (class_rti.IsValid()) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000717 DCHECK(is_first_run_);
Nicolas Geoffray8c3794c2016-12-20 09:25:42 +0000718 ScopedObjectAccess soa(Thread::Current());
David Brazdilf5552582015-12-27 13:36:12 +0000719 // This is the first run of RTP and class is resolved.
Nicolas Geoffray8c3794c2016-12-20 09:25:42 +0000720 bool is_exact = class_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes();
721 bound_type->SetUpperBound(ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), is_exact),
722 /* CheckCast succeeds for nulls. */ true);
David Brazdilf5552582015-12-27 13:36:12 +0000723 } else {
724 // This is the first run of RTP and class is unresolved. Remove the binding.
725 // The instruction itself is removed in VisitBoundType so as to not
726 // invalidate HInstructionIterator.
727 bound_type->ReplaceWith(bound_type->InputAt(0));
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000728 }
729}
730
Calin Juravleb1498f62015-02-16 13:13:29 +0000731void ReferenceTypePropagation::VisitPhi(HPhi* phi) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100732 if (phi->IsDead() || phi->GetType() != DataType::Type::kReference) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000733 return;
Calin Juravleacf735c2015-02-12 15:25:22 +0000734 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000735
736 if (phi->GetBlock()->IsLoopHeader()) {
737 // Set the initial type for the phi. Use the non back edge input for reaching
738 // a fixed point faster.
David Brazdilfe860702015-12-02 09:06:57 +0000739 HInstruction* first_input = phi->InputAt(0);
740 ReferenceTypeInfo first_input_rti = first_input->GetReferenceTypeInfo();
741 if (first_input_rti.IsValid() && !first_input->IsNullConstant()) {
742 phi->SetCanBeNull(first_input->CanBeNull());
743 phi->SetReferenceTypeInfo(first_input_rti);
744 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000745 AddToWorklist(phi);
Calin Juravle10e244f2015-01-26 18:54:32 +0000746 } else {
Calin Juravleb1498f62015-02-16 13:13:29 +0000747 // Eagerly compute the type of the phi, for quicker convergence. Note
748 // that we don't need to add users to the worklist because we are
749 // doing a reverse post-order visit, therefore either the phi users are
750 // non-loop phi and will be visited later in the visit, or are loop-phis,
751 // and they are already in the work list.
752 UpdateNullability(phi);
753 UpdateReferenceTypeInfo(phi);
754 }
755}
756
Mads Ager16e52892017-07-14 13:11:37 +0200757void ReferenceTypePropagation::FixUpInstructionType(HInstruction* instruction,
758 VariableSizedHandleScope* handle_scope) {
759 if (instruction->IsSelect()) {
760 ScopedObjectAccess soa(Thread::Current());
761 HandleCache handle_cache(handle_scope);
762 HSelect* select = instruction->AsSelect();
763 ReferenceTypeInfo false_rti = select->GetFalseValue()->GetReferenceTypeInfo();
764 ReferenceTypeInfo true_rti = select->GetTrueValue()->GetReferenceTypeInfo();
765 select->SetReferenceTypeInfo(MergeTypes(false_rti, true_rti, &handle_cache));
766 } else {
767 LOG(FATAL) << "Invalid instruction in FixUpInstructionType";
768 }
769}
770
Calin Juravleb1498f62015-02-16 13:13:29 +0000771ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a,
Mads Ager16e52892017-07-14 13:11:37 +0200772 const ReferenceTypeInfo& b,
773 HandleCache* handle_cache) {
Calin Juravle2e768302015-07-28 14:41:11 +0000774 if (!b.IsValid()) {
775 return a;
776 }
777 if (!a.IsValid()) {
778 return b;
Calin Juravle20e60712015-07-01 18:41:04 +0100779 }
780
Calin Juravle2e768302015-07-28 14:41:11 +0000781 bool is_exact = a.IsExact() && b.IsExact();
Calin Juravle52503d82015-11-11 16:58:31 +0000782 ReferenceTypeInfo::TypeHandle result_type_handle;
783 ReferenceTypeInfo::TypeHandle a_type_handle = a.GetTypeHandle();
784 ReferenceTypeInfo::TypeHandle b_type_handle = b.GetTypeHandle();
785 bool a_is_interface = a_type_handle->IsInterface();
786 bool b_is_interface = b_type_handle->IsInterface();
Calin Juravle2e768302015-07-28 14:41:11 +0000787
788 if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) {
Calin Juravle52503d82015-11-11 16:58:31 +0000789 result_type_handle = a_type_handle;
Calin Juravle2e768302015-07-28 14:41:11 +0000790 } else if (a.IsSupertypeOf(b)) {
Calin Juravle52503d82015-11-11 16:58:31 +0000791 result_type_handle = a_type_handle;
Calin Juravle2e768302015-07-28 14:41:11 +0000792 is_exact = false;
793 } else if (b.IsSupertypeOf(a)) {
Calin Juravle52503d82015-11-11 16:58:31 +0000794 result_type_handle = b_type_handle;
795 is_exact = false;
796 } else if (!a_is_interface && !b_is_interface) {
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000797 result_type_handle =
Mads Ager16e52892017-07-14 13:11:37 +0200798 handle_cache->NewHandle(a_type_handle->GetCommonSuperClass(b_type_handle));
Calin Juravle2e768302015-07-28 14:41:11 +0000799 is_exact = false;
800 } else {
Calin Juravle52503d82015-11-11 16:58:31 +0000801 // This can happen if:
802 // - both types are interfaces. TODO(calin): implement
803 // - one is an interface, the other a class, and the type does not implement the interface
804 // e.g:
805 // void foo(Interface i, boolean cond) {
806 // Object o = cond ? i : new Object();
807 // }
Mads Ager16e52892017-07-14 13:11:37 +0200808 result_type_handle = handle_cache->GetObjectClassHandle();
Calin Juravle2e768302015-07-28 14:41:11 +0000809 is_exact = false;
810 }
811
Calin Juravle52503d82015-11-11 16:58:31 +0000812 return ReferenceTypeInfo::Create(result_type_handle, is_exact);
Calin Juravle2e768302015-07-28 14:41:11 +0000813}
814
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000815void ReferenceTypePropagation::UpdateArrayGet(HArrayGet* instr, HandleCache* handle_cache) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100816 DCHECK_EQ(DataType::Type::kReference, instr->GetType());
Calin Juravle2e768302015-07-28 14:41:11 +0000817
818 ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
David Brazdilfe860702015-12-02 09:06:57 +0000819 if (!parent_rti.IsValid()) {
820 return;
821 }
Calin Juravle2e768302015-07-28 14:41:11 +0000822
823 Handle<mirror::Class> handle = parent_rti.GetTypeHandle();
Aart Bikf417ff42016-04-25 12:51:37 -0700824 if (handle->IsObjectArrayClass() && IsAdmissible(handle->GetComponentType())) {
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000825 ReferenceTypeInfo::TypeHandle component_handle =
826 handle_cache->NewHandle(handle->GetComponentType());
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000827 bool is_exact = component_handle->CannotBeAssignedFromOtherTypes();
828 instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(component_handle, is_exact));
Calin Juravle2e768302015-07-28 14:41:11 +0000829 } else {
830 // We don't know what the parent actually is, so we fallback to object.
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000831 instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
Calin Juravle2e768302015-07-28 14:41:11 +0000832 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000833}
834
835bool ReferenceTypePropagation::UpdateReferenceTypeInfo(HInstruction* instr) {
836 ScopedObjectAccess soa(Thread::Current());
837
838 ReferenceTypeInfo previous_rti = instr->GetReferenceTypeInfo();
839 if (instr->IsBoundType()) {
840 UpdateBoundType(instr->AsBoundType());
841 } else if (instr->IsPhi()) {
842 UpdatePhi(instr->AsPhi());
Calin Juravle2e768302015-07-28 14:41:11 +0000843 } else if (instr->IsNullCheck()) {
844 ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo();
845 if (parent_rti.IsValid()) {
846 instr->SetReferenceTypeInfo(parent_rti);
847 }
848 } else if (instr->IsArrayGet()) {
David Brazdilfe860702015-12-02 09:06:57 +0000849 // TODO: consider if it's worth "looking back" and binding the input object
Calin Juravle2e768302015-07-28 14:41:11 +0000850 // to an array type.
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000851 UpdateArrayGet(instr->AsArrayGet(), &handle_cache_);
Calin Juravleb1498f62015-02-16 13:13:29 +0000852 } else {
853 LOG(FATAL) << "Invalid instruction (should not get here)";
854 }
855
856 return !previous_rti.IsEqual(instr->GetReferenceTypeInfo());
857}
858
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000859void ReferenceTypePropagation::RTPVisitor::VisitInvoke(HInvoke* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100860 if (instr->GetType() != DataType::Type::kReference) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100861 return;
862 }
863
864 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000865 ArtMethod* method = instr->GetResolvedMethod();
Vladimir Markob45528c2017-07-27 14:14:28 +0100866 ObjPtr<mirror::Class> klass = (method == nullptr) ? nullptr : method->LookupResolvedReturnType();
Calin Juravle2e768302015-07-28 14:41:11 +0000867 SetClassAsTypeInfo(instr, klass, /* is_exact */ false);
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100868}
869
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000870void ReferenceTypePropagation::RTPVisitor::VisitArrayGet(HArrayGet* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100871 if (instr->GetType() != DataType::Type::kReference) {
Guillaume "Vermeille" Sanchez72a5eb52015-06-02 17:39:45 +0100872 return;
873 }
David Brazdilfe860702015-12-02 09:06:57 +0000874
Guillaume "Vermeille" Sanchez72a5eb52015-06-02 17:39:45 +0100875 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko7d1fbf32016-01-26 15:01:12 +0000876 UpdateArrayGet(instr, handle_cache_);
Calin Juravle2e768302015-07-28 14:41:11 +0000877 if (!instr->GetReferenceTypeInfo().IsValid()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100878 worklist_->push_back(instr);
Guillaume "Vermeille" Sanchez72a5eb52015-06-02 17:39:45 +0100879 }
880}
881
Calin Juravleb1498f62015-02-16 13:13:29 +0000882void ReferenceTypePropagation::UpdateBoundType(HBoundType* instr) {
Nicolas Geoffray8c3794c2016-12-20 09:25:42 +0000883 ReferenceTypeInfo input_rti = instr->InputAt(0)->GetReferenceTypeInfo();
884 if (!input_rti.IsValid()) {
Calin Juravle2e768302015-07-28 14:41:11 +0000885 return; // No new info yet.
886 }
887
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000888 ReferenceTypeInfo upper_bound_rti = instr->GetUpperBound();
Nicolas Geoffray8c3794c2016-12-20 09:25:42 +0000889 if (upper_bound_rti.IsExact()) {
890 instr->SetReferenceTypeInfo(upper_bound_rti);
891 } else if (upper_bound_rti.IsSupertypeOf(input_rti)) {
892 // input is more specific.
893 instr->SetReferenceTypeInfo(input_rti);
894 } else {
895 // upper_bound is more specific or unrelated.
896 // Note that the object might then be exact, and we know the code dominated by this
897 // bound type is dead. To not confuse potential other optimizations, we mark
898 // the bound as non-exact.
899 instr->SetReferenceTypeInfo(
900 ReferenceTypeInfo::Create(upper_bound_rti.GetTypeHandle(), /* is_exact */ false));
Calin Juravleb1498f62015-02-16 13:13:29 +0000901 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000902}
903
Calin Juravle617bd922015-11-11 14:59:46 +0000904// NullConstant inputs are ignored during merging as they do not provide any useful information.
905// If all the inputs are NullConstants then the type of the phi will be set to Object.
Calin Juravleb1498f62015-02-16 13:13:29 +0000906void ReferenceTypePropagation::UpdatePhi(HPhi* instr) {
David Brazdild9510df2015-11-04 23:30:22 +0000907 DCHECK(instr->IsLive());
908
Vladimir Markoe9004912016-06-16 16:50:52 +0100909 HInputsRef inputs = instr->GetInputs();
Calin Juravle617bd922015-11-11 14:59:46 +0000910 size_t first_input_index_not_null = 0;
Vladimir Marko372f10e2016-05-17 16:30:10 +0100911 while (first_input_index_not_null < inputs.size() &&
Vladimir Markoe9004912016-06-16 16:50:52 +0100912 inputs[first_input_index_not_null]->IsNullConstant()) {
Calin Juravle617bd922015-11-11 14:59:46 +0000913 first_input_index_not_null++;
914 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100915 if (first_input_index_not_null == inputs.size()) {
Calin Juravle617bd922015-11-11 14:59:46 +0000916 // All inputs are NullConstants, set the type to object.
917 // This may happen in the presence of inlining.
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000918 instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti());
Calin Juravle617bd922015-11-11 14:59:46 +0000919 return;
920 }
921
922 ReferenceTypeInfo new_rti = instr->InputAt(first_input_index_not_null)->GetReferenceTypeInfo();
923
Calin Juravle2e768302015-07-28 14:41:11 +0000924 if (new_rti.IsValid() && new_rti.IsObjectClass() && !new_rti.IsExact()) {
925 // Early return if we are Object and inexact.
Calin Juravleb1498f62015-02-16 13:13:29 +0000926 instr->SetReferenceTypeInfo(new_rti);
927 return;
928 }
Calin Juravle617bd922015-11-11 14:59:46 +0000929
Vladimir Marko372f10e2016-05-17 16:30:10 +0100930 for (size_t i = first_input_index_not_null + 1; i < inputs.size(); i++) {
931 if (inputs[i]->IsNullConstant()) {
Calin Juravle617bd922015-11-11 14:59:46 +0000932 continue;
933 }
Mads Ager16e52892017-07-14 13:11:37 +0200934 new_rti = MergeTypes(new_rti, inputs[i]->GetReferenceTypeInfo(), &handle_cache_);
Calin Juravle2e768302015-07-28 14:41:11 +0000935 if (new_rti.IsValid() && new_rti.IsObjectClass()) {
Calin Juravleb1498f62015-02-16 13:13:29 +0000936 if (!new_rti.IsExact()) {
937 break;
938 } else {
939 continue;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000940 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000941 }
942 }
David Brazdilfe860702015-12-02 09:06:57 +0000943
944 if (new_rti.IsValid()) {
945 instr->SetReferenceTypeInfo(new_rti);
946 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000947}
948
949// Re-computes and updates the nullability of the instruction. Returns whether or
950// not the nullability was changed.
951bool ReferenceTypePropagation::UpdateNullability(HInstruction* instr) {
David Brazdild9510df2015-11-04 23:30:22 +0000952 DCHECK((instr->IsPhi() && instr->AsPhi()->IsLive())
Calin Juravle2e768302015-07-28 14:41:11 +0000953 || instr->IsBoundType()
954 || instr->IsNullCheck()
955 || instr->IsArrayGet());
956
957 if (!instr->IsPhi() && !instr->IsBoundType()) {
958 return false;
959 }
Calin Juravleb1498f62015-02-16 13:13:29 +0000960
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000961 bool existing_can_be_null = instr->CanBeNull();
962 if (instr->IsPhi()) {
963 HPhi* phi = instr->AsPhi();
964 bool new_can_be_null = false;
Vladimir Marko372f10e2016-05-17 16:30:10 +0100965 for (HInstruction* input : phi->GetInputs()) {
966 if (input->CanBeNull()) {
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000967 new_can_be_null = true;
968 break;
969 }
970 }
971 phi->SetCanBeNull(new_can_be_null);
972 } else if (instr->IsBoundType()) {
973 HBoundType* bound_type = instr->AsBoundType();
974 bound_type->SetCanBeNull(instr->InputAt(0)->CanBeNull() && bound_type->GetUpperCanBeNull());
Calin Juravleb1498f62015-02-16 13:13:29 +0000975 }
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000976 return existing_can_be_null != instr->CanBeNull();
Calin Juravle10e244f2015-01-26 18:54:32 +0000977}
978
979void ReferenceTypePropagation::ProcessWorklist() {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100980 while (!worklist_.empty()) {
981 HInstruction* instruction = worklist_.back();
982 worklist_.pop_back();
Calin Juravle12617592015-10-16 16:28:46 +0100983 bool updated_nullability = UpdateNullability(instruction);
984 bool updated_reference_type = UpdateReferenceTypeInfo(instruction);
985 if (updated_nullability || updated_reference_type) {
Calin Juravle10e244f2015-01-26 18:54:32 +0000986 AddDependentInstructionsToWorklist(instruction);
987 }
988 }
989}
990
Calin Juravleb1498f62015-02-16 13:13:29 +0000991void ReferenceTypePropagation::AddToWorklist(HInstruction* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100992 DCHECK_EQ(instruction->GetType(), DataType::Type::kReference)
Calin Juravle2e768302015-07-28 14:41:11 +0000993 << instruction->DebugName() << ":" << instruction->GetType();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100994 worklist_.push_back(instruction);
Calin Juravle10e244f2015-01-26 18:54:32 +0000995}
996
Calin Juravleb1498f62015-02-16 13:13:29 +0000997void ReferenceTypePropagation::AddDependentInstructionsToWorklist(HInstruction* instruction) {
Vladimir Marko46817b82016-03-29 12:21:58 +0100998 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
999 HInstruction* user = use.GetUser();
David Brazdild9510df2015-11-04 23:30:22 +00001000 if ((user->IsPhi() && user->AsPhi()->IsLive())
Calin Juravle2e768302015-07-28 14:41:11 +00001001 || user->IsBoundType()
1002 || user->IsNullCheck()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001003 || (user->IsArrayGet() && (user->GetType() == DataType::Type::kReference))) {
Calin Juravlebeba9302015-07-08 15:57:18 +00001004 AddToWorklist(user);
Calin Juravle10e244f2015-01-26 18:54:32 +00001005 }
1006 }
1007}
Vladimir Marko7d1fbf32016-01-26 15:01:12 +00001008
Calin Juravle10e244f2015-01-26 18:54:32 +00001009} // namespace art