blob: e3a4d8138e8919da6372c3918d7b50b46ee52ce4 [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_;
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100134 if (is_environment_) {
135 stream << " (env)";
136 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100137 }
138
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000139 UsePosition* Dup(ArenaAllocator* allocator) const {
140 return new (allocator) UsePosition(
141 user_, input_index_, is_environment_, position_,
142 next_ == nullptr ? nullptr : next_->Dup(allocator));
143 }
144
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100145 private:
146 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100147 const size_t input_index_;
148 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100149 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100150 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100151
152 DISALLOW_COPY_AND_ASSIGN(UsePosition);
153};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100154
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100155class SafepointPosition : public ArenaObject<kArenaAllocMisc> {
156 public:
157 explicit SafepointPosition(HInstruction* instruction)
158 : instruction_(instruction),
159 next_(nullptr) {}
160
161 void SetNext(SafepointPosition* next) {
162 next_ = next;
163 }
164
165 size_t GetPosition() const {
166 return instruction_->GetLifetimePosition();
167 }
168
169 SafepointPosition* GetNext() const {
170 return next_;
171 }
172
173 LocationSummary* GetLocations() const {
174 return instruction_->GetLocations();
175 }
176
177 HInstruction* GetInstruction() const {
178 return instruction_;
179 }
180
181 private:
182 HInstruction* const instruction_;
183 SafepointPosition* next_;
184
185 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
186};
187
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100188/**
189 * An interval is a list of disjoint live ranges where an instruction is live.
190 * Each instruction that has uses gets an interval.
191 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700192class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100193 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700194 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
195 Primitive::Type type,
196 HInstruction* instruction = nullptr) {
197 return new (allocator) LiveInterval(allocator, type, instruction);
198 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100199
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100200 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
201 return new (allocator) LiveInterval(
202 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
203 }
204
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100205 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100206 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
207 }
208
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100209 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
210 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100211 }
212
213 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700214 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100215 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700216 // This interval is the result of a split.
217 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100218
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000219 void AddTempUse(HInstruction* instruction, size_t temp_index) {
220 DCHECK(IsTemp());
221 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
222 size_t position = instruction->GetLifetimePosition();
223 first_use_ = new (allocator_) UsePosition(
224 instruction, temp_index, /* is_environment */ false, position, first_use_);
225 AddRange(position, position + 1);
226 }
227
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000228 void AddUse(HInstruction* instruction,
229 size_t input_index,
230 bool is_environment,
231 bool keep_alive = false) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100232 // Set the use within the instruction.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000233 size_t position = instruction->GetLifetimePosition() + 1;
234 LocationSummary* locations = instruction->GetLocations();
235 if (!is_environment) {
236 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
237 // For fixed inputs and output same as input, the register allocator
238 // requires to have inputs die at the instruction, so that input moves use the
239 // location of the input just before that instruction (and not potential moves due
240 // to splitting).
241 position = instruction->GetLifetimePosition();
242 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100243 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000244
245 DCHECK(position == instruction->GetLifetimePosition()
246 || position == instruction->GetLifetimePosition() + 1);
247
Nicolas Geoffray76905622014-09-25 14:39:26 +0100248 if ((first_use_ != nullptr)
249 && (first_use_->GetUser() == instruction)
250 && (first_use_->GetPosition() < position)) {
251 // The user uses the instruction multiple times, and one use dies before the other.
252 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000253 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100254 UsePosition* cursor = first_use_;
255 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
256 cursor = cursor->GetNext();
257 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100258 DCHECK(first_use_->GetPosition() + 1 == position);
259 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100260 instruction, input_index, is_environment, position, cursor->GetNext());
261 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100262 if (first_range_->GetEnd() == first_use_->GetPosition()) {
263 first_range_->end_ = position;
264 }
265 return;
266 }
267
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000268 first_use_ = new (allocator_) UsePosition(
269 instruction, input_index, is_environment, position, first_use_);
270
271 if (is_environment && !keep_alive) {
272 // If this environment use does not keep the instruction live, it does not
273 // affect the live range of that instruction.
274 return;
275 }
276
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100277 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100278 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100279 // First time we see a use of that interval.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100280 first_range_ = last_range_ = new (allocator_) LiveRange(
281 start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100282 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100283 // There is a use later in the same block or in a following block.
284 // Note that in such a case, `AddRange` for the whole blocks has been called
285 // before arriving in this method, and this is the reason the start of
286 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100287 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100288 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100289 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100290 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100291 // Note that the start of `first_range_` can be equal to `end`: two blocks
292 // having adjacent lifetime positions are not necessarily
293 // predecessor/successor. When two blocks are predecessor/successor, the
294 // liveness algorithm has called `AddRange` before arriving in this method,
295 // and the check line 205 would succeed.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100296 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100297 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100298 }
299
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100300 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100301 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100302 first_use_ = new (allocator_) UsePosition(
303 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100304 }
305
306 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100307 if (first_range_ == nullptr) {
308 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
309 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100310 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100311 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100312 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
313 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100314 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100315 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100316 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100317 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318 }
319 }
320
321 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100322 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000323 DCHECK_LE(start, first_range_->GetStart());
324 // Find the range that covers the positions after the loop.
325 LiveRange* after_loop = first_range_;
326 LiveRange* last_in_loop = nullptr;
327 while (after_loop != nullptr && after_loop->GetEnd() < end) {
328 DCHECK_LE(start, after_loop->GetStart());
329 last_in_loop = after_loop;
330 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100331 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000332 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100333 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100334 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000335 } else if (after_loop->GetStart() <= end) {
336 first_range_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100337 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100338 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000339 } else {
340 // The use after the loop is after a lifetime hole.
341 DCHECK(last_in_loop != nullptr);
342 first_range_ = last_in_loop;
343 first_range_->start_ = start;
344 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100345 }
346 }
347
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100348 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100349 void SetSpillSlot(int slot) {
350 DCHECK(!is_fixed_);
351 DCHECK(!is_temp_);
352 spill_slot_ = slot;
353 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100354 int GetSpillSlot() const { return spill_slot_; }
355
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100356 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100357 if (first_range_ != nullptr) {
358 first_range_->start_ = from;
359 } else {
360 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000361 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100362 DCHECK(from == defined_by_->GetLifetimePosition());
363 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
364 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100365 }
366
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100367 LiveInterval* GetParent() const { return parent_; }
368
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100369 // Returns whether this interval is the parent interval, that is, the interval
370 // that starts where the HInstruction is defined.
371 bool IsParent() const { return parent_ == this; }
372
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100373 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000374 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100375
376 int GetRegister() const { return register_; }
377 void SetRegister(int reg) { register_ = reg; }
378 void ClearRegister() { register_ = kNoRegister; }
379 bool HasRegister() const { return register_ != kNoRegister; }
380
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100381 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100382 return GetEnd() <= position;
383 }
384
385 bool IsDefinedAt(size_t position) const {
386 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100387 }
388
David Brazdil5b8e6a52015-02-25 16:17:05 +0000389 bool Covers(size_t position) {
390 return !IsDeadAt(position) && FindRangeAt(position) != nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100391 }
392
393 /**
394 * Returns the first intersection of this interval with `other`.
395 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100396 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100397 // Advance both intervals and find the first matching range start in
398 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100399 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100400 LiveRange* other_range = other->first_range_;
401 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000402 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100403 my_range = my_range->GetNext();
404 if (my_range == nullptr) {
405 return kNoLifetime;
406 }
David Brazdil714e14f2015-02-25 11:57:05 +0000407 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100408 other_range = other_range->GetNext();
409 if (other_range == nullptr) {
410 return kNoLifetime;
411 }
David Brazdil714e14f2015-02-25 11:57:05 +0000412 } else {
413 DCHECK(my_range->IntersectsWith(*other_range));
414 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100415 }
416 } while (true);
417 }
418
419 size_t GetStart() const {
420 return first_range_->GetStart();
421 }
422
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100423 size_t GetEnd() const {
424 return last_range_->GetEnd();
425 }
426
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100427 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100428 if (is_temp_) {
429 return position == GetStart() ? position : kNoLifetime;
430 }
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100431 if (position == GetStart() && IsParent()) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100432 LocationSummary* locations = defined_by_->GetLocations();
433 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100434 // This interval is the first interval of the instruction. If the output
435 // of the instruction requires a register, we return the position of that instruction
436 // as the first register use.
437 if (location.IsUnallocated()) {
438 if ((location.GetPolicy() == Location::kRequiresRegister)
439 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000440 && (locations->InAt(0).IsRegister()
441 || locations->InAt(0).IsRegisterPair()
442 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100443 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100444 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
445 || (location.GetPolicy() == Location::kSameAsFirstInput
446 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
447 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100448 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000449 } else if (location.IsRegister() || location.IsRegisterPair()) {
450 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100451 }
452 }
453
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100454 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100455 size_t end = GetEnd();
456 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100457 size_t use_position = use->GetPosition();
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100458 if (use_position > position && !use->GetIsEnvironment()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100459 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100460 if (location.IsUnallocated()
461 && (location.GetPolicy() == Location::kRequiresRegister
462 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100463 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100464 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100465 }
466 use = use->GetNext();
467 }
468 return kNoLifetime;
469 }
470
471 size_t FirstRegisterUse() const {
472 return FirstRegisterUseAfter(GetStart());
473 }
474
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000475 size_t FirstUseAfter(size_t position) const {
476 if (is_temp_) {
477 return position == GetStart() ? position : kNoLifetime;
478 }
479
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100480 if (position == GetStart() && IsParent()) {
481 if (defined_by_->GetLocations()->Out().IsValid()) {
482 return position;
483 }
484 }
485
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000486 UsePosition* use = first_use_;
487 size_t end = GetEnd();
488 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000489 if (!use->GetIsEnvironment()) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100490 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000491 size_t use_position = use->GetPosition();
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100492 if (use_position > position && location.IsValid()) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000493 return use_position;
494 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000495 }
496 use = use->GetNext();
497 }
498 return kNoLifetime;
499 }
500
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100501 UsePosition* GetFirstUse() const {
502 return first_use_;
503 }
504
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100505 Primitive::Type GetType() const {
506 return type_;
507 }
508
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100509 HInstruction* GetDefinedBy() const {
510 return defined_by_;
511 }
512
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100513 SafepointPosition* FindSafepointJustBefore(size_t position) const {
514 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
515 safepoint != nullptr;
516 previous = safepoint, safepoint = safepoint->GetNext()) {
517 if (safepoint->GetPosition() >= position) return previous;
518 }
519 return last_safepoint_;
520 }
521
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100522 /**
523 * Split this interval at `position`. This interval is changed to:
524 * [start ... position).
525 *
526 * The new interval covers:
527 * [position ... end)
528 */
529 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100530 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100531 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100532 DCHECK_GT(position, GetStart());
533
David Brazdil241a4862015-04-16 17:59:03 +0100534 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100535 // This range dies before `position`, no need to split.
536 return nullptr;
537 }
538
539 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100540 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
541 if (new_last_safepoint == nullptr) {
542 new_interval->first_safepoint_ = first_safepoint_;
543 new_interval->last_safepoint_ = last_safepoint_;
544 first_safepoint_ = last_safepoint_ = nullptr;
545 } else if (last_safepoint_ != new_last_safepoint) {
546 new_interval->last_safepoint_ = last_safepoint_;
547 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
548 DCHECK(new_interval->first_safepoint_ != nullptr);
549 last_safepoint_ = new_last_safepoint;
550 last_safepoint_->SetNext(nullptr);
551 }
552
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100553 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100554 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100555 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100556
557 new_interval->first_use_ = first_use_;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000558 last_visited_range_ = nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100559 LiveRange* current = first_range_;
560 LiveRange* previous = nullptr;
561 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000562 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100563 do {
564 if (position >= current->GetEnd()) {
565 // Move to next range.
566 previous = current;
567 current = current->next_;
568 } else if (position <= current->GetStart()) {
569 // If the previous range did not cover this position, we know position is in
570 // a lifetime hole. We can just break the first_range_ and last_range_ links
571 // and return the new interval.
572 DCHECK(previous != nullptr);
573 DCHECK(current != first_range_);
574 new_interval->last_range_ = last_range_;
575 last_range_ = previous;
576 previous->next_ = nullptr;
577 new_interval->first_range_ = current;
578 return new_interval;
579 } else {
580 // This range covers position. We create a new last_range_ for this interval
581 // that covers last_range_->Start() and position. We also shorten the current
582 // range and make it the first range of the new interval.
583 DCHECK(position < current->GetEnd() && position > current->GetStart());
584 new_interval->last_range_ = last_range_;
585 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
586 if (previous != nullptr) {
587 previous->next_ = last_range_;
588 } else {
589 first_range_ = last_range_;
590 }
591 new_interval->first_range_ = current;
592 current->start_ = position;
593 return new_interval;
594 }
595 } while (current != nullptr);
596
597 LOG(FATAL) << "Unreachable";
598 return nullptr;
599 }
600
Nicolas Geoffray76905622014-09-25 14:39:26 +0100601 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100602 return GetStart() <= other->GetStart();
603 }
604
605 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100606 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100607 }
608
609 void Dump(std::ostream& stream) const {
610 stream << "ranges: { ";
611 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000612 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100613 current->Dump(stream);
614 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000615 current = current->GetNext();
616 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100617 stream << "}, uses: { ";
618 UsePosition* use = first_use_;
619 if (use != nullptr) {
620 do {
621 use->Dump(stream);
622 stream << " ";
623 } while ((use = use->GetNext()) != nullptr);
624 }
625 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700626 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000627 stream << " is_high: " << IsHighInterval();
628 stream << " is_low: " << IsLowInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100629 }
630
631 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000632 LiveInterval* GetLastSibling() {
633 LiveInterval* result = this;
634 while (result->next_sibling_ != nullptr) {
635 result = result->next_sibling_;
636 }
637 return result;
638 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100639
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100640 // Returns the first register hint that is at least free before
641 // the value contained in `free_until`. If none is found, returns
642 // `kNoRegister`.
643 int FindFirstRegisterHint(size_t* free_until) const;
644
645 // If there is enough at the definition site to find a register (for example
646 // it uses the same input as the first input), returns the register as a hint.
647 // Returns kNoRegister otherwise.
648 int FindHintAtDefinition() const;
649
650 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
651 // slots for spilling.
652 bool NeedsTwoSpillSlots() const;
653
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100654 bool IsFloatingPoint() const {
655 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
656 }
657
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100658 // Converts the location of the interval to a `Location` object.
659 Location ToLocation() const;
660
661 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000662 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100663
David Brazdil241a4862015-04-16 17:59:03 +0100664 // Finds the sibling that is defined at `position`.
665 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100666
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100667 // Returns whether `other` and `this` share the same kind of register.
668 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000669 bool SameRegisterKind(const LiveInterval& other) const {
670 return IsFloatingPoint() == other.IsFloatingPoint();
671 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100672
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000673 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000674 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000675 }
676
677 bool HasLowInterval() const {
678 return IsHighInterval();
679 }
680
681 LiveInterval* GetLowInterval() const {
682 DCHECK(HasLowInterval());
683 return high_or_low_interval_;
684 }
685
686 LiveInterval* GetHighInterval() const {
687 DCHECK(HasHighInterval());
688 return high_or_low_interval_;
689 }
690
691 bool IsHighInterval() const {
692 return GetParent()->is_high_interval_;
693 }
694
695 bool IsLowInterval() const {
696 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
697 }
698
699 void SetLowInterval(LiveInterval* low) {
700 DCHECK(IsHighInterval());
701 high_or_low_interval_ = low;
702 }
703
704 void SetHighInterval(LiveInterval* high) {
705 DCHECK(IsLowInterval());
706 high_or_low_interval_ = high;
707 }
708
709 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100710 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000711 DCHECK(!HasHighInterval());
712 DCHECK(!HasLowInterval());
713 high_or_low_interval_ = new (allocator_) LiveInterval(
714 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
715 high_or_low_interval_->high_or_low_interval_ = this;
716 if (first_range_ != nullptr) {
717 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
718 high_or_low_interval_->last_range_ = first_range_->GetLastRange();
719 }
720 if (first_use_ != nullptr) {
721 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
722 }
723 }
724
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000725 // Returns whether an interval, when it is non-split, is using
726 // the same register of one of its input.
727 bool IsUsingInputRegister() const {
728 if (defined_by_ != nullptr && !IsSplit()) {
729 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
730 LiveInterval* interval = it.Current()->GetLiveInterval();
731
732 // Find the interval that covers `defined_by`_.
733 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
734 interval = interval->GetNextSibling();
735 }
736
737 // Check if both intervals have the same register of the same kind.
738 if (interval != nullptr
739 && interval->SameRegisterKind(*this)
740 && interval->GetRegister() == GetRegister()) {
741 return true;
742 }
743 }
744 }
745 return false;
746 }
747
748 // Returns whether an interval, when it is non-split, can safely use
749 // the same register of one of its input. Note that this method requires
750 // IsUsingInputRegister() to be true.
751 bool CanUseInputRegister() const {
752 DCHECK(IsUsingInputRegister());
753 if (defined_by_ != nullptr && !IsSplit()) {
754 LocationSummary* locations = defined_by_->GetLocations();
755 if (locations->OutputCanOverlapWithInputs()) {
756 return false;
757 }
758 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
759 LiveInterval* interval = it.Current()->GetLiveInterval();
760
761 // Find the interval that covers `defined_by`_.
762 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
763 interval = interval->GetNextSibling();
764 }
765
766 if (interval != nullptr
767 && interval->SameRegisterKind(*this)
768 && interval->GetRegister() == GetRegister()) {
769 // We found the input that has the same register. Check if it is live after
770 // `defined_by`_.
771 return !interval->Covers(defined_by_->GetLifetimePosition() + 1);
772 }
773 }
774 }
775 LOG(FATAL) << "Unreachable";
776 UNREACHABLE();
777 }
778
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100779 void AddSafepoint(HInstruction* instruction) {
780 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
781 if (first_safepoint_ == nullptr) {
782 first_safepoint_ = last_safepoint_ = safepoint;
783 } else {
784 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
785 last_safepoint_->SetNext(safepoint);
786 last_safepoint_ = safepoint;
787 }
788 }
789
790 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100791 return first_safepoint_;
792 }
793
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100794 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700795 LiveInterval(ArenaAllocator* allocator,
796 Primitive::Type type,
797 HInstruction* defined_by = nullptr,
798 bool is_fixed = false,
799 int reg = kNoRegister,
800 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000801 bool is_slow_path_safepoint = false,
802 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700803 : allocator_(allocator),
804 first_range_(nullptr),
805 last_range_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100806 first_safepoint_(nullptr),
807 last_safepoint_(nullptr),
David Brazdil5b8e6a52015-02-25 16:17:05 +0000808 last_visited_range_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700809 first_use_(nullptr),
810 type_(type),
811 next_sibling_(nullptr),
812 parent_(this),
813 register_(reg),
814 spill_slot_(kNoSpillSlot),
815 is_fixed_(is_fixed),
816 is_temp_(is_temp),
817 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000818 is_high_interval_(is_high_interval),
819 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700820 defined_by_(defined_by) {}
821
David Brazdil5b8e6a52015-02-25 16:17:05 +0000822 // Returns a LiveRange covering the given position or nullptr if no such range
823 // exists in the interval.
824 // This is a linear search optimized for multiple queries in a non-decreasing
825 // position order typical for linear scan register allocation.
826 LiveRange* FindRangeAt(size_t position) {
827 // Make sure operations on the interval didn't leave us with a cached result
828 // from a sibling.
829 if (kIsDebugBuild) {
830 if (last_visited_range_ != nullptr) {
831 DCHECK_GE(last_visited_range_->GetStart(), GetStart());
832 DCHECK_LE(last_visited_range_->GetEnd(), GetEnd());
833 }
834 }
835
836 // If this method was called earlier on a lower position, use that result as
837 // a starting point to save time. However, linear scan performs 3 scans:
838 // integers, floats, and resolution. Instead of resetting at the beginning
839 // of a scan, we do it here.
840 LiveRange* current;
841 if (last_visited_range_ != nullptr && position >= last_visited_range_->GetStart()) {
842 current = last_visited_range_;
843 } else {
844 current = first_range_;
845 }
846 while (current != nullptr && current->GetEnd() <= position) {
847 current = current->GetNext();
848 }
849 last_visited_range_ = current;
850 if (current != nullptr && position >= current->GetStart()) {
851 return current;
852 } else {
853 return nullptr;
854 }
855 }
856
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100857 ArenaAllocator* const allocator_;
858
859 // Ranges of this interval. We need a quick access to the last range to test
860 // for liveness (see `IsDeadAt`).
861 LiveRange* first_range_;
862 LiveRange* last_range_;
863
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100864 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100865 SafepointPosition* first_safepoint_;
866 SafepointPosition* last_safepoint_;
867
David Brazdil5b8e6a52015-02-25 16:17:05 +0000868 // Last visited range. This is a range search optimization leveraging the fact
869 // that the register allocator does a linear scan through the intervals.
870 LiveRange* last_visited_range_;
871
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100872 // Uses of this interval. Note that this linked list is shared amongst siblings.
873 UsePosition* first_use_;
874
875 // The instruction type this interval corresponds to.
876 const Primitive::Type type_;
877
878 // Live interval that is the result of a split.
879 LiveInterval* next_sibling_;
880
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100881 // The first interval from which split intervals come from.
882 LiveInterval* parent_;
883
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100884 // The register allocated to this interval.
885 int register_;
886
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100887 // The spill slot allocated to this interval.
888 int spill_slot_;
889
890 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100891 const bool is_fixed_;
892
893 // Whether the interval is for a temporary.
894 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100895
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100896 // Whether the interval is for a safepoint that calls on slow path.
897 const bool is_slow_path_safepoint_;
898
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000899 // Whether this interval is a synthesized interval for register pair.
900 const bool is_high_interval_;
901
902 // If this interval needs a register pair, the high or low equivalent.
903 // `is_high_interval_` tells whether this holds the low or the high.
904 LiveInterval* high_or_low_interval_;
905
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100906 // The instruction represented by this interval.
907 HInstruction* const defined_by_;
908
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100909 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100910 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100911
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000912 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
913
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100914 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
915};
916
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000917/**
918 * Analysis that computes the liveness of instructions:
919 *
920 * (a) Non-environment uses of an instruction always make
921 * the instruction live.
922 * (b) Environment uses of an instruction whose type is
923 * object (that is, non-primitive), make the instruction live.
924 * This is due to having to keep alive objects that have
925 * finalizers deleting native objects.
926 * (c) When the graph has the debuggable property, environment uses
927 * of an instruction that has a primitive type make the instruction live.
928 * If the graph does not have the debuggable property, the environment
929 * use has no effect, and may get a 'none' value after register allocation.
930 *
931 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
932 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100933class SsaLivenessAnalysis : public ValueObject {
934 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100935 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100936 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100937 codegen_(codegen),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100938 block_infos_(graph->GetArena(), graph->GetBlocks().Size()),
939 instructions_from_ssa_index_(graph->GetArena(), 0),
940 instructions_from_lifetime_position_(graph->GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100941 number_of_ssa_values_(0) {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100942 block_infos_.SetSize(graph->GetBlocks().Size());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100943 }
944
945 void Analyze();
946
947 BitVector* GetLiveInSet(const HBasicBlock& block) const {
948 return &block_infos_.Get(block.GetBlockId())->live_in_;
949 }
950
951 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
952 return &block_infos_.Get(block.GetBlockId())->live_out_;
953 }
954
955 BitVector* GetKillSet(const HBasicBlock& block) const {
956 return &block_infos_.Get(block.GetBlockId())->kill_;
957 }
958
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100959 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100960 return instructions_from_ssa_index_.Get(index);
961 }
962
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100963 HInstruction* GetInstructionFromPosition(size_t index) const {
964 return instructions_from_lifetime_position_.Get(index);
965 }
966
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100967 HInstruction* GetTempUser(LiveInterval* temp) const {
968 // A temporary shares the same lifetime start as the instruction that requires it.
969 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000970 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
971 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
972 return user;
973 }
974
975 size_t GetTempIndex(LiveInterval* temp) const {
976 // We use the input index to store the index of the temporary in the user's temporary list.
977 DCHECK(temp->IsTemp());
978 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100979 }
980
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100981 size_t GetMaxLifetimePosition() const {
982 return instructions_from_lifetime_position_.Size() * 2 - 1;
983 }
984
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100985 size_t GetNumberOfSsaValues() const {
986 return number_of_ssa_values_;
987 }
988
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800989 static constexpr const char* kLivenessPassName = "liveness";
990
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100991 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100992 // Linearize the graph so that:
993 // (1): a block is always after its dominator,
994 // (2): blocks of loops are contiguous.
995 // This creates a natural and efficient ordering when visualizing live ranges.
996 void LinearizeGraph();
997
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100998 // Give an SSA number to each instruction that defines a value used by another instruction,
999 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001000 void NumberInstructions();
1001
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001002 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1003 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001004
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001005 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1006 // kill sets, that do not take into account backward branches.
1007 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001008
1009 // After computing the initial sets, this method does a fixed point
1010 // calculation over the live_in and live_out set to take into account
1011 // backwards branches.
1012 void ComputeLiveInAndLiveOutSets();
1013
1014 // Update the live_in set of the block and returns whether it has changed.
1015 bool UpdateLiveIn(const HBasicBlock& block);
1016
1017 // Update the live_out set of the block and returns whether it has changed.
1018 bool UpdateLiveOut(const HBasicBlock& block);
1019
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001020 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
1021 if (instruction == nullptr) return false;
1022 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1023 return instruction->GetType() == Primitive::kPrimNot;
1024 }
1025
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001026 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001027 CodeGenerator* const codegen_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001028 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001029
1030 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001031 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001032
1033 // Temporary array used when inserting moves in the graph.
1034 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001035 size_t number_of_ssa_values_;
1036
1037 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1038};
1039
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001040} // namespace art
1041
1042#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_