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