blob: 5de1ab9c312f431efbb1fb5ee6f3fe21c161bd4b [file] [log] [blame]
Nicolas Geoffray804d0932014-05-02 08:46:00 +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_liveness_analysis.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010018
Ian Rogerse77493c2014-08-20 15:08:45 -070019#include "base/bit_vector-inl.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010020#include "code_generator.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010021#include "nodes.h"
22
23namespace art {
24
25void SsaLivenessAnalysis::Analyze() {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010026 LinearizeGraph();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010027 NumberInstructions();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010028 ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010029}
30
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010031static bool IsLoopExit(HLoopInformation* current, HLoopInformation* to) {
32 // `to` is either not part of a loop, or `current` is an inner loop of `to`.
33 return to == nullptr || (current != to && current->IsIn(*to));
34}
35
36static bool IsLoop(HLoopInformation* info) {
37 return info != nullptr;
38}
39
40static bool InSameLoop(HLoopInformation* first_loop, HLoopInformation* second_loop) {
41 return first_loop == second_loop;
42}
43
44static bool IsInnerLoop(HLoopInformation* outer, HLoopInformation* inner) {
45 return (inner != outer)
46 && (inner != nullptr)
47 && (outer != nullptr)
48 && inner->IsIn(*outer);
49}
50
51static void VisitBlockForLinearization(HBasicBlock* block,
52 GrowableArray<HBasicBlock*>* order,
53 ArenaBitVector* visited) {
54 if (visited->IsBitSet(block->GetBlockId())) {
55 return;
56 }
57 visited->SetBit(block->GetBlockId());
58 size_t number_of_successors = block->GetSuccessors().Size();
59 if (number_of_successors == 0) {
60 // Nothing to do.
61 } else if (number_of_successors == 1) {
62 VisitBlockForLinearization(block->GetSuccessors().Get(0), order, visited);
63 } else {
64 DCHECK_EQ(number_of_successors, 2u);
65 HBasicBlock* first_successor = block->GetSuccessors().Get(0);
66 HBasicBlock* second_successor = block->GetSuccessors().Get(1);
67 HLoopInformation* my_loop = block->GetLoopInformation();
68 HLoopInformation* first_loop = first_successor->GetLoopInformation();
69 HLoopInformation* second_loop = second_successor->GetLoopInformation();
70
71 if (!IsLoop(my_loop)) {
72 // Nothing to do. Current order is fine.
73 } else if (IsLoopExit(my_loop, second_loop) && InSameLoop(my_loop, first_loop)) {
74 // Visit the loop exit first in post order.
75 std::swap(first_successor, second_successor);
76 } else if (IsInnerLoop(my_loop, first_loop) && !IsInnerLoop(my_loop, second_loop)) {
77 // Visit the inner loop last in post order.
78 std::swap(first_successor, second_successor);
79 }
80 VisitBlockForLinearization(first_successor, order, visited);
81 VisitBlockForLinearization(second_successor, order, visited);
82 }
83 order->Add(block);
84}
85
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010086void SsaLivenessAnalysis::LinearizeGraph() {
87 // For simplicity of the implementation, we create post linear order. The order for
88 // computing live ranges is the reverse of that order.
89 ArenaBitVector visited(graph_.GetArena(), graph_.GetBlocks().Size(), false);
90 VisitBlockForLinearization(graph_.GetEntryBlock(), &linear_post_order_, &visited);
91}
92
Nicolas Geoffray804d0932014-05-02 08:46:00 +010093void SsaLivenessAnalysis::NumberInstructions() {
94 int ssa_index = 0;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010095 size_t lifetime_position = 0;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010096 // Each instruction gets a lifetime position, and a block gets a lifetime
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010097 // start and end position. Non-phi instructions have a distinct lifetime position than
98 // the block they are in. Phi instructions have the lifetime start of their block as
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010099 // lifetime position.
100 //
101 // Because the register allocator will insert moves in the graph, we need
102 // to differentiate between the start and end of an instruction. Adding 2 to
103 // the lifetime position for each instruction ensures the start of an
104 // instruction is different than the end of the previous instruction.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100105 for (HLinearOrderIterator it(*this); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100106 HBasicBlock* block = it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100107 block->SetLifetimeStart(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100108
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100109 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100110 HInstruction* current = it.Current();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100111 current->Accept(codegen_->GetLocationBuilder());
112 LocationSummary* locations = current->GetLocations();
113 if (locations != nullptr && locations->Out().IsValid()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100114 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100115 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100116 current->SetLiveInterval(
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100117 new (graph_.GetArena()) LiveInterval(graph_.GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100118 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100119 current->SetLifetimePosition(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100120 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100121 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100122
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100123 // Add a null marker to notify we are starting a block.
124 instructions_from_lifetime_position_.Add(nullptr);
125
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100126 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100127 HInstruction* current = it.Current();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100128 current->Accept(codegen_->GetLocationBuilder());
129 LocationSummary* locations = current->GetLocations();
130 if (locations != nullptr && locations->Out().IsValid()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100131 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100132 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100133 current->SetLiveInterval(
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100134 new (graph_.GetArena()) LiveInterval(graph_.GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100135 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100136 instructions_from_lifetime_position_.Add(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100137 current->SetLifetimePosition(lifetime_position);
138 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100139 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100140
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100141 block->SetLifetimeEnd(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100142 }
143 number_of_ssa_values_ = ssa_index;
144}
145
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100146void SsaLivenessAnalysis::ComputeLiveness() {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100147 for (HLinearOrderIterator it(*this); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100148 HBasicBlock* block = it.Current();
149 block_infos_.Put(
150 block->GetBlockId(),
151 new (graph_.GetArena()) BlockInfo(graph_.GetArena(), *block, number_of_ssa_values_));
152 }
153
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100154 // Compute the live ranges, as well as the initial live_in, live_out, and kill sets.
155 // This method does not handle backward branches for the sets, therefore live_in
156 // and live_out sets are not yet correct.
157 ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100158
159 // Do a fixed point calculation to take into account backward branches,
160 // that will update live_in of loop headers, and therefore live_out and live_in
161 // of blocks in the loop.
162 ComputeLiveInAndLiveOutSets();
163}
164
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100165void SsaLivenessAnalysis::ComputeLiveRanges() {
166 // Do a post order visit, adding inputs of instructions live in the block where
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 // that instruction is defined, and killing instructions that are being visited.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100168 for (HLinearPostOrderIterator it(*this); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100169 HBasicBlock* block = it.Current();
170
171 BitVector* kill = GetKillSet(*block);
172 BitVector* live_in = GetLiveInSet(*block);
173
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100174 // Set phi inputs of successors of this block corresponding to this block
175 // as live_in.
176 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
177 HBasicBlock* successor = block->GetSuccessors().Get(i);
178 live_in->Union(GetLiveInSet(*successor));
179 size_t phi_input_index = successor->GetPredecessorIndexOf(block);
180 for (HInstructionIterator it(successor->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100181 HInstruction* phi = it.Current();
182 HInstruction* input = phi->InputAt(phi_input_index);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100183 input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100184 // A phi input whose last user is the phi dies at the end of the predecessor block,
185 // and not at the phi's lifetime position.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100186 live_in->SetBit(input->GetSsaIndex());
187 }
188 }
189
190 // Add a range that covers this block to all instructions live_in because of successors.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100191 for (uint32_t idx : live_in->Indexes()) {
192 HInstruction* current = instructions_from_ssa_index_.Get(idx);
193 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100194 }
195
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100196 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100197 HInstruction* current = it.Current();
198 if (current->HasSsaIndex()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100199 // Kill the instruction and shorten its interval.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100200 kill->SetBit(current->GetSsaIndex());
201 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100202 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100203 }
204
205 // All inputs of an instruction must be live.
206 for (size_t i = 0, e = current->InputCount(); i < e; ++i) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100207 HInstruction* input = current->InputAt(i);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100208 // Some instructions 'inline' their inputs, that is they do not need
209 // to be materialized.
210 if (input->HasSsaIndex()) {
211 live_in->SetBit(input->GetSsaIndex());
212 input->GetLiveInterval()->AddUse(current, i, false);
213 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100214 }
215
216 if (current->HasEnvironment()) {
217 // All instructions in the environment must be live.
218 GrowableArray<HInstruction*>* environment = current->GetEnvironment()->GetVRegs();
219 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
220 HInstruction* instruction = environment->Get(i);
221 if (instruction != nullptr) {
222 DCHECK(instruction->HasSsaIndex());
223 live_in->SetBit(instruction->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100224 instruction->GetLiveInterval()->AddUse(current, i, true);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100225 }
226 }
227 }
228 }
229
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100230 // Kill phis defined in this block.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100231 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100232 HInstruction* current = it.Current();
233 if (current->HasSsaIndex()) {
234 kill->SetBit(current->GetSsaIndex());
235 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100236 LiveInterval* interval = current->GetLiveInterval();
237 DCHECK((interval->GetFirstRange() == nullptr)
238 || (interval->GetStart() == current->GetLifetimePosition()));
239 interval->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100240 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100241 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100242
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100243 if (block->IsLoopHeader()) {
244 HBasicBlock* back_edge = block->GetLoopInformation()->GetBackEdges().Get(0);
245 // For all live_in instructions at the loop header, we need to create a range
246 // that covers the full loop.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100247 for (uint32_t idx : live_in->Indexes()) {
248 HInstruction* current = instructions_from_ssa_index_.Get(idx);
249 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(),
250 back_edge->GetLifetimeEnd());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100251 }
252 }
253 }
254}
255
256void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
257 bool changed;
258 do {
259 changed = false;
260
261 for (HPostOrderIterator it(graph_); !it.Done(); it.Advance()) {
262 const HBasicBlock& block = *it.Current();
263
264 // The live_in set depends on the kill set (which does not
265 // change in this loop), and the live_out set. If the live_out
266 // set does not change, there is no need to update the live_in set.
267 if (UpdateLiveOut(block) && UpdateLiveIn(block)) {
268 changed = true;
269 }
270 }
271 } while (changed);
272}
273
274bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
275 BitVector* live_out = GetLiveOutSet(block);
276 bool changed = false;
277 // The live_out set of a block is the union of live_in sets of its successors.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100278 for (size_t i = 0, e = block.GetSuccessors().Size(); i < e; ++i) {
279 HBasicBlock* successor = block.GetSuccessors().Get(i);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100280 if (live_out->Union(GetLiveInSet(*successor))) {
281 changed = true;
282 }
283 }
284 return changed;
285}
286
287
288bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
289 BitVector* live_out = GetLiveOutSet(block);
290 BitVector* kill = GetKillSet(block);
291 BitVector* live_in = GetLiveInSet(block);
292 // If live_out is updated (because of backward branches), we need to make
293 // sure instructions in live_out are also in live_in, unless they are killed
294 // by this block.
295 return live_in->UnionIfNotIn(live_out, kill);
296}
297
298} // namespace art