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" |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 20 | #include "primitive_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 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 25 | /** |
| 26 | * A debuggable application may require to reviving phis, to ensure their |
| 27 | * associated DEX register is available to a debugger. This class implements |
| 28 | * the logic for statement (c) of the SsaBuilder (see ssa_builder.h). It |
| 29 | * also makes sure that phis with incompatible input types are not revived |
| 30 | * (statement (b) of the SsaBuilder). |
| 31 | * |
| 32 | * This phase must be run after detecting dead phis through the |
| 33 | * DeadPhiElimination phase, and before deleting the dead phis. |
| 34 | */ |
| 35 | class DeadPhiHandling : public ValueObject { |
| 36 | public: |
| 37 | explicit DeadPhiHandling(HGraph* graph) |
| 38 | : graph_(graph), worklist_(graph->GetArena(), kDefaultWorklistSize) {} |
| 39 | |
| 40 | void Run(); |
| 41 | |
| 42 | private: |
| 43 | void VisitBasicBlock(HBasicBlock* block); |
| 44 | void ProcessWorklist(); |
| 45 | void AddToWorklist(HPhi* phi); |
| 46 | void AddDependentInstructionsToWorklist(HPhi* phi); |
| 47 | bool UpdateType(HPhi* phi); |
| 48 | |
| 49 | HGraph* const graph_; |
| 50 | GrowableArray<HPhi*> worklist_; |
| 51 | |
| 52 | static constexpr size_t kDefaultWorklistSize = 8; |
| 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(DeadPhiHandling); |
| 55 | }; |
| 56 | |
| 57 | bool DeadPhiHandling::UpdateType(HPhi* phi) { |
| 58 | Primitive::Type existing = phi->GetType(); |
| 59 | DCHECK(phi->IsLive()); |
| 60 | |
| 61 | bool conflict = false; |
| 62 | Primitive::Type new_type = existing; |
| 63 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 64 | HInstruction* input = phi->InputAt(i); |
| 65 | if (input->IsPhi() && input->AsPhi()->IsDead()) { |
| 66 | // We are doing a reverse post order visit of the graph, reviving |
| 67 | // phis that have environment uses and updating their types. If an |
| 68 | // input is a phi, and it is dead (because its input types are |
| 69 | // conflicting), this phi must be marked dead as well. |
| 70 | conflict = true; |
| 71 | break; |
| 72 | } |
| 73 | Primitive::Type input_type = HPhi::ToPhiType(input->GetType()); |
| 74 | |
| 75 | // The only acceptable transitions are: |
| 76 | // - From void to typed: first time we update the type of this phi. |
| 77 | // - From int to reference (or reference to int): the phi has to change |
| 78 | // to reference type. If the integer input cannot be converted to a |
| 79 | // reference input, the phi will remain dead. |
| 80 | if (new_type == Primitive::kPrimVoid) { |
| 81 | new_type = input_type; |
| 82 | } else if (new_type == Primitive::kPrimNot && input_type == Primitive::kPrimInt) { |
| 83 | HInstruction* equivalent = SsaBuilder::GetReferenceTypeEquivalent(input); |
| 84 | if (equivalent == nullptr) { |
| 85 | conflict = true; |
| 86 | break; |
| 87 | } else { |
| 88 | phi->ReplaceInput(equivalent, i); |
| 89 | if (equivalent->IsPhi()) { |
| 90 | DCHECK_EQ(equivalent->GetType(), Primitive::kPrimNot); |
| 91 | // We created a new phi, but that phi has the same inputs as the old phi. We |
| 92 | // add it to the worklist to ensure its inputs can also be converted to reference. |
| 93 | // If not, it will remain dead, and the algorithm will make the current phi dead |
| 94 | // as well. |
| 95 | equivalent->AsPhi()->SetLive(); |
| 96 | AddToWorklist(equivalent->AsPhi()); |
| 97 | } |
| 98 | } |
| 99 | } else if (new_type == Primitive::kPrimInt && input_type == Primitive::kPrimNot) { |
| 100 | new_type = Primitive::kPrimNot; |
| 101 | // Start over, we may request reference equivalents for the inputs of the phi. |
| 102 | i = -1; |
| 103 | } else if (new_type != input_type) { |
| 104 | conflict = true; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (conflict) { |
| 110 | phi->SetType(Primitive::kPrimVoid); |
| 111 | phi->SetDead(); |
| 112 | return true; |
| 113 | } else { |
| 114 | DCHECK(phi->IsLive()); |
| 115 | phi->SetType(new_type); |
| 116 | return existing != new_type; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void DeadPhiHandling::VisitBasicBlock(HBasicBlock* block) { |
| 121 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 122 | HPhi* phi = it.Current()->AsPhi(); |
| 123 | if (phi->IsDead() && phi->HasEnvironmentUses()) { |
| 124 | phi->SetLive(); |
| 125 | if (block->IsLoopHeader()) { |
| 126 | // Give a type to the loop phi, to guarantee convergence of the algorithm. |
| 127 | phi->SetType(phi->InputAt(0)->GetType()); |
| 128 | AddToWorklist(phi); |
| 129 | } else { |
| 130 | // Because we are doing a reverse post order visit, all inputs of |
| 131 | // this phi have been visited and therefore had their (initial) type set. |
| 132 | UpdateType(phi); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void DeadPhiHandling::ProcessWorklist() { |
| 139 | while (!worklist_.IsEmpty()) { |
| 140 | HPhi* instruction = worklist_.Pop(); |
| 141 | // Note that the same equivalent phi can be added multiple times in the work list, if |
| 142 | // used by multiple phis. The first call to `UpdateType` will know whether the phi is |
| 143 | // dead or live. |
| 144 | if (instruction->IsLive() && UpdateType(instruction)) { |
| 145 | AddDependentInstructionsToWorklist(instruction); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void DeadPhiHandling::AddToWorklist(HPhi* instruction) { |
| 151 | DCHECK(instruction->IsLive()); |
| 152 | worklist_.Add(instruction); |
| 153 | } |
| 154 | |
| 155 | void DeadPhiHandling::AddDependentInstructionsToWorklist(HPhi* instruction) { |
| 156 | for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) { |
| 157 | HPhi* phi = it.Current()->GetUser()->AsPhi(); |
| 158 | if (phi != nullptr && !phi->IsDead()) { |
| 159 | AddToWorklist(phi); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void DeadPhiHandling::Run() { |
| 165 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 166 | VisitBasicBlock(it.Current()); |
| 167 | } |
| 168 | ProcessWorklist(); |
| 169 | } |
| 170 | |
| 171 | static bool IsPhiEquivalentOf(HInstruction* instruction, HPhi* phi) { |
| 172 | return instruction != nullptr |
| 173 | && instruction->IsPhi() |
| 174 | && instruction->AsPhi()->GetRegNumber() == phi->GetRegNumber(); |
| 175 | } |
| 176 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 177 | void SsaBuilder::FixNullConstantType() { |
| 178 | // The order doesn't matter here. |
| 179 | for (HReversePostOrderIterator itb(*GetGraph()); !itb.Done(); itb.Advance()) { |
| 180 | for (HInstructionIterator it(itb.Current()->GetInstructions()); !it.Done(); it.Advance()) { |
| 181 | HInstruction* equality_instr = it.Current(); |
| 182 | if (!equality_instr->IsEqual() && !equality_instr->IsNotEqual()) { |
| 183 | continue; |
| 184 | } |
| 185 | HInstruction* left = equality_instr->InputAt(0); |
| 186 | HInstruction* right = equality_instr->InputAt(1); |
| 187 | HInstruction* null_instr = nullptr; |
| 188 | |
Calin Juravle | 3192540 | 2015-04-17 10:36:57 +0100 | [diff] [blame] | 189 | if ((left->GetType() == Primitive::kPrimNot) && right->IsIntConstant()) { |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 190 | null_instr = right; |
Calin Juravle | 3192540 | 2015-04-17 10:36:57 +0100 | [diff] [blame] | 191 | } else if ((right->GetType() == Primitive::kPrimNot) && left->IsIntConstant()) { |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 192 | null_instr = left; |
| 193 | } else { |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | // If we got here, we are comparing against a reference and the int constant |
| 198 | // should be replaced with a null constant. |
| 199 | if (null_instr->IsIntConstant()) { |
| 200 | DCHECK_EQ(0, null_instr->AsIntConstant()->GetValue()); |
| 201 | equality_instr->ReplaceInput(GetGraph()->GetNullConstant(), null_instr == right ? 1 : 0); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void SsaBuilder::EquivalentPhisCleanup() { |
| 208 | // The order doesn't matter here. |
| 209 | for (HReversePostOrderIterator itb(*GetGraph()); !itb.Done(); itb.Advance()) { |
| 210 | for (HInstructionIterator it(itb.Current()->GetPhis()); !it.Done(); it.Advance()) { |
| 211 | HPhi* phi = it.Current()->AsPhi(); |
| 212 | HPhi* next = phi->GetNextEquivalentPhiWithSameType(); |
| 213 | if (next != nullptr) { |
| 214 | phi->ReplaceWith(next); |
| 215 | DCHECK(next->GetNextEquivalentPhiWithSameType() == nullptr) |
| 216 | << "More then one phi equivalent with type " << phi->GetType() |
| 217 | << " found for phi" << phi->GetId(); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 223 | void SsaBuilder::BuildSsa() { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 224 | // 1) Visit in reverse post order. We need to have all predecessors of a block visited |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 225 | // (with the exception of loops) in order to create the right environment for that |
| 226 | // block. For loops, we create phis whose inputs will be set in 2). |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 227 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 228 | VisitBasicBlock(it.Current()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // 2) Set inputs of loop phis. |
| 232 | for (size_t i = 0; i < loop_headers_.Size(); i++) { |
| 233 | HBasicBlock* block = loop_headers_.Get(i); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 234 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 235 | HPhi* phi = it.Current()->AsPhi(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 236 | for (size_t pred = 0; pred < block->GetPredecessors().Size(); pred++) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 237 | HInstruction* input = ValueOfLocal(block->GetPredecessors().Get(pred), phi->GetRegNumber()); |
| 238 | phi->AddInput(input); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 243 | // 3) Mark dead phis. This will mark phis that are only used by environments: |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 244 | // at the DEX level, the type of these phis does not need to be consistent, but |
| 245 | // our code generator will complain if the inputs of a phi do not have the same |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 246 | // type. The marking allows the type propagation to know which phis it needs |
| 247 | // to handle. We mark but do not eliminate: the elimination will be done in |
Nicolas Geoffray | b59dba0 | 2015-03-11 18:13:21 +0000 | [diff] [blame] | 248 | // step 9). |
| 249 | SsaDeadPhiElimination dead_phis_for_type_propagation(GetGraph()); |
| 250 | dead_phis_for_type_propagation.MarkDeadPhis(); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 251 | |
| 252 | // 4) Propagate types of phis. At this point, phis are typed void in the general |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 253 | // case, or float/double/reference when we created an equivalent phi. So we |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 254 | // need to propagate the types across phis to give them a correct type. |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 255 | PrimitiveTypePropagation type_propagation(GetGraph()); |
Nicolas Geoffray | 184d640 | 2014-06-09 14:06:02 +0100 | [diff] [blame] | 256 | type_propagation.Run(); |
| 257 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 258 | // 5) Fix the type for null constants which are part of an equality comparison. |
| 259 | FixNullConstantType(); |
| 260 | |
| 261 | // 6) When creating equivalent phis we copy the inputs of the original phi which |
| 262 | // may be improperly typed. This will be fixed during the type propagation but |
| 263 | // as a result we may end up with two equivalent phis with the same type for |
| 264 | // the same dex register. This pass cleans them up. |
| 265 | EquivalentPhisCleanup(); |
| 266 | |
| 267 | // 7) Mark dead phis again. Step 4) may have introduced new phis. |
| 268 | // Step 6) might enable the death of new phis. |
Nicolas Geoffray | b59dba0 | 2015-03-11 18:13:21 +0000 | [diff] [blame] | 269 | SsaDeadPhiElimination dead_phis(GetGraph()); |
| 270 | dead_phis.MarkDeadPhis(); |
| 271 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 272 | // 8) Now that the graph is correctly typed, we can get rid of redundant phis. |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 273 | // Note that we cannot do this phase before type propagation, otherwise |
| 274 | // we could get rid of phi equivalents, whose presence is a requirement for the |
| 275 | // type propagation phase. Note that this is to satisfy statement (a) of the |
| 276 | // SsaBuilder (see ssa_builder.h). |
| 277 | SsaRedundantPhiElimination redundant_phi(GetGraph()); |
| 278 | redundant_phi.Run(); |
| 279 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 280 | // 9) Make sure environments use the right phi "equivalent": a phi marked dead |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 281 | // can have a phi equivalent that is not dead. We must therefore update |
| 282 | // all environment uses of the dead phi to use its equivalent. Note that there |
| 283 | // can be multiple phis for the same Dex register that are live (for example |
| 284 | // when merging constants), in which case it is OK for the environments |
| 285 | // to just reference one. |
| 286 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 287 | HBasicBlock* block = it.Current(); |
| 288 | for (HInstructionIterator it_phis(block->GetPhis()); !it_phis.Done(); it_phis.Advance()) { |
| 289 | HPhi* phi = it_phis.Current()->AsPhi(); |
| 290 | // If the phi is not dead, or has no environment uses, there is nothing to do. |
| 291 | if (!phi->IsDead() || !phi->HasEnvironmentUses()) continue; |
| 292 | HInstruction* next = phi->GetNext(); |
| 293 | if (!IsPhiEquivalentOf(next, phi)) continue; |
| 294 | if (next->AsPhi()->IsDead()) { |
| 295 | // If the phi equivalent is dead, check if there is another one. |
| 296 | next = next->GetNext(); |
| 297 | if (!IsPhiEquivalentOf(next, phi)) continue; |
| 298 | // There can be at most two phi equivalents. |
| 299 | DCHECK(!IsPhiEquivalentOf(next->GetNext(), phi)); |
| 300 | if (next->AsPhi()->IsDead()) continue; |
| 301 | } |
| 302 | // We found a live phi equivalent. Update the environment uses of `phi` with it. |
| 303 | phi->ReplaceWith(next); |
| 304 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 307 | // 10) Deal with phis to guarantee liveness of phis in case of a debuggable |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 308 | // application. This is for satisfying statement (c) of the SsaBuilder |
| 309 | // (see ssa_builder.h). |
| 310 | if (GetGraph()->IsDebuggable()) { |
| 311 | DeadPhiHandling dead_phi_handler(GetGraph()); |
| 312 | dead_phi_handler.Run(); |
| 313 | } |
| 314 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 315 | // 11) Now that the right phis are used for the environments, and we |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 316 | // have potentially revive dead phis in case of a debuggable application, |
| 317 | // we can eliminate phis we do not need. Regardless of the debuggable status, |
| 318 | // this phase is necessary for statement (b) of the SsaBuilder (see ssa_builder.h), |
| 319 | // as well as for the code generation, which does not deal with phis of conflicting |
| 320 | // input types. |
| 321 | dead_phis.EliminateDeadPhis(); |
| 322 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 323 | // 12) Clear locals. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 324 | for (HInstructionIterator it(GetGraph()->GetEntryBlock()->GetInstructions()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 325 | !it.Done(); |
| 326 | it.Advance()) { |
| 327 | HInstruction* current = it.Current(); |
Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 328 | if (current->IsLocal()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 329 | current->GetBlock()->RemoveInstruction(current); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | HInstruction* SsaBuilder::ValueOfLocal(HBasicBlock* block, size_t local) { |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 335 | return GetLocalsFor(block)->Get(local); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void SsaBuilder::VisitBasicBlock(HBasicBlock* block) { |
| 339 | current_locals_ = GetLocalsFor(block); |
| 340 | |
| 341 | if (block->IsLoopHeader()) { |
| 342 | // 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] | 343 | // 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] | 344 | // locals from the pre header. Their inputs will be populated at the end of |
| 345 | // the analysis. |
| 346 | for (size_t local = 0; local < current_locals_->Size(); local++) { |
| 347 | HInstruction* incoming = ValueOfLocal(block->GetLoopInformation()->GetPreHeader(), local); |
| 348 | if (incoming != nullptr) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 349 | HPhi* phi = new (GetGraph()->GetArena()) HPhi( |
| 350 | GetGraph()->GetArena(), local, 0, Primitive::kPrimVoid); |
| 351 | block->AddPhi(phi); |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 352 | current_locals_->Put(local, phi); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | // Save the loop header so that the last phase of the analysis knows which |
| 356 | // blocks need to be updated. |
| 357 | loop_headers_.Add(block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 358 | } else if (block->GetPredecessors().Size() > 0) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 359 | // 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] | 360 | // We merge the values of all locals, creating phis if those values differ. |
| 361 | for (size_t local = 0; local < current_locals_->Size(); local++) { |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 362 | bool one_predecessor_has_no_value = false; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 363 | bool is_different = false; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 364 | HInstruction* value = ValueOfLocal(block->GetPredecessors().Get(0), local); |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 365 | |
| 366 | for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) { |
| 367 | HInstruction* current = ValueOfLocal(block->GetPredecessors().Get(i), local); |
| 368 | if (current == nullptr) { |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 369 | one_predecessor_has_no_value = true; |
| 370 | break; |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 371 | } else if (current != value) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 372 | is_different = true; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 373 | } |
| 374 | } |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 375 | |
| 376 | if (one_predecessor_has_no_value) { |
| 377 | // If one predecessor has no value for this local, we trust the verifier has |
| 378 | // successfully checked that there is a store dominating any read after this block. |
| 379 | continue; |
| 380 | } |
| 381 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 382 | if (is_different) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 383 | HPhi* phi = new (GetGraph()->GetArena()) HPhi( |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 384 | GetGraph()->GetArena(), local, block->GetPredecessors().Size(), Primitive::kPrimVoid); |
| 385 | for (size_t i = 0; i < block->GetPredecessors().Size(); i++) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 386 | HInstruction* pred_value = ValueOfLocal(block->GetPredecessors().Get(i), local); |
| 387 | phi->SetRawInputAt(i, pred_value); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 388 | } |
| 389 | block->AddPhi(phi); |
| 390 | value = phi; |
| 391 | } |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 392 | current_locals_->Put(local, value); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | // Visit all instructions. The instructions of interest are: |
| 397 | // - HLoadLocal: replace them with the current value of the local. |
| 398 | // - HStoreLocal: update current value of the local and remove the instruction. |
| 399 | // - Instructions that require an environment: populate their environment |
| 400 | // with the current values of the locals. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 401 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 402 | it.Current()->Accept(this); |
| 403 | } |
| 404 | } |
| 405 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 406 | /** |
| 407 | * Constants in the Dex format are not typed. So the builder types them as |
| 408 | * integers, but when doing the SSA form, we might realize the constant |
| 409 | * is used for floating point operations. We create a floating-point equivalent |
| 410 | * constant to make the operations correctly typed. |
| 411 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 412 | HFloatConstant* SsaBuilder::GetFloatEquivalent(HIntConstant* constant) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 413 | // We place the floating point constant next to this constant. |
| 414 | HFloatConstant* result = constant->GetNext()->AsFloatConstant(); |
| 415 | if (result == nullptr) { |
| 416 | HGraph* graph = constant->GetBlock()->GetGraph(); |
| 417 | ArenaAllocator* allocator = graph->GetArena(); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 418 | result = new (allocator) HFloatConstant(bit_cast<float, int32_t>(constant->GetValue())); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 419 | constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext()); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 420 | graph->CacheFloatConstant(result); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 421 | } else { |
| 422 | // If there is already a constant with the expected type, we know it is |
| 423 | // the floating point equivalent of this constant. |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 424 | DCHECK_EQ((bit_cast<int32_t, float>(result->GetValue())), constant->GetValue()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 425 | } |
| 426 | return result; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Wide constants in the Dex format are not typed. So the builder types them as |
| 431 | * longs, but when doing the SSA form, we might realize the constant |
| 432 | * is used for floating point operations. We create a floating-point equivalent |
| 433 | * constant to make the operations correctly typed. |
| 434 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 435 | HDoubleConstant* SsaBuilder::GetDoubleEquivalent(HLongConstant* constant) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 436 | // We place the floating point constant next to this constant. |
| 437 | HDoubleConstant* result = constant->GetNext()->AsDoubleConstant(); |
| 438 | if (result == nullptr) { |
| 439 | HGraph* graph = constant->GetBlock()->GetGraph(); |
| 440 | ArenaAllocator* allocator = graph->GetArena(); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 441 | result = new (allocator) HDoubleConstant(bit_cast<double, int64_t>(constant->GetValue())); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 442 | constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext()); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 443 | graph->CacheDoubleConstant(result); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 444 | } else { |
| 445 | // If there is already a constant with the expected type, we know it is |
| 446 | // the floating point equivalent of this constant. |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 447 | DCHECK_EQ((bit_cast<int64_t, double>(result->GetValue())), constant->GetValue()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 448 | } |
| 449 | return result; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * 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] | 454 | * used for non floating point operations and floating point / reference operations. |
| 455 | * 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] | 456 | * 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] | 457 | * phi with a floating point / reference type. |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 458 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 459 | HPhi* SsaBuilder::GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 460 | // We place the floating point /reference phi next to this phi. |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 461 | HInstruction* next = phi->GetNext(); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 462 | if (next != nullptr |
| 463 | && next->AsPhi()->GetRegNumber() == phi->GetRegNumber() |
| 464 | && next->GetType() != type) { |
| 465 | // Move to the next phi to see if it is the one we are looking for. |
| 466 | next = next->GetNext(); |
| 467 | } |
| 468 | |
| 469 | if (next == nullptr |
| 470 | || (next->AsPhi()->GetRegNumber() != phi->GetRegNumber()) |
| 471 | || (next->GetType() != type)) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 472 | ArenaAllocator* allocator = phi->GetBlock()->GetGraph()->GetArena(); |
| 473 | HPhi* new_phi = new (allocator) HPhi(allocator, phi->GetRegNumber(), phi->InputCount(), type); |
| 474 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 475 | // Copy the inputs. Note that the graph may not be correctly typed by doing this copy, |
| 476 | // but the type propagation phase will fix it. |
| 477 | new_phi->SetRawInputAt(i, phi->InputAt(i)); |
| 478 | } |
| 479 | phi->GetBlock()->InsertPhiAfter(new_phi, phi); |
| 480 | return new_phi; |
| 481 | } else { |
Nicolas Geoffray | 21cc798 | 2014-11-17 17:50:33 +0000 | [diff] [blame] | 482 | DCHECK_EQ(next->GetType(), type); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 483 | return next->AsPhi(); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | HInstruction* SsaBuilder::GetFloatOrDoubleEquivalent(HInstruction* user, |
| 488 | HInstruction* value, |
| 489 | Primitive::Type type) { |
| 490 | if (value->IsArrayGet()) { |
| 491 | // The verifier has checked that values in arrays cannot be used for both |
| 492 | // floating point and non-floating point operations. It is therefore safe to just |
| 493 | // change the type of the operation. |
| 494 | value->AsArrayGet()->SetType(type); |
| 495 | return value; |
| 496 | } else if (value->IsLongConstant()) { |
| 497 | return GetDoubleEquivalent(value->AsLongConstant()); |
| 498 | } else if (value->IsIntConstant()) { |
| 499 | return GetFloatEquivalent(value->AsIntConstant()); |
| 500 | } else if (value->IsPhi()) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 501 | return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), type); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 502 | } else { |
| 503 | // For other instructions, we assume the verifier has checked that the dex format is correctly |
| 504 | // typed and the value in a dex register will not be used for both floating point and |
| 505 | // non-floating point operations. So the only reason an instruction would want a floating |
| 506 | // point equivalent is for an unused phi that will be removed by the dead phi elimination phase. |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 507 | DCHECK(user->IsPhi()) << "is actually " << user->DebugName() << " (" << user->GetId() << ")"; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 508 | return value; |
| 509 | } |
| 510 | } |
| 511 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 512 | HInstruction* SsaBuilder::GetReferenceTypeEquivalent(HInstruction* value) { |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 513 | if (value->IsIntConstant() && value->AsIntConstant()->GetValue() == 0) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 514 | return value->GetBlock()->GetGraph()->GetNullConstant(); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 515 | } else if (value->IsPhi()) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 516 | return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), Primitive::kPrimNot); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 517 | } else { |
| 518 | return nullptr; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 522 | void SsaBuilder::VisitLoadLocal(HLoadLocal* load) { |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 523 | HInstruction* value = current_locals_->Get(load->GetLocal()->GetRegNumber()); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 524 | // If the operation requests a specific type, we make sure its input is of that type. |
| 525 | if (load->GetType() != value->GetType()) { |
| 526 | if (load->GetType() == Primitive::kPrimFloat || load->GetType() == Primitive::kPrimDouble) { |
| 527 | value = GetFloatOrDoubleEquivalent(load, value, load->GetType()); |
| 528 | } else if (load->GetType() == Primitive::kPrimNot) { |
| 529 | value = GetReferenceTypeEquivalent(value); |
| 530 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 531 | } |
| 532 | load->ReplaceWith(value); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 533 | load->GetBlock()->RemoveInstruction(load); |
| 534 | } |
| 535 | |
| 536 | void SsaBuilder::VisitStoreLocal(HStoreLocal* store) { |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 537 | current_locals_->Put(store->GetLocal()->GetRegNumber(), store->InputAt(1)); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 538 | store->GetBlock()->RemoveInstruction(store); |
| 539 | } |
| 540 | |
| 541 | void SsaBuilder::VisitInstruction(HInstruction* instruction) { |
| 542 | if (!instruction->NeedsEnvironment()) { |
| 543 | return; |
| 544 | } |
| 545 | HEnvironment* environment = new (GetGraph()->GetArena()) HEnvironment( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 546 | GetGraph()->GetArena(), |
| 547 | current_locals_->Size(), |
| 548 | GetGraph()->GetDexFile(), |
| 549 | GetGraph()->GetMethodIdx(), |
| 550 | instruction->GetDexPc()); |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 551 | environment->CopyFrom(*current_locals_); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 552 | instruction->SetRawEnvironment(environment); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 553 | } |
| 554 | |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 555 | void SsaBuilder::VisitTemporary(HTemporary* temp) { |
| 556 | // Temporaries are only used by the baseline register allocator. |
| 557 | temp->GetBlock()->RemoveInstruction(temp); |
| 558 | } |
| 559 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 560 | } // namespace art |