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