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