blob: ca08d5b3e64363f79d814c3ffcda34c5387ba592 [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"
21
22namespace art {
23
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010024class CodeGenerator;
25
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010026static constexpr int kNoRegister = -1;
27
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070028class BlockInfo : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010029 public:
30 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
31 : block_(block),
32 live_in_(allocator, number_of_ssa_values, false),
33 live_out_(allocator, number_of_ssa_values, false),
34 kill_(allocator, number_of_ssa_values, false) {
Ian Rogerscf7f1912014-10-22 22:06:39 -070035 UNUSED(block_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010036 live_in_.ClearAllBits();
37 live_out_.ClearAllBits();
38 kill_.ClearAllBits();
39 }
40
41 private:
42 const HBasicBlock& block_;
43 ArenaBitVector live_in_;
44 ArenaBitVector live_out_;
45 ArenaBitVector kill_;
46
47 friend class SsaLivenessAnalysis;
48
49 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
50};
51
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010052/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010054 * is live.
55 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070056class LiveRange FINAL : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010057 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010058 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010060 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061 }
62
63 size_t GetStart() const { return start_; }
64 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010065 LiveRange* GetNext() const { return next_; }
66
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070067 bool IntersectsWith(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010068 return (start_ >= other.start_ && start_ < other.end_)
69 || (other.start_ >= start_ && other.start_ < end_);
70 }
71
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070072 bool IsBefore(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010073 return end_ <= other.start_;
74 }
75
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070076 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010077 stream << "[" << start_ << ", " << end_ << ")";
78 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010079
80 private:
81 size_t start_;
Nicolas Geoffray76905622014-09-25 14:39:26 +010082 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010083 LiveRange* next_;
84
85 friend class LiveInterval;
86
87 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010088};
89
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010090/**
91 * A use position represents a live interval use at a given position.
92 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070093class UsePosition : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010094 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010095 UsePosition(HInstruction* user,
96 size_t input_index,
97 bool is_environment,
98 size_t position,
99 UsePosition* next)
100 : user_(user),
101 input_index_(input_index),
102 is_environment_(is_environment),
103 position_(position),
104 next_(next) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100105 DCHECK(user->IsPhi()
106 || (GetPosition() == user->GetLifetimePosition() + 1)
107 || (GetPosition() == user->GetLifetimePosition()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100108 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
109 }
110
111 size_t GetPosition() const { return position_; }
112
113 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100114 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100115
116 HInstruction* GetUser() const { return user_; }
117
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100118 bool GetIsEnvironment() const { return is_environment_; }
119
120 size_t GetInputIndex() const { return input_index_; }
121
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100122 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100123 stream << position_;
124 }
125
126 private:
127 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100128 const size_t input_index_;
129 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100130 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100131 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100132
133 DISALLOW_COPY_AND_ASSIGN(UsePosition);
134};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100135
136/**
137 * An interval is a list of disjoint live ranges where an instruction is live.
138 * Each instruction that has uses gets an interval.
139 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700140class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100141 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700142 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
143 Primitive::Type type,
144 HInstruction* instruction = nullptr) {
145 return new (allocator) LiveInterval(allocator, type, instruction);
146 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100147
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100148 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
149 return new (allocator) LiveInterval(
150 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
151 }
152
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100153 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100154 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
155 }
156
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100157 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
158 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100159 }
160
161 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700162 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100163 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700164 // This interval is the result of a split.
165 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100166
167 void AddUse(HInstruction* instruction, size_t input_index, bool is_environment) {
168 // Set the use within the instruction.
Nicolas Geoffray76905622014-09-25 14:39:26 +0100169 size_t position = instruction->GetLifetimePosition();
170 if (instruction->GetLocations()->InputOverlapsWithOutputOrTemp(input_index, is_environment)) {
171 // If it overlaps, we need to make sure the user will not try to allocate a temp
172 // or its output to the same register.
173 ++position;
174 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100175 if ((first_use_ != nullptr)
176 && (first_use_->GetUser() == instruction)
177 && (first_use_->GetPosition() < position)) {
178 // The user uses the instruction multiple times, and one use dies before the other.
179 // We update the use list so that the latter is first.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100180 UsePosition* cursor = first_use_;
181 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
182 cursor = cursor->GetNext();
183 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100184 DCHECK(first_use_->GetPosition() + 1 == position);
185 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100186 instruction, input_index, is_environment, position, cursor->GetNext());
187 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100188 if (first_range_->GetEnd() == first_use_->GetPosition()) {
189 first_range_->end_ = position;
190 }
191 return;
192 }
193
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100194 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100195 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100196 // First time we see a use of that interval.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100197 first_range_ = last_range_ = new (allocator_) LiveRange(
198 start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100199 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100200 // There is a use later in the same block or in a following block.
201 // Note that in such a case, `AddRange` for the whole blocks has been called
202 // before arriving in this method, and this is the reason the start of
203 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100204 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100205 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100206 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100207 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100208 // Note that the start of `first_range_` can be equal to `end`: two blocks
209 // having adjacent lifetime positions are not necessarily
210 // predecessor/successor. When two blocks are predecessor/successor, the
211 // liveness algorithm has called `AddRange` before arriving in this method,
212 // and the check line 205 would succeed.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100213 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100214 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100215 first_use_ = new (allocator_) UsePosition(
216 instruction, input_index, is_environment, position, first_use_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100217 }
218
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100219 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100220 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100221 first_use_ = new (allocator_) UsePosition(
222 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100223 }
224
225 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100226 if (first_range_ == nullptr) {
227 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
228 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100229 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100230 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100231 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
232 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100233 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100234 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100235 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100236 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100237 }
238 }
239
240 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100241 DCHECK(first_range_ != nullptr);
242 while (first_range_ != nullptr && first_range_->GetEnd() < end) {
243 DCHECK_LE(start, first_range_->GetStart());
244 first_range_ = first_range_->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100245 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100246 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100247 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100248 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100249 } else {
250 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100251 first_range_->start_ = start;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100252 }
253 }
254
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100255 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100256 void SetSpillSlot(int slot) {
257 DCHECK(!is_fixed_);
258 DCHECK(!is_temp_);
259 spill_slot_ = slot;
260 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100261 int GetSpillSlot() const { return spill_slot_; }
262
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100263 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100264 if (first_range_ != nullptr) {
265 first_range_->start_ = from;
266 } else {
267 // Instruction without uses.
268 DCHECK(!defined_by_->HasUses());
269 DCHECK(from == defined_by_->GetLifetimePosition());
270 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
271 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100272 }
273
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100274 LiveInterval* GetParent() const { return parent_; }
275
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100276 LiveRange* GetFirstRange() const { return first_range_; }
277
278 int GetRegister() const { return register_; }
279 void SetRegister(int reg) { register_ = reg; }
280 void ClearRegister() { register_ = kNoRegister; }
281 bool HasRegister() const { return register_ != kNoRegister; }
282
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100283 bool IsDeadAt(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100284 return last_range_->GetEnd() <= position;
285 }
286
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100287 bool Covers(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100288 if (IsDeadAt(position)) {
289 return false;
290 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100291 LiveRange* current = first_range_;
292 while (current != nullptr) {
293 if (position >= current->GetStart() && position < current->GetEnd()) {
294 return true;
295 }
296 current = current->GetNext();
297 }
298 return false;
299 }
300
301 /**
302 * Returns the first intersection of this interval with `other`.
303 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100304 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100305 // Advance both intervals and find the first matching range start in
306 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100307 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100308 LiveRange* other_range = other->first_range_;
309 do {
310 if (my_range->IntersectsWith(*other_range)) {
311 return std::max(my_range->GetStart(), other_range->GetStart());
312 } else if (my_range->IsBefore(*other_range)) {
313 my_range = my_range->GetNext();
314 if (my_range == nullptr) {
315 return kNoLifetime;
316 }
317 } else {
318 DCHECK(other_range->IsBefore(*my_range));
319 other_range = other_range->GetNext();
320 if (other_range == nullptr) {
321 return kNoLifetime;
322 }
323 }
324 } while (true);
325 }
326
327 size_t GetStart() const {
328 return first_range_->GetStart();
329 }
330
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100331 size_t GetEnd() const {
332 return last_range_->GetEnd();
333 }
334
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100335 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100336 if (is_temp_) {
337 return position == GetStart() ? position : kNoLifetime;
338 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100339 if (position == GetStart() && defined_by_ != nullptr) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100340 LocationSummary* locations = defined_by_->GetLocations();
341 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100342 // This interval is the first interval of the instruction. If the output
343 // of the instruction requires a register, we return the position of that instruction
344 // as the first register use.
345 if (location.IsUnallocated()) {
346 if ((location.GetPolicy() == Location::kRequiresRegister)
347 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100348 && locations->InAt(0).GetPolicy() == Location::kRequiresRegister)) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100349 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100350 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
351 || (location.GetPolicy() == Location::kSameAsFirstInput
352 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
353 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100354 }
355 }
356 }
357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100358 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100359 size_t end = GetEnd();
360 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100361 size_t use_position = use->GetPosition();
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100362 if (use_position > position && !use->GetIsEnvironment()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100363 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100364 if (location.IsUnallocated()
365 && (location.GetPolicy() == Location::kRequiresRegister
366 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100367 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100368 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100369 }
370 use = use->GetNext();
371 }
372 return kNoLifetime;
373 }
374
375 size_t FirstRegisterUse() const {
376 return FirstRegisterUseAfter(GetStart());
377 }
378
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100379 UsePosition* GetFirstUse() const {
380 return first_use_;
381 }
382
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100383 Primitive::Type GetType() const {
384 return type_;
385 }
386
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100387 HInstruction* GetDefinedBy() const {
388 return defined_by_;
389 }
390
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100391 /**
392 * Split this interval at `position`. This interval is changed to:
393 * [start ... position).
394 *
395 * The new interval covers:
396 * [position ... end)
397 */
398 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100399 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100400 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100401 DCHECK_GT(position, GetStart());
402
403 if (last_range_->GetEnd() <= position) {
404 // This range dies before `position`, no need to split.
405 return nullptr;
406 }
407
408 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100409 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100410 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100411 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100412
413 new_interval->first_use_ = first_use_;
414 LiveRange* current = first_range_;
415 LiveRange* previous = nullptr;
416 // Iterate over the ranges, and either find a range that covers this position, or
417 // a two ranges in between this position (that is, the position is in a lifetime hole).
418 do {
419 if (position >= current->GetEnd()) {
420 // Move to next range.
421 previous = current;
422 current = current->next_;
423 } else if (position <= current->GetStart()) {
424 // If the previous range did not cover this position, we know position is in
425 // a lifetime hole. We can just break the first_range_ and last_range_ links
426 // and return the new interval.
427 DCHECK(previous != nullptr);
428 DCHECK(current != first_range_);
429 new_interval->last_range_ = last_range_;
430 last_range_ = previous;
431 previous->next_ = nullptr;
432 new_interval->first_range_ = current;
433 return new_interval;
434 } else {
435 // This range covers position. We create a new last_range_ for this interval
436 // that covers last_range_->Start() and position. We also shorten the current
437 // range and make it the first range of the new interval.
438 DCHECK(position < current->GetEnd() && position > current->GetStart());
439 new_interval->last_range_ = last_range_;
440 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
441 if (previous != nullptr) {
442 previous->next_ = last_range_;
443 } else {
444 first_range_ = last_range_;
445 }
446 new_interval->first_range_ = current;
447 current->start_ = position;
448 return new_interval;
449 }
450 } while (current != nullptr);
451
452 LOG(FATAL) << "Unreachable";
453 return nullptr;
454 }
455
Nicolas Geoffray76905622014-09-25 14:39:26 +0100456 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100457 return GetStart() <= other->GetStart();
458 }
459
460 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100461 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100462 }
463
464 void Dump(std::ostream& stream) const {
465 stream << "ranges: { ";
466 LiveRange* current = first_range_;
467 do {
468 current->Dump(stream);
469 stream << " ";
470 } while ((current = current->GetNext()) != nullptr);
471 stream << "}, uses: { ";
472 UsePosition* use = first_use_;
473 if (use != nullptr) {
474 do {
475 use->Dump(stream);
476 stream << " ";
477 } while ((use = use->GetNext()) != nullptr);
478 }
479 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700480 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100481 }
482
483 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100484
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100485 // Returns the first register hint that is at least free before
486 // the value contained in `free_until`. If none is found, returns
487 // `kNoRegister`.
488 int FindFirstRegisterHint(size_t* free_until) const;
489
490 // If there is enough at the definition site to find a register (for example
491 // it uses the same input as the first input), returns the register as a hint.
492 // Returns kNoRegister otherwise.
493 int FindHintAtDefinition() const;
494
495 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
496 // slots for spilling.
497 bool NeedsTwoSpillSlots() const;
498
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100499 bool IsFloatingPoint() const {
500 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
501 }
502
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100503 // Converts the location of the interval to a `Location` object.
504 Location ToLocation() const;
505
506 // Returns the location of the interval following its siblings at `position`.
507 Location GetLocationAt(size_t position) const;
508
509 // Finds the interval that covers `position`.
510 const LiveInterval& GetIntervalAt(size_t position) const;
511
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100512 // Returns whether `other` and `this` share the same kind of register.
513 bool SameRegisterKind(Location other) const;
514
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100515 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700516 LiveInterval(ArenaAllocator* allocator,
517 Primitive::Type type,
518 HInstruction* defined_by = nullptr,
519 bool is_fixed = false,
520 int reg = kNoRegister,
521 bool is_temp = false,
522 bool is_slow_path_safepoint = false)
523 : allocator_(allocator),
524 first_range_(nullptr),
525 last_range_(nullptr),
526 first_use_(nullptr),
527 type_(type),
528 next_sibling_(nullptr),
529 parent_(this),
530 register_(reg),
531 spill_slot_(kNoSpillSlot),
532 is_fixed_(is_fixed),
533 is_temp_(is_temp),
534 is_slow_path_safepoint_(is_slow_path_safepoint),
535 defined_by_(defined_by) {}
536
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100537 ArenaAllocator* const allocator_;
538
539 // Ranges of this interval. We need a quick access to the last range to test
540 // for liveness (see `IsDeadAt`).
541 LiveRange* first_range_;
542 LiveRange* last_range_;
543
544 // Uses of this interval. Note that this linked list is shared amongst siblings.
545 UsePosition* first_use_;
546
547 // The instruction type this interval corresponds to.
548 const Primitive::Type type_;
549
550 // Live interval that is the result of a split.
551 LiveInterval* next_sibling_;
552
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100553 // The first interval from which split intervals come from.
554 LiveInterval* parent_;
555
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100556 // The register allocated to this interval.
557 int register_;
558
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100559 // The spill slot allocated to this interval.
560 int spill_slot_;
561
562 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100563 const bool is_fixed_;
564
565 // Whether the interval is for a temporary.
566 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100567
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100568 // Whether the interval is for a safepoint that calls on slow path.
569 const bool is_slow_path_safepoint_;
570
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100571 // The instruction represented by this interval.
572 HInstruction* const defined_by_;
573
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100574 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100575 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100576
577 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
578};
579
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100580class SsaLivenessAnalysis : public ValueObject {
581 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100582 SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100583 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100584 codegen_(codegen),
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100585 linear_post_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100586 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100587 instructions_from_ssa_index_(graph.GetArena(), 0),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100588 instructions_from_lifetime_position_(graph.GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100589 number_of_ssa_values_(0) {
590 block_infos_.SetSize(graph.GetBlocks().Size());
591 }
592
593 void Analyze();
594
595 BitVector* GetLiveInSet(const HBasicBlock& block) const {
596 return &block_infos_.Get(block.GetBlockId())->live_in_;
597 }
598
599 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
600 return &block_infos_.Get(block.GetBlockId())->live_out_;
601 }
602
603 BitVector* GetKillSet(const HBasicBlock& block) const {
604 return &block_infos_.Get(block.GetBlockId())->kill_;
605 }
606
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100607 const GrowableArray<HBasicBlock*>& GetLinearPostOrder() const {
608 return linear_post_order_;
609 }
610
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100611 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100612 return instructions_from_ssa_index_.Get(index);
613 }
614
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100615 HInstruction* GetInstructionFromPosition(size_t index) const {
616 return instructions_from_lifetime_position_.Get(index);
617 }
618
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100619 HInstruction* GetTempUser(LiveInterval* temp) const {
620 // A temporary shares the same lifetime start as the instruction that requires it.
621 DCHECK(temp->IsTemp());
622 return GetInstructionFromPosition(temp->GetStart() / 2);
623 }
624
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100625 size_t GetMaxLifetimePosition() const {
626 return instructions_from_lifetime_position_.Size() * 2 - 1;
627 }
628
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100629 size_t GetNumberOfSsaValues() const {
630 return number_of_ssa_values_;
631 }
632
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100633 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100634 // Linearize the graph so that:
635 // (1): a block is always after its dominator,
636 // (2): blocks of loops are contiguous.
637 // This creates a natural and efficient ordering when visualizing live ranges.
638 void LinearizeGraph();
639
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100640 // Give an SSA number to each instruction that defines a value used by another instruction,
641 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100642 void NumberInstructions();
643
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100644 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
645 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100646
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100647 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
648 // kill sets, that do not take into account backward branches.
649 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100650
651 // After computing the initial sets, this method does a fixed point
652 // calculation over the live_in and live_out set to take into account
653 // backwards branches.
654 void ComputeLiveInAndLiveOutSets();
655
656 // Update the live_in set of the block and returns whether it has changed.
657 bool UpdateLiveIn(const HBasicBlock& block);
658
659 // Update the live_out set of the block and returns whether it has changed.
660 bool UpdateLiveOut(const HBasicBlock& block);
661
662 const HGraph& graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100663 CodeGenerator* const codegen_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100664 GrowableArray<HBasicBlock*> linear_post_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100665 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100666
667 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100668 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100669
670 // Temporary array used when inserting moves in the graph.
671 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100672 size_t number_of_ssa_values_;
673
674 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
675};
676
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100677class HLinearOrderIterator : public ValueObject {
678 public:
679 explicit HLinearOrderIterator(const SsaLivenessAnalysis& liveness)
680 : post_order_(liveness.GetLinearPostOrder()), index_(liveness.GetLinearPostOrder().Size()) {}
681
682 bool Done() const { return index_ == 0; }
683 HBasicBlock* Current() const { return post_order_.Get(index_ -1); }
684 void Advance() { --index_; DCHECK_GE(index_, 0U); }
685
686 private:
687 const GrowableArray<HBasicBlock*>& post_order_;
688 size_t index_;
689
690 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
691};
692
693class HLinearPostOrderIterator : public ValueObject {
694 public:
695 explicit HLinearPostOrderIterator(const SsaLivenessAnalysis& liveness)
696 : post_order_(liveness.GetLinearPostOrder()), index_(0) {}
697
698 bool Done() const { return index_ == post_order_.Size(); }
699 HBasicBlock* Current() const { return post_order_.Get(index_); }
700 void Advance() { ++index_; }
701
702 private:
703 const GrowableArray<HBasicBlock*>& post_order_;
704 size_t index_;
705
706 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
707};
708
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100709} // namespace art
710
711#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_