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: |
| 28 | RTPVisitor(HGraph* graph, StackHandleScopeCollection* handles) |
| 29 | : HGraphDelegateVisitor(graph), |
| 30 | handles_(handles) {} |
| 31 | |
| 32 | void VisitNewInstance(HNewInstance* new_instance) OVERRIDE; |
| 33 | void VisitLoadClass(HLoadClass* load_class) OVERRIDE; |
| 34 | void VisitNewArray(HNewArray* instr) OVERRIDE; |
| 35 | void UpdateFieldAccessTypeInfo(HInstruction* instr, const FieldInfo& info); |
| 36 | void SetClassAsTypeInfo(HInstruction* instr, mirror::Class* klass, bool is_exact); |
| 37 | void VisitInstanceFieldGet(HInstanceFieldGet* instr) OVERRIDE; |
| 38 | void VisitStaticFieldGet(HStaticFieldGet* instr) OVERRIDE; |
| 39 | void VisitInvoke(HInvoke* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | 72a5eb5 | 2015-06-02 17:39:45 +0100 | [diff] [blame] | 40 | void VisitArrayGet(HArrayGet* instr) OVERRIDE; |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 41 | void UpdateReferenceTypeInfo(HInstruction* instr, |
| 42 | uint16_t type_idx, |
| 43 | const DexFile& dex_file, |
| 44 | bool is_exact); |
| 45 | |
| 46 | private: |
| 47 | StackHandleScopeCollection* handles_; |
| 48 | }; |
| 49 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 50 | void ReferenceTypePropagation::Run() { |
| 51 | // To properly propagate type info we need to visit in the dominator-based order. |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 52 | // Reverse post order guarantees a node's dominators are visited first. |
| 53 | // We take advantage of this order in `VisitBasicBlock`. |
Calin Juravle | 6c0c4f2 | 2015-06-12 15:40:42 +0000 | [diff] [blame] | 54 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 55 | VisitBasicBlock(it.Current()); |
| 56 | } |
| 57 | ProcessWorklist(); |
| 58 | } |
| 59 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 60 | void ReferenceTypePropagation::VisitBasicBlock(HBasicBlock* block) { |
| 61 | // TODO: handle other instructions that give type info |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 62 | // (array accesses) |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 63 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 64 | RTPVisitor visitor(graph_, handles_); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 65 | // Initialize exact types first for faster convergence. |
| 66 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 67 | HInstruction* instr = it.Current(); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 68 | instr->Accept(&visitor); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Handle Phis. |
| 72 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 73 | VisitPhi(it.Current()->AsPhi()); |
| 74 | } |
| 75 | |
| 76 | // Add extra nodes to bound types. |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 77 | BoundTypeForIfNotNull(block); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 78 | BoundTypeForIfInstanceOf(block); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 81 | void ReferenceTypePropagation::BoundTypeForIfNotNull(HBasicBlock* block) { |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 82 | HIf* ifInstruction = block->GetLastInstruction()->AsIf(); |
| 83 | if (ifInstruction == nullptr) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 84 | return; |
| 85 | } |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 86 | HInstruction* ifInput = ifInstruction->InputAt(0); |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 87 | if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) { |
| 88 | return; |
| 89 | } |
| 90 | HInstruction* input0 = ifInput->InputAt(0); |
| 91 | HInstruction* input1 = ifInput->InputAt(1); |
Calin Juravle | edad8ad | 2015-04-23 14:34:33 +0100 | [diff] [blame] | 92 | HInstruction* obj = nullptr; |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 93 | |
Calin Juravle | edad8ad | 2015-04-23 14:34:33 +0100 | [diff] [blame] | 94 | if (input1->IsNullConstant()) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 95 | obj = input0; |
Calin Juravle | edad8ad | 2015-04-23 14:34:33 +0100 | [diff] [blame] | 96 | } else if (input0->IsNullConstant()) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 97 | obj = input1; |
| 98 | } else { |
| 99 | return; |
| 100 | } |
| 101 | |
Nicolas Geoffray | 7d5ea03 | 2015-07-02 15:48:27 +0100 | [diff] [blame^] | 102 | if (!obj->CanBeNull() || obj->IsNullConstant()) { |
| 103 | // Null check is dead code and will be removed by DCE. |
| 104 | return; |
| 105 | } |
| 106 | DCHECK(!obj->IsLoadClass()) << "We should not replace HLoadClass instructions"; |
| 107 | |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 108 | // We only need to bound the type if we have uses in the relevant block. |
| 109 | // So start with null and create the HBoundType lazily, only if it's needed. |
| 110 | HBoundType* bound_type = nullptr; |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 111 | HBasicBlock* notNullBlock = ifInput->IsNotEqual() |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 112 | ? ifInstruction->IfTrueSuccessor() |
| 113 | : ifInstruction->IfFalseSuccessor(); |
| 114 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 115 | for (HUseIterator<HInstruction*> it(obj->GetUses()); !it.Done(); it.Advance()) { |
| 116 | HInstruction* user = it.Current()->GetUser(); |
| 117 | if (notNullBlock->Dominates(user->GetBlock())) { |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 118 | if (bound_type == nullptr) { |
Calin Juravle | 6c0c4f2 | 2015-06-12 15:40:42 +0000 | [diff] [blame] | 119 | bound_type = new (graph_->GetArena()) HBoundType(obj, ReferenceTypeInfo::CreateTop(false)); |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 120 | notNullBlock->InsertInstructionBefore(bound_type, notNullBlock->GetFirstInstruction()); |
| 121 | } |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 122 | user->ReplaceInput(bound_type, it.Current()->GetIndex()); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 127 | // Detects if `block` is the True block for the pattern |
| 128 | // `if (x instanceof ClassX) { }` |
| 129 | // If that's the case insert an HBoundType instruction to bound the type of `x` |
| 130 | // to `ClassX` in the scope of the dominated blocks. |
| 131 | void ReferenceTypePropagation::BoundTypeForIfInstanceOf(HBasicBlock* block) { |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 132 | HIf* ifInstruction = block->GetLastInstruction()->AsIf(); |
| 133 | if (ifInstruction == nullptr) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 134 | return; |
| 135 | } |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 136 | HInstruction* ifInput = ifInstruction->InputAt(0); |
| 137 | HInstruction* instanceOf = nullptr; |
| 138 | HBasicBlock* instanceOfTrueBlock = nullptr; |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 139 | |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 140 | // The instruction simplifier has transformed: |
| 141 | // - `if (a instanceof A)` into an HIf with an HInstanceOf input |
| 142 | // - `if (!(a instanceof A)` into an HIf with an HBooleanNot input (which in turn |
| 143 | // has an HInstanceOf input) |
| 144 | // So we should not see the usual HEqual here. |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 145 | if (ifInput->IsInstanceOf()) { |
| 146 | instanceOf = ifInput; |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 147 | instanceOfTrueBlock = ifInstruction->IfTrueSuccessor(); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 148 | } else if (ifInput->IsBooleanNot() && ifInput->InputAt(0)->IsInstanceOf()) { |
| 149 | instanceOf = ifInput->InputAt(0); |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 150 | instanceOfTrueBlock = ifInstruction->IfFalseSuccessor(); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 151 | } else { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 152 | return; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 155 | // We only need to bound the type if we have uses in the relevant block. |
| 156 | // So start with null and create the HBoundType lazily, only if it's needed. |
| 157 | HBoundType* bound_type = nullptr; |
| 158 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 159 | HInstruction* obj = instanceOf->InputAt(0); |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 160 | if (obj->GetReferenceTypeInfo().IsExact() && !obj->IsPhi()) { |
| 161 | // This method is being called while doing a fixed-point calculation |
| 162 | // over phis. Non-phis instruction whose type is already known do |
| 163 | // not need to be bound to another type. |
| 164 | // Not that this also prevents replacing `HLoadClass` with a `HBoundType`. |
| 165 | // `HCheckCast` and `HInstanceOf` expect a `HLoadClass` as a second |
| 166 | // input. |
| 167 | return; |
| 168 | } |
Nicolas Geoffray | 7d5ea03 | 2015-07-02 15:48:27 +0100 | [diff] [blame^] | 169 | DCHECK(!obj->IsLoadClass()) << "We should not replace HLoadClass instructions"; |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 170 | for (HUseIterator<HInstruction*> it(obj->GetUses()); !it.Done(); it.Advance()) { |
| 171 | HInstruction* user = it.Current()->GetUser(); |
| 172 | if (instanceOfTrueBlock->Dominates(user->GetBlock())) { |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 173 | if (bound_type == nullptr) { |
| 174 | HLoadClass* load_class = instanceOf->InputAt(1)->AsLoadClass(); |
| 175 | |
| 176 | ReferenceTypeInfo obj_rti = obj->GetReferenceTypeInfo(); |
| 177 | ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI(); |
Calin Juravle | 6c0c4f2 | 2015-06-12 15:40:42 +0000 | [diff] [blame] | 178 | bound_type = new (graph_->GetArena()) HBoundType(obj, class_rti); |
Calin Juravle | b330664 | 2015-04-20 18:30:42 +0100 | [diff] [blame] | 179 | |
| 180 | // Narrow the type as much as possible. |
| 181 | { |
| 182 | ScopedObjectAccess soa(Thread::Current()); |
| 183 | if (!load_class->IsResolved() || class_rti.IsSupertypeOf(obj_rti)) { |
| 184 | bound_type->SetReferenceTypeInfo(obj_rti); |
| 185 | } else { |
| 186 | bound_type->SetReferenceTypeInfo( |
| 187 | ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false)); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | instanceOfTrueBlock->InsertInstructionBefore( |
| 192 | bound_type, instanceOfTrueBlock->GetFirstInstruction()); |
| 193 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 194 | user->ReplaceInput(bound_type, it.Current()->GetIndex()); |
| 195 | } |
| 196 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 199 | void RTPVisitor::SetClassAsTypeInfo(HInstruction* instr, |
| 200 | mirror::Class* klass, |
| 201 | bool is_exact) { |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 202 | if (klass != nullptr) { |
| 203 | ScopedObjectAccess soa(Thread::Current()); |
| 204 | MutableHandle<mirror::Class> handle = handles_->NewHandle(klass); |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 205 | is_exact = is_exact || klass->IsFinal(); |
| 206 | instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(handle, is_exact)); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 210 | void RTPVisitor::UpdateReferenceTypeInfo(HInstruction* instr, |
| 211 | uint16_t type_idx, |
| 212 | const DexFile& dex_file, |
| 213 | bool is_exact) { |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 214 | DCHECK_EQ(instr->GetType(), Primitive::kPrimNot); |
| 215 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 216 | ScopedObjectAccess soa(Thread::Current()); |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 217 | mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 218 | // Get type from dex cache assuming it was populated by the verifier. |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 219 | SetClassAsTypeInfo(instr, dex_cache->GetResolvedType(type_idx), is_exact); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 222 | void RTPVisitor::VisitNewInstance(HNewInstance* instr) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 223 | UpdateReferenceTypeInfo(instr, instr->GetTypeIndex(), instr->GetDexFile(), /* is_exact */ true); |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 226 | void RTPVisitor::VisitNewArray(HNewArray* instr) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 227 | UpdateReferenceTypeInfo(instr, instr->GetTypeIndex(), instr->GetDexFile(), /* is_exact */ true); |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 230 | void RTPVisitor::UpdateFieldAccessTypeInfo(HInstruction* instr, |
| 231 | const FieldInfo& info) { |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 232 | // The field index is unknown only during tests. |
| 233 | if (instr->GetType() != Primitive::kPrimNot || info.GetFieldIndex() == kUnknownFieldIndex) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | ScopedObjectAccess soa(Thread::Current()); |
| 238 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 239 | mirror::DexCache* dex_cache = cl->FindDexCache(info.GetDexFile()); |
| 240 | ArtField* field = cl->GetResolvedField(info.GetFieldIndex(), dex_cache); |
Calin Juravle | 183617a | 2015-06-18 18:38:29 +0100 | [diff] [blame] | 241 | if (field != nullptr) { |
| 242 | mirror::Class* klass = field->GetType<false>(); |
| 243 | SetClassAsTypeInfo(instr, klass, /* is_exact */ false); |
| 244 | } |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 247 | void RTPVisitor::VisitInstanceFieldGet(HInstanceFieldGet* instr) { |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 248 | UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo()); |
| 249 | } |
| 250 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 251 | void RTPVisitor::VisitStaticFieldGet(HStaticFieldGet* instr) { |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 252 | UpdateFieldAccessTypeInfo(instr, instr->GetFieldInfo()); |
| 253 | } |
| 254 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 255 | void RTPVisitor::VisitLoadClass(HLoadClass* instr) { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 256 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 257 | mirror::DexCache* dex_cache = |
| 258 | Runtime::Current()->GetClassLinker()->FindDexCache(instr->GetDexFile()); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 259 | // Get type from dex cache assuming it was populated by the verifier. |
| 260 | mirror::Class* resolved_class = dex_cache->GetResolvedType(instr->GetTypeIndex()); |
| 261 | if (resolved_class != nullptr) { |
| 262 | Handle<mirror::Class> handle = handles_->NewHandle(resolved_class); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 263 | instr->SetLoadedClassRTI(ReferenceTypeInfo::Create(handle, /* is_exact */ true)); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 264 | } |
| 265 | Handle<mirror::Class> class_handle = handles_->NewHandle(mirror::Class::GetJavaLangClass()); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 266 | instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(class_handle, /* is_exact */ true)); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 267 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 268 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 269 | void ReferenceTypePropagation::VisitPhi(HPhi* phi) { |
| 270 | if (phi->GetType() != Primitive::kPrimNot) { |
| 271 | return; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 272 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 273 | |
| 274 | if (phi->GetBlock()->IsLoopHeader()) { |
| 275 | // Set the initial type for the phi. Use the non back edge input for reaching |
| 276 | // a fixed point faster. |
| 277 | AddToWorklist(phi); |
| 278 | phi->SetCanBeNull(phi->InputAt(0)->CanBeNull()); |
| 279 | phi->SetReferenceTypeInfo(phi->InputAt(0)->GetReferenceTypeInfo()); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 280 | } else { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 281 | // Eagerly compute the type of the phi, for quicker convergence. Note |
| 282 | // that we don't need to add users to the worklist because we are |
| 283 | // doing a reverse post-order visit, therefore either the phi users are |
| 284 | // non-loop phi and will be visited later in the visit, or are loop-phis, |
| 285 | // and they are already in the work list. |
| 286 | UpdateNullability(phi); |
| 287 | UpdateReferenceTypeInfo(phi); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a, |
| 292 | const ReferenceTypeInfo& b) { |
| 293 | bool is_exact = a.IsExact() && b.IsExact(); |
| 294 | bool is_top = a.IsTop() || b.IsTop(); |
| 295 | Handle<mirror::Class> type_handle; |
| 296 | |
| 297 | if (!is_top) { |
| 298 | if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) { |
| 299 | type_handle = a.GetTypeHandle(); |
| 300 | } else if (a.IsSupertypeOf(b)) { |
| 301 | type_handle = a.GetTypeHandle(); |
| 302 | is_exact = false; |
| 303 | } else if (b.IsSupertypeOf(a)) { |
| 304 | type_handle = b.GetTypeHandle(); |
| 305 | is_exact = false; |
| 306 | } else { |
| 307 | // TODO: Find a common super class. |
| 308 | is_top = true; |
| 309 | is_exact = false; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return is_top |
| 314 | ? ReferenceTypeInfo::CreateTop(is_exact) |
| 315 | : ReferenceTypeInfo::Create(type_handle, is_exact); |
| 316 | } |
| 317 | |
| 318 | bool ReferenceTypePropagation::UpdateReferenceTypeInfo(HInstruction* instr) { |
| 319 | ScopedObjectAccess soa(Thread::Current()); |
| 320 | |
| 321 | ReferenceTypeInfo previous_rti = instr->GetReferenceTypeInfo(); |
| 322 | if (instr->IsBoundType()) { |
| 323 | UpdateBoundType(instr->AsBoundType()); |
| 324 | } else if (instr->IsPhi()) { |
| 325 | UpdatePhi(instr->AsPhi()); |
| 326 | } else { |
| 327 | LOG(FATAL) << "Invalid instruction (should not get here)"; |
| 328 | } |
| 329 | |
| 330 | return !previous_rti.IsEqual(instr->GetReferenceTypeInfo()); |
| 331 | } |
| 332 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 333 | void RTPVisitor::VisitInvoke(HInvoke* instr) { |
| 334 | if (instr->GetType() != Primitive::kPrimNot) { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | ScopedObjectAccess soa(Thread::Current()); |
| 339 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 340 | mirror::DexCache* dex_cache = cl->FindDexCache(instr->GetDexFile()); |
| 341 | ArtMethod* method = dex_cache->GetResolvedMethod( |
| 342 | instr->GetDexMethodIndex(), cl->GetImagePointerSize()); |
Calin Juravle | 183617a | 2015-06-18 18:38:29 +0100 | [diff] [blame] | 343 | if (method != nullptr) { |
| 344 | mirror::Class* klass = method->GetReturnType(false); |
| 345 | SetClassAsTypeInfo(instr, klass, /* is_exact */ false); |
| 346 | } |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Guillaume "Vermeille" Sanchez | 72a5eb5 | 2015-06-02 17:39:45 +0100 | [diff] [blame] | 349 | void RTPVisitor::VisitArrayGet(HArrayGet* instr) { |
| 350 | if (instr->GetType() != Primitive::kPrimNot) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | HInstruction* parent = instr->InputAt(0); |
| 355 | ScopedObjectAccess soa(Thread::Current()); |
| 356 | Handle<mirror::Class> handle = parent->GetReferenceTypeInfo().GetTypeHandle(); |
| 357 | if (handle.GetReference() != nullptr && handle->IsObjectArrayClass()) { |
| 358 | SetClassAsTypeInfo(instr, handle->GetComponentType(), /* is_exact */ false); |
| 359 | } |
| 360 | } |
| 361 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 362 | void ReferenceTypePropagation::UpdateBoundType(HBoundType* instr) { |
| 363 | ReferenceTypeInfo new_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
| 364 | // Be sure that we don't go over the bounded type. |
| 365 | ReferenceTypeInfo bound_rti = instr->GetBoundType(); |
| 366 | if (!bound_rti.IsSupertypeOf(new_rti)) { |
| 367 | new_rti = bound_rti; |
| 368 | } |
| 369 | instr->SetReferenceTypeInfo(new_rti); |
| 370 | } |
| 371 | |
| 372 | void ReferenceTypePropagation::UpdatePhi(HPhi* instr) { |
| 373 | ReferenceTypeInfo new_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
| 374 | if (new_rti.IsTop() && !new_rti.IsExact()) { |
| 375 | // Early return if we are Top and inexact. |
| 376 | instr->SetReferenceTypeInfo(new_rti); |
| 377 | return; |
| 378 | } |
| 379 | for (size_t i = 1; i < instr->InputCount(); i++) { |
| 380 | new_rti = MergeTypes(new_rti, instr->InputAt(i)->GetReferenceTypeInfo()); |
| 381 | if (new_rti.IsTop()) { |
| 382 | if (!new_rti.IsExact()) { |
| 383 | break; |
| 384 | } else { |
| 385 | continue; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 386 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 389 | instr->SetReferenceTypeInfo(new_rti); |
| 390 | } |
| 391 | |
| 392 | // Re-computes and updates the nullability of the instruction. Returns whether or |
| 393 | // not the nullability was changed. |
| 394 | bool ReferenceTypePropagation::UpdateNullability(HInstruction* instr) { |
| 395 | DCHECK(instr->IsPhi() || instr->IsBoundType()); |
| 396 | |
| 397 | if (!instr->IsPhi()) { |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | HPhi* phi = instr->AsPhi(); |
| 402 | bool existing_can_be_null = phi->CanBeNull(); |
| 403 | bool new_can_be_null = false; |
| 404 | for (size_t i = 0; i < phi->InputCount(); i++) { |
| 405 | new_can_be_null |= phi->InputAt(i)->CanBeNull(); |
| 406 | } |
| 407 | phi->SetCanBeNull(new_can_be_null); |
| 408 | |
| 409 | return existing_can_be_null != new_can_be_null; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void ReferenceTypePropagation::ProcessWorklist() { |
| 413 | while (!worklist_.IsEmpty()) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 414 | HInstruction* instruction = worklist_.Pop(); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 415 | if (UpdateNullability(instruction) || UpdateReferenceTypeInfo(instruction)) { |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 416 | AddDependentInstructionsToWorklist(instruction); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 421 | void ReferenceTypePropagation::AddToWorklist(HInstruction* instruction) { |
| 422 | DCHECK_EQ(instruction->GetType(), Primitive::kPrimNot) << instruction->GetType(); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 423 | worklist_.Add(instruction); |
| 424 | } |
| 425 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 426 | void ReferenceTypePropagation::AddDependentInstructionsToWorklist(HInstruction* instruction) { |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 427 | for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 428 | HInstruction* user = it.Current()->GetUser(); |
| 429 | if (user->IsPhi() || user->IsBoundType()) { |
| 430 | AddToWorklist(user); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 434 | } // namespace art |