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