Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +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_phi_elimination.h" |
| 18 | |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 19 | #include "base/arena_containers.h" |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 20 | #include "base/bit_vector-inl.h" |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 21 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 22 | namespace art { |
| 23 | |
| 24 | void SsaDeadPhiElimination::Run() { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 25 | MarkDeadPhis(); |
| 26 | EliminateDeadPhis(); |
| 27 | } |
| 28 | |
| 29 | void SsaDeadPhiElimination::MarkDeadPhis() { |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 30 | // Phis are constructed live and should not be revived if previously marked |
| 31 | // dead. This algorithm temporarily breaks that invariant but we DCHECK that |
| 32 | // only phis which were initially live are revived. |
| 33 | ArenaSet<HPhi*> initially_live(graph_->GetArena()->Adapter()); |
| 34 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 35 | // Add to the worklist phis referenced by non-phi instructions. |
| 36 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 37 | HBasicBlock* block = it.Current(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 38 | for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
| 39 | HPhi* phi = inst_it.Current()->AsPhi(); |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 40 | if (phi->IsDead()) { |
| 41 | continue; |
| 42 | } |
| 43 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 44 | bool keep_alive = (graph_->IsDebuggable() && phi->HasEnvironmentUses()); |
| 45 | if (!keep_alive) { |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 46 | for (const HUseListNode<HInstruction*>& use : phi->GetUses()) { |
| 47 | if (!use.GetUser()->IsPhi()) { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 48 | keep_alive = true; |
| 49 | break; |
| 50 | } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 51 | } |
| 52 | } |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 53 | |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 54 | if (keep_alive) { |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 55 | worklist_.push_back(phi); |
| 56 | } else { |
| 57 | phi->SetDead(); |
| 58 | if (kIsDebugBuild) { |
| 59 | initially_live.insert(phi); |
| 60 | } |
| 61 | } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | // Process the worklist by propagating liveness to phi inputs. |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 66 | while (!worklist_.empty()) { |
| 67 | HPhi* phi = worklist_.back(); |
| 68 | worklist_.pop_back(); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 69 | for (HInputIterator it(phi); !it.Done(); it.Advance()) { |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 70 | HPhi* input = it.Current()->AsPhi(); |
| 71 | if (input != nullptr && input->IsDead()) { |
| 72 | // Input is a dead phi. Revive it and add to the worklist. We make sure |
| 73 | // that the phi was not dead initially (see definition of `initially_live`). |
| 74 | DCHECK(ContainsElement(initially_live, input)); |
| 75 | input->SetLive(); |
| 76 | worklist_.push_back(input); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 80 | } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 81 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 82 | void SsaDeadPhiElimination::EliminateDeadPhis() { |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 83 | // Remove phis that are not live. Visit in post order so that phis |
| 84 | // that are not inputs of loop phis can be removed when they have |
| 85 | // no users left (dead phis might use dead phis). |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 86 | for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 87 | HBasicBlock* block = it.Current(); |
| 88 | HInstruction* current = block->GetFirstPhi(); |
| 89 | HInstruction* next = nullptr; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 90 | HPhi* phi; |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 91 | while (current != nullptr) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 92 | phi = current->AsPhi(); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 93 | next = current->GetNext(); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 94 | if (phi->IsDead()) { |
| 95 | // Make sure the phi is only used by other dead phis. |
| 96 | if (kIsDebugBuild) { |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 97 | for (const HUseListNode<HInstruction*>& use : phi->GetUses()) { |
| 98 | HInstruction* user = use.GetUser(); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 99 | DCHECK(user->IsLoopHeaderPhi()); |
| 100 | DCHECK(user->AsPhi()->IsDead()); |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 101 | } |
| 102 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 103 | // Remove the phi from use lists of its inputs. |
| 104 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 105 | phi->RemoveAsUserOfInput(i); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 106 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 107 | // Remove the phi from environments that use it. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 108 | for (const HUseListNode<HEnvironment*>& use : phi->GetEnvUses()) { |
| 109 | HEnvironment* user = use.GetUser(); |
| 110 | user->SetRawEnvAt(use.GetIndex(), nullptr); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 111 | } |
| 112 | // Delete it from the instruction list. |
| 113 | block->RemovePhi(phi, /*ensure_safety=*/ false); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 114 | } |
| 115 | current = next; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void SsaRedundantPhiElimination::Run() { |
David Brazdil | 77b022d | 2015-08-19 14:17:31 +0100 | [diff] [blame] | 121 | // Add all phis in the worklist. Order does not matter for correctness, and |
| 122 | // neither will necessarily converge faster. |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 123 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 124 | HBasicBlock* block = it.Current(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 125 | for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 126 | worklist_.push_back(inst_it.Current()->AsPhi()); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 130 | ArenaSet<uint32_t> visited_phis_in_cycle(graph_->GetArena()->Adapter()); |
| 131 | ArenaVector<HPhi*> cycle_worklist(graph_->GetArena()->Adapter()); |
| 132 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 133 | while (!worklist_.empty()) { |
| 134 | HPhi* phi = worklist_.back(); |
| 135 | worklist_.pop_back(); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 136 | |
| 137 | // If the phi has already been processed, continue. |
| 138 | if (!phi->IsInBlock()) { |
| 139 | continue; |
| 140 | } |
| 141 | |
Nicolas Geoffray | 05b3fa0 | 2016-05-04 12:05:56 +0100 | [diff] [blame^] | 142 | // If the phi is dead, we know we won't revive it and it will be removed, |
| 143 | // so don't process it. |
| 144 | if (phi->IsDead()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 145 | continue; |
| 146 | } |
| 147 | |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 148 | HInstruction* candidate = nullptr; |
| 149 | visited_phis_in_cycle.clear(); |
| 150 | cycle_worklist.clear(); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 151 | |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 152 | cycle_worklist.push_back(phi); |
| 153 | visited_phis_in_cycle.insert(phi->GetId()); |
| 154 | bool catch_phi_in_cycle = phi->IsCatchPhi(); |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 155 | bool irreducible_loop_phi_in_cycle = phi->IsIrreducibleLoopHeaderPhi(); |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 156 | |
| 157 | // First do a simple loop over inputs and check if they are all the same. |
| 158 | for (size_t j = 0; j < phi->InputCount(); ++j) { |
| 159 | HInstruction* input = phi->InputAt(j); |
| 160 | if (input == phi) { |
| 161 | continue; |
| 162 | } else if (candidate == nullptr) { |
| 163 | candidate = input; |
| 164 | } else if (candidate != input) { |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 165 | candidate = nullptr; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 170 | // If we haven't found a candidate, check for a phi cycle. Note that we need to detect |
| 171 | // such cycles to avoid having reference and non-reference equivalents. We check this |
| 172 | // invariant in the graph checker. |
| 173 | if (candidate == nullptr) { |
| 174 | // We iterate over the array as long as it grows. |
| 175 | for (size_t i = 0; i < cycle_worklist.size(); ++i) { |
| 176 | HPhi* current = cycle_worklist[i]; |
| 177 | DCHECK(!current->IsLoopHeaderPhi() || |
| 178 | current->GetBlock()->IsLoopPreHeaderFirstPredecessor()); |
| 179 | |
| 180 | for (size_t j = 0; j < current->InputCount(); ++j) { |
| 181 | HInstruction* input = current->InputAt(j); |
| 182 | if (input == current) { |
| 183 | continue; |
| 184 | } else if (input->IsPhi()) { |
| 185 | if (!ContainsElement(visited_phis_in_cycle, input->GetId())) { |
| 186 | cycle_worklist.push_back(input->AsPhi()); |
| 187 | visited_phis_in_cycle.insert(input->GetId()); |
| 188 | catch_phi_in_cycle |= input->AsPhi()->IsCatchPhi(); |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 189 | irreducible_loop_phi_in_cycle |= input->IsIrreducibleLoopHeaderPhi(); |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 190 | } else { |
| 191 | // Already visited, nothing to do. |
| 192 | } |
| 193 | } else if (candidate == nullptr) { |
| 194 | candidate = input; |
| 195 | } else if (candidate != input) { |
| 196 | candidate = nullptr; |
| 197 | // Clear the cycle worklist to break out of the outer loop. |
| 198 | cycle_worklist.clear(); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 205 | if (candidate == nullptr) { |
| 206 | continue; |
| 207 | } |
| 208 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 209 | if (irreducible_loop_phi_in_cycle && !candidate->IsConstant()) { |
| 210 | // For irreducible loops, we need to keep the phis to satisfy our linear scan |
| 211 | // algorithm. |
| 212 | // There is one exception for constants, as the type propagation requires redundant |
| 213 | // cyclic phis of a constant to be removed. This is ok for the linear scan as it |
| 214 | // has to deal with constants anyway, and they can trivially be rematerialized. |
| 215 | continue; |
| 216 | } |
| 217 | |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 218 | for (HPhi* current : cycle_worklist) { |
| 219 | // The candidate may not dominate a phi in a catch block: there may be non-throwing |
| 220 | // instructions at the beginning of a try range, that may be the first input of |
| 221 | // catch phis. |
| 222 | // TODO(dbrazdil): Remove this situation by moving those non-throwing instructions |
| 223 | // before the try entry. |
| 224 | if (catch_phi_in_cycle) { |
| 225 | if (!candidate->StrictlyDominates(current)) { |
| 226 | continue; |
| 227 | } |
| 228 | } else { |
| 229 | DCHECK(candidate->StrictlyDominates(current)); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 230 | } |
David Brazdil | 77b022d | 2015-08-19 14:17:31 +0100 | [diff] [blame] | 231 | |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 232 | // Because we're updating the users of this phi, we may have new candidates |
| 233 | // for elimination. Add phis that use this phi to the worklist. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 234 | for (const HUseListNode<HInstruction*>& use : current->GetUses()) { |
| 235 | HInstruction* user = use.GetUser(); |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 236 | if (user->IsPhi() && !ContainsElement(visited_phis_in_cycle, user->GetId())) { |
| 237 | worklist_.push_back(user->AsPhi()); |
| 238 | } |
| 239 | } |
| 240 | DCHECK(candidate->StrictlyDominates(current)); |
| 241 | current->ReplaceWith(candidate); |
| 242 | current->GetBlock()->RemovePhi(current); |
| 243 | } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | } // namespace art |