blob: b550d8ad8e28ba3e8e71b864d8aed57b02a787c3 [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;
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +010026class SsaLivenessAnalysis;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010027
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010028static constexpr int kNoRegister = -1;
29
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070030class BlockInfo : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010031 public:
32 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
33 : block_(block),
34 live_in_(allocator, number_of_ssa_values, false),
35 live_out_(allocator, number_of_ssa_values, false),
36 kill_(allocator, number_of_ssa_values, false) {
Ian Rogerscf7f1912014-10-22 22:06:39 -070037 UNUSED(block_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010038 live_in_.ClearAllBits();
39 live_out_.ClearAllBits();
40 kill_.ClearAllBits();
41 }
42
43 private:
44 const HBasicBlock& block_;
45 ArenaBitVector live_in_;
46 ArenaBitVector live_out_;
47 ArenaBitVector kill_;
48
49 friend class SsaLivenessAnalysis;
50
51 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
52};
53
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010054/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056 * is live.
57 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070058class LiveRange FINAL : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010060 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010062 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010063 }
64
65 size_t GetStart() const { return start_; }
66 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010067 LiveRange* GetNext() const { return next_; }
68
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070069 bool IntersectsWith(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010070 return (start_ >= other.start_ && start_ < other.end_)
71 || (other.start_ >= start_ && other.start_ < end_);
72 }
73
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070074 bool IsBefore(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010075 return end_ <= other.start_;
76 }
77
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070078 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010079 stream << "[" << start_ << ", " << end_ << ")";
80 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010081
Nicolas Geoffray840e5462015-01-07 16:01:24 +000082 LiveRange* Dup(ArenaAllocator* allocator) const {
83 return new (allocator) LiveRange(
84 start_, end_, next_ == nullptr ? nullptr : next_->Dup(allocator));
85 }
86
87 LiveRange* GetLastRange() {
88 return next_ == nullptr ? this : next_->GetLastRange();
89 }
90
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010091 private:
92 size_t start_;
Nicolas Geoffray76905622014-09-25 14:39:26 +010093 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010094 LiveRange* next_;
95
96 friend class LiveInterval;
97
98 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010099};
100
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100101/**
102 * A use position represents a live interval use at a given position.
103 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104class UsePosition : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100105 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100106 UsePosition(HInstruction* user,
107 size_t input_index,
108 bool is_environment,
109 size_t position,
110 UsePosition* next)
111 : user_(user),
112 input_index_(input_index),
113 is_environment_(is_environment),
114 position_(position),
115 next_(next) {
Nicolas Geoffray57902602015-04-21 14:28:41 +0100116 DCHECK((user == nullptr)
117 || user->IsPhi()
Nicolas Geoffray76905622014-09-25 14:39:26 +0100118 || (GetPosition() == user->GetLifetimePosition() + 1)
119 || (GetPosition() == user->GetLifetimePosition()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100120 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
121 }
122
Nicolas Geoffray57902602015-04-21 14:28:41 +0100123 static constexpr size_t kNoInput = -1;
124
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100125 size_t GetPosition() const { return position_; }
126
127 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100128 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100129
130 HInstruction* GetUser() const { return user_; }
131
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100132 bool GetIsEnvironment() const { return is_environment_; }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100133 bool IsSynthesized() const { return user_ == nullptr; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100134
135 size_t GetInputIndex() const { return input_index_; }
136
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100137 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100138 stream << position_;
Nicolas Geoffray57902602015-04-21 14:28:41 +0100139 }
140
141 HLoopInformation* GetLoopInformation() const {
142 return user_->GetBlock()->GetLoopInformation();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100143 }
144
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000145 UsePosition* Dup(ArenaAllocator* allocator) const {
146 return new (allocator) UsePosition(
147 user_, input_index_, is_environment_, position_,
148 next_ == nullptr ? nullptr : next_->Dup(allocator));
149 }
150
Nicolas Geoffray57902602015-04-21 14:28:41 +0100151 bool RequiresRegister() const {
152 if (GetIsEnvironment()) return false;
153 if (IsSynthesized()) return false;
154 Location location = GetUser()->GetLocations()->InAt(GetInputIndex());
155 return location.IsUnallocated()
156 && (location.GetPolicy() == Location::kRequiresRegister
157 || location.GetPolicy() == Location::kRequiresFpuRegister);
158 }
159
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100160 private:
161 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100162 const size_t input_index_;
163 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100164 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100165 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100166
167 DISALLOW_COPY_AND_ASSIGN(UsePosition);
168};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100169
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100170class SafepointPosition : public ArenaObject<kArenaAllocMisc> {
171 public:
172 explicit SafepointPosition(HInstruction* instruction)
173 : instruction_(instruction),
174 next_(nullptr) {}
175
176 void SetNext(SafepointPosition* next) {
177 next_ = next;
178 }
179
180 size_t GetPosition() const {
181 return instruction_->GetLifetimePosition();
182 }
183
184 SafepointPosition* GetNext() const {
185 return next_;
186 }
187
188 LocationSummary* GetLocations() const {
189 return instruction_->GetLocations();
190 }
191
192 HInstruction* GetInstruction() const {
193 return instruction_;
194 }
195
196 private:
197 HInstruction* const instruction_;
198 SafepointPosition* next_;
199
200 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
201};
202
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100203/**
204 * An interval is a list of disjoint live ranges where an instruction is live.
205 * Each instruction that has uses gets an interval.
206 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700207class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100208 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700209 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
210 Primitive::Type type,
211 HInstruction* instruction = nullptr) {
212 return new (allocator) LiveInterval(allocator, type, instruction);
213 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100214
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100215 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
216 return new (allocator) LiveInterval(
217 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
218 }
219
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100220 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100221 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
222 }
223
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100224 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
225 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100226 }
227
228 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700229 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100230 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700231 // This interval is the result of a split.
232 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100233
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000234 void AddTempUse(HInstruction* instruction, size_t temp_index) {
235 DCHECK(IsTemp());
236 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100237 DCHECK(first_env_use_ == nullptr) << "A temporary cannot have environment user";
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000238 size_t position = instruction->GetLifetimePosition();
239 first_use_ = new (allocator_) UsePosition(
240 instruction, temp_index, /* is_environment */ false, position, first_use_);
241 AddRange(position, position + 1);
242 }
243
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000244 void AddUse(HInstruction* instruction,
245 size_t input_index,
246 bool is_environment,
247 bool keep_alive = false) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100248 // Set the use within the instruction.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000249 size_t position = instruction->GetLifetimePosition() + 1;
250 LocationSummary* locations = instruction->GetLocations();
251 if (!is_environment) {
252 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
253 // For fixed inputs and output same as input, the register allocator
254 // requires to have inputs die at the instruction, so that input moves use the
255 // location of the input just before that instruction (and not potential moves due
256 // to splitting).
257 position = instruction->GetLifetimePosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100258 } else if (!locations->InAt(input_index).IsValid()) {
259 return;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000260 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100261 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000262
Nicolas Geoffray57902602015-04-21 14:28:41 +0100263 if (!is_environment && instruction->IsInLoop()) {
264 AddBackEdgeUses(*instruction->GetBlock());
265 }
266
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000267 DCHECK(position == instruction->GetLifetimePosition()
268 || position == instruction->GetLifetimePosition() + 1);
269
Nicolas Geoffray76905622014-09-25 14:39:26 +0100270 if ((first_use_ != nullptr)
271 && (first_use_->GetUser() == instruction)
272 && (first_use_->GetPosition() < position)) {
273 // The user uses the instruction multiple times, and one use dies before the other.
274 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000275 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100276 UsePosition* cursor = first_use_;
277 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
278 cursor = cursor->GetNext();
279 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100280 DCHECK(first_use_->GetPosition() + 1 == position);
281 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100282 instruction, input_index, is_environment, position, cursor->GetNext());
283 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100284 if (first_range_->GetEnd() == first_use_->GetPosition()) {
285 first_range_->end_ = position;
286 }
287 return;
288 }
289
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100290 if (is_environment) {
291 first_env_use_ = new (allocator_) UsePosition(
292 instruction, input_index, is_environment, position, first_env_use_);
293 } else {
294 first_use_ = new (allocator_) UsePosition(
295 instruction, input_index, is_environment, position, first_use_);
296 }
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000297
298 if (is_environment && !keep_alive) {
299 // If this environment use does not keep the instruction live, it does not
300 // affect the live range of that instruction.
301 return;
302 }
303
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100304 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100305 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100306 // First time we see a use of that interval.
David Brazdil3fc992f2015-04-16 18:31:55 +0100307 first_range_ = last_range_ = range_search_start_ =
308 new (allocator_) LiveRange(start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100309 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100310 // There is a use later in the same block or in a following block.
311 // Note that in such a case, `AddRange` for the whole blocks has been called
312 // before arriving in this method, and this is the reason the start of
313 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100314 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100315 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100316 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100317 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100318 // Note that the start of `first_range_` can be equal to `end`: two blocks
319 // having adjacent lifetime positions are not necessarily
320 // predecessor/successor. When two blocks are predecessor/successor, the
321 // liveness algorithm has called `AddRange` before arriving in this method,
322 // and the check line 205 would succeed.
David Brazdil3fc992f2015-04-16 18:31:55 +0100323 first_range_ = range_search_start_ =
324 new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100325 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100326 }
327
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100328 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100329 DCHECK(instruction->IsPhi());
Nicolas Geoffray57902602015-04-21 14:28:41 +0100330 if (block->IsInLoop()) {
331 AddBackEdgeUses(*block);
332 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100333 first_use_ = new (allocator_) UsePosition(
334 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100335 }
336
337 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100338 if (first_range_ == nullptr) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100339 first_range_ = last_range_ = range_search_start_ =
340 new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100341 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100342 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100343 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100344 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
345 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100346 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100347 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100348 // There is a hole in the interval. Create a new range.
David Brazdil3fc992f2015-04-16 18:31:55 +0100349 first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100350 }
351 }
352
353 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100354 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000355 DCHECK_LE(start, first_range_->GetStart());
356 // Find the range that covers the positions after the loop.
357 LiveRange* after_loop = first_range_;
358 LiveRange* last_in_loop = nullptr;
359 while (after_loop != nullptr && after_loop->GetEnd() < end) {
360 DCHECK_LE(start, after_loop->GetStart());
361 last_in_loop = after_loop;
362 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100363 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000364 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100365 // Uses are only in the loop.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700366 first_range_ = last_range_ = range_search_start_ =
367 new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000368 } else if (after_loop->GetStart() <= end) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100369 first_range_ = range_search_start_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100370 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100371 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000372 } else {
373 // The use after the loop is after a lifetime hole.
374 DCHECK(last_in_loop != nullptr);
David Brazdil3fc992f2015-04-16 18:31:55 +0100375 first_range_ = range_search_start_ = last_in_loop;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000376 first_range_->start_ = start;
377 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100378 }
379 }
380
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100381 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100382 void SetSpillSlot(int slot) {
383 DCHECK(!is_fixed_);
384 DCHECK(!is_temp_);
385 spill_slot_ = slot;
386 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100387 int GetSpillSlot() const { return spill_slot_; }
388
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100389 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100390 if (first_range_ != nullptr) {
391 first_range_->start_ = from;
392 } else {
393 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000394 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100395 DCHECK(from == defined_by_->GetLifetimePosition());
David Brazdil3fc992f2015-04-16 18:31:55 +0100396 first_range_ = last_range_ = range_search_start_ =
397 new (allocator_) LiveRange(from, from + 2, nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100398 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100399 }
400
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100401 LiveInterval* GetParent() const { return parent_; }
402
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100403 // Returns whether this interval is the parent interval, that is, the interval
404 // that starts where the HInstruction is defined.
405 bool IsParent() const { return parent_ == this; }
406
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100407 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000408 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100409
410 int GetRegister() const { return register_; }
411 void SetRegister(int reg) { register_ = reg; }
412 void ClearRegister() { register_ = kNoRegister; }
413 bool HasRegister() const { return register_ != kNoRegister; }
414
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100415 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100416 return GetEnd() <= position;
417 }
418
419 bool IsDefinedAt(size_t position) const {
420 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100421 }
422
David Brazdil3fc992f2015-04-16 18:31:55 +0100423 // Returns true if the interval contains a LiveRange covering `position`.
424 // The range at or immediately after the current position of linear scan
425 // is cached for better performance. If `position` can be smaller than
426 // that, CoversSlow should be used instead.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000427 bool Covers(size_t position) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100428 LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_);
429 range_search_start_ = candidate;
430 return (candidate != nullptr && candidate->GetStart() <= position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100431 }
432
David Brazdil3fc992f2015-04-16 18:31:55 +0100433 // Same as Covers but always tests all ranges.
434 bool CoversSlow(size_t position) const {
435 LiveRange* candidate = FindRangeAtOrAfter(position, first_range_);
436 return candidate != nullptr && candidate->GetStart() <= position;
437 }
438
439 // Returns the first intersection of this interval with `current`, which
440 // must be the interval currently being allocated by linear scan.
441 size_t FirstIntersectionWith(LiveInterval* current) const {
442 // Find the first range after the start of `current`. We use the search
443 // cache to improve performance.
444 DCHECK(GetStart() <= current->GetStart() || IsFixed());
445 LiveRange* other_range = current->first_range_;
446 LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_);
447 if (my_range == nullptr) {
448 return kNoLifetime;
449 }
450
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100451 // Advance both intervals and find the first matching range start in
452 // this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100453 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000454 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100455 my_range = my_range->GetNext();
456 if (my_range == nullptr) {
457 return kNoLifetime;
458 }
David Brazdil714e14f2015-02-25 11:57:05 +0000459 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100460 other_range = other_range->GetNext();
461 if (other_range == nullptr) {
462 return kNoLifetime;
463 }
David Brazdil714e14f2015-02-25 11:57:05 +0000464 } else {
465 DCHECK(my_range->IntersectsWith(*other_range));
466 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100467 }
468 } while (true);
469 }
470
471 size_t GetStart() const {
472 return first_range_->GetStart();
473 }
474
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100475 size_t GetEnd() const {
476 return last_range_->GetEnd();
477 }
478
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100479 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100480 if (is_temp_) {
481 return position == GetStart() ? position : kNoLifetime;
482 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100483
484 if (IsDefiningPosition(position) && DefinitionRequiresRegister()) {
485 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100486 }
487
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100488 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100489 size_t end = GetEnd();
490 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100491 size_t use_position = use->GetPosition();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100492 if (use_position > position) {
Nicolas Geoffray57902602015-04-21 14:28:41 +0100493 if (use->RequiresRegister()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100494 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100495 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100496 }
497 use = use->GetNext();
498 }
499 return kNoLifetime;
500 }
501
502 size_t FirstRegisterUse() const {
503 return FirstRegisterUseAfter(GetStart());
504 }
505
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000506 size_t FirstUseAfter(size_t position) const {
507 if (is_temp_) {
508 return position == GetStart() ? position : kNoLifetime;
509 }
510
Nicolas Geoffray57902602015-04-21 14:28:41 +0100511 if (IsDefiningPosition(position)) {
512 DCHECK(defined_by_->GetLocations()->Out().IsValid());
513 return position;
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100514 }
515
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000516 UsePosition* use = first_use_;
517 size_t end = GetEnd();
518 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100519 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100520 if (use_position > position) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100521 return use_position;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000522 }
523 use = use->GetNext();
524 }
525 return kNoLifetime;
526 }
527
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100528 UsePosition* GetFirstUse() const {
529 return first_use_;
530 }
531
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100532 UsePosition* GetFirstEnvironmentUse() const {
533 return first_env_use_;
534 }
535
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100536 Primitive::Type GetType() const {
537 return type_;
538 }
539
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100540 HInstruction* GetDefinedBy() const {
541 return defined_by_;
542 }
543
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100544 SafepointPosition* FindSafepointJustBefore(size_t position) const {
545 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
546 safepoint != nullptr;
547 previous = safepoint, safepoint = safepoint->GetNext()) {
548 if (safepoint->GetPosition() >= position) return previous;
549 }
550 return last_safepoint_;
551 }
552
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100553 /**
554 * Split this interval at `position`. This interval is changed to:
555 * [start ... position).
556 *
557 * The new interval covers:
558 * [position ... end)
559 */
560 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100561 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100562 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100563 DCHECK_GT(position, GetStart());
564
David Brazdil241a4862015-04-16 17:59:03 +0100565 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100566 // This range dies before `position`, no need to split.
567 return nullptr;
568 }
569
570 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100571 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
572 if (new_last_safepoint == nullptr) {
573 new_interval->first_safepoint_ = first_safepoint_;
574 new_interval->last_safepoint_ = last_safepoint_;
575 first_safepoint_ = last_safepoint_ = nullptr;
576 } else if (last_safepoint_ != new_last_safepoint) {
577 new_interval->last_safepoint_ = last_safepoint_;
578 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
579 DCHECK(new_interval->first_safepoint_ != nullptr);
580 last_safepoint_ = new_last_safepoint;
581 last_safepoint_->SetNext(nullptr);
582 }
583
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100584 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100585 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100586 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100587
588 new_interval->first_use_ = first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100589 new_interval->first_env_use_ = first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100590 LiveRange* current = first_range_;
591 LiveRange* previous = nullptr;
592 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000593 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100594 do {
595 if (position >= current->GetEnd()) {
596 // Move to next range.
597 previous = current;
598 current = current->next_;
599 } else if (position <= current->GetStart()) {
600 // If the previous range did not cover this position, we know position is in
601 // a lifetime hole. We can just break the first_range_ and last_range_ links
602 // and return the new interval.
603 DCHECK(previous != nullptr);
604 DCHECK(current != first_range_);
605 new_interval->last_range_ = last_range_;
606 last_range_ = previous;
607 previous->next_ = nullptr;
608 new_interval->first_range_ = current;
David Brazdil3fc992f2015-04-16 18:31:55 +0100609 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700610 // Search start point is inside `new_interval`. Change it to null
David Brazdil3fc992f2015-04-16 18:31:55 +0100611 // (i.e. the end of the interval) in the original interval.
612 range_search_start_ = nullptr;
613 }
614 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100615 return new_interval;
616 } else {
617 // This range covers position. We create a new last_range_ for this interval
618 // that covers last_range_->Start() and position. We also shorten the current
619 // range and make it the first range of the new interval.
620 DCHECK(position < current->GetEnd() && position > current->GetStart());
621 new_interval->last_range_ = last_range_;
622 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
623 if (previous != nullptr) {
624 previous->next_ = last_range_;
625 } else {
626 first_range_ = last_range_;
627 }
628 new_interval->first_range_ = current;
629 current->start_ = position;
David Brazdil3fc992f2015-04-16 18:31:55 +0100630 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
631 // Search start point is inside `new_interval`. Change it to `last_range`
632 // in the original interval. This is conservative but always correct.
633 range_search_start_ = last_range_;
634 }
635 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100636 return new_interval;
637 }
638 } while (current != nullptr);
639
640 LOG(FATAL) << "Unreachable";
641 return nullptr;
642 }
643
Nicolas Geoffray76905622014-09-25 14:39:26 +0100644 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100645 return GetStart() <= other->GetStart();
646 }
647
648 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100649 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100650 }
651
652 void Dump(std::ostream& stream) const {
653 stream << "ranges: { ";
654 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000655 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100656 current->Dump(stream);
657 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000658 current = current->GetNext();
659 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100660 stream << "}, uses: { ";
661 UsePosition* use = first_use_;
662 if (use != nullptr) {
663 do {
664 use->Dump(stream);
665 stream << " ";
666 } while ((use = use->GetNext()) != nullptr);
667 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100668 stream << "}, { ";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100669 use = first_env_use_;
670 if (use != nullptr) {
671 do {
672 use->Dump(stream);
673 stream << " ";
674 } while ((use = use->GetNext()) != nullptr);
675 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100676 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700677 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000678 stream << " is_low: " << IsLowInterval();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100679 stream << " is_high: " << IsHighInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100680 }
681
682 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000683 LiveInterval* GetLastSibling() {
684 LiveInterval* result = this;
685 while (result->next_sibling_ != nullptr) {
686 result = result->next_sibling_;
687 }
688 return result;
689 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100690
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100691 // Returns the first register hint that is at least free before
692 // the value contained in `free_until`. If none is found, returns
693 // `kNoRegister`.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100694 int FindFirstRegisterHint(size_t* free_until, const SsaLivenessAnalysis& liveness) const;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100695
696 // If there is enough at the definition site to find a register (for example
697 // it uses the same input as the first input), returns the register as a hint.
698 // Returns kNoRegister otherwise.
699 int FindHintAtDefinition() const;
700
701 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
702 // slots for spilling.
703 bool NeedsTwoSpillSlots() const;
704
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100705 bool IsFloatingPoint() const {
706 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
707 }
708
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100709 // Converts the location of the interval to a `Location` object.
710 Location ToLocation() const;
711
712 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000713 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100714
David Brazdil241a4862015-04-16 17:59:03 +0100715 // Finds the sibling that is defined at `position`.
716 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100717
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100718 // Returns whether `other` and `this` share the same kind of register.
719 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000720 bool SameRegisterKind(const LiveInterval& other) const {
721 return IsFloatingPoint() == other.IsFloatingPoint();
722 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100723
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000724 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000725 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000726 }
727
728 bool HasLowInterval() const {
729 return IsHighInterval();
730 }
731
732 LiveInterval* GetLowInterval() const {
733 DCHECK(HasLowInterval());
734 return high_or_low_interval_;
735 }
736
737 LiveInterval* GetHighInterval() const {
738 DCHECK(HasHighInterval());
739 return high_or_low_interval_;
740 }
741
742 bool IsHighInterval() const {
743 return GetParent()->is_high_interval_;
744 }
745
746 bool IsLowInterval() const {
747 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
748 }
749
750 void SetLowInterval(LiveInterval* low) {
751 DCHECK(IsHighInterval());
752 high_or_low_interval_ = low;
753 }
754
755 void SetHighInterval(LiveInterval* high) {
756 DCHECK(IsLowInterval());
757 high_or_low_interval_ = high;
758 }
759
760 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100761 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000762 DCHECK(!HasHighInterval());
763 DCHECK(!HasLowInterval());
764 high_or_low_interval_ = new (allocator_) LiveInterval(
765 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
766 high_or_low_interval_->high_or_low_interval_ = this;
767 if (first_range_ != nullptr) {
768 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
David Brazdilc08675c2015-04-17 15:49:51 +0100769 high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange();
David Brazdil3fc992f2015-04-16 18:31:55 +0100770 high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000771 }
772 if (first_use_ != nullptr) {
773 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
774 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100775
776 if (first_env_use_ != nullptr) {
777 high_or_low_interval_->first_env_use_ = first_env_use_->Dup(allocator_);
778 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000779 }
780
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000781 // Returns whether an interval, when it is non-split, is using
782 // the same register of one of its input.
783 bool IsUsingInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100784 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000785 if (defined_by_ != nullptr && !IsSplit()) {
786 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
787 LiveInterval* interval = it.Current()->GetLiveInterval();
788
David Brazdil3fc992f2015-04-16 18:31:55 +0100789 // Find the interval that covers `defined_by`_. Calls to this function
790 // are made outside the linear scan, hence we need to use CoversSlow.
791 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000792 interval = interval->GetNextSibling();
793 }
794
795 // Check if both intervals have the same register of the same kind.
796 if (interval != nullptr
797 && interval->SameRegisterKind(*this)
798 && interval->GetRegister() == GetRegister()) {
799 return true;
800 }
801 }
802 }
803 return false;
804 }
805
806 // Returns whether an interval, when it is non-split, can safely use
807 // the same register of one of its input. Note that this method requires
808 // IsUsingInputRegister() to be true.
809 bool CanUseInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100810 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000811 DCHECK(IsUsingInputRegister());
812 if (defined_by_ != nullptr && !IsSplit()) {
813 LocationSummary* locations = defined_by_->GetLocations();
814 if (locations->OutputCanOverlapWithInputs()) {
815 return false;
816 }
817 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
818 LiveInterval* interval = it.Current()->GetLiveInterval();
819
David Brazdil3fc992f2015-04-16 18:31:55 +0100820 // Find the interval that covers `defined_by`_. Calls to this function
821 // are made outside the linear scan, hence we need to use CoversSlow.
822 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000823 interval = interval->GetNextSibling();
824 }
825
826 if (interval != nullptr
827 && interval->SameRegisterKind(*this)
828 && interval->GetRegister() == GetRegister()) {
829 // We found the input that has the same register. Check if it is live after
830 // `defined_by`_.
David Brazdil3fc992f2015-04-16 18:31:55 +0100831 return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000832 }
833 }
834 }
835 LOG(FATAL) << "Unreachable";
836 UNREACHABLE();
837 }
838
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100839 void AddSafepoint(HInstruction* instruction) {
840 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
841 if (first_safepoint_ == nullptr) {
842 first_safepoint_ = last_safepoint_ = safepoint;
843 } else {
844 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
845 last_safepoint_->SetNext(safepoint);
846 last_safepoint_ = safepoint;
847 }
848 }
849
850 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100851 return first_safepoint_;
852 }
853
David Brazdil3fc992f2015-04-16 18:31:55 +0100854 // Resets the starting point for range-searching queries to the first range.
855 // Intervals must be reset prior to starting a new linear scan over them.
856 void ResetSearchCache() {
857 range_search_start_ = first_range_;
858 }
859
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100860 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700861 LiveInterval(ArenaAllocator* allocator,
862 Primitive::Type type,
863 HInstruction* defined_by = nullptr,
864 bool is_fixed = false,
865 int reg = kNoRegister,
866 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000867 bool is_slow_path_safepoint = false,
868 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700869 : allocator_(allocator),
870 first_range_(nullptr),
871 last_range_(nullptr),
David Brazdil3fc992f2015-04-16 18:31:55 +0100872 range_search_start_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100873 first_safepoint_(nullptr),
874 last_safepoint_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700875 first_use_(nullptr),
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100876 first_env_use_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700877 type_(type),
878 next_sibling_(nullptr),
879 parent_(this),
880 register_(reg),
881 spill_slot_(kNoSpillSlot),
882 is_fixed_(is_fixed),
883 is_temp_(is_temp),
884 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000885 is_high_interval_(is_high_interval),
886 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700887 defined_by_(defined_by) {}
888
David Brazdil3fc992f2015-04-16 18:31:55 +0100889 // Searches for a LiveRange that either covers the given position or is the
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700890 // first next LiveRange. Returns null if no such LiveRange exists. Ranges
David Brazdil3fc992f2015-04-16 18:31:55 +0100891 // known to end before `position` can be skipped with `search_start`.
892 LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000893 if (kIsDebugBuild) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100894 if (search_start != first_range_) {
895 // If we are not searching the entire list of ranges, make sure we do
896 // not skip the range we are searching for.
897 if (search_start == nullptr) {
898 DCHECK(IsDeadAt(position));
899 } else if (search_start->GetStart() > position) {
900 DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_));
901 }
David Brazdil5b8e6a52015-02-25 16:17:05 +0000902 }
903 }
904
David Brazdil3fc992f2015-04-16 18:31:55 +0100905 LiveRange* range;
906 for (range = search_start;
907 range != nullptr && range->GetEnd() <= position;
908 range = range->GetNext()) {
909 continue;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000910 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100911 return range;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000912 }
913
Nicolas Geoffray57902602015-04-21 14:28:41 +0100914 bool DefinitionRequiresRegister() const {
915 DCHECK(IsParent());
916 LocationSummary* locations = defined_by_->GetLocations();
917 Location location = locations->Out();
918 // This interval is the first interval of the instruction. If the output
919 // of the instruction requires a register, we return the position of that instruction
920 // as the first register use.
921 if (location.IsUnallocated()) {
922 if ((location.GetPolicy() == Location::kRequiresRegister)
923 || (location.GetPolicy() == Location::kSameAsFirstInput
924 && (locations->InAt(0).IsRegister()
925 || locations->InAt(0).IsRegisterPair()
926 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
927 return true;
928 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
929 || (location.GetPolicy() == Location::kSameAsFirstInput
930 && (locations->InAt(0).IsFpuRegister()
931 || locations->InAt(0).IsFpuRegisterPair()
932 || locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister))) {
933 return true;
934 }
935 } else if (location.IsRegister() || location.IsRegisterPair()) {
936 return true;
937 }
938 return false;
939 }
940
941 bool IsDefiningPosition(size_t position) const {
942 return IsParent() && (position == GetStart());
943 }
944
945 bool HasSynthesizeUseAt(size_t position) const {
946 UsePosition* use = first_use_;
947 while (use != nullptr) {
948 size_t use_position = use->GetPosition();
949 if ((use_position == position) && use->IsSynthesized()) {
950 return true;
951 }
952 if (use_position > position) break;
953 use = use->GetNext();
954 }
955 return false;
956 }
957
958 void AddBackEdgeUses(const HBasicBlock& block_at_use) {
959 DCHECK(block_at_use.IsInLoop());
960 // Add synthesized uses at the back edge of loops to help the register allocator.
961 // Note that this method is called in decreasing liveness order, to faciliate adding
962 // uses at the head of the `first_use_` linked list. Because below
963 // we iterate from inner-most to outer-most, which is in increasing liveness order,
964 // we need to take extra care of how the `first_use_` linked list is being updated.
965 UsePosition* first_in_new_list = nullptr;
966 UsePosition* last_in_new_list = nullptr;
967 for (HLoopInformationOutwardIterator it(block_at_use);
968 !it.Done();
969 it.Advance()) {
970 HLoopInformation* current = it.Current();
971 if (GetDefinedBy()->GetLifetimePosition() >= current->GetHeader()->GetLifetimeStart()) {
972 // This interval is defined in the loop. We can stop going outward.
973 break;
974 }
975
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100976 // We're only adding a synthesized use at the last back edge. Adding syntehsized uses on
977 // all back edges is not necessary: anything used in the loop will have its use at the
978 // last back edge. If we want branches in a loop to have better register allocation than
979 // another branch, then it is the linear order we should change.
980 size_t back_edge_use_position = current->GetLifetimeEnd();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100981 if ((first_use_ != nullptr) && (first_use_->GetPosition() <= back_edge_use_position)) {
982 // There was a use already seen in this loop. Therefore the previous call to `AddUse`
983 // already inserted the backedge use. We can stop going outward.
984 DCHECK(HasSynthesizeUseAt(back_edge_use_position));
985 break;
986 }
987
988 DCHECK(last_in_new_list == nullptr
989 || back_edge_use_position > last_in_new_list->GetPosition());
990
991 UsePosition* new_use = new (allocator_) UsePosition(
992 nullptr, UsePosition::kNoInput, /* is_environment */ false,
993 back_edge_use_position, nullptr);
994
995 if (last_in_new_list != nullptr) {
996 // Going outward. The latest created use needs to point to the new use.
997 last_in_new_list->SetNext(new_use);
998 } else {
999 // This is the inner-most loop.
1000 DCHECK_EQ(current, block_at_use.GetLoopInformation());
1001 first_in_new_list = new_use;
1002 }
1003 last_in_new_list = new_use;
1004 }
1005 // Link the newly created linked list with `first_use_`.
1006 if (last_in_new_list != nullptr) {
1007 last_in_new_list->SetNext(first_use_);
1008 first_use_ = first_in_new_list;
1009 }
1010 }
1011
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001012 ArenaAllocator* const allocator_;
1013
1014 // Ranges of this interval. We need a quick access to the last range to test
1015 // for liveness (see `IsDeadAt`).
1016 LiveRange* first_range_;
1017 LiveRange* last_range_;
1018
David Brazdil3fc992f2015-04-16 18:31:55 +01001019 // The first range at or after the current position of a linear scan. It is
1020 // used to optimize range-searching queries.
1021 LiveRange* range_search_start_;
1022
Nicolas Geoffray43af7282015-04-16 13:01:01 +01001023 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001024 SafepointPosition* first_safepoint_;
1025 SafepointPosition* last_safepoint_;
1026
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001027 // Uses of this interval. Note that this linked list is shared amongst siblings.
1028 UsePosition* first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001029 UsePosition* first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001030
1031 // The instruction type this interval corresponds to.
1032 const Primitive::Type type_;
1033
1034 // Live interval that is the result of a split.
1035 LiveInterval* next_sibling_;
1036
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001037 // The first interval from which split intervals come from.
1038 LiveInterval* parent_;
1039
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001040 // The register allocated to this interval.
1041 int register_;
1042
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001043 // The spill slot allocated to this interval.
1044 int spill_slot_;
1045
1046 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001047 const bool is_fixed_;
1048
1049 // Whether the interval is for a temporary.
1050 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001051
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001052 // Whether the interval is for a safepoint that calls on slow path.
1053 const bool is_slow_path_safepoint_;
1054
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001055 // Whether this interval is a synthesized interval for register pair.
1056 const bool is_high_interval_;
1057
1058 // If this interval needs a register pair, the high or low equivalent.
1059 // `is_high_interval_` tells whether this holds the low or the high.
1060 LiveInterval* high_or_low_interval_;
1061
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001062 // The instruction represented by this interval.
1063 HInstruction* const defined_by_;
1064
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001065 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001066 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001067
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001068 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1069
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001070 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
1071};
1072
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001073/**
1074 * Analysis that computes the liveness of instructions:
1075 *
1076 * (a) Non-environment uses of an instruction always make
1077 * the instruction live.
1078 * (b) Environment uses of an instruction whose type is
1079 * object (that is, non-primitive), make the instruction live.
1080 * This is due to having to keep alive objects that have
1081 * finalizers deleting native objects.
1082 * (c) When the graph has the debuggable property, environment uses
1083 * of an instruction that has a primitive type make the instruction live.
1084 * If the graph does not have the debuggable property, the environment
1085 * use has no effect, and may get a 'none' value after register allocation.
1086 *
1087 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
1088 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001089class SsaLivenessAnalysis : public ValueObject {
1090 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001091 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001092 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001093 codegen_(codegen),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001094 block_infos_(graph->GetArena(), graph->GetBlocks().Size()),
1095 instructions_from_ssa_index_(graph->GetArena(), 0),
1096 instructions_from_lifetime_position_(graph->GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001097 number_of_ssa_values_(0) {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001098 block_infos_.SetSize(graph->GetBlocks().Size());
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001099 }
1100
1101 void Analyze();
1102
1103 BitVector* GetLiveInSet(const HBasicBlock& block) const {
1104 return &block_infos_.Get(block.GetBlockId())->live_in_;
1105 }
1106
1107 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
1108 return &block_infos_.Get(block.GetBlockId())->live_out_;
1109 }
1110
1111 BitVector* GetKillSet(const HBasicBlock& block) const {
1112 return &block_infos_.Get(block.GetBlockId())->kill_;
1113 }
1114
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001115 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001116 return instructions_from_ssa_index_.Get(index);
1117 }
1118
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001119 HInstruction* GetInstructionFromPosition(size_t index) const {
1120 return instructions_from_lifetime_position_.Get(index);
1121 }
1122
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001123 HBasicBlock* GetBlockFromPosition(size_t index) const {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001124 HInstruction* instruction = GetInstructionFromPosition(index);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001125 if (instruction == nullptr) {
1126 // If we are at a block boundary, get the block following.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001127 instruction = GetInstructionFromPosition(index + 1);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001128 }
1129 return instruction->GetBlock();
1130 }
1131
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001132 bool IsAtBlockBoundary(size_t index) const {
1133 return GetInstructionFromPosition(index) == nullptr;
1134 }
1135
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001136 HInstruction* GetTempUser(LiveInterval* temp) const {
1137 // A temporary shares the same lifetime start as the instruction that requires it.
1138 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001139 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
1140 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
1141 return user;
1142 }
1143
1144 size_t GetTempIndex(LiveInterval* temp) const {
1145 // We use the input index to store the index of the temporary in the user's temporary list.
1146 DCHECK(temp->IsTemp());
1147 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001148 }
1149
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001150 size_t GetMaxLifetimePosition() const {
1151 return instructions_from_lifetime_position_.Size() * 2 - 1;
1152 }
1153
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001154 size_t GetNumberOfSsaValues() const {
1155 return number_of_ssa_values_;
1156 }
1157
Andreas Gampe7c3952f2015-02-19 18:21:24 -08001158 static constexpr const char* kLivenessPassName = "liveness";
1159
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001160 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +01001161 // Linearize the graph so that:
1162 // (1): a block is always after its dominator,
1163 // (2): blocks of loops are contiguous.
1164 // This creates a natural and efficient ordering when visualizing live ranges.
1165 void LinearizeGraph();
1166
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001167 // Give an SSA number to each instruction that defines a value used by another instruction,
1168 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001169 void NumberInstructions();
1170
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001171 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1172 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001173
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001174 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1175 // kill sets, that do not take into account backward branches.
1176 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001177
1178 // After computing the initial sets, this method does a fixed point
1179 // calculation over the live_in and live_out set to take into account
1180 // backwards branches.
1181 void ComputeLiveInAndLiveOutSets();
1182
1183 // Update the live_in set of the block and returns whether it has changed.
1184 bool UpdateLiveIn(const HBasicBlock& block);
1185
1186 // Update the live_out set of the block and returns whether it has changed.
1187 bool UpdateLiveOut(const HBasicBlock& block);
1188
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001189 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
1190 if (instruction == nullptr) return false;
1191 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1192 return instruction->GetType() == Primitive::kPrimNot;
1193 }
1194
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001195 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001196 CodeGenerator* const codegen_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001197 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001198
1199 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001200 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001201
1202 // Temporary array used when inserting moves in the graph.
1203 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001204 size_t number_of_ssa_values_;
1205
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001206 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1207
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001208 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1209};
1210
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001211} // namespace art
1212
1213#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_