blob: aec7a3c555a46ba3dcd3619465d0231baadbe2e0 [file] [log] [blame]
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001/*
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 Brazdil809d70f2015-11-19 10:29:39 +000019#include "base/arena_containers.h"
Vladimir Markoc9ef1682016-05-10 13:31:23 +010020#include "base/arena_bit_vector.h"
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +000021#include "base/bit_vector-inl.h"
David Brazdil809d70f2015-11-19 10:29:39 +000022
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010023namespace art {
24
25void SsaDeadPhiElimination::Run() {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000026 MarkDeadPhis();
27 EliminateDeadPhis();
28}
29
30void SsaDeadPhiElimination::MarkDeadPhis() {
David Brazdil809d70f2015-11-19 10:29:39 +000031 // Phis are constructed live and should not be revived if previously marked
32 // dead. This algorithm temporarily breaks that invariant but we DCHECK that
33 // only phis which were initially live are revived.
Vladimir Marko3ea5a972016-05-09 20:23:34 +010034 ArenaSet<HPhi*> initially_live(graph_->GetArena()->Adapter(kArenaAllocSsaPhiElimination));
David Brazdil809d70f2015-11-19 10:29:39 +000035
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010036 // Add to the worklist phis referenced by non-phi instructions.
Vladimir Marko2c45bc92016-10-25 16:54:12 +010037 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -080038 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
39 HPhi* phi = inst_it.Current()->AsPhi();
David Brazdil809d70f2015-11-19 10:29:39 +000040 if (phi->IsDead()) {
41 continue;
42 }
43
David Brazdild9510df2015-11-04 23:30:22 +000044 bool keep_alive = (graph_->IsDebuggable() && phi->HasEnvironmentUses());
45 if (!keep_alive) {
Vladimir Marko46817b82016-03-29 12:21:58 +010046 for (const HUseListNode<HInstruction*>& use : phi->GetUses()) {
47 if (!use.GetUser()->IsPhi()) {
David Brazdild9510df2015-11-04 23:30:22 +000048 keep_alive = true;
49 break;
50 }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010051 }
52 }
David Brazdil809d70f2015-11-19 10:29:39 +000053
David Brazdild9510df2015-11-04 23:30:22 +000054 if (keep_alive) {
David Brazdil809d70f2015-11-19 10:29:39 +000055 worklist_.push_back(phi);
56 } else {
57 phi->SetDead();
58 if (kIsDebugBuild) {
59 initially_live.insert(phi);
60 }
61 }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010062 }
63 }
64
65 // Process the worklist by propagating liveness to phi inputs.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010066 while (!worklist_.empty()) {
67 HPhi* phi = worklist_.back();
68 worklist_.pop_back();
Vladimir Marko372f10e2016-05-17 16:30:10 +010069 for (HInstruction* raw_input : phi->GetInputs()) {
70 HPhi* input = raw_input->AsPhi();
David Brazdil809d70f2015-11-19 10:29:39 +000071 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 Geoffray7dc206a2014-07-11 09:49:49 +010077 }
78 }
79 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000080}
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010081
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000082void SsaDeadPhiElimination::EliminateDeadPhis() {
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +010083 // 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).
Vladimir Marko2c45bc92016-10-25 16:54:12 +010086 for (HBasicBlock* block : graph_->GetPostOrder()) {
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010087 HInstruction* current = block->GetFirstPhi();
88 HInstruction* next = nullptr;
David Brazdil1abb4192015-02-17 18:33:36 +000089 HPhi* phi;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010090 while (current != nullptr) {
David Brazdil1abb4192015-02-17 18:33:36 +000091 phi = current->AsPhi();
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010092 next = current->GetNext();
David Brazdil1abb4192015-02-17 18:33:36 +000093 if (phi->IsDead()) {
94 // Make sure the phi is only used by other dead phis.
95 if (kIsDebugBuild) {
Vladimir Marko46817b82016-03-29 12:21:58 +010096 for (const HUseListNode<HInstruction*>& use : phi->GetUses()) {
97 HInstruction* user = use.GetUser();
David Brazdild9510df2015-11-04 23:30:22 +000098 DCHECK(user->IsLoopHeaderPhi());
99 DCHECK(user->AsPhi()->IsDead());
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100100 }
101 }
David Brazdil1abb4192015-02-17 18:33:36 +0000102 // Remove the phi from use lists of its inputs.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100103 phi->RemoveAsUserOfAllInputs();
David Brazdil1abb4192015-02-17 18:33:36 +0000104 // Remove the phi from environments that use it.
Vladimir Marko46817b82016-03-29 12:21:58 +0100105 for (const HUseListNode<HEnvironment*>& use : phi->GetEnvUses()) {
106 HEnvironment* user = use.GetUser();
107 user->SetRawEnvAt(use.GetIndex(), nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +0000108 }
109 // Delete it from the instruction list.
110 block->RemovePhi(phi, /*ensure_safety=*/ false);
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100111 }
112 current = next;
113 }
114 }
115}
116
117void SsaRedundantPhiElimination::Run() {
David Brazdil77b022d2015-08-19 14:17:31 +0100118 // Add all phis in the worklist. Order does not matter for correctness, and
119 // neither will necessarily converge faster.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100120 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800121 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100122 worklist_.push_back(inst_it.Current()->AsPhi());
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100123 }
124 }
125
Vladimir Markoc9ef1682016-05-10 13:31:23 +0100126 ArenaBitVector visited_phis_in_cycle(graph_->GetArena(),
127 graph_->GetCurrentInstructionId(),
128 /* expandable */ false,
129 kArenaAllocSsaPhiElimination);
Vladimir Marko3ea5a972016-05-09 20:23:34 +0100130 ArenaVector<HPhi*> cycle_worklist(graph_->GetArena()->Adapter(kArenaAllocSsaPhiElimination));
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000131
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100132 while (!worklist_.empty()) {
133 HPhi* phi = worklist_.back();
134 worklist_.pop_back();
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100135
136 // If the phi has already been processed, continue.
137 if (!phi->IsInBlock()) {
138 continue;
139 }
140
Nicolas Geoffray05b3fa02016-05-04 12:05:56 +0100141 // If the phi is dead, we know we won't revive it and it will be removed,
142 // so don't process it.
143 if (phi->IsDead()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100144 continue;
145 }
146
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000147 HInstruction* candidate = nullptr;
Vladimir Markoc9ef1682016-05-10 13:31:23 +0100148 visited_phis_in_cycle.ClearAllBits();
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000149 cycle_worklist.clear();
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100150
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000151 cycle_worklist.push_back(phi);
Vladimir Markoc9ef1682016-05-10 13:31:23 +0100152 visited_phis_in_cycle.SetBit(phi->GetId());
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000153 bool catch_phi_in_cycle = phi->IsCatchPhi();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000154 bool irreducible_loop_phi_in_cycle = phi->IsIrreducibleLoopHeaderPhi();
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000155
156 // First do a simple loop over inputs and check if they are all the same.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100157 for (HInstruction* input : phi->GetInputs()) {
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000158 if (input == phi) {
159 continue;
160 } else if (candidate == nullptr) {
161 candidate = input;
162 } else if (candidate != input) {
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100163 candidate = nullptr;
164 break;
165 }
166 }
167
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000168 // If we haven't found a candidate, check for a phi cycle. Note that we need to detect
169 // such cycles to avoid having reference and non-reference equivalents. We check this
170 // invariant in the graph checker.
171 if (candidate == nullptr) {
172 // We iterate over the array as long as it grows.
173 for (size_t i = 0; i < cycle_worklist.size(); ++i) {
174 HPhi* current = cycle_worklist[i];
175 DCHECK(!current->IsLoopHeaderPhi() ||
176 current->GetBlock()->IsLoopPreHeaderFirstPredecessor());
177
Vladimir Marko372f10e2016-05-17 16:30:10 +0100178 for (HInstruction* input : current->GetInputs()) {
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000179 if (input == current) {
180 continue;
181 } else if (input->IsPhi()) {
Vladimir Markoc9ef1682016-05-10 13:31:23 +0100182 if (!visited_phis_in_cycle.IsBitSet(input->GetId())) {
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000183 cycle_worklist.push_back(input->AsPhi());
Vladimir Markoc9ef1682016-05-10 13:31:23 +0100184 visited_phis_in_cycle.SetBit(input->GetId());
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000185 catch_phi_in_cycle |= input->AsPhi()->IsCatchPhi();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000186 irreducible_loop_phi_in_cycle |= input->IsIrreducibleLoopHeaderPhi();
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000187 } else {
188 // Already visited, nothing to do.
189 }
190 } else if (candidate == nullptr) {
191 candidate = input;
192 } else if (candidate != input) {
193 candidate = nullptr;
194 // Clear the cycle worklist to break out of the outer loop.
195 cycle_worklist.clear();
196 break;
197 }
198 }
199 }
200 }
201
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100202 if (candidate == nullptr) {
203 continue;
204 }
205
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000206 if (irreducible_loop_phi_in_cycle && !candidate->IsConstant()) {
207 // For irreducible loops, we need to keep the phis to satisfy our linear scan
208 // algorithm.
209 // There is one exception for constants, as the type propagation requires redundant
210 // cyclic phis of a constant to be removed. This is ok for the linear scan as it
211 // has to deal with constants anyway, and they can trivially be rematerialized.
212 continue;
213 }
214
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000215 for (HPhi* current : cycle_worklist) {
216 // The candidate may not dominate a phi in a catch block: there may be non-throwing
217 // instructions at the beginning of a try range, that may be the first input of
218 // catch phis.
219 // TODO(dbrazdil): Remove this situation by moving those non-throwing instructions
220 // before the try entry.
221 if (catch_phi_in_cycle) {
222 if (!candidate->StrictlyDominates(current)) {
223 continue;
224 }
225 } else {
226 DCHECK(candidate->StrictlyDominates(current));
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100227 }
David Brazdil77b022d2015-08-19 14:17:31 +0100228
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000229 // Because we're updating the users of this phi, we may have new candidates
230 // for elimination. Add phis that use this phi to the worklist.
Vladimir Marko46817b82016-03-29 12:21:58 +0100231 for (const HUseListNode<HInstruction*>& use : current->GetUses()) {
232 HInstruction* user = use.GetUser();
Vladimir Markoc9ef1682016-05-10 13:31:23 +0100233 if (user->IsPhi() && !visited_phis_in_cycle.IsBitSet(user->GetId())) {
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000234 worklist_.push_back(user->AsPhi());
235 }
236 }
237 DCHECK(candidate->StrictlyDominates(current));
238 current->ReplaceWith(candidate);
239 current->GetBlock()->RemovePhi(current);
240 }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100241 }
242}
243
244} // namespace art