blob: 9ce34aa80b74665748a60778342e13563c59d8d4 [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#include "ssa_liveness_analysis.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010018
Ian Rogerse77493c2014-08-20 15:08:45 -070019#include "base/bit_vector-inl.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010020#include "code_generator.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010021#include "nodes.h"
22
23namespace art {
24
25void SsaLivenessAnalysis::Analyze() {
Aart Bik20e9db62016-09-14 10:52:13 -070026 graph_->Linearize();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010027 NumberInstructions();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010028 ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010029}
30
31void SsaLivenessAnalysis::NumberInstructions() {
32 int ssa_index = 0;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010033 size_t lifetime_position = 0;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010034 // Each instruction gets a lifetime position, and a block gets a lifetime
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010035 // start and end position. Non-phi instructions have a distinct lifetime position than
36 // the block they are in. Phi instructions have the lifetime start of their block as
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010037 // lifetime position.
38 //
39 // Because the register allocator will insert moves in the graph, we need
40 // to differentiate between the start and end of an instruction. Adding 2 to
41 // the lifetime position for each instruction ensures the start of an
42 // instruction is different than the end of the previous instruction.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010043 for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010044 HBasicBlock* block = it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010045 block->SetLifetimeStart(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010046
Andreas Gampe277ccbd2014-11-03 21:36:10 -080047 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
48 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +000049 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010050 LocationSummary* locations = current->GetLocations();
51 if (locations != nullptr && locations->Out().IsValid()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010052 instructions_from_ssa_index_.push_back(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010053 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010054 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010055 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +010056 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010057 current->SetLifetimePosition(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010058 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +010059 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010060
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010061 // Add a null marker to notify we are starting a block.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010062 instructions_from_lifetime_position_.push_back(nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010063
Andreas Gampe277ccbd2014-11-03 21:36:10 -080064 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
65 inst_it.Advance()) {
66 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +000067 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010068 LocationSummary* locations = current->GetLocations();
69 if (locations != nullptr && locations->Out().IsValid()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010070 instructions_from_ssa_index_.push_back(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010071 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010072 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010073 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +010074 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010075 instructions_from_lifetime_position_.push_back(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010076 current->SetLifetimePosition(lifetime_position);
77 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010078 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010079
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010080 block->SetLifetimeEnd(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010081 }
82 number_of_ssa_values_ = ssa_index;
83}
84
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010085void SsaLivenessAnalysis::ComputeLiveness() {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010086 for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010087 HBasicBlock* block = it.Current();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010088 block_infos_[block->GetBlockId()] =
89 new (graph_->GetArena()) BlockInfo(graph_->GetArena(), *block, number_of_ssa_values_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010090 }
91
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010092 // Compute the live ranges, as well as the initial live_in, live_out, and kill sets.
93 // This method does not handle backward branches for the sets, therefore live_in
94 // and live_out sets are not yet correct.
95 ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010096
97 // Do a fixed point calculation to take into account backward branches,
98 // that will update live_in of loop headers, and therefore live_out and live_in
99 // of blocks in the loop.
100 ComputeLiveInAndLiveOutSets();
101}
102
David Brazdil674f5192016-02-02 16:50:46 +0000103static void RecursivelyProcessInputs(HInstruction* current,
104 HInstruction* actual_user,
105 BitVector* live_in) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100106 HInputsRef inputs = current->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100107 for (size_t i = 0; i < inputs.size(); ++i) {
108 HInstruction* input = inputs[i];
David Brazdil674f5192016-02-02 16:50:46 +0000109 bool has_in_location = current->GetLocations()->InAt(i).IsValid();
110 bool has_out_location = input->GetLocations()->Out().IsValid();
111
112 if (has_in_location) {
113 DCHECK(has_out_location)
114 << "Instruction " << current->DebugName() << current->GetId()
115 << " expects an input value at index " << i << " but "
116 << input->DebugName() << input->GetId() << " does not produce one.";
117 DCHECK(input->HasSsaIndex());
118 // `input` generates a result used by `current`. Add use and update
119 // the live-in set.
120 input->GetLiveInterval()->AddUse(current, /* environment */ nullptr, i, actual_user);
121 live_in->SetBit(input->GetSsaIndex());
122 } else if (has_out_location) {
123 // `input` generates a result but it is not used by `current`.
124 } else {
125 // `input` is inlined into `current`. Walk over its inputs and record
126 // uses at `current`.
127 DCHECK(input->IsEmittedAtUseSite());
128 // Check that the inlined input is not a phi. Recursing on loop phis could
129 // lead to an infinite loop.
130 DCHECK(!input->IsPhi());
131 RecursivelyProcessInputs(input, actual_user, live_in);
132 }
133 }
134}
135
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100136void SsaLivenessAnalysis::ComputeLiveRanges() {
137 // Do a post order visit, adding inputs of instructions live in the block where
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100138 // that instruction is defined, and killing instructions that are being visited.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100139 for (HLinearPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 HBasicBlock* block = it.Current();
141
142 BitVector* kill = GetKillSet(*block);
143 BitVector* live_in = GetLiveInSet(*block);
144
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100145 // Set phi inputs of successors of this block corresponding to this block
146 // as live_in.
Vladimir Marko60584552015-09-03 13:35:12 +0000147 for (HBasicBlock* successor : block->GetSuccessors()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100148 live_in->Union(GetLiveInSet(*successor));
David Brazdil77a48ae2015-09-15 12:34:04 +0000149 if (successor->IsCatchBlock()) {
150 // Inputs of catch phis will be kept alive through their environment
151 // uses, allowing the runtime to copy their values to the corresponding
152 // catch phi spill slots when an exception is thrown.
153 // The only instructions which may not be recorded in the environments
154 // are constants created by the SSA builder as typed equivalents of
155 // untyped constants from the bytecode, or phis with only such constants
David Brazdilbadd8262016-02-02 16:28:56 +0000156 // as inputs (verified by GraphChecker). Their raw binary value must
David Brazdil77a48ae2015-09-15 12:34:04 +0000157 // therefore be the same and we only need to keep alive one.
158 } else {
159 size_t phi_input_index = successor->GetPredecessorIndexOf(block);
160 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
161 HInstruction* phi = phi_it.Current();
162 HInstruction* input = phi->InputAt(phi_input_index);
163 input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block);
164 // A phi input whose last user is the phi dies at the end of the predecessor block,
165 // and not at the phi's lifetime position.
166 live_in->SetBit(input->GetSsaIndex());
167 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100168 }
169 }
170
171 // Add a range that covers this block to all instructions live_in because of successors.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100172 // Instructions defined in this block will have their start of the range adjusted.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100173 for (uint32_t idx : live_in->Indexes()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100174 HInstruction* current = GetInstructionFromSsaIndex(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100175 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100176 }
177
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800178 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
179 back_it.Advance()) {
180 HInstruction* current = back_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100181 if (current->HasSsaIndex()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100182 // Kill the instruction and shorten its interval.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100183 kill->SetBit(current->GetSsaIndex());
184 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100185 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100186 }
187
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000188 // Process the environment first, because we know their uses come after
189 // or at the same liveness position of inputs.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100190 for (HEnvironment* environment = current->GetEnvironment();
191 environment != nullptr;
192 environment = environment->GetParent()) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000193 // Handle environment uses. See statements (b) and (c) of the
194 // SsaLivenessAnalysis.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000195 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
196 HInstruction* instruction = environment->GetInstructionAt(i);
Mingyao Yang718493c2015-07-22 15:56:34 -0700197 bool should_be_live = ShouldBeLiveForEnvironment(current, instruction);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000198 if (should_be_live) {
199 DCHECK(instruction->HasSsaIndex());
200 live_in->SetBit(instruction->GetSsaIndex());
201 }
202 if (instruction != nullptr) {
203 instruction->GetLiveInterval()->AddUse(
David Brazdilb3e773e2016-01-26 11:28:37 +0000204 current, environment, i, /* actual_user */ nullptr, should_be_live);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000205 }
206 }
207 }
208
David Brazdilb3e773e2016-01-26 11:28:37 +0000209 // Process inputs of instructions.
210 if (current->IsEmittedAtUseSite()) {
211 if (kIsDebugBuild) {
212 DCHECK(!current->GetLocations()->Out().IsValid());
Vladimir Marko46817b82016-03-29 12:21:58 +0100213 for (const HUseListNode<HInstruction*>& use : current->GetUses()) {
214 HInstruction* user = use.GetUser();
215 size_t index = use.GetIndex();
David Brazdilb3e773e2016-01-26 11:28:37 +0000216 DCHECK(!user->GetLocations()->InAt(index).IsValid());
217 }
218 DCHECK(!current->HasEnvironmentUses());
219 }
220 } else {
David Brazdil674f5192016-02-02 16:50:46 +0000221 RecursivelyProcessInputs(current, current, live_in);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100222 }
223 }
224
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100225 // Kill phis defined in this block.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800226 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
227 HInstruction* current = inst_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100228 if (current->HasSsaIndex()) {
229 kill->SetBit(current->GetSsaIndex());
230 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100231 LiveInterval* interval = current->GetLiveInterval();
232 DCHECK((interval->GetFirstRange() == nullptr)
233 || (interval->GetStart() == current->GetLifetimePosition()));
234 interval->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100235 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100236 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100237
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100238 if (block->IsLoopHeader()) {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100239 if (kIsDebugBuild) {
240 CheckNoLiveInIrreducibleLoop(*block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000241 }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100242 size_t last_position = block->GetLoopInformation()->GetLifetimeEnd();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100243 // For all live_in instructions at the loop header, we need to create a range
244 // that covers the full loop.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100245 for (uint32_t idx : live_in->Indexes()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100246 HInstruction* current = GetInstructionFromSsaIndex(idx);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100247 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(), last_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100248 }
249 }
250 }
251}
252
253void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
254 bool changed;
255 do {
256 changed = false;
257
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100258 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100259 const HBasicBlock& block = *it.Current();
260
261 // The live_in set depends on the kill set (which does not
262 // change in this loop), and the live_out set. If the live_out
263 // set does not change, there is no need to update the live_in set.
264 if (UpdateLiveOut(block) && UpdateLiveIn(block)) {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100265 if (kIsDebugBuild) {
266 CheckNoLiveInIrreducibleLoop(block);
267 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100268 changed = true;
269 }
270 }
271 } while (changed);
272}
273
274bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
275 BitVector* live_out = GetLiveOutSet(block);
276 bool changed = false;
277 // The live_out set of a block is the union of live_in sets of its successors.
Vladimir Marko60584552015-09-03 13:35:12 +0000278 for (HBasicBlock* successor : block.GetSuccessors()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100279 if (live_out->Union(GetLiveInSet(*successor))) {
280 changed = true;
281 }
282 }
283 return changed;
284}
285
286
287bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
288 BitVector* live_out = GetLiveOutSet(block);
289 BitVector* kill = GetKillSet(block);
290 BitVector* live_in = GetLiveInSet(block);
291 // If live_out is updated (because of backward branches), we need to make
292 // sure instructions in live_out are also in live_in, unless they are killed
293 // by this block.
294 return live_in->UnionIfNotIn(live_out, kill);
295}
296
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700297void LiveInterval::DumpWithContext(std::ostream& stream,
298 const CodeGenerator& codegen) const {
299 Dump(stream);
300 if (IsFixed()) {
301 stream << ", register:" << GetRegister() << "(";
302 if (IsFloatingPoint()) {
303 codegen.DumpFloatingPointRegister(stream, GetRegister());
304 } else {
305 codegen.DumpCoreRegister(stream, GetRegister());
306 }
307 stream << ")";
308 } else {
309 stream << ", spill slot:" << GetSpillSlot();
310 }
311 stream << ", requires_register:" << (GetDefinedBy() != nullptr && RequiresRegister());
312 if (GetParent()->GetDefinedBy() != nullptr) {
313 stream << ", defined_by:" << GetParent()->GetDefinedBy()->GetKind();
314 stream << "(" << GetParent()->GetDefinedBy()->GetLifetimePosition() << ")";
315 }
316}
317
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000318static int RegisterOrLowRegister(Location location) {
319 return location.IsPair() ? location.low() : location.reg();
320}
321
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100322int LiveInterval::FindFirstRegisterHint(size_t* free_until,
323 const SsaLivenessAnalysis& liveness) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000324 DCHECK(!IsHighInterval());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000325 if (IsTemp()) return kNoRegister;
326
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100327 if (GetParent() == this && defined_by_ != nullptr) {
328 // This is the first interval for the instruction. Try to find
329 // a register based on its definition.
330 DCHECK_EQ(defined_by_->GetLiveInterval(), this);
331 int hint = FindHintAtDefinition();
332 if (hint != kNoRegister && free_until[hint] > GetStart()) {
333 return hint;
334 }
335 }
336
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100337 if (IsSplit() && liveness.IsAtBlockBoundary(GetStart() / 2)) {
338 // If the start of this interval is at a block boundary, we look at the
339 // location of the interval in blocks preceding the block this interval
340 // starts at. If one location is a register we return it as a hint. This
341 // will avoid a move between the two blocks.
342 HBasicBlock* block = liveness.GetBlockFromPosition(GetStart() / 2);
Nicolas Geoffray82726882015-06-01 13:51:57 +0100343 size_t next_register_use = FirstRegisterUse();
Vladimir Marko60584552015-09-03 13:35:12 +0000344 for (HBasicBlock* predecessor : block->GetPredecessors()) {
345 size_t position = predecessor->GetLifetimeEnd() - 1;
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100346 // We know positions above GetStart() do not have a location yet.
347 if (position < GetStart()) {
348 LiveInterval* existing = GetParent()->GetSiblingAt(position);
349 if (existing != nullptr
350 && existing->HasRegister()
Nicolas Geoffray82726882015-06-01 13:51:57 +0100351 // It's worth using that register if it is available until
352 // the next use.
353 && (free_until[existing->GetRegister()] >= next_register_use)) {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100354 return existing->GetRegister();
355 }
356 }
357 }
358 }
359
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100360 UsePosition* use = first_use_;
361 size_t start = GetStart();
362 size_t end = GetEnd();
363 while (use != nullptr && use->GetPosition() <= end) {
364 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100365 if (use_position >= start && !use->IsSynthesized()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100366 HInstruction* user = use->GetUser();
367 size_t input_index = use->GetInputIndex();
368 if (user->IsPhi()) {
369 // If the phi has a register, try to use the same.
370 Location phi_location = user->GetLiveInterval()->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000371 if (phi_location.IsRegisterKind()) {
372 DCHECK(SameRegisterKind(phi_location));
373 int reg = RegisterOrLowRegister(phi_location);
374 if (free_until[reg] >= use_position) {
375 return reg;
376 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100377 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100378 // If the instruction dies at the phi assignment, we can try having the
379 // same register.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100380 if (end == user->GetBlock()->GetPredecessors()[input_index]->GetLifetimeEnd()) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100381 HInputsRef inputs = user->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100382 for (size_t i = 0; i < inputs.size(); ++i) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100383 if (i == input_index) {
384 continue;
385 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100386 Location location = inputs[i]->GetLiveInterval()->GetLocationAt(
Vladimir Markoec7802a2015-10-01 20:57:57 +0100387 user->GetBlock()->GetPredecessors()[i]->GetLifetimeEnd() - 1);
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000388 if (location.IsRegisterKind()) {
389 int reg = RegisterOrLowRegister(location);
390 if (free_until[reg] >= use_position) {
391 return reg;
392 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100393 }
394 }
395 }
396 } else {
397 // If the instruction is expected in a register, try to use it.
398 LocationSummary* locations = user->GetLocations();
399 Location expected = locations->InAt(use->GetInputIndex());
400 // We use the user's lifetime position - 1 (and not `use_position`) because the
401 // register is blocked at the beginning of the user.
402 size_t position = user->GetLifetimePosition() - 1;
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000403 if (expected.IsRegisterKind()) {
404 DCHECK(SameRegisterKind(expected));
405 int reg = RegisterOrLowRegister(expected);
406 if (free_until[reg] >= position) {
407 return reg;
408 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100409 }
410 }
411 }
412 use = use->GetNext();
413 }
414
415 return kNoRegister;
416}
417
418int LiveInterval::FindHintAtDefinition() const {
419 if (defined_by_->IsPhi()) {
420 // Try to use the same register as one of the inputs.
Vladimir Marko60584552015-09-03 13:35:12 +0000421 const ArenaVector<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors();
Vladimir Markoe9004912016-06-16 16:50:52 +0100422 HInputsRef inputs = defined_by_->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100423 for (size_t i = 0; i < inputs.size(); ++i) {
Vladimir Marko60584552015-09-03 13:35:12 +0000424 size_t end = predecessors[i]->GetLifetimeEnd();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100425 LiveInterval* input_interval = inputs[i]->GetLiveInterval()->GetSiblingAt(end - 1);
David Brazdil241a4862015-04-16 17:59:03 +0100426 if (input_interval->GetEnd() == end) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100427 // If the input dies at the end of the predecessor, we know its register can
428 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100429 Location input_location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000430 if (input_location.IsRegisterKind()) {
431 DCHECK(SameRegisterKind(input_location));
432 return RegisterOrLowRegister(input_location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100433 }
434 }
435 }
436 } else {
437 LocationSummary* locations = GetDefinedBy()->GetLocations();
438 Location out = locations->Out();
439 if (out.IsUnallocated() && out.GetPolicy() == Location::kSameAsFirstInput) {
440 // Try to use the same register as the first input.
David Brazdil241a4862015-04-16 17:59:03 +0100441 LiveInterval* input_interval =
442 GetDefinedBy()->InputAt(0)->GetLiveInterval()->GetSiblingAt(GetStart() - 1);
443 if (input_interval->GetEnd() == GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100444 // If the input dies at the start of this instruction, we know its register can
445 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100446 Location location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000447 if (location.IsRegisterKind()) {
448 DCHECK(SameRegisterKind(location));
449 return RegisterOrLowRegister(location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100450 }
451 }
452 }
453 }
454 return kNoRegister;
455}
456
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100457bool LiveInterval::SameRegisterKind(Location other) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000458 if (IsFloatingPoint()) {
459 if (IsLowInterval() || IsHighInterval()) {
460 return other.IsFpuRegisterPair();
461 } else {
462 return other.IsFpuRegister();
463 }
464 } else {
465 if (IsLowInterval() || IsHighInterval()) {
466 return other.IsRegisterPair();
467 } else {
468 return other.IsRegister();
469 }
470 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100471}
472
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100473bool LiveInterval::NeedsTwoSpillSlots() const {
474 return type_ == Primitive::kPrimLong || type_ == Primitive::kPrimDouble;
475}
476
477Location LiveInterval::ToLocation() const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000478 DCHECK(!IsHighInterval());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100479 if (HasRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000480 if (IsFloatingPoint()) {
481 if (HasHighInterval()) {
482 return Location::FpuRegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
483 } else {
484 return Location::FpuRegisterLocation(GetRegister());
485 }
486 } else {
487 if (HasHighInterval()) {
488 return Location::RegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
489 } else {
490 return Location::RegisterLocation(GetRegister());
491 }
492 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100493 } else {
494 HInstruction* defined_by = GetParent()->GetDefinedBy();
495 if (defined_by->IsConstant()) {
496 return defined_by->GetLocations()->Out();
497 } else if (GetParent()->HasSpillSlot()) {
498 if (NeedsTwoSpillSlots()) {
499 return Location::DoubleStackSlot(GetParent()->GetSpillSlot());
500 } else {
501 return Location::StackSlot(GetParent()->GetSpillSlot());
502 }
503 } else {
504 return Location();
505 }
506 }
507}
508
David Brazdil5b8e6a52015-02-25 16:17:05 +0000509Location LiveInterval::GetLocationAt(size_t position) {
David Brazdil241a4862015-04-16 17:59:03 +0100510 LiveInterval* sibling = GetSiblingAt(position);
511 DCHECK(sibling != nullptr);
512 return sibling->ToLocation();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100513}
514
David Brazdil241a4862015-04-16 17:59:03 +0100515LiveInterval* LiveInterval::GetSiblingAt(size_t position) {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000516 LiveInterval* current = this;
David Brazdil241a4862015-04-16 17:59:03 +0100517 while (current != nullptr && !current->IsDefinedAt(position)) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100518 current = current->GetNextSibling();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100519 }
David Brazdil241a4862015-04-16 17:59:03 +0100520 return current;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100521}
522
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100523} // namespace art