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