blob: 4d56e1f9c15088e890ea460f94f67b6501aaa4e0 [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#ifndef ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_
18#define ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_
19
20#include "nodes.h"
21
22namespace art {
23
24class BlockInfo : public ArenaObject {
25 public:
26 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
27 : block_(block),
28 live_in_(allocator, number_of_ssa_values, false),
29 live_out_(allocator, number_of_ssa_values, false),
30 kill_(allocator, number_of_ssa_values, false) {
31 live_in_.ClearAllBits();
32 live_out_.ClearAllBits();
33 kill_.ClearAllBits();
34 }
35
36 private:
37 const HBasicBlock& block_;
38 ArenaBitVector live_in_;
39 ArenaBitVector live_out_;
40 ArenaBitVector kill_;
41
42 friend class SsaLivenessAnalysis;
43
44 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
45};
46
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010047/**
48 * A live range contains the start and end of a range where an instruction
49 * is live.
50 */
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010051class LiveRange : public ArenaObject {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010052 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010053 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010054 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010055 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056 }
57
58 size_t GetStart() const { return start_; }
59 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010060 LiveRange* GetNext() const { return next_; }
61
62 bool IntersectsWith(const LiveRange& other) {
63 return (start_ >= other.start_ && start_ < other.end_)
64 || (other.start_ >= start_ && other.start_ < end_);
65 }
66
67 bool IsBefore(const LiveRange& other) {
68 return end_ <= other.start_;
69 }
70
71 void Dump(std::ostream& stream) {
72 stream << "[" << start_ << ", " << end_ << ")";
73 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010074
75 private:
76 size_t start_;
77 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010078 LiveRange* next_;
79
80 friend class LiveInterval;
81
82 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010083};
84
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010085/**
86 * A use position represents a live interval use at a given position.
87 */
88class UsePosition : public ArenaObject {
89 public:
90 UsePosition(HInstruction* user, size_t position, UsePosition* next)
91 : user_(user), position_(position), next_(next) {
92 DCHECK(user->AsPhi() != nullptr || GetPosition() == user->GetLifetimePosition());
93 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
94 }
95
96 size_t GetPosition() const { return position_; }
97
98 UsePosition* GetNext() const { return next_; }
99
100 HInstruction* GetUser() const { return user_; }
101
102 void Dump(std::ostream& stream) {
103 stream << position_;
104 }
105
106 private:
107 HInstruction* const user_;
108 const size_t position_;
109 UsePosition* const next_;
110
111 DISALLOW_COPY_AND_ASSIGN(UsePosition);
112};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100113
114/**
115 * An interval is a list of disjoint live ranges where an instruction is live.
116 * Each instruction that has uses gets an interval.
117 */
118class LiveInterval : public ArenaObject {
119 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100120 LiveInterval(ArenaAllocator* allocator, Primitive::Type type)
121 : allocator_(allocator),
122 first_range_(nullptr),
123 last_range_(nullptr),
124 first_use_(nullptr),
125 type_(type),
126 next_sibling_(nullptr),
127 register_(kNoRegister) {}
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100128
129 void AddUse(HInstruction* instruction) {
130 size_t position = instruction->GetLifetimePosition();
131 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
132 size_t end_block_position = instruction->GetBlock()->GetLifetimeEnd();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100133 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100134 // First time we see a use of that interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100135 first_range_ = last_range_ = new (allocator_) LiveRange(start_block_position, position, nullptr);
136 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100137 // There is a use later in the same block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100138 DCHECK_LE(position, first_range_->GetEnd());
139 } else if (first_range_->GetStart() == end_block_position) {
140 // Last use is in the following block.
141 first_range_->start_ = start_block_position;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100142 } else {
143 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100144 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100145 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 first_use_ = new (allocator_) UsePosition(instruction, position, first_use_);
147 }
148
149 void AddPhiUse(HInstruction* instruction, HBasicBlock* block) {
150 DCHECK(instruction->AsPhi() != nullptr);
151 first_use_ = new (allocator_) UsePosition(instruction, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100152 }
153
154 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100155 if (first_range_ == nullptr) {
156 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
157 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100158 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100159 first_range_->start_ = start;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100160 } else {
161 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100162 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100163 }
164 }
165
166 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100167 DCHECK(first_range_ != nullptr);
168 while (first_range_ != nullptr && first_range_->GetEnd() < end) {
169 DCHECK_LE(start, first_range_->GetStart());
170 first_range_ = first_range_->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100171 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100172 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100173 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100174 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100175 } else {
176 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100177 first_range_->start_ = start;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100178 }
179 }
180
181 void SetFrom(size_t from) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100182 DCHECK(first_range_ != nullptr);
183 first_range_->start_ = from;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100184 }
185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100186 LiveRange* GetFirstRange() const { return first_range_; }
187
188 int GetRegister() const { return register_; }
189 void SetRegister(int reg) { register_ = reg; }
190 void ClearRegister() { register_ = kNoRegister; }
191 bool HasRegister() const { return register_ != kNoRegister; }
192
193 bool IsDeadAt(size_t position) {
194 return last_range_->GetEnd() <= position;
195 }
196
197 bool Covers(size_t position) {
198 LiveRange* current = first_range_;
199 while (current != nullptr) {
200 if (position >= current->GetStart() && position < current->GetEnd()) {
201 return true;
202 }
203 current = current->GetNext();
204 }
205 return false;
206 }
207
208 /**
209 * Returns the first intersection of this interval with `other`.
210 */
211 size_t FirstIntersectionWith(LiveInterval* other) {
212 // We only call this method if there is a lifetime hole in this interval
213 // at the start of `other`.
214 DCHECK(!Covers(other->GetStart()));
215 DCHECK_LE(GetStart(), other->GetStart());
216 // Move to the range in this interval that starts after the other interval.
217 size_t other_start = other->GetStart();
218 LiveRange* my_range = first_range_;
219 while (my_range != nullptr) {
220 if (my_range->GetStart() >= other_start) {
221 break;
222 } else {
223 my_range = my_range->GetNext();
224 }
225 }
226 if (my_range == nullptr) {
227 return kNoLifetime;
228 }
229
230 // Advance both intervals and find the first matching range start in
231 // this interval.
232 LiveRange* other_range = other->first_range_;
233 do {
234 if (my_range->IntersectsWith(*other_range)) {
235 return std::max(my_range->GetStart(), other_range->GetStart());
236 } else if (my_range->IsBefore(*other_range)) {
237 my_range = my_range->GetNext();
238 if (my_range == nullptr) {
239 return kNoLifetime;
240 }
241 } else {
242 DCHECK(other_range->IsBefore(*my_range));
243 other_range = other_range->GetNext();
244 if (other_range == nullptr) {
245 return kNoLifetime;
246 }
247 }
248 } while (true);
249 }
250
251 size_t GetStart() const {
252 return first_range_->GetStart();
253 }
254
255 size_t FirstRegisterUseAfter(size_t position) const {
256 UsePosition* use = first_use_;
257 while (use != nullptr) {
258 size_t use_position = use->GetPosition();
259 // TODO: Once we plug the Locations builder of the code generator
260 // to the register allocator, this method must be adjusted. We
261 // test if there is an environment, because these are currently the only
262 // instructions that could have more uses than the number of registers.
263 if (use_position >= position && !use->GetUser()->NeedsEnvironment()) {
264 return use_position;
265 }
266 use = use->GetNext();
267 }
268 return kNoLifetime;
269 }
270
271 size_t FirstRegisterUse() const {
272 return FirstRegisterUseAfter(GetStart());
273 }
274
275 Primitive::Type GetType() const {
276 return type_;
277 }
278
279 /**
280 * Split this interval at `position`. This interval is changed to:
281 * [start ... position).
282 *
283 * The new interval covers:
284 * [position ... end)
285 */
286 LiveInterval* SplitAt(size_t position) {
287 DCHECK(next_sibling_ == nullptr);
288 DCHECK_GT(position, GetStart());
289
290 if (last_range_->GetEnd() <= position) {
291 // This range dies before `position`, no need to split.
292 return nullptr;
293 }
294
295 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
296 next_sibling_ = new_interval;
297
298 new_interval->first_use_ = first_use_;
299 LiveRange* current = first_range_;
300 LiveRange* previous = nullptr;
301 // Iterate over the ranges, and either find a range that covers this position, or
302 // a two ranges in between this position (that is, the position is in a lifetime hole).
303 do {
304 if (position >= current->GetEnd()) {
305 // Move to next range.
306 previous = current;
307 current = current->next_;
308 } else if (position <= current->GetStart()) {
309 // If the previous range did not cover this position, we know position is in
310 // a lifetime hole. We can just break the first_range_ and last_range_ links
311 // and return the new interval.
312 DCHECK(previous != nullptr);
313 DCHECK(current != first_range_);
314 new_interval->last_range_ = last_range_;
315 last_range_ = previous;
316 previous->next_ = nullptr;
317 new_interval->first_range_ = current;
318 return new_interval;
319 } else {
320 // This range covers position. We create a new last_range_ for this interval
321 // that covers last_range_->Start() and position. We also shorten the current
322 // range and make it the first range of the new interval.
323 DCHECK(position < current->GetEnd() && position > current->GetStart());
324 new_interval->last_range_ = last_range_;
325 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
326 if (previous != nullptr) {
327 previous->next_ = last_range_;
328 } else {
329 first_range_ = last_range_;
330 }
331 new_interval->first_range_ = current;
332 current->start_ = position;
333 return new_interval;
334 }
335 } while (current != nullptr);
336
337 LOG(FATAL) << "Unreachable";
338 return nullptr;
339 }
340
341 bool StartsBefore(LiveInterval* other) const {
342 return GetStart() <= other->GetStart();
343 }
344
345 bool StartsAfter(LiveInterval* other) const {
346 return GetStart() >= other->GetStart();
347 }
348
349 void Dump(std::ostream& stream) const {
350 stream << "ranges: { ";
351 LiveRange* current = first_range_;
352 do {
353 current->Dump(stream);
354 stream << " ";
355 } while ((current = current->GetNext()) != nullptr);
356 stream << "}, uses: { ";
357 UsePosition* use = first_use_;
358 if (use != nullptr) {
359 do {
360 use->Dump(stream);
361 stream << " ";
362 } while ((use = use->GetNext()) != nullptr);
363 }
364 stream << "}";
365 }
366
367 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100368
369 private:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100370 ArenaAllocator* const allocator_;
371
372 // Ranges of this interval. We need a quick access to the last range to test
373 // for liveness (see `IsDeadAt`).
374 LiveRange* first_range_;
375 LiveRange* last_range_;
376
377 // Uses of this interval. Note that this linked list is shared amongst siblings.
378 UsePosition* first_use_;
379
380 // The instruction type this interval corresponds to.
381 const Primitive::Type type_;
382
383 // Live interval that is the result of a split.
384 LiveInterval* next_sibling_;
385
386 // The register allocated to this interval.
387 int register_;
388
389 static constexpr int kNoRegister = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100390
391 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
392};
393
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100394class SsaLivenessAnalysis : public ValueObject {
395 public:
396 explicit SsaLivenessAnalysis(const HGraph& graph)
397 : graph_(graph),
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100398 linear_post_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100399 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100400 instructions_from_ssa_index_(graph.GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100401 number_of_ssa_values_(0) {
402 block_infos_.SetSize(graph.GetBlocks().Size());
403 }
404
405 void Analyze();
406
407 BitVector* GetLiveInSet(const HBasicBlock& block) const {
408 return &block_infos_.Get(block.GetBlockId())->live_in_;
409 }
410
411 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
412 return &block_infos_.Get(block.GetBlockId())->live_out_;
413 }
414
415 BitVector* GetKillSet(const HBasicBlock& block) const {
416 return &block_infos_.Get(block.GetBlockId())->kill_;
417 }
418
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100419 const GrowableArray<HBasicBlock*>& GetLinearPostOrder() const {
420 return linear_post_order_;
421 }
422
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100423 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100424 return instructions_from_ssa_index_.Get(index);
425 }
426
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100427 size_t GetNumberOfSsaValues() const {
428 return number_of_ssa_values_;
429 }
430
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100431 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100432 // Linearize the graph so that:
433 // (1): a block is always after its dominator,
434 // (2): blocks of loops are contiguous.
435 // This creates a natural and efficient ordering when visualizing live ranges.
436 void LinearizeGraph();
437
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100438 // Give an SSA number to each instruction that defines a value used by another instruction,
439 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100440 void NumberInstructions();
441
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100442 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
443 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100444
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100445 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
446 // kill sets, that do not take into account backward branches.
447 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100448
449 // After computing the initial sets, this method does a fixed point
450 // calculation over the live_in and live_out set to take into account
451 // backwards branches.
452 void ComputeLiveInAndLiveOutSets();
453
454 // Update the live_in set of the block and returns whether it has changed.
455 bool UpdateLiveIn(const HBasicBlock& block);
456
457 // Update the live_out set of the block and returns whether it has changed.
458 bool UpdateLiveOut(const HBasicBlock& block);
459
460 const HGraph& graph_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100461 GrowableArray<HBasicBlock*> linear_post_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100462 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100463 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100464 size_t number_of_ssa_values_;
465
466 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
467};
468
469} // namespace art
470
471#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_