blob: 7903ad6cff7c2b39ad6be513602829c0fde488e5 [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
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010024class CodeGenerator;
25
Nicolas Geoffray804d0932014-05-02 08:46:00 +010026class BlockInfo : public ArenaObject {
27 public:
28 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
29 : block_(block),
30 live_in_(allocator, number_of_ssa_values, false),
31 live_out_(allocator, number_of_ssa_values, false),
32 kill_(allocator, number_of_ssa_values, false) {
33 live_in_.ClearAllBits();
34 live_out_.ClearAllBits();
35 kill_.ClearAllBits();
36 }
37
38 private:
39 const HBasicBlock& block_;
40 ArenaBitVector live_in_;
41 ArenaBitVector live_out_;
42 ArenaBitVector kill_;
43
44 friend class SsaLivenessAnalysis;
45
46 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
47};
48
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049/**
50 * A live range contains the start and end of a range where an instruction
51 * is live.
52 */
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010053class LiveRange : public ArenaObject {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010054 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010055 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010057 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058 }
59
60 size_t GetStart() const { return start_; }
61 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010062 LiveRange* GetNext() const { return next_; }
63
64 bool IntersectsWith(const LiveRange& other) {
65 return (start_ >= other.start_ && start_ < other.end_)
66 || (other.start_ >= start_ && other.start_ < end_);
67 }
68
69 bool IsBefore(const LiveRange& other) {
70 return end_ <= other.start_;
71 }
72
73 void Dump(std::ostream& stream) {
74 stream << "[" << start_ << ", " << end_ << ")";
75 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010076
77 private:
78 size_t start_;
79 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010080 LiveRange* next_;
81
82 friend class LiveInterval;
83
84 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010085};
86
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010087/**
88 * A use position represents a live interval use at a given position.
89 */
90class UsePosition : public ArenaObject {
91 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010092 UsePosition(HInstruction* user,
93 size_t input_index,
94 bool is_environment,
95 size_t position,
96 UsePosition* next)
97 : user_(user),
98 input_index_(input_index),
99 is_environment_(is_environment),
100 position_(position),
101 next_(next) {
102 DCHECK(user->AsPhi() != nullptr || GetPosition() == user->GetLifetimePosition() + 1);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100103 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
104 }
105
106 size_t GetPosition() const { return position_; }
107
108 UsePosition* GetNext() const { return next_; }
109
110 HInstruction* GetUser() const { return user_; }
111
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100112 bool GetIsEnvironment() const { return is_environment_; }
113
114 size_t GetInputIndex() const { return input_index_; }
115
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100116 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100117 stream << position_;
118 }
119
120 private:
121 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100122 const size_t input_index_;
123 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100124 const size_t position_;
125 UsePosition* const next_;
126
127 DISALLOW_COPY_AND_ASSIGN(UsePosition);
128};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100129
130/**
131 * An interval is a list of disjoint live ranges where an instruction is live.
132 * Each instruction that has uses gets an interval.
133 */
134class LiveInterval : public ArenaObject {
135 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100136 LiveInterval(ArenaAllocator* allocator, Primitive::Type type, HInstruction* defined_by = nullptr)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100137 : allocator_(allocator),
138 first_range_(nullptr),
139 last_range_(nullptr),
140 first_use_(nullptr),
141 type_(type),
142 next_sibling_(nullptr),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100143 parent_(this),
144 register_(kNoRegister),
145 spill_slot_(kNoSpillSlot),
146 is_fixed_(false),
147 defined_by_(defined_by) {}
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100148
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100149 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
150 LiveInterval* interval = new (allocator) LiveInterval(allocator, type);
151 interval->SetRegister(reg);
152 interval->is_fixed_ = true;
153 return interval;
154 }
155
156 bool IsFixed() const { return is_fixed_; }
157
158 void AddUse(HInstruction* instruction, size_t input_index, bool is_environment) {
159 // Set the use within the instruction.
160 // TODO: Use the instruction's location to know whether the instruction can die
161 // at entry, or needs to say alive within the user.
162 size_t position = instruction->GetLifetimePosition() + 1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100163 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
164 size_t end_block_position = instruction->GetBlock()->GetLifetimeEnd();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100165 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100166 // First time we see a use of that interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100167 first_range_ = last_range_ = new (allocator_) LiveRange(start_block_position, position, nullptr);
168 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100169 // There is a use later in the same block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100170 DCHECK_LE(position, first_range_->GetEnd());
171 } else if (first_range_->GetStart() == end_block_position) {
172 // Last use is in the following block.
173 first_range_->start_ = start_block_position;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100174 } else {
175 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100176 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100177 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100178 first_use_ = new (allocator_) UsePosition(
179 instruction, input_index, is_environment, position, first_use_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100180 }
181
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100182 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100183 DCHECK(instruction->AsPhi() != nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100184 first_use_ = new (allocator_) UsePosition(
185 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100186 }
187
188 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100189 if (first_range_ == nullptr) {
190 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
191 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100192 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100193 first_range_->start_ = start;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100194 } else {
195 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100196 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100197 }
198 }
199
200 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100201 DCHECK(first_range_ != nullptr);
202 while (first_range_ != nullptr && first_range_->GetEnd() < end) {
203 DCHECK_LE(start, first_range_->GetStart());
204 first_range_ = first_range_->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100205 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100206 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100207 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100208 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100209 } else {
210 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100211 first_range_->start_ = start;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100212 }
213 }
214
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100215 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
216 void SetSpillSlot(int slot) { spill_slot_ = slot; }
217 int GetSpillSlot() const { return spill_slot_; }
218
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100219 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100220 if (first_range_ != nullptr) {
221 first_range_->start_ = from;
222 } else {
223 // Instruction without uses.
224 DCHECK(!defined_by_->HasUses());
225 DCHECK(from == defined_by_->GetLifetimePosition());
226 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
227 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100228 }
229
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100230 LiveInterval* GetParent() const { return parent_; }
231
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100232 LiveRange* GetFirstRange() const { return first_range_; }
233
234 int GetRegister() const { return register_; }
235 void SetRegister(int reg) { register_ = reg; }
236 void ClearRegister() { register_ = kNoRegister; }
237 bool HasRegister() const { return register_ != kNoRegister; }
238
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100239 bool IsDeadAt(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100240 return last_range_->GetEnd() <= position;
241 }
242
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100243 bool Covers(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100244 LiveRange* current = first_range_;
245 while (current != nullptr) {
246 if (position >= current->GetStart() && position < current->GetEnd()) {
247 return true;
248 }
249 current = current->GetNext();
250 }
251 return false;
252 }
253
254 /**
255 * Returns the first intersection of this interval with `other`.
256 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100257 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100258 // Advance both intervals and find the first matching range start in
259 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100260 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100261 LiveRange* other_range = other->first_range_;
262 do {
263 if (my_range->IntersectsWith(*other_range)) {
264 return std::max(my_range->GetStart(), other_range->GetStart());
265 } else if (my_range->IsBefore(*other_range)) {
266 my_range = my_range->GetNext();
267 if (my_range == nullptr) {
268 return kNoLifetime;
269 }
270 } else {
271 DCHECK(other_range->IsBefore(*my_range));
272 other_range = other_range->GetNext();
273 if (other_range == nullptr) {
274 return kNoLifetime;
275 }
276 }
277 } while (true);
278 }
279
280 size_t GetStart() const {
281 return first_range_->GetStart();
282 }
283
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100284 size_t GetEnd() const {
285 return last_range_->GetEnd();
286 }
287
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100288 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100289 if (position == GetStart() && defined_by_ != nullptr) {
290 Location location = defined_by_->GetLocations()->Out();
291 // This interval is the first interval of the instruction. If the output
292 // of the instruction requires a register, we return the position of that instruction
293 // as the first register use.
294 if (location.IsUnallocated()) {
295 if ((location.GetPolicy() == Location::kRequiresRegister)
296 || (location.GetPolicy() == Location::kSameAsFirstInput
297 && defined_by_->GetLocations()->InAt(0).GetPolicy() == Location::kRequiresRegister)) {
298 return position;
299 }
300 }
301 }
302
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100303 UsePosition* use = first_use_;
304 while (use != nullptr) {
305 size_t use_position = use->GetPosition();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100306 if (use_position >= position && !use->GetIsEnvironment()) {
307 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
308 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
309 return use_position;
310 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100311 }
312 use = use->GetNext();
313 }
314 return kNoLifetime;
315 }
316
317 size_t FirstRegisterUse() const {
318 return FirstRegisterUseAfter(GetStart());
319 }
320
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100321 UsePosition* GetFirstUse() const {
322 return first_use_;
323 }
324
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100325 Primitive::Type GetType() const {
326 return type_;
327 }
328
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100329 HInstruction* GetDefinedBy() const {
330 return defined_by_;
331 }
332
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100333 /**
334 * Split this interval at `position`. This interval is changed to:
335 * [start ... position).
336 *
337 * The new interval covers:
338 * [position ... end)
339 */
340 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100341 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342 DCHECK_GT(position, GetStart());
343
344 if (last_range_->GetEnd() <= position) {
345 // This range dies before `position`, no need to split.
346 return nullptr;
347 }
348
349 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100350 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100351 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100352 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100353
354 new_interval->first_use_ = first_use_;
355 LiveRange* current = first_range_;
356 LiveRange* previous = nullptr;
357 // Iterate over the ranges, and either find a range that covers this position, or
358 // a two ranges in between this position (that is, the position is in a lifetime hole).
359 do {
360 if (position >= current->GetEnd()) {
361 // Move to next range.
362 previous = current;
363 current = current->next_;
364 } else if (position <= current->GetStart()) {
365 // If the previous range did not cover this position, we know position is in
366 // a lifetime hole. We can just break the first_range_ and last_range_ links
367 // and return the new interval.
368 DCHECK(previous != nullptr);
369 DCHECK(current != first_range_);
370 new_interval->last_range_ = last_range_;
371 last_range_ = previous;
372 previous->next_ = nullptr;
373 new_interval->first_range_ = current;
374 return new_interval;
375 } else {
376 // This range covers position. We create a new last_range_ for this interval
377 // that covers last_range_->Start() and position. We also shorten the current
378 // range and make it the first range of the new interval.
379 DCHECK(position < current->GetEnd() && position > current->GetStart());
380 new_interval->last_range_ = last_range_;
381 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
382 if (previous != nullptr) {
383 previous->next_ = last_range_;
384 } else {
385 first_range_ = last_range_;
386 }
387 new_interval->first_range_ = current;
388 current->start_ = position;
389 return new_interval;
390 }
391 } while (current != nullptr);
392
393 LOG(FATAL) << "Unreachable";
394 return nullptr;
395 }
396
397 bool StartsBefore(LiveInterval* other) const {
398 return GetStart() <= other->GetStart();
399 }
400
401 bool StartsAfter(LiveInterval* other) const {
402 return GetStart() >= other->GetStart();
403 }
404
405 void Dump(std::ostream& stream) const {
406 stream << "ranges: { ";
407 LiveRange* current = first_range_;
408 do {
409 current->Dump(stream);
410 stream << " ";
411 } while ((current = current->GetNext()) != nullptr);
412 stream << "}, uses: { ";
413 UsePosition* use = first_use_;
414 if (use != nullptr) {
415 do {
416 use->Dump(stream);
417 stream << " ";
418 } while ((use = use->GetNext()) != nullptr);
419 }
420 stream << "}";
421 }
422
423 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100424
425 private:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100426 ArenaAllocator* const allocator_;
427
428 // Ranges of this interval. We need a quick access to the last range to test
429 // for liveness (see `IsDeadAt`).
430 LiveRange* first_range_;
431 LiveRange* last_range_;
432
433 // Uses of this interval. Note that this linked list is shared amongst siblings.
434 UsePosition* first_use_;
435
436 // The instruction type this interval corresponds to.
437 const Primitive::Type type_;
438
439 // Live interval that is the result of a split.
440 LiveInterval* next_sibling_;
441
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100442 // The first interval from which split intervals come from.
443 LiveInterval* parent_;
444
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100445 // The register allocated to this interval.
446 int register_;
447
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100448 // The spill slot allocated to this interval.
449 int spill_slot_;
450
451 // Whether the interval is for a fixed register.
452 bool is_fixed_;
453
454 // The instruction represented by this interval.
455 HInstruction* const defined_by_;
456
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100457 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100458 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100459
460 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
461};
462
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100463class SsaLivenessAnalysis : public ValueObject {
464 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100465 SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100466 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100467 codegen_(codegen),
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100468 linear_post_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100469 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100470 instructions_from_ssa_index_(graph.GetArena(), 0),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100471 instructions_from_lifetime_position_(graph.GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100472 number_of_ssa_values_(0) {
473 block_infos_.SetSize(graph.GetBlocks().Size());
474 }
475
476 void Analyze();
477
478 BitVector* GetLiveInSet(const HBasicBlock& block) const {
479 return &block_infos_.Get(block.GetBlockId())->live_in_;
480 }
481
482 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
483 return &block_infos_.Get(block.GetBlockId())->live_out_;
484 }
485
486 BitVector* GetKillSet(const HBasicBlock& block) const {
487 return &block_infos_.Get(block.GetBlockId())->kill_;
488 }
489
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100490 const GrowableArray<HBasicBlock*>& GetLinearPostOrder() const {
491 return linear_post_order_;
492 }
493
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100494 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100495 return instructions_from_ssa_index_.Get(index);
496 }
497
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100498 HInstruction* GetInstructionFromPosition(size_t index) const {
499 return instructions_from_lifetime_position_.Get(index);
500 }
501
502 size_t GetMaxLifetimePosition() const {
503 return instructions_from_lifetime_position_.Size() * 2 - 1;
504 }
505
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100506 size_t GetNumberOfSsaValues() const {
507 return number_of_ssa_values_;
508 }
509
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100510 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100511 // Linearize the graph so that:
512 // (1): a block is always after its dominator,
513 // (2): blocks of loops are contiguous.
514 // This creates a natural and efficient ordering when visualizing live ranges.
515 void LinearizeGraph();
516
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100517 // Give an SSA number to each instruction that defines a value used by another instruction,
518 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100519 void NumberInstructions();
520
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100521 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
522 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100523
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100524 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
525 // kill sets, that do not take into account backward branches.
526 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100527
528 // After computing the initial sets, this method does a fixed point
529 // calculation over the live_in and live_out set to take into account
530 // backwards branches.
531 void ComputeLiveInAndLiveOutSets();
532
533 // Update the live_in set of the block and returns whether it has changed.
534 bool UpdateLiveIn(const HBasicBlock& block);
535
536 // Update the live_out set of the block and returns whether it has changed.
537 bool UpdateLiveOut(const HBasicBlock& block);
538
539 const HGraph& graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100540 CodeGenerator* const codegen_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100541 GrowableArray<HBasicBlock*> linear_post_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100542 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100543
544 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100545 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100546
547 // Temporary array used when inserting moves in the graph.
548 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100549 size_t number_of_ssa_values_;
550
551 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
552};
553
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100554class HLinearOrderIterator : public ValueObject {
555 public:
556 explicit HLinearOrderIterator(const SsaLivenessAnalysis& liveness)
557 : post_order_(liveness.GetLinearPostOrder()), index_(liveness.GetLinearPostOrder().Size()) {}
558
559 bool Done() const { return index_ == 0; }
560 HBasicBlock* Current() const { return post_order_.Get(index_ -1); }
561 void Advance() { --index_; DCHECK_GE(index_, 0U); }
562
563 private:
564 const GrowableArray<HBasicBlock*>& post_order_;
565 size_t index_;
566
567 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
568};
569
570class HLinearPostOrderIterator : public ValueObject {
571 public:
572 explicit HLinearPostOrderIterator(const SsaLivenessAnalysis& liveness)
573 : post_order_(liveness.GetLinearPostOrder()), index_(0) {}
574
575 bool Done() const { return index_ == post_order_.Size(); }
576 HBasicBlock* Current() const { return post_order_.Get(index_); }
577 void Advance() { ++index_; }
578
579 private:
580 const GrowableArray<HBasicBlock*>& post_order_;
581 size_t index_;
582
583 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
584};
585
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100586} // namespace art
587
588#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_