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 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 19 | #include "class_linker.h" |
| 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 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 26 | void ReferenceTypePropagation::Run() { |
| 27 | // 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] | 28 | // Reverse post order guarantees a node's dominators are visited first. |
| 29 | // We take advantage of this order in `VisitBasicBlock`. |
| 30 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 31 | VisitBasicBlock(it.Current()); |
| 32 | } |
| 33 | ProcessWorklist(); |
| 34 | } |
| 35 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 36 | void ReferenceTypePropagation::VisitBasicBlock(HBasicBlock* block) { |
| 37 | // TODO: handle other instructions that give type info |
| 38 | // (NewArray/Call/Field accesses/array accesses) |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 39 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 40 | // Initialize exact types first for faster convergence. |
| 41 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 42 | HInstruction* instr = it.Current(); |
| 43 | if (instr->IsNewInstance()) { |
| 44 | VisitNewInstance(instr->AsNewInstance()); |
| 45 | } else if (instr->IsLoadClass()) { |
| 46 | VisitLoadClass(instr->AsLoadClass()); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Handle Phis. |
| 51 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 52 | VisitPhi(it.Current()->AsPhi()); |
| 53 | } |
| 54 | |
| 55 | // Add extra nodes to bound types. |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 56 | BoundTypeForIfNotNull(block); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 57 | BoundTypeForIfInstanceOf(block); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 60 | void ReferenceTypePropagation::BoundTypeForIfNotNull(HBasicBlock* block) { |
| 61 | HInstruction* lastInstruction = block->GetLastInstruction(); |
| 62 | if (!lastInstruction->IsIf()) { |
| 63 | return; |
| 64 | } |
| 65 | HInstruction* ifInput = lastInstruction->InputAt(0); |
| 66 | if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) { |
| 67 | return; |
| 68 | } |
| 69 | HInstruction* input0 = ifInput->InputAt(0); |
| 70 | HInstruction* input1 = ifInput->InputAt(1); |
| 71 | HInstruction* obj; |
| 72 | |
| 73 | if ((input0->GetType() == Primitive::kPrimNot) && input1->ActAsNullConstant()) { |
| 74 | obj = input0; |
| 75 | } else if ((input1->GetType() == Primitive::kPrimNot) && input0->ActAsNullConstant()) { |
| 76 | obj = input1; |
| 77 | } else { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | HBoundType* bound_type = |
| 82 | new (graph_->GetArena()) HBoundType(obj, ReferenceTypeInfo::CreateTop(false)); |
| 83 | |
| 84 | block->InsertInstructionBefore(bound_type, lastInstruction); |
| 85 | HBasicBlock* notNullBlock = ifInput->IsNotEqual() |
| 86 | ? lastInstruction->AsIf()->IfTrueSuccessor() |
| 87 | : lastInstruction->AsIf()->IfFalseSuccessor(); |
| 88 | for (HUseIterator<HInstruction*> it(obj->GetUses()); !it.Done(); it.Advance()) { |
| 89 | HInstruction* user = it.Current()->GetUser(); |
| 90 | if (notNullBlock->Dominates(user->GetBlock())) { |
| 91 | user->ReplaceInput(bound_type, it.Current()->GetIndex()); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 96 | // Detects if `block` is the True block for the pattern |
| 97 | // `if (x instanceof ClassX) { }` |
| 98 | // If that's the case insert an HBoundType instruction to bound the type of `x` |
| 99 | // to `ClassX` in the scope of the dominated blocks. |
| 100 | void ReferenceTypePropagation::BoundTypeForIfInstanceOf(HBasicBlock* block) { |
| 101 | HInstruction* lastInstruction = block->GetLastInstruction(); |
| 102 | if (!lastInstruction->IsIf()) { |
| 103 | return; |
| 104 | } |
| 105 | HInstruction* ifInput = lastInstruction->InputAt(0); |
| 106 | // TODO: Handle more patterns here: HIf(bool) HIf(HNotEqual). |
| 107 | if (!ifInput->IsEqual()) { |
| 108 | return; |
| 109 | } |
| 110 | HInstruction* instanceOf = ifInput->InputAt(0); |
| 111 | HInstruction* comp_value = ifInput->InputAt(1); |
| 112 | if (!instanceOf->IsInstanceOf() || !comp_value->IsIntConstant()) { |
| 113 | return; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 116 | HInstruction* obj = instanceOf->InputAt(0); |
| 117 | HLoadClass* load_class = instanceOf->InputAt(1)->AsLoadClass(); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 118 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 119 | ReferenceTypeInfo obj_rti = obj->GetReferenceTypeInfo(); |
| 120 | ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI(); |
| 121 | HBoundType* bound_type = new (graph_->GetArena()) HBoundType(obj, class_rti); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 122 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 123 | // Narrow the type as much as possible. |
| 124 | { |
| 125 | ScopedObjectAccess soa(Thread::Current()); |
| 126 | if (!load_class->IsResolved() || class_rti.IsSupertypeOf(obj_rti)) { |
| 127 | bound_type->SetReferenceTypeInfo(obj_rti); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 128 | } else { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 129 | bound_type->SetReferenceTypeInfo( |
| 130 | ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false)); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 134 | block->InsertInstructionBefore(bound_type, lastInstruction); |
| 135 | // Pick the right successor based on the value we compare against. |
| 136 | HIntConstant* comp_value_int = comp_value->AsIntConstant(); |
| 137 | HBasicBlock* instanceOfTrueBlock = comp_value_int->GetValue() == 0 |
| 138 | ? lastInstruction->AsIf()->IfFalseSuccessor() |
| 139 | : lastInstruction->AsIf()->IfTrueSuccessor(); |
| 140 | |
| 141 | for (HUseIterator<HInstruction*> it(obj->GetUses()); !it.Done(); it.Advance()) { |
| 142 | HInstruction* user = it.Current()->GetUser(); |
| 143 | if (instanceOfTrueBlock->Dominates(user->GetBlock())) { |
| 144 | user->ReplaceInput(bound_type, it.Current()->GetIndex()); |
| 145 | } |
| 146 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void ReferenceTypePropagation::VisitNewInstance(HNewInstance* instr) { |
| 150 | ScopedObjectAccess soa(Thread::Current()); |
| 151 | mirror::DexCache* dex_cache = dex_compilation_unit_.GetClassLinker()->FindDexCache(dex_file_); |
| 152 | // Get type from dex cache assuming it was populated by the verifier. |
| 153 | mirror::Class* resolved_class = dex_cache->GetResolvedType(instr->GetTypeIndex()); |
| 154 | if (resolved_class != nullptr) { |
| 155 | MutableHandle<mirror::Class> handle = handles_->NewHandle(resolved_class); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 156 | instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(handle, true)); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | void ReferenceTypePropagation::VisitLoadClass(HLoadClass* instr) { |
| 161 | ScopedObjectAccess soa(Thread::Current()); |
| 162 | mirror::DexCache* dex_cache = dex_compilation_unit_.GetClassLinker()->FindDexCache(dex_file_); |
| 163 | // Get type from dex cache assuming it was populated by the verifier. |
| 164 | mirror::Class* resolved_class = dex_cache->GetResolvedType(instr->GetTypeIndex()); |
| 165 | if (resolved_class != nullptr) { |
| 166 | Handle<mirror::Class> handle = handles_->NewHandle(resolved_class); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 167 | instr->SetLoadedClassRTI(ReferenceTypeInfo::Create(handle, /* is_exact */ true)); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 168 | } |
| 169 | Handle<mirror::Class> class_handle = handles_->NewHandle(mirror::Class::GetJavaLangClass()); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 170 | instr->SetReferenceTypeInfo(ReferenceTypeInfo::Create(class_handle, /* is_exact */ true)); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 171 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 172 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 173 | void ReferenceTypePropagation::VisitPhi(HPhi* phi) { |
| 174 | if (phi->GetType() != Primitive::kPrimNot) { |
| 175 | return; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 176 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 177 | |
| 178 | if (phi->GetBlock()->IsLoopHeader()) { |
| 179 | // Set the initial type for the phi. Use the non back edge input for reaching |
| 180 | // a fixed point faster. |
| 181 | AddToWorklist(phi); |
| 182 | phi->SetCanBeNull(phi->InputAt(0)->CanBeNull()); |
| 183 | phi->SetReferenceTypeInfo(phi->InputAt(0)->GetReferenceTypeInfo()); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 184 | } else { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 185 | // Eagerly compute the type of the phi, for quicker convergence. Note |
| 186 | // that we don't need to add users to the worklist because we are |
| 187 | // doing a reverse post-order visit, therefore either the phi users are |
| 188 | // non-loop phi and will be visited later in the visit, or are loop-phis, |
| 189 | // and they are already in the work list. |
| 190 | UpdateNullability(phi); |
| 191 | UpdateReferenceTypeInfo(phi); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | ReferenceTypeInfo ReferenceTypePropagation::MergeTypes(const ReferenceTypeInfo& a, |
| 196 | const ReferenceTypeInfo& b) { |
| 197 | bool is_exact = a.IsExact() && b.IsExact(); |
| 198 | bool is_top = a.IsTop() || b.IsTop(); |
| 199 | Handle<mirror::Class> type_handle; |
| 200 | |
| 201 | if (!is_top) { |
| 202 | if (a.GetTypeHandle().Get() == b.GetTypeHandle().Get()) { |
| 203 | type_handle = a.GetTypeHandle(); |
| 204 | } else if (a.IsSupertypeOf(b)) { |
| 205 | type_handle = a.GetTypeHandle(); |
| 206 | is_exact = false; |
| 207 | } else if (b.IsSupertypeOf(a)) { |
| 208 | type_handle = b.GetTypeHandle(); |
| 209 | is_exact = false; |
| 210 | } else { |
| 211 | // TODO: Find a common super class. |
| 212 | is_top = true; |
| 213 | is_exact = false; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return is_top |
| 218 | ? ReferenceTypeInfo::CreateTop(is_exact) |
| 219 | : ReferenceTypeInfo::Create(type_handle, is_exact); |
| 220 | } |
| 221 | |
| 222 | bool ReferenceTypePropagation::UpdateReferenceTypeInfo(HInstruction* instr) { |
| 223 | ScopedObjectAccess soa(Thread::Current()); |
| 224 | |
| 225 | ReferenceTypeInfo previous_rti = instr->GetReferenceTypeInfo(); |
| 226 | if (instr->IsBoundType()) { |
| 227 | UpdateBoundType(instr->AsBoundType()); |
| 228 | } else if (instr->IsPhi()) { |
| 229 | UpdatePhi(instr->AsPhi()); |
| 230 | } else { |
| 231 | LOG(FATAL) << "Invalid instruction (should not get here)"; |
| 232 | } |
| 233 | |
| 234 | return !previous_rti.IsEqual(instr->GetReferenceTypeInfo()); |
| 235 | } |
| 236 | |
| 237 | void ReferenceTypePropagation::UpdateBoundType(HBoundType* instr) { |
| 238 | ReferenceTypeInfo new_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
| 239 | // Be sure that we don't go over the bounded type. |
| 240 | ReferenceTypeInfo bound_rti = instr->GetBoundType(); |
| 241 | if (!bound_rti.IsSupertypeOf(new_rti)) { |
| 242 | new_rti = bound_rti; |
| 243 | } |
| 244 | instr->SetReferenceTypeInfo(new_rti); |
| 245 | } |
| 246 | |
| 247 | void ReferenceTypePropagation::UpdatePhi(HPhi* instr) { |
| 248 | ReferenceTypeInfo new_rti = instr->InputAt(0)->GetReferenceTypeInfo(); |
| 249 | if (new_rti.IsTop() && !new_rti.IsExact()) { |
| 250 | // Early return if we are Top and inexact. |
| 251 | instr->SetReferenceTypeInfo(new_rti); |
| 252 | return; |
| 253 | } |
| 254 | for (size_t i = 1; i < instr->InputCount(); i++) { |
| 255 | new_rti = MergeTypes(new_rti, instr->InputAt(i)->GetReferenceTypeInfo()); |
| 256 | if (new_rti.IsTop()) { |
| 257 | if (!new_rti.IsExact()) { |
| 258 | break; |
| 259 | } else { |
| 260 | continue; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 261 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 264 | instr->SetReferenceTypeInfo(new_rti); |
| 265 | } |
| 266 | |
| 267 | // Re-computes and updates the nullability of the instruction. Returns whether or |
| 268 | // not the nullability was changed. |
| 269 | bool ReferenceTypePropagation::UpdateNullability(HInstruction* instr) { |
| 270 | DCHECK(instr->IsPhi() || instr->IsBoundType()); |
| 271 | |
| 272 | if (!instr->IsPhi()) { |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | HPhi* phi = instr->AsPhi(); |
| 277 | bool existing_can_be_null = phi->CanBeNull(); |
| 278 | bool new_can_be_null = false; |
| 279 | for (size_t i = 0; i < phi->InputCount(); i++) { |
| 280 | new_can_be_null |= phi->InputAt(i)->CanBeNull(); |
| 281 | } |
| 282 | phi->SetCanBeNull(new_can_be_null); |
| 283 | |
| 284 | return existing_can_be_null != new_can_be_null; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void ReferenceTypePropagation::ProcessWorklist() { |
| 288 | while (!worklist_.IsEmpty()) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 289 | HInstruction* instruction = worklist_.Pop(); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 290 | if (UpdateNullability(instruction) || UpdateReferenceTypeInfo(instruction)) { |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 291 | AddDependentInstructionsToWorklist(instruction); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 296 | void ReferenceTypePropagation::AddToWorklist(HInstruction* instruction) { |
| 297 | DCHECK_EQ(instruction->GetType(), Primitive::kPrimNot) << instruction->GetType(); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 298 | worklist_.Add(instruction); |
| 299 | } |
| 300 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 301 | void ReferenceTypePropagation::AddDependentInstructionsToWorklist(HInstruction* instruction) { |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 302 | for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) { |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 303 | HInstruction* user = it.Current()->GetUser(); |
| 304 | if (user->IsPhi() || user->IsBoundType()) { |
| 305 | AddToWorklist(user); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 309 | } // namespace art |