blob: c367611377ef6a3c7257c0d14eb9baa29c89c6f6 [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"
18#include "nodes.h"
19
20namespace art {
21
22void SsaLivenessAnalysis::Analyze() {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010023 LinearizeGraph();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010024 NumberInstructions();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010025 ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010026}
27
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010028static bool IsLoopExit(HLoopInformation* current, HLoopInformation* to) {
29 // `to` is either not part of a loop, or `current` is an inner loop of `to`.
30 return to == nullptr || (current != to && current->IsIn(*to));
31}
32
33static bool IsLoop(HLoopInformation* info) {
34 return info != nullptr;
35}
36
37static bool InSameLoop(HLoopInformation* first_loop, HLoopInformation* second_loop) {
38 return first_loop == second_loop;
39}
40
41static bool IsInnerLoop(HLoopInformation* outer, HLoopInformation* inner) {
42 return (inner != outer)
43 && (inner != nullptr)
44 && (outer != nullptr)
45 && inner->IsIn(*outer);
46}
47
48static void VisitBlockForLinearization(HBasicBlock* block,
49 GrowableArray<HBasicBlock*>* order,
50 ArenaBitVector* visited) {
51 if (visited->IsBitSet(block->GetBlockId())) {
52 return;
53 }
54 visited->SetBit(block->GetBlockId());
55 size_t number_of_successors = block->GetSuccessors().Size();
56 if (number_of_successors == 0) {
57 // Nothing to do.
58 } else if (number_of_successors == 1) {
59 VisitBlockForLinearization(block->GetSuccessors().Get(0), order, visited);
60 } else {
61 DCHECK_EQ(number_of_successors, 2u);
62 HBasicBlock* first_successor = block->GetSuccessors().Get(0);
63 HBasicBlock* second_successor = block->GetSuccessors().Get(1);
64 HLoopInformation* my_loop = block->GetLoopInformation();
65 HLoopInformation* first_loop = first_successor->GetLoopInformation();
66 HLoopInformation* second_loop = second_successor->GetLoopInformation();
67
68 if (!IsLoop(my_loop)) {
69 // Nothing to do. Current order is fine.
70 } else if (IsLoopExit(my_loop, second_loop) && InSameLoop(my_loop, first_loop)) {
71 // Visit the loop exit first in post order.
72 std::swap(first_successor, second_successor);
73 } else if (IsInnerLoop(my_loop, first_loop) && !IsInnerLoop(my_loop, second_loop)) {
74 // Visit the inner loop last in post order.
75 std::swap(first_successor, second_successor);
76 }
77 VisitBlockForLinearization(first_successor, order, visited);
78 VisitBlockForLinearization(second_successor, order, visited);
79 }
80 order->Add(block);
81}
82
83class HLinearOrderIterator : public ValueObject {
84 public:
85 explicit HLinearOrderIterator(const GrowableArray<HBasicBlock*>& post_order)
86 : post_order_(post_order), index_(post_order.Size()) {}
87
88 bool Done() const { return index_ == 0; }
89 HBasicBlock* Current() const { return post_order_.Get(index_ -1); }
90 void Advance() { --index_; DCHECK_GE(index_, 0U); }
91
92 private:
93 const GrowableArray<HBasicBlock*>& post_order_;
94 size_t index_;
95
96 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
97};
98
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010099class HLinearPostOrderIterator : public ValueObject {
100 public:
101 explicit HLinearPostOrderIterator(const GrowableArray<HBasicBlock*>& post_order)
102 : post_order_(post_order), index_(0) {}
103
104 bool Done() const { return index_ == post_order_.Size(); }
105 HBasicBlock* Current() const { return post_order_.Get(index_); }
106 void Advance() { ++index_; }
107
108 private:
109 const GrowableArray<HBasicBlock*>& post_order_;
110 size_t index_;
111
112 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
113};
114
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100115void SsaLivenessAnalysis::LinearizeGraph() {
116 // For simplicity of the implementation, we create post linear order. The order for
117 // computing live ranges is the reverse of that order.
118 ArenaBitVector visited(graph_.GetArena(), graph_.GetBlocks().Size(), false);
119 VisitBlockForLinearization(graph_.GetEntryBlock(), &linear_post_order_, &visited);
120}
121
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100122void SsaLivenessAnalysis::NumberInstructions() {
123 int ssa_index = 0;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100124 size_t lifetime_position = 0;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100125 // Each instruction gets a lifetime position, and a block gets a lifetime
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100126 // start and end position. Non-phi instructions have a distinct lifetime position than
127 // the block they are in. Phi instructions have the lifetime start of their block as
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100128 // lifetime position.
129 //
130 // Because the register allocator will insert moves in the graph, we need
131 // to differentiate between the start and end of an instruction. Adding 2 to
132 // the lifetime position for each instruction ensures the start of an
133 // instruction is different than the end of the previous instruction.
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100134 for (HLinearOrderIterator it(linear_post_order_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100135 HBasicBlock* block = it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100136 block->SetLifetimeStart(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100137
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100138 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100139 HInstruction* current = it.Current();
140 if (current->HasUses()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100141 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100142 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100143 current->SetLiveInterval(
144 new (graph_.GetArena()) LiveInterval(graph_.GetArena(), current->GetType()));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100145 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100146 current->SetLifetimePosition(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100147 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100148 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100149
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100150 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100151 HInstruction* current = it.Current();
152 if (current->HasUses()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100153 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100154 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100155 current->SetLiveInterval(
156 new (graph_.GetArena()) LiveInterval(graph_.GetArena(), current->GetType()));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100157 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100158 current->SetLifetimePosition(lifetime_position);
159 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100160 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100161
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100162 block->SetLifetimeEnd(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163 }
164 number_of_ssa_values_ = ssa_index;
165}
166
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100167void SsaLivenessAnalysis::ComputeLiveness() {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100168 for (HLinearOrderIterator it(linear_post_order_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100169 HBasicBlock* block = it.Current();
170 block_infos_.Put(
171 block->GetBlockId(),
172 new (graph_.GetArena()) BlockInfo(graph_.GetArena(), *block, number_of_ssa_values_));
173 }
174
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100175 // Compute the live ranges, as well as the initial live_in, live_out, and kill sets.
176 // This method does not handle backward branches for the sets, therefore live_in
177 // and live_out sets are not yet correct.
178 ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100179
180 // Do a fixed point calculation to take into account backward branches,
181 // that will update live_in of loop headers, and therefore live_out and live_in
182 // of blocks in the loop.
183 ComputeLiveInAndLiveOutSets();
184}
185
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100186void SsaLivenessAnalysis::ComputeLiveRanges() {
187 // Do a post order visit, adding inputs of instructions live in the block where
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100188 // that instruction is defined, and killing instructions that are being visited.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100189 for (HLinearPostOrderIterator it(linear_post_order_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100190 HBasicBlock* block = it.Current();
191
192 BitVector* kill = GetKillSet(*block);
193 BitVector* live_in = GetLiveInSet(*block);
194
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100195 // Set phi inputs of successors of this block corresponding to this block
196 // as live_in.
197 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
198 HBasicBlock* successor = block->GetSuccessors().Get(i);
199 live_in->Union(GetLiveInSet(*successor));
200 size_t phi_input_index = successor->GetPredecessorIndexOf(block);
201 for (HInstructionIterator it(successor->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100202 HInstruction* phi = it.Current();
203 HInstruction* input = phi->InputAt(phi_input_index);
204 input->GetLiveInterval()->AddPhiUse(phi, block);
205 // A phi input whose last user is the phi dies at the end of the predecessor block,
206 // and not at the phi's lifetime position.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100207 live_in->SetBit(input->GetSsaIndex());
208 }
209 }
210
211 // Add a range that covers this block to all instructions live_in because of successors.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100212 for (uint32_t idx : live_in->Indexes()) {
213 HInstruction* current = instructions_from_ssa_index_.Get(idx);
214 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100215 }
216
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100217 for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100218 HInstruction* current = it.Current();
219 if (current->HasSsaIndex()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100220 // Kill the instruction and shorten its interval.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100221 kill->SetBit(current->GetSsaIndex());
222 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100223 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100224 }
225
226 // All inputs of an instruction must be live.
227 for (size_t i = 0, e = current->InputCount(); i < e; ++i) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100228 HInstruction* input = current->InputAt(i);
229 DCHECK(input->HasSsaIndex());
230 live_in->SetBit(input->GetSsaIndex());
231 input->GetLiveInterval()->AddUse(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100232 }
233
234 if (current->HasEnvironment()) {
235 // All instructions in the environment must be live.
236 GrowableArray<HInstruction*>* environment = current->GetEnvironment()->GetVRegs();
237 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
238 HInstruction* instruction = environment->Get(i);
239 if (instruction != nullptr) {
240 DCHECK(instruction->HasSsaIndex());
241 live_in->SetBit(instruction->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100242 instruction->GetLiveInterval()->AddUse(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100243 }
244 }
245 }
246 }
247
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100248 // Kill phis defined in this block.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100249 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100250 HInstruction* current = it.Current();
251 if (current->HasSsaIndex()) {
252 kill->SetBit(current->GetSsaIndex());
253 live_in->ClearBit(current->GetSsaIndex());
254 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100255 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100256
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100257 if (block->IsLoopHeader()) {
258 HBasicBlock* back_edge = block->GetLoopInformation()->GetBackEdges().Get(0);
259 // For all live_in instructions at the loop header, we need to create a range
260 // that covers the full loop.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100261 for (uint32_t idx : live_in->Indexes()) {
262 HInstruction* current = instructions_from_ssa_index_.Get(idx);
263 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(),
264 back_edge->GetLifetimeEnd());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100265 }
266 }
267 }
268}
269
270void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
271 bool changed;
272 do {
273 changed = false;
274
275 for (HPostOrderIterator it(graph_); !it.Done(); it.Advance()) {
276 const HBasicBlock& block = *it.Current();
277
278 // The live_in set depends on the kill set (which does not
279 // change in this loop), and the live_out set. If the live_out
280 // set does not change, there is no need to update the live_in set.
281 if (UpdateLiveOut(block) && UpdateLiveIn(block)) {
282 changed = true;
283 }
284 }
285 } while (changed);
286}
287
288bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
289 BitVector* live_out = GetLiveOutSet(block);
290 bool changed = false;
291 // The live_out set of a block is the union of live_in sets of its successors.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100292 for (size_t i = 0, e = block.GetSuccessors().Size(); i < e; ++i) {
293 HBasicBlock* successor = block.GetSuccessors().Get(i);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100294 if (live_out->Union(GetLiveInSet(*successor))) {
295 changed = true;
296 }
297 }
298 return changed;
299}
300
301
302bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
303 BitVector* live_out = GetLiveOutSet(block);
304 BitVector* kill = GetKillSet(block);
305 BitVector* live_in = GetLiveInSet(block);
306 // If live_out is updated (because of backward branches), we need to make
307 // sure instructions in live_out are also in live_in, unless they are killed
308 // by this block.
309 return live_in->UnionIfNotIn(live_out, kill);
310}
311
312} // namespace art