Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "reference_type_propagation.h" |
| 18 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 19 | #include "art_field-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 20 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 21 | #include "base/enums.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 22 | #include "class_linker-inl.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 23 | #include "mirror/class-inl.h" |
| 24 | #include "mirror/dex_cache.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 26 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 27 | namespace art { |
| 28 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 29 | static inline ObjPtr<mirror::DexCache> FindDexCacheWithHint( |
| 30 | Thread* self, const DexFile& dex_file, Handle<mirror::DexCache> hint_dex_cache) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 31 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 32 | 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 Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 39 | static inline ReferenceTypeInfo::TypeHandle GetRootHandle(VariableSizedHandleScope* handles, |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 40 | 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 | |
| 51 | ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetObjectClassHandle() { |
| 52 | return GetRootHandle(handles_, ClassLinker::kJavaLangObject, &object_class_handle_); |
| 53 | } |
| 54 | |
| 55 | ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetClassClassHandle() { |
| 56 | return GetRootHandle(handles_, ClassLinker::kJavaLangClass, &class_class_handle_); |
| 57 | } |
| 58 | |
| 59 | ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetStringClassHandle() { |
| 60 | return GetRootHandle(handles_, ClassLinker::kJavaLangString, &string_class_handle_); |
| 61 | } |
| 62 | |
| 63 | ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetThrowableClassHandle() { |
| 64 | return GetRootHandle(handles_, ClassLinker::kJavaLangThrowable, &throwable_class_handle_); |
| 65 | } |
| 66 | |
| 67 | class ReferenceTypePropagation::RTPVisitor : public HGraphDelegateVisitor { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 68 | public: |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 69 | RTPVisitor(HGraph* graph, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 70 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 71 | Handle<mirror::DexCache> hint_dex_cache, |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 72 | HandleCache* handle_cache, |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 73 | ArenaVector<HInstruction*>* worklist, |
| 74 | bool is_first_run) |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 75 | : HGraphDelegateVisitor(graph), |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 76 | class_loader_(class_loader), |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 77 | hint_dex_cache_(hint_dex_cache), |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 78 | handle_cache_(handle_cache), |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 79 | worklist_(worklist), |
| 80 | is_first_run_(is_first_run) {} |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 81 | |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 82 | void VisitDeoptimize(HDeoptimize* deopt) OVERRIDE; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 83 | void VisitNewInstance(HNewInstance* new_instance) OVERRIDE; |
| 84 | void VisitLoadClass(HLoadClass* load_class) OVERRIDE; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 85 | void VisitClinitCheck(HClinitCheck* clinit_check) OVERRIDE; |
| 86 | void VisitLoadString(HLoadString* instr) OVERRIDE; |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 87 | void VisitLoadException(HLoadException* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 88 | void VisitNewArray(HNewArray* instr) OVERRIDE; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 89 | void VisitParameterValue(HParameterValue* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 90 | void UpdateFieldAccessTypeInfo(HInstruction* instr, const FieldInfo& info); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 91 | void SetClassAsTypeInfo(HInstruction* instr, ObjPtr<mirror::Class> klass, bool is_exact) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 92 | REQUIRES_SHARED(Locks::mutator_lock_); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 93 | void VisitInstanceFieldGet(HInstanceFieldGet* instr) OVERRIDE; |
| 94 | void VisitStaticFieldGet(HStaticFieldGet* instr) OVERRIDE; |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 95 | void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* instr) OVERRIDE; |
| 96 | void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 97 | void VisitInvoke(HInvoke* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | 72a5eb5 | 2015-06-02 17:39:45 +0100 | [diff] [blame] | 98 | void VisitArrayGet(HArrayGet* instr) OVERRIDE; |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 99 | void VisitCheckCast(HCheckCast* instr) OVERRIDE; |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 100 | void VisitBoundType(HBoundType* instr) OVERRIDE; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 101 | void VisitNullCheck(HNullCheck* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 102 | void UpdateReferenceTypeInfo(HInstruction* instr, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 103 | dex::TypeIndex type_idx, |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 104 | const DexFile& dex_file, |
| 105 | bool is_exact); |
| 106 | |
| 107 | private: |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 108 | Handle<mirror::ClassLoader> class_loader_; |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 109 | Handle<mirror::DexCache> hint_dex_cache_; |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 110 | HandleCache* handle_cache_; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 111 | ArenaVector<HInstruction*>* worklist_; |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 112 | const bool is_first_run_; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 115 | ReferenceTypePropagation::ReferenceTypePropagation(HGraph* graph, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 116 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 117 | Handle<mirror::DexCache> hint_dex_cache, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 118 | VariableSizedHandleScope* handles, |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 119 | bool is_first_run, |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 120 | const char* name) |
| 121 | : HOptimization(graph, name), |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 122 | class_loader_(class_loader), |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 123 | hint_dex_cache_(hint_dex_cache), |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 124 | handle_cache_(handles), |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 125 | worklist_(graph->GetArena()->Adapter(kArenaAllocReferenceTypePropagation)), |
| 126 | is_first_run_(is_first_run) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 129 | void ReferenceTypePropagation::ValidateTypes() { |
| 130 | // TODO: move this to the graph checker. |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 131 | if (kIsDebugBuild) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 132 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 133 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 134 | for (HInstructionIterator iti(block->GetInstructions()); !iti.Done(); iti.Advance()) { |
| 135 | HInstruction* instr = iti.Current(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 136 | if (instr->GetType() == DataType::Type::kReference) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 137 | 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 Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 142 | HLoadClass* cls = instr->AsLoadClass(); |
| 143 | DCHECK(cls->GetReferenceTypeInfo().IsExact()); |
| 144 | DCHECK(!cls->GetLoadedClassRTI().IsValid() || cls->GetLoadedClassRTI().IsExact()); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 145 | } 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 Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 156 | void ReferenceTypePropagation::Visit(HInstruction* instruction) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 157 | RTPVisitor visitor(graph_, |
| 158 | class_loader_, |
| 159 | hint_dex_cache_, |
| 160 | &handle_cache_, |
| 161 | &worklist_, |
| 162 | is_first_run_); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 163 | instruction->Accept(&visitor); |
| 164 | } |
| 165 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 166 | // 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. |
| 171 | static bool ShouldCreateBoundType(HInstruction* position, |
| 172 | HInstruction* obj, |
| 173 | ReferenceTypeInfo upper_bound, |
| 174 | HInstruction* dominator_instr, |
| 175 | HBasicBlock* dominator_block) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 176 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 177 | // 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 Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 187 | for (const HUseListNode<HInstruction*>& use : obj->GetUses()) { |
| 188 | HInstruction* user = use.GetUser(); |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 189 | 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 Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 208 | // 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`. |
| 211 | static 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) |
| 262 | static 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 Geoffray | cd9f857 | 2017-01-16 15:08:56 +0000 | [diff] [blame] | 309 | HBasicBlock* trueBlock = compare->IsEqual() |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 310 | ? check->AsIf()->IfTrueSuccessor() |
| 311 | : check->AsIf()->IfFalseSuccessor(); |
| 312 | BoundTypeIn(receiver, trueBlock, /* start_instruction */ nullptr, class_rti); |
| 313 | } else { |
| 314 | DCHECK(check->IsDeoptimize()); |
Nicolas Geoffray | 6f8e2c9 | 2017-03-23 14:37:26 +0000 | [diff] [blame] | 315 | if (compare->IsEqual() && check->AsDeoptimize()->GuardsAnInput()) { |
| 316 | check->SetReferenceTypeInfo(class_rti); |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void 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 | |
| 335 | void ReferenceTypePropagation::VisitBasicBlock(HBasicBlock* block) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 336 | RTPVisitor visitor(graph_, |
| 337 | class_loader_, |
| 338 | hint_dex_cache_, |
| 339 | &handle_cache_, |
| 340 | &worklist_, |
| 341 | is_first_run_); |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 342 | // 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 Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 360 | void ReferenceTypePropagation::BoundTypeForIfNotNull(HBasicBlock* block) { |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 361 | HIf* ifInstruction = block->GetLastInstruction()->AsIf(); |
| 362 | if (ifInstruction == nullptr) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 363 | return; |
| 364 | } |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 365 | HInstruction* ifInput = ifInstruction->InputAt(0); |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 366 | if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) { |
| 367 | return; |
| 368 | } |
| 369 | HInstruction* input0 = ifInput->InputAt(0); |
| 370 | HInstruction* input1 = ifInput->InputAt(1); |
Calin Juravle | edad8ad | 2015-04-23 14:34:33 +0100 | [diff] [blame] | 371 | HInstruction* obj = nullptr; |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 372 | |
Calin Juravle | edad8ad | 2015-04-23 14:34:33 +0100 | [diff] [blame] | 373 | if (input1->IsNullConstant()) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 374 | obj = input0; |
Calin Juravle | edad8ad | 2015-04-23 14:34:33 +0100 | [diff] [blame] | 375 | } else if (input0->IsNullConstant()) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 376 | obj = input1; |
| 377 | } else { |
| 378 | return; |
| 379 | } |
| 380 | |
Nicolas Geoffray | 7d5ea03 | 2015-07-02 15:48:27 +0100 | [diff] [blame] | 381 | 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 Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 387 | // 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 Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 389 | HBasicBlock* notNullBlock = ifInput->IsNotEqual() |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 390 | ? ifInstruction->IfTrueSuccessor() |
| 391 | : ifInstruction->IfFalseSuccessor(); |
| 392 | |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 393 | ReferenceTypeInfo object_rti = ReferenceTypeInfo::Create( |
| 394 | handle_cache_.GetObjectClassHandle(), /* is_exact */ false); |
| 395 | |
| 396 | BoundTypeIn(obj, notNullBlock, /* start_instruction */ nullptr, object_rti); |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 397 | } |
| 398 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 399 | // 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 |
| 411 | static 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 Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 421 | if (rhs->AsIntConstant()->IsTrue()) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 422 | // Case (1a) |
| 423 | *trueBranch = ifInstruction->IfTrueSuccessor(); |
| 424 | } else { |
| 425 | // Case (2a) |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 426 | DCHECK(rhs->AsIntConstant()->IsFalse()) << rhs->AsIntConstant()->GetValue(); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 427 | *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 Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 438 | if (rhs->AsIntConstant()->IsFalse()) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 439 | // Case (1b) |
| 440 | *trueBranch = ifInstruction->IfTrueSuccessor(); |
| 441 | } else { |
| 442 | // Case (2b) |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 443 | DCHECK(rhs->AsIntConstant()->IsTrue()) << rhs->AsIntConstant()->GetValue(); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 444 | *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 Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 468 | // 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. |
| 472 | void ReferenceTypePropagation::BoundTypeForIfInstanceOf(HBasicBlock* block) { |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 473 | HIf* ifInstruction = block->GetLastInstruction()->AsIf(); |
| 474 | if (ifInstruction == nullptr) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 475 | return; |
| 476 | } |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 477 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 478 | // 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 Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 482 | return; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 485 | HLoadClass* load_class = instanceOf->InputAt(1)->AsLoadClass(); |
| 486 | ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI(); |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 487 | if (!class_rti.IsValid()) { |
| 488 | // He have loaded an unresolved class. Don't bother bounding the type. |
| 489 | return; |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 490 | } |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 491 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 492 | HInstruction* obj = instanceOf->InputAt(0); |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 493 | 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 Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 502 | |
| 503 | { |
| 504 | ScopedObjectAccess soa(Thread::Current()); |
| 505 | if (!class_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes()) { |
| 506 | class_rti = ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 509 | BoundTypeIn(obj, instanceOfTrueBlock, /* start_instruction */ nullptr, class_rti); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 512 | void ReferenceTypePropagation::RTPVisitor::SetClassAsTypeInfo(HInstruction* instr, |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 513 | ObjPtr<mirror::Class> klass, |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 514 | bool is_exact) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 515 | if (instr->IsInvokeStaticOrDirect() && instr->AsInvokeStaticOrDirect()->IsStringInit()) { |
| 516 | // Calls to String.<init> are replaced with a StringFactory. |
| 517 | if (kIsDebugBuild) { |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 518 | HInvokeStaticOrDirect* invoke = instr->AsInvokeStaticOrDirect(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 519 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 62977ff | 2016-04-20 15:06:31 +0100 | [diff] [blame] | 520 | Thread* self = Thread::Current(); |
| 521 | StackHandleScope<2> hs(self); |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 522 | const DexFile& dex_file = *invoke->GetTargetMethod().dex_file; |
Nicolas Geoffray | c89715c | 2015-10-28 12:06:25 +0000 | [diff] [blame] | 523 | Handle<mirror::DexCache> dex_cache( |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 524 | hs.NewHandle(FindDexCacheWithHint(self, dex_file, hint_dex_cache_))); |
Nicolas Geoffray | c89715c | 2015-10-28 12:06:25 +0000 | [diff] [blame] | 525 | // 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 Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 528 | Handle<mirror::ClassLoader> loader(hs.NewHandle<mirror::ClassLoader>(nullptr)); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 529 | ArtMethod* method = cl->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 530 | dex_file, invoke->GetDexMethodIndex(), dex_cache, loader, nullptr, kDirect); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 531 | DCHECK(method != nullptr); |
| 532 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
| 533 | DCHECK(declaring_class != nullptr); |
| 534 | DCHECK(declaring_class->IsStringClass()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 535 | << "Expected String class: " << declaring_class->PrettyDescriptor(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 536 | DCHECK(method->IsConstructor()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 537 | << "Expected String.<init>: " << method->PrettyMethod(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 538 | } |
| 539 | instr->SetReferenceTypeInfo( |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 540 | ReferenceTypeInfo::Create(handle_cache_->GetStringClassHandle(), /* is_exact */ true)); |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 541 | } else if (IsAdmissible(klass.Ptr())) { |
Aart Bik | f417ff4 | 2016-04-25 12:51:37 -0700 | [diff] [blame] | 542 | ReferenceTypeInfo::TypeHandle handle = handle_cache_->NewHandle(klass); |
| 543 | is_exact = is_exact || handle->CannotBeAssignedFromOtherTypes(); |
| 544 | instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(handle, is_exact)); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 545 | } else { |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 546 | instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti()); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 550 | void ReferenceTypePropagation::RTPVisitor::VisitDeoptimize(HDeoptimize* instr) { |
| 551 | BoundTypeForClassCheck(instr); |
| 552 | } |
| 553 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 554 | void ReferenceTypePropagation::RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 555 | dex::TypeIndex type_idx, |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 556 | const DexFile& dex_file, |
| 557 | bool is_exact) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 558 | DCHECK_EQ(instr->GetType(), DataType::Type::kReference); |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 559 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 560 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 561 | ObjPtr<mirror::DexCache> dex_cache = FindDexCacheWithHint(soa.Self(), dex_file, hint_dex_cache_); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 562 | ObjPtr<mirror::Class> klass = |
| 563 | ClassLinker::LookupResolvedType(type_idx, dex_cache, class_loader_.Get()); |
| 564 | SetClassAsTypeInfo(instr, klass, is_exact); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 567 | void ReferenceTypePropagation::RTPVisitor::VisitNewInstance(HNewInstance* instr) { |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 568 | ScopedObjectAccess soa(Thread::Current()); |
| 569 | SetClassAsTypeInfo(instr, instr->GetLoadClass()->GetClass().Get(), /* is_exact */ true); |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 572 | void ReferenceTypePropagation::RTPVisitor::VisitNewArray(HNewArray* instr) { |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 573 | ScopedObjectAccess soa(Thread::Current()); |
| 574 | SetClassAsTypeInfo(instr, instr->GetLoadClass()->GetClass().Get(), /* is_exact */ true); |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 575 | } |
| 576 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 577 | void ReferenceTypePropagation::RTPVisitor::VisitParameterValue(HParameterValue* instr) { |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 578 | // We check if the existing type is valid: the inliner may have set it. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 579 | if (instr->GetType() == DataType::Type::kReference && !instr->GetReferenceTypeInfo().IsValid()) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 580 | UpdateReferenceTypeInfo(instr, |
| 581 | instr->GetTypeIndex(), |
| 582 | instr->GetDexFile(), |
| 583 | /* is_exact */ false); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 587 | void ReferenceTypePropagation::RTPVisitor::UpdateFieldAccessTypeInfo(HInstruction* instr, |
| 588 | const FieldInfo& info) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 589 | if (instr->GetType() != DataType::Type::kReference) { |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 590 | return; |
| 591 | } |
| 592 | |
Vladimir Marko | 62977ff | 2016-04-20 15:06:31 +0100 | [diff] [blame] | 593 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 594 | ObjPtr<mirror::Class> klass; |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 595 | |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 596 | // The field is unknown only during tests. |
| 597 | if (info.GetField() != nullptr) { |
| 598 | klass = info.GetField()->GetType<false>(); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 601 | SetClassAsTypeInfo(instr, klass, /* is_exact */ false); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 602 | } |
| 603 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 604 | void ReferenceTypePropagation::RTPVisitor::VisitInstanceFieldGet(HInstanceFieldGet* instr) { |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 605 | UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo()); |
| 606 | } |
| 607 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 608 | void ReferenceTypePropagation::RTPVisitor::VisitStaticFieldGet(HStaticFieldGet* instr) { |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 609 | UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo()); |
| 610 | } |
| 611 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 612 | void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedInstanceFieldGet( |
| 613 | HUnresolvedInstanceFieldGet* instr) { |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 614 | // TODO: Use descriptor to get the actual type. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 615 | if (instr->GetFieldType() == DataType::Type::kReference) { |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 616 | instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti()); |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 620 | void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedStaticFieldGet( |
| 621 | HUnresolvedStaticFieldGet* instr) { |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 622 | // TODO: Use descriptor to get the actual type. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 623 | if (instr->GetFieldType() == DataType::Type::kReference) { |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 624 | instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti()); |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 628 | void ReferenceTypePropagation::RTPVisitor::VisitLoadClass(HLoadClass* instr) { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 629 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 630 | Handle<mirror::Class> resolved_class = instr->GetClass(); |
| 631 | if (IsAdmissible(resolved_class.Get())) { |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 632 | instr->SetLoadedClassRTI(ReferenceTypeInfo::Create( |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 633 | resolved_class, /* is_exact */ true)); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 634 | } |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 635 | instr->SetReferenceTypeInfo( |
| 636 | ReferenceTypeInfo::Create(handle_cache_->GetClassClassHandle(), /* is_exact */ true)); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 639 | void ReferenceTypePropagation::RTPVisitor::VisitClinitCheck(HClinitCheck* instr) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 640 | instr->SetReferenceTypeInfo(instr->InputAt(0)->GetReferenceTypeInfo()); |
| 641 | } |
| 642 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 643 | void ReferenceTypePropagation::RTPVisitor::VisitLoadString(HLoadString* instr) { |
| 644 | instr->SetReferenceTypeInfo( |
| 645 | ReferenceTypeInfo::Create(handle_cache_->GetStringClassHandle(), /* is_exact */ true)); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 648 | void ReferenceTypePropagation::RTPVisitor::VisitLoadException(HLoadException* instr) { |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 649 | DCHECK(instr->GetBlock()->IsCatchBlock()); |
| 650 | TryCatchInformation* catch_info = instr->GetBlock()->GetTryCatchInformation(); |
| 651 | |
| 652 | if (catch_info->IsCatchAllTypeIndex()) { |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 653 | instr->SetReferenceTypeInfo( |
| 654 | ReferenceTypeInfo::Create(handle_cache_->GetThrowableClassHandle(), /* is_exact */ false)); |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 655 | } else { |
| 656 | UpdateReferenceTypeInfo(instr, |
| 657 | catch_info->GetCatchTypeIndex(), |
| 658 | catch_info->GetCatchDexFile(), |
| 659 | /* is_exact */ false); |
| 660 | } |
| 661 | } |
| 662 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 663 | void ReferenceTypePropagation::RTPVisitor::VisitNullCheck(HNullCheck* instr) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 664 | ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
David Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 665 | if (parent_rti.IsValid()) { |
| 666 | instr->SetReferenceTypeInfo(parent_rti); |
| 667 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 670 | void ReferenceTypePropagation::RTPVisitor::VisitBoundType(HBoundType* instr) { |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 671 | ReferenceTypeInfo class_rti = instr->GetUpperBound(); |
| 672 | if (class_rti.IsValid()) { |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 673 | ScopedObjectAccess soa(Thread::Current()); |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 674 | // Narrow the type as much as possible. |
| 675 | HInstruction* obj = instr->InputAt(0); |
| 676 | ReferenceTypeInfo obj_rti = obj->GetReferenceTypeInfo(); |
Nicolas Geoffray | 8c3794c | 2016-12-20 09:25:42 +0000 | [diff] [blame] | 677 | if (class_rti.IsExact()) { |
| 678 | instr->SetReferenceTypeInfo(class_rti); |
David Brazdil | 744a1c6 | 2015-12-28 10:53:34 +0000 | [diff] [blame] | 679 | } 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 Geoffray | 8c3794c | 2016-12-20 09:25:42 +0000 | [diff] [blame] | 684 | // 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 Brazdil | 744a1c6 | 2015-12-28 10:53:34 +0000 | [diff] [blame] | 688 | instr->SetReferenceTypeInfo( |
| 689 | ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false)); |
| 690 | } |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 691 | } else { |
David Brazdil | 744a1c6 | 2015-12-28 10:53:34 +0000 | [diff] [blame] | 692 | // Object not typed yet. Leave BoundType untyped for now rather than |
| 693 | // assign the type conservatively. |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 694 | } |
| 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 Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 705 | void ReferenceTypePropagation::RTPVisitor::VisitCheckCast(HCheckCast* check_cast) { |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 706 | HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass(); |
| 707 | ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI(); |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 708 | 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 Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 713 | } |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 714 | DCHECK_EQ(bound_type->InputAt(0), check_cast->InputAt(0)); |
| 715 | |
| 716 | if (class_rti.IsValid()) { |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 717 | DCHECK(is_first_run_); |
Nicolas Geoffray | 8c3794c | 2016-12-20 09:25:42 +0000 | [diff] [blame] | 718 | ScopedObjectAccess soa(Thread::Current()); |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 719 | // This is the first run of RTP and class is resolved. |
Nicolas Geoffray | 8c3794c | 2016-12-20 09:25:42 +0000 | [diff] [blame] | 720 | 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 Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 723 | } 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 Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 731 | void ReferenceTypePropagation::VisitPhi(HPhi* phi) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 732 | if (phi->IsDead() || phi->GetType() != DataType::Type::kReference) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 733 | return; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 734 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 735 | |
| 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 Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 739 | 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 Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 745 | AddToWorklist(phi); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 746 | } else { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 747 | // 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 Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 757 | void 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 Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 771 | ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a, |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 772 | const ReferenceTypeInfo& b, |
| 773 | HandleCache* handle_cache) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 774 | if (!b.IsValid()) { |
| 775 | return a; |
| 776 | } |
| 777 | if (!a.IsValid()) { |
| 778 | return b; |
Calin Juravle | 20e6071 | 2015-07-01 18:41:04 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 781 | bool is_exact = a.IsExact() && b.IsExact(); |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 782 | 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 Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 787 | |
| 788 | if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) { |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 789 | result_type_handle = a_type_handle; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 790 | } else if (a.IsSupertypeOf(b)) { |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 791 | result_type_handle = a_type_handle; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 792 | is_exact = false; |
| 793 | } else if (b.IsSupertypeOf(a)) { |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 794 | result_type_handle = b_type_handle; |
| 795 | is_exact = false; |
| 796 | } else if (!a_is_interface && !b_is_interface) { |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 797 | result_type_handle = |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 798 | handle_cache->NewHandle(a_type_handle->GetCommonSuperClass(b_type_handle)); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 799 | is_exact = false; |
| 800 | } else { |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 801 | // 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 Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 808 | result_type_handle = handle_cache->GetObjectClassHandle(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 809 | is_exact = false; |
| 810 | } |
| 811 | |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 812 | return ReferenceTypeInfo::Create(result_type_handle, is_exact); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 815 | void ReferenceTypePropagation::UpdateArrayGet(HArrayGet* instr, HandleCache* handle_cache) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 816 | DCHECK_EQ(DataType::Type::kReference, instr->GetType()); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 817 | |
| 818 | ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
David Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 819 | if (!parent_rti.IsValid()) { |
| 820 | return; |
| 821 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 822 | |
| 823 | Handle<mirror::Class> handle = parent_rti.GetTypeHandle(); |
Aart Bik | f417ff4 | 2016-04-25 12:51:37 -0700 | [diff] [blame] | 824 | if (handle->IsObjectArrayClass() && IsAdmissible(handle->GetComponentType())) { |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 825 | ReferenceTypeInfo::TypeHandle component_handle = |
| 826 | handle_cache->NewHandle(handle->GetComponentType()); |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 827 | bool is_exact = component_handle->CannotBeAssignedFromOtherTypes(); |
| 828 | instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(component_handle, is_exact)); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 829 | } else { |
| 830 | // We don't know what the parent actually is, so we fallback to object. |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 831 | instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti()); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 832 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | bool 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 Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 843 | } 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 Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 849 | // TODO: consider if it's worth "looking back" and binding the input object |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 850 | // to an array type. |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 851 | UpdateArrayGet(instr->AsArrayGet(), &handle_cache_); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 852 | } else { |
| 853 | LOG(FATAL) << "Invalid instruction (should not get here)"; |
| 854 | } |
| 855 | |
| 856 | return !previous_rti.IsEqual(instr->GetReferenceTypeInfo()); |
| 857 | } |
| 858 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 859 | void ReferenceTypePropagation::RTPVisitor::VisitInvoke(HInvoke* instr) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 860 | if (instr->GetType() != DataType::Type::kReference) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 861 | return; |
| 862 | } |
| 863 | |
| 864 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 865 | ArtMethod* method = instr->GetResolvedMethod(); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 866 | ObjPtr<mirror::Class> klass = (method == nullptr) ? nullptr : method->LookupResolvedReturnType(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 867 | SetClassAsTypeInfo(instr, klass, /* is_exact */ false); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 868 | } |
| 869 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 870 | void ReferenceTypePropagation::RTPVisitor::VisitArrayGet(HArrayGet* instr) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 871 | if (instr->GetType() != DataType::Type::kReference) { |
Guillaume "Vermeille" Sanchez | 72a5eb5 | 2015-06-02 17:39:45 +0100 | [diff] [blame] | 872 | return; |
| 873 | } |
David Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 874 | |
Guillaume "Vermeille" Sanchez | 72a5eb5 | 2015-06-02 17:39:45 +0100 | [diff] [blame] | 875 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 876 | UpdateArrayGet(instr, handle_cache_); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 877 | if (!instr->GetReferenceTypeInfo().IsValid()) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 878 | worklist_->push_back(instr); |
Guillaume "Vermeille" Sanchez | 72a5eb5 | 2015-06-02 17:39:45 +0100 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 882 | void ReferenceTypePropagation::UpdateBoundType(HBoundType* instr) { |
Nicolas Geoffray | 8c3794c | 2016-12-20 09:25:42 +0000 | [diff] [blame] | 883 | ReferenceTypeInfo input_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
| 884 | if (!input_rti.IsValid()) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 885 | return; // No new info yet. |
| 886 | } |
| 887 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 888 | ReferenceTypeInfo upper_bound_rti = instr->GetUpperBound(); |
Nicolas Geoffray | 8c3794c | 2016-12-20 09:25:42 +0000 | [diff] [blame] | 889 | 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 Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 901 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 904 | // 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 Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 906 | void ReferenceTypePropagation::UpdatePhi(HPhi* instr) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 907 | DCHECK(instr->IsLive()); |
| 908 | |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 909 | HInputsRef inputs = instr->GetInputs(); |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 910 | size_t first_input_index_not_null = 0; |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 911 | while (first_input_index_not_null < inputs.size() && |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 912 | inputs[first_input_index_not_null]->IsNullConstant()) { |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 913 | first_input_index_not_null++; |
| 914 | } |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 915 | if (first_input_index_not_null == inputs.size()) { |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 916 | // All inputs are NullConstants, set the type to object. |
| 917 | // This may happen in the presence of inlining. |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 918 | instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti()); |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 919 | return; |
| 920 | } |
| 921 | |
| 922 | ReferenceTypeInfo new_rti = instr->InputAt(first_input_index_not_null)->GetReferenceTypeInfo(); |
| 923 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 924 | if (new_rti.IsValid() && new_rti.IsObjectClass() && !new_rti.IsExact()) { |
| 925 | // Early return if we are Object and inexact. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 926 | instr->SetReferenceTypeInfo(new_rti); |
| 927 | return; |
| 928 | } |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 929 | |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 930 | for (size_t i = first_input_index_not_null + 1; i < inputs.size(); i++) { |
| 931 | if (inputs[i]->IsNullConstant()) { |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 932 | continue; |
| 933 | } |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 934 | new_rti = MergeTypes(new_rti, inputs[i]->GetReferenceTypeInfo(), &handle_cache_); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 935 | if (new_rti.IsValid() && new_rti.IsObjectClass()) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 936 | if (!new_rti.IsExact()) { |
| 937 | break; |
| 938 | } else { |
| 939 | continue; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 940 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 941 | } |
| 942 | } |
David Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 943 | |
| 944 | if (new_rti.IsValid()) { |
| 945 | instr->SetReferenceTypeInfo(new_rti); |
| 946 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | // Re-computes and updates the nullability of the instruction. Returns whether or |
| 950 | // not the nullability was changed. |
| 951 | bool ReferenceTypePropagation::UpdateNullability(HInstruction* instr) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 952 | DCHECK((instr->IsPhi() && instr->AsPhi()->IsLive()) |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 953 | || instr->IsBoundType() |
| 954 | || instr->IsNullCheck() |
| 955 | || instr->IsArrayGet()); |
| 956 | |
| 957 | if (!instr->IsPhi() && !instr->IsBoundType()) { |
| 958 | return false; |
| 959 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 960 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 961 | bool existing_can_be_null = instr->CanBeNull(); |
| 962 | if (instr->IsPhi()) { |
| 963 | HPhi* phi = instr->AsPhi(); |
| 964 | bool new_can_be_null = false; |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 965 | for (HInstruction* input : phi->GetInputs()) { |
| 966 | if (input->CanBeNull()) { |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 967 | 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 Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 975 | } |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 976 | return existing_can_be_null != instr->CanBeNull(); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void ReferenceTypePropagation::ProcessWorklist() { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 980 | while (!worklist_.empty()) { |
| 981 | HInstruction* instruction = worklist_.back(); |
| 982 | worklist_.pop_back(); |
Calin Juravle | 1261759 | 2015-10-16 16:28:46 +0100 | [diff] [blame] | 983 | bool updated_nullability = UpdateNullability(instruction); |
| 984 | bool updated_reference_type = UpdateReferenceTypeInfo(instruction); |
| 985 | if (updated_nullability || updated_reference_type) { |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 986 | AddDependentInstructionsToWorklist(instruction); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 991 | void ReferenceTypePropagation::AddToWorklist(HInstruction* instruction) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 992 | DCHECK_EQ(instruction->GetType(), DataType::Type::kReference) |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 993 | << instruction->DebugName() << ":" << instruction->GetType(); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 994 | worklist_.push_back(instruction); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 997 | void ReferenceTypePropagation::AddDependentInstructionsToWorklist(HInstruction* instruction) { |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 998 | for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) { |
| 999 | HInstruction* user = use.GetUser(); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 1000 | if ((user->IsPhi() && user->AsPhi()->IsLive()) |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1001 | || user->IsBoundType() |
| 1002 | || user->IsNullCheck() |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1003 | || (user->IsArrayGet() && (user->GetType() == DataType::Type::kReference))) { |
Calin Juravle | beba930 | 2015-07-08 15:57:18 +0000 | [diff] [blame] | 1004 | AddToWorklist(user); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | } |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 1008 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1009 | } // namespace art |