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