blob: 33b1f1fc9ae5490d5fa605efff994db4c84c6a87 [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/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010051 * 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_;
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 const 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 Geoffray39468442014-09-02 15:17:15 +0100136 LiveInterval(ArenaAllocator* allocator,
137 Primitive::Type type,
138 HInstruction* defined_by = nullptr,
139 bool is_fixed = false,
140 int reg = kNoRegister,
141 bool is_temp = false)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100142 : allocator_(allocator),
143 first_range_(nullptr),
144 last_range_(nullptr),
145 first_use_(nullptr),
146 type_(type),
147 next_sibling_(nullptr),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100148 parent_(this),
Nicolas Geoffray39468442014-09-02 15:17:15 +0100149 register_(reg),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100150 spill_slot_(kNoSpillSlot),
Nicolas Geoffray39468442014-09-02 15:17:15 +0100151 is_fixed_(is_fixed),
152 is_temp_(is_temp),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100153 defined_by_(defined_by) {}
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100154
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100155 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100156 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
157 }
158
159 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator,
160 HInstruction* defined_by,
161 Primitive::Type type) {
162 return new (allocator) LiveInterval(allocator, type, defined_by, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100163 }
164
165 bool IsFixed() const { return is_fixed_; }
166
167 void AddUse(HInstruction* instruction, size_t input_index, bool is_environment) {
168 // Set the use within the instruction.
169 // TODO: Use the instruction's location to know whether the instruction can die
170 // at entry, or needs to say alive within the user.
171 size_t position = instruction->GetLifetimePosition() + 1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100172 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
173 size_t end_block_position = instruction->GetBlock()->GetLifetimeEnd();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100174 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100175 // First time we see a use of that interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100176 first_range_ = last_range_ = new (allocator_) LiveRange(start_block_position, position, nullptr);
177 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100178 // There is a use later in the same block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100179 DCHECK_LE(position, first_range_->GetEnd());
180 } else if (first_range_->GetStart() == end_block_position) {
181 // Last use is in the following block.
182 first_range_->start_ = start_block_position;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100183 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100184 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100185 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100186 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100187 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100188 first_use_ = new (allocator_) UsePosition(
189 instruction, input_index, is_environment, position, first_use_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100190 }
191
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100192 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100193 DCHECK(instruction->AsPhi() != nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100194 first_use_ = new (allocator_) UsePosition(
195 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100196 }
197
198 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100199 if (first_range_ == nullptr) {
200 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
201 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100202 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100203 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100204 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
205 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100206 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100207 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100208 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100209 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100210 }
211 }
212
213 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100214 DCHECK(first_range_ != nullptr);
215 while (first_range_ != nullptr && first_range_->GetEnd() < end) {
216 DCHECK_LE(start, first_range_->GetStart());
217 first_range_ = first_range_->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100218 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100219 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100220 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100221 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100222 } else {
223 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100224 first_range_->start_ = start;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100225 }
226 }
227
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100228 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100229 void SetSpillSlot(int slot) {
230 DCHECK(!is_fixed_);
231 DCHECK(!is_temp_);
232 spill_slot_ = slot;
233 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100234 int GetSpillSlot() const { return spill_slot_; }
235
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100236 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100237 if (first_range_ != nullptr) {
238 first_range_->start_ = from;
239 } else {
240 // Instruction without uses.
241 DCHECK(!defined_by_->HasUses());
242 DCHECK(from == defined_by_->GetLifetimePosition());
243 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
244 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100245 }
246
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100247 LiveInterval* GetParent() const { return parent_; }
248
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100249 LiveRange* GetFirstRange() const { return first_range_; }
250
251 int GetRegister() const { return register_; }
252 void SetRegister(int reg) { register_ = reg; }
253 void ClearRegister() { register_ = kNoRegister; }
254 bool HasRegister() const { return register_ != kNoRegister; }
255
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100256 bool IsDeadAt(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100257 return last_range_->GetEnd() <= position;
258 }
259
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100260 bool Covers(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100261 if (IsDeadAt(position)) {
262 return false;
263 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100264 LiveRange* current = first_range_;
265 while (current != nullptr) {
266 if (position >= current->GetStart() && position < current->GetEnd()) {
267 return true;
268 }
269 current = current->GetNext();
270 }
271 return false;
272 }
273
274 /**
275 * Returns the first intersection of this interval with `other`.
276 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100277 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100278 // Advance both intervals and find the first matching range start in
279 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100280 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100281 LiveRange* other_range = other->first_range_;
282 do {
283 if (my_range->IntersectsWith(*other_range)) {
284 return std::max(my_range->GetStart(), other_range->GetStart());
285 } else if (my_range->IsBefore(*other_range)) {
286 my_range = my_range->GetNext();
287 if (my_range == nullptr) {
288 return kNoLifetime;
289 }
290 } else {
291 DCHECK(other_range->IsBefore(*my_range));
292 other_range = other_range->GetNext();
293 if (other_range == nullptr) {
294 return kNoLifetime;
295 }
296 }
297 } while (true);
298 }
299
300 size_t GetStart() const {
301 return first_range_->GetStart();
302 }
303
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100304 size_t GetEnd() const {
305 return last_range_->GetEnd();
306 }
307
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100308 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100309 if (is_temp_) {
310 return position == GetStart() ? position : kNoLifetime;
311 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100312 if (position == GetStart() && defined_by_ != nullptr) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100313 LocationSummary* locations = defined_by_->GetLocations();
314 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100315 // This interval is the first interval of the instruction. If the output
316 // of the instruction requires a register, we return the position of that instruction
317 // as the first register use.
318 if (location.IsUnallocated()) {
319 if ((location.GetPolicy() == Location::kRequiresRegister)
320 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100321 && locations->InAt(0).GetPolicy() == Location::kRequiresRegister)) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100322 return position;
323 }
324 }
325 }
326
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100327 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100328 size_t end = GetEnd();
329 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100330 size_t use_position = use->GetPosition();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100331 if (use_position >= position && !use->GetIsEnvironment()) {
332 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
333 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
334 return use_position;
335 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100336 }
337 use = use->GetNext();
338 }
339 return kNoLifetime;
340 }
341
342 size_t FirstRegisterUse() const {
343 return FirstRegisterUseAfter(GetStart());
344 }
345
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100346 UsePosition* GetFirstUse() const {
347 return first_use_;
348 }
349
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100350 Primitive::Type GetType() const {
351 return type_;
352 }
353
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100354 HInstruction* GetDefinedBy() const {
355 return defined_by_;
356 }
357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100358 /**
359 * Split this interval at `position`. This interval is changed to:
360 * [start ... position).
361 *
362 * The new interval covers:
363 * [position ... end)
364 */
365 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100366 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100367 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100368 DCHECK_GT(position, GetStart());
369
370 if (last_range_->GetEnd() <= position) {
371 // This range dies before `position`, no need to split.
372 return nullptr;
373 }
374
375 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100376 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100377 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100378 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100379
380 new_interval->first_use_ = first_use_;
381 LiveRange* current = first_range_;
382 LiveRange* previous = nullptr;
383 // Iterate over the ranges, and either find a range that covers this position, or
384 // a two ranges in between this position (that is, the position is in a lifetime hole).
385 do {
386 if (position >= current->GetEnd()) {
387 // Move to next range.
388 previous = current;
389 current = current->next_;
390 } else if (position <= current->GetStart()) {
391 // If the previous range did not cover this position, we know position is in
392 // a lifetime hole. We can just break the first_range_ and last_range_ links
393 // and return the new interval.
394 DCHECK(previous != nullptr);
395 DCHECK(current != first_range_);
396 new_interval->last_range_ = last_range_;
397 last_range_ = previous;
398 previous->next_ = nullptr;
399 new_interval->first_range_ = current;
400 return new_interval;
401 } else {
402 // This range covers position. We create a new last_range_ for this interval
403 // that covers last_range_->Start() and position. We also shorten the current
404 // range and make it the first range of the new interval.
405 DCHECK(position < current->GetEnd() && position > current->GetStart());
406 new_interval->last_range_ = last_range_;
407 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
408 if (previous != nullptr) {
409 previous->next_ = last_range_;
410 } else {
411 first_range_ = last_range_;
412 }
413 new_interval->first_range_ = current;
414 current->start_ = position;
415 return new_interval;
416 }
417 } while (current != nullptr);
418
419 LOG(FATAL) << "Unreachable";
420 return nullptr;
421 }
422
423 bool StartsBefore(LiveInterval* other) const {
424 return GetStart() <= other->GetStart();
425 }
426
427 bool StartsAfter(LiveInterval* other) const {
428 return GetStart() >= other->GetStart();
429 }
430
431 void Dump(std::ostream& stream) const {
432 stream << "ranges: { ";
433 LiveRange* current = first_range_;
434 do {
435 current->Dump(stream);
436 stream << " ";
437 } while ((current = current->GetNext()) != nullptr);
438 stream << "}, uses: { ";
439 UsePosition* use = first_use_;
440 if (use != nullptr) {
441 do {
442 use->Dump(stream);
443 stream << " ";
444 } while ((use = use->GetNext()) != nullptr);
445 }
446 stream << "}";
447 }
448
449 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100450
451 private:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100452 ArenaAllocator* const allocator_;
453
454 // Ranges of this interval. We need a quick access to the last range to test
455 // for liveness (see `IsDeadAt`).
456 LiveRange* first_range_;
457 LiveRange* last_range_;
458
459 // Uses of this interval. Note that this linked list is shared amongst siblings.
460 UsePosition* first_use_;
461
462 // The instruction type this interval corresponds to.
463 const Primitive::Type type_;
464
465 // Live interval that is the result of a split.
466 LiveInterval* next_sibling_;
467
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100468 // The first interval from which split intervals come from.
469 LiveInterval* parent_;
470
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100471 // The register allocated to this interval.
472 int register_;
473
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100474 // The spill slot allocated to this interval.
475 int spill_slot_;
476
477 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100478 const bool is_fixed_;
479
480 // Whether the interval is for a temporary.
481 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100482
483 // The instruction represented by this interval.
484 HInstruction* const defined_by_;
485
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100486 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100487 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100488
489 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
490};
491
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100492class SsaLivenessAnalysis : public ValueObject {
493 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100494 SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100495 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100496 codegen_(codegen),
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100497 linear_post_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100498 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100499 instructions_from_ssa_index_(graph.GetArena(), 0),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100500 instructions_from_lifetime_position_(graph.GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100501 number_of_ssa_values_(0) {
502 block_infos_.SetSize(graph.GetBlocks().Size());
503 }
504
505 void Analyze();
506
507 BitVector* GetLiveInSet(const HBasicBlock& block) const {
508 return &block_infos_.Get(block.GetBlockId())->live_in_;
509 }
510
511 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
512 return &block_infos_.Get(block.GetBlockId())->live_out_;
513 }
514
515 BitVector* GetKillSet(const HBasicBlock& block) const {
516 return &block_infos_.Get(block.GetBlockId())->kill_;
517 }
518
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100519 const GrowableArray<HBasicBlock*>& GetLinearPostOrder() const {
520 return linear_post_order_;
521 }
522
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100523 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100524 return instructions_from_ssa_index_.Get(index);
525 }
526
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100527 HInstruction* GetInstructionFromPosition(size_t index) const {
528 return instructions_from_lifetime_position_.Get(index);
529 }
530
531 size_t GetMaxLifetimePosition() const {
532 return instructions_from_lifetime_position_.Size() * 2 - 1;
533 }
534
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100535 size_t GetNumberOfSsaValues() const {
536 return number_of_ssa_values_;
537 }
538
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100539 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100540 // Linearize the graph so that:
541 // (1): a block is always after its dominator,
542 // (2): blocks of loops are contiguous.
543 // This creates a natural and efficient ordering when visualizing live ranges.
544 void LinearizeGraph();
545
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100546 // Give an SSA number to each instruction that defines a value used by another instruction,
547 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100548 void NumberInstructions();
549
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100550 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
551 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100552
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100553 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
554 // kill sets, that do not take into account backward branches.
555 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100556
557 // After computing the initial sets, this method does a fixed point
558 // calculation over the live_in and live_out set to take into account
559 // backwards branches.
560 void ComputeLiveInAndLiveOutSets();
561
562 // Update the live_in set of the block and returns whether it has changed.
563 bool UpdateLiveIn(const HBasicBlock& block);
564
565 // Update the live_out set of the block and returns whether it has changed.
566 bool UpdateLiveOut(const HBasicBlock& block);
567
568 const HGraph& graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100569 CodeGenerator* const codegen_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100570 GrowableArray<HBasicBlock*> linear_post_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100571 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100572
573 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100574 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100575
576 // Temporary array used when inserting moves in the graph.
577 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100578 size_t number_of_ssa_values_;
579
580 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
581};
582
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100583class HLinearOrderIterator : public ValueObject {
584 public:
585 explicit HLinearOrderIterator(const SsaLivenessAnalysis& liveness)
586 : post_order_(liveness.GetLinearPostOrder()), index_(liveness.GetLinearPostOrder().Size()) {}
587
588 bool Done() const { return index_ == 0; }
589 HBasicBlock* Current() const { return post_order_.Get(index_ -1); }
590 void Advance() { --index_; DCHECK_GE(index_, 0U); }
591
592 private:
593 const GrowableArray<HBasicBlock*>& post_order_;
594 size_t index_;
595
596 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
597};
598
599class HLinearPostOrderIterator : public ValueObject {
600 public:
601 explicit HLinearPostOrderIterator(const SsaLivenessAnalysis& liveness)
602 : post_order_(liveness.GetLinearPostOrder()), index_(0) {}
603
604 bool Done() const { return index_ == post_order_.Size(); }
605 HBasicBlock* Current() const { return post_order_.Get(index_); }
606 void Advance() { ++index_; }
607
608 private:
609 const GrowableArray<HBasicBlock*>& post_order_;
610 size_t index_;
611
612 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
613};
614
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100615} // namespace art
616
617#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_