blob: 346753b775fa16e2324a2a48617286c825b0c0b0 [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.
517 bool RequiresRegister() const { return FirstRegisterUse() != kNoLifetime; }
518
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000519 size_t FirstUseAfter(size_t position) const {
520 if (is_temp_) {
521 return position == GetStart() ? position : kNoLifetime;
522 }
523
Nicolas Geoffray57902602015-04-21 14:28:41 +0100524 if (IsDefiningPosition(position)) {
525 DCHECK(defined_by_->GetLocations()->Out().IsValid());
526 return position;
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100527 }
528
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000529 UsePosition* use = first_use_;
530 size_t end = GetEnd();
531 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100532 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100533 if (use_position > position) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100534 return use_position;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000535 }
536 use = use->GetNext();
537 }
538 return kNoLifetime;
539 }
540
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100541 UsePosition* GetFirstUse() const {
542 return first_use_;
543 }
544
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100545 UsePosition* GetFirstEnvironmentUse() const {
546 return first_env_use_;
547 }
548
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100549 Primitive::Type GetType() const {
550 return type_;
551 }
552
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100553 HInstruction* GetDefinedBy() const {
554 return defined_by_;
555 }
556
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100557 bool HasWillCallSafepoint() const {
558 for (SafepointPosition* safepoint = first_safepoint_;
559 safepoint != nullptr;
560 safepoint = safepoint->GetNext()) {
561 if (safepoint->GetLocations()->WillCall()) return true;
562 }
563 return false;
564 }
565
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100566 SafepointPosition* FindSafepointJustBefore(size_t position) const {
567 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
568 safepoint != nullptr;
569 previous = safepoint, safepoint = safepoint->GetNext()) {
570 if (safepoint->GetPosition() >= position) return previous;
571 }
572 return last_safepoint_;
573 }
574
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100575 /**
576 * Split this interval at `position`. This interval is changed to:
577 * [start ... position).
578 *
579 * The new interval covers:
580 * [position ... end)
581 */
582 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100583 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100584 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100585 DCHECK_GT(position, GetStart());
586
David Brazdil241a4862015-04-16 17:59:03 +0100587 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100588 // This range dies before `position`, no need to split.
589 return nullptr;
590 }
591
592 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100593 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
594 if (new_last_safepoint == nullptr) {
595 new_interval->first_safepoint_ = first_safepoint_;
596 new_interval->last_safepoint_ = last_safepoint_;
597 first_safepoint_ = last_safepoint_ = nullptr;
598 } else if (last_safepoint_ != new_last_safepoint) {
599 new_interval->last_safepoint_ = last_safepoint_;
600 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
601 DCHECK(new_interval->first_safepoint_ != nullptr);
602 last_safepoint_ = new_last_safepoint;
603 last_safepoint_->SetNext(nullptr);
604 }
605
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100606 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100607 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100608 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100609
610 new_interval->first_use_ = first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100611 new_interval->first_env_use_ = first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100612 LiveRange* current = first_range_;
613 LiveRange* previous = nullptr;
614 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000615 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100616 do {
617 if (position >= current->GetEnd()) {
618 // Move to next range.
619 previous = current;
620 current = current->next_;
621 } else if (position <= current->GetStart()) {
622 // If the previous range did not cover this position, we know position is in
623 // a lifetime hole. We can just break the first_range_ and last_range_ links
624 // and return the new interval.
625 DCHECK(previous != nullptr);
626 DCHECK(current != first_range_);
627 new_interval->last_range_ = last_range_;
628 last_range_ = previous;
629 previous->next_ = nullptr;
630 new_interval->first_range_ = current;
David Brazdil3fc992f2015-04-16 18:31:55 +0100631 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700632 // Search start point is inside `new_interval`. Change it to null
David Brazdil3fc992f2015-04-16 18:31:55 +0100633 // (i.e. the end of the interval) in the original interval.
634 range_search_start_ = nullptr;
635 }
636 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100637 return new_interval;
638 } else {
639 // This range covers position. We create a new last_range_ for this interval
640 // that covers last_range_->Start() and position. We also shorten the current
641 // range and make it the first range of the new interval.
642 DCHECK(position < current->GetEnd() && position > current->GetStart());
643 new_interval->last_range_ = last_range_;
644 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
645 if (previous != nullptr) {
646 previous->next_ = last_range_;
647 } else {
648 first_range_ = last_range_;
649 }
650 new_interval->first_range_ = current;
651 current->start_ = position;
David Brazdil3fc992f2015-04-16 18:31:55 +0100652 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
653 // Search start point is inside `new_interval`. Change it to `last_range`
654 // in the original interval. This is conservative but always correct.
655 range_search_start_ = last_range_;
656 }
657 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100658 return new_interval;
659 }
660 } while (current != nullptr);
661
662 LOG(FATAL) << "Unreachable";
663 return nullptr;
664 }
665
Nicolas Geoffray76905622014-09-25 14:39:26 +0100666 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100667 return GetStart() <= other->GetStart();
668 }
669
670 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100671 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100672 }
673
674 void Dump(std::ostream& stream) const {
675 stream << "ranges: { ";
676 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000677 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100678 current->Dump(stream);
679 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000680 current = current->GetNext();
681 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100682 stream << "}, uses: { ";
683 UsePosition* use = first_use_;
684 if (use != nullptr) {
685 do {
686 use->Dump(stream);
687 stream << " ";
688 } while ((use = use->GetNext()) != nullptr);
689 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100690 stream << "}, { ";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100691 use = first_env_use_;
692 if (use != nullptr) {
693 do {
694 use->Dump(stream);
695 stream << " ";
696 } while ((use = use->GetNext()) != nullptr);
697 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100698 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700699 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000700 stream << " is_low: " << IsLowInterval();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100701 stream << " is_high: " << IsHighInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100702 }
703
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700704 // Same as Dump, but adds context such as the instruction defining this interval, and
705 // the register currently assigned to this interval.
706 void DumpWithContext(std::ostream& stream, const CodeGenerator& codegen) const;
707
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100708 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000709 LiveInterval* GetLastSibling() {
710 LiveInterval* result = this;
711 while (result->next_sibling_ != nullptr) {
712 result = result->next_sibling_;
713 }
714 return result;
715 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100716
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100717 // Returns the first register hint that is at least free before
718 // the value contained in `free_until`. If none is found, returns
719 // `kNoRegister`.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100720 int FindFirstRegisterHint(size_t* free_until, const SsaLivenessAnalysis& liveness) const;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100721
722 // If there is enough at the definition site to find a register (for example
723 // it uses the same input as the first input), returns the register as a hint.
724 // Returns kNoRegister otherwise.
725 int FindHintAtDefinition() const;
726
727 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
728 // slots for spilling.
729 bool NeedsTwoSpillSlots() const;
730
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100731 bool IsFloatingPoint() const {
732 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
733 }
734
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100735 // Converts the location of the interval to a `Location` object.
736 Location ToLocation() const;
737
738 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000739 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100740
David Brazdil241a4862015-04-16 17:59:03 +0100741 // Finds the sibling that is defined at `position`.
742 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100743
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100744 // Returns whether `other` and `this` share the same kind of register.
745 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000746 bool SameRegisterKind(const LiveInterval& other) const {
747 return IsFloatingPoint() == other.IsFloatingPoint();
748 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100749
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000750 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000751 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000752 }
753
754 bool HasLowInterval() const {
755 return IsHighInterval();
756 }
757
758 LiveInterval* GetLowInterval() const {
759 DCHECK(HasLowInterval());
760 return high_or_low_interval_;
761 }
762
763 LiveInterval* GetHighInterval() const {
764 DCHECK(HasHighInterval());
765 return high_or_low_interval_;
766 }
767
768 bool IsHighInterval() const {
769 return GetParent()->is_high_interval_;
770 }
771
772 bool IsLowInterval() const {
773 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
774 }
775
776 void SetLowInterval(LiveInterval* low) {
777 DCHECK(IsHighInterval());
778 high_or_low_interval_ = low;
779 }
780
781 void SetHighInterval(LiveInterval* high) {
782 DCHECK(IsLowInterval());
783 high_or_low_interval_ = high;
784 }
785
786 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100787 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000788 DCHECK(!HasHighInterval());
789 DCHECK(!HasLowInterval());
790 high_or_low_interval_ = new (allocator_) LiveInterval(
791 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
792 high_or_low_interval_->high_or_low_interval_ = this;
793 if (first_range_ != nullptr) {
794 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
David Brazdilc08675c2015-04-17 15:49:51 +0100795 high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange();
David Brazdil3fc992f2015-04-16 18:31:55 +0100796 high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000797 }
798 if (first_use_ != nullptr) {
799 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
800 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100801
802 if (first_env_use_ != nullptr) {
803 high_or_low_interval_->first_env_use_ = first_env_use_->Dup(allocator_);
804 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000805 }
806
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000807 // Returns whether an interval, when it is non-split, is using
808 // the same register of one of its input.
809 bool IsUsingInputRegister() 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 if (defined_by_ != nullptr && !IsSplit()) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100812 for (const HInstruction* input : defined_by_->GetInputs()) {
813 LiveInterval* interval = input->GetLiveInterval();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000814
David Brazdil3fc992f2015-04-16 18:31:55 +0100815 // Find the interval that covers `defined_by`_. Calls to this function
816 // are made outside the linear scan, hence we need to use CoversSlow.
817 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000818 interval = interval->GetNextSibling();
819 }
820
821 // Check if both intervals have the same register of the same kind.
822 if (interval != nullptr
823 && interval->SameRegisterKind(*this)
824 && interval->GetRegister() == GetRegister()) {
825 return true;
826 }
827 }
828 }
829 return false;
830 }
831
832 // Returns whether an interval, when it is non-split, can safely use
833 // the same register of one of its input. Note that this method requires
834 // IsUsingInputRegister() to be true.
835 bool CanUseInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100836 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000837 DCHECK(IsUsingInputRegister());
838 if (defined_by_ != nullptr && !IsSplit()) {
839 LocationSummary* locations = defined_by_->GetLocations();
840 if (locations->OutputCanOverlapWithInputs()) {
841 return false;
842 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100843 for (const HInstruction* input : defined_by_->GetInputs()) {
844 LiveInterval* interval = input->GetLiveInterval();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000845
David Brazdil3fc992f2015-04-16 18:31:55 +0100846 // Find the interval that covers `defined_by`_. Calls to this function
847 // are made outside the linear scan, hence we need to use CoversSlow.
848 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000849 interval = interval->GetNextSibling();
850 }
851
852 if (interval != nullptr
853 && interval->SameRegisterKind(*this)
854 && interval->GetRegister() == GetRegister()) {
855 // We found the input that has the same register. Check if it is live after
856 // `defined_by`_.
David Brazdil3fc992f2015-04-16 18:31:55 +0100857 return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000858 }
859 }
860 }
861 LOG(FATAL) << "Unreachable";
862 UNREACHABLE();
863 }
864
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100865 void AddSafepoint(HInstruction* instruction) {
866 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
867 if (first_safepoint_ == nullptr) {
868 first_safepoint_ = last_safepoint_ = safepoint;
869 } else {
870 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
871 last_safepoint_->SetNext(safepoint);
872 last_safepoint_ = safepoint;
873 }
874 }
875
876 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100877 return first_safepoint_;
878 }
879
David Brazdil3fc992f2015-04-16 18:31:55 +0100880 // Resets the starting point for range-searching queries to the first range.
881 // Intervals must be reset prior to starting a new linear scan over them.
882 void ResetSearchCache() {
883 range_search_start_ = first_range_;
884 }
885
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700886 bool DefinitionRequiresRegister() const {
887 DCHECK(IsParent());
888 LocationSummary* locations = defined_by_->GetLocations();
889 Location location = locations->Out();
890 // This interval is the first interval of the instruction. If the output
891 // of the instruction requires a register, we return the position of that instruction
892 // as the first register use.
893 if (location.IsUnallocated()) {
894 if ((location.GetPolicy() == Location::kRequiresRegister)
895 || (location.GetPolicy() == Location::kSameAsFirstInput
896 && (locations->InAt(0).IsRegister()
897 || locations->InAt(0).IsRegisterPair()
898 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
899 return true;
900 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
901 || (location.GetPolicy() == Location::kSameAsFirstInput
902 && (locations->InAt(0).IsFpuRegister()
903 || locations->InAt(0).IsFpuRegisterPair()
904 || locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister))) {
905 return true;
906 }
907 } else if (location.IsRegister() || location.IsRegisterPair()) {
908 return true;
909 }
910 return false;
911 }
912
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100913 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700914 LiveInterval(ArenaAllocator* allocator,
915 Primitive::Type type,
916 HInstruction* defined_by = nullptr,
917 bool is_fixed = false,
918 int reg = kNoRegister,
919 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000920 bool is_slow_path_safepoint = false,
921 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700922 : allocator_(allocator),
923 first_range_(nullptr),
924 last_range_(nullptr),
David Brazdil3fc992f2015-04-16 18:31:55 +0100925 range_search_start_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100926 first_safepoint_(nullptr),
927 last_safepoint_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700928 first_use_(nullptr),
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100929 first_env_use_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700930 type_(type),
931 next_sibling_(nullptr),
932 parent_(this),
933 register_(reg),
934 spill_slot_(kNoSpillSlot),
935 is_fixed_(is_fixed),
936 is_temp_(is_temp),
937 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000938 is_high_interval_(is_high_interval),
939 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700940 defined_by_(defined_by) {}
941
David Brazdil3fc992f2015-04-16 18:31:55 +0100942 // Searches for a LiveRange that either covers the given position or is the
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700943 // first next LiveRange. Returns null if no such LiveRange exists. Ranges
David Brazdil3fc992f2015-04-16 18:31:55 +0100944 // known to end before `position` can be skipped with `search_start`.
945 LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000946 if (kIsDebugBuild) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100947 if (search_start != first_range_) {
948 // If we are not searching the entire list of ranges, make sure we do
949 // not skip the range we are searching for.
950 if (search_start == nullptr) {
951 DCHECK(IsDeadAt(position));
952 } else if (search_start->GetStart() > position) {
953 DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_));
954 }
David Brazdil5b8e6a52015-02-25 16:17:05 +0000955 }
956 }
957
David Brazdil3fc992f2015-04-16 18:31:55 +0100958 LiveRange* range;
959 for (range = search_start;
960 range != nullptr && range->GetEnd() <= position;
961 range = range->GetNext()) {
962 continue;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000963 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100964 return range;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000965 }
966
Nicolas Geoffray57902602015-04-21 14:28:41 +0100967 bool IsDefiningPosition(size_t position) const {
968 return IsParent() && (position == GetStart());
969 }
970
971 bool HasSynthesizeUseAt(size_t position) const {
972 UsePosition* use = first_use_;
973 while (use != nullptr) {
974 size_t use_position = use->GetPosition();
975 if ((use_position == position) && use->IsSynthesized()) {
976 return true;
977 }
978 if (use_position > position) break;
979 use = use->GetNext();
980 }
981 return false;
982 }
983
David Brazdiladf84912016-04-14 13:47:24 +0100984 bool IsLinearOrderWellFormed(const HGraph& graph) {
985 for (HBasicBlock* header : graph.GetBlocks()) {
David Brazdilfa3091e2016-04-19 10:10:17 +0100986 if (header == nullptr || !header->IsLoopHeader()) {
David Brazdiladf84912016-04-14 13:47:24 +0100987 continue;
988 }
989
990 HLoopInformation* loop = header->GetLoopInformation();
991 size_t num_blocks = loop->GetBlocks().NumSetBits();
992 size_t found_blocks = 0u;
993
994 for (HLinearOrderIterator it(graph); !it.Done(); it.Advance()) {
995 HBasicBlock* current = it.Current();
996 if (loop->Contains(*current)) {
997 found_blocks++;
998 if (found_blocks == 1u && current != header) {
999 // First block is not the header.
1000 return false;
1001 } else if (found_blocks == num_blocks && !loop->IsBackEdge(*current)) {
1002 // Last block is not a back edge.
1003 return false;
1004 }
1005 } else if (found_blocks != 0u && found_blocks != num_blocks) {
1006 // Blocks are not adjacent.
1007 return false;
1008 }
1009 }
1010 DCHECK_EQ(found_blocks, num_blocks);
1011 }
1012
1013 return true;
1014 }
1015
Nicolas Geoffray57902602015-04-21 14:28:41 +01001016 void AddBackEdgeUses(const HBasicBlock& block_at_use) {
1017 DCHECK(block_at_use.IsInLoop());
David Brazdil07b35102016-04-27 15:33:22 +01001018 if (block_at_use.GetGraph()->HasIrreducibleLoops()) {
1019 // Linear order may not be well formed when irreducible loops are present,
1020 // i.e. loop blocks may not be adjacent and a back edge may not be last,
1021 // which violates assumptions made in this method.
1022 return;
1023 }
1024
1025 DCHECK(IsLinearOrderWellFormed(*block_at_use.GetGraph()));
1026
Nicolas Geoffray57902602015-04-21 14:28:41 +01001027 // Add synthesized uses at the back edge of loops to help the register allocator.
1028 // Note that this method is called in decreasing liveness order, to faciliate adding
1029 // uses at the head of the `first_use_` linked list. Because below
1030 // we iterate from inner-most to outer-most, which is in increasing liveness order,
1031 // we need to take extra care of how the `first_use_` linked list is being updated.
1032 UsePosition* first_in_new_list = nullptr;
1033 UsePosition* last_in_new_list = nullptr;
1034 for (HLoopInformationOutwardIterator it(block_at_use);
1035 !it.Done();
1036 it.Advance()) {
1037 HLoopInformation* current = it.Current();
1038 if (GetDefinedBy()->GetLifetimePosition() >= current->GetHeader()->GetLifetimeStart()) {
1039 // This interval is defined in the loop. We can stop going outward.
1040 break;
1041 }
1042
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001043 // We're only adding a synthesized use at the last back edge. Adding syntehsized uses on
1044 // all back edges is not necessary: anything used in the loop will have its use at the
1045 // last back edge. If we want branches in a loop to have better register allocation than
1046 // another branch, then it is the linear order we should change.
1047 size_t back_edge_use_position = current->GetLifetimeEnd();
Nicolas Geoffray57902602015-04-21 14:28:41 +01001048 if ((first_use_ != nullptr) && (first_use_->GetPosition() <= back_edge_use_position)) {
1049 // There was a use already seen in this loop. Therefore the previous call to `AddUse`
1050 // already inserted the backedge use. We can stop going outward.
David Brazdil07b35102016-04-27 15:33:22 +01001051 DCHECK(HasSynthesizeUseAt(back_edge_use_position));
Nicolas Geoffray57902602015-04-21 14:28:41 +01001052 break;
1053 }
1054
David Brazdil07b35102016-04-27 15:33:22 +01001055 DCHECK(last_in_new_list == nullptr ||
1056 back_edge_use_position > last_in_new_list->GetPosition());
Nicolas Geoffray57902602015-04-21 14:28:41 +01001057
1058 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001059 /* user */ nullptr,
1060 /* environment */ nullptr,
1061 UsePosition::kNoInput,
1062 back_edge_use_position,
1063 /* next */ nullptr);
Nicolas Geoffray57902602015-04-21 14:28:41 +01001064
1065 if (last_in_new_list != nullptr) {
1066 // Going outward. The latest created use needs to point to the new use.
1067 last_in_new_list->SetNext(new_use);
1068 } else {
1069 // This is the inner-most loop.
1070 DCHECK_EQ(current, block_at_use.GetLoopInformation());
1071 first_in_new_list = new_use;
1072 }
1073 last_in_new_list = new_use;
1074 }
1075 // Link the newly created linked list with `first_use_`.
1076 if (last_in_new_list != nullptr) {
1077 last_in_new_list->SetNext(first_use_);
1078 first_use_ = first_in_new_list;
1079 }
1080 }
1081
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001082 ArenaAllocator* const allocator_;
1083
1084 // Ranges of this interval. We need a quick access to the last range to test
1085 // for liveness (see `IsDeadAt`).
1086 LiveRange* first_range_;
1087 LiveRange* last_range_;
1088
David Brazdil3fc992f2015-04-16 18:31:55 +01001089 // The first range at or after the current position of a linear scan. It is
1090 // used to optimize range-searching queries.
1091 LiveRange* range_search_start_;
1092
Nicolas Geoffray43af7282015-04-16 13:01:01 +01001093 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001094 SafepointPosition* first_safepoint_;
1095 SafepointPosition* last_safepoint_;
1096
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001097 // Uses of this interval. Note that this linked list is shared amongst siblings.
1098 UsePosition* first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001099 UsePosition* first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001100
1101 // The instruction type this interval corresponds to.
1102 const Primitive::Type type_;
1103
1104 // Live interval that is the result of a split.
1105 LiveInterval* next_sibling_;
1106
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001107 // The first interval from which split intervals come from.
1108 LiveInterval* parent_;
1109
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001110 // The register allocated to this interval.
1111 int register_;
1112
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001113 // The spill slot allocated to this interval.
1114 int spill_slot_;
1115
1116 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001117 const bool is_fixed_;
1118
1119 // Whether the interval is for a temporary.
1120 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001121
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001122 // Whether the interval is for a safepoint that calls on slow path.
1123 const bool is_slow_path_safepoint_;
1124
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001125 // Whether this interval is a synthesized interval for register pair.
1126 const bool is_high_interval_;
1127
1128 // If this interval needs a register pair, the high or low equivalent.
1129 // `is_high_interval_` tells whether this holds the low or the high.
1130 LiveInterval* high_or_low_interval_;
1131
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001132 // The instruction represented by this interval.
1133 HInstruction* const defined_by_;
1134
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001135 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001136 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001137
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001138 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1139
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001140 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
1141};
1142
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001143/**
1144 * Analysis that computes the liveness of instructions:
1145 *
1146 * (a) Non-environment uses of an instruction always make
1147 * the instruction live.
1148 * (b) Environment uses of an instruction whose type is
1149 * object (that is, non-primitive), make the instruction live.
1150 * This is due to having to keep alive objects that have
1151 * finalizers deleting native objects.
1152 * (c) When the graph has the debuggable property, environment uses
1153 * of an instruction that has a primitive type make the instruction live.
1154 * If the graph does not have the debuggable property, the environment
1155 * use has no effect, and may get a 'none' value after register allocation.
1156 *
1157 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
1158 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001159class SsaLivenessAnalysis : public ValueObject {
1160 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001161 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001162 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001163 codegen_(codegen),
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001164 block_infos_(graph->GetBlocks().size(),
1165 nullptr,
1166 graph->GetArena()->Adapter(kArenaAllocSsaLiveness)),
1167 instructions_from_ssa_index_(graph->GetArena()->Adapter(kArenaAllocSsaLiveness)),
1168 instructions_from_lifetime_position_(graph->GetArena()->Adapter(kArenaAllocSsaLiveness)),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001169 number_of_ssa_values_(0) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001170 }
1171
1172 void Analyze();
1173
1174 BitVector* GetLiveInSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001175 return &block_infos_[block.GetBlockId()]->live_in_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001176 }
1177
1178 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001179 return &block_infos_[block.GetBlockId()]->live_out_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001180 }
1181
1182 BitVector* GetKillSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001183 return &block_infos_[block.GetBlockId()]->kill_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001184 }
1185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001186 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001187 return instructions_from_ssa_index_[index];
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001188 }
1189
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001190 HInstruction* GetInstructionFromPosition(size_t index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001191 return instructions_from_lifetime_position_[index];
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001192 }
1193
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001194 HBasicBlock* GetBlockFromPosition(size_t index) const {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001195 HInstruction* instruction = GetInstructionFromPosition(index);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001196 if (instruction == nullptr) {
1197 // If we are at a block boundary, get the block following.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001198 instruction = GetInstructionFromPosition(index + 1);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001199 }
1200 return instruction->GetBlock();
1201 }
1202
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001203 bool IsAtBlockBoundary(size_t index) const {
1204 return GetInstructionFromPosition(index) == nullptr;
1205 }
1206
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001207 HInstruction* GetTempUser(LiveInterval* temp) const {
1208 // A temporary shares the same lifetime start as the instruction that requires it.
1209 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001210 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
1211 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
1212 return user;
1213 }
1214
1215 size_t GetTempIndex(LiveInterval* temp) const {
1216 // We use the input index to store the index of the temporary in the user's temporary list.
1217 DCHECK(temp->IsTemp());
1218 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001219 }
1220
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001221 size_t GetMaxLifetimePosition() const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001222 return instructions_from_lifetime_position_.size() * 2 - 1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001223 }
1224
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001225 size_t GetNumberOfSsaValues() const {
1226 return number_of_ssa_values_;
1227 }
1228
Andreas Gampe7c3952f2015-02-19 18:21:24 -08001229 static constexpr const char* kLivenessPassName = "liveness";
1230
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001231 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +01001232 // Linearize the graph so that:
1233 // (1): a block is always after its dominator,
1234 // (2): blocks of loops are contiguous.
1235 // This creates a natural and efficient ordering when visualizing live ranges.
1236 void LinearizeGraph();
1237
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001238 // Give an SSA number to each instruction that defines a value used by another instruction,
1239 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001240 void NumberInstructions();
1241
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001242 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1243 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001244
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001245 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1246 // kill sets, that do not take into account backward branches.
1247 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001248
1249 // After computing the initial sets, this method does a fixed point
1250 // calculation over the live_in and live_out set to take into account
1251 // backwards branches.
1252 void ComputeLiveInAndLiveOutSets();
1253
1254 // Update the live_in set of the block and returns whether it has changed.
1255 bool UpdateLiveIn(const HBasicBlock& block);
1256
1257 // Update the live_out set of the block and returns whether it has changed.
1258 bool UpdateLiveOut(const HBasicBlock& block);
1259
Mingyao Yang718493c2015-07-22 15:56:34 -07001260 // Returns whether `instruction` in an HEnvironment held by `env_holder`
1261 // should be kept live by the HEnvironment.
1262 static bool ShouldBeLiveForEnvironment(HInstruction* env_holder,
1263 HInstruction* instruction) {
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001264 if (instruction == nullptr) return false;
Mingyao Yang718493c2015-07-22 15:56:34 -07001265 // A value that's not live in compiled code may still be needed in interpreter,
1266 // due to code motion, etc.
1267 if (env_holder->IsDeoptimize()) return true;
David Brazdil77a48ae2015-09-15 12:34:04 +00001268 // A value live at a throwing instruction in a try block may be copied by
1269 // the exception handler to its location at the top of the catch block.
1270 if (env_holder->CanThrowIntoCatchBlock()) return true;
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001271 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1272 return instruction->GetType() == Primitive::kPrimNot;
1273 }
1274
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +01001275 void CheckNoLiveInIrreducibleLoop(const HBasicBlock& block) const {
1276 if (!block.IsLoopHeader() || !block.GetLoopInformation()->IsIrreducible()) {
1277 return;
1278 }
1279 BitVector* live_in = GetLiveInSet(block);
1280 // To satisfy our liveness algorithm, we need to ensure loop headers of
1281 // irreducible loops do not have any live-in instructions, except constants
1282 // and the current method, which can be trivially re-materialized.
1283 for (uint32_t idx : live_in->Indexes()) {
1284 HInstruction* instruction = GetInstructionFromSsaIndex(idx);
1285 DCHECK(instruction->GetBlock()->IsEntryBlock()) << instruction->DebugName();
1286 DCHECK(!instruction->IsParameterValue());
1287 DCHECK(instruction->IsCurrentMethod() || instruction->IsConstant())
1288 << instruction->DebugName();
1289 }
1290 }
1291
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001292 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001293 CodeGenerator* const codegen_;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001294 ArenaVector<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001295
1296 // Temporary array used when computing live_in, live_out, and kill sets.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001297 ArenaVector<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001298
1299 // Temporary array used when inserting moves in the graph.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001300 ArenaVector<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001301 size_t number_of_ssa_values_;
1302
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001303 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
Nicolas Geoffray23a81882015-06-01 18:12:38 +01001304 ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001305
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001306 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1307};
1308
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001309} // namespace art
1310
1311#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_