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