blob: 8ce5ce902ea01fa4e18de114be7a54da3212fb6c [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 Geoffray01ef3452014-10-01 11:32:17 +010026static constexpr int kNoRegister = -1;
27
Nicolas Geoffray804d0932014-05-02 08:46:00 +010028class BlockInfo : public ArenaObject {
29 public:
30 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
31 : block_(block),
32 live_in_(allocator, number_of_ssa_values, false),
33 live_out_(allocator, number_of_ssa_values, false),
34 kill_(allocator, number_of_ssa_values, false) {
35 live_in_.ClearAllBits();
36 live_out_.ClearAllBits();
37 kill_.ClearAllBits();
38 }
39
40 private:
41 const HBasicBlock& block_;
42 ArenaBitVector live_in_;
43 ArenaBitVector live_out_;
44 ArenaBitVector kill_;
45
46 friend class SsaLivenessAnalysis;
47
48 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
49};
50
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010051/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010052 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010053 * is live.
54 */
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010055class LiveRange : public ArenaObject {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010057 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010059 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060 }
61
62 size_t GetStart() const { return start_; }
63 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010064 LiveRange* GetNext() const { return next_; }
65
66 bool IntersectsWith(const LiveRange& other) {
67 return (start_ >= other.start_ && start_ < other.end_)
68 || (other.start_ >= start_ && other.start_ < end_);
69 }
70
71 bool IsBefore(const LiveRange& other) {
72 return end_ <= other.start_;
73 }
74
75 void Dump(std::ostream& stream) {
76 stream << "[" << start_ << ", " << end_ << ")";
77 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010078
79 private:
80 size_t start_;
Nicolas Geoffray76905622014-09-25 14:39:26 +010081 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010082 LiveRange* next_;
83
84 friend class LiveInterval;
85
86 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010087};
88
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010089/**
90 * A use position represents a live interval use at a given position.
91 */
92class UsePosition : public ArenaObject {
93 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010094 UsePosition(HInstruction* user,
95 size_t input_index,
96 bool is_environment,
97 size_t position,
98 UsePosition* next)
99 : user_(user),
100 input_index_(input_index),
101 is_environment_(is_environment),
102 position_(position),
103 next_(next) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100104 DCHECK(user->IsPhi()
105 || (GetPosition() == user->GetLifetimePosition() + 1)
106 || (GetPosition() == user->GetLifetimePosition()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100107 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
108 }
109
110 size_t GetPosition() const { return position_; }
111
112 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100113 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100114
115 HInstruction* GetUser() const { return user_; }
116
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100117 bool GetIsEnvironment() const { return is_environment_; }
118
119 size_t GetInputIndex() const { return input_index_; }
120
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100121 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100122 stream << position_;
123 }
124
125 private:
126 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100127 const size_t input_index_;
128 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100129 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100130 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100131
132 DISALLOW_COPY_AND_ASSIGN(UsePosition);
133};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100134
135/**
136 * An interval is a list of disjoint live ranges where an instruction is live.
137 * Each instruction that has uses gets an interval.
138 */
139class LiveInterval : public ArenaObject {
140 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100141 LiveInterval(ArenaAllocator* allocator,
142 Primitive::Type type,
143 HInstruction* defined_by = nullptr,
144 bool is_fixed = false,
145 int reg = kNoRegister,
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100146 bool is_temp = false,
147 bool is_slow_path_safepoint = false)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100148 : allocator_(allocator),
149 first_range_(nullptr),
150 last_range_(nullptr),
151 first_use_(nullptr),
152 type_(type),
153 next_sibling_(nullptr),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100154 parent_(this),
Nicolas Geoffray39468442014-09-02 15:17:15 +0100155 register_(reg),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100156 spill_slot_(kNoSpillSlot),
Nicolas Geoffray39468442014-09-02 15:17:15 +0100157 is_fixed_(is_fixed),
158 is_temp_(is_temp),
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100159 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100160 defined_by_(defined_by) {}
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100161
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100162 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
163 return new (allocator) LiveInterval(
164 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
165 }
166
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100167 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100168 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
169 }
170
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100171 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
172 return new (allocator) LiveInterval(allocator, type, nullptr, 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 Geoffray76905622014-09-25 14:39:26 +0100186 if ((first_use_ != nullptr)
187 && (first_use_->GetUser() == instruction)
188 && (first_use_->GetPosition() < position)) {
189 // The user uses the instruction multiple times, and one use dies before the other.
190 // We update the use list so that the latter is first.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100191 UsePosition* cursor = first_use_;
192 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
193 cursor = cursor->GetNext();
194 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100195 DCHECK(first_use_->GetPosition() + 1 == position);
196 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100197 instruction, input_index, is_environment, position, cursor->GetNext());
198 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100199 if (first_range_->GetEnd() == first_use_->GetPosition()) {
200 first_range_->end_ = position;
201 }
202 return;
203 }
204
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100205 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100206 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100207 // First time we see a use of that interval.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100208 first_range_ = last_range_ = new (allocator_) LiveRange(
209 start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100210 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100211 // There is a use later in the same block or in a following block.
212 // Note that in such a case, `AddRange` for the whole blocks has been called
213 // before arriving in this method, and this is the reason the start of
214 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100215 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100216 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100217 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100218 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100219 // Note that the start of `first_range_` can be equal to `end`: two blocks
220 // having adjacent lifetime positions are not necessarily
221 // predecessor/successor. When two blocks are predecessor/successor, the
222 // liveness algorithm has called `AddRange` before arriving in this method,
223 // and the check line 205 would succeed.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100224 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100225 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100226 first_use_ = new (allocator_) UsePosition(
227 instruction, input_index, is_environment, position, first_use_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100228 }
229
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100230 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100231 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100232 first_use_ = new (allocator_) UsePosition(
233 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100234 }
235
236 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100237 if (first_range_ == nullptr) {
238 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
239 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100240 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100241 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100242 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
243 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100244 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100245 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100246 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100247 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100248 }
249 }
250
251 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100252 DCHECK(first_range_ != nullptr);
253 while (first_range_ != nullptr && first_range_->GetEnd() < end) {
254 DCHECK_LE(start, first_range_->GetStart());
255 first_range_ = first_range_->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100256 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100257 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100258 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100259 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100260 } else {
261 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100262 first_range_->start_ = start;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100263 }
264 }
265
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100266 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100267 void SetSpillSlot(int slot) {
268 DCHECK(!is_fixed_);
269 DCHECK(!is_temp_);
270 spill_slot_ = slot;
271 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100272 int GetSpillSlot() const { return spill_slot_; }
273
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100275 if (first_range_ != nullptr) {
276 first_range_->start_ = from;
277 } else {
278 // Instruction without uses.
279 DCHECK(!defined_by_->HasUses());
280 DCHECK(from == defined_by_->GetLifetimePosition());
281 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
282 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100283 }
284
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100285 LiveInterval* GetParent() const { return parent_; }
286
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100287 LiveRange* GetFirstRange() const { return first_range_; }
288
289 int GetRegister() const { return register_; }
290 void SetRegister(int reg) { register_ = reg; }
291 void ClearRegister() { register_ = kNoRegister; }
292 bool HasRegister() const { return register_ != kNoRegister; }
293
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100294 bool IsDeadAt(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100295 return last_range_->GetEnd() <= position;
296 }
297
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100298 bool Covers(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100299 if (IsDeadAt(position)) {
300 return false;
301 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100302 LiveRange* current = first_range_;
303 while (current != nullptr) {
304 if (position >= current->GetStart() && position < current->GetEnd()) {
305 return true;
306 }
307 current = current->GetNext();
308 }
309 return false;
310 }
311
312 /**
313 * Returns the first intersection of this interval with `other`.
314 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100315 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100316 // Advance both intervals and find the first matching range start in
317 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100318 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100319 LiveRange* other_range = other->first_range_;
320 do {
321 if (my_range->IntersectsWith(*other_range)) {
322 return std::max(my_range->GetStart(), other_range->GetStart());
323 } else if (my_range->IsBefore(*other_range)) {
324 my_range = my_range->GetNext();
325 if (my_range == nullptr) {
326 return kNoLifetime;
327 }
328 } else {
329 DCHECK(other_range->IsBefore(*my_range));
330 other_range = other_range->GetNext();
331 if (other_range == nullptr) {
332 return kNoLifetime;
333 }
334 }
335 } while (true);
336 }
337
338 size_t GetStart() const {
339 return first_range_->GetStart();
340 }
341
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100342 size_t GetEnd() const {
343 return last_range_->GetEnd();
344 }
345
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100346 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100347 if (is_temp_) {
348 return position == GetStart() ? position : kNoLifetime;
349 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100350 if (position == GetStart() && defined_by_ != nullptr) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100351 LocationSummary* locations = defined_by_->GetLocations();
352 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100353 // This interval is the first interval of the instruction. If the output
354 // of the instruction requires a register, we return the position of that instruction
355 // as the first register use.
356 if (location.IsUnallocated()) {
357 if ((location.GetPolicy() == Location::kRequiresRegister)
358 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100359 && locations->InAt(0).GetPolicy() == Location::kRequiresRegister)) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100360 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100361 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
362 || (location.GetPolicy() == Location::kSameAsFirstInput
363 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
364 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100365 }
366 }
367 }
368
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100369 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100370 size_t end = GetEnd();
371 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100372 size_t use_position = use->GetPosition();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100373 if (use_position >= position && !use->GetIsEnvironment()) {
374 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100375 if (location.IsUnallocated()
376 && (location.GetPolicy() == Location::kRequiresRegister
377 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100378 // Return the lifetime just before the user, so that the interval has a register
379 // when entering the user.
380 return use->GetUser()->GetLifetimePosition() - 1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100381 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100382 }
383 use = use->GetNext();
384 }
385 return kNoLifetime;
386 }
387
388 size_t FirstRegisterUse() const {
389 return FirstRegisterUseAfter(GetStart());
390 }
391
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100392 UsePosition* GetFirstUse() const {
393 return first_use_;
394 }
395
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100396 Primitive::Type GetType() const {
397 return type_;
398 }
399
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100400 HInstruction* GetDefinedBy() const {
401 return defined_by_;
402 }
403
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100404 /**
405 * Split this interval at `position`. This interval is changed to:
406 * [start ... position).
407 *
408 * The new interval covers:
409 * [position ... end)
410 */
411 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100412 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100413 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100414 DCHECK_GT(position, GetStart());
415
416 if (last_range_->GetEnd() <= position) {
417 // This range dies before `position`, no need to split.
418 return nullptr;
419 }
420
421 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100422 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100423 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100424 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100425
426 new_interval->first_use_ = first_use_;
427 LiveRange* current = first_range_;
428 LiveRange* previous = nullptr;
429 // Iterate over the ranges, and either find a range that covers this position, or
430 // a two ranges in between this position (that is, the position is in a lifetime hole).
431 do {
432 if (position >= current->GetEnd()) {
433 // Move to next range.
434 previous = current;
435 current = current->next_;
436 } else if (position <= current->GetStart()) {
437 // If the previous range did not cover this position, we know position is in
438 // a lifetime hole. We can just break the first_range_ and last_range_ links
439 // and return the new interval.
440 DCHECK(previous != nullptr);
441 DCHECK(current != first_range_);
442 new_interval->last_range_ = last_range_;
443 last_range_ = previous;
444 previous->next_ = nullptr;
445 new_interval->first_range_ = current;
446 return new_interval;
447 } else {
448 // This range covers position. We create a new last_range_ for this interval
449 // that covers last_range_->Start() and position. We also shorten the current
450 // range and make it the first range of the new interval.
451 DCHECK(position < current->GetEnd() && position > current->GetStart());
452 new_interval->last_range_ = last_range_;
453 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
454 if (previous != nullptr) {
455 previous->next_ = last_range_;
456 } else {
457 first_range_ = last_range_;
458 }
459 new_interval->first_range_ = current;
460 current->start_ = position;
461 return new_interval;
462 }
463 } while (current != nullptr);
464
465 LOG(FATAL) << "Unreachable";
466 return nullptr;
467 }
468
Nicolas Geoffray76905622014-09-25 14:39:26 +0100469 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100470 return GetStart() <= other->GetStart();
471 }
472
473 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100474 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100475 }
476
477 void Dump(std::ostream& stream) const {
478 stream << "ranges: { ";
479 LiveRange* current = first_range_;
480 do {
481 current->Dump(stream);
482 stream << " ";
483 } while ((current = current->GetNext()) != nullptr);
484 stream << "}, uses: { ";
485 UsePosition* use = first_use_;
486 if (use != nullptr) {
487 do {
488 use->Dump(stream);
489 stream << " ";
490 } while ((use = use->GetNext()) != nullptr);
491 }
492 stream << "}";
493 }
494
495 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100496
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100497 // Returns the first register hint that is at least free before
498 // the value contained in `free_until`. If none is found, returns
499 // `kNoRegister`.
500 int FindFirstRegisterHint(size_t* free_until) const;
501
502 // If there is enough at the definition site to find a register (for example
503 // it uses the same input as the first input), returns the register as a hint.
504 // Returns kNoRegister otherwise.
505 int FindHintAtDefinition() const;
506
507 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
508 // slots for spilling.
509 bool NeedsTwoSpillSlots() const;
510
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100511 bool IsFloatingPoint() const {
512 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
513 }
514
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100515 // Converts the location of the interval to a `Location` object.
516 Location ToLocation() const;
517
518 // Returns the location of the interval following its siblings at `position`.
519 Location GetLocationAt(size_t position) const;
520
521 // Finds the interval that covers `position`.
522 const LiveInterval& GetIntervalAt(size_t position) const;
523
524 bool IsTemp() const { return is_temp_; }
525
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100526 // Returns whether `other` and `this` share the same kind of register.
527 bool SameRegisterKind(Location other) const;
528
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100529 private:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100530 ArenaAllocator* const allocator_;
531
532 // Ranges of this interval. We need a quick access to the last range to test
533 // for liveness (see `IsDeadAt`).
534 LiveRange* first_range_;
535 LiveRange* last_range_;
536
537 // Uses of this interval. Note that this linked list is shared amongst siblings.
538 UsePosition* first_use_;
539
540 // The instruction type this interval corresponds to.
541 const Primitive::Type type_;
542
543 // Live interval that is the result of a split.
544 LiveInterval* next_sibling_;
545
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100546 // The first interval from which split intervals come from.
547 LiveInterval* parent_;
548
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100549 // The register allocated to this interval.
550 int register_;
551
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100552 // The spill slot allocated to this interval.
553 int spill_slot_;
554
555 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100556 const bool is_fixed_;
557
558 // Whether the interval is for a temporary.
559 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100560
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100561 // Whether the interval is for a safepoint that calls on slow path.
562 const bool is_slow_path_safepoint_;
563
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100564 // The instruction represented by this interval.
565 HInstruction* const defined_by_;
566
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100567 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100568 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100569
570 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
571};
572
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100573class SsaLivenessAnalysis : public ValueObject {
574 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100575 SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100576 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100577 codegen_(codegen),
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100578 linear_post_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100579 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100580 instructions_from_ssa_index_(graph.GetArena(), 0),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100581 instructions_from_lifetime_position_(graph.GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100582 number_of_ssa_values_(0) {
583 block_infos_.SetSize(graph.GetBlocks().Size());
584 }
585
586 void Analyze();
587
588 BitVector* GetLiveInSet(const HBasicBlock& block) const {
589 return &block_infos_.Get(block.GetBlockId())->live_in_;
590 }
591
592 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
593 return &block_infos_.Get(block.GetBlockId())->live_out_;
594 }
595
596 BitVector* GetKillSet(const HBasicBlock& block) const {
597 return &block_infos_.Get(block.GetBlockId())->kill_;
598 }
599
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100600 const GrowableArray<HBasicBlock*>& GetLinearPostOrder() const {
601 return linear_post_order_;
602 }
603
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100604 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100605 return instructions_from_ssa_index_.Get(index);
606 }
607
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100608 HInstruction* GetInstructionFromPosition(size_t index) const {
609 return instructions_from_lifetime_position_.Get(index);
610 }
611
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100612 HInstruction* GetTempUser(LiveInterval* temp) const {
613 // A temporary shares the same lifetime start as the instruction that requires it.
614 DCHECK(temp->IsTemp());
615 return GetInstructionFromPosition(temp->GetStart() / 2);
616 }
617
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100618 size_t GetMaxLifetimePosition() const {
619 return instructions_from_lifetime_position_.Size() * 2 - 1;
620 }
621
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100622 size_t GetNumberOfSsaValues() const {
623 return number_of_ssa_values_;
624 }
625
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100626 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100627 // Linearize the graph so that:
628 // (1): a block is always after its dominator,
629 // (2): blocks of loops are contiguous.
630 // This creates a natural and efficient ordering when visualizing live ranges.
631 void LinearizeGraph();
632
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100633 // Give an SSA number to each instruction that defines a value used by another instruction,
634 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100635 void NumberInstructions();
636
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100637 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
638 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100639
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100640 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
641 // kill sets, that do not take into account backward branches.
642 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100643
644 // After computing the initial sets, this method does a fixed point
645 // calculation over the live_in and live_out set to take into account
646 // backwards branches.
647 void ComputeLiveInAndLiveOutSets();
648
649 // Update the live_in set of the block and returns whether it has changed.
650 bool UpdateLiveIn(const HBasicBlock& block);
651
652 // Update the live_out set of the block and returns whether it has changed.
653 bool UpdateLiveOut(const HBasicBlock& block);
654
655 const HGraph& graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100656 CodeGenerator* const codegen_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100657 GrowableArray<HBasicBlock*> linear_post_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100658 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100659
660 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100661 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100662
663 // Temporary array used when inserting moves in the graph.
664 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100665 size_t number_of_ssa_values_;
666
667 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
668};
669
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100670class HLinearOrderIterator : public ValueObject {
671 public:
672 explicit HLinearOrderIterator(const SsaLivenessAnalysis& liveness)
673 : post_order_(liveness.GetLinearPostOrder()), index_(liveness.GetLinearPostOrder().Size()) {}
674
675 bool Done() const { return index_ == 0; }
676 HBasicBlock* Current() const { return post_order_.Get(index_ -1); }
677 void Advance() { --index_; DCHECK_GE(index_, 0U); }
678
679 private:
680 const GrowableArray<HBasicBlock*>& post_order_;
681 size_t index_;
682
683 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
684};
685
686class HLinearPostOrderIterator : public ValueObject {
687 public:
688 explicit HLinearPostOrderIterator(const SsaLivenessAnalysis& liveness)
689 : post_order_(liveness.GetLinearPostOrder()), index_(0) {}
690
691 bool Done() const { return index_ == post_order_.Size(); }
692 HBasicBlock* Current() const { return post_order_.Get(index_); }
693 void Advance() { ++index_; }
694
695 private:
696 const GrowableArray<HBasicBlock*>& post_order_;
697 size_t index_;
698
699 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
700};
701
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100702} // namespace art
703
704#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_