blob: beb4907bc1a7cf51faf0df84281422fa36efc25b [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"
Nicolas Geoffray829280c2015-01-28 10:20:37 +000021#include <iostream>
Nicolas Geoffray804d0932014-05-02 08:46:00 +010022
23namespace art {
24
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010025class CodeGenerator;
26
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010027static constexpr int kNoRegister = -1;
28
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070029class BlockInfo : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010030 public:
31 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
32 : block_(block),
33 live_in_(allocator, number_of_ssa_values, false),
34 live_out_(allocator, number_of_ssa_values, false),
35 kill_(allocator, number_of_ssa_values, false) {
Ian Rogerscf7f1912014-10-22 22:06:39 -070036 UNUSED(block_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010037 live_in_.ClearAllBits();
38 live_out_.ClearAllBits();
39 kill_.ClearAllBits();
40 }
41
42 private:
43 const HBasicBlock& block_;
44 ArenaBitVector live_in_;
45 ArenaBitVector live_out_;
46 ArenaBitVector kill_;
47
48 friend class SsaLivenessAnalysis;
49
50 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
51};
52
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010053/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010055 * is live.
56 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070057class LiveRange FINAL : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010059 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010061 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010062 }
63
64 size_t GetStart() const { return start_; }
65 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010066 LiveRange* GetNext() const { return next_; }
67
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070068 bool IntersectsWith(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010069 return (start_ >= other.start_ && start_ < other.end_)
70 || (other.start_ >= start_ && other.start_ < end_);
71 }
72
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070073 bool IsBefore(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010074 return end_ <= other.start_;
75 }
76
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070077 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010078 stream << "[" << start_ << ", " << end_ << ")";
79 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010080
Nicolas Geoffray840e5462015-01-07 16:01:24 +000081 LiveRange* Dup(ArenaAllocator* allocator) const {
82 return new (allocator) LiveRange(
83 start_, end_, next_ == nullptr ? nullptr : next_->Dup(allocator));
84 }
85
86 LiveRange* GetLastRange() {
87 return next_ == nullptr ? this : next_->GetLastRange();
88 }
89
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010090 private:
91 size_t start_;
Nicolas Geoffray76905622014-09-25 14:39:26 +010092 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010093 LiveRange* next_;
94
95 friend class LiveInterval;
96
97 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010098};
99
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100100/**
101 * A use position represents a live interval use at a given position.
102 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700103class UsePosition : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100104 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100105 UsePosition(HInstruction* user,
106 size_t input_index,
107 bool is_environment,
108 size_t position,
109 UsePosition* next)
110 : user_(user),
111 input_index_(input_index),
112 is_environment_(is_environment),
113 position_(position),
114 next_(next) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100115 DCHECK(user->IsPhi()
116 || (GetPosition() == user->GetLifetimePosition() + 1)
117 || (GetPosition() == user->GetLifetimePosition()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100118 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
119 }
120
121 size_t GetPosition() const { return position_; }
122
123 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100124 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100125
126 HInstruction* GetUser() const { return user_; }
127
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100128 bool GetIsEnvironment() const { return is_environment_; }
129
130 size_t GetInputIndex() const { return input_index_; }
131
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100132 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100133 stream << position_;
134 }
135
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000136 UsePosition* Dup(ArenaAllocator* allocator) const {
137 return new (allocator) UsePosition(
138 user_, input_index_, is_environment_, position_,
139 next_ == nullptr ? nullptr : next_->Dup(allocator));
140 }
141
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100142 private:
143 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100144 const size_t input_index_;
145 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100147 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100148
149 DISALLOW_COPY_AND_ASSIGN(UsePosition);
150};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100151
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100152class SafepointPosition : public ArenaObject<kArenaAllocMisc> {
153 public:
154 explicit SafepointPosition(HInstruction* instruction)
155 : instruction_(instruction),
156 next_(nullptr) {}
157
158 void SetNext(SafepointPosition* next) {
159 next_ = next;
160 }
161
162 size_t GetPosition() const {
163 return instruction_->GetLifetimePosition();
164 }
165
166 SafepointPosition* GetNext() const {
167 return next_;
168 }
169
170 LocationSummary* GetLocations() const {
171 return instruction_->GetLocations();
172 }
173
174 HInstruction* GetInstruction() const {
175 return instruction_;
176 }
177
178 private:
179 HInstruction* const instruction_;
180 SafepointPosition* next_;
181
182 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
183};
184
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100185/**
186 * An interval is a list of disjoint live ranges where an instruction is live.
187 * Each instruction that has uses gets an interval.
188 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700189class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100190 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700191 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
192 Primitive::Type type,
193 HInstruction* instruction = nullptr) {
194 return new (allocator) LiveInterval(allocator, type, instruction);
195 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100196
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100197 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
198 return new (allocator) LiveInterval(
199 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
200 }
201
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100202 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100203 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
204 }
205
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100206 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
207 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100208 }
209
210 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700211 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100212 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700213 // This interval is the result of a split.
214 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100215
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000216 void AddTempUse(HInstruction* instruction, size_t temp_index) {
217 DCHECK(IsTemp());
218 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
219 size_t position = instruction->GetLifetimePosition();
220 first_use_ = new (allocator_) UsePosition(
221 instruction, temp_index, /* is_environment */ false, position, first_use_);
222 AddRange(position, position + 1);
223 }
224
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000225 void AddUse(HInstruction* instruction,
226 size_t input_index,
227 bool is_environment,
228 bool keep_alive = false) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100229 // Set the use within the instruction.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000230 size_t position = instruction->GetLifetimePosition() + 1;
231 LocationSummary* locations = instruction->GetLocations();
232 if (!is_environment) {
233 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
234 // For fixed inputs and output same as input, the register allocator
235 // requires to have inputs die at the instruction, so that input moves use the
236 // location of the input just before that instruction (and not potential moves due
237 // to splitting).
238 position = instruction->GetLifetimePosition();
239 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100240 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000241
242 DCHECK(position == instruction->GetLifetimePosition()
243 || position == instruction->GetLifetimePosition() + 1);
244
Nicolas Geoffray76905622014-09-25 14:39:26 +0100245 if ((first_use_ != nullptr)
246 && (first_use_->GetUser() == instruction)
247 && (first_use_->GetPosition() < position)) {
248 // The user uses the instruction multiple times, and one use dies before the other.
249 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000250 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100251 UsePosition* cursor = first_use_;
252 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
253 cursor = cursor->GetNext();
254 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100255 DCHECK(first_use_->GetPosition() + 1 == position);
256 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100257 instruction, input_index, is_environment, position, cursor->GetNext());
258 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100259 if (first_range_->GetEnd() == first_use_->GetPosition()) {
260 first_range_->end_ = position;
261 }
262 return;
263 }
264
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000265 first_use_ = new (allocator_) UsePosition(
266 instruction, input_index, is_environment, position, first_use_);
267
268 if (is_environment && !keep_alive) {
269 // If this environment use does not keep the instruction live, it does not
270 // affect the live range of that instruction.
271 return;
272 }
273
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100274 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100275 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276 // First time we see a use of that interval.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100277 first_range_ = last_range_ = new (allocator_) LiveRange(
278 start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100279 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100280 // There is a use later in the same block or in a following block.
281 // Note that in such a case, `AddRange` for the whole blocks has been called
282 // before arriving in this method, and this is the reason the start of
283 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100284 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100285 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100286 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100287 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100288 // Note that the start of `first_range_` can be equal to `end`: two blocks
289 // having adjacent lifetime positions are not necessarily
290 // predecessor/successor. When two blocks are predecessor/successor, the
291 // liveness algorithm has called `AddRange` before arriving in this method,
292 // and the check line 205 would succeed.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100293 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100294 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100295 }
296
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100297 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100298 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100299 first_use_ = new (allocator_) UsePosition(
300 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100301 }
302
303 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100304 if (first_range_ == nullptr) {
305 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
306 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100307 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100308 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100309 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
310 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100311 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100312 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100313 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100314 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100315 }
316 }
317
318 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100319 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000320 DCHECK_LE(start, first_range_->GetStart());
321 // Find the range that covers the positions after the loop.
322 LiveRange* after_loop = first_range_;
323 LiveRange* last_in_loop = nullptr;
324 while (after_loop != nullptr && after_loop->GetEnd() < end) {
325 DCHECK_LE(start, after_loop->GetStart());
326 last_in_loop = after_loop;
327 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100328 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000329 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100330 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100331 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000332 } else if (after_loop->GetStart() <= end) {
333 first_range_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100334 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100335 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000336 } else {
337 // The use after the loop is after a lifetime hole.
338 DCHECK(last_in_loop != nullptr);
339 first_range_ = last_in_loop;
340 first_range_->start_ = start;
341 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100342 }
343 }
344
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100345 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100346 void SetSpillSlot(int slot) {
347 DCHECK(!is_fixed_);
348 DCHECK(!is_temp_);
349 spill_slot_ = slot;
350 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100351 int GetSpillSlot() const { return spill_slot_; }
352
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100353 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100354 if (first_range_ != nullptr) {
355 first_range_->start_ = from;
356 } else {
357 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000358 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100359 DCHECK(from == defined_by_->GetLifetimePosition());
360 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
361 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100362 }
363
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100364 LiveInterval* GetParent() const { return parent_; }
365
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100366 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000367 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100368
369 int GetRegister() const { return register_; }
370 void SetRegister(int reg) { register_ = reg; }
371 void ClearRegister() { register_ = kNoRegister; }
372 bool HasRegister() const { return register_ != kNoRegister; }
373
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100374 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100375 return GetEnd() <= position;
376 }
377
378 bool IsDefinedAt(size_t position) const {
379 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100380 }
381
David Brazdil5b8e6a52015-02-25 16:17:05 +0000382 bool Covers(size_t position) {
383 return !IsDeadAt(position) && FindRangeAt(position) != nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100384 }
385
386 /**
387 * Returns the first intersection of this interval with `other`.
388 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100389 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100390 // Advance both intervals and find the first matching range start in
391 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100392 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100393 LiveRange* other_range = other->first_range_;
394 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000395 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100396 my_range = my_range->GetNext();
397 if (my_range == nullptr) {
398 return kNoLifetime;
399 }
David Brazdil714e14f2015-02-25 11:57:05 +0000400 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100401 other_range = other_range->GetNext();
402 if (other_range == nullptr) {
403 return kNoLifetime;
404 }
David Brazdil714e14f2015-02-25 11:57:05 +0000405 } else {
406 DCHECK(my_range->IntersectsWith(*other_range));
407 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100408 }
409 } while (true);
410 }
411
412 size_t GetStart() const {
413 return first_range_->GetStart();
414 }
415
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100416 size_t GetEnd() const {
417 return last_range_->GetEnd();
418 }
419
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100420 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100421 if (is_temp_) {
422 return position == GetStart() ? position : kNoLifetime;
423 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100424 if (position == GetStart() && defined_by_ != nullptr) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100425 LocationSummary* locations = defined_by_->GetLocations();
426 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100427 // This interval is the first interval of the instruction. If the output
428 // of the instruction requires a register, we return the position of that instruction
429 // as the first register use.
430 if (location.IsUnallocated()) {
431 if ((location.GetPolicy() == Location::kRequiresRegister)
432 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000433 && (locations->InAt(0).IsRegister()
434 || locations->InAt(0).IsRegisterPair()
435 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100436 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100437 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
438 || (location.GetPolicy() == Location::kSameAsFirstInput
439 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
440 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100441 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000442 } else if (location.IsRegister() || location.IsRegisterPair()) {
443 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100444 }
445 }
446
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100447 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100448 size_t end = GetEnd();
449 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100450 size_t use_position = use->GetPosition();
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100451 if (use_position > position && !use->GetIsEnvironment()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100452 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100453 if (location.IsUnallocated()
454 && (location.GetPolicy() == Location::kRequiresRegister
455 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100456 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100457 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100458 }
459 use = use->GetNext();
460 }
461 return kNoLifetime;
462 }
463
464 size_t FirstRegisterUse() const {
465 return FirstRegisterUseAfter(GetStart());
466 }
467
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000468 size_t FirstUseAfter(size_t position) const {
469 if (is_temp_) {
470 return position == GetStart() ? position : kNoLifetime;
471 }
472
473 UsePosition* use = first_use_;
474 size_t end = GetEnd();
475 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000476 if (!use->GetIsEnvironment()) {
477 size_t use_position = use->GetPosition();
478 if (use_position > position) {
479 return use_position;
480 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000481 }
482 use = use->GetNext();
483 }
484 return kNoLifetime;
485 }
486
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100487 UsePosition* GetFirstUse() const {
488 return first_use_;
489 }
490
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100491 Primitive::Type GetType() const {
492 return type_;
493 }
494
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100495 HInstruction* GetDefinedBy() const {
496 return defined_by_;
497 }
498
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100499 SafepointPosition* FindSafepointJustBefore(size_t position) const {
500 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
501 safepoint != nullptr;
502 previous = safepoint, safepoint = safepoint->GetNext()) {
503 if (safepoint->GetPosition() >= position) return previous;
504 }
505 return last_safepoint_;
506 }
507
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100508 /**
509 * Split this interval at `position`. This interval is changed to:
510 * [start ... position).
511 *
512 * The new interval covers:
513 * [position ... end)
514 */
515 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100516 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100517 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100518 DCHECK_GT(position, GetStart());
519
David Brazdil241a4862015-04-16 17:59:03 +0100520 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100521 // This range dies before `position`, no need to split.
522 return nullptr;
523 }
524
525 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100526 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
527 if (new_last_safepoint == nullptr) {
528 new_interval->first_safepoint_ = first_safepoint_;
529 new_interval->last_safepoint_ = last_safepoint_;
530 first_safepoint_ = last_safepoint_ = nullptr;
531 } else if (last_safepoint_ != new_last_safepoint) {
532 new_interval->last_safepoint_ = last_safepoint_;
533 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
534 DCHECK(new_interval->first_safepoint_ != nullptr);
535 last_safepoint_ = new_last_safepoint;
536 last_safepoint_->SetNext(nullptr);
537 }
538
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100539 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100540 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100541 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100542
543 new_interval->first_use_ = first_use_;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000544 last_visited_range_ = nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100545 LiveRange* current = first_range_;
546 LiveRange* previous = nullptr;
547 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000548 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100549 do {
550 if (position >= current->GetEnd()) {
551 // Move to next range.
552 previous = current;
553 current = current->next_;
554 } else if (position <= current->GetStart()) {
555 // If the previous range did not cover this position, we know position is in
556 // a lifetime hole. We can just break the first_range_ and last_range_ links
557 // and return the new interval.
558 DCHECK(previous != nullptr);
559 DCHECK(current != first_range_);
560 new_interval->last_range_ = last_range_;
561 last_range_ = previous;
562 previous->next_ = nullptr;
563 new_interval->first_range_ = current;
564 return new_interval;
565 } else {
566 // This range covers position. We create a new last_range_ for this interval
567 // that covers last_range_->Start() and position. We also shorten the current
568 // range and make it the first range of the new interval.
569 DCHECK(position < current->GetEnd() && position > current->GetStart());
570 new_interval->last_range_ = last_range_;
571 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
572 if (previous != nullptr) {
573 previous->next_ = last_range_;
574 } else {
575 first_range_ = last_range_;
576 }
577 new_interval->first_range_ = current;
578 current->start_ = position;
579 return new_interval;
580 }
581 } while (current != nullptr);
582
583 LOG(FATAL) << "Unreachable";
584 return nullptr;
585 }
586
Nicolas Geoffray76905622014-09-25 14:39:26 +0100587 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100588 return GetStart() <= other->GetStart();
589 }
590
591 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100592 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100593 }
594
595 void Dump(std::ostream& stream) const {
596 stream << "ranges: { ";
597 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000598 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100599 current->Dump(stream);
600 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000601 current = current->GetNext();
602 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100603 stream << "}, uses: { ";
604 UsePosition* use = first_use_;
605 if (use != nullptr) {
606 do {
607 use->Dump(stream);
608 stream << " ";
609 } while ((use = use->GetNext()) != nullptr);
610 }
611 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700612 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000613 stream << " is_high: " << IsHighInterval();
614 stream << " is_low: " << IsLowInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100615 }
616
617 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000618 LiveInterval* GetLastSibling() {
619 LiveInterval* result = this;
620 while (result->next_sibling_ != nullptr) {
621 result = result->next_sibling_;
622 }
623 return result;
624 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100625
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100626 // Returns the first register hint that is at least free before
627 // the value contained in `free_until`. If none is found, returns
628 // `kNoRegister`.
629 int FindFirstRegisterHint(size_t* free_until) const;
630
631 // If there is enough at the definition site to find a register (for example
632 // it uses the same input as the first input), returns the register as a hint.
633 // Returns kNoRegister otherwise.
634 int FindHintAtDefinition() const;
635
636 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
637 // slots for spilling.
638 bool NeedsTwoSpillSlots() const;
639
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100640 bool IsFloatingPoint() const {
641 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
642 }
643
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100644 // Converts the location of the interval to a `Location` object.
645 Location ToLocation() const;
646
647 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000648 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100649
David Brazdil241a4862015-04-16 17:59:03 +0100650 // Finds the sibling that is defined at `position`.
651 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100652
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100653 // Returns whether `other` and `this` share the same kind of register.
654 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000655 bool SameRegisterKind(const LiveInterval& other) const {
656 return IsFloatingPoint() == other.IsFloatingPoint();
657 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100658
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000659 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000660 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000661 }
662
663 bool HasLowInterval() const {
664 return IsHighInterval();
665 }
666
667 LiveInterval* GetLowInterval() const {
668 DCHECK(HasLowInterval());
669 return high_or_low_interval_;
670 }
671
672 LiveInterval* GetHighInterval() const {
673 DCHECK(HasHighInterval());
674 return high_or_low_interval_;
675 }
676
677 bool IsHighInterval() const {
678 return GetParent()->is_high_interval_;
679 }
680
681 bool IsLowInterval() const {
682 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
683 }
684
685 void SetLowInterval(LiveInterval* low) {
686 DCHECK(IsHighInterval());
687 high_or_low_interval_ = low;
688 }
689
690 void SetHighInterval(LiveInterval* high) {
691 DCHECK(IsLowInterval());
692 high_or_low_interval_ = high;
693 }
694
695 void AddHighInterval(bool is_temp = false) {
696 DCHECK_EQ(GetParent(), this);
697 DCHECK(!HasHighInterval());
698 DCHECK(!HasLowInterval());
699 high_or_low_interval_ = new (allocator_) LiveInterval(
700 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
701 high_or_low_interval_->high_or_low_interval_ = this;
702 if (first_range_ != nullptr) {
703 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
704 high_or_low_interval_->last_range_ = first_range_->GetLastRange();
705 }
706 if (first_use_ != nullptr) {
707 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
708 }
709 }
710
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000711 // Returns whether an interval, when it is non-split, is using
712 // the same register of one of its input.
713 bool IsUsingInputRegister() const {
714 if (defined_by_ != nullptr && !IsSplit()) {
715 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
716 LiveInterval* interval = it.Current()->GetLiveInterval();
717
718 // Find the interval that covers `defined_by`_.
719 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
720 interval = interval->GetNextSibling();
721 }
722
723 // Check if both intervals have the same register of the same kind.
724 if (interval != nullptr
725 && interval->SameRegisterKind(*this)
726 && interval->GetRegister() == GetRegister()) {
727 return true;
728 }
729 }
730 }
731 return false;
732 }
733
734 // Returns whether an interval, when it is non-split, can safely use
735 // the same register of one of its input. Note that this method requires
736 // IsUsingInputRegister() to be true.
737 bool CanUseInputRegister() const {
738 DCHECK(IsUsingInputRegister());
739 if (defined_by_ != nullptr && !IsSplit()) {
740 LocationSummary* locations = defined_by_->GetLocations();
741 if (locations->OutputCanOverlapWithInputs()) {
742 return false;
743 }
744 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
745 LiveInterval* interval = it.Current()->GetLiveInterval();
746
747 // Find the interval that covers `defined_by`_.
748 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
749 interval = interval->GetNextSibling();
750 }
751
752 if (interval != nullptr
753 && interval->SameRegisterKind(*this)
754 && interval->GetRegister() == GetRegister()) {
755 // We found the input that has the same register. Check if it is live after
756 // `defined_by`_.
757 return !interval->Covers(defined_by_->GetLifetimePosition() + 1);
758 }
759 }
760 }
761 LOG(FATAL) << "Unreachable";
762 UNREACHABLE();
763 }
764
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100765 void AddSafepoint(HInstruction* instruction) {
766 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
767 if (first_safepoint_ == nullptr) {
768 first_safepoint_ = last_safepoint_ = safepoint;
769 } else {
770 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
771 last_safepoint_->SetNext(safepoint);
772 last_safepoint_ = safepoint;
773 }
774 }
775
776 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100777 return first_safepoint_;
778 }
779
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100780 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700781 LiveInterval(ArenaAllocator* allocator,
782 Primitive::Type type,
783 HInstruction* defined_by = nullptr,
784 bool is_fixed = false,
785 int reg = kNoRegister,
786 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000787 bool is_slow_path_safepoint = false,
788 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700789 : allocator_(allocator),
790 first_range_(nullptr),
791 last_range_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100792 first_safepoint_(nullptr),
793 last_safepoint_(nullptr),
David Brazdil5b8e6a52015-02-25 16:17:05 +0000794 last_visited_range_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700795 first_use_(nullptr),
796 type_(type),
797 next_sibling_(nullptr),
798 parent_(this),
799 register_(reg),
800 spill_slot_(kNoSpillSlot),
801 is_fixed_(is_fixed),
802 is_temp_(is_temp),
803 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000804 is_high_interval_(is_high_interval),
805 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700806 defined_by_(defined_by) {}
807
David Brazdil5b8e6a52015-02-25 16:17:05 +0000808 // Returns a LiveRange covering the given position or nullptr if no such range
809 // exists in the interval.
810 // This is a linear search optimized for multiple queries in a non-decreasing
811 // position order typical for linear scan register allocation.
812 LiveRange* FindRangeAt(size_t position) {
813 // Make sure operations on the interval didn't leave us with a cached result
814 // from a sibling.
815 if (kIsDebugBuild) {
816 if (last_visited_range_ != nullptr) {
817 DCHECK_GE(last_visited_range_->GetStart(), GetStart());
818 DCHECK_LE(last_visited_range_->GetEnd(), GetEnd());
819 }
820 }
821
822 // If this method was called earlier on a lower position, use that result as
823 // a starting point to save time. However, linear scan performs 3 scans:
824 // integers, floats, and resolution. Instead of resetting at the beginning
825 // of a scan, we do it here.
826 LiveRange* current;
827 if (last_visited_range_ != nullptr && position >= last_visited_range_->GetStart()) {
828 current = last_visited_range_;
829 } else {
830 current = first_range_;
831 }
832 while (current != nullptr && current->GetEnd() <= position) {
833 current = current->GetNext();
834 }
835 last_visited_range_ = current;
836 if (current != nullptr && position >= current->GetStart()) {
837 return current;
838 } else {
839 return nullptr;
840 }
841 }
842
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100843 ArenaAllocator* const allocator_;
844
845 // Ranges of this interval. We need a quick access to the last range to test
846 // for liveness (see `IsDeadAt`).
847 LiveRange* first_range_;
848 LiveRange* last_range_;
849
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100850 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100851 SafepointPosition* first_safepoint_;
852 SafepointPosition* last_safepoint_;
853
David Brazdil5b8e6a52015-02-25 16:17:05 +0000854 // Last visited range. This is a range search optimization leveraging the fact
855 // that the register allocator does a linear scan through the intervals.
856 LiveRange* last_visited_range_;
857
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100858 // Uses of this interval. Note that this linked list is shared amongst siblings.
859 UsePosition* first_use_;
860
861 // The instruction type this interval corresponds to.
862 const Primitive::Type type_;
863
864 // Live interval that is the result of a split.
865 LiveInterval* next_sibling_;
866
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100867 // The first interval from which split intervals come from.
868 LiveInterval* parent_;
869
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100870 // The register allocated to this interval.
871 int register_;
872
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100873 // The spill slot allocated to this interval.
874 int spill_slot_;
875
876 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100877 const bool is_fixed_;
878
879 // Whether the interval is for a temporary.
880 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100881
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100882 // Whether the interval is for a safepoint that calls on slow path.
883 const bool is_slow_path_safepoint_;
884
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000885 // Whether this interval is a synthesized interval for register pair.
886 const bool is_high_interval_;
887
888 // If this interval needs a register pair, the high or low equivalent.
889 // `is_high_interval_` tells whether this holds the low or the high.
890 LiveInterval* high_or_low_interval_;
891
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100892 // The instruction represented by this interval.
893 HInstruction* const defined_by_;
894
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100895 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100896 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100897
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000898 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
899
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100900 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
901};
902
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000903/**
904 * Analysis that computes the liveness of instructions:
905 *
906 * (a) Non-environment uses of an instruction always make
907 * the instruction live.
908 * (b) Environment uses of an instruction whose type is
909 * object (that is, non-primitive), make the instruction live.
910 * This is due to having to keep alive objects that have
911 * finalizers deleting native objects.
912 * (c) When the graph has the debuggable property, environment uses
913 * of an instruction that has a primitive type make the instruction live.
914 * If the graph does not have the debuggable property, the environment
915 * use has no effect, and may get a 'none' value after register allocation.
916 *
917 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
918 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100919class SsaLivenessAnalysis : public ValueObject {
920 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100921 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100922 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100923 codegen_(codegen),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100924 block_infos_(graph->GetArena(), graph->GetBlocks().Size()),
925 instructions_from_ssa_index_(graph->GetArena(), 0),
926 instructions_from_lifetime_position_(graph->GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100927 number_of_ssa_values_(0) {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100928 block_infos_.SetSize(graph->GetBlocks().Size());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100929 }
930
931 void Analyze();
932
933 BitVector* GetLiveInSet(const HBasicBlock& block) const {
934 return &block_infos_.Get(block.GetBlockId())->live_in_;
935 }
936
937 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
938 return &block_infos_.Get(block.GetBlockId())->live_out_;
939 }
940
941 BitVector* GetKillSet(const HBasicBlock& block) const {
942 return &block_infos_.Get(block.GetBlockId())->kill_;
943 }
944
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100945 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100946 return instructions_from_ssa_index_.Get(index);
947 }
948
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100949 HInstruction* GetInstructionFromPosition(size_t index) const {
950 return instructions_from_lifetime_position_.Get(index);
951 }
952
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100953 HInstruction* GetTempUser(LiveInterval* temp) const {
954 // A temporary shares the same lifetime start as the instruction that requires it.
955 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000956 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
957 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
958 return user;
959 }
960
961 size_t GetTempIndex(LiveInterval* temp) const {
962 // We use the input index to store the index of the temporary in the user's temporary list.
963 DCHECK(temp->IsTemp());
964 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100965 }
966
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100967 size_t GetMaxLifetimePosition() const {
968 return instructions_from_lifetime_position_.Size() * 2 - 1;
969 }
970
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100971 size_t GetNumberOfSsaValues() const {
972 return number_of_ssa_values_;
973 }
974
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800975 static constexpr const char* kLivenessPassName = "liveness";
976
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100977 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100978 // Linearize the graph so that:
979 // (1): a block is always after its dominator,
980 // (2): blocks of loops are contiguous.
981 // This creates a natural and efficient ordering when visualizing live ranges.
982 void LinearizeGraph();
983
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100984 // Give an SSA number to each instruction that defines a value used by another instruction,
985 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100986 void NumberInstructions();
987
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100988 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
989 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100990
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100991 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
992 // kill sets, that do not take into account backward branches.
993 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100994
995 // After computing the initial sets, this method does a fixed point
996 // calculation over the live_in and live_out set to take into account
997 // backwards branches.
998 void ComputeLiveInAndLiveOutSets();
999
1000 // Update the live_in set of the block and returns whether it has changed.
1001 bool UpdateLiveIn(const HBasicBlock& block);
1002
1003 // Update the live_out set of the block and returns whether it has changed.
1004 bool UpdateLiveOut(const HBasicBlock& block);
1005
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001006 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
1007 if (instruction == nullptr) return false;
1008 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1009 return instruction->GetType() == Primitive::kPrimNot;
1010 }
1011
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001012 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001013 CodeGenerator* const codegen_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001014 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001015
1016 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001017 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001018
1019 // Temporary array used when inserting moves in the graph.
1020 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001021 size_t number_of_ssa_values_;
1022
1023 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1024};
1025
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001026} // namespace art
1027
1028#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_