blob: b62bf4e5f938768aa84abf7c5a2b0bb2300c94a0 [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 Geoffray31d76b42014-06-09 15:02:22 +0100211 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100212 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
213 }
214
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100215 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
216 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100217 }
218
219 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700220 bool IsTemp() const { return is_temp_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700221 // This interval is the result of a split.
222 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100223
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000224 void AddTempUse(HInstruction* instruction, size_t temp_index) {
225 DCHECK(IsTemp());
226 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100227 DCHECK(first_env_use_ == nullptr) << "A temporary cannot have environment user";
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000228 size_t position = instruction->GetLifetimePosition();
229 first_use_ = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100230 instruction, /* environment */ nullptr, temp_index, position, first_use_);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000231 AddRange(position, position + 1);
232 }
233
David Brazdilb3e773e2016-01-26 11:28:37 +0000234 // Record use of an input. The use will be recorded as an environment use if
235 // `environment` is not null and as register use otherwise. If `actual_user`
236 // is specified, the use will be recorded at `actual_user`'s lifetime position.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000237 void AddUse(HInstruction* instruction,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100238 HEnvironment* environment,
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000239 size_t input_index,
David Brazdilb3e773e2016-01-26 11:28:37 +0000240 HInstruction* actual_user = nullptr,
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000241 bool keep_alive = false) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100242 bool is_environment = (environment != nullptr);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000243 LocationSummary* locations = instruction->GetLocations();
David Brazdilb3e773e2016-01-26 11:28:37 +0000244 if (actual_user == nullptr) {
245 actual_user = instruction;
246 }
247
248 // Set the use within the instruction.
249 size_t position = actual_user->GetLifetimePosition() + 1;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000250 if (!is_environment) {
251 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
252 // For fixed inputs and output same as input, the register allocator
253 // requires to have inputs die at the instruction, so that input moves use the
254 // location of the input just before that instruction (and not potential moves due
255 // to splitting).
David Brazdilb3e773e2016-01-26 11:28:37 +0000256 DCHECK_EQ(instruction, actual_user);
257 position = actual_user->GetLifetimePosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100258 } else if (!locations->InAt(input_index).IsValid()) {
259 return;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000260 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100261 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000262
Nicolas Geoffray57902602015-04-21 14:28:41 +0100263 if (!is_environment && instruction->IsInLoop()) {
264 AddBackEdgeUses(*instruction->GetBlock());
265 }
266
Nicolas Geoffray76905622014-09-25 14:39:26 +0100267 if ((first_use_ != nullptr)
David Brazdilb3e773e2016-01-26 11:28:37 +0000268 && (first_use_->GetUser() == actual_user)
Nicolas Geoffray76905622014-09-25 14:39:26 +0100269 && (first_use_->GetPosition() < position)) {
270 // The user uses the instruction multiple times, and one use dies before the other.
271 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000272 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100273 UsePosition* cursor = first_use_;
274 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
275 cursor = cursor->GetNext();
276 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100277 DCHECK(first_use_->GetPosition() + 1 == position);
278 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100279 instruction, nullptr /* environment */, input_index, position, cursor->GetNext());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100280 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100281 if (first_range_->GetEnd() == first_use_->GetPosition()) {
282 first_range_->end_ = position;
283 }
284 return;
285 }
286
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100287 if (is_environment) {
288 first_env_use_ = new (allocator_) UsePosition(
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100289 nullptr /* instruction */, environment, input_index, position, first_env_use_);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100290 } else {
291 first_use_ = new (allocator_) UsePosition(
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100292 instruction, nullptr /* environment */, input_index, position, first_use_);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100293 }
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000294
295 if (is_environment && !keep_alive) {
296 // If this environment use does not keep the instruction live, it does not
297 // affect the live range of that instruction.
298 return;
299 }
300
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100301 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100302 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100303 // First time we see a use of that interval.
David Brazdil3fc992f2015-04-16 18:31:55 +0100304 first_range_ = last_range_ = range_search_start_ =
305 new (allocator_) LiveRange(start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100306 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100307 // There is a use later in the same block or in a following block.
308 // Note that in such a case, `AddRange` for the whole blocks has been called
309 // before arriving in this method, and this is the reason the start of
310 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100311 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100312 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100313 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100314 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100315 // Note that the start of `first_range_` can be equal to `end`: two blocks
316 // having adjacent lifetime positions are not necessarily
317 // predecessor/successor. When two blocks are predecessor/successor, the
318 // liveness algorithm has called `AddRange` before arriving in this method,
319 // and the check line 205 would succeed.
David Brazdil3fc992f2015-04-16 18:31:55 +0100320 first_range_ = range_search_start_ =
321 new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100322 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100323 }
324
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100325 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100326 DCHECK(instruction->IsPhi());
Nicolas Geoffray57902602015-04-21 14:28:41 +0100327 if (block->IsInLoop()) {
328 AddBackEdgeUses(*block);
329 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100330 first_use_ = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100331 instruction, /* environment */ nullptr, input_index, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100332 }
333
334 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100335 if (first_range_ == nullptr) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100336 first_range_ = last_range_ = range_search_start_ =
337 new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100338 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100339 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100340 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100341 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
342 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100343 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100344 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100345 // There is a hole in the interval. Create a new range.
David Brazdil3fc992f2015-04-16 18:31:55 +0100346 first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100347 }
348 }
349
350 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100351 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000352 DCHECK_LE(start, first_range_->GetStart());
353 // Find the range that covers the positions after the loop.
354 LiveRange* after_loop = first_range_;
355 LiveRange* last_in_loop = nullptr;
356 while (after_loop != nullptr && after_loop->GetEnd() < end) {
357 DCHECK_LE(start, after_loop->GetStart());
358 last_in_loop = after_loop;
359 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100360 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000361 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100362 // Uses are only in the loop.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700363 first_range_ = last_range_ = range_search_start_ =
364 new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000365 } else if (after_loop->GetStart() <= end) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100366 first_range_ = range_search_start_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100367 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100368 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000369 } else {
370 // The use after the loop is after a lifetime hole.
371 DCHECK(last_in_loop != nullptr);
David Brazdil3fc992f2015-04-16 18:31:55 +0100372 first_range_ = range_search_start_ = last_in_loop;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000373 first_range_->start_ = start;
374 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100375 }
376 }
377
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100378 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100379 void SetSpillSlot(int slot) {
380 DCHECK(!is_fixed_);
381 DCHECK(!is_temp_);
382 spill_slot_ = slot;
383 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100384 int GetSpillSlot() const { return spill_slot_; }
385
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100386 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100387 if (first_range_ != nullptr) {
388 first_range_->start_ = from;
389 } else {
390 // Instruction without uses.
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100391 DCHECK(first_use_ == nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100392 DCHECK(from == defined_by_->GetLifetimePosition());
David Brazdil3fc992f2015-04-16 18:31:55 +0100393 first_range_ = last_range_ = range_search_start_ =
394 new (allocator_) LiveRange(from, from + 2, nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100395 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100396 }
397
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100398 LiveInterval* GetParent() const { return parent_; }
399
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100400 // Returns whether this interval is the parent interval, that is, the interval
401 // that starts where the HInstruction is defined.
402 bool IsParent() const { return parent_ == this; }
403
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100404 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000405 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100406
407 int GetRegister() const { return register_; }
408 void SetRegister(int reg) { register_ = reg; }
409 void ClearRegister() { register_ = kNoRegister; }
410 bool HasRegister() const { return register_ != kNoRegister; }
411
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100412 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100413 return GetEnd() <= position;
414 }
415
416 bool IsDefinedAt(size_t position) const {
417 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100418 }
419
David Brazdil3fc992f2015-04-16 18:31:55 +0100420 // Returns true if the interval contains a LiveRange covering `position`.
421 // The range at or immediately after the current position of linear scan
422 // is cached for better performance. If `position` can be smaller than
423 // that, CoversSlow should be used instead.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000424 bool Covers(size_t position) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100425 LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_);
426 range_search_start_ = candidate;
427 return (candidate != nullptr && candidate->GetStart() <= position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100428 }
429
David Brazdil3fc992f2015-04-16 18:31:55 +0100430 // Same as Covers but always tests all ranges.
431 bool CoversSlow(size_t position) const {
432 LiveRange* candidate = FindRangeAtOrAfter(position, first_range_);
433 return candidate != nullptr && candidate->GetStart() <= position;
434 }
435
436 // Returns the first intersection of this interval with `current`, which
437 // must be the interval currently being allocated by linear scan.
438 size_t FirstIntersectionWith(LiveInterval* current) const {
439 // Find the first range after the start of `current`. We use the search
440 // cache to improve performance.
441 DCHECK(GetStart() <= current->GetStart() || IsFixed());
442 LiveRange* other_range = current->first_range_;
443 LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_);
444 if (my_range == nullptr) {
445 return kNoLifetime;
446 }
447
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100448 // Advance both intervals and find the first matching range start in
449 // this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100450 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000451 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100452 my_range = my_range->GetNext();
453 if (my_range == nullptr) {
454 return kNoLifetime;
455 }
David Brazdil714e14f2015-02-25 11:57:05 +0000456 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100457 other_range = other_range->GetNext();
458 if (other_range == nullptr) {
459 return kNoLifetime;
460 }
David Brazdil714e14f2015-02-25 11:57:05 +0000461 } else {
462 DCHECK(my_range->IntersectsWith(*other_range));
463 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100464 }
465 } while (true);
466 }
467
468 size_t GetStart() const {
469 return first_range_->GetStart();
470 }
471
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100472 size_t GetEnd() const {
473 return last_range_->GetEnd();
474 }
475
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700476 size_t GetLength() const {
477 return GetEnd() - GetStart();
478 }
479
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100480 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100481 if (is_temp_) {
482 return position == GetStart() ? position : kNoLifetime;
483 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100484
485 if (IsDefiningPosition(position) && DefinitionRequiresRegister()) {
486 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100487 }
488
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100489 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100490 size_t end = GetEnd();
491 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100492 size_t use_position = use->GetPosition();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100493 if (use_position > position) {
Nicolas Geoffray57902602015-04-21 14:28:41 +0100494 if (use->RequiresRegister()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100495 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100496 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100497 }
498 use = use->GetNext();
499 }
500 return kNoLifetime;
501 }
502
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700503 // Returns the location of the first register use for this live interval,
504 // including a register definition if applicable.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100505 size_t FirstRegisterUse() const {
506 return FirstRegisterUseAfter(GetStart());
507 }
508
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700509 // Whether the interval requires a register rather than a stack location.
510 // If needed for performance, this could be cached.
Matthew Gharrity2ccae4a2016-08-12 16:10:45 +0000511 bool RequiresRegister() const {
512 return !HasRegister() && FirstRegisterUse() != kNoLifetime;
513 }
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700514
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000515 size_t FirstUseAfter(size_t position) const {
516 if (is_temp_) {
517 return position == GetStart() ? position : kNoLifetime;
518 }
519
Nicolas Geoffray57902602015-04-21 14:28:41 +0100520 if (IsDefiningPosition(position)) {
521 DCHECK(defined_by_->GetLocations()->Out().IsValid());
522 return position;
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100523 }
524
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000525 UsePosition* use = first_use_;
526 size_t end = GetEnd();
527 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100528 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100529 if (use_position > position) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100530 return use_position;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000531 }
532 use = use->GetNext();
533 }
534 return kNoLifetime;
535 }
536
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100537 UsePosition* GetFirstUse() const {
538 return first_use_;
539 }
540
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100541 UsePosition* GetFirstEnvironmentUse() const {
542 return first_env_use_;
543 }
544
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100545 Primitive::Type GetType() const {
546 return type_;
547 }
548
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100549 HInstruction* GetDefinedBy() const {
550 return defined_by_;
551 }
552
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100553 bool HasWillCallSafepoint() const {
554 for (SafepointPosition* safepoint = first_safepoint_;
555 safepoint != nullptr;
556 safepoint = safepoint->GetNext()) {
557 if (safepoint->GetLocations()->WillCall()) return true;
558 }
559 return false;
560 }
561
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100562 SafepointPosition* FindSafepointJustBefore(size_t position) const {
563 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
564 safepoint != nullptr;
565 previous = safepoint, safepoint = safepoint->GetNext()) {
566 if (safepoint->GetPosition() >= position) return previous;
567 }
568 return last_safepoint_;
569 }
570
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100571 /**
572 * Split this interval at `position`. This interval is changed to:
573 * [start ... position).
574 *
575 * The new interval covers:
576 * [position ... end)
577 */
578 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100579 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100580 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100581 DCHECK_GT(position, GetStart());
582
David Brazdil241a4862015-04-16 17:59:03 +0100583 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100584 // This range dies before `position`, no need to split.
585 return nullptr;
586 }
587
588 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100589 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
590 if (new_last_safepoint == nullptr) {
591 new_interval->first_safepoint_ = first_safepoint_;
592 new_interval->last_safepoint_ = last_safepoint_;
593 first_safepoint_ = last_safepoint_ = nullptr;
594 } else if (last_safepoint_ != new_last_safepoint) {
595 new_interval->last_safepoint_ = last_safepoint_;
596 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
597 DCHECK(new_interval->first_safepoint_ != nullptr);
598 last_safepoint_ = new_last_safepoint;
599 last_safepoint_->SetNext(nullptr);
600 }
601
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100602 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100603 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100604 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100605
606 new_interval->first_use_ = first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100607 new_interval->first_env_use_ = first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100608 LiveRange* current = first_range_;
609 LiveRange* previous = nullptr;
610 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000611 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100612 do {
613 if (position >= current->GetEnd()) {
614 // Move to next range.
615 previous = current;
616 current = current->next_;
617 } else if (position <= current->GetStart()) {
618 // If the previous range did not cover this position, we know position is in
619 // a lifetime hole. We can just break the first_range_ and last_range_ links
620 // and return the new interval.
621 DCHECK(previous != nullptr);
622 DCHECK(current != first_range_);
623 new_interval->last_range_ = last_range_;
624 last_range_ = previous;
625 previous->next_ = nullptr;
626 new_interval->first_range_ = current;
David Brazdil3fc992f2015-04-16 18:31:55 +0100627 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700628 // Search start point is inside `new_interval`. Change it to null
David Brazdil3fc992f2015-04-16 18:31:55 +0100629 // (i.e. the end of the interval) in the original interval.
630 range_search_start_ = nullptr;
631 }
632 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100633 return new_interval;
634 } else {
635 // This range covers position. We create a new last_range_ for this interval
636 // that covers last_range_->Start() and position. We also shorten the current
637 // range and make it the first range of the new interval.
638 DCHECK(position < current->GetEnd() && position > current->GetStart());
639 new_interval->last_range_ = last_range_;
640 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
641 if (previous != nullptr) {
642 previous->next_ = last_range_;
643 } else {
644 first_range_ = last_range_;
645 }
646 new_interval->first_range_ = current;
647 current->start_ = position;
David Brazdil3fc992f2015-04-16 18:31:55 +0100648 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
649 // Search start point is inside `new_interval`. Change it to `last_range`
650 // in the original interval. This is conservative but always correct.
651 range_search_start_ = last_range_;
652 }
653 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100654 return new_interval;
655 }
656 } while (current != nullptr);
657
658 LOG(FATAL) << "Unreachable";
659 return nullptr;
660 }
661
Nicolas Geoffray76905622014-09-25 14:39:26 +0100662 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100663 return GetStart() <= other->GetStart();
664 }
665
666 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100667 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100668 }
669
670 void Dump(std::ostream& stream) const {
671 stream << "ranges: { ";
672 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000673 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100674 current->Dump(stream);
675 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000676 current = current->GetNext();
677 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100678 stream << "}, uses: { ";
679 UsePosition* use = first_use_;
680 if (use != nullptr) {
681 do {
682 use->Dump(stream);
683 stream << " ";
684 } while ((use = use->GetNext()) != nullptr);
685 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100686 stream << "}, { ";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100687 use = first_env_use_;
688 if (use != nullptr) {
689 do {
690 use->Dump(stream);
691 stream << " ";
692 } while ((use = use->GetNext()) != nullptr);
693 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100694 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700695 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000696 stream << " is_low: " << IsLowInterval();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100697 stream << " is_high: " << IsHighInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100698 }
699
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700700 // Same as Dump, but adds context such as the instruction defining this interval, and
701 // the register currently assigned to this interval.
702 void DumpWithContext(std::ostream& stream, const CodeGenerator& codegen) const;
703
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100704 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000705 LiveInterval* GetLastSibling() {
706 LiveInterval* result = this;
707 while (result->next_sibling_ != nullptr) {
708 result = result->next_sibling_;
709 }
710 return result;
711 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100712
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100713 // Returns the first register hint that is at least free before
714 // the value contained in `free_until`. If none is found, returns
715 // `kNoRegister`.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100716 int FindFirstRegisterHint(size_t* free_until, const SsaLivenessAnalysis& liveness) const;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100717
718 // If there is enough at the definition site to find a register (for example
719 // it uses the same input as the first input), returns the register as a hint.
720 // Returns kNoRegister otherwise.
721 int FindHintAtDefinition() const;
722
723 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
724 // slots for spilling.
725 bool NeedsTwoSpillSlots() const;
726
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100727 bool IsFloatingPoint() const {
728 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
729 }
730
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100731 // Converts the location of the interval to a `Location` object.
732 Location ToLocation() const;
733
734 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000735 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100736
David Brazdil241a4862015-04-16 17:59:03 +0100737 // Finds the sibling that is defined at `position`.
738 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100739
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100740 // Returns whether `other` and `this` share the same kind of register.
741 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000742 bool SameRegisterKind(const LiveInterval& other) const {
743 return IsFloatingPoint() == other.IsFloatingPoint();
744 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100745
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000746 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000747 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000748 }
749
750 bool HasLowInterval() const {
751 return IsHighInterval();
752 }
753
754 LiveInterval* GetLowInterval() const {
755 DCHECK(HasLowInterval());
756 return high_or_low_interval_;
757 }
758
759 LiveInterval* GetHighInterval() const {
760 DCHECK(HasHighInterval());
761 return high_or_low_interval_;
762 }
763
764 bool IsHighInterval() const {
765 return GetParent()->is_high_interval_;
766 }
767
768 bool IsLowInterval() const {
769 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
770 }
771
772 void SetLowInterval(LiveInterval* low) {
773 DCHECK(IsHighInterval());
774 high_or_low_interval_ = low;
775 }
776
777 void SetHighInterval(LiveInterval* high) {
778 DCHECK(IsLowInterval());
779 high_or_low_interval_ = high;
780 }
781
782 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100783 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000784 DCHECK(!HasHighInterval());
785 DCHECK(!HasLowInterval());
786 high_or_low_interval_ = new (allocator_) LiveInterval(
Vladimir Marko70e97462016-08-09 11:04:26 +0100787 allocator_, type_, defined_by_, false, kNoRegister, is_temp, true);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000788 high_or_low_interval_->high_or_low_interval_ = this;
789 if (first_range_ != nullptr) {
790 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
David Brazdilc08675c2015-04-17 15:49:51 +0100791 high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange();
David Brazdil3fc992f2015-04-16 18:31:55 +0100792 high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000793 }
794 if (first_use_ != nullptr) {
795 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
796 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100797
798 if (first_env_use_ != nullptr) {
799 high_or_low_interval_->first_env_use_ = first_env_use_->Dup(allocator_);
800 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000801 }
802
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000803 // Returns whether an interval, when it is non-split, is using
804 // the same register of one of its input.
805 bool IsUsingInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100806 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000807 if (defined_by_ != nullptr && !IsSplit()) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100808 for (const HInstruction* input : defined_by_->GetInputs()) {
809 LiveInterval* interval = input->GetLiveInterval();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000810
David Brazdil3fc992f2015-04-16 18:31:55 +0100811 // Find the interval that covers `defined_by`_. Calls to this function
812 // are made outside the linear scan, hence we need to use CoversSlow.
813 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000814 interval = interval->GetNextSibling();
815 }
816
817 // Check if both intervals have the same register of the same kind.
818 if (interval != nullptr
819 && interval->SameRegisterKind(*this)
820 && interval->GetRegister() == GetRegister()) {
821 return true;
822 }
823 }
824 }
825 return false;
826 }
827
828 // Returns whether an interval, when it is non-split, can safely use
829 // the same register of one of its input. Note that this method requires
830 // IsUsingInputRegister() to be true.
831 bool CanUseInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100832 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000833 DCHECK(IsUsingInputRegister());
834 if (defined_by_ != nullptr && !IsSplit()) {
835 LocationSummary* locations = defined_by_->GetLocations();
836 if (locations->OutputCanOverlapWithInputs()) {
837 return false;
838 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100839 for (const HInstruction* input : defined_by_->GetInputs()) {
840 LiveInterval* interval = input->GetLiveInterval();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000841
David Brazdil3fc992f2015-04-16 18:31:55 +0100842 // Find the interval that covers `defined_by`_. Calls to this function
843 // are made outside the linear scan, hence we need to use CoversSlow.
844 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000845 interval = interval->GetNextSibling();
846 }
847
848 if (interval != nullptr
849 && interval->SameRegisterKind(*this)
850 && interval->GetRegister() == GetRegister()) {
851 // We found the input that has the same register. Check if it is live after
852 // `defined_by`_.
David Brazdil3fc992f2015-04-16 18:31:55 +0100853 return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000854 }
855 }
856 }
857 LOG(FATAL) << "Unreachable";
858 UNREACHABLE();
859 }
860
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100861 void AddSafepoint(HInstruction* instruction) {
862 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
863 if (first_safepoint_ == nullptr) {
864 first_safepoint_ = last_safepoint_ = safepoint;
865 } else {
866 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
867 last_safepoint_->SetNext(safepoint);
868 last_safepoint_ = safepoint;
869 }
870 }
871
872 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100873 return first_safepoint_;
874 }
875
David Brazdil3fc992f2015-04-16 18:31:55 +0100876 // Resets the starting point for range-searching queries to the first range.
877 // Intervals must be reset prior to starting a new linear scan over them.
878 void ResetSearchCache() {
879 range_search_start_ = first_range_;
880 }
881
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700882 bool DefinitionRequiresRegister() const {
883 DCHECK(IsParent());
884 LocationSummary* locations = defined_by_->GetLocations();
885 Location location = locations->Out();
886 // This interval is the first interval of the instruction. If the output
887 // of the instruction requires a register, we return the position of that instruction
888 // as the first register use.
889 if (location.IsUnallocated()) {
890 if ((location.GetPolicy() == Location::kRequiresRegister)
891 || (location.GetPolicy() == Location::kSameAsFirstInput
892 && (locations->InAt(0).IsRegister()
893 || locations->InAt(0).IsRegisterPair()
894 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
895 return true;
896 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
897 || (location.GetPolicy() == Location::kSameAsFirstInput
898 && (locations->InAt(0).IsFpuRegister()
899 || locations->InAt(0).IsFpuRegisterPair()
900 || locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister))) {
901 return true;
902 }
903 } else if (location.IsRegister() || location.IsRegisterPair()) {
904 return true;
905 }
906 return false;
907 }
908
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100909 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700910 LiveInterval(ArenaAllocator* allocator,
911 Primitive::Type type,
912 HInstruction* defined_by = nullptr,
913 bool is_fixed = false,
914 int reg = kNoRegister,
915 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000916 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700917 : allocator_(allocator),
918 first_range_(nullptr),
919 last_range_(nullptr),
David Brazdil3fc992f2015-04-16 18:31:55 +0100920 range_search_start_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100921 first_safepoint_(nullptr),
922 last_safepoint_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700923 first_use_(nullptr),
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100924 first_env_use_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700925 type_(type),
926 next_sibling_(nullptr),
927 parent_(this),
928 register_(reg),
929 spill_slot_(kNoSpillSlot),
930 is_fixed_(is_fixed),
931 is_temp_(is_temp),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000932 is_high_interval_(is_high_interval),
933 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700934 defined_by_(defined_by) {}
935
David Brazdil3fc992f2015-04-16 18:31:55 +0100936 // Searches for a LiveRange that either covers the given position or is the
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700937 // first next LiveRange. Returns null if no such LiveRange exists. Ranges
David Brazdil3fc992f2015-04-16 18:31:55 +0100938 // known to end before `position` can be skipped with `search_start`.
939 LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000940 if (kIsDebugBuild) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100941 if (search_start != first_range_) {
942 // If we are not searching the entire list of ranges, make sure we do
943 // not skip the range we are searching for.
944 if (search_start == nullptr) {
945 DCHECK(IsDeadAt(position));
946 } else if (search_start->GetStart() > position) {
947 DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_));
948 }
David Brazdil5b8e6a52015-02-25 16:17:05 +0000949 }
950 }
951
David Brazdil3fc992f2015-04-16 18:31:55 +0100952 LiveRange* range;
953 for (range = search_start;
954 range != nullptr && range->GetEnd() <= position;
955 range = range->GetNext()) {
956 continue;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000957 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100958 return range;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000959 }
960
Nicolas Geoffray57902602015-04-21 14:28:41 +0100961 bool IsDefiningPosition(size_t position) const {
962 return IsParent() && (position == GetStart());
963 }
964
965 bool HasSynthesizeUseAt(size_t position) const {
966 UsePosition* use = first_use_;
967 while (use != nullptr) {
968 size_t use_position = use->GetPosition();
969 if ((use_position == position) && use->IsSynthesized()) {
970 return true;
971 }
972 if (use_position > position) break;
973 use = use->GetNext();
974 }
975 return false;
976 }
977
978 void AddBackEdgeUses(const HBasicBlock& block_at_use) {
979 DCHECK(block_at_use.IsInLoop());
David Brazdil07b35102016-04-27 15:33:22 +0100980 if (block_at_use.GetGraph()->HasIrreducibleLoops()) {
981 // Linear order may not be well formed when irreducible loops are present,
982 // i.e. loop blocks may not be adjacent and a back edge may not be last,
983 // which violates assumptions made in this method.
984 return;
985 }
986
Nicolas Geoffray57902602015-04-21 14:28:41 +0100987 // Add synthesized uses at the back edge of loops to help the register allocator.
988 // Note that this method is called in decreasing liveness order, to faciliate adding
989 // uses at the head of the `first_use_` linked list. Because below
990 // we iterate from inner-most to outer-most, which is in increasing liveness order,
991 // we need to take extra care of how the `first_use_` linked list is being updated.
992 UsePosition* first_in_new_list = nullptr;
993 UsePosition* last_in_new_list = nullptr;
994 for (HLoopInformationOutwardIterator it(block_at_use);
995 !it.Done();
996 it.Advance()) {
997 HLoopInformation* current = it.Current();
998 if (GetDefinedBy()->GetLifetimePosition() >= current->GetHeader()->GetLifetimeStart()) {
999 // This interval is defined in the loop. We can stop going outward.
1000 break;
1001 }
1002
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001003 // We're only adding a synthesized use at the last back edge. Adding syntehsized uses on
1004 // all back edges is not necessary: anything used in the loop will have its use at the
1005 // last back edge. If we want branches in a loop to have better register allocation than
1006 // another branch, then it is the linear order we should change.
1007 size_t back_edge_use_position = current->GetLifetimeEnd();
Nicolas Geoffray57902602015-04-21 14:28:41 +01001008 if ((first_use_ != nullptr) && (first_use_->GetPosition() <= back_edge_use_position)) {
1009 // There was a use already seen in this loop. Therefore the previous call to `AddUse`
1010 // already inserted the backedge use. We can stop going outward.
David Brazdil07b35102016-04-27 15:33:22 +01001011 DCHECK(HasSynthesizeUseAt(back_edge_use_position));
Nicolas Geoffray57902602015-04-21 14:28:41 +01001012 break;
1013 }
1014
David Brazdil07b35102016-04-27 15:33:22 +01001015 DCHECK(last_in_new_list == nullptr ||
1016 back_edge_use_position > last_in_new_list->GetPosition());
Nicolas Geoffray57902602015-04-21 14:28:41 +01001017
1018 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001019 /* user */ nullptr,
1020 /* environment */ nullptr,
1021 UsePosition::kNoInput,
1022 back_edge_use_position,
1023 /* next */ nullptr);
Nicolas Geoffray57902602015-04-21 14:28:41 +01001024
1025 if (last_in_new_list != nullptr) {
1026 // Going outward. The latest created use needs to point to the new use.
1027 last_in_new_list->SetNext(new_use);
1028 } else {
1029 // This is the inner-most loop.
1030 DCHECK_EQ(current, block_at_use.GetLoopInformation());
1031 first_in_new_list = new_use;
1032 }
1033 last_in_new_list = new_use;
1034 }
1035 // Link the newly created linked list with `first_use_`.
1036 if (last_in_new_list != nullptr) {
1037 last_in_new_list->SetNext(first_use_);
1038 first_use_ = first_in_new_list;
1039 }
1040 }
1041
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001042 ArenaAllocator* const allocator_;
1043
1044 // Ranges of this interval. We need a quick access to the last range to test
1045 // for liveness (see `IsDeadAt`).
1046 LiveRange* first_range_;
1047 LiveRange* last_range_;
1048
David Brazdil3fc992f2015-04-16 18:31:55 +01001049 // The first range at or after the current position of a linear scan. It is
1050 // used to optimize range-searching queries.
1051 LiveRange* range_search_start_;
1052
Nicolas Geoffray43af7282015-04-16 13:01:01 +01001053 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001054 SafepointPosition* first_safepoint_;
1055 SafepointPosition* last_safepoint_;
1056
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001057 // Uses of this interval. Note that this linked list is shared amongst siblings.
1058 UsePosition* first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001059 UsePosition* first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001060
1061 // The instruction type this interval corresponds to.
1062 const Primitive::Type type_;
1063
1064 // Live interval that is the result of a split.
1065 LiveInterval* next_sibling_;
1066
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001067 // The first interval from which split intervals come from.
1068 LiveInterval* parent_;
1069
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001070 // The register allocated to this interval.
1071 int register_;
1072
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001073 // The spill slot allocated to this interval.
1074 int spill_slot_;
1075
1076 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001077 const bool is_fixed_;
1078
1079 // Whether the interval is for a temporary.
1080 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001081
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001082 // Whether this interval is a synthesized interval for register pair.
1083 const bool is_high_interval_;
1084
1085 // If this interval needs a register pair, the high or low equivalent.
1086 // `is_high_interval_` tells whether this holds the low or the high.
1087 LiveInterval* high_or_low_interval_;
1088
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001089 // The instruction represented by this interval.
1090 HInstruction* const defined_by_;
1091
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001092 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001093 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001094
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001095 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1096
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001097 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
1098};
1099
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001100/**
1101 * Analysis that computes the liveness of instructions:
1102 *
1103 * (a) Non-environment uses of an instruction always make
1104 * the instruction live.
1105 * (b) Environment uses of an instruction whose type is
1106 * object (that is, non-primitive), make the instruction live.
1107 * This is due to having to keep alive objects that have
1108 * finalizers deleting native objects.
1109 * (c) When the graph has the debuggable property, environment uses
1110 * of an instruction that has a primitive type make the instruction live.
1111 * If the graph does not have the debuggable property, the environment
1112 * use has no effect, and may get a 'none' value after register allocation.
1113 *
1114 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
1115 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001116class SsaLivenessAnalysis : public ValueObject {
1117 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001118 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001119 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001120 codegen_(codegen),
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001121 block_infos_(graph->GetBlocks().size(),
1122 nullptr,
1123 graph->GetArena()->Adapter(kArenaAllocSsaLiveness)),
1124 instructions_from_ssa_index_(graph->GetArena()->Adapter(kArenaAllocSsaLiveness)),
1125 instructions_from_lifetime_position_(graph->GetArena()->Adapter(kArenaAllocSsaLiveness)),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001126 number_of_ssa_values_(0) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001127 }
1128
1129 void Analyze();
1130
1131 BitVector* GetLiveInSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001132 return &block_infos_[block.GetBlockId()]->live_in_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001133 }
1134
1135 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001136 return &block_infos_[block.GetBlockId()]->live_out_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001137 }
1138
1139 BitVector* GetKillSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001140 return &block_infos_[block.GetBlockId()]->kill_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001141 }
1142
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001143 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001144 return instructions_from_ssa_index_[index];
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001145 }
1146
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001147 HInstruction* GetInstructionFromPosition(size_t index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001148 return instructions_from_lifetime_position_[index];
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001149 }
1150
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001151 HBasicBlock* GetBlockFromPosition(size_t index) const {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001152 HInstruction* instruction = GetInstructionFromPosition(index);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001153 if (instruction == nullptr) {
1154 // If we are at a block boundary, get the block following.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001155 instruction = GetInstructionFromPosition(index + 1);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001156 }
1157 return instruction->GetBlock();
1158 }
1159
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001160 bool IsAtBlockBoundary(size_t index) const {
1161 return GetInstructionFromPosition(index) == nullptr;
1162 }
1163
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001164 HInstruction* GetTempUser(LiveInterval* temp) const {
1165 // A temporary shares the same lifetime start as the instruction that requires it.
1166 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001167 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
1168 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
1169 return user;
1170 }
1171
1172 size_t GetTempIndex(LiveInterval* temp) const {
1173 // We use the input index to store the index of the temporary in the user's temporary list.
1174 DCHECK(temp->IsTemp());
1175 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001176 }
1177
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001178 size_t GetMaxLifetimePosition() const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001179 return instructions_from_lifetime_position_.size() * 2 - 1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001180 }
1181
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001182 size_t GetNumberOfSsaValues() const {
1183 return number_of_ssa_values_;
1184 }
1185
Andreas Gampe7c3952f2015-02-19 18:21:24 -08001186 static constexpr const char* kLivenessPassName = "liveness";
1187
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001188 private:
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001189 // Give an SSA number to each instruction that defines a value used by another instruction,
1190 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001191 void NumberInstructions();
1192
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001193 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1194 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001195
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001196 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1197 // kill sets, that do not take into account backward branches.
1198 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001199
1200 // After computing the initial sets, this method does a fixed point
1201 // calculation over the live_in and live_out set to take into account
1202 // backwards branches.
1203 void ComputeLiveInAndLiveOutSets();
1204
1205 // Update the live_in set of the block and returns whether it has changed.
1206 bool UpdateLiveIn(const HBasicBlock& block);
1207
1208 // Update the live_out set of the block and returns whether it has changed.
1209 bool UpdateLiveOut(const HBasicBlock& block);
1210
Mingyao Yang718493c2015-07-22 15:56:34 -07001211 // Returns whether `instruction` in an HEnvironment held by `env_holder`
1212 // should be kept live by the HEnvironment.
1213 static bool ShouldBeLiveForEnvironment(HInstruction* env_holder,
1214 HInstruction* instruction) {
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001215 if (instruction == nullptr) return false;
Mingyao Yang718493c2015-07-22 15:56:34 -07001216 // A value that's not live in compiled code may still be needed in interpreter,
1217 // due to code motion, etc.
1218 if (env_holder->IsDeoptimize()) return true;
David Brazdil77a48ae2015-09-15 12:34:04 +00001219 // A value live at a throwing instruction in a try block may be copied by
1220 // the exception handler to its location at the top of the catch block.
1221 if (env_holder->CanThrowIntoCatchBlock()) return true;
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001222 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1223 return instruction->GetType() == Primitive::kPrimNot;
1224 }
1225
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +01001226 void CheckNoLiveInIrreducibleLoop(const HBasicBlock& block) const {
1227 if (!block.IsLoopHeader() || !block.GetLoopInformation()->IsIrreducible()) {
1228 return;
1229 }
1230 BitVector* live_in = GetLiveInSet(block);
1231 // To satisfy our liveness algorithm, we need to ensure loop headers of
1232 // irreducible loops do not have any live-in instructions, except constants
1233 // and the current method, which can be trivially re-materialized.
1234 for (uint32_t idx : live_in->Indexes()) {
1235 HInstruction* instruction = GetInstructionFromSsaIndex(idx);
1236 DCHECK(instruction->GetBlock()->IsEntryBlock()) << instruction->DebugName();
1237 DCHECK(!instruction->IsParameterValue());
1238 DCHECK(instruction->IsCurrentMethod() || instruction->IsConstant())
1239 << instruction->DebugName();
1240 }
1241 }
1242
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001243 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001244 CodeGenerator* const codegen_;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001245 ArenaVector<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001246
1247 // Temporary array used when computing live_in, live_out, and kill sets.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001248 ArenaVector<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001249
1250 // Temporary array used when inserting moves in the graph.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001251 ArenaVector<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001252 size_t number_of_ssa_values_;
1253
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001254 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
Nicolas Geoffray23a81882015-06-01 18:12:38 +01001255 ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001256
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001257 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1258};
1259
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001260} // namespace art
1261
1262#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_