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 | |
| 19 | namespace art { |
| 20 | |
| 21 | void SsaDeadPhiElimination::Run() { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 22 | MarkDeadPhis(); |
| 23 | EliminateDeadPhis(); |
| 24 | } |
| 25 | |
| 26 | void SsaDeadPhiElimination::MarkDeadPhis() { |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 27 | // Add to the worklist phis referenced by non-phi instructions. |
| 28 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 29 | HBasicBlock* block = it.Current(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 30 | for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
| 31 | HPhi* phi = inst_it.Current()->AsPhi(); |
David Brazdil | 1749e2c | 2015-09-28 13:49:59 +0100 | [diff] [blame^] | 32 | if (phi->IsDead()) { |
| 33 | // Phis are constructed live so this one was proven conflicting. |
| 34 | continue; |
| 35 | } |
| 36 | |
| 37 | bool is_live = false; |
| 38 | if (graph_->IsDebuggable() && phi->HasEnvironmentUses()) { |
| 39 | is_live = true; |
| 40 | } else { |
| 41 | for (HUseIterator<HInstruction*> use_it(phi->GetUses()); !use_it.Done(); use_it.Advance()) { |
| 42 | if (!use_it.Current()->GetUser()->IsPhi()) { |
| 43 | is_live = true; |
| 44 | break; |
| 45 | } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 46 | } |
| 47 | } |
David Brazdil | 1749e2c | 2015-09-28 13:49:59 +0100 | [diff] [blame^] | 48 | |
| 49 | if (is_live) { |
| 50 | worklist_.push_back(phi); |
| 51 | } else { |
| 52 | phi->SetDead(); |
| 53 | } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | // Process the worklist by propagating liveness to phi inputs. |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 58 | while (!worklist_.empty()) { |
| 59 | HPhi* phi = worklist_.back(); |
| 60 | worklist_.pop_back(); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 61 | for (HInputIterator it(phi); !it.Done(); it.Advance()) { |
| 62 | HInstruction* input = it.Current(); |
| 63 | if (input->IsPhi() && input->AsPhi()->IsDead()) { |
David Brazdil | 1749e2c | 2015-09-28 13:49:59 +0100 | [diff] [blame^] | 64 | // If we revive a phi it must have been live at the beginning of |
| 65 | // the pass but had no non-phi uses of its own. |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 66 | input->AsPhi()->SetLive(); |
David Brazdil | 1749e2c | 2015-09-28 13:49:59 +0100 | [diff] [blame^] | 67 | worklist_.push_back(input->AsPhi()); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 71 | } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 72 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 73 | void SsaDeadPhiElimination::EliminateDeadPhis() { |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 74 | // Remove phis that are not live. Visit in post order so that phis |
| 75 | // that are not inputs of loop phis can be removed when they have |
| 76 | // no users left (dead phis might use dead phis). |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 77 | for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 78 | HBasicBlock* block = it.Current(); |
| 79 | HInstruction* current = block->GetFirstPhi(); |
| 80 | HInstruction* next = nullptr; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 81 | HPhi* phi; |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 82 | while (current != nullptr) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 83 | phi = current->AsPhi(); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 84 | next = current->GetNext(); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 85 | if (phi->IsDead()) { |
| 86 | // Make sure the phi is only used by other dead phis. |
| 87 | if (kIsDebugBuild) { |
| 88 | for (HUseIterator<HInstruction*> use_it(phi->GetUses()); !use_it.Done(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 89 | use_it.Advance()) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 90 | HInstruction* user = use_it.Current()->GetUser(); |
David Brazdil | 1749e2c | 2015-09-28 13:49:59 +0100 | [diff] [blame^] | 91 | DCHECK(user->IsPhi()); |
| 92 | DCHECK(user->AsPhi()->IsDead()); |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 93 | } |
| 94 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 95 | // Remove the phi from use lists of its inputs. |
| 96 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 97 | phi->RemoveAsUserOfInput(i); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 98 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 99 | // Remove the phi from environments that use it. |
| 100 | for (HUseIterator<HEnvironment*> use_it(phi->GetEnvUses()); !use_it.Done(); |
| 101 | use_it.Advance()) { |
| 102 | HUseListNode<HEnvironment*>* user_node = use_it.Current(); |
| 103 | HEnvironment* user = user_node->GetUser(); |
| 104 | user->SetRawEnvAt(user_node->GetIndex(), nullptr); |
| 105 | } |
| 106 | // Delete it from the instruction list. |
| 107 | block->RemovePhi(phi, /*ensure_safety=*/ false); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 108 | } |
| 109 | current = next; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void SsaRedundantPhiElimination::Run() { |
David Brazdil | 77b022d | 2015-08-19 14:17:31 +0100 | [diff] [blame] | 115 | // Add all phis in the worklist. Order does not matter for correctness, and |
| 116 | // neither will necessarily converge faster. |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 117 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 118 | HBasicBlock* block = it.Current(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 119 | for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 120 | worklist_.push_back(inst_it.Current()->AsPhi()); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 124 | while (!worklist_.empty()) { |
| 125 | HPhi* phi = worklist_.back(); |
| 126 | worklist_.pop_back(); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 127 | |
| 128 | // If the phi has already been processed, continue. |
| 129 | if (!phi->IsInBlock()) { |
| 130 | continue; |
| 131 | } |
| 132 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 133 | if (phi->InputCount() == 0) { |
| 134 | DCHECK(phi->IsCatchPhi()); |
| 135 | DCHECK(phi->IsDead()); |
| 136 | continue; |
| 137 | } |
| 138 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 139 | // Find if the inputs of the phi are the same instruction. |
| 140 | HInstruction* candidate = phi->InputAt(0); |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 141 | // A loop phi cannot have itself as the first phi. Note that this |
| 142 | // check relies on our simplification pass ensuring the pre-header |
| 143 | // block is first in the list of predecessors of the loop header. |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 144 | DCHECK(!phi->IsLoopHeaderPhi() || phi->GetBlock()->IsLoopPreHeaderFirstPredecessor()); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 145 | DCHECK_NE(phi, candidate); |
| 146 | |
| 147 | for (size_t i = 1; i < phi->InputCount(); ++i) { |
| 148 | HInstruction* input = phi->InputAt(i); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 149 | // For a loop phi, if the input is the phi, the phi is still candidate for |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 150 | // elimination. |
| 151 | if (input != candidate && input != phi) { |
| 152 | candidate = nullptr; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // If the inputs are not the same, continue. |
| 158 | if (candidate == nullptr) { |
| 159 | continue; |
| 160 | } |
| 161 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 162 | // The candidate may not dominate a phi in a catch block. |
| 163 | if (phi->IsCatchPhi() && !candidate->StrictlyDominates(phi)) { |
| 164 | continue; |
| 165 | } |
| 166 | |
David Brazdil | 77b022d | 2015-08-19 14:17:31 +0100 | [diff] [blame] | 167 | // Because we're updating the users of this phi, we may have new candidates |
| 168 | // for elimination. Add phis that use this phi to the worklist. |
| 169 | for (HUseIterator<HInstruction*> it(phi->GetUses()); !it.Done(); it.Advance()) { |
| 170 | HUseListNode<HInstruction*>* current = it.Current(); |
| 171 | HInstruction* user = current->GetUser(); |
| 172 | if (user->IsPhi()) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 173 | worklist_.push_back(user->AsPhi()); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 174 | } |
| 175 | } |
David Brazdil | 77b022d | 2015-08-19 14:17:31 +0100 | [diff] [blame] | 176 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 177 | phi->ReplaceWith(candidate); |
| 178 | phi->GetBlock()->RemovePhi(phi); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | } // namespace art |