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) |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 38 | : graph_(graph), worklist_(graph->GetArena()->Adapter(kArenaAllocSsaBuilder)) { |
| 39 | worklist_.reserve(kDefaultWorklistSize); |
| 40 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 41 | |
| 42 | void Run(); |
| 43 | |
| 44 | private: |
| 45 | void VisitBasicBlock(HBasicBlock* block); |
| 46 | void ProcessWorklist(); |
| 47 | void AddToWorklist(HPhi* phi); |
| 48 | void AddDependentInstructionsToWorklist(HPhi* phi); |
| 49 | bool UpdateType(HPhi* phi); |
| 50 | |
| 51 | HGraph* const graph_; |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 52 | ArenaVector<HPhi*> worklist_; |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 53 | |
| 54 | static constexpr size_t kDefaultWorklistSize = 8; |
| 55 | |
| 56 | DISALLOW_COPY_AND_ASSIGN(DeadPhiHandling); |
| 57 | }; |
| 58 | |
Nicolas Geoffray | b11fc61 | 2015-09-29 13:42:22 +0100 | [diff] [blame] | 59 | static bool HasConflictingEquivalent(HPhi* phi) { |
| 60 | if (phi->GetNext() == nullptr) { |
| 61 | return false; |
| 62 | } |
| 63 | HPhi* next = phi->GetNext()->AsPhi(); |
| 64 | if (next->GetRegNumber() == phi->GetRegNumber()) { |
| 65 | if (next->GetType() == Primitive::kPrimVoid) { |
| 66 | // We only get a void type for an equivalent phi we processed and found out |
| 67 | // it was conflicting. |
| 68 | return true; |
| 69 | } else { |
| 70 | // Go to the next phi, in case it is also an equivalent. |
| 71 | return HasConflictingEquivalent(next); |
| 72 | } |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 77 | bool DeadPhiHandling::UpdateType(HPhi* phi) { |
David Brazdil | b701315 | 2015-09-17 16:47:21 +0100 | [diff] [blame] | 78 | if (phi->IsDead()) { |
| 79 | // Phi was rendered dead while waiting in the worklist because it was replaced |
| 80 | // with an equivalent. |
| 81 | return false; |
| 82 | } |
| 83 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 84 | Primitive::Type existing = phi->GetType(); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 85 | |
| 86 | bool conflict = false; |
| 87 | Primitive::Type new_type = existing; |
| 88 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 89 | HInstruction* input = phi->InputAt(i); |
| 90 | if (input->IsPhi() && input->AsPhi()->IsDead()) { |
| 91 | // We are doing a reverse post order visit of the graph, reviving |
| 92 | // phis that have environment uses and updating their types. If an |
| 93 | // input is a phi, and it is dead (because its input types are |
| 94 | // conflicting), this phi must be marked dead as well. |
| 95 | conflict = true; |
| 96 | break; |
| 97 | } |
| 98 | Primitive::Type input_type = HPhi::ToPhiType(input->GetType()); |
| 99 | |
| 100 | // The only acceptable transitions are: |
| 101 | // - From void to typed: first time we update the type of this phi. |
| 102 | // - From int to reference (or reference to int): the phi has to change |
| 103 | // to reference type. If the integer input cannot be converted to a |
| 104 | // reference input, the phi will remain dead. |
| 105 | if (new_type == Primitive::kPrimVoid) { |
| 106 | new_type = input_type; |
| 107 | } else if (new_type == Primitive::kPrimNot && input_type == Primitive::kPrimInt) { |
Nicolas Geoffray | b11fc61 | 2015-09-29 13:42:22 +0100 | [diff] [blame] | 108 | if (input->IsPhi() && HasConflictingEquivalent(input->AsPhi())) { |
| 109 | // If we already asked for an equivalent of the input phi, but that equivalent |
| 110 | // ended up conflicting, make this phi conflicting too. |
| 111 | conflict = true; |
| 112 | break; |
| 113 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 114 | HInstruction* equivalent = SsaBuilder::GetReferenceTypeEquivalent(input); |
| 115 | if (equivalent == nullptr) { |
| 116 | conflict = true; |
| 117 | break; |
Nicolas Geoffray | b11fc61 | 2015-09-29 13:42:22 +0100 | [diff] [blame] | 118 | } |
| 119 | phi->ReplaceInput(equivalent, i); |
| 120 | if (equivalent->IsPhi()) { |
| 121 | DCHECK_EQ(equivalent->GetType(), Primitive::kPrimNot); |
| 122 | // We created a new phi, but that phi has the same inputs as the old phi. We |
| 123 | // add it to the worklist to ensure its inputs can also be converted to reference. |
| 124 | // If not, it will remain dead, and the algorithm will make the current phi dead |
| 125 | // as well. |
| 126 | equivalent->AsPhi()->SetLive(); |
| 127 | AddToWorklist(equivalent->AsPhi()); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 128 | } |
| 129 | } else if (new_type == Primitive::kPrimInt && input_type == Primitive::kPrimNot) { |
| 130 | new_type = Primitive::kPrimNot; |
| 131 | // Start over, we may request reference equivalents for the inputs of the phi. |
| 132 | i = -1; |
| 133 | } else if (new_type != input_type) { |
| 134 | conflict = true; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (conflict) { |
| 140 | phi->SetType(Primitive::kPrimVoid); |
| 141 | phi->SetDead(); |
| 142 | return true; |
David Brazdil | b701315 | 2015-09-17 16:47:21 +0100 | [diff] [blame] | 143 | } else if (existing == new_type) { |
| 144 | return false; |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 145 | } |
David Brazdil | b701315 | 2015-09-17 16:47:21 +0100 | [diff] [blame] | 146 | |
| 147 | DCHECK(phi->IsLive()); |
| 148 | phi->SetType(new_type); |
| 149 | |
| 150 | // There might exist a `new_type` equivalent of `phi` already. In that case, |
| 151 | // we replace the equivalent with the, now live, `phi`. |
| 152 | HPhi* equivalent = phi->GetNextEquivalentPhiWithSameType(); |
| 153 | if (equivalent != nullptr) { |
| 154 | // There cannot be more than two equivalents with the same type. |
| 155 | DCHECK(equivalent->GetNextEquivalentPhiWithSameType() == nullptr); |
| 156 | // If doing fix-point iteration, the equivalent might be in `worklist_`. |
| 157 | // Setting it dead will make UpdateType skip it. |
| 158 | equivalent->SetDead(); |
| 159 | equivalent->ReplaceWith(phi); |
| 160 | } |
| 161 | |
| 162 | return true; |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void DeadPhiHandling::VisitBasicBlock(HBasicBlock* block) { |
| 166 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 167 | HPhi* phi = it.Current()->AsPhi(); |
| 168 | if (phi->IsDead() && phi->HasEnvironmentUses()) { |
| 169 | phi->SetLive(); |
| 170 | if (block->IsLoopHeader()) { |
David Brazdil | 1d0a03c | 2015-09-28 14:11:09 +0100 | [diff] [blame] | 171 | // Give a type to the loop phi to guarantee convergence of the algorithm. |
| 172 | // Note that the dead phi may already have a type if it is an equivalent |
| 173 | // generated for a typed LoadLocal. In that case we do not change the |
| 174 | // type because it could lead to an unsupported PrimNot/Float/Double -> |
| 175 | // PrimInt/Long transition and create same type equivalents. |
| 176 | if (phi->GetType() == Primitive::kPrimVoid) { |
| 177 | phi->SetType(phi->InputAt(0)->GetType()); |
| 178 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 179 | AddToWorklist(phi); |
| 180 | } else { |
| 181 | // Because we are doing a reverse post order visit, all inputs of |
| 182 | // this phi have been visited and therefore had their (initial) type set. |
| 183 | UpdateType(phi); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void DeadPhiHandling::ProcessWorklist() { |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 190 | while (!worklist_.empty()) { |
| 191 | HPhi* instruction = worklist_.back(); |
| 192 | worklist_.pop_back(); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 193 | // Note that the same equivalent phi can be added multiple times in the work list, if |
| 194 | // used by multiple phis. The first call to `UpdateType` will know whether the phi is |
| 195 | // dead or live. |
| 196 | if (instruction->IsLive() && UpdateType(instruction)) { |
| 197 | AddDependentInstructionsToWorklist(instruction); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void DeadPhiHandling::AddToWorklist(HPhi* instruction) { |
| 203 | DCHECK(instruction->IsLive()); |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 204 | worklist_.push_back(instruction); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void DeadPhiHandling::AddDependentInstructionsToWorklist(HPhi* instruction) { |
| 208 | for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) { |
| 209 | HPhi* phi = it.Current()->GetUser()->AsPhi(); |
| 210 | if (phi != nullptr && !phi->IsDead()) { |
| 211 | AddToWorklist(phi); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void DeadPhiHandling::Run() { |
| 217 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 218 | VisitBasicBlock(it.Current()); |
| 219 | } |
| 220 | ProcessWorklist(); |
| 221 | } |
| 222 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 223 | void SsaBuilder::FixNullConstantType() { |
| 224 | // The order doesn't matter here. |
| 225 | for (HReversePostOrderIterator itb(*GetGraph()); !itb.Done(); itb.Advance()) { |
| 226 | for (HInstructionIterator it(itb.Current()->GetInstructions()); !it.Done(); it.Advance()) { |
| 227 | HInstruction* equality_instr = it.Current(); |
| 228 | if (!equality_instr->IsEqual() && !equality_instr->IsNotEqual()) { |
| 229 | continue; |
| 230 | } |
| 231 | HInstruction* left = equality_instr->InputAt(0); |
| 232 | HInstruction* right = equality_instr->InputAt(1); |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 233 | HInstruction* int_operand = nullptr; |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 234 | |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 235 | if ((left->GetType() == Primitive::kPrimNot) && (right->GetType() == Primitive::kPrimInt)) { |
| 236 | int_operand = right; |
| 237 | } else if ((right->GetType() == Primitive::kPrimNot) |
| 238 | && (left->GetType() == Primitive::kPrimInt)) { |
| 239 | int_operand = left; |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 240 | } else { |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | // If we got here, we are comparing against a reference and the int constant |
| 245 | // should be replaced with a null constant. |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 246 | // Both type propagation and redundant phi elimination ensure `int_operand` |
| 247 | // can only be the 0 constant. |
| 248 | DCHECK(int_operand->IsIntConstant()); |
| 249 | DCHECK_EQ(0, int_operand->AsIntConstant()->GetValue()); |
| 250 | equality_instr->ReplaceInput(GetGraph()->GetNullConstant(), int_operand == right ? 1 : 0); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void SsaBuilder::EquivalentPhisCleanup() { |
| 256 | // The order doesn't matter here. |
| 257 | for (HReversePostOrderIterator itb(*GetGraph()); !itb.Done(); itb.Advance()) { |
| 258 | for (HInstructionIterator it(itb.Current()->GetPhis()); !it.Done(); it.Advance()) { |
| 259 | HPhi* phi = it.Current()->AsPhi(); |
| 260 | HPhi* next = phi->GetNextEquivalentPhiWithSameType(); |
| 261 | if (next != nullptr) { |
Nicolas Geoffray | 4230e18 | 2015-06-29 14:34:46 +0100 | [diff] [blame] | 262 | // Make sure we do not replace a live phi with a dead phi. A live phi has been |
| 263 | // handled by the type propagation phase, unlike a dead phi. |
| 264 | if (next->IsLive()) { |
| 265 | phi->ReplaceWith(next); |
| 266 | } else { |
| 267 | next->ReplaceWith(phi); |
| 268 | } |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 269 | DCHECK(next->GetNextEquivalentPhiWithSameType() == nullptr) |
| 270 | << "More then one phi equivalent with type " << phi->GetType() |
| 271 | << " found for phi" << phi->GetId(); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 277 | void SsaBuilder::BuildSsa() { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 278 | // 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] | 279 | // (with the exception of loops) in order to create the right environment for that |
| 280 | // 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] | 281 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 282 | VisitBasicBlock(it.Current()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // 2) Set inputs of loop phis. |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 286 | for (HBasicBlock* block : loop_headers_) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 287 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 288 | HPhi* phi = it.Current()->AsPhi(); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 289 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
| 290 | HInstruction* input = ValueOfLocal(predecessor, phi->GetRegNumber()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 291 | phi->AddInput(input); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 296 | // 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] | 297 | // at the DEX level, the type of these phis does not need to be consistent, but |
| 298 | // 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] | 299 | // type. The marking allows the type propagation to know which phis it needs |
| 300 | // 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] | 301 | // step 9). |
| 302 | SsaDeadPhiElimination dead_phis_for_type_propagation(GetGraph()); |
| 303 | dead_phis_for_type_propagation.MarkDeadPhis(); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 304 | |
| 305 | // 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] | 306 | // 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] | 307 | // 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] | 308 | PrimitiveTypePropagation type_propagation(GetGraph()); |
Nicolas Geoffray | 184d640 | 2014-06-09 14:06:02 +0100 | [diff] [blame] | 309 | type_propagation.Run(); |
| 310 | |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 311 | // 5) When creating equivalent phis we copy the inputs of the original phi which |
| 312 | // may be improperly typed. This was fixed during the type propagation in 4) but |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 313 | // as a result we may end up with two equivalent phis with the same type for |
| 314 | // the same dex register. This pass cleans them up. |
| 315 | EquivalentPhisCleanup(); |
| 316 | |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 317 | // 6) Mark dead phis again. Step 4) may have introduced new phis. |
| 318 | // Step 5) might enable the death of new phis. |
Nicolas Geoffray | b59dba0 | 2015-03-11 18:13:21 +0000 | [diff] [blame] | 319 | SsaDeadPhiElimination dead_phis(GetGraph()); |
| 320 | dead_phis.MarkDeadPhis(); |
| 321 | |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 322 | // 7) 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] | 323 | // Note that we cannot do this phase before type propagation, otherwise |
| 324 | // we could get rid of phi equivalents, whose presence is a requirement for the |
| 325 | // type propagation phase. Note that this is to satisfy statement (a) of the |
| 326 | // SsaBuilder (see ssa_builder.h). |
| 327 | SsaRedundantPhiElimination redundant_phi(GetGraph()); |
| 328 | redundant_phi.Run(); |
| 329 | |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 330 | // 8) Fix the type for null constants which are part of an equality comparison. |
| 331 | // We need to do this after redundant phi elimination, to ensure the only cases |
| 332 | // that we can see are reference comparison against 0. The redundant phi |
| 333 | // elimination ensures we do not see a phi taking two 0 constants in a HEqual |
| 334 | // or HNotEqual. |
| 335 | FixNullConstantType(); |
| 336 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 337 | // 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] | 338 | // can have a phi equivalent that is not dead. We must therefore update |
| 339 | // all environment uses of the dead phi to use its equivalent. Note that there |
| 340 | // can be multiple phis for the same Dex register that are live (for example |
| 341 | // when merging constants), in which case it is OK for the environments |
| 342 | // to just reference one. |
| 343 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 344 | HBasicBlock* block = it.Current(); |
| 345 | for (HInstructionIterator it_phis(block->GetPhis()); !it_phis.Done(); it_phis.Advance()) { |
| 346 | HPhi* phi = it_phis.Current()->AsPhi(); |
| 347 | // If the phi is not dead, or has no environment uses, there is nothing to do. |
| 348 | if (!phi->IsDead() || !phi->HasEnvironmentUses()) continue; |
| 349 | HInstruction* next = phi->GetNext(); |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 350 | if (!phi->IsVRegEquivalentOf(next)) continue; |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 351 | if (next->AsPhi()->IsDead()) { |
| 352 | // If the phi equivalent is dead, check if there is another one. |
| 353 | next = next->GetNext(); |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 354 | if (!phi->IsVRegEquivalentOf(next)) continue; |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 355 | // There can be at most two phi equivalents. |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 356 | DCHECK(!phi->IsVRegEquivalentOf(next->GetNext())); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 357 | if (next->AsPhi()->IsDead()) continue; |
| 358 | } |
| 359 | // We found a live phi equivalent. Update the environment uses of `phi` with it. |
| 360 | phi->ReplaceWith(next); |
| 361 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 364 | // 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] | 365 | // application. This is for satisfying statement (c) of the SsaBuilder |
| 366 | // (see ssa_builder.h). |
| 367 | if (GetGraph()->IsDebuggable()) { |
| 368 | DeadPhiHandling dead_phi_handler(GetGraph()); |
| 369 | dead_phi_handler.Run(); |
| 370 | } |
| 371 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 372 | // 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] | 373 | // have potentially revive dead phis in case of a debuggable application, |
| 374 | // we can eliminate phis we do not need. Regardless of the debuggable status, |
| 375 | // this phase is necessary for statement (b) of the SsaBuilder (see ssa_builder.h), |
| 376 | // as well as for the code generation, which does not deal with phis of conflicting |
| 377 | // input types. |
| 378 | dead_phis.EliminateDeadPhis(); |
| 379 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 380 | // 12) Clear locals. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 381 | for (HInstructionIterator it(GetGraph()->GetEntryBlock()->GetInstructions()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 382 | !it.Done(); |
| 383 | it.Advance()) { |
| 384 | HInstruction* current = it.Current(); |
Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 385 | if (current->IsLocal()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 386 | current->GetBlock()->RemoveInstruction(current); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
David Brazdil | eead071 | 2015-09-18 14:58:57 +0100 | [diff] [blame] | 391 | ArenaVector<HInstruction*>* SsaBuilder::GetLocalsFor(HBasicBlock* block) { |
David Brazdil | eead071 | 2015-09-18 14:58:57 +0100 | [diff] [blame] | 392 | ArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()]; |
| 393 | const size_t vregs = GetGraph()->GetNumberOfVRegs(); |
| 394 | if (locals->empty() && vregs != 0u) { |
| 395 | locals->resize(vregs, nullptr); |
| 396 | |
| 397 | if (block->IsCatchBlock()) { |
| 398 | ArenaAllocator* arena = GetGraph()->GetArena(); |
| 399 | // We record incoming inputs of catch phis at throwing instructions and |
| 400 | // must therefore eagerly create the phis. Phis for undefined vregs will |
| 401 | // be deleted when the first throwing instruction with the vreg undefined |
| 402 | // is encountered. Unused phis will be removed by dead phi analysis. |
| 403 | for (size_t i = 0; i < vregs; ++i) { |
| 404 | // No point in creating the catch phi if it is already undefined at |
| 405 | // the first throwing instruction. |
| 406 | if ((*current_locals_)[i] != nullptr) { |
| 407 | HPhi* phi = new (arena) HPhi(arena, i, 0, Primitive::kPrimVoid); |
| 408 | block->AddPhi(phi); |
| 409 | (*locals)[i] = phi; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | return locals; |
| 415 | } |
| 416 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 417 | HInstruction* SsaBuilder::ValueOfLocal(HBasicBlock* block, size_t local) { |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 418 | ArenaVector<HInstruction*>* locals = GetLocalsFor(block); |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 419 | return (*locals)[local]; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void SsaBuilder::VisitBasicBlock(HBasicBlock* block) { |
| 423 | current_locals_ = GetLocalsFor(block); |
| 424 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 425 | if (block->IsCatchBlock()) { |
| 426 | // Catch phis were already created and inputs collected from throwing sites. |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 427 | if (kIsDebugBuild) { |
| 428 | // Make sure there was at least one throwing instruction which initialized |
| 429 | // locals (guaranteed by HGraphBuilder) and that all try blocks have been |
| 430 | // visited already (from HTryBoundary scoping and reverse post order). |
| 431 | bool throwing_instruction_found = false; |
| 432 | bool catch_block_visited = false; |
| 433 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 434 | HBasicBlock* current = it.Current(); |
| 435 | if (current == block) { |
| 436 | catch_block_visited = true; |
| 437 | } else if (current->IsTryBlock() && |
| 438 | current->GetTryCatchInformation()->GetTryEntry().HasExceptionHandler(*block)) { |
| 439 | DCHECK(!catch_block_visited) << "Catch block visited before its try block."; |
| 440 | throwing_instruction_found |= current->HasThrowingInstructions(); |
| 441 | } |
| 442 | } |
| 443 | DCHECK(throwing_instruction_found) << "No instructions throwing into a live catch block."; |
| 444 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 445 | } else if (block->IsLoopHeader()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 446 | // 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] | 447 | // 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] | 448 | // locals from the pre header. Their inputs will be populated at the end of |
| 449 | // the analysis. |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 450 | for (size_t local = 0; local < current_locals_->size(); ++local) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 451 | HInstruction* incoming = ValueOfLocal(block->GetLoopInformation()->GetPreHeader(), local); |
| 452 | if (incoming != nullptr) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 453 | HPhi* phi = new (GetGraph()->GetArena()) HPhi( |
| 454 | GetGraph()->GetArena(), local, 0, Primitive::kPrimVoid); |
| 455 | block->AddPhi(phi); |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 456 | (*current_locals_)[local] = phi; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | // Save the loop header so that the last phase of the analysis knows which |
| 460 | // blocks need to be updated. |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 461 | loop_headers_.push_back(block); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 462 | } else if (block->GetPredecessors().size() > 0) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 463 | // 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] | 464 | // We merge the values of all locals, creating phis if those values differ. |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 465 | for (size_t local = 0; local < current_locals_->size(); ++local) { |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 466 | bool one_predecessor_has_no_value = false; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 467 | bool is_different = false; |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame^] | 468 | HInstruction* value = ValueOfLocal(block->GetPredecessors()[0], local); |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 469 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 470 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
| 471 | HInstruction* current = ValueOfLocal(predecessor, local); |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 472 | if (current == nullptr) { |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 473 | one_predecessor_has_no_value = true; |
| 474 | break; |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 475 | } else if (current != value) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 476 | is_different = true; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 477 | } |
| 478 | } |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 479 | |
| 480 | if (one_predecessor_has_no_value) { |
| 481 | // If one predecessor has no value for this local, we trust the verifier has |
| 482 | // successfully checked that there is a store dominating any read after this block. |
| 483 | continue; |
| 484 | } |
| 485 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 486 | if (is_different) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 487 | HPhi* phi = new (GetGraph()->GetArena()) HPhi( |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 488 | GetGraph()->GetArena(), local, block->GetPredecessors().size(), Primitive::kPrimVoid); |
| 489 | for (size_t i = 0; i < block->GetPredecessors().size(); i++) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame^] | 490 | HInstruction* pred_value = ValueOfLocal(block->GetPredecessors()[i], local); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 491 | phi->SetRawInputAt(i, pred_value); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 492 | } |
| 493 | block->AddPhi(phi); |
| 494 | value = phi; |
| 495 | } |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 496 | (*current_locals_)[local] = value; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | // Visit all instructions. The instructions of interest are: |
| 501 | // - HLoadLocal: replace them with the current value of the local. |
| 502 | // - HStoreLocal: update current value of the local and remove the instruction. |
| 503 | // - Instructions that require an environment: populate their environment |
| 504 | // with the current values of the locals. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 505 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 506 | it.Current()->Accept(this); |
| 507 | } |
| 508 | } |
| 509 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 510 | /** |
| 511 | * Constants in the Dex format are not typed. So the builder types them as |
| 512 | * integers, but when doing the SSA form, we might realize the constant |
| 513 | * is used for floating point operations. We create a floating-point equivalent |
| 514 | * constant to make the operations correctly typed. |
| 515 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 516 | HFloatConstant* SsaBuilder::GetFloatEquivalent(HIntConstant* constant) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 517 | // We place the floating point constant next to this constant. |
| 518 | HFloatConstant* result = constant->GetNext()->AsFloatConstant(); |
| 519 | if (result == nullptr) { |
| 520 | HGraph* graph = constant->GetBlock()->GetGraph(); |
| 521 | ArenaAllocator* allocator = graph->GetArena(); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 522 | result = new (allocator) HFloatConstant(bit_cast<float, int32_t>(constant->GetValue())); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 523 | constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext()); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 524 | graph->CacheFloatConstant(result); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 525 | } else { |
| 526 | // If there is already a constant with the expected type, we know it is |
| 527 | // the floating point equivalent of this constant. |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 528 | DCHECK_EQ((bit_cast<int32_t, float>(result->GetValue())), constant->GetValue()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 529 | } |
| 530 | return result; |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Wide constants in the Dex format are not typed. So the builder types them as |
| 535 | * longs, but when doing the SSA form, we might realize the constant |
| 536 | * is used for floating point operations. We create a floating-point equivalent |
| 537 | * constant to make the operations correctly typed. |
| 538 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 539 | HDoubleConstant* SsaBuilder::GetDoubleEquivalent(HLongConstant* constant) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 540 | // We place the floating point constant next to this constant. |
| 541 | HDoubleConstant* result = constant->GetNext()->AsDoubleConstant(); |
| 542 | if (result == nullptr) { |
| 543 | HGraph* graph = constant->GetBlock()->GetGraph(); |
| 544 | ArenaAllocator* allocator = graph->GetArena(); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 545 | result = new (allocator) HDoubleConstant(bit_cast<double, int64_t>(constant->GetValue())); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 546 | constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext()); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 547 | graph->CacheDoubleConstant(result); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 548 | } else { |
| 549 | // If there is already a constant with the expected type, we know it is |
| 550 | // the floating point equivalent of this constant. |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 551 | DCHECK_EQ((bit_cast<int64_t, double>(result->GetValue())), constant->GetValue()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 552 | } |
| 553 | return result; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * 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] | 558 | * used for non floating point operations and floating point / reference operations. |
| 559 | * 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] | 560 | * 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] | 561 | * phi with a floating point / reference type. |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 562 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 563 | HPhi* SsaBuilder::GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 564 | // We place the floating point /reference phi next to this phi. |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 565 | HInstruction* next = phi->GetNext(); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 566 | if (next != nullptr |
| 567 | && next->AsPhi()->GetRegNumber() == phi->GetRegNumber() |
| 568 | && next->GetType() != type) { |
| 569 | // Move to the next phi to see if it is the one we are looking for. |
| 570 | next = next->GetNext(); |
| 571 | } |
| 572 | |
| 573 | if (next == nullptr |
| 574 | || (next->AsPhi()->GetRegNumber() != phi->GetRegNumber()) |
| 575 | || (next->GetType() != type)) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 576 | ArenaAllocator* allocator = phi->GetBlock()->GetGraph()->GetArena(); |
| 577 | HPhi* new_phi = new (allocator) HPhi(allocator, phi->GetRegNumber(), phi->InputCount(), type); |
| 578 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 579 | // Copy the inputs. Note that the graph may not be correctly typed by doing this copy, |
| 580 | // but the type propagation phase will fix it. |
| 581 | new_phi->SetRawInputAt(i, phi->InputAt(i)); |
| 582 | } |
| 583 | phi->GetBlock()->InsertPhiAfter(new_phi, phi); |
| 584 | return new_phi; |
| 585 | } else { |
Nicolas Geoffray | 21cc798 | 2014-11-17 17:50:33 +0000 | [diff] [blame] | 586 | DCHECK_EQ(next->GetType(), type); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 587 | return next->AsPhi(); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | HInstruction* SsaBuilder::GetFloatOrDoubleEquivalent(HInstruction* user, |
| 592 | HInstruction* value, |
| 593 | Primitive::Type type) { |
| 594 | if (value->IsArrayGet()) { |
| 595 | // The verifier has checked that values in arrays cannot be used for both |
| 596 | // floating point and non-floating point operations. It is therefore safe to just |
| 597 | // change the type of the operation. |
| 598 | value->AsArrayGet()->SetType(type); |
| 599 | return value; |
| 600 | } else if (value->IsLongConstant()) { |
| 601 | return GetDoubleEquivalent(value->AsLongConstant()); |
| 602 | } else if (value->IsIntConstant()) { |
| 603 | return GetFloatEquivalent(value->AsIntConstant()); |
| 604 | } else if (value->IsPhi()) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 605 | return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), type); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 606 | } else { |
| 607 | // For other instructions, we assume the verifier has checked that the dex format is correctly |
| 608 | // typed and the value in a dex register will not be used for both floating point and |
| 609 | // non-floating point operations. So the only reason an instruction would want a floating |
| 610 | // 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] | 611 | DCHECK(user->IsPhi()) << "is actually " << user->DebugName() << " (" << user->GetId() << ")"; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 612 | return value; |
| 613 | } |
| 614 | } |
| 615 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 616 | HInstruction* SsaBuilder::GetReferenceTypeEquivalent(HInstruction* value) { |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 617 | if (value->IsIntConstant() && value->AsIntConstant()->GetValue() == 0) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 618 | return value->GetBlock()->GetGraph()->GetNullConstant(); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 619 | } else if (value->IsPhi()) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 620 | return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), Primitive::kPrimNot); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 621 | } else { |
| 622 | return nullptr; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 626 | void SsaBuilder::VisitLoadLocal(HLoadLocal* load) { |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 627 | HInstruction* value = (*current_locals_)[load->GetLocal()->GetRegNumber()]; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 628 | // If the operation requests a specific type, we make sure its input is of that type. |
| 629 | if (load->GetType() != value->GetType()) { |
| 630 | if (load->GetType() == Primitive::kPrimFloat || load->GetType() == Primitive::kPrimDouble) { |
| 631 | value = GetFloatOrDoubleEquivalent(load, value, load->GetType()); |
| 632 | } else if (load->GetType() == Primitive::kPrimNot) { |
| 633 | value = GetReferenceTypeEquivalent(value); |
| 634 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 635 | } |
| 636 | load->ReplaceWith(value); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 637 | load->GetBlock()->RemoveInstruction(load); |
| 638 | } |
| 639 | |
| 640 | void SsaBuilder::VisitStoreLocal(HStoreLocal* store) { |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 641 | (*current_locals_)[store->GetLocal()->GetRegNumber()] = store->InputAt(1); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 642 | store->GetBlock()->RemoveInstruction(store); |
| 643 | } |
| 644 | |
| 645 | void SsaBuilder::VisitInstruction(HInstruction* instruction) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 646 | if (instruction->NeedsEnvironment()) { |
| 647 | HEnvironment* environment = new (GetGraph()->GetArena()) HEnvironment( |
| 648 | GetGraph()->GetArena(), |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 649 | current_locals_->size(), |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 650 | GetGraph()->GetDexFile(), |
| 651 | GetGraph()->GetMethodIdx(), |
| 652 | instruction->GetDexPc(), |
| 653 | GetGraph()->GetInvokeType(), |
| 654 | instruction); |
| 655 | environment->CopyFrom(*current_locals_); |
| 656 | instruction->SetRawEnvironment(environment); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 657 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 658 | |
| 659 | // If in a try block, propagate values of locals into catch blocks. |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 660 | if (instruction->CanThrowIntoCatchBlock()) { |
| 661 | const HTryBoundary& try_entry = |
| 662 | instruction->GetBlock()->GetTryCatchInformation()->GetTryEntry(); |
| 663 | for (HExceptionHandlerIterator it(try_entry); !it.Done(); it.Advance()) { |
David Brazdil | 3eaa32f | 2015-09-18 10:58:32 +0100 | [diff] [blame] | 664 | HBasicBlock* catch_block = it.Current(); |
| 665 | ArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block); |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 666 | DCHECK_EQ(handler_locals->size(), current_locals_->size()); |
David Brazdil | 3eaa32f | 2015-09-18 10:58:32 +0100 | [diff] [blame] | 667 | for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) { |
| 668 | HInstruction* handler_value = (*handler_locals)[vreg]; |
| 669 | if (handler_value == nullptr) { |
| 670 | // Vreg was undefined at a previously encountered throwing instruction |
| 671 | // and the catch phi was deleted. Do not record the local value. |
| 672 | continue; |
| 673 | } |
| 674 | DCHECK(handler_value->IsPhi()); |
| 675 | |
| 676 | HInstruction* local_value = (*current_locals_)[vreg]; |
| 677 | if (local_value == nullptr) { |
| 678 | // This is the first instruction throwing into `catch_block` where |
| 679 | // `vreg` is undefined. Delete the catch phi. |
| 680 | catch_block->RemovePhi(handler_value->AsPhi()); |
| 681 | (*handler_locals)[vreg] = nullptr; |
| 682 | } else { |
| 683 | // Vreg has been defined at all instructions throwing into `catch_block` |
| 684 | // encountered so far. Record the local value in the catch phi. |
| 685 | handler_value->AsPhi()->AddInput(local_value); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 690 | } |
| 691 | |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 692 | void SsaBuilder::VisitTemporary(HTemporary* temp) { |
| 693 | // Temporaries are only used by the baseline register allocator. |
| 694 | temp->GetBlock()->RemoveInstruction(temp); |
| 695 | } |
| 696 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 697 | } // namespace art |