blob: 3ef24137cc839388b34f374576cecc37f598a0e9 [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 Geoffray76905622014-09-25 14:39:26 +010079 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) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100102 DCHECK(user->IsPhi()
103 || (GetPosition() == user->GetLifetimePosition() + 1)
104 || (GetPosition() == user->GetLifetimePosition()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100105 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
106 }
107
108 size_t GetPosition() const { return position_; }
109
110 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100111 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100112
113 HInstruction* GetUser() const { return user_; }
114
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100115 bool GetIsEnvironment() const { return is_environment_; }
116
117 size_t GetInputIndex() const { return input_index_; }
118
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100119 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100120 stream << position_;
121 }
122
123 private:
124 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100125 const size_t input_index_;
126 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100127 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100128 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100129
130 DISALLOW_COPY_AND_ASSIGN(UsePosition);
131};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100132
133/**
134 * An interval is a list of disjoint live ranges where an instruction is live.
135 * Each instruction that has uses gets an interval.
136 */
137class LiveInterval : public ArenaObject {
138 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100139 LiveInterval(ArenaAllocator* allocator,
140 Primitive::Type type,
141 HInstruction* defined_by = nullptr,
142 bool is_fixed = false,
143 int reg = kNoRegister,
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100144 bool is_temp = false,
145 bool is_slow_path_safepoint = false)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 : allocator_(allocator),
147 first_range_(nullptr),
148 last_range_(nullptr),
149 first_use_(nullptr),
150 type_(type),
151 next_sibling_(nullptr),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100152 parent_(this),
Nicolas Geoffray39468442014-09-02 15:17:15 +0100153 register_(reg),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100154 spill_slot_(kNoSpillSlot),
Nicolas Geoffray39468442014-09-02 15:17:15 +0100155 is_fixed_(is_fixed),
156 is_temp_(is_temp),
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100157 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100158 defined_by_(defined_by) {}
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100159
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100160 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
161 return new (allocator) LiveInterval(
162 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
163 }
164
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100165 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100166 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
167 }
168
169 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator,
170 HInstruction* defined_by,
171 Primitive::Type type) {
172 return new (allocator) LiveInterval(allocator, type, defined_by, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100173 }
174
175 bool IsFixed() const { return is_fixed_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100176 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100177
178 void AddUse(HInstruction* instruction, size_t input_index, bool is_environment) {
179 // Set the use within the instruction.
Nicolas Geoffray76905622014-09-25 14:39:26 +0100180 size_t position = instruction->GetLifetimePosition();
181 if (instruction->GetLocations()->InputOverlapsWithOutputOrTemp(input_index, is_environment)) {
182 // If it overlaps, we need to make sure the user will not try to allocate a temp
183 // or its output to the same register.
184 ++position;
185 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100186 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
187 size_t end_block_position = instruction->GetBlock()->GetLifetimeEnd();
Nicolas Geoffray76905622014-09-25 14:39:26 +0100188 if ((first_use_ != nullptr)
189 && (first_use_->GetUser() == instruction)
190 && (first_use_->GetPosition() < position)) {
191 // The user uses the instruction multiple times, and one use dies before the other.
192 // We update the use list so that the latter is first.
193 DCHECK(first_use_->GetPosition() + 1 == position);
194 UsePosition* new_use = new (allocator_) UsePosition(
195 instruction, input_index, is_environment, position, first_use_->GetNext());
196 first_use_->SetNext(new_use);
197 if (first_range_->GetEnd() == first_use_->GetPosition()) {
198 first_range_->end_ = position;
199 }
200 return;
201 }
202
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100203 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100204 // First time we see a use of that interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100205 first_range_ = last_range_ = new (allocator_) LiveRange(start_block_position, position, nullptr);
206 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100207 // There is a use later in the same block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100208 DCHECK_LE(position, first_range_->GetEnd());
209 } else if (first_range_->GetStart() == end_block_position) {
210 // Last use is in the following block.
211 first_range_->start_ = start_block_position;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100212 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100213 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100214 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100215 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100216 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100217 first_use_ = new (allocator_) UsePosition(
218 instruction, input_index, is_environment, position, first_use_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100219 }
220
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100221 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100222 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100223 first_use_ = new (allocator_) UsePosition(
224 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100225 }
226
227 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100228 if (first_range_ == nullptr) {
229 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
230 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100231 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100232 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100233 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
234 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100235 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100236 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100237 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100238 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100239 }
240 }
241
242 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100243 DCHECK(first_range_ != nullptr);
244 while (first_range_ != nullptr && first_range_->GetEnd() < end) {
245 DCHECK_LE(start, first_range_->GetStart());
246 first_range_ = first_range_->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100247 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100248 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100249 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100250 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100251 } else {
252 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100253 first_range_->start_ = start;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100254 }
255 }
256
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100257 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100258 void SetSpillSlot(int slot) {
259 DCHECK(!is_fixed_);
260 DCHECK(!is_temp_);
261 spill_slot_ = slot;
262 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100263 int GetSpillSlot() const { return spill_slot_; }
264
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100265 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100266 if (first_range_ != nullptr) {
267 first_range_->start_ = from;
268 } else {
269 // Instruction without uses.
270 DCHECK(!defined_by_->HasUses());
271 DCHECK(from == defined_by_->GetLifetimePosition());
272 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
273 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274 }
275
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100276 LiveInterval* GetParent() const { return parent_; }
277
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100278 LiveRange* GetFirstRange() const { return first_range_; }
279
280 int GetRegister() const { return register_; }
281 void SetRegister(int reg) { register_ = reg; }
282 void ClearRegister() { register_ = kNoRegister; }
283 bool HasRegister() const { return register_ != kNoRegister; }
284
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100285 bool IsDeadAt(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100286 return last_range_->GetEnd() <= position;
287 }
288
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100289 bool Covers(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100290 if (IsDeadAt(position)) {
291 return false;
292 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100293 LiveRange* current = first_range_;
294 while (current != nullptr) {
295 if (position >= current->GetStart() && position < current->GetEnd()) {
296 return true;
297 }
298 current = current->GetNext();
299 }
300 return false;
301 }
302
303 /**
304 * Returns the first intersection of this interval with `other`.
305 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100306 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100307 // Advance both intervals and find the first matching range start in
308 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100309 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100310 LiveRange* other_range = other->first_range_;
311 do {
312 if (my_range->IntersectsWith(*other_range)) {
313 return std::max(my_range->GetStart(), other_range->GetStart());
314 } else if (my_range->IsBefore(*other_range)) {
315 my_range = my_range->GetNext();
316 if (my_range == nullptr) {
317 return kNoLifetime;
318 }
319 } else {
320 DCHECK(other_range->IsBefore(*my_range));
321 other_range = other_range->GetNext();
322 if (other_range == nullptr) {
323 return kNoLifetime;
324 }
325 }
326 } while (true);
327 }
328
329 size_t GetStart() const {
330 return first_range_->GetStart();
331 }
332
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100333 size_t GetEnd() const {
334 return last_range_->GetEnd();
335 }
336
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100337 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100338 if (is_temp_) {
339 return position == GetStart() ? position : kNoLifetime;
340 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100341 if (position == GetStart() && defined_by_ != nullptr) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100342 LocationSummary* locations = defined_by_->GetLocations();
343 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100344 // This interval is the first interval of the instruction. If the output
345 // of the instruction requires a register, we return the position of that instruction
346 // as the first register use.
347 if (location.IsUnallocated()) {
348 if ((location.GetPolicy() == Location::kRequiresRegister)
349 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100350 && locations->InAt(0).GetPolicy() == Location::kRequiresRegister)) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100351 return position;
352 }
353 }
354 }
355
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100356 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100357 size_t end = GetEnd();
358 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100359 size_t use_position = use->GetPosition();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100360 if (use_position >= position && !use->GetIsEnvironment()) {
361 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
362 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100363 // Return the lifetime just before the user, so that the interval has a register
364 // when entering the user.
365 return use->GetUser()->GetLifetimePosition() - 1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100366 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100367 }
368 use = use->GetNext();
369 }
370 return kNoLifetime;
371 }
372
373 size_t FirstRegisterUse() const {
374 return FirstRegisterUseAfter(GetStart());
375 }
376
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100377 UsePosition* GetFirstUse() const {
378 return first_use_;
379 }
380
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100381 Primitive::Type GetType() const {
382 return type_;
383 }
384
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100385 HInstruction* GetDefinedBy() const {
386 return defined_by_;
387 }
388
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100389 /**
390 * Split this interval at `position`. This interval is changed to:
391 * [start ... position).
392 *
393 * The new interval covers:
394 * [position ... end)
395 */
396 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100397 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100398 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100399 DCHECK_GT(position, GetStart());
400
401 if (last_range_->GetEnd() <= position) {
402 // This range dies before `position`, no need to split.
403 return nullptr;
404 }
405
406 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100407 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100408 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100409 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100410
411 new_interval->first_use_ = first_use_;
412 LiveRange* current = first_range_;
413 LiveRange* previous = nullptr;
414 // Iterate over the ranges, and either find a range that covers this position, or
415 // a two ranges in between this position (that is, the position is in a lifetime hole).
416 do {
417 if (position >= current->GetEnd()) {
418 // Move to next range.
419 previous = current;
420 current = current->next_;
421 } else if (position <= current->GetStart()) {
422 // If the previous range did not cover this position, we know position is in
423 // a lifetime hole. We can just break the first_range_ and last_range_ links
424 // and return the new interval.
425 DCHECK(previous != nullptr);
426 DCHECK(current != first_range_);
427 new_interval->last_range_ = last_range_;
428 last_range_ = previous;
429 previous->next_ = nullptr;
430 new_interval->first_range_ = current;
431 return new_interval;
432 } else {
433 // This range covers position. We create a new last_range_ for this interval
434 // that covers last_range_->Start() and position. We also shorten the current
435 // range and make it the first range of the new interval.
436 DCHECK(position < current->GetEnd() && position > current->GetStart());
437 new_interval->last_range_ = last_range_;
438 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
439 if (previous != nullptr) {
440 previous->next_ = last_range_;
441 } else {
442 first_range_ = last_range_;
443 }
444 new_interval->first_range_ = current;
445 current->start_ = position;
446 return new_interval;
447 }
448 } while (current != nullptr);
449
450 LOG(FATAL) << "Unreachable";
451 return nullptr;
452 }
453
Nicolas Geoffray76905622014-09-25 14:39:26 +0100454 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100455 return GetStart() <= other->GetStart();
456 }
457
458 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100459 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100460 }
461
462 void Dump(std::ostream& stream) const {
463 stream << "ranges: { ";
464 LiveRange* current = first_range_;
465 do {
466 current->Dump(stream);
467 stream << " ";
468 } while ((current = current->GetNext()) != nullptr);
469 stream << "}, uses: { ";
470 UsePosition* use = first_use_;
471 if (use != nullptr) {
472 do {
473 use->Dump(stream);
474 stream << " ";
475 } while ((use = use->GetNext()) != nullptr);
476 }
477 stream << "}";
478 }
479
480 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100481
482 private:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100483 ArenaAllocator* const allocator_;
484
485 // Ranges of this interval. We need a quick access to the last range to test
486 // for liveness (see `IsDeadAt`).
487 LiveRange* first_range_;
488 LiveRange* last_range_;
489
490 // Uses of this interval. Note that this linked list is shared amongst siblings.
491 UsePosition* first_use_;
492
493 // The instruction type this interval corresponds to.
494 const Primitive::Type type_;
495
496 // Live interval that is the result of a split.
497 LiveInterval* next_sibling_;
498
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100499 // The first interval from which split intervals come from.
500 LiveInterval* parent_;
501
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100502 // The register allocated to this interval.
503 int register_;
504
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100505 // The spill slot allocated to this interval.
506 int spill_slot_;
507
508 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100509 const bool is_fixed_;
510
511 // Whether the interval is for a temporary.
512 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100513
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100514 // Whether the interval is for a safepoint that calls on slow path.
515 const bool is_slow_path_safepoint_;
516
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100517 // The instruction represented by this interval.
518 HInstruction* const defined_by_;
519
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100520 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100521 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100522
523 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
524};
525
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100526class SsaLivenessAnalysis : public ValueObject {
527 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100528 SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100529 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100530 codegen_(codegen),
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100531 linear_post_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100532 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100533 instructions_from_ssa_index_(graph.GetArena(), 0),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100534 instructions_from_lifetime_position_(graph.GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100535 number_of_ssa_values_(0) {
536 block_infos_.SetSize(graph.GetBlocks().Size());
537 }
538
539 void Analyze();
540
541 BitVector* GetLiveInSet(const HBasicBlock& block) const {
542 return &block_infos_.Get(block.GetBlockId())->live_in_;
543 }
544
545 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
546 return &block_infos_.Get(block.GetBlockId())->live_out_;
547 }
548
549 BitVector* GetKillSet(const HBasicBlock& block) const {
550 return &block_infos_.Get(block.GetBlockId())->kill_;
551 }
552
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100553 const GrowableArray<HBasicBlock*>& GetLinearPostOrder() const {
554 return linear_post_order_;
555 }
556
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100557 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100558 return instructions_from_ssa_index_.Get(index);
559 }
560
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100561 HInstruction* GetInstructionFromPosition(size_t index) const {
562 return instructions_from_lifetime_position_.Get(index);
563 }
564
565 size_t GetMaxLifetimePosition() const {
566 return instructions_from_lifetime_position_.Size() * 2 - 1;
567 }
568
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100569 size_t GetNumberOfSsaValues() const {
570 return number_of_ssa_values_;
571 }
572
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100573 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100574 // Linearize the graph so that:
575 // (1): a block is always after its dominator,
576 // (2): blocks of loops are contiguous.
577 // This creates a natural and efficient ordering when visualizing live ranges.
578 void LinearizeGraph();
579
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100580 // Give an SSA number to each instruction that defines a value used by another instruction,
581 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100582 void NumberInstructions();
583
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100584 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
585 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100586
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100587 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
588 // kill sets, that do not take into account backward branches.
589 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100590
591 // After computing the initial sets, this method does a fixed point
592 // calculation over the live_in and live_out set to take into account
593 // backwards branches.
594 void ComputeLiveInAndLiveOutSets();
595
596 // Update the live_in set of the block and returns whether it has changed.
597 bool UpdateLiveIn(const HBasicBlock& block);
598
599 // Update the live_out set of the block and returns whether it has changed.
600 bool UpdateLiveOut(const HBasicBlock& block);
601
602 const HGraph& graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100603 CodeGenerator* const codegen_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100604 GrowableArray<HBasicBlock*> linear_post_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100605 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100606
607 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100608 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100609
610 // Temporary array used when inserting moves in the graph.
611 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100612 size_t number_of_ssa_values_;
613
614 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
615};
616
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100617class HLinearOrderIterator : public ValueObject {
618 public:
619 explicit HLinearOrderIterator(const SsaLivenessAnalysis& liveness)
620 : post_order_(liveness.GetLinearPostOrder()), index_(liveness.GetLinearPostOrder().Size()) {}
621
622 bool Done() const { return index_ == 0; }
623 HBasicBlock* Current() const { return post_order_.Get(index_ -1); }
624 void Advance() { --index_; DCHECK_GE(index_, 0U); }
625
626 private:
627 const GrowableArray<HBasicBlock*>& post_order_;
628 size_t index_;
629
630 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
631};
632
633class HLinearPostOrderIterator : public ValueObject {
634 public:
635 explicit HLinearPostOrderIterator(const SsaLivenessAnalysis& liveness)
636 : post_order_(liveness.GetLinearPostOrder()), index_(0) {}
637
638 bool Done() const { return index_ == post_order_.Size(); }
639 HBasicBlock* Current() const { return post_order_.Get(index_); }
640 void Advance() { ++index_; }
641
642 private:
643 const GrowableArray<HBasicBlock*>& post_order_;
644 size_t index_;
645
646 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
647};
648
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100649} // namespace art
650
651#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_