blob: fe70d3a861059bc02a5917a71bb6bbfe73216e43 [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.
David Brazdil3fc992f2015-04-16 18:31:55 +0100280 first_range_ = last_range_ = range_search_start_ =
281 new (allocator_) LiveRange(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.
David Brazdil3fc992f2015-04-16 18:31:55 +0100296 first_range_ = range_search_start_ =
297 new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100298 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100299 }
300
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100301 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100302 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100303 first_use_ = new (allocator_) UsePosition(
304 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100305 }
306
307 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100308 if (first_range_ == nullptr) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100309 first_range_ = last_range_ = range_search_start_ =
310 new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100311 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100312 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100313 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100314 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
315 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100316 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100317 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318 // There is a hole in the interval. Create a new range.
David Brazdil3fc992f2015-04-16 18:31:55 +0100319 first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100320 }
321 }
322
323 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100324 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000325 DCHECK_LE(start, first_range_->GetStart());
326 // Find the range that covers the positions after the loop.
327 LiveRange* after_loop = first_range_;
328 LiveRange* last_in_loop = nullptr;
329 while (after_loop != nullptr && after_loop->GetEnd() < end) {
330 DCHECK_LE(start, after_loop->GetStart());
331 last_in_loop = after_loop;
332 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100333 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000334 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100335 // Uses are only in the loop.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700336 first_range_ = last_range_ = range_search_start_ =
337 new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000338 } else if (after_loop->GetStart() <= end) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100339 first_range_ = range_search_start_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100340 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100341 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000342 } else {
343 // The use after the loop is after a lifetime hole.
344 DCHECK(last_in_loop != nullptr);
David Brazdil3fc992f2015-04-16 18:31:55 +0100345 first_range_ = range_search_start_ = last_in_loop;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000346 first_range_->start_ = start;
347 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100348 }
349 }
350
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100351 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100352 void SetSpillSlot(int slot) {
353 DCHECK(!is_fixed_);
354 DCHECK(!is_temp_);
355 spill_slot_ = slot;
356 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100357 int GetSpillSlot() const { return spill_slot_; }
358
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100359 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100360 if (first_range_ != nullptr) {
361 first_range_->start_ = from;
362 } else {
363 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000364 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100365 DCHECK(from == defined_by_->GetLifetimePosition());
David Brazdil3fc992f2015-04-16 18:31:55 +0100366 first_range_ = last_range_ = range_search_start_ =
367 new (allocator_) LiveRange(from, from + 2, nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100368 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100369 }
370
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100371 LiveInterval* GetParent() const { return parent_; }
372
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100373 // Returns whether this interval is the parent interval, that is, the interval
374 // that starts where the HInstruction is defined.
375 bool IsParent() const { return parent_ == this; }
376
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100377 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000378 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100379
380 int GetRegister() const { return register_; }
381 void SetRegister(int reg) { register_ = reg; }
382 void ClearRegister() { register_ = kNoRegister; }
383 bool HasRegister() const { return register_ != kNoRegister; }
384
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100385 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100386 return GetEnd() <= position;
387 }
388
389 bool IsDefinedAt(size_t position) const {
390 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100391 }
392
David Brazdil3fc992f2015-04-16 18:31:55 +0100393 // Returns true if the interval contains a LiveRange covering `position`.
394 // The range at or immediately after the current position of linear scan
395 // is cached for better performance. If `position` can be smaller than
396 // that, CoversSlow should be used instead.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000397 bool Covers(size_t position) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100398 LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_);
399 range_search_start_ = candidate;
400 return (candidate != nullptr && candidate->GetStart() <= position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100401 }
402
David Brazdil3fc992f2015-04-16 18:31:55 +0100403 // Same as Covers but always tests all ranges.
404 bool CoversSlow(size_t position) const {
405 LiveRange* candidate = FindRangeAtOrAfter(position, first_range_);
406 return candidate != nullptr && candidate->GetStart() <= position;
407 }
408
409 // Returns the first intersection of this interval with `current`, which
410 // must be the interval currently being allocated by linear scan.
411 size_t FirstIntersectionWith(LiveInterval* current) const {
412 // Find the first range after the start of `current`. We use the search
413 // cache to improve performance.
414 DCHECK(GetStart() <= current->GetStart() || IsFixed());
415 LiveRange* other_range = current->first_range_;
416 LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_);
417 if (my_range == nullptr) {
418 return kNoLifetime;
419 }
420
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100421 // Advance both intervals and find the first matching range start in
422 // this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100423 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000424 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100425 my_range = my_range->GetNext();
426 if (my_range == nullptr) {
427 return kNoLifetime;
428 }
David Brazdil714e14f2015-02-25 11:57:05 +0000429 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100430 other_range = other_range->GetNext();
431 if (other_range == nullptr) {
432 return kNoLifetime;
433 }
David Brazdil714e14f2015-02-25 11:57:05 +0000434 } else {
435 DCHECK(my_range->IntersectsWith(*other_range));
436 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100437 }
438 } while (true);
439 }
440
441 size_t GetStart() const {
442 return first_range_->GetStart();
443 }
444
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100445 size_t GetEnd() const {
446 return last_range_->GetEnd();
447 }
448
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100449 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100450 if (is_temp_) {
451 return position == GetStart() ? position : kNoLifetime;
452 }
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100453 if (position == GetStart() && IsParent()) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100454 LocationSummary* locations = defined_by_->GetLocations();
455 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100456 // This interval is the first interval of the instruction. If the output
457 // of the instruction requires a register, we return the position of that instruction
458 // as the first register use.
459 if (location.IsUnallocated()) {
460 if ((location.GetPolicy() == Location::kRequiresRegister)
461 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000462 && (locations->InAt(0).IsRegister()
463 || locations->InAt(0).IsRegisterPair()
464 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100465 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100466 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
467 || (location.GetPolicy() == Location::kSameAsFirstInput
468 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
469 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100470 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000471 } else if (location.IsRegister() || location.IsRegisterPair()) {
472 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100473 }
474 }
475
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100476 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100477 size_t end = GetEnd();
478 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100479 size_t use_position = use->GetPosition();
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100480 if (use_position > position && !use->GetIsEnvironment()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100481 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100482 if (location.IsUnallocated()
483 && (location.GetPolicy() == Location::kRequiresRegister
484 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100485 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100486 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100487 }
488 use = use->GetNext();
489 }
490 return kNoLifetime;
491 }
492
493 size_t FirstRegisterUse() const {
494 return FirstRegisterUseAfter(GetStart());
495 }
496
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000497 size_t FirstUseAfter(size_t position) const {
498 if (is_temp_) {
499 return position == GetStart() ? position : kNoLifetime;
500 }
501
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100502 if (position == GetStart() && IsParent()) {
503 if (defined_by_->GetLocations()->Out().IsValid()) {
504 return position;
505 }
506 }
507
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000508 UsePosition* use = first_use_;
509 size_t end = GetEnd();
510 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000511 if (!use->GetIsEnvironment()) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100512 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000513 size_t use_position = use->GetPosition();
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100514 if (use_position > position && location.IsValid()) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000515 return use_position;
516 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000517 }
518 use = use->GetNext();
519 }
520 return kNoLifetime;
521 }
522
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100523 UsePosition* GetFirstUse() const {
524 return first_use_;
525 }
526
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100527 Primitive::Type GetType() const {
528 return type_;
529 }
530
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100531 HInstruction* GetDefinedBy() const {
532 return defined_by_;
533 }
534
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100535 SafepointPosition* FindSafepointJustBefore(size_t position) const {
536 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
537 safepoint != nullptr;
538 previous = safepoint, safepoint = safepoint->GetNext()) {
539 if (safepoint->GetPosition() >= position) return previous;
540 }
541 return last_safepoint_;
542 }
543
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100544 /**
545 * Split this interval at `position`. This interval is changed to:
546 * [start ... position).
547 *
548 * The new interval covers:
549 * [position ... end)
550 */
551 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100552 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100553 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100554 DCHECK_GT(position, GetStart());
555
David Brazdil241a4862015-04-16 17:59:03 +0100556 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100557 // This range dies before `position`, no need to split.
558 return nullptr;
559 }
560
561 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100562 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
563 if (new_last_safepoint == nullptr) {
564 new_interval->first_safepoint_ = first_safepoint_;
565 new_interval->last_safepoint_ = last_safepoint_;
566 first_safepoint_ = last_safepoint_ = nullptr;
567 } else if (last_safepoint_ != new_last_safepoint) {
568 new_interval->last_safepoint_ = last_safepoint_;
569 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
570 DCHECK(new_interval->first_safepoint_ != nullptr);
571 last_safepoint_ = new_last_safepoint;
572 last_safepoint_->SetNext(nullptr);
573 }
574
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100575 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100576 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100577 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100578
579 new_interval->first_use_ = first_use_;
580 LiveRange* current = first_range_;
581 LiveRange* previous = nullptr;
582 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000583 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100584 do {
585 if (position >= current->GetEnd()) {
586 // Move to next range.
587 previous = current;
588 current = current->next_;
589 } else if (position <= current->GetStart()) {
590 // If the previous range did not cover this position, we know position is in
591 // a lifetime hole. We can just break the first_range_ and last_range_ links
592 // and return the new interval.
593 DCHECK(previous != nullptr);
594 DCHECK(current != first_range_);
595 new_interval->last_range_ = last_range_;
596 last_range_ = previous;
597 previous->next_ = nullptr;
598 new_interval->first_range_ = current;
David Brazdil3fc992f2015-04-16 18:31:55 +0100599 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700600 // Search start point is inside `new_interval`. Change it to null
David Brazdil3fc992f2015-04-16 18:31:55 +0100601 // (i.e. the end of the interval) in the original interval.
602 range_search_start_ = nullptr;
603 }
604 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100605 return new_interval;
606 } else {
607 // This range covers position. We create a new last_range_ for this interval
608 // that covers last_range_->Start() and position. We also shorten the current
609 // range and make it the first range of the new interval.
610 DCHECK(position < current->GetEnd() && position > current->GetStart());
611 new_interval->last_range_ = last_range_;
612 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
613 if (previous != nullptr) {
614 previous->next_ = last_range_;
615 } else {
616 first_range_ = last_range_;
617 }
618 new_interval->first_range_ = current;
619 current->start_ = position;
David Brazdil3fc992f2015-04-16 18:31:55 +0100620 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
621 // Search start point is inside `new_interval`. Change it to `last_range`
622 // in the original interval. This is conservative but always correct.
623 range_search_start_ = last_range_;
624 }
625 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100626 return new_interval;
627 }
628 } while (current != nullptr);
629
630 LOG(FATAL) << "Unreachable";
631 return nullptr;
632 }
633
Nicolas Geoffray76905622014-09-25 14:39:26 +0100634 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100635 return GetStart() <= other->GetStart();
636 }
637
638 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100639 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100640 }
641
642 void Dump(std::ostream& stream) const {
643 stream << "ranges: { ";
644 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000645 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100646 current->Dump(stream);
647 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000648 current = current->GetNext();
649 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100650 stream << "}, uses: { ";
651 UsePosition* use = first_use_;
652 if (use != nullptr) {
653 do {
654 use->Dump(stream);
655 stream << " ";
656 } while ((use = use->GetNext()) != nullptr);
657 }
658 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700659 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000660 stream << " is_high: " << IsHighInterval();
661 stream << " is_low: " << IsLowInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100662 }
663
664 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000665 LiveInterval* GetLastSibling() {
666 LiveInterval* result = this;
667 while (result->next_sibling_ != nullptr) {
668 result = result->next_sibling_;
669 }
670 return result;
671 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100672
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100673 // Returns the first register hint that is at least free before
674 // the value contained in `free_until`. If none is found, returns
675 // `kNoRegister`.
676 int FindFirstRegisterHint(size_t* free_until) const;
677
678 // If there is enough at the definition site to find a register (for example
679 // it uses the same input as the first input), returns the register as a hint.
680 // Returns kNoRegister otherwise.
681 int FindHintAtDefinition() const;
682
683 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
684 // slots for spilling.
685 bool NeedsTwoSpillSlots() const;
686
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100687 bool IsFloatingPoint() const {
688 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
689 }
690
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100691 // Converts the location of the interval to a `Location` object.
692 Location ToLocation() const;
693
694 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000695 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100696
David Brazdil241a4862015-04-16 17:59:03 +0100697 // Finds the sibling that is defined at `position`.
698 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100699
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100700 // Returns whether `other` and `this` share the same kind of register.
701 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000702 bool SameRegisterKind(const LiveInterval& other) const {
703 return IsFloatingPoint() == other.IsFloatingPoint();
704 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100705
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000706 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000707 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000708 }
709
710 bool HasLowInterval() const {
711 return IsHighInterval();
712 }
713
714 LiveInterval* GetLowInterval() const {
715 DCHECK(HasLowInterval());
716 return high_or_low_interval_;
717 }
718
719 LiveInterval* GetHighInterval() const {
720 DCHECK(HasHighInterval());
721 return high_or_low_interval_;
722 }
723
724 bool IsHighInterval() const {
725 return GetParent()->is_high_interval_;
726 }
727
728 bool IsLowInterval() const {
729 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
730 }
731
732 void SetLowInterval(LiveInterval* low) {
733 DCHECK(IsHighInterval());
734 high_or_low_interval_ = low;
735 }
736
737 void SetHighInterval(LiveInterval* high) {
738 DCHECK(IsLowInterval());
739 high_or_low_interval_ = high;
740 }
741
742 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100743 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000744 DCHECK(!HasHighInterval());
745 DCHECK(!HasLowInterval());
746 high_or_low_interval_ = new (allocator_) LiveInterval(
747 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
748 high_or_low_interval_->high_or_low_interval_ = this;
749 if (first_range_ != nullptr) {
750 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
David Brazdilc08675c2015-04-17 15:49:51 +0100751 high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange();
David Brazdil3fc992f2015-04-16 18:31:55 +0100752 high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000753 }
754 if (first_use_ != nullptr) {
755 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
756 }
757 }
758
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000759 // Returns whether an interval, when it is non-split, is using
760 // the same register of one of its input.
761 bool IsUsingInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100762 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000763 if (defined_by_ != nullptr && !IsSplit()) {
764 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
765 LiveInterval* interval = it.Current()->GetLiveInterval();
766
David Brazdil3fc992f2015-04-16 18:31:55 +0100767 // Find the interval that covers `defined_by`_. Calls to this function
768 // are made outside the linear scan, hence we need to use CoversSlow.
769 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000770 interval = interval->GetNextSibling();
771 }
772
773 // Check if both intervals have the same register of the same kind.
774 if (interval != nullptr
775 && interval->SameRegisterKind(*this)
776 && interval->GetRegister() == GetRegister()) {
777 return true;
778 }
779 }
780 }
781 return false;
782 }
783
784 // Returns whether an interval, when it is non-split, can safely use
785 // the same register of one of its input. Note that this method requires
786 // IsUsingInputRegister() to be true.
787 bool CanUseInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100788 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000789 DCHECK(IsUsingInputRegister());
790 if (defined_by_ != nullptr && !IsSplit()) {
791 LocationSummary* locations = defined_by_->GetLocations();
792 if (locations->OutputCanOverlapWithInputs()) {
793 return false;
794 }
795 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
796 LiveInterval* interval = it.Current()->GetLiveInterval();
797
David Brazdil3fc992f2015-04-16 18:31:55 +0100798 // Find the interval that covers `defined_by`_. Calls to this function
799 // are made outside the linear scan, hence we need to use CoversSlow.
800 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000801 interval = interval->GetNextSibling();
802 }
803
804 if (interval != nullptr
805 && interval->SameRegisterKind(*this)
806 && interval->GetRegister() == GetRegister()) {
807 // We found the input that has the same register. Check if it is live after
808 // `defined_by`_.
David Brazdil3fc992f2015-04-16 18:31:55 +0100809 return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000810 }
811 }
812 }
813 LOG(FATAL) << "Unreachable";
814 UNREACHABLE();
815 }
816
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100817 void AddSafepoint(HInstruction* instruction) {
818 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
819 if (first_safepoint_ == nullptr) {
820 first_safepoint_ = last_safepoint_ = safepoint;
821 } else {
822 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
823 last_safepoint_->SetNext(safepoint);
824 last_safepoint_ = safepoint;
825 }
826 }
827
828 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100829 return first_safepoint_;
830 }
831
David Brazdil3fc992f2015-04-16 18:31:55 +0100832 // Resets the starting point for range-searching queries to the first range.
833 // Intervals must be reset prior to starting a new linear scan over them.
834 void ResetSearchCache() {
835 range_search_start_ = first_range_;
836 }
837
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100838 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700839 LiveInterval(ArenaAllocator* allocator,
840 Primitive::Type type,
841 HInstruction* defined_by = nullptr,
842 bool is_fixed = false,
843 int reg = kNoRegister,
844 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000845 bool is_slow_path_safepoint = false,
846 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700847 : allocator_(allocator),
848 first_range_(nullptr),
849 last_range_(nullptr),
David Brazdil3fc992f2015-04-16 18:31:55 +0100850 range_search_start_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100851 first_safepoint_(nullptr),
852 last_safepoint_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700853 first_use_(nullptr),
854 type_(type),
855 next_sibling_(nullptr),
856 parent_(this),
857 register_(reg),
858 spill_slot_(kNoSpillSlot),
859 is_fixed_(is_fixed),
860 is_temp_(is_temp),
861 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000862 is_high_interval_(is_high_interval),
863 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700864 defined_by_(defined_by) {}
865
David Brazdil3fc992f2015-04-16 18:31:55 +0100866 // Searches for a LiveRange that either covers the given position or is the
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700867 // first next LiveRange. Returns null if no such LiveRange exists. Ranges
David Brazdil3fc992f2015-04-16 18:31:55 +0100868 // known to end before `position` can be skipped with `search_start`.
869 LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000870 if (kIsDebugBuild) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100871 if (search_start != first_range_) {
872 // If we are not searching the entire list of ranges, make sure we do
873 // not skip the range we are searching for.
874 if (search_start == nullptr) {
875 DCHECK(IsDeadAt(position));
876 } else if (search_start->GetStart() > position) {
877 DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_));
878 }
David Brazdil5b8e6a52015-02-25 16:17:05 +0000879 }
880 }
881
David Brazdil3fc992f2015-04-16 18:31:55 +0100882 LiveRange* range;
883 for (range = search_start;
884 range != nullptr && range->GetEnd() <= position;
885 range = range->GetNext()) {
886 continue;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000887 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100888 return range;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000889 }
890
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100891 ArenaAllocator* const allocator_;
892
893 // Ranges of this interval. We need a quick access to the last range to test
894 // for liveness (see `IsDeadAt`).
895 LiveRange* first_range_;
896 LiveRange* last_range_;
897
David Brazdil3fc992f2015-04-16 18:31:55 +0100898 // The first range at or after the current position of a linear scan. It is
899 // used to optimize range-searching queries.
900 LiveRange* range_search_start_;
901
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100902 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100903 SafepointPosition* first_safepoint_;
904 SafepointPosition* last_safepoint_;
905
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100906 // Uses of this interval. Note that this linked list is shared amongst siblings.
907 UsePosition* first_use_;
908
909 // The instruction type this interval corresponds to.
910 const Primitive::Type type_;
911
912 // Live interval that is the result of a split.
913 LiveInterval* next_sibling_;
914
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100915 // The first interval from which split intervals come from.
916 LiveInterval* parent_;
917
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100918 // The register allocated to this interval.
919 int register_;
920
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100921 // The spill slot allocated to this interval.
922 int spill_slot_;
923
924 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100925 const bool is_fixed_;
926
927 // Whether the interval is for a temporary.
928 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100929
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100930 // Whether the interval is for a safepoint that calls on slow path.
931 const bool is_slow_path_safepoint_;
932
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000933 // Whether this interval is a synthesized interval for register pair.
934 const bool is_high_interval_;
935
936 // If this interval needs a register pair, the high or low equivalent.
937 // `is_high_interval_` tells whether this holds the low or the high.
938 LiveInterval* high_or_low_interval_;
939
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100940 // The instruction represented by this interval.
941 HInstruction* const defined_by_;
942
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100943 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100944 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100945
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000946 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
947
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100948 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
949};
950
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000951/**
952 * Analysis that computes the liveness of instructions:
953 *
954 * (a) Non-environment uses of an instruction always make
955 * the instruction live.
956 * (b) Environment uses of an instruction whose type is
957 * object (that is, non-primitive), make the instruction live.
958 * This is due to having to keep alive objects that have
959 * finalizers deleting native objects.
960 * (c) When the graph has the debuggable property, environment uses
961 * of an instruction that has a primitive type make the instruction live.
962 * If the graph does not have the debuggable property, the environment
963 * use has no effect, and may get a 'none' value after register allocation.
964 *
965 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
966 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100967class SsaLivenessAnalysis : public ValueObject {
968 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100969 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100970 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100971 codegen_(codegen),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100972 block_infos_(graph->GetArena(), graph->GetBlocks().Size()),
973 instructions_from_ssa_index_(graph->GetArena(), 0),
974 instructions_from_lifetime_position_(graph->GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100975 number_of_ssa_values_(0) {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100976 block_infos_.SetSize(graph->GetBlocks().Size());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100977 }
978
979 void Analyze();
980
981 BitVector* GetLiveInSet(const HBasicBlock& block) const {
982 return &block_infos_.Get(block.GetBlockId())->live_in_;
983 }
984
985 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
986 return &block_infos_.Get(block.GetBlockId())->live_out_;
987 }
988
989 BitVector* GetKillSet(const HBasicBlock& block) const {
990 return &block_infos_.Get(block.GetBlockId())->kill_;
991 }
992
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100993 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100994 return instructions_from_ssa_index_.Get(index);
995 }
996
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100997 HInstruction* GetInstructionFromPosition(size_t index) const {
998 return instructions_from_lifetime_position_.Get(index);
999 }
1000
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001001 HInstruction* GetTempUser(LiveInterval* temp) const {
1002 // A temporary shares the same lifetime start as the instruction that requires it.
1003 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001004 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
1005 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
1006 return user;
1007 }
1008
1009 size_t GetTempIndex(LiveInterval* temp) const {
1010 // We use the input index to store the index of the temporary in the user's temporary list.
1011 DCHECK(temp->IsTemp());
1012 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001013 }
1014
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001015 size_t GetMaxLifetimePosition() const {
1016 return instructions_from_lifetime_position_.Size() * 2 - 1;
1017 }
1018
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001019 size_t GetNumberOfSsaValues() const {
1020 return number_of_ssa_values_;
1021 }
1022
Andreas Gampe7c3952f2015-02-19 18:21:24 -08001023 static constexpr const char* kLivenessPassName = "liveness";
1024
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001025 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +01001026 // Linearize the graph so that:
1027 // (1): a block is always after its dominator,
1028 // (2): blocks of loops are contiguous.
1029 // This creates a natural and efficient ordering when visualizing live ranges.
1030 void LinearizeGraph();
1031
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001032 // Give an SSA number to each instruction that defines a value used by another instruction,
1033 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001034 void NumberInstructions();
1035
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001036 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1037 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001038
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001039 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1040 // kill sets, that do not take into account backward branches.
1041 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001042
1043 // After computing the initial sets, this method does a fixed point
1044 // calculation over the live_in and live_out set to take into account
1045 // backwards branches.
1046 void ComputeLiveInAndLiveOutSets();
1047
1048 // Update the live_in set of the block and returns whether it has changed.
1049 bool UpdateLiveIn(const HBasicBlock& block);
1050
1051 // Update the live_out set of the block and returns whether it has changed.
1052 bool UpdateLiveOut(const HBasicBlock& block);
1053
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001054 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
1055 if (instruction == nullptr) return false;
1056 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1057 return instruction->GetType() == Primitive::kPrimNot;
1058 }
1059
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001060 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001061 CodeGenerator* const codegen_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001062 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001063
1064 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001065 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001066
1067 // Temporary array used when inserting moves in the graph.
1068 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001069 size_t number_of_ssa_values_;
1070
1071 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1072};
1073
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001074} // namespace art
1075
1076#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_