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