Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "ssa_builder.h" |
Nicolas Geoffray | 184d640 | 2014-06-09 14:06:02 +0100 | [diff] [blame] | 18 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 19 | #include "nodes.h" |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 20 | #include "reference_type_propagation.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 21 | #include "ssa_phi_elimination.h" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 25 | void SsaBuilder::SetLoopHeaderPhiInputs() { |
| 26 | for (size_t i = loop_headers_.size(); i > 0; --i) { |
| 27 | HBasicBlock* block = loop_headers_[i - 1]; |
| 28 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 29 | HPhi* phi = it.Current()->AsPhi(); |
| 30 | size_t vreg = phi->GetRegNumber(); |
| 31 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
| 32 | HInstruction* value = ValueOfLocal(predecessor, vreg); |
| 33 | if (value == nullptr) { |
| 34 | // Vreg is undefined at this predecessor. Mark it dead and leave with |
| 35 | // fewer inputs than predecessors. SsaChecker will fail if not removed. |
| 36 | phi->SetDead(); |
| 37 | break; |
| 38 | } else { |
| 39 | phi->AddInput(value); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 46 | void SsaBuilder::FixNullConstantType() { |
| 47 | // The order doesn't matter here. |
| 48 | for (HReversePostOrderIterator itb(*GetGraph()); !itb.Done(); itb.Advance()) { |
| 49 | for (HInstructionIterator it(itb.Current()->GetInstructions()); !it.Done(); it.Advance()) { |
| 50 | HInstruction* equality_instr = it.Current(); |
| 51 | if (!equality_instr->IsEqual() && !equality_instr->IsNotEqual()) { |
| 52 | continue; |
| 53 | } |
| 54 | HInstruction* left = equality_instr->InputAt(0); |
| 55 | HInstruction* right = equality_instr->InputAt(1); |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 56 | HInstruction* int_operand = nullptr; |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 57 | |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 58 | if ((left->GetType() == Primitive::kPrimNot) && (right->GetType() == Primitive::kPrimInt)) { |
| 59 | int_operand = right; |
| 60 | } else if ((right->GetType() == Primitive::kPrimNot) |
| 61 | && (left->GetType() == Primitive::kPrimInt)) { |
| 62 | int_operand = left; |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 63 | } else { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | // If we got here, we are comparing against a reference and the int constant |
| 68 | // should be replaced with a null constant. |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 69 | // Both type propagation and redundant phi elimination ensure `int_operand` |
| 70 | // can only be the 0 constant. |
| 71 | DCHECK(int_operand->IsIntConstant()); |
| 72 | DCHECK_EQ(0, int_operand->AsIntConstant()->GetValue()); |
| 73 | equality_instr->ReplaceInput(GetGraph()->GetNullConstant(), int_operand == right ? 1 : 0); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void SsaBuilder::EquivalentPhisCleanup() { |
| 79 | // The order doesn't matter here. |
| 80 | for (HReversePostOrderIterator itb(*GetGraph()); !itb.Done(); itb.Advance()) { |
| 81 | for (HInstructionIterator it(itb.Current()->GetPhis()); !it.Done(); it.Advance()) { |
| 82 | HPhi* phi = it.Current()->AsPhi(); |
| 83 | HPhi* next = phi->GetNextEquivalentPhiWithSameType(); |
| 84 | if (next != nullptr) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 85 | // Make sure we do not replace a live phi with a dead phi. A live phi |
| 86 | // has been handled by the type propagation phase, unlike a dead phi. |
Nicolas Geoffray | 4230e18 | 2015-06-29 14:34:46 +0100 | [diff] [blame] | 87 | if (next->IsLive()) { |
| 88 | phi->ReplaceWith(next); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 89 | phi->SetDead(); |
Nicolas Geoffray | 4230e18 | 2015-06-29 14:34:46 +0100 | [diff] [blame] | 90 | } else { |
| 91 | next->ReplaceWith(phi); |
| 92 | } |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 93 | DCHECK(next->GetNextEquivalentPhiWithSameType() == nullptr) |
| 94 | << "More then one phi equivalent with type " << phi->GetType() |
| 95 | << " found for phi" << phi->GetId(); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 101 | void SsaBuilder::FixEnvironmentPhis() { |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 102 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 103 | HBasicBlock* block = it.Current(); |
| 104 | for (HInstructionIterator it_phis(block->GetPhis()); !it_phis.Done(); it_phis.Advance()) { |
| 105 | HPhi* phi = it_phis.Current()->AsPhi(); |
| 106 | // If the phi is not dead, or has no environment uses, there is nothing to do. |
| 107 | if (!phi->IsDead() || !phi->HasEnvironmentUses()) continue; |
| 108 | HInstruction* next = phi->GetNext(); |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 109 | if (!phi->IsVRegEquivalentOf(next)) continue; |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 110 | if (next->AsPhi()->IsDead()) { |
| 111 | // If the phi equivalent is dead, check if there is another one. |
| 112 | next = next->GetNext(); |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 113 | if (!phi->IsVRegEquivalentOf(next)) continue; |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 114 | // There can be at most two phi equivalents. |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 115 | DCHECK(!phi->IsVRegEquivalentOf(next->GetNext())); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 116 | if (next->AsPhi()->IsDead()) continue; |
| 117 | } |
| 118 | // We found a live phi equivalent. Update the environment uses of `phi` with it. |
| 119 | phi->ReplaceWith(next); |
| 120 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 121 | } |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 122 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 123 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 124 | static void AddDependentInstructionsToWorklist(HInstruction* instruction, |
| 125 | ArenaVector<HPhi*>* worklist) { |
| 126 | // If `instruction` is a dead phi, type conflict was just identified. All its |
| 127 | // live phi users, and transitively users of those users, therefore need to be |
| 128 | // marked dead/conflicting too, so we add them to the worklist. Otherwise we |
| 129 | // add users whose type does not match and needs to be updated. |
| 130 | bool add_all_live_phis = instruction->IsPhi() && instruction->AsPhi()->IsDead(); |
| 131 | for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) { |
| 132 | HInstruction* user = it.Current()->GetUser(); |
| 133 | if (user->IsPhi() && user->AsPhi()->IsLive()) { |
| 134 | if (add_all_live_phis || user->GetType() != instruction->GetType()) { |
| 135 | worklist->push_back(user->AsPhi()); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Find a candidate primitive type for `phi` by merging the type of its inputs. |
| 142 | // Return false if conflict is identified. |
| 143 | static bool TypePhiFromInputs(HPhi* phi) { |
| 144 | Primitive::Type common_type = phi->GetType(); |
| 145 | |
| 146 | for (HInputIterator it(phi); !it.Done(); it.Advance()) { |
| 147 | HInstruction* input = it.Current(); |
| 148 | if (input->IsPhi() && input->AsPhi()->IsDead()) { |
| 149 | // Phis are constructed live so if an input is a dead phi, it must have |
| 150 | // been made dead due to type conflict. Mark this phi conflicting too. |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | Primitive::Type input_type = HPhi::ToPhiType(input->GetType()); |
| 155 | if (common_type == input_type) { |
| 156 | // No change in type. |
| 157 | } else if (Primitive::ComponentSize(common_type) != Primitive::ComponentSize(input_type)) { |
| 158 | // Types are of different sizes, e.g. int vs. long. Must be a conflict. |
| 159 | return false; |
| 160 | } else if (Primitive::IsIntegralType(common_type)) { |
| 161 | // Previous inputs were integral, this one is not but is of the same size. |
| 162 | // This does not imply conflict since some bytecode instruction types are |
| 163 | // ambiguous. TypeInputsOfPhi will either type them or detect a conflict. |
| 164 | DCHECK(Primitive::IsFloatingPointType(input_type) || input_type == Primitive::kPrimNot); |
| 165 | common_type = input_type; |
| 166 | } else if (Primitive::IsIntegralType(input_type)) { |
| 167 | // Input is integral, common type is not. Same as in the previous case, if |
| 168 | // there is a conflict, it will be detected during TypeInputsOfPhi. |
| 169 | DCHECK(Primitive::IsFloatingPointType(common_type) || common_type == Primitive::kPrimNot); |
| 170 | } else { |
| 171 | // Combining float and reference types. Clearly a conflict. |
| 172 | DCHECK((common_type == Primitive::kPrimFloat && input_type == Primitive::kPrimNot) || |
| 173 | (common_type == Primitive::kPrimNot && input_type == Primitive::kPrimFloat)); |
| 174 | return false; |
| 175 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 176 | } |
| 177 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 178 | // We have found a candidate type for the phi. Set it and return true. We may |
| 179 | // still discover conflict whilst typing the individual inputs in TypeInputsOfPhi. |
| 180 | phi->SetType(common_type); |
| 181 | return true; |
| 182 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 183 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 184 | // Replace inputs of `phi` to match its type. Return false if conflict is identified. |
| 185 | bool SsaBuilder::TypeInputsOfPhi(HPhi* phi, ArenaVector<HPhi*>* worklist) { |
| 186 | Primitive::Type common_type = phi->GetType(); |
| 187 | if (common_type == Primitive::kPrimVoid || Primitive::IsIntegralType(common_type)) { |
| 188 | // Phi either contains only other untyped phis (common_type == kPrimVoid), |
| 189 | // or `common_type` is integral and we do not need to retype ambiguous inputs |
| 190 | // because they are always constructed with the integral type candidate. |
| 191 | if (kIsDebugBuild) { |
| 192 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 193 | HInstruction* input = phi->InputAt(i); |
| 194 | if (common_type == Primitive::kPrimVoid) { |
| 195 | DCHECK(input->IsPhi() && input->GetType() == Primitive::kPrimVoid); |
| 196 | } else { |
| 197 | DCHECK((input->IsPhi() && input->GetType() == Primitive::kPrimVoid) || |
| 198 | HPhi::ToPhiType(input->GetType()) == common_type); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | // Inputs did not need to be replaced, hence no conflict. Report success. |
| 203 | return true; |
| 204 | } else { |
| 205 | DCHECK(common_type == Primitive::kPrimNot || Primitive::IsFloatingPointType(common_type)); |
| 206 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 207 | HInstruction* input = phi->InputAt(i); |
| 208 | if (input->GetType() != common_type) { |
| 209 | // Input type does not match phi's type. Try to retype the input or |
| 210 | // generate a suitably typed equivalent. |
| 211 | HInstruction* equivalent = (common_type == Primitive::kPrimNot) |
| 212 | ? GetReferenceTypeEquivalent(input) |
| 213 | : GetFloatOrDoubleEquivalent(input, common_type); |
| 214 | if (equivalent == nullptr) { |
| 215 | // Input could not be typed. Report conflict. |
| 216 | return false; |
| 217 | } |
| 218 | // Make sure the input did not change its type and we do not need to |
| 219 | // update its users. |
| 220 | DCHECK_NE(input, equivalent); |
| 221 | |
| 222 | phi->ReplaceInput(equivalent, i); |
| 223 | if (equivalent->IsPhi()) { |
| 224 | worklist->push_back(equivalent->AsPhi()); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | // All inputs either matched the type of the phi or we successfully replaced |
| 229 | // them with a suitable equivalent. Report success. |
| 230 | return true; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Attempt to set the primitive type of `phi` to match its inputs. Return whether |
| 235 | // it was changed by the algorithm or not. |
| 236 | bool SsaBuilder::UpdatePrimitiveType(HPhi* phi, ArenaVector<HPhi*>* worklist) { |
| 237 | DCHECK(phi->IsLive()); |
| 238 | Primitive::Type original_type = phi->GetType(); |
| 239 | |
| 240 | // Try to type the phi in two stages: |
| 241 | // (1) find a candidate type for the phi by merging types of all its inputs, |
| 242 | // (2) try to type the phi's inputs to that candidate type. |
| 243 | // Either of these stages may detect a type conflict and fail, in which case |
| 244 | // we immediately abort. |
| 245 | if (!TypePhiFromInputs(phi) || !TypeInputsOfPhi(phi, worklist)) { |
| 246 | // Conflict detected. Mark the phi dead and return true because it changed. |
| 247 | phi->SetDead(); |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | // Return true if the type of the phi has changed. |
| 252 | return phi->GetType() != original_type; |
| 253 | } |
| 254 | |
| 255 | void SsaBuilder::RunPrimitiveTypePropagation() { |
| 256 | ArenaVector<HPhi*> worklist(GetGraph()->GetArena()->Adapter()); |
| 257 | |
| 258 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 259 | HBasicBlock* block = it.Current(); |
| 260 | if (block->IsLoopHeader()) { |
| 261 | for (HInstructionIterator phi_it(block->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 262 | HPhi* phi = phi_it.Current()->AsPhi(); |
| 263 | if (phi->IsLive()) { |
| 264 | worklist.push_back(phi); |
| 265 | } |
| 266 | } |
| 267 | } else { |
| 268 | for (HInstructionIterator phi_it(block->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 269 | // Eagerly compute the type of the phi, for quicker convergence. Note |
| 270 | // that we don't need to add users to the worklist because we are |
| 271 | // doing a reverse post-order visit, therefore either the phi users are |
| 272 | // non-loop phi and will be visited later in the visit, or are loop-phis, |
| 273 | // and they are already in the work list. |
| 274 | HPhi* phi = phi_it.Current()->AsPhi(); |
| 275 | if (phi->IsLive()) { |
| 276 | UpdatePrimitiveType(phi, &worklist); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | ProcessPrimitiveTypePropagationWorklist(&worklist); |
| 283 | EquivalentPhisCleanup(); |
| 284 | } |
| 285 | |
| 286 | void SsaBuilder::ProcessPrimitiveTypePropagationWorklist(ArenaVector<HPhi*>* worklist) { |
| 287 | // Process worklist |
| 288 | while (!worklist->empty()) { |
| 289 | HPhi* phi = worklist->back(); |
| 290 | worklist->pop_back(); |
| 291 | // The phi could have been made dead as a result of conflicts while in the |
| 292 | // worklist. If it is now dead, there is no point in updating its type. |
| 293 | if (phi->IsLive() && UpdatePrimitiveType(phi, worklist)) { |
| 294 | AddDependentInstructionsToWorklist(phi, worklist); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static HArrayGet* FindFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) { |
| 300 | Primitive::Type type = aget->GetType(); |
| 301 | DCHECK(Primitive::IsIntOrLongType(type)); |
| 302 | HArrayGet* next = aget->GetNext()->AsArrayGet(); |
| 303 | return (next != nullptr && next->IsEquivalentOf(aget)) ? next : nullptr; |
| 304 | } |
| 305 | |
| 306 | static HArrayGet* CreateFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) { |
| 307 | Primitive::Type type = aget->GetType(); |
| 308 | DCHECK(Primitive::IsIntOrLongType(type)); |
| 309 | DCHECK(FindFloatOrDoubleEquivalentOfArrayGet(aget) == nullptr); |
| 310 | |
| 311 | HArrayGet* equivalent = new (aget->GetBlock()->GetGraph()->GetArena()) HArrayGet( |
| 312 | aget->GetArray(), |
| 313 | aget->GetIndex(), |
| 314 | type == Primitive::kPrimInt ? Primitive::kPrimFloat : Primitive::kPrimDouble, |
| 315 | aget->GetDexPc()); |
| 316 | aget->GetBlock()->InsertInstructionAfter(equivalent, aget); |
| 317 | return equivalent; |
| 318 | } |
| 319 | |
| 320 | // Returns true if the array input of `aget` is either of type int[] or long[]. |
| 321 | // Should only be called on ArrayGets with ambiguous type (int/float, long/double) |
| 322 | // on arrays which were typed to an array class by RTP. |
| 323 | static bool IsArrayGetOnIntegralArray(HArrayGet* aget) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 324 | ReferenceTypeInfo array_type = aget->GetArray()->GetReferenceTypeInfo(); |
| 325 | DCHECK(array_type.IsPrimitiveArrayClass()); |
| 326 | ReferenceTypeInfo::TypeHandle array_type_handle = array_type.GetTypeHandle(); |
| 327 | |
| 328 | bool is_integral_type; |
| 329 | if (Primitive::Is64BitType(aget->GetType())) { |
| 330 | is_integral_type = array_type_handle->GetComponentType()->IsPrimitiveLong(); |
| 331 | DCHECK(is_integral_type || array_type_handle->GetComponentType()->IsPrimitiveDouble()); |
| 332 | } else { |
| 333 | is_integral_type = array_type_handle->GetComponentType()->IsPrimitiveInt(); |
| 334 | DCHECK(is_integral_type || array_type_handle->GetComponentType()->IsPrimitiveFloat()); |
| 335 | } |
| 336 | return is_integral_type; |
| 337 | } |
| 338 | |
| 339 | bool SsaBuilder::FixAmbiguousArrayGets() { |
| 340 | if (ambiguous_agets_.empty()) { |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | // The wrong ArrayGet equivalent may still have Phi uses coming from ArraySet |
| 345 | // uses (because they are untyped) and environment uses (if --debuggable). |
| 346 | // After resolving all ambiguous ArrayGets, we will re-run primitive type |
| 347 | // propagation on the Phis which need to be updated. |
| 348 | ArenaVector<HPhi*> worklist(GetGraph()->GetArena()->Adapter()); |
| 349 | |
| 350 | { |
| 351 | ScopedObjectAccess soa(Thread::Current()); |
| 352 | |
| 353 | for (HArrayGet* aget_int : ambiguous_agets_) { |
| 354 | if (!aget_int->GetArray()->GetReferenceTypeInfo().IsPrimitiveArrayClass()) { |
| 355 | // RTP did not type the input array. Bail. |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | HArrayGet* aget_float = FindFloatOrDoubleEquivalentOfArrayGet(aget_int); |
| 360 | if (IsArrayGetOnIntegralArray(aget_int)) { |
| 361 | if (aget_float != nullptr) { |
| 362 | // There is a float/double equivalent. We must replace it and re-run |
| 363 | // primitive type propagation on all dependent instructions. |
| 364 | aget_float->ReplaceWith(aget_int); |
| 365 | aget_float->GetBlock()->RemoveInstruction(aget_float); |
| 366 | AddDependentInstructionsToWorklist(aget_int, &worklist); |
| 367 | } |
| 368 | } else { |
| 369 | if (aget_float == nullptr) { |
| 370 | // This is a float/double ArrayGet but there were no typed uses which |
| 371 | // would create the typed equivalent. Create it now. |
| 372 | aget_float = CreateFloatOrDoubleEquivalentOfArrayGet(aget_int); |
| 373 | } |
| 374 | // Replace the original int/long instruction. Note that it may have phi |
| 375 | // uses, environment uses, as well as real uses (from untyped ArraySets). |
| 376 | // We need to re-run primitive type propagation on its dependent instructions. |
| 377 | aget_int->ReplaceWith(aget_float); |
| 378 | aget_int->GetBlock()->RemoveInstruction(aget_int); |
| 379 | AddDependentInstructionsToWorklist(aget_float, &worklist); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // Set a flag stating that types of ArrayGets have been resolved. This is used |
| 385 | // by GetFloatOrDoubleEquivalentOfArrayGet to report conflict. |
| 386 | agets_fixed_ = true; |
| 387 | |
| 388 | if (!worklist.empty()) { |
| 389 | ProcessPrimitiveTypePropagationWorklist(&worklist); |
| 390 | EquivalentPhisCleanup(); |
| 391 | } |
| 392 | |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | BuildSsaResult SsaBuilder::BuildSsa() { |
| 397 | // 1) Visit in reverse post order. We need to have all predecessors of a block |
| 398 | // visited (with the exception of loops) in order to create the right environment |
| 399 | // for that block. For loops, we create phis whose inputs will be set in 2). |
| 400 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 401 | VisitBasicBlock(it.Current()); |
| 402 | } |
| 403 | |
| 404 | // 2) Set inputs of loop header phis. |
| 405 | SetLoopHeaderPhiInputs(); |
| 406 | |
| 407 | // 3) Propagate types of phis. At this point, phis are typed void in the general |
| 408 | // case, or float/double/reference if we created an equivalent phi. So we need |
| 409 | // to propagate the types across phis to give them a correct type. If a type |
| 410 | // conflict is detected in this stage, the phi is marked dead. |
| 411 | RunPrimitiveTypePropagation(); |
| 412 | |
| 413 | // 4) Now that the correct primitive types have been assigned, we can get rid |
| 414 | // of redundant phis. Note that we cannot do this phase before type propagation, |
| 415 | // otherwise we could get rid of phi equivalents, whose presence is a requirement |
| 416 | // for the type propagation phase. Note that this is to satisfy statement (a) |
| 417 | // of the SsaBuilder (see ssa_builder.h). |
| 418 | SsaRedundantPhiElimination(GetGraph()).Run(); |
| 419 | |
| 420 | // 5) Fix the type for null constants which are part of an equality comparison. |
| 421 | // We need to do this after redundant phi elimination, to ensure the only cases |
| 422 | // that we can see are reference comparison against 0. The redundant phi |
| 423 | // elimination ensures we do not see a phi taking two 0 constants in a HEqual |
| 424 | // or HNotEqual. |
| 425 | FixNullConstantType(); |
| 426 | |
| 427 | // 6) Compute type of reference type instructions. The pass assumes that |
| 428 | // NullConstant has been fixed up. |
| 429 | ReferenceTypePropagation(GetGraph(), handles_).Run(); |
| 430 | |
| 431 | // 7) Step 1) duplicated ArrayGet instructions with ambiguous type (int/float |
| 432 | // or long/double). Now that RTP computed the type of the array input, the |
| 433 | // ambiguity can be resolved and the correct equivalent kept. |
| 434 | if (!FixAmbiguousArrayGets()) { |
| 435 | return kBuildSsaFailAmbiguousArrayGet; |
| 436 | } |
| 437 | |
| 438 | // 8) Mark dead phis. This will mark phis which are not used by instructions |
| 439 | // or other live phis. If compiling as debuggable code, phis will also be kept |
| 440 | // live if they have an environment use. |
| 441 | SsaDeadPhiElimination dead_phi_elimimation(GetGraph()); |
| 442 | dead_phi_elimimation.MarkDeadPhis(); |
| 443 | |
| 444 | // 9) Make sure environments use the right phi equivalent: a phi marked dead |
| 445 | // can have a phi equivalent that is not dead. In that case we have to replace |
| 446 | // it with the live equivalent because deoptimization and try/catch rely on |
| 447 | // environments containing values of all live vregs at that point. Note that |
| 448 | // there can be multiple phis for the same Dex register that are live |
| 449 | // (for example when merging constants), in which case it is okay for the |
| 450 | // environments to just reference one. |
| 451 | FixEnvironmentPhis(); |
| 452 | |
| 453 | // 10) Now that the right phis are used for the environments, we can eliminate |
| 454 | // phis we do not need. Regardless of the debuggable status, this phase is |
| 455 | /// necessary for statement (b) of the SsaBuilder (see ssa_builder.h), as well |
| 456 | // as for the code generation, which does not deal with phis of conflicting |
| 457 | // input types. |
| 458 | dead_phi_elimimation.EliminateDeadPhis(); |
| 459 | |
| 460 | // 11) Clear locals. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 461 | for (HInstructionIterator it(GetGraph()->GetEntryBlock()->GetInstructions()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 462 | !it.Done(); |
| 463 | it.Advance()) { |
| 464 | HInstruction* current = it.Current(); |
Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 465 | if (current->IsLocal()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 466 | current->GetBlock()->RemoveInstruction(current); |
| 467 | } |
| 468 | } |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 469 | |
| 470 | return kBuildSsaSuccess; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 471 | } |
| 472 | |
David Brazdil | eead071 | 2015-09-18 14:58:57 +0100 | [diff] [blame] | 473 | ArenaVector<HInstruction*>* SsaBuilder::GetLocalsFor(HBasicBlock* block) { |
David Brazdil | eead071 | 2015-09-18 14:58:57 +0100 | [diff] [blame] | 474 | ArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()]; |
| 475 | const size_t vregs = GetGraph()->GetNumberOfVRegs(); |
| 476 | if (locals->empty() && vregs != 0u) { |
| 477 | locals->resize(vregs, nullptr); |
| 478 | |
| 479 | if (block->IsCatchBlock()) { |
| 480 | ArenaAllocator* arena = GetGraph()->GetArena(); |
| 481 | // We record incoming inputs of catch phis at throwing instructions and |
| 482 | // must therefore eagerly create the phis. Phis for undefined vregs will |
| 483 | // be deleted when the first throwing instruction with the vreg undefined |
| 484 | // is encountered. Unused phis will be removed by dead phi analysis. |
| 485 | for (size_t i = 0; i < vregs; ++i) { |
| 486 | // No point in creating the catch phi if it is already undefined at |
| 487 | // the first throwing instruction. |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 488 | HInstruction* current_local_value = (*current_locals_)[i]; |
| 489 | if (current_local_value != nullptr) { |
| 490 | HPhi* phi = new (arena) HPhi( |
| 491 | arena, |
| 492 | i, |
| 493 | 0, |
| 494 | current_local_value->GetType()); |
David Brazdil | eead071 | 2015-09-18 14:58:57 +0100 | [diff] [blame] | 495 | block->AddPhi(phi); |
| 496 | (*locals)[i] = phi; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | return locals; |
| 502 | } |
| 503 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 504 | HInstruction* SsaBuilder::ValueOfLocal(HBasicBlock* block, size_t local) { |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 505 | ArenaVector<HInstruction*>* locals = GetLocalsFor(block); |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 506 | return (*locals)[local]; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void SsaBuilder::VisitBasicBlock(HBasicBlock* block) { |
| 510 | current_locals_ = GetLocalsFor(block); |
| 511 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 512 | if (block->IsCatchBlock()) { |
| 513 | // Catch phis were already created and inputs collected from throwing sites. |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 514 | if (kIsDebugBuild) { |
| 515 | // Make sure there was at least one throwing instruction which initialized |
| 516 | // locals (guaranteed by HGraphBuilder) and that all try blocks have been |
| 517 | // visited already (from HTryBoundary scoping and reverse post order). |
| 518 | bool throwing_instruction_found = false; |
| 519 | bool catch_block_visited = false; |
| 520 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 521 | HBasicBlock* current = it.Current(); |
| 522 | if (current == block) { |
| 523 | catch_block_visited = true; |
| 524 | } else if (current->IsTryBlock() && |
| 525 | current->GetTryCatchInformation()->GetTryEntry().HasExceptionHandler(*block)) { |
| 526 | DCHECK(!catch_block_visited) << "Catch block visited before its try block."; |
| 527 | throwing_instruction_found |= current->HasThrowingInstructions(); |
| 528 | } |
| 529 | } |
| 530 | DCHECK(throwing_instruction_found) << "No instructions throwing into a live catch block."; |
| 531 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 532 | } else if (block->IsLoopHeader()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 533 | // If the block is a loop header, we know we only have visited the pre header |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 534 | // because we are visiting in reverse post order. We create phis for all initialized |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 535 | // locals from the pre header. Their inputs will be populated at the end of |
| 536 | // the analysis. |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 537 | for (size_t local = 0; local < current_locals_->size(); ++local) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 538 | HInstruction* incoming = ValueOfLocal(block->GetLoopInformation()->GetPreHeader(), local); |
| 539 | if (incoming != nullptr) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 540 | HPhi* phi = new (GetGraph()->GetArena()) HPhi( |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 541 | GetGraph()->GetArena(), |
| 542 | local, |
| 543 | 0, |
| 544 | incoming->GetType()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 545 | block->AddPhi(phi); |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 546 | (*current_locals_)[local] = phi; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | // Save the loop header so that the last phase of the analysis knows which |
| 550 | // blocks need to be updated. |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 551 | loop_headers_.push_back(block); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 552 | } else if (block->GetPredecessors().size() > 0) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 553 | // All predecessors have already been visited because we are visiting in reverse post order. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 554 | // We merge the values of all locals, creating phis if those values differ. |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 555 | for (size_t local = 0; local < current_locals_->size(); ++local) { |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 556 | bool one_predecessor_has_no_value = false; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 557 | bool is_different = false; |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 558 | HInstruction* value = ValueOfLocal(block->GetPredecessors()[0], local); |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 559 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 560 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
| 561 | HInstruction* current = ValueOfLocal(predecessor, local); |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 562 | if (current == nullptr) { |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 563 | one_predecessor_has_no_value = true; |
| 564 | break; |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 565 | } else if (current != value) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 566 | is_different = true; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 567 | } |
| 568 | } |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 569 | |
| 570 | if (one_predecessor_has_no_value) { |
| 571 | // If one predecessor has no value for this local, we trust the verifier has |
| 572 | // successfully checked that there is a store dominating any read after this block. |
| 573 | continue; |
| 574 | } |
| 575 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 576 | if (is_different) { |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 577 | HInstruction* first_input = ValueOfLocal(block->GetPredecessors()[0], local); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 578 | HPhi* phi = new (GetGraph()->GetArena()) HPhi( |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 579 | GetGraph()->GetArena(), |
| 580 | local, |
| 581 | block->GetPredecessors().size(), |
| 582 | first_input->GetType()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 583 | for (size_t i = 0; i < block->GetPredecessors().size(); i++) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 584 | HInstruction* pred_value = ValueOfLocal(block->GetPredecessors()[i], local); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 585 | phi->SetRawInputAt(i, pred_value); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 586 | } |
| 587 | block->AddPhi(phi); |
| 588 | value = phi; |
| 589 | } |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 590 | (*current_locals_)[local] = value; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | // Visit all instructions. The instructions of interest are: |
| 595 | // - HLoadLocal: replace them with the current value of the local. |
| 596 | // - HStoreLocal: update current value of the local and remove the instruction. |
| 597 | // - Instructions that require an environment: populate their environment |
| 598 | // with the current values of the locals. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 599 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 600 | it.Current()->Accept(this); |
| 601 | } |
| 602 | } |
| 603 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 604 | /** |
| 605 | * Constants in the Dex format are not typed. So the builder types them as |
| 606 | * integers, but when doing the SSA form, we might realize the constant |
| 607 | * is used for floating point operations. We create a floating-point equivalent |
| 608 | * constant to make the operations correctly typed. |
| 609 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 610 | HFloatConstant* SsaBuilder::GetFloatEquivalent(HIntConstant* constant) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 611 | // We place the floating point constant next to this constant. |
| 612 | HFloatConstant* result = constant->GetNext()->AsFloatConstant(); |
| 613 | if (result == nullptr) { |
| 614 | HGraph* graph = constant->GetBlock()->GetGraph(); |
| 615 | ArenaAllocator* allocator = graph->GetArena(); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 616 | result = new (allocator) HFloatConstant(bit_cast<float, int32_t>(constant->GetValue())); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 617 | constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext()); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 618 | graph->CacheFloatConstant(result); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 619 | } else { |
| 620 | // If there is already a constant with the expected type, we know it is |
| 621 | // the floating point equivalent of this constant. |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 622 | DCHECK_EQ((bit_cast<int32_t, float>(result->GetValue())), constant->GetValue()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 623 | } |
| 624 | return result; |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Wide constants in the Dex format are not typed. So the builder types them as |
| 629 | * longs, but when doing the SSA form, we might realize the constant |
| 630 | * is used for floating point operations. We create a floating-point equivalent |
| 631 | * constant to make the operations correctly typed. |
| 632 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 633 | HDoubleConstant* SsaBuilder::GetDoubleEquivalent(HLongConstant* constant) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 634 | // We place the floating point constant next to this constant. |
| 635 | HDoubleConstant* result = constant->GetNext()->AsDoubleConstant(); |
| 636 | if (result == nullptr) { |
| 637 | HGraph* graph = constant->GetBlock()->GetGraph(); |
| 638 | ArenaAllocator* allocator = graph->GetArena(); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 639 | result = new (allocator) HDoubleConstant(bit_cast<double, int64_t>(constant->GetValue())); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 640 | constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext()); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 641 | graph->CacheDoubleConstant(result); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 642 | } else { |
| 643 | // If there is already a constant with the expected type, we know it is |
| 644 | // the floating point equivalent of this constant. |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 645 | DCHECK_EQ((bit_cast<int64_t, double>(result->GetValue())), constant->GetValue()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 646 | } |
| 647 | return result; |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Because of Dex format, we might end up having the same phi being |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 652 | * used for non floating point operations and floating point / reference operations. |
| 653 | * Because we want the graph to be correctly typed (and thereafter avoid moves between |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 654 | * floating point registers and core registers), we need to create a copy of the |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 655 | * phi with a floating point / reference type. |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 656 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 657 | HPhi* SsaBuilder::GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 658 | DCHECK(phi->IsLive()) << "Cannot get equivalent of a dead phi since it would create a live one."; |
| 659 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 660 | // We place the floating point /reference phi next to this phi. |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 661 | HInstruction* next = phi->GetNext(); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 662 | if (next != nullptr |
| 663 | && next->AsPhi()->GetRegNumber() == phi->GetRegNumber() |
| 664 | && next->GetType() != type) { |
| 665 | // Move to the next phi to see if it is the one we are looking for. |
| 666 | next = next->GetNext(); |
| 667 | } |
| 668 | |
| 669 | if (next == nullptr |
| 670 | || (next->AsPhi()->GetRegNumber() != phi->GetRegNumber()) |
| 671 | || (next->GetType() != type)) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 672 | ArenaAllocator* allocator = phi->GetBlock()->GetGraph()->GetArena(); |
| 673 | HPhi* new_phi = new (allocator) HPhi(allocator, phi->GetRegNumber(), phi->InputCount(), type); |
| 674 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 675 | // Copy the inputs. Note that the graph may not be correctly typed |
| 676 | // by doing this copy, but the type propagation phase will fix it. |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 677 | new_phi->SetRawInputAt(i, phi->InputAt(i)); |
| 678 | } |
| 679 | phi->GetBlock()->InsertPhiAfter(new_phi, phi); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 680 | DCHECK(new_phi->IsLive()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 681 | return new_phi; |
| 682 | } else { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 683 | // An existing equivalent was found. If it is dead, conflict was previously |
| 684 | // identified and we return nullptr instead. |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 685 | HPhi* next_phi = next->AsPhi(); |
| 686 | DCHECK_EQ(next_phi->GetType(), type); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 687 | return next_phi->IsLive() ? next_phi : nullptr; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 691 | HArrayGet* SsaBuilder::GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) { |
| 692 | DCHECK(Primitive::IsIntegralType(aget->GetType())); |
| 693 | |
| 694 | if (!Primitive::IsIntOrLongType(aget->GetType())) { |
| 695 | // Cannot type boolean, char, byte, short to float/double. |
| 696 | return nullptr; |
| 697 | } |
| 698 | |
| 699 | DCHECK(ContainsElement(ambiguous_agets_, aget)); |
| 700 | if (agets_fixed_) { |
| 701 | // This used to be an ambiguous ArrayGet but its type has been resolved to |
| 702 | // int/long. Requesting a float/double equivalent should lead to a conflict. |
| 703 | if (kIsDebugBuild) { |
| 704 | ScopedObjectAccess soa(Thread::Current()); |
| 705 | DCHECK(IsArrayGetOnIntegralArray(aget)); |
| 706 | } |
| 707 | return nullptr; |
| 708 | } else { |
| 709 | // This is an ambiguous ArrayGet which has not been resolved yet. Return an |
| 710 | // equivalent float/double instruction to use until it is resolved. |
| 711 | HArrayGet* equivalent = FindFloatOrDoubleEquivalentOfArrayGet(aget); |
| 712 | return (equivalent == nullptr) ? CreateFloatOrDoubleEquivalentOfArrayGet(aget) : equivalent; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | HInstruction* SsaBuilder::GetFloatOrDoubleEquivalent(HInstruction* value, Primitive::Type type) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 717 | if (value->IsArrayGet()) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 718 | return GetFloatOrDoubleEquivalentOfArrayGet(value->AsArrayGet()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 719 | } else if (value->IsLongConstant()) { |
| 720 | return GetDoubleEquivalent(value->AsLongConstant()); |
| 721 | } else if (value->IsIntConstant()) { |
| 722 | return GetFloatEquivalent(value->AsIntConstant()); |
| 723 | } else if (value->IsPhi()) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 724 | return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), type); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 725 | } else { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 726 | return nullptr; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 730 | HInstruction* SsaBuilder::GetReferenceTypeEquivalent(HInstruction* value) { |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 731 | if (value->IsIntConstant() && value->AsIntConstant()->GetValue() == 0) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 732 | return value->GetBlock()->GetGraph()->GetNullConstant(); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 733 | } else if (value->IsPhi()) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 734 | return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), Primitive::kPrimNot); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 735 | } else { |
| 736 | return nullptr; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 740 | void SsaBuilder::VisitLoadLocal(HLoadLocal* load) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 741 | Primitive::Type load_type = load->GetType(); |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 742 | HInstruction* value = (*current_locals_)[load->GetLocal()->GetRegNumber()]; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 743 | // If the operation requests a specific type, we make sure its input is of that type. |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 744 | if (load_type != value->GetType()) { |
| 745 | if (load_type == Primitive::kPrimFloat || load_type == Primitive::kPrimDouble) { |
| 746 | value = GetFloatOrDoubleEquivalent(value, load_type); |
| 747 | } else if (load_type == Primitive::kPrimNot) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 748 | value = GetReferenceTypeEquivalent(value); |
| 749 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 750 | } |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 751 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 752 | load->ReplaceWith(value); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 753 | load->GetBlock()->RemoveInstruction(load); |
| 754 | } |
| 755 | |
| 756 | void SsaBuilder::VisitStoreLocal(HStoreLocal* store) { |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 757 | uint32_t reg_number = store->GetLocal()->GetRegNumber(); |
| 758 | HInstruction* stored_value = store->InputAt(1); |
| 759 | Primitive::Type stored_type = stored_value->GetType(); |
| 760 | DCHECK_NE(stored_type, Primitive::kPrimVoid); |
| 761 | |
| 762 | // Storing into vreg `reg_number` may implicitly invalidate the surrounding |
| 763 | // registers. Consider the following cases: |
| 764 | // (1) Storing a wide value must overwrite previous values in both `reg_number` |
| 765 | // and `reg_number+1`. We store `nullptr` in `reg_number+1`. |
| 766 | // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number` |
| 767 | // must invalidate it. We store `nullptr` in `reg_number-1`. |
| 768 | // Consequently, storing a wide value into the high vreg of another wide value |
| 769 | // will invalidate both `reg_number-1` and `reg_number+1`. |
| 770 | |
| 771 | if (reg_number != 0) { |
| 772 | HInstruction* local_low = (*current_locals_)[reg_number - 1]; |
| 773 | if (local_low != nullptr && Primitive::Is64BitType(local_low->GetType())) { |
| 774 | // The vreg we are storing into was previously the high vreg of a pair. |
| 775 | // We need to invalidate its low vreg. |
| 776 | DCHECK((*current_locals_)[reg_number] == nullptr); |
| 777 | (*current_locals_)[reg_number - 1] = nullptr; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | (*current_locals_)[reg_number] = stored_value; |
| 782 | if (Primitive::Is64BitType(stored_type)) { |
| 783 | // We are storing a pair. Invalidate the instruction in the high vreg. |
| 784 | (*current_locals_)[reg_number + 1] = nullptr; |
| 785 | } |
| 786 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 787 | store->GetBlock()->RemoveInstruction(store); |
| 788 | } |
| 789 | |
| 790 | void SsaBuilder::VisitInstruction(HInstruction* instruction) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 791 | if (instruction->NeedsEnvironment()) { |
| 792 | HEnvironment* environment = new (GetGraph()->GetArena()) HEnvironment( |
| 793 | GetGraph()->GetArena(), |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 794 | current_locals_->size(), |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 795 | GetGraph()->GetDexFile(), |
| 796 | GetGraph()->GetMethodIdx(), |
| 797 | instruction->GetDexPc(), |
| 798 | GetGraph()->GetInvokeType(), |
| 799 | instruction); |
| 800 | environment->CopyFrom(*current_locals_); |
| 801 | instruction->SetRawEnvironment(environment); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 802 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 803 | |
| 804 | // If in a try block, propagate values of locals into catch blocks. |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 805 | if (instruction->CanThrowIntoCatchBlock()) { |
| 806 | const HTryBoundary& try_entry = |
| 807 | instruction->GetBlock()->GetTryCatchInformation()->GetTryEntry(); |
David Brazdil | d26a411 | 2015-11-10 11:07:31 +0000 | [diff] [blame] | 808 | for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) { |
David Brazdil | 3eaa32f | 2015-09-18 10:58:32 +0100 | [diff] [blame] | 809 | ArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block); |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 810 | DCHECK_EQ(handler_locals->size(), current_locals_->size()); |
David Brazdil | 3eaa32f | 2015-09-18 10:58:32 +0100 | [diff] [blame] | 811 | for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) { |
| 812 | HInstruction* handler_value = (*handler_locals)[vreg]; |
| 813 | if (handler_value == nullptr) { |
| 814 | // Vreg was undefined at a previously encountered throwing instruction |
| 815 | // and the catch phi was deleted. Do not record the local value. |
| 816 | continue; |
| 817 | } |
| 818 | DCHECK(handler_value->IsPhi()); |
| 819 | |
| 820 | HInstruction* local_value = (*current_locals_)[vreg]; |
| 821 | if (local_value == nullptr) { |
| 822 | // This is the first instruction throwing into `catch_block` where |
| 823 | // `vreg` is undefined. Delete the catch phi. |
| 824 | catch_block->RemovePhi(handler_value->AsPhi()); |
| 825 | (*handler_locals)[vreg] = nullptr; |
| 826 | } else { |
| 827 | // Vreg has been defined at all instructions throwing into `catch_block` |
| 828 | // encountered so far. Record the local value in the catch phi. |
| 829 | handler_value->AsPhi()->AddInput(local_value); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | } |
| 833 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 834 | } |
| 835 | |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 836 | void SsaBuilder::VisitTemporary(HTemporary* temp) { |
| 837 | // Temporaries are only used by the baseline register allocator. |
| 838 | temp->GetBlock()->RemoveInstruction(temp); |
| 839 | } |
| 840 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame^] | 841 | void SsaBuilder::VisitArrayGet(HArrayGet* aget) { |
| 842 | Primitive::Type type = aget->GetType(); |
| 843 | DCHECK(!Primitive::IsFloatingPointType(type)); |
| 844 | if (Primitive::IsIntOrLongType(type)) { |
| 845 | ambiguous_agets_.push_back(aget); |
| 846 | } |
| 847 | VisitInstruction(aget); |
| 848 | } |
| 849 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 850 | } // namespace art |