blob: 92788fe6b84330f6510de68e268e086ff34f8dc8 [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
Vladimir Marko5233f932015-09-29 19:01:15 +010030class BlockInfo : public ArenaObject<kArenaAllocSsaLiveness> {
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),
Vladimir Markof6a35de2016-03-21 12:01:50 +000034 live_in_(allocator, number_of_ssa_values, false, kArenaAllocSsaLiveness),
35 live_out_(allocator, number_of_ssa_values, false, kArenaAllocSsaLiveness),
36 kill_(allocator, number_of_ssa_values, false, kArenaAllocSsaLiveness) {
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 */
Vladimir Marko5233f932015-09-29 19:01:15 +010058class LiveRange FINAL : public ArenaObject<kArenaAllocSsaLiveness> {
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 {
David Brazdilc7a24852015-05-15 16:44:05 +010079 stream << "[" << start_ << "," << end_ << ")";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010080 }
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 */
Vladimir Marko5233f932015-09-29 19:01:15 +0100104class UsePosition : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100105 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100106 UsePosition(HInstruction* user,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100107 HEnvironment* environment,
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100108 size_t input_index,
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100109 size_t position,
110 UsePosition* next)
111 : user_(user),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100112 environment_(environment),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100113 input_index_(input_index),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100114 position_(position),
115 next_(next) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100116 DCHECK(environment == nullptr || user == nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100117 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
118 }
119
Nicolas Geoffray57902602015-04-21 14:28:41 +0100120 static constexpr size_t kNoInput = -1;
121
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100122 size_t GetPosition() const { return position_; }
123
124 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100125 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100126
127 HInstruction* GetUser() const { return user_; }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100128 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100129
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100130 bool GetIsEnvironment() const { return environment_ != nullptr; }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100131 bool IsSynthesized() const { return user_ == nullptr; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100132
133 size_t GetInputIndex() const { return input_index_; }
134
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100135 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100136 stream << position_;
Nicolas Geoffray57902602015-04-21 14:28:41 +0100137 }
138
139 HLoopInformation* GetLoopInformation() const {
140 return user_->GetBlock()->GetLoopInformation();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100141 }
142
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000143 UsePosition* Dup(ArenaAllocator* allocator) const {
144 return new (allocator) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100145 user_, environment_, input_index_, position_,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000146 next_ == nullptr ? nullptr : next_->Dup(allocator));
147 }
148
Nicolas Geoffray57902602015-04-21 14:28:41 +0100149 bool RequiresRegister() const {
150 if (GetIsEnvironment()) return false;
151 if (IsSynthesized()) return false;
152 Location location = GetUser()->GetLocations()->InAt(GetInputIndex());
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700153 return location.IsUnallocated() && location.RequiresRegisterKind();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100154 }
155
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100156 private:
157 HInstruction* const user_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100158 HEnvironment* const environment_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100159 const size_t input_index_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100160 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100161 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100162
163 DISALLOW_COPY_AND_ASSIGN(UsePosition);
164};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100165
Vladimir Marko5233f932015-09-29 19:01:15 +0100166class SafepointPosition : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100167 public:
168 explicit SafepointPosition(HInstruction* instruction)
169 : instruction_(instruction),
170 next_(nullptr) {}
171
172 void SetNext(SafepointPosition* next) {
173 next_ = next;
174 }
175
176 size_t GetPosition() const {
177 return instruction_->GetLifetimePosition();
178 }
179
180 SafepointPosition* GetNext() const {
181 return next_;
182 }
183
184 LocationSummary* GetLocations() const {
185 return instruction_->GetLocations();
186 }
187
188 HInstruction* GetInstruction() const {
189 return instruction_;
190 }
191
192 private:
193 HInstruction* const instruction_;
194 SafepointPosition* next_;
195
196 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
197};
198
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100199/**
200 * An interval is a list of disjoint live ranges where an instruction is live.
201 * Each instruction that has uses gets an interval.
202 */
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100203class LiveInterval : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100204 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700205 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
206 Primitive::Type type,
207 HInstruction* instruction = nullptr) {
208 return new (allocator) LiveInterval(allocator, type, instruction);
209 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100210
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100211 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
212 return new (allocator) LiveInterval(
213 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
214 }
215
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100216 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100217 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
218 }
219
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100220 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
221 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100222 }
223
224 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700225 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100226 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700227 // This interval is the result of a split.
228 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100229
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000230 void AddTempUse(HInstruction* instruction, size_t temp_index) {
231 DCHECK(IsTemp());
232 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100233 DCHECK(first_env_use_ == nullptr) << "A temporary cannot have environment user";
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000234 size_t position = instruction->GetLifetimePosition();
235 first_use_ = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100236 instruction, /* environment */ nullptr, temp_index, position, first_use_);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000237 AddRange(position, position + 1);
238 }
239
David Brazdilb3e773e2016-01-26 11:28:37 +0000240 // Record use of an input. The use will be recorded as an environment use if
241 // `environment` is not null and as register use otherwise. If `actual_user`
242 // is specified, the use will be recorded at `actual_user`'s lifetime position.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000243 void AddUse(HInstruction* instruction,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100244 HEnvironment* environment,
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000245 size_t input_index,
David Brazdilb3e773e2016-01-26 11:28:37 +0000246 HInstruction* actual_user = nullptr,
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000247 bool keep_alive = false) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100248 bool is_environment = (environment != nullptr);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000249 LocationSummary* locations = instruction->GetLocations();
David Brazdilb3e773e2016-01-26 11:28:37 +0000250 if (actual_user == nullptr) {
251 actual_user = instruction;
252 }
253
254 // Set the use within the instruction.
255 size_t position = actual_user->GetLifetimePosition() + 1;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000256 if (!is_environment) {
257 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
258 // For fixed inputs and output same as input, the register allocator
259 // requires to have inputs die at the instruction, so that input moves use the
260 // location of the input just before that instruction (and not potential moves due
261 // to splitting).
David Brazdilb3e773e2016-01-26 11:28:37 +0000262 DCHECK_EQ(instruction, actual_user);
263 position = actual_user->GetLifetimePosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100264 } else if (!locations->InAt(input_index).IsValid()) {
265 return;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000266 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100267 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000268
Nicolas Geoffray57902602015-04-21 14:28:41 +0100269 if (!is_environment && instruction->IsInLoop()) {
270 AddBackEdgeUses(*instruction->GetBlock());
271 }
272
Nicolas Geoffray76905622014-09-25 14:39:26 +0100273 if ((first_use_ != nullptr)
David Brazdilb3e773e2016-01-26 11:28:37 +0000274 && (first_use_->GetUser() == actual_user)
Nicolas Geoffray76905622014-09-25 14:39:26 +0100275 && (first_use_->GetPosition() < position)) {
276 // The user uses the instruction multiple times, and one use dies before the other.
277 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000278 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100279 UsePosition* cursor = first_use_;
280 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
281 cursor = cursor->GetNext();
282 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100283 DCHECK(first_use_->GetPosition() + 1 == position);
284 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100285 instruction, nullptr /* environment */, input_index, position, cursor->GetNext());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100286 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100287 if (first_range_->GetEnd() == first_use_->GetPosition()) {
288 first_range_->end_ = position;
289 }
290 return;
291 }
292
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100293 if (is_environment) {
294 first_env_use_ = new (allocator_) UsePosition(
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100295 nullptr /* instruction */, environment, input_index, position, first_env_use_);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100296 } else {
297 first_use_ = new (allocator_) UsePosition(
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100298 instruction, nullptr /* environment */, input_index, position, first_use_);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100299 }
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000300
301 if (is_environment && !keep_alive) {
302 // If this environment use does not keep the instruction live, it does not
303 // affect the live range of that instruction.
304 return;
305 }
306
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100307 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100308 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100309 // First time we see a use of that interval.
David Brazdil3fc992f2015-04-16 18:31:55 +0100310 first_range_ = last_range_ = range_search_start_ =
311 new (allocator_) LiveRange(start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100312 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100313 // There is a use later in the same block or in a following block.
314 // Note that in such a case, `AddRange` for the whole blocks has been called
315 // before arriving in this method, and this is the reason the start of
316 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100317 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100319 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100320 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100321 // Note that the start of `first_range_` can be equal to `end`: two blocks
322 // having adjacent lifetime positions are not necessarily
323 // predecessor/successor. When two blocks are predecessor/successor, the
324 // liveness algorithm has called `AddRange` before arriving in this method,
325 // and the check line 205 would succeed.
David Brazdil3fc992f2015-04-16 18:31:55 +0100326 first_range_ = range_search_start_ =
327 new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100328 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100329 }
330
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100331 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100332 DCHECK(instruction->IsPhi());
Nicolas Geoffray57902602015-04-21 14:28:41 +0100333 if (block->IsInLoop()) {
334 AddBackEdgeUses(*block);
335 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100336 first_use_ = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100337 instruction, /* environment */ nullptr, input_index, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100338 }
339
340 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100341 if (first_range_ == nullptr) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100342 first_range_ = last_range_ = range_search_start_ =
343 new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100344 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100345 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100346 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100347 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
348 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100349 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100350 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100351 // There is a hole in the interval. Create a new range.
David Brazdil3fc992f2015-04-16 18:31:55 +0100352 first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100353 }
354 }
355
356 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100357 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000358 DCHECK_LE(start, first_range_->GetStart());
359 // Find the range that covers the positions after the loop.
360 LiveRange* after_loop = first_range_;
361 LiveRange* last_in_loop = nullptr;
362 while (after_loop != nullptr && after_loop->GetEnd() < end) {
363 DCHECK_LE(start, after_loop->GetStart());
364 last_in_loop = after_loop;
365 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100366 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000367 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100368 // Uses are only in the loop.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700369 first_range_ = last_range_ = range_search_start_ =
370 new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000371 } else if (after_loop->GetStart() <= end) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100372 first_range_ = range_search_start_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100373 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100374 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000375 } else {
376 // The use after the loop is after a lifetime hole.
377 DCHECK(last_in_loop != nullptr);
David Brazdil3fc992f2015-04-16 18:31:55 +0100378 first_range_ = range_search_start_ = last_in_loop;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000379 first_range_->start_ = start;
380 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100381 }
382 }
383
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100384 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100385 void SetSpillSlot(int slot) {
386 DCHECK(!is_fixed_);
387 DCHECK(!is_temp_);
388 spill_slot_ = slot;
389 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100390 int GetSpillSlot() const { return spill_slot_; }
391
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100392 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100393 if (first_range_ != nullptr) {
394 first_range_->start_ = from;
395 } else {
396 // Instruction without uses.
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100397 DCHECK(first_use_ == nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100398 DCHECK(from == defined_by_->GetLifetimePosition());
David Brazdil3fc992f2015-04-16 18:31:55 +0100399 first_range_ = last_range_ = range_search_start_ =
400 new (allocator_) LiveRange(from, from + 2, nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100401 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100402 }
403
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100404 LiveInterval* GetParent() const { return parent_; }
405
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100406 // Returns whether this interval is the parent interval, that is, the interval
407 // that starts where the HInstruction is defined.
408 bool IsParent() const { return parent_ == this; }
409
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100410 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000411 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100412
413 int GetRegister() const { return register_; }
414 void SetRegister(int reg) { register_ = reg; }
415 void ClearRegister() { register_ = kNoRegister; }
416 bool HasRegister() const { return register_ != kNoRegister; }
417
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100418 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100419 return GetEnd() <= position;
420 }
421
422 bool IsDefinedAt(size_t position) const {
423 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100424 }
425
David Brazdil3fc992f2015-04-16 18:31:55 +0100426 // Returns true if the interval contains a LiveRange covering `position`.
427 // The range at or immediately after the current position of linear scan
428 // is cached for better performance. If `position` can be smaller than
429 // that, CoversSlow should be used instead.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000430 bool Covers(size_t position) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100431 LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_);
432 range_search_start_ = candidate;
433 return (candidate != nullptr && candidate->GetStart() <= position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100434 }
435
David Brazdil3fc992f2015-04-16 18:31:55 +0100436 // Same as Covers but always tests all ranges.
437 bool CoversSlow(size_t position) const {
438 LiveRange* candidate = FindRangeAtOrAfter(position, first_range_);
439 return candidate != nullptr && candidate->GetStart() <= position;
440 }
441
442 // Returns the first intersection of this interval with `current`, which
443 // must be the interval currently being allocated by linear scan.
444 size_t FirstIntersectionWith(LiveInterval* current) const {
445 // Find the first range after the start of `current`. We use the search
446 // cache to improve performance.
447 DCHECK(GetStart() <= current->GetStart() || IsFixed());
448 LiveRange* other_range = current->first_range_;
449 LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_);
450 if (my_range == nullptr) {
451 return kNoLifetime;
452 }
453
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100454 // Advance both intervals and find the first matching range start in
455 // this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100456 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000457 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100458 my_range = my_range->GetNext();
459 if (my_range == nullptr) {
460 return kNoLifetime;
461 }
David Brazdil714e14f2015-02-25 11:57:05 +0000462 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100463 other_range = other_range->GetNext();
464 if (other_range == nullptr) {
465 return kNoLifetime;
466 }
David Brazdil714e14f2015-02-25 11:57:05 +0000467 } else {
468 DCHECK(my_range->IntersectsWith(*other_range));
469 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100470 }
471 } while (true);
472 }
473
474 size_t GetStart() const {
475 return first_range_->GetStart();
476 }
477
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100478 size_t GetEnd() const {
479 return last_range_->GetEnd();
480 }
481
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700482 size_t GetLength() const {
483 return GetEnd() - GetStart();
484 }
485
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100486 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100487 if (is_temp_) {
488 return position == GetStart() ? position : kNoLifetime;
489 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100490
491 if (IsDefiningPosition(position) && DefinitionRequiresRegister()) {
492 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100493 }
494
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100495 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100496 size_t end = GetEnd();
497 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100498 size_t use_position = use->GetPosition();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100499 if (use_position > position) {
Nicolas Geoffray57902602015-04-21 14:28:41 +0100500 if (use->RequiresRegister()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100501 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100502 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100503 }
504 use = use->GetNext();
505 }
506 return kNoLifetime;
507 }
508
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700509 // Returns the location of the first register use for this live interval,
510 // including a register definition if applicable.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100511 size_t FirstRegisterUse() const {
512 return FirstRegisterUseAfter(GetStart());
513 }
514
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700515 // Whether the interval requires a register rather than a stack location.
516 // If needed for performance, this could be cached.
Matthew Gharrity2ccae4a2016-08-12 16:10:45 +0000517 bool RequiresRegister() const {
518 return !HasRegister() && FirstRegisterUse() != kNoLifetime;
519 }
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700520
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000521 size_t FirstUseAfter(size_t position) const {
522 if (is_temp_) {
523 return position == GetStart() ? position : kNoLifetime;
524 }
525
Nicolas Geoffray57902602015-04-21 14:28:41 +0100526 if (IsDefiningPosition(position)) {
527 DCHECK(defined_by_->GetLocations()->Out().IsValid());
528 return position;
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100529 }
530
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000531 UsePosition* use = first_use_;
532 size_t end = GetEnd();
533 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100534 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100535 if (use_position > position) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100536 return use_position;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000537 }
538 use = use->GetNext();
539 }
540 return kNoLifetime;
541 }
542
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100543 UsePosition* GetFirstUse() const {
544 return first_use_;
545 }
546
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100547 UsePosition* GetFirstEnvironmentUse() const {
548 return first_env_use_;
549 }
550
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100551 Primitive::Type GetType() const {
552 return type_;
553 }
554
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100555 HInstruction* GetDefinedBy() const {
556 return defined_by_;
557 }
558
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100559 bool HasWillCallSafepoint() const {
560 for (SafepointPosition* safepoint = first_safepoint_;
561 safepoint != nullptr;
562 safepoint = safepoint->GetNext()) {
563 if (safepoint->GetLocations()->WillCall()) return true;
564 }
565 return false;
566 }
567
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100568 SafepointPosition* FindSafepointJustBefore(size_t position) const {
569 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
570 safepoint != nullptr;
571 previous = safepoint, safepoint = safepoint->GetNext()) {
572 if (safepoint->GetPosition() >= position) return previous;
573 }
574 return last_safepoint_;
575 }
576
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100577 /**
578 * Split this interval at `position`. This interval is changed to:
579 * [start ... position).
580 *
581 * The new interval covers:
582 * [position ... end)
583 */
584 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100585 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100586 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100587 DCHECK_GT(position, GetStart());
588
David Brazdil241a4862015-04-16 17:59:03 +0100589 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100590 // This range dies before `position`, no need to split.
591 return nullptr;
592 }
593
594 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100595 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
596 if (new_last_safepoint == nullptr) {
597 new_interval->first_safepoint_ = first_safepoint_;
598 new_interval->last_safepoint_ = last_safepoint_;
599 first_safepoint_ = last_safepoint_ = nullptr;
600 } else if (last_safepoint_ != new_last_safepoint) {
601 new_interval->last_safepoint_ = last_safepoint_;
602 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
603 DCHECK(new_interval->first_safepoint_ != nullptr);
604 last_safepoint_ = new_last_safepoint;
605 last_safepoint_->SetNext(nullptr);
606 }
607
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100608 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100609 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100610 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100611
612 new_interval->first_use_ = first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100613 new_interval->first_env_use_ = first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100614 LiveRange* current = first_range_;
615 LiveRange* previous = nullptr;
616 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000617 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100618 do {
619 if (position >= current->GetEnd()) {
620 // Move to next range.
621 previous = current;
622 current = current->next_;
623 } else if (position <= current->GetStart()) {
624 // If the previous range did not cover this position, we know position is in
625 // a lifetime hole. We can just break the first_range_ and last_range_ links
626 // and return the new interval.
627 DCHECK(previous != nullptr);
628 DCHECK(current != first_range_);
629 new_interval->last_range_ = last_range_;
630 last_range_ = previous;
631 previous->next_ = nullptr;
632 new_interval->first_range_ = current;
David Brazdil3fc992f2015-04-16 18:31:55 +0100633 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700634 // Search start point is inside `new_interval`. Change it to null
David Brazdil3fc992f2015-04-16 18:31:55 +0100635 // (i.e. the end of the interval) in the original interval.
636 range_search_start_ = nullptr;
637 }
638 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100639 return new_interval;
640 } else {
641 // This range covers position. We create a new last_range_ for this interval
642 // that covers last_range_->Start() and position. We also shorten the current
643 // range and make it the first range of the new interval.
644 DCHECK(position < current->GetEnd() && position > current->GetStart());
645 new_interval->last_range_ = last_range_;
646 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
647 if (previous != nullptr) {
648 previous->next_ = last_range_;
649 } else {
650 first_range_ = last_range_;
651 }
652 new_interval->first_range_ = current;
653 current->start_ = position;
David Brazdil3fc992f2015-04-16 18:31:55 +0100654 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
655 // Search start point is inside `new_interval`. Change it to `last_range`
656 // in the original interval. This is conservative but always correct.
657 range_search_start_ = last_range_;
658 }
659 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100660 return new_interval;
661 }
662 } while (current != nullptr);
663
664 LOG(FATAL) << "Unreachable";
665 return nullptr;
666 }
667
Nicolas Geoffray76905622014-09-25 14:39:26 +0100668 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100669 return GetStart() <= other->GetStart();
670 }
671
672 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100673 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100674 }
675
676 void Dump(std::ostream& stream) const {
677 stream << "ranges: { ";
678 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000679 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100680 current->Dump(stream);
681 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000682 current = current->GetNext();
683 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100684 stream << "}, uses: { ";
685 UsePosition* use = first_use_;
686 if (use != nullptr) {
687 do {
688 use->Dump(stream);
689 stream << " ";
690 } while ((use = use->GetNext()) != nullptr);
691 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100692 stream << "}, { ";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100693 use = first_env_use_;
694 if (use != nullptr) {
695 do {
696 use->Dump(stream);
697 stream << " ";
698 } while ((use = use->GetNext()) != nullptr);
699 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100700 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700701 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000702 stream << " is_low: " << IsLowInterval();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100703 stream << " is_high: " << IsHighInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100704 }
705
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700706 // Same as Dump, but adds context such as the instruction defining this interval, and
707 // the register currently assigned to this interval.
708 void DumpWithContext(std::ostream& stream, const CodeGenerator& codegen) const;
709
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100710 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000711 LiveInterval* GetLastSibling() {
712 LiveInterval* result = this;
713 while (result->next_sibling_ != nullptr) {
714 result = result->next_sibling_;
715 }
716 return result;
717 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100718
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100719 // Returns the first register hint that is at least free before
720 // the value contained in `free_until`. If none is found, returns
721 // `kNoRegister`.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100722 int FindFirstRegisterHint(size_t* free_until, const SsaLivenessAnalysis& liveness) const;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100723
724 // If there is enough at the definition site to find a register (for example
725 // it uses the same input as the first input), returns the register as a hint.
726 // Returns kNoRegister otherwise.
727 int FindHintAtDefinition() const;
728
729 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
730 // slots for spilling.
731 bool NeedsTwoSpillSlots() const;
732
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100733 bool IsFloatingPoint() const {
734 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
735 }
736
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100737 // Converts the location of the interval to a `Location` object.
738 Location ToLocation() const;
739
740 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000741 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100742
David Brazdil241a4862015-04-16 17:59:03 +0100743 // Finds the sibling that is defined at `position`.
744 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100745
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100746 // Returns whether `other` and `this` share the same kind of register.
747 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000748 bool SameRegisterKind(const LiveInterval& other) const {
749 return IsFloatingPoint() == other.IsFloatingPoint();
750 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100751
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000752 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000753 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000754 }
755
756 bool HasLowInterval() const {
757 return IsHighInterval();
758 }
759
760 LiveInterval* GetLowInterval() const {
761 DCHECK(HasLowInterval());
762 return high_or_low_interval_;
763 }
764
765 LiveInterval* GetHighInterval() const {
766 DCHECK(HasHighInterval());
767 return high_or_low_interval_;
768 }
769
770 bool IsHighInterval() const {
771 return GetParent()->is_high_interval_;
772 }
773
774 bool IsLowInterval() const {
775 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
776 }
777
778 void SetLowInterval(LiveInterval* low) {
779 DCHECK(IsHighInterval());
780 high_or_low_interval_ = low;
781 }
782
783 void SetHighInterval(LiveInterval* high) {
784 DCHECK(IsLowInterval());
785 high_or_low_interval_ = high;
786 }
787
788 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100789 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000790 DCHECK(!HasHighInterval());
791 DCHECK(!HasLowInterval());
792 high_or_low_interval_ = new (allocator_) LiveInterval(
793 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
794 high_or_low_interval_->high_or_low_interval_ = this;
795 if (first_range_ != nullptr) {
796 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
David Brazdilc08675c2015-04-17 15:49:51 +0100797 high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange();
David Brazdil3fc992f2015-04-16 18:31:55 +0100798 high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000799 }
800 if (first_use_ != nullptr) {
801 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
802 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100803
804 if (first_env_use_ != nullptr) {
805 high_or_low_interval_->first_env_use_ = first_env_use_->Dup(allocator_);
806 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000807 }
808
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000809 // Returns whether an interval, when it is non-split, is using
810 // the same register of one of its input.
811 bool IsUsingInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100812 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000813 if (defined_by_ != nullptr && !IsSplit()) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100814 for (const HInstruction* input : defined_by_->GetInputs()) {
815 LiveInterval* interval = input->GetLiveInterval();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000816
David Brazdil3fc992f2015-04-16 18:31:55 +0100817 // Find the interval that covers `defined_by`_. Calls to this function
818 // are made outside the linear scan, hence we need to use CoversSlow.
819 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000820 interval = interval->GetNextSibling();
821 }
822
823 // Check if both intervals have the same register of the same kind.
824 if (interval != nullptr
825 && interval->SameRegisterKind(*this)
826 && interval->GetRegister() == GetRegister()) {
827 return true;
828 }
829 }
830 }
831 return false;
832 }
833
834 // Returns whether an interval, when it is non-split, can safely use
835 // the same register of one of its input. Note that this method requires
836 // IsUsingInputRegister() to be true.
837 bool CanUseInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100838 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000839 DCHECK(IsUsingInputRegister());
840 if (defined_by_ != nullptr && !IsSplit()) {
841 LocationSummary* locations = defined_by_->GetLocations();
842 if (locations->OutputCanOverlapWithInputs()) {
843 return false;
844 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100845 for (const HInstruction* input : defined_by_->GetInputs()) {
846 LiveInterval* interval = input->GetLiveInterval();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000847
David Brazdil3fc992f2015-04-16 18:31:55 +0100848 // Find the interval that covers `defined_by`_. Calls to this function
849 // are made outside the linear scan, hence we need to use CoversSlow.
850 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000851 interval = interval->GetNextSibling();
852 }
853
854 if (interval != nullptr
855 && interval->SameRegisterKind(*this)
856 && interval->GetRegister() == GetRegister()) {
857 // We found the input that has the same register. Check if it is live after
858 // `defined_by`_.
David Brazdil3fc992f2015-04-16 18:31:55 +0100859 return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000860 }
861 }
862 }
863 LOG(FATAL) << "Unreachable";
864 UNREACHABLE();
865 }
866
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100867 void AddSafepoint(HInstruction* instruction) {
868 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
869 if (first_safepoint_ == nullptr) {
870 first_safepoint_ = last_safepoint_ = safepoint;
871 } else {
872 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
873 last_safepoint_->SetNext(safepoint);
874 last_safepoint_ = safepoint;
875 }
876 }
877
878 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100879 return first_safepoint_;
880 }
881
David Brazdil3fc992f2015-04-16 18:31:55 +0100882 // Resets the starting point for range-searching queries to the first range.
883 // Intervals must be reset prior to starting a new linear scan over them.
884 void ResetSearchCache() {
885 range_search_start_ = first_range_;
886 }
887
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700888 bool DefinitionRequiresRegister() const {
889 DCHECK(IsParent());
890 LocationSummary* locations = defined_by_->GetLocations();
891 Location location = locations->Out();
892 // This interval is the first interval of the instruction. If the output
893 // of the instruction requires a register, we return the position of that instruction
894 // as the first register use.
895 if (location.IsUnallocated()) {
896 if ((location.GetPolicy() == Location::kRequiresRegister)
897 || (location.GetPolicy() == Location::kSameAsFirstInput
898 && (locations->InAt(0).IsRegister()
899 || locations->InAt(0).IsRegisterPair()
900 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
901 return true;
902 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
903 || (location.GetPolicy() == Location::kSameAsFirstInput
904 && (locations->InAt(0).IsFpuRegister()
905 || locations->InAt(0).IsFpuRegisterPair()
906 || locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister))) {
907 return true;
908 }
909 } else if (location.IsRegister() || location.IsRegisterPair()) {
910 return true;
911 }
912 return false;
913 }
914
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100915 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700916 LiveInterval(ArenaAllocator* allocator,
917 Primitive::Type type,
918 HInstruction* defined_by = nullptr,
919 bool is_fixed = false,
920 int reg = kNoRegister,
921 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000922 bool is_slow_path_safepoint = false,
923 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700924 : allocator_(allocator),
925 first_range_(nullptr),
926 last_range_(nullptr),
David Brazdil3fc992f2015-04-16 18:31:55 +0100927 range_search_start_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100928 first_safepoint_(nullptr),
929 last_safepoint_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700930 first_use_(nullptr),
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100931 first_env_use_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700932 type_(type),
933 next_sibling_(nullptr),
934 parent_(this),
935 register_(reg),
936 spill_slot_(kNoSpillSlot),
937 is_fixed_(is_fixed),
938 is_temp_(is_temp),
939 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000940 is_high_interval_(is_high_interval),
941 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700942 defined_by_(defined_by) {}
943
David Brazdil3fc992f2015-04-16 18:31:55 +0100944 // Searches for a LiveRange that either covers the given position or is the
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700945 // first next LiveRange. Returns null if no such LiveRange exists. Ranges
David Brazdil3fc992f2015-04-16 18:31:55 +0100946 // known to end before `position` can be skipped with `search_start`.
947 LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000948 if (kIsDebugBuild) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100949 if (search_start != first_range_) {
950 // If we are not searching the entire list of ranges, make sure we do
951 // not skip the range we are searching for.
952 if (search_start == nullptr) {
953 DCHECK(IsDeadAt(position));
954 } else if (search_start->GetStart() > position) {
955 DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_));
956 }
David Brazdil5b8e6a52015-02-25 16:17:05 +0000957 }
958 }
959
David Brazdil3fc992f2015-04-16 18:31:55 +0100960 LiveRange* range;
961 for (range = search_start;
962 range != nullptr && range->GetEnd() <= position;
963 range = range->GetNext()) {
964 continue;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000965 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100966 return range;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000967 }
968
Nicolas Geoffray57902602015-04-21 14:28:41 +0100969 bool IsDefiningPosition(size_t position) const {
970 return IsParent() && (position == GetStart());
971 }
972
973 bool HasSynthesizeUseAt(size_t position) const {
974 UsePosition* use = first_use_;
975 while (use != nullptr) {
976 size_t use_position = use->GetPosition();
977 if ((use_position == position) && use->IsSynthesized()) {
978 return true;
979 }
980 if (use_position > position) break;
981 use = use->GetNext();
982 }
983 return false;
984 }
985
David Brazdiladf84912016-04-14 13:47:24 +0100986 bool IsLinearOrderWellFormed(const HGraph& graph) {
987 for (HBasicBlock* header : graph.GetBlocks()) {
David Brazdilfa3091e2016-04-19 10:10:17 +0100988 if (header == nullptr || !header->IsLoopHeader()) {
David Brazdiladf84912016-04-14 13:47:24 +0100989 continue;
990 }
991
992 HLoopInformation* loop = header->GetLoopInformation();
993 size_t num_blocks = loop->GetBlocks().NumSetBits();
994 size_t found_blocks = 0u;
995
996 for (HLinearOrderIterator it(graph); !it.Done(); it.Advance()) {
997 HBasicBlock* current = it.Current();
998 if (loop->Contains(*current)) {
999 found_blocks++;
1000 if (found_blocks == 1u && current != header) {
1001 // First block is not the header.
1002 return false;
1003 } else if (found_blocks == num_blocks && !loop->IsBackEdge(*current)) {
1004 // Last block is not a back edge.
1005 return false;
1006 }
1007 } else if (found_blocks != 0u && found_blocks != num_blocks) {
1008 // Blocks are not adjacent.
1009 return false;
1010 }
1011 }
1012 DCHECK_EQ(found_blocks, num_blocks);
1013 }
1014
1015 return true;
1016 }
1017
Nicolas Geoffray57902602015-04-21 14:28:41 +01001018 void AddBackEdgeUses(const HBasicBlock& block_at_use) {
1019 DCHECK(block_at_use.IsInLoop());
David Brazdil07b35102016-04-27 15:33:22 +01001020 if (block_at_use.GetGraph()->HasIrreducibleLoops()) {
1021 // Linear order may not be well formed when irreducible loops are present,
1022 // i.e. loop blocks may not be adjacent and a back edge may not be last,
1023 // which violates assumptions made in this method.
1024 return;
1025 }
1026
1027 DCHECK(IsLinearOrderWellFormed(*block_at_use.GetGraph()));
1028
Nicolas Geoffray57902602015-04-21 14:28:41 +01001029 // Add synthesized uses at the back edge of loops to help the register allocator.
1030 // Note that this method is called in decreasing liveness order, to faciliate adding
1031 // uses at the head of the `first_use_` linked list. Because below
1032 // we iterate from inner-most to outer-most, which is in increasing liveness order,
1033 // we need to take extra care of how the `first_use_` linked list is being updated.
1034 UsePosition* first_in_new_list = nullptr;
1035 UsePosition* last_in_new_list = nullptr;
1036 for (HLoopInformationOutwardIterator it(block_at_use);
1037 !it.Done();
1038 it.Advance()) {
1039 HLoopInformation* current = it.Current();
1040 if (GetDefinedBy()->GetLifetimePosition() >= current->GetHeader()->GetLifetimeStart()) {
1041 // This interval is defined in the loop. We can stop going outward.
1042 break;
1043 }
1044
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001045 // We're only adding a synthesized use at the last back edge. Adding syntehsized uses on
1046 // all back edges is not necessary: anything used in the loop will have its use at the
1047 // last back edge. If we want branches in a loop to have better register allocation than
1048 // another branch, then it is the linear order we should change.
1049 size_t back_edge_use_position = current->GetLifetimeEnd();
Nicolas Geoffray57902602015-04-21 14:28:41 +01001050 if ((first_use_ != nullptr) && (first_use_->GetPosition() <= back_edge_use_position)) {
1051 // There was a use already seen in this loop. Therefore the previous call to `AddUse`
1052 // already inserted the backedge use. We can stop going outward.
David Brazdil07b35102016-04-27 15:33:22 +01001053 DCHECK(HasSynthesizeUseAt(back_edge_use_position));
Nicolas Geoffray57902602015-04-21 14:28:41 +01001054 break;
1055 }
1056
David Brazdil07b35102016-04-27 15:33:22 +01001057 DCHECK(last_in_new_list == nullptr ||
1058 back_edge_use_position > last_in_new_list->GetPosition());
Nicolas Geoffray57902602015-04-21 14:28:41 +01001059
1060 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001061 /* user */ nullptr,
1062 /* environment */ nullptr,
1063 UsePosition::kNoInput,
1064 back_edge_use_position,
1065 /* next */ nullptr);
Nicolas Geoffray57902602015-04-21 14:28:41 +01001066
1067 if (last_in_new_list != nullptr) {
1068 // Going outward. The latest created use needs to point to the new use.
1069 last_in_new_list->SetNext(new_use);
1070 } else {
1071 // This is the inner-most loop.
1072 DCHECK_EQ(current, block_at_use.GetLoopInformation());
1073 first_in_new_list = new_use;
1074 }
1075 last_in_new_list = new_use;
1076 }
1077 // Link the newly created linked list with `first_use_`.
1078 if (last_in_new_list != nullptr) {
1079 last_in_new_list->SetNext(first_use_);
1080 first_use_ = first_in_new_list;
1081 }
1082 }
1083
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001084 ArenaAllocator* const allocator_;
1085
1086 // Ranges of this interval. We need a quick access to the last range to test
1087 // for liveness (see `IsDeadAt`).
1088 LiveRange* first_range_;
1089 LiveRange* last_range_;
1090
David Brazdil3fc992f2015-04-16 18:31:55 +01001091 // The first range at or after the current position of a linear scan. It is
1092 // used to optimize range-searching queries.
1093 LiveRange* range_search_start_;
1094
Nicolas Geoffray43af7282015-04-16 13:01:01 +01001095 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001096 SafepointPosition* first_safepoint_;
1097 SafepointPosition* last_safepoint_;
1098
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001099 // Uses of this interval. Note that this linked list is shared amongst siblings.
1100 UsePosition* first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001101 UsePosition* first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001102
1103 // The instruction type this interval corresponds to.
1104 const Primitive::Type type_;
1105
1106 // Live interval that is the result of a split.
1107 LiveInterval* next_sibling_;
1108
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001109 // The first interval from which split intervals come from.
1110 LiveInterval* parent_;
1111
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001112 // The register allocated to this interval.
1113 int register_;
1114
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001115 // The spill slot allocated to this interval.
1116 int spill_slot_;
1117
1118 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001119 const bool is_fixed_;
1120
1121 // Whether the interval is for a temporary.
1122 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001123
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001124 // Whether the interval is for a safepoint that calls on slow path.
1125 const bool is_slow_path_safepoint_;
1126
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001127 // Whether this interval is a synthesized interval for register pair.
1128 const bool is_high_interval_;
1129
1130 // If this interval needs a register pair, the high or low equivalent.
1131 // `is_high_interval_` tells whether this holds the low or the high.
1132 LiveInterval* high_or_low_interval_;
1133
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001134 // The instruction represented by this interval.
1135 HInstruction* const defined_by_;
1136
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001137 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001138 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001139
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001140 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1141
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001142 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
1143};
1144
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001145/**
1146 * Analysis that computes the liveness of instructions:
1147 *
1148 * (a) Non-environment uses of an instruction always make
1149 * the instruction live.
1150 * (b) Environment uses of an instruction whose type is
1151 * object (that is, non-primitive), make the instruction live.
1152 * This is due to having to keep alive objects that have
1153 * finalizers deleting native objects.
1154 * (c) When the graph has the debuggable property, environment uses
1155 * of an instruction that has a primitive type make the instruction live.
1156 * If the graph does not have the debuggable property, the environment
1157 * use has no effect, and may get a 'none' value after register allocation.
1158 *
1159 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
1160 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001161class SsaLivenessAnalysis : public ValueObject {
1162 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001163 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001164 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001165 codegen_(codegen),
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001166 block_infos_(graph->GetBlocks().size(),
1167 nullptr,
1168 graph->GetArena()->Adapter(kArenaAllocSsaLiveness)),
1169 instructions_from_ssa_index_(graph->GetArena()->Adapter(kArenaAllocSsaLiveness)),
1170 instructions_from_lifetime_position_(graph->GetArena()->Adapter(kArenaAllocSsaLiveness)),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001171 number_of_ssa_values_(0) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001172 }
1173
1174 void Analyze();
1175
1176 BitVector* GetLiveInSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001177 return &block_infos_[block.GetBlockId()]->live_in_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001178 }
1179
1180 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001181 return &block_infos_[block.GetBlockId()]->live_out_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001182 }
1183
1184 BitVector* GetKillSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001185 return &block_infos_[block.GetBlockId()]->kill_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001186 }
1187
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001188 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001189 return instructions_from_ssa_index_[index];
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001190 }
1191
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001192 HInstruction* GetInstructionFromPosition(size_t index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001193 return instructions_from_lifetime_position_[index];
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001194 }
1195
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001196 HBasicBlock* GetBlockFromPosition(size_t index) const {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001197 HInstruction* instruction = GetInstructionFromPosition(index);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001198 if (instruction == nullptr) {
1199 // If we are at a block boundary, get the block following.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001200 instruction = GetInstructionFromPosition(index + 1);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001201 }
1202 return instruction->GetBlock();
1203 }
1204
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001205 bool IsAtBlockBoundary(size_t index) const {
1206 return GetInstructionFromPosition(index) == nullptr;
1207 }
1208
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001209 HInstruction* GetTempUser(LiveInterval* temp) const {
1210 // A temporary shares the same lifetime start as the instruction that requires it.
1211 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001212 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
1213 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
1214 return user;
1215 }
1216
1217 size_t GetTempIndex(LiveInterval* temp) const {
1218 // We use the input index to store the index of the temporary in the user's temporary list.
1219 DCHECK(temp->IsTemp());
1220 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001221 }
1222
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001223 size_t GetMaxLifetimePosition() const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001224 return instructions_from_lifetime_position_.size() * 2 - 1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001225 }
1226
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001227 size_t GetNumberOfSsaValues() const {
1228 return number_of_ssa_values_;
1229 }
1230
Andreas Gampe7c3952f2015-02-19 18:21:24 -08001231 static constexpr const char* kLivenessPassName = "liveness";
1232
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001233 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +01001234 // Linearize the graph so that:
1235 // (1): a block is always after its dominator,
1236 // (2): blocks of loops are contiguous.
1237 // This creates a natural and efficient ordering when visualizing live ranges.
1238 void LinearizeGraph();
1239
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001240 // Give an SSA number to each instruction that defines a value used by another instruction,
1241 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001242 void NumberInstructions();
1243
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001244 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1245 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001246
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001247 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1248 // kill sets, that do not take into account backward branches.
1249 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001250
1251 // After computing the initial sets, this method does a fixed point
1252 // calculation over the live_in and live_out set to take into account
1253 // backwards branches.
1254 void ComputeLiveInAndLiveOutSets();
1255
1256 // Update the live_in set of the block and returns whether it has changed.
1257 bool UpdateLiveIn(const HBasicBlock& block);
1258
1259 // Update the live_out set of the block and returns whether it has changed.
1260 bool UpdateLiveOut(const HBasicBlock& block);
1261
Mingyao Yang718493c2015-07-22 15:56:34 -07001262 // Returns whether `instruction` in an HEnvironment held by `env_holder`
1263 // should be kept live by the HEnvironment.
1264 static bool ShouldBeLiveForEnvironment(HInstruction* env_holder,
1265 HInstruction* instruction) {
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001266 if (instruction == nullptr) return false;
Mingyao Yang718493c2015-07-22 15:56:34 -07001267 // A value that's not live in compiled code may still be needed in interpreter,
1268 // due to code motion, etc.
1269 if (env_holder->IsDeoptimize()) return true;
David Brazdil77a48ae2015-09-15 12:34:04 +00001270 // A value live at a throwing instruction in a try block may be copied by
1271 // the exception handler to its location at the top of the catch block.
1272 if (env_holder->CanThrowIntoCatchBlock()) return true;
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001273 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1274 return instruction->GetType() == Primitive::kPrimNot;
1275 }
1276
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +01001277 void CheckNoLiveInIrreducibleLoop(const HBasicBlock& block) const {
1278 if (!block.IsLoopHeader() || !block.GetLoopInformation()->IsIrreducible()) {
1279 return;
1280 }
1281 BitVector* live_in = GetLiveInSet(block);
1282 // To satisfy our liveness algorithm, we need to ensure loop headers of
1283 // irreducible loops do not have any live-in instructions, except constants
1284 // and the current method, which can be trivially re-materialized.
1285 for (uint32_t idx : live_in->Indexes()) {
1286 HInstruction* instruction = GetInstructionFromSsaIndex(idx);
1287 DCHECK(instruction->GetBlock()->IsEntryBlock()) << instruction->DebugName();
1288 DCHECK(!instruction->IsParameterValue());
1289 DCHECK(instruction->IsCurrentMethod() || instruction->IsConstant())
1290 << instruction->DebugName();
1291 }
1292 }
1293
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001294 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001295 CodeGenerator* const codegen_;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001296 ArenaVector<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001297
1298 // Temporary array used when computing live_in, live_out, and kill sets.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001299 ArenaVector<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001300
1301 // Temporary array used when inserting moves in the graph.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001302 ArenaVector<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001303 size_t number_of_ssa_values_;
1304
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001305 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
Nicolas Geoffray23a81882015-06-01 18:12:38 +01001306 ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001307
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001308 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1309};
1310
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001311} // namespace art
1312
1313#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_