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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "class_linker-inl.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 20 | #include "mirror/class-inl.h" |
| 21 | #include "mirror/dex_cache.h" |
| 22 | #include "scoped_thread_state_change.h" |
| 23 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 26 | static inline mirror::DexCache* FindDexCacheWithHint(Thread* self, |
| 27 | const DexFile& dex_file, |
| 28 | Handle<mirror::DexCache> hint_dex_cache) |
| 29 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 30 | if (LIKELY(hint_dex_cache->GetDexFile() == &dex_file)) { |
| 31 | return hint_dex_cache.Get(); |
| 32 | } else { |
| 33 | return Runtime::Current()->GetClassLinker()->FindDexCache(self, dex_file); |
| 34 | } |
| 35 | } |
| 36 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 37 | static inline ReferenceTypeInfo::TypeHandle GetRootHandle(StackHandleScopeCollection* handles, |
| 38 | ClassLinker::ClassRoot class_root, |
| 39 | ReferenceTypeInfo::TypeHandle* cache) { |
| 40 | if (!ReferenceTypeInfo::IsValidHandle(*cache)) { |
| 41 | // Mutator lock is required for NewHandle. |
| 42 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 43 | ScopedObjectAccess soa(Thread::Current()); |
| 44 | *cache = handles->NewHandle(linker->GetClassRoot(class_root)); |
| 45 | } |
| 46 | return *cache; |
| 47 | } |
| 48 | |
Aart Bik | f417ff4 | 2016-04-25 12:51:37 -0700 | [diff] [blame] | 49 | // Returns true if klass is admissible to the propagation: non-null and non-erroneous. |
| 50 | // For an array type, we also check if the component type is admissible. |
| 51 | static bool IsAdmissible(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 52 | return klass != nullptr && !klass->IsErroneous() && |
| 53 | (!klass->IsArrayClass() || IsAdmissible(klass->GetComponentType())); |
| 54 | } |
| 55 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 56 | ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetObjectClassHandle() { |
| 57 | return GetRootHandle(handles_, ClassLinker::kJavaLangObject, &object_class_handle_); |
| 58 | } |
| 59 | |
| 60 | ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetClassClassHandle() { |
| 61 | return GetRootHandle(handles_, ClassLinker::kJavaLangClass, &class_class_handle_); |
| 62 | } |
| 63 | |
| 64 | ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetStringClassHandle() { |
| 65 | return GetRootHandle(handles_, ClassLinker::kJavaLangString, &string_class_handle_); |
| 66 | } |
| 67 | |
| 68 | ReferenceTypeInfo::TypeHandle ReferenceTypePropagation::HandleCache::GetThrowableClassHandle() { |
| 69 | return GetRootHandle(handles_, ClassLinker::kJavaLangThrowable, &throwable_class_handle_); |
| 70 | } |
| 71 | |
| 72 | class ReferenceTypePropagation::RTPVisitor : public HGraphDelegateVisitor { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 73 | public: |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 74 | RTPVisitor(HGraph* graph, |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 75 | Handle<mirror::DexCache> hint_dex_cache, |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 76 | HandleCache* handle_cache, |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 77 | ArenaVector<HInstruction*>* worklist, |
| 78 | bool is_first_run) |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 79 | : HGraphDelegateVisitor(graph), |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 80 | hint_dex_cache_(hint_dex_cache), |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 81 | handle_cache_(handle_cache), |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 82 | worklist_(worklist), |
| 83 | is_first_run_(is_first_run) {} |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 84 | |
| 85 | void VisitNewInstance(HNewInstance* new_instance) OVERRIDE; |
| 86 | void VisitLoadClass(HLoadClass* load_class) OVERRIDE; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 87 | void VisitClinitCheck(HClinitCheck* clinit_check) OVERRIDE; |
| 88 | void VisitLoadString(HLoadString* instr) OVERRIDE; |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 89 | void VisitLoadException(HLoadException* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 90 | void VisitNewArray(HNewArray* instr) OVERRIDE; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 91 | void VisitParameterValue(HParameterValue* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 92 | void UpdateFieldAccessTypeInfo(HInstruction* instr, const FieldInfo& info); |
Vladimir Marko | 62977ff | 2016-04-20 15:06:31 +0100 | [diff] [blame] | 93 | void SetClassAsTypeInfo(HInstruction* instr, mirror::Class* klass, bool is_exact) |
| 94 | SHARED_REQUIRES(Locks::mutator_lock_); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 95 | void VisitInstanceFieldGet(HInstanceFieldGet* instr) OVERRIDE; |
| 96 | void VisitStaticFieldGet(HStaticFieldGet* instr) OVERRIDE; |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 97 | void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* instr) OVERRIDE; |
| 98 | void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 99 | void VisitInvoke(HInvoke* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | 72a5eb5 | 2015-06-02 17:39:45 +0100 | [diff] [blame] | 100 | void VisitArrayGet(HArrayGet* instr) OVERRIDE; |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 101 | void VisitCheckCast(HCheckCast* instr) OVERRIDE; |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 102 | void VisitBoundType(HBoundType* instr) OVERRIDE; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 103 | void VisitNullCheck(HNullCheck* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 104 | void UpdateReferenceTypeInfo(HInstruction* instr, |
| 105 | uint16_t type_idx, |
| 106 | const DexFile& dex_file, |
| 107 | bool is_exact); |
| 108 | |
| 109 | private: |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 110 | Handle<mirror::DexCache> hint_dex_cache_; |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 111 | HandleCache* handle_cache_; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 112 | ArenaVector<HInstruction*>* worklist_; |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 113 | const bool is_first_run_; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 116 | ReferenceTypePropagation::ReferenceTypePropagation(HGraph* graph, |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 117 | Handle<mirror::DexCache> hint_dex_cache, |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 118 | StackHandleScopeCollection* 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 | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 122 | hint_dex_cache_(hint_dex_cache), |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 123 | handle_cache_(handles), |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 124 | worklist_(graph->GetArena()->Adapter(kArenaAllocReferenceTypePropagation)), |
| 125 | is_first_run_(is_first_run) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 128 | void ReferenceTypePropagation::ValidateTypes() { |
| 129 | // TODO: move this to the graph checker. |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 130 | if (kIsDebugBuild) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 131 | ScopedObjectAccess soa(Thread::Current()); |
| 132 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 133 | HBasicBlock* block = it.Current(); |
| 134 | for (HInstructionIterator iti(block->GetInstructions()); !iti.Done(); iti.Advance()) { |
| 135 | HInstruction* instr = iti.Current(); |
| 136 | if (instr->GetType() == Primitive::kPrimNot) { |
| 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 | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 157 | RTPVisitor visitor(graph_, hint_dex_cache_, &handle_cache_, &worklist_, is_first_run_); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 158 | instruction->Accept(&visitor); |
| 159 | } |
| 160 | |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 161 | void ReferenceTypePropagation::Run() { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 162 | worklist_.reserve(kDefaultWorklistSize); |
| 163 | |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 164 | // To properly propagate type info we need to visit in the dominator-based order. |
| 165 | // Reverse post order guarantees a node's dominators are visited first. |
| 166 | // We take advantage of this order in `VisitBasicBlock`. |
| 167 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 168 | VisitBasicBlock(it.Current()); |
| 169 | } |
| 170 | |
| 171 | ProcessWorklist(); |
| 172 | ValidateTypes(); |
| 173 | } |
| 174 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 175 | void ReferenceTypePropagation::VisitBasicBlock(HBasicBlock* block) { |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 176 | RTPVisitor visitor(graph_, hint_dex_cache_, &handle_cache_, &worklist_, is_first_run_); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 177 | // Handle Phis first as there might be instructions in the same block who depend on them. |
| 178 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 179 | VisitPhi(it.Current()->AsPhi()); |
| 180 | } |
Calin Juravle | beba930 | 2015-07-08 15:57:18 +0000 | [diff] [blame] | 181 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 182 | // Handle instructions. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 183 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 184 | HInstruction* instr = it.Current(); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 185 | instr->Accept(&visitor); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 188 | // Add extra nodes to bound types. |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 189 | BoundTypeForIfNotNull(block); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 190 | BoundTypeForIfInstanceOf(block); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 193 | // Check if we should create a bound type for the given object at the specified |
| 194 | // position. Because of inlining and the fact we run RTP more than once and we |
| 195 | // might have a HBoundType already. If we do, we should not create a new one. |
| 196 | // In this case we also assert that there are no other uses of the object (except |
| 197 | // the bound type) dominated by the specified dominator_instr or dominator_block. |
| 198 | static bool ShouldCreateBoundType(HInstruction* position, |
| 199 | HInstruction* obj, |
| 200 | ReferenceTypeInfo upper_bound, |
| 201 | HInstruction* dominator_instr, |
| 202 | HBasicBlock* dominator_block) |
| 203 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 204 | // If the position where we should insert the bound type is not already a |
| 205 | // a bound type then we need to create one. |
| 206 | if (position == nullptr || !position->IsBoundType()) { |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | HBoundType* existing_bound_type = position->AsBoundType(); |
| 211 | if (existing_bound_type->GetUpperBound().IsSupertypeOf(upper_bound)) { |
| 212 | if (kIsDebugBuild) { |
| 213 | // Check that the existing HBoundType dominates all the uses. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 214 | for (const HUseListNode<HInstruction*>& use : obj->GetUses()) { |
| 215 | HInstruction* user = use.GetUser(); |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 216 | if (dominator_instr != nullptr) { |
| 217 | DCHECK(!dominator_instr->StrictlyDominates(user) |
| 218 | || user == existing_bound_type |
| 219 | || existing_bound_type->StrictlyDominates(user)); |
| 220 | } else if (dominator_block != nullptr) { |
| 221 | DCHECK(!dominator_block->Dominates(user->GetBlock()) |
| 222 | || user == existing_bound_type |
| 223 | || existing_bound_type->StrictlyDominates(user)); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } else { |
| 228 | // TODO: if the current bound type is a refinement we could update the |
| 229 | // existing_bound_type with the a new upper limit. However, we also need to |
| 230 | // update its users and have access to the work list. |
| 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 235 | void ReferenceTypePropagation::BoundTypeForIfNotNull(HBasicBlock* block) { |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 236 | HIf* ifInstruction = block->GetLastInstruction()->AsIf(); |
| 237 | if (ifInstruction == nullptr) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 238 | return; |
| 239 | } |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 240 | HInstruction* ifInput = ifInstruction->InputAt(0); |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 241 | if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) { |
| 242 | return; |
| 243 | } |
| 244 | HInstruction* input0 = ifInput->InputAt(0); |
| 245 | HInstruction* input1 = ifInput->InputAt(1); |
Calin Juravle | edad8ad | 2015-04-23 14:34:33 +0100 | [diff] [blame] | 246 | HInstruction* obj = nullptr; |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 247 | |
Calin Juravle | edad8ad | 2015-04-23 14:34:33 +0100 | [diff] [blame] | 248 | if (input1->IsNullConstant()) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 249 | obj = input0; |
Calin Juravle | edad8ad | 2015-04-23 14:34:33 +0100 | [diff] [blame] | 250 | } else if (input0->IsNullConstant()) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 251 | obj = input1; |
| 252 | } else { |
| 253 | return; |
| 254 | } |
| 255 | |
Nicolas Geoffray | 7d5ea03 | 2015-07-02 15:48:27 +0100 | [diff] [blame] | 256 | if (!obj->CanBeNull() || obj->IsNullConstant()) { |
| 257 | // Null check is dead code and will be removed by DCE. |
| 258 | return; |
| 259 | } |
| 260 | DCHECK(!obj->IsLoadClass()) << "We should not replace HLoadClass instructions"; |
| 261 | |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 262 | // We only need to bound the type if we have uses in the relevant block. |
| 263 | // So start with null and create the HBoundType lazily, only if it's needed. |
| 264 | HBoundType* bound_type = nullptr; |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 265 | HBasicBlock* notNullBlock = ifInput->IsNotEqual() |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 266 | ? ifInstruction->IfTrueSuccessor() |
| 267 | : ifInstruction->IfFalseSuccessor(); |
| 268 | |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 269 | const HUseList<HInstruction*>& uses = obj->GetUses(); |
| 270 | for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) { |
| 271 | HInstruction* user = it->GetUser(); |
| 272 | size_t index = it->GetIndex(); |
| 273 | // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput(). |
| 274 | ++it; |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 275 | if (notNullBlock->Dominates(user->GetBlock())) { |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 276 | if (bound_type == nullptr) { |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 277 | ScopedObjectAccess soa(Thread::Current()); |
| 278 | HInstruction* insert_point = notNullBlock->GetFirstInstruction(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 279 | ReferenceTypeInfo object_rti = ReferenceTypeInfo::Create( |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 280 | handle_cache_.GetObjectClassHandle(), /* is_exact */ true); |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 281 | if (ShouldCreateBoundType(insert_point, obj, object_rti, nullptr, notNullBlock)) { |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 282 | bound_type = new (graph_->GetArena()) HBoundType(obj); |
| 283 | bound_type->SetUpperBound(object_rti, /* bound_can_be_null */ false); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 284 | if (obj->GetReferenceTypeInfo().IsValid()) { |
| 285 | bound_type->SetReferenceTypeInfo(obj->GetReferenceTypeInfo()); |
| 286 | } |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 287 | notNullBlock->InsertInstructionBefore(bound_type, insert_point); |
| 288 | } else { |
| 289 | // We already have a bound type on the position we would need to insert |
| 290 | // the new one. The existing bound type should dominate all the users |
| 291 | // (dchecked) so there's no need to continue. |
| 292 | break; |
| 293 | } |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 294 | } |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 295 | user->ReplaceInput(bound_type, index); |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 300 | // Returns true if one of the patterns below has been recognized. If so, the |
| 301 | // InstanceOf instruction together with the true branch of `ifInstruction` will |
| 302 | // be returned using the out parameters. |
| 303 | // Recognized patterns: |
| 304 | // (1) patterns equivalent to `if (obj instanceof X)` |
| 305 | // (a) InstanceOf -> Equal to 1 -> If |
| 306 | // (b) InstanceOf -> NotEqual to 0 -> If |
| 307 | // (c) InstanceOf -> If |
| 308 | // (2) patterns equivalent to `if (!(obj instanceof X))` |
| 309 | // (a) InstanceOf -> Equal to 0 -> If |
| 310 | // (b) InstanceOf -> NotEqual to 1 -> If |
| 311 | // (c) InstanceOf -> BooleanNot -> If |
| 312 | static bool MatchIfInstanceOf(HIf* ifInstruction, |
| 313 | /* out */ HInstanceOf** instanceOf, |
| 314 | /* out */ HBasicBlock** trueBranch) { |
| 315 | HInstruction* input = ifInstruction->InputAt(0); |
| 316 | |
| 317 | if (input->IsEqual()) { |
| 318 | HInstruction* rhs = input->AsEqual()->GetConstantRight(); |
| 319 | if (rhs != nullptr) { |
| 320 | HInstruction* lhs = input->AsEqual()->GetLeastConstantLeft(); |
| 321 | if (lhs->IsInstanceOf() && rhs->IsIntConstant()) { |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 322 | if (rhs->AsIntConstant()->IsTrue()) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 323 | // Case (1a) |
| 324 | *trueBranch = ifInstruction->IfTrueSuccessor(); |
| 325 | } else { |
| 326 | // Case (2a) |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 327 | DCHECK(rhs->AsIntConstant()->IsFalse()) << rhs->AsIntConstant()->GetValue(); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 328 | *trueBranch = ifInstruction->IfFalseSuccessor(); |
| 329 | } |
| 330 | *instanceOf = lhs->AsInstanceOf(); |
| 331 | return true; |
| 332 | } |
| 333 | } |
| 334 | } else if (input->IsNotEqual()) { |
| 335 | HInstruction* rhs = input->AsNotEqual()->GetConstantRight(); |
| 336 | if (rhs != nullptr) { |
| 337 | HInstruction* lhs = input->AsNotEqual()->GetLeastConstantLeft(); |
| 338 | if (lhs->IsInstanceOf() && rhs->IsIntConstant()) { |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 339 | if (rhs->AsIntConstant()->IsFalse()) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 340 | // Case (1b) |
| 341 | *trueBranch = ifInstruction->IfTrueSuccessor(); |
| 342 | } else { |
| 343 | // Case (2b) |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 344 | DCHECK(rhs->AsIntConstant()->IsTrue()) << rhs->AsIntConstant()->GetValue(); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 345 | *trueBranch = ifInstruction->IfFalseSuccessor(); |
| 346 | } |
| 347 | *instanceOf = lhs->AsInstanceOf(); |
| 348 | return true; |
| 349 | } |
| 350 | } |
| 351 | } else if (input->IsInstanceOf()) { |
| 352 | // Case (1c) |
| 353 | *instanceOf = input->AsInstanceOf(); |
| 354 | *trueBranch = ifInstruction->IfTrueSuccessor(); |
| 355 | return true; |
| 356 | } else if (input->IsBooleanNot()) { |
| 357 | HInstruction* not_input = input->InputAt(0); |
| 358 | if (not_input->IsInstanceOf()) { |
| 359 | // Case (2c) |
| 360 | *instanceOf = not_input->AsInstanceOf(); |
| 361 | *trueBranch = ifInstruction->IfFalseSuccessor(); |
| 362 | return true; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return false; |
| 367 | } |
| 368 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 369 | // Detects if `block` is the True block for the pattern |
| 370 | // `if (x instanceof ClassX) { }` |
| 371 | // If that's the case insert an HBoundType instruction to bound the type of `x` |
| 372 | // to `ClassX` in the scope of the dominated blocks. |
| 373 | void ReferenceTypePropagation::BoundTypeForIfInstanceOf(HBasicBlock* block) { |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 374 | HIf* ifInstruction = block->GetLastInstruction()->AsIf(); |
| 375 | if (ifInstruction == nullptr) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 376 | return; |
| 377 | } |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 378 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 379 | // Try to recognize common `if (instanceof)` and `if (!instanceof)` patterns. |
| 380 | HInstanceOf* instanceOf = nullptr; |
| 381 | HBasicBlock* instanceOfTrueBlock = nullptr; |
| 382 | if (!MatchIfInstanceOf(ifInstruction, &instanceOf, &instanceOfTrueBlock)) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 383 | return; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 386 | HLoadClass* load_class = instanceOf->InputAt(1)->AsLoadClass(); |
| 387 | ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI(); |
| 388 | { |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 389 | if (!class_rti.IsValid()) { |
| 390 | // He have loaded an unresolved class. Don't bother bounding the type. |
| 391 | return; |
| 392 | } |
| 393 | } |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 394 | // We only need to bound the type if we have uses in the relevant block. |
| 395 | // So start with null and create the HBoundType lazily, only if it's needed. |
| 396 | HBoundType* bound_type = nullptr; |
| 397 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 398 | HInstruction* obj = instanceOf->InputAt(0); |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 399 | if (obj->GetReferenceTypeInfo().IsExact() && !obj->IsPhi()) { |
| 400 | // This method is being called while doing a fixed-point calculation |
| 401 | // over phis. Non-phis instruction whose type is already known do |
| 402 | // not need to be bound to another type. |
| 403 | // Not that this also prevents replacing `HLoadClass` with a `HBoundType`. |
| 404 | // `HCheckCast` and `HInstanceOf` expect a `HLoadClass` as a second |
| 405 | // input. |
| 406 | return; |
| 407 | } |
Nicolas Geoffray | 7d5ea03 | 2015-07-02 15:48:27 +0100 | [diff] [blame] | 408 | DCHECK(!obj->IsLoadClass()) << "We should not replace HLoadClass instructions"; |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 409 | const HUseList<HInstruction*>& uses = obj->GetUses(); |
| 410 | for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) { |
| 411 | HInstruction* user = it->GetUser(); |
| 412 | size_t index = it->GetIndex(); |
| 413 | // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput(). |
| 414 | ++it; |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 415 | if (instanceOfTrueBlock->Dominates(user->GetBlock())) { |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 416 | if (bound_type == nullptr) { |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 417 | ScopedObjectAccess soa(Thread::Current()); |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 418 | HInstruction* insert_point = instanceOfTrueBlock->GetFirstInstruction(); |
| 419 | if (ShouldCreateBoundType(insert_point, obj, class_rti, nullptr, instanceOfTrueBlock)) { |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 420 | bound_type = new (graph_->GetArena()) HBoundType(obj); |
| 421 | bound_type->SetUpperBound(class_rti, /* InstanceOf fails for null. */ false); |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 422 | instanceOfTrueBlock->InsertInstructionBefore(bound_type, insert_point); |
| 423 | } else { |
| 424 | // We already have a bound type on the position we would need to insert |
| 425 | // the new one. The existing bound type should dominate all the users |
| 426 | // (dchecked) so there's no need to continue. |
| 427 | break; |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 428 | } |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 429 | } |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 430 | user->ReplaceInput(bound_type, index); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 435 | void ReferenceTypePropagation::RTPVisitor::SetClassAsTypeInfo(HInstruction* instr, |
| 436 | mirror::Class* klass, |
| 437 | bool is_exact) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 438 | if (instr->IsInvokeStaticOrDirect() && instr->AsInvokeStaticOrDirect()->IsStringInit()) { |
| 439 | // Calls to String.<init> are replaced with a StringFactory. |
| 440 | if (kIsDebugBuild) { |
Nicolas Geoffray | c89715c | 2015-10-28 12:06:25 +0000 | [diff] [blame] | 441 | HInvoke* invoke = instr->AsInvoke(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 442 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 62977ff | 2016-04-20 15:06:31 +0100 | [diff] [blame] | 443 | Thread* self = Thread::Current(); |
| 444 | StackHandleScope<2> hs(self); |
Nicolas Geoffray | c89715c | 2015-10-28 12:06:25 +0000 | [diff] [blame] | 445 | Handle<mirror::DexCache> dex_cache( |
Vladimir Marko | 62977ff | 2016-04-20 15:06:31 +0100 | [diff] [blame] | 446 | hs.NewHandle(FindDexCacheWithHint(self, invoke->GetDexFile(), hint_dex_cache_))); |
Nicolas Geoffray | c89715c | 2015-10-28 12:06:25 +0000 | [diff] [blame] | 447 | // Use a null loader. We should probably use the compiling method's class loader, |
| 448 | // but then we would need to pass it to RTPVisitor just for this debug check. Since |
| 449 | // the method is from the String class, the null loader is good enough. |
| 450 | Handle<mirror::ClassLoader> loader; |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 451 | ArtMethod* method = cl->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
Nicolas Geoffray | c89715c | 2015-10-28 12:06:25 +0000 | [diff] [blame] | 452 | invoke->GetDexFile(), invoke->GetDexMethodIndex(), dex_cache, loader, nullptr, kDirect); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 453 | DCHECK(method != nullptr); |
| 454 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
| 455 | DCHECK(declaring_class != nullptr); |
| 456 | DCHECK(declaring_class->IsStringClass()) |
| 457 | << "Expected String class: " << PrettyDescriptor(declaring_class); |
| 458 | DCHECK(method->IsConstructor()) |
| 459 | << "Expected String.<init>: " << PrettyMethod(method); |
| 460 | } |
| 461 | instr->SetReferenceTypeInfo( |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 462 | ReferenceTypeInfo::Create(handle_cache_->GetStringClassHandle(), /* is_exact */ true)); |
Aart Bik | f417ff4 | 2016-04-25 12:51:37 -0700 | [diff] [blame] | 463 | } else if (IsAdmissible(klass)) { |
| 464 | ReferenceTypeInfo::TypeHandle handle = handle_cache_->NewHandle(klass); |
| 465 | is_exact = is_exact || handle->CannotBeAssignedFromOtherTypes(); |
| 466 | instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(handle, is_exact)); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 467 | } else { |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 468 | instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti()); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 472 | void ReferenceTypePropagation::RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr, |
| 473 | uint16_t type_idx, |
| 474 | const DexFile& dex_file, |
| 475 | bool is_exact) { |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 476 | DCHECK_EQ(instr->GetType(), Primitive::kPrimNot); |
| 477 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 478 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 479 | mirror::DexCache* dex_cache = FindDexCacheWithHint(soa.Self(), dex_file, hint_dex_cache_); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 480 | // Get type from dex cache assuming it was populated by the verifier. |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 481 | SetClassAsTypeInfo(instr, dex_cache->GetResolvedType(type_idx), is_exact); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 484 | void ReferenceTypePropagation::RTPVisitor::VisitNewInstance(HNewInstance* instr) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 485 | UpdateReferenceTypeInfo(instr, instr->GetTypeIndex(), instr->GetDexFile(), /* is_exact */ true); |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 486 | } |
| 487 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 488 | void ReferenceTypePropagation::RTPVisitor::VisitNewArray(HNewArray* instr) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 489 | UpdateReferenceTypeInfo(instr, instr->GetTypeIndex(), instr->GetDexFile(), /* is_exact */ true); |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 492 | static mirror::Class* GetClassFromDexCache(Thread* self, |
| 493 | const DexFile& dex_file, |
| 494 | uint16_t type_idx, |
| 495 | Handle<mirror::DexCache> hint_dex_cache) |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 496 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 497 | mirror::DexCache* dex_cache = FindDexCacheWithHint(self, dex_file, hint_dex_cache); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 498 | // Get type from dex cache assuming it was populated by the verifier. |
| 499 | return dex_cache->GetResolvedType(type_idx); |
| 500 | } |
| 501 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 502 | void ReferenceTypePropagation::RTPVisitor::VisitParameterValue(HParameterValue* instr) { |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 503 | // We check if the existing type is valid: the inliner may have set it. |
| 504 | if (instr->GetType() == Primitive::kPrimNot && !instr->GetReferenceTypeInfo().IsValid()) { |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 505 | ScopedObjectAccess soa(Thread::Current()); |
| 506 | mirror::Class* resolved_class = GetClassFromDexCache(soa.Self(), |
| 507 | instr->GetDexFile(), |
| 508 | instr->GetTypeIndex(), |
| 509 | hint_dex_cache_); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 510 | SetClassAsTypeInfo(instr, resolved_class, /* is_exact */ false); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 514 | void ReferenceTypePropagation::RTPVisitor::UpdateFieldAccessTypeInfo(HInstruction* instr, |
| 515 | const FieldInfo& info) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 516 | if (instr->GetType() != Primitive::kPrimNot) { |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 517 | return; |
| 518 | } |
| 519 | |
Vladimir Marko | 62977ff | 2016-04-20 15:06:31 +0100 | [diff] [blame] | 520 | ScopedObjectAccess soa(Thread::Current()); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 521 | mirror::Class* klass = nullptr; |
| 522 | |
| 523 | // The field index is unknown only during tests. |
| 524 | if (info.GetFieldIndex() != kUnknownFieldIndex) { |
| 525 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 526 | ArtField* field = cl->GetResolvedField(info.GetFieldIndex(), info.GetDexCache().Get()); |
| 527 | // TODO: There are certain cases where we can't resolve the field. |
| 528 | // b/21914925 is open to keep track of a repro case for this issue. |
| 529 | if (field != nullptr) { |
| 530 | klass = field->GetType<false>(); |
| 531 | } |
| 532 | } |
| 533 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 534 | SetClassAsTypeInfo(instr, klass, /* is_exact */ false); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 535 | } |
| 536 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 537 | void ReferenceTypePropagation::RTPVisitor::VisitInstanceFieldGet(HInstanceFieldGet* instr) { |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 538 | UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo()); |
| 539 | } |
| 540 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 541 | void ReferenceTypePropagation::RTPVisitor::VisitStaticFieldGet(HStaticFieldGet* instr) { |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 542 | UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo()); |
| 543 | } |
| 544 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 545 | void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedInstanceFieldGet( |
| 546 | HUnresolvedInstanceFieldGet* instr) { |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 547 | // TODO: Use descriptor to get the actual type. |
| 548 | if (instr->GetFieldType() == Primitive::kPrimNot) { |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 549 | instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti()); |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 553 | void ReferenceTypePropagation::RTPVisitor::VisitUnresolvedStaticFieldGet( |
| 554 | HUnresolvedStaticFieldGet* instr) { |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 555 | // TODO: Use descriptor to get the actual type. |
| 556 | if (instr->GetFieldType() == Primitive::kPrimNot) { |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 557 | instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti()); |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 561 | void ReferenceTypePropagation::RTPVisitor::VisitLoadClass(HLoadClass* instr) { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 562 | ScopedObjectAccess soa(Thread::Current()); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 563 | // Get type from dex cache assuming it was populated by the verifier. |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 564 | mirror::Class* resolved_class = GetClassFromDexCache(soa.Self(), |
| 565 | instr->GetDexFile(), |
| 566 | instr->GetTypeIndex(), |
| 567 | hint_dex_cache_); |
Aart Bik | f417ff4 | 2016-04-25 12:51:37 -0700 | [diff] [blame] | 568 | if (IsAdmissible(resolved_class)) { |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 569 | instr->SetLoadedClassRTI(ReferenceTypeInfo::Create( |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 570 | handle_cache_->NewHandle(resolved_class), /* is_exact */ true)); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 571 | } |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 572 | instr->SetReferenceTypeInfo( |
| 573 | ReferenceTypeInfo::Create(handle_cache_->GetClassClassHandle(), /* is_exact */ true)); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 576 | void ReferenceTypePropagation::RTPVisitor::VisitClinitCheck(HClinitCheck* instr) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 577 | instr->SetReferenceTypeInfo(instr->InputAt(0)->GetReferenceTypeInfo()); |
| 578 | } |
| 579 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 580 | void ReferenceTypePropagation::RTPVisitor::VisitLoadString(HLoadString* instr) { |
| 581 | instr->SetReferenceTypeInfo( |
| 582 | ReferenceTypeInfo::Create(handle_cache_->GetStringClassHandle(), /* is_exact */ true)); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 585 | void ReferenceTypePropagation::RTPVisitor::VisitLoadException(HLoadException* instr) { |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 586 | DCHECK(instr->GetBlock()->IsCatchBlock()); |
| 587 | TryCatchInformation* catch_info = instr->GetBlock()->GetTryCatchInformation(); |
| 588 | |
| 589 | if (catch_info->IsCatchAllTypeIndex()) { |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 590 | instr->SetReferenceTypeInfo( |
| 591 | ReferenceTypeInfo::Create(handle_cache_->GetThrowableClassHandle(), /* is_exact */ false)); |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 592 | } else { |
| 593 | UpdateReferenceTypeInfo(instr, |
| 594 | catch_info->GetCatchTypeIndex(), |
| 595 | catch_info->GetCatchDexFile(), |
| 596 | /* is_exact */ false); |
| 597 | } |
| 598 | } |
| 599 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 600 | void ReferenceTypePropagation::RTPVisitor::VisitNullCheck(HNullCheck* instr) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 601 | ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
David Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 602 | if (parent_rti.IsValid()) { |
| 603 | instr->SetReferenceTypeInfo(parent_rti); |
| 604 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 607 | void ReferenceTypePropagation::RTPVisitor::VisitBoundType(HBoundType* instr) { |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 608 | ReferenceTypeInfo class_rti = instr->GetUpperBound(); |
| 609 | if (class_rti.IsValid()) { |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 610 | ScopedObjectAccess soa(Thread::Current()); |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 611 | // Narrow the type as much as possible. |
| 612 | HInstruction* obj = instr->InputAt(0); |
| 613 | ReferenceTypeInfo obj_rti = obj->GetReferenceTypeInfo(); |
| 614 | if (class_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes()) { |
| 615 | instr->SetReferenceTypeInfo( |
| 616 | ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ true)); |
David Brazdil | 744a1c6 | 2015-12-28 10:53:34 +0000 | [diff] [blame] | 617 | } else if (obj_rti.IsValid()) { |
| 618 | if (class_rti.IsSupertypeOf(obj_rti)) { |
| 619 | // Object type is more specific. |
| 620 | instr->SetReferenceTypeInfo(obj_rti); |
| 621 | } else { |
| 622 | // Upper bound is more specific. |
| 623 | instr->SetReferenceTypeInfo( |
| 624 | ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false)); |
| 625 | } |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 626 | } else { |
David Brazdil | 744a1c6 | 2015-12-28 10:53:34 +0000 | [diff] [blame] | 627 | // Object not typed yet. Leave BoundType untyped for now rather than |
| 628 | // assign the type conservatively. |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 629 | } |
| 630 | instr->SetCanBeNull(obj->CanBeNull() && instr->GetUpperCanBeNull()); |
| 631 | } else { |
| 632 | // The owner of the BoundType was already visited. If the class is unresolved, |
| 633 | // the BoundType should have been removed from the data flow and this method |
| 634 | // should remove it from the graph. |
| 635 | DCHECK(!instr->HasUses()); |
| 636 | instr->GetBlock()->RemoveInstruction(instr); |
| 637 | } |
| 638 | } |
| 639 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 640 | void ReferenceTypePropagation::RTPVisitor::VisitCheckCast(HCheckCast* check_cast) { |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 641 | HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass(); |
| 642 | ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI(); |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 643 | HBoundType* bound_type = check_cast->GetNext()->AsBoundType(); |
| 644 | if (bound_type == nullptr || bound_type->GetUpperBound().IsValid()) { |
| 645 | // The next instruction is not an uninitialized BoundType. This must be |
| 646 | // an RTP pass after SsaBuilder and we do not need to do anything. |
| 647 | return; |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 648 | } |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 649 | DCHECK_EQ(bound_type->InputAt(0), check_cast->InputAt(0)); |
| 650 | |
| 651 | if (class_rti.IsValid()) { |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 652 | DCHECK(is_first_run_); |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 653 | // This is the first run of RTP and class is resolved. |
| 654 | bound_type->SetUpperBound(class_rti, /* CheckCast succeeds for nulls. */ true); |
| 655 | } else { |
| 656 | // This is the first run of RTP and class is unresolved. Remove the binding. |
| 657 | // The instruction itself is removed in VisitBoundType so as to not |
| 658 | // invalidate HInstructionIterator. |
| 659 | bound_type->ReplaceWith(bound_type->InputAt(0)); |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 663 | void ReferenceTypePropagation::VisitPhi(HPhi* phi) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 664 | if (phi->IsDead() || phi->GetType() != Primitive::kPrimNot) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 665 | return; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 666 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 667 | |
| 668 | if (phi->GetBlock()->IsLoopHeader()) { |
| 669 | // Set the initial type for the phi. Use the non back edge input for reaching |
| 670 | // a fixed point faster. |
David Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 671 | HInstruction* first_input = phi->InputAt(0); |
| 672 | ReferenceTypeInfo first_input_rti = first_input->GetReferenceTypeInfo(); |
| 673 | if (first_input_rti.IsValid() && !first_input->IsNullConstant()) { |
| 674 | phi->SetCanBeNull(first_input->CanBeNull()); |
| 675 | phi->SetReferenceTypeInfo(first_input_rti); |
| 676 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 677 | AddToWorklist(phi); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 678 | } else { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 679 | // Eagerly compute the type of the phi, for quicker convergence. Note |
| 680 | // that we don't need to add users to the worklist because we are |
| 681 | // doing a reverse post-order visit, therefore either the phi users are |
| 682 | // non-loop phi and will be visited later in the visit, or are loop-phis, |
| 683 | // and they are already in the work list. |
| 684 | UpdateNullability(phi); |
| 685 | UpdateReferenceTypeInfo(phi); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a, |
| 690 | const ReferenceTypeInfo& b) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 691 | if (!b.IsValid()) { |
| 692 | return a; |
| 693 | } |
| 694 | if (!a.IsValid()) { |
| 695 | return b; |
Calin Juravle | 20e6071 | 2015-07-01 18:41:04 +0100 | [diff] [blame] | 696 | } |
| 697 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 698 | bool is_exact = a.IsExact() && b.IsExact(); |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 699 | ReferenceTypeInfo::TypeHandle result_type_handle; |
| 700 | ReferenceTypeInfo::TypeHandle a_type_handle = a.GetTypeHandle(); |
| 701 | ReferenceTypeInfo::TypeHandle b_type_handle = b.GetTypeHandle(); |
| 702 | bool a_is_interface = a_type_handle->IsInterface(); |
| 703 | bool b_is_interface = b_type_handle->IsInterface(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 704 | |
| 705 | if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) { |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 706 | result_type_handle = a_type_handle; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 707 | } else if (a.IsSupertypeOf(b)) { |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 708 | result_type_handle = a_type_handle; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 709 | is_exact = false; |
| 710 | } else if (b.IsSupertypeOf(a)) { |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 711 | result_type_handle = b_type_handle; |
| 712 | is_exact = false; |
| 713 | } else if (!a_is_interface && !b_is_interface) { |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 714 | result_type_handle = |
| 715 | handle_cache_.NewHandle(a_type_handle->GetCommonSuperClass(b_type_handle)); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 716 | is_exact = false; |
| 717 | } else { |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 718 | // This can happen if: |
| 719 | // - both types are interfaces. TODO(calin): implement |
| 720 | // - one is an interface, the other a class, and the type does not implement the interface |
| 721 | // e.g: |
| 722 | // void foo(Interface i, boolean cond) { |
| 723 | // Object o = cond ? i : new Object(); |
| 724 | // } |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 725 | result_type_handle = handle_cache_.GetObjectClassHandle(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 726 | is_exact = false; |
| 727 | } |
| 728 | |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 729 | return ReferenceTypeInfo::Create(result_type_handle, is_exact); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 732 | void ReferenceTypePropagation::UpdateArrayGet(HArrayGet* instr, HandleCache* handle_cache) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 733 | DCHECK_EQ(Primitive::kPrimNot, instr->GetType()); |
| 734 | |
| 735 | ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
David Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 736 | if (!parent_rti.IsValid()) { |
| 737 | return; |
| 738 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 739 | |
| 740 | Handle<mirror::Class> handle = parent_rti.GetTypeHandle(); |
Aart Bik | f417ff4 | 2016-04-25 12:51:37 -0700 | [diff] [blame] | 741 | if (handle->IsObjectArrayClass() && IsAdmissible(handle->GetComponentType())) { |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 742 | ReferenceTypeInfo::TypeHandle component_handle = |
| 743 | handle_cache->NewHandle(handle->GetComponentType()); |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 744 | bool is_exact = component_handle->CannotBeAssignedFromOtherTypes(); |
| 745 | instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(component_handle, is_exact)); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 746 | } else { |
| 747 | // 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] | 748 | instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti()); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 749 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | bool ReferenceTypePropagation::UpdateReferenceTypeInfo(HInstruction* instr) { |
| 753 | ScopedObjectAccess soa(Thread::Current()); |
| 754 | |
| 755 | ReferenceTypeInfo previous_rti = instr->GetReferenceTypeInfo(); |
| 756 | if (instr->IsBoundType()) { |
| 757 | UpdateBoundType(instr->AsBoundType()); |
| 758 | } else if (instr->IsPhi()) { |
| 759 | UpdatePhi(instr->AsPhi()); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 760 | } else if (instr->IsNullCheck()) { |
| 761 | ReferenceTypeInfo parent_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
| 762 | if (parent_rti.IsValid()) { |
| 763 | instr->SetReferenceTypeInfo(parent_rti); |
| 764 | } |
| 765 | } else if (instr->IsArrayGet()) { |
David Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 766 | // 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] | 767 | // to an array type. |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 768 | UpdateArrayGet(instr->AsArrayGet(), &handle_cache_); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 769 | } else { |
| 770 | LOG(FATAL) << "Invalid instruction (should not get here)"; |
| 771 | } |
| 772 | |
| 773 | return !previous_rti.IsEqual(instr->GetReferenceTypeInfo()); |
| 774 | } |
| 775 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 776 | void ReferenceTypePropagation::RTPVisitor::VisitInvoke(HInvoke* instr) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 777 | if (instr->GetType() != Primitive::kPrimNot) { |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | ScopedObjectAccess soa(Thread::Current()); |
| 782 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 783 | mirror::DexCache* dex_cache = |
| 784 | FindDexCacheWithHint(soa.Self(), instr->GetDexFile(), hint_dex_cache_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 785 | size_t pointer_size = cl->GetImagePointerSize(); |
| 786 | ArtMethod* method = dex_cache->GetResolvedMethod(instr->GetDexMethodIndex(), pointer_size); |
| 787 | mirror::Class* klass = (method == nullptr) ? nullptr : method->GetReturnType(false, pointer_size); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 788 | SetClassAsTypeInfo(instr, klass, /* is_exact */ false); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 791 | void ReferenceTypePropagation::RTPVisitor::VisitArrayGet(HArrayGet* instr) { |
Guillaume "Vermeille" Sanchez | 72a5eb5 | 2015-06-02 17:39:45 +0100 | [diff] [blame] | 792 | if (instr->GetType() != Primitive::kPrimNot) { |
| 793 | return; |
| 794 | } |
David Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 795 | |
Guillaume "Vermeille" Sanchez | 72a5eb5 | 2015-06-02 17:39:45 +0100 | [diff] [blame] | 796 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 797 | UpdateArrayGet(instr, handle_cache_); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 798 | if (!instr->GetReferenceTypeInfo().IsValid()) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 799 | worklist_->push_back(instr); |
Guillaume "Vermeille" Sanchez | 72a5eb5 | 2015-06-02 17:39:45 +0100 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 803 | void ReferenceTypePropagation::UpdateBoundType(HBoundType* instr) { |
| 804 | ReferenceTypeInfo new_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 805 | if (!new_rti.IsValid()) { |
| 806 | return; // No new info yet. |
| 807 | } |
| 808 | |
| 809 | // Make sure that we don't go over the bounded type. |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 810 | ReferenceTypeInfo upper_bound_rti = instr->GetUpperBound(); |
| 811 | if (!upper_bound_rti.IsSupertypeOf(new_rti)) { |
Nicolas Geoffray | a90d489 | 2016-06-01 16:30:20 +0100 | [diff] [blame^] | 812 | // Note that the input might be exact, in which case we know the branch leading |
| 813 | // to the bound type is dead. We play it safe by not marking the bound type as |
| 814 | // exact. |
| 815 | bool is_exact = upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes(); |
| 816 | new_rti = ReferenceTypeInfo::Create(upper_bound_rti.GetTypeHandle(), is_exact); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 817 | } |
| 818 | instr->SetReferenceTypeInfo(new_rti); |
| 819 | } |
| 820 | |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 821 | // NullConstant inputs are ignored during merging as they do not provide any useful information. |
| 822 | // 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] | 823 | void ReferenceTypePropagation::UpdatePhi(HPhi* instr) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 824 | DCHECK(instr->IsLive()); |
| 825 | |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 826 | size_t input_count = instr->InputCount(); |
| 827 | size_t first_input_index_not_null = 0; |
| 828 | while (first_input_index_not_null < input_count && |
| 829 | instr->InputAt(first_input_index_not_null)->IsNullConstant()) { |
| 830 | first_input_index_not_null++; |
| 831 | } |
| 832 | if (first_input_index_not_null == input_count) { |
| 833 | // All inputs are NullConstants, set the type to object. |
| 834 | // This may happen in the presence of inlining. |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 835 | instr->SetReferenceTypeInfo(instr->GetBlock()->GetGraph()->GetInexactObjectRti()); |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 836 | return; |
| 837 | } |
| 838 | |
| 839 | ReferenceTypeInfo new_rti = instr->InputAt(first_input_index_not_null)->GetReferenceTypeInfo(); |
| 840 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 841 | if (new_rti.IsValid() && new_rti.IsObjectClass() && !new_rti.IsExact()) { |
| 842 | // Early return if we are Object and inexact. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 843 | instr->SetReferenceTypeInfo(new_rti); |
| 844 | return; |
| 845 | } |
Calin Juravle | 617bd92 | 2015-11-11 14:59:46 +0000 | [diff] [blame] | 846 | |
| 847 | for (size_t i = first_input_index_not_null + 1; i < input_count; i++) { |
| 848 | if (instr->InputAt(i)->IsNullConstant()) { |
| 849 | continue; |
| 850 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 851 | new_rti = MergeTypes(new_rti, instr->InputAt(i)->GetReferenceTypeInfo()); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 852 | if (new_rti.IsValid() && new_rti.IsObjectClass()) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 853 | if (!new_rti.IsExact()) { |
| 854 | break; |
| 855 | } else { |
| 856 | continue; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 857 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 858 | } |
| 859 | } |
David Brazdil | fe86070 | 2015-12-02 09:06:57 +0000 | [diff] [blame] | 860 | |
| 861 | if (new_rti.IsValid()) { |
| 862 | instr->SetReferenceTypeInfo(new_rti); |
| 863 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | // Re-computes and updates the nullability of the instruction. Returns whether or |
| 867 | // not the nullability was changed. |
| 868 | bool ReferenceTypePropagation::UpdateNullability(HInstruction* instr) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 869 | DCHECK((instr->IsPhi() && instr->AsPhi()->IsLive()) |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 870 | || instr->IsBoundType() |
| 871 | || instr->IsNullCheck() |
| 872 | || instr->IsArrayGet()); |
| 873 | |
| 874 | if (!instr->IsPhi() && !instr->IsBoundType()) { |
| 875 | return false; |
| 876 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 877 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 878 | bool existing_can_be_null = instr->CanBeNull(); |
| 879 | if (instr->IsPhi()) { |
| 880 | HPhi* phi = instr->AsPhi(); |
| 881 | bool new_can_be_null = false; |
| 882 | for (size_t i = 0; i < phi->InputCount(); i++) { |
| 883 | if (phi->InputAt(i)->CanBeNull()) { |
| 884 | new_can_be_null = true; |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | phi->SetCanBeNull(new_can_be_null); |
| 889 | } else if (instr->IsBoundType()) { |
| 890 | HBoundType* bound_type = instr->AsBoundType(); |
| 891 | bound_type->SetCanBeNull(instr->InputAt(0)->CanBeNull() && bound_type->GetUpperCanBeNull()); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 892 | } |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 893 | return existing_can_be_null != instr->CanBeNull(); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | void ReferenceTypePropagation::ProcessWorklist() { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 897 | while (!worklist_.empty()) { |
| 898 | HInstruction* instruction = worklist_.back(); |
| 899 | worklist_.pop_back(); |
Calin Juravle | 1261759 | 2015-10-16 16:28:46 +0100 | [diff] [blame] | 900 | bool updated_nullability = UpdateNullability(instruction); |
| 901 | bool updated_reference_type = UpdateReferenceTypeInfo(instruction); |
| 902 | if (updated_nullability || updated_reference_type) { |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 903 | AddDependentInstructionsToWorklist(instruction); |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 908 | void ReferenceTypePropagation::AddToWorklist(HInstruction* instruction) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 909 | DCHECK_EQ(instruction->GetType(), Primitive::kPrimNot) |
| 910 | << instruction->DebugName() << ":" << instruction->GetType(); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 911 | worklist_.push_back(instruction); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 914 | void ReferenceTypePropagation::AddDependentInstructionsToWorklist(HInstruction* instruction) { |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 915 | for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) { |
| 916 | HInstruction* user = use.GetUser(); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 917 | if ((user->IsPhi() && user->AsPhi()->IsLive()) |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 918 | || user->IsBoundType() |
| 919 | || user->IsNullCheck() |
| 920 | || (user->IsArrayGet() && (user->GetType() == Primitive::kPrimNot))) { |
Calin Juravle | beba930 | 2015-07-08 15:57:18 +0000 | [diff] [blame] | 921 | AddToWorklist(user); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | } |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 925 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 926 | } // namespace art |