blob: 18942a1823f17f9b2a71bed44f2ee28dddb113d3 [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"
Aart Bik96202302016-10-04 17:33:56 -070021#include "linear_order.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010022#include "nodes.h"
23
Vladimir Marko0a516052019-10-14 13:00:44 +000024namespace art {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010025
26void SsaLivenessAnalysis::Analyze() {
Aart Bik96202302016-10-04 17:33:56 -070027 // Compute the linear order directly in the graph's data structure
28 // (there are no more following graph mutations).
Vladimir Markoca6fff82017-10-03 14:49:14 +010029 LinearizeGraph(graph_, &graph_->linear_order_);
Aart Bik96202302016-10-04 17:33:56 -070030
31 // Liveness analysis.
Nicolas Geoffray804d0932014-05-02 08:46:00 +010032 NumberInstructions();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010033 ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010034}
35
36void SsaLivenessAnalysis::NumberInstructions() {
37 int ssa_index = 0;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010038 size_t lifetime_position = 0;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010039 // Each instruction gets a lifetime position, and a block gets a lifetime
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010040 // start and end position. Non-phi instructions have a distinct lifetime position than
41 // the block they are in. Phi instructions have the lifetime start of their block as
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010042 // lifetime position.
43 //
44 // Because the register allocator will insert moves in the graph, we need
45 // to differentiate between the start and end of an instruction. Adding 2 to
46 // the lifetime position for each instruction ensures the start of an
47 // instruction is different than the end of the previous instruction.
Aart Bik96202302016-10-04 17:33:56 -070048 for (HBasicBlock* block : graph_->GetLinearOrder()) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010049 block->SetLifetimeStart(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010050
Andreas Gampe277ccbd2014-11-03 21:36:10 -080051 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
52 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +000053 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010054 LocationSummary* locations = current->GetLocations();
55 if (locations != nullptr && locations->Out().IsValid()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010056 instructions_from_ssa_index_.push_back(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010057 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010058 current->SetLiveInterval(
Vladimir Markoe764d2e2017-10-05 14:35:55 +010059 LiveInterval::MakeInterval(allocator_, current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +010060 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061 current->SetLifetimePosition(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010062 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +010063 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010064
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010065 // Add a null marker to notify we are starting a block.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010066 instructions_from_lifetime_position_.push_back(nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010067
Andreas Gampe277ccbd2014-11-03 21:36:10 -080068 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
69 inst_it.Advance()) {
70 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +000071 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010072 LocationSummary* locations = current->GetLocations();
73 if (locations != nullptr && locations->Out().IsValid()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010074 instructions_from_ssa_index_.push_back(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010075 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010076 current->SetLiveInterval(
Vladimir Markoe764d2e2017-10-05 14:35:55 +010077 LiveInterval::MakeInterval(allocator_, current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +010078 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010079 instructions_from_lifetime_position_.push_back(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010080 current->SetLifetimePosition(lifetime_position);
81 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010082 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010083
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010084 block->SetLifetimeEnd(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010085 }
86 number_of_ssa_values_ = ssa_index;
87}
88
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010089void SsaLivenessAnalysis::ComputeLiveness() {
Aart Bik96202302016-10-04 17:33:56 -070090 for (HBasicBlock* block : graph_->GetLinearOrder()) {
Vladimir Markoe764d2e2017-10-05 14:35:55 +010091 block_infos_[block->GetBlockId()] =
92 new (allocator_) BlockInfo(allocator_, *block, number_of_ssa_values_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010093 }
94
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010095 // Compute the live ranges, as well as the initial live_in, live_out, and kill sets.
96 // This method does not handle backward branches for the sets, therefore live_in
97 // and live_out sets are not yet correct.
98 ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010099
100 // Do a fixed point calculation to take into account backward branches,
101 // that will update live_in of loop headers, and therefore live_out and live_in
102 // of blocks in the loop.
103 ComputeLiveInAndLiveOutSets();
104}
105
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +0100106void SsaLivenessAnalysis::RecursivelyProcessInputs(HInstruction* current,
107 HInstruction* actual_user,
108 BitVector* live_in) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100109 HInputsRef inputs = current->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100110 for (size_t i = 0; i < inputs.size(); ++i) {
111 HInstruction* input = inputs[i];
David Brazdil674f5192016-02-02 16:50:46 +0000112 bool has_in_location = current->GetLocations()->InAt(i).IsValid();
113 bool has_out_location = input->GetLocations()->Out().IsValid();
114
115 if (has_in_location) {
116 DCHECK(has_out_location)
117 << "Instruction " << current->DebugName() << current->GetId()
118 << " expects an input value at index " << i << " but "
119 << input->DebugName() << input->GetId() << " does not produce one.";
120 DCHECK(input->HasSsaIndex());
121 // `input` generates a result used by `current`. Add use and update
122 // the live-in set.
Andreas Gampe3db70682018-12-26 15:12:03 -0800123 input->GetLiveInterval()->AddUse(current, /* environment= */ nullptr, i, actual_user);
David Brazdil674f5192016-02-02 16:50:46 +0000124 live_in->SetBit(input->GetSsaIndex());
125 } else if (has_out_location) {
126 // `input` generates a result but it is not used by `current`.
127 } else {
128 // `input` is inlined into `current`. Walk over its inputs and record
129 // uses at `current`.
130 DCHECK(input->IsEmittedAtUseSite());
131 // Check that the inlined input is not a phi. Recursing on loop phis could
132 // lead to an infinite loop.
133 DCHECK(!input->IsPhi());
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +0100134 DCHECK(!input->HasEnvironment());
David Brazdil674f5192016-02-02 16:50:46 +0000135 RecursivelyProcessInputs(input, actual_user, live_in);
136 }
137 }
138}
139
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +0100140void SsaLivenessAnalysis::ProcessEnvironment(HInstruction* current,
141 HInstruction* actual_user,
142 BitVector* live_in) {
143 for (HEnvironment* environment = current->GetEnvironment();
144 environment != nullptr;
145 environment = environment->GetParent()) {
146 // Handle environment uses. See statements (b) and (c) of the
147 // SsaLivenessAnalysis.
148 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
149 HInstruction* instruction = environment->GetInstructionAt(i);
150 if (instruction == nullptr) {
151 continue;
152 }
153 bool should_be_live = ShouldBeLiveForEnvironment(current, instruction);
154 // If this environment use does not keep the instruction live, it does not
155 // affect the live range of that instruction.
156 if (should_be_live) {
157 CHECK(instruction->HasSsaIndex()) << instruction->DebugName();
158 live_in->SetBit(instruction->GetSsaIndex());
159 instruction->GetLiveInterval()->AddUse(current,
160 environment,
161 i,
162 actual_user);
163 }
164 }
165 }
166}
167
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100168void SsaLivenessAnalysis::ComputeLiveRanges() {
169 // Do a post order visit, adding inputs of instructions live in the block where
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100170 // that instruction is defined, and killing instructions that are being visited.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100171 for (HBasicBlock* block : ReverseRange(graph_->GetLinearOrder())) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100172 BitVector* kill = GetKillSet(*block);
173 BitVector* live_in = GetLiveInSet(*block);
174
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100175 // Set phi inputs of successors of this block corresponding to this block
176 // as live_in.
Vladimir Marko60584552015-09-03 13:35:12 +0000177 for (HBasicBlock* successor : block->GetSuccessors()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100178 live_in->Union(GetLiveInSet(*successor));
David Brazdil77a48ae2015-09-15 12:34:04 +0000179 if (successor->IsCatchBlock()) {
180 // Inputs of catch phis will be kept alive through their environment
181 // uses, allowing the runtime to copy their values to the corresponding
182 // catch phi spill slots when an exception is thrown.
183 // The only instructions which may not be recorded in the environments
184 // are constants created by the SSA builder as typed equivalents of
185 // untyped constants from the bytecode, or phis with only such constants
David Brazdilbadd8262016-02-02 16:28:56 +0000186 // as inputs (verified by GraphChecker). Their raw binary value must
David Brazdil77a48ae2015-09-15 12:34:04 +0000187 // therefore be the same and we only need to keep alive one.
188 } else {
189 size_t phi_input_index = successor->GetPredecessorIndexOf(block);
190 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
191 HInstruction* phi = phi_it.Current();
192 HInstruction* input = phi->InputAt(phi_input_index);
193 input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block);
194 // A phi input whose last user is the phi dies at the end of the predecessor block,
195 // and not at the phi's lifetime position.
196 live_in->SetBit(input->GetSsaIndex());
197 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100198 }
199 }
200
201 // Add a range that covers this block to all instructions live_in because of successors.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100202 // Instructions defined in this block will have their start of the range adjusted.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100203 for (uint32_t idx : live_in->Indexes()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100204 HInstruction* current = GetInstructionFromSsaIndex(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100205 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100206 }
207
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800208 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
209 back_it.Advance()) {
210 HInstruction* current = back_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100211 if (current->HasSsaIndex()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100212 // Kill the instruction and shorten its interval.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100213 kill->SetBit(current->GetSsaIndex());
214 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100215 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100216 }
217
David Brazdilb3e773e2016-01-26 11:28:37 +0000218 // Process inputs of instructions.
219 if (current->IsEmittedAtUseSite()) {
220 if (kIsDebugBuild) {
221 DCHECK(!current->GetLocations()->Out().IsValid());
Vladimir Marko46817b82016-03-29 12:21:58 +0100222 for (const HUseListNode<HInstruction*>& use : current->GetUses()) {
223 HInstruction* user = use.GetUser();
224 size_t index = use.GetIndex();
David Brazdilb3e773e2016-01-26 11:28:37 +0000225 DCHECK(!user->GetLocations()->InAt(index).IsValid());
226 }
227 DCHECK(!current->HasEnvironmentUses());
228 }
229 } else {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +0100230 // Process the environment first, because we know their uses come after
231 // or at the same liveness position of inputs.
232 ProcessEnvironment(current, current, live_in);
233
234 // Special case implicit null checks. We want their environment uses to be
235 // emitted at the instruction doing the actual null check.
236 HNullCheck* check = current->GetImplicitNullCheck();
237 if (check != nullptr) {
238 ProcessEnvironment(check, current, live_in);
239 }
David Brazdil674f5192016-02-02 16:50:46 +0000240 RecursivelyProcessInputs(current, current, live_in);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100241 }
242 }
243
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100244 // Kill phis defined in this block.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800245 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
246 HInstruction* current = inst_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100247 if (current->HasSsaIndex()) {
248 kill->SetBit(current->GetSsaIndex());
249 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100250 LiveInterval* interval = current->GetLiveInterval();
251 DCHECK((interval->GetFirstRange() == nullptr)
252 || (interval->GetStart() == current->GetLifetimePosition()));
253 interval->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100254 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100255 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100256
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100257 if (block->IsLoopHeader()) {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100258 if (kIsDebugBuild) {
259 CheckNoLiveInIrreducibleLoop(*block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000260 }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100261 size_t last_position = block->GetLoopInformation()->GetLifetimeEnd();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100262 // For all live_in instructions at the loop header, we need to create a range
263 // that covers the full loop.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100264 for (uint32_t idx : live_in->Indexes()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100265 HInstruction* current = GetInstructionFromSsaIndex(idx);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100266 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(), last_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100267 }
268 }
269 }
270}
271
272void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
273 bool changed;
274 do {
275 changed = false;
276
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100277 for (const HBasicBlock* block : graph_->GetPostOrder()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100278 // The live_in set depends on the kill set (which does not
279 // change in this loop), and the live_out set. If the live_out
280 // set does not change, there is no need to update the live_in set.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100281 if (UpdateLiveOut(*block) && UpdateLiveIn(*block)) {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100282 if (kIsDebugBuild) {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100283 CheckNoLiveInIrreducibleLoop(*block);
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100284 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100285 changed = true;
286 }
287 }
288 } while (changed);
289}
290
291bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
292 BitVector* live_out = GetLiveOutSet(block);
293 bool changed = false;
294 // The live_out set of a block is the union of live_in sets of its successors.
Vladimir Marko60584552015-09-03 13:35:12 +0000295 for (HBasicBlock* successor : block.GetSuccessors()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100296 if (live_out->Union(GetLiveInSet(*successor))) {
297 changed = true;
298 }
299 }
300 return changed;
301}
302
303
304bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
305 BitVector* live_out = GetLiveOutSet(block);
306 BitVector* kill = GetKillSet(block);
307 BitVector* live_in = GetLiveInSet(block);
308 // If live_out is updated (because of backward branches), we need to make
309 // sure instructions in live_out are also in live_in, unless they are killed
310 // by this block.
311 return live_in->UnionIfNotIn(live_out, kill);
312}
313
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700314void LiveInterval::DumpWithContext(std::ostream& stream,
315 const CodeGenerator& codegen) const {
316 Dump(stream);
317 if (IsFixed()) {
318 stream << ", register:" << GetRegister() << "(";
319 if (IsFloatingPoint()) {
320 codegen.DumpFloatingPointRegister(stream, GetRegister());
321 } else {
322 codegen.DumpCoreRegister(stream, GetRegister());
323 }
324 stream << ")";
325 } else {
326 stream << ", spill slot:" << GetSpillSlot();
327 }
328 stream << ", requires_register:" << (GetDefinedBy() != nullptr && RequiresRegister());
329 if (GetParent()->GetDefinedBy() != nullptr) {
330 stream << ", defined_by:" << GetParent()->GetDefinedBy()->GetKind();
331 stream << "(" << GetParent()->GetDefinedBy()->GetLifetimePosition() << ")";
332 }
333}
334
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000335static int RegisterOrLowRegister(Location location) {
336 return location.IsPair() ? location.low() : location.reg();
337}
338
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100339int LiveInterval::FindFirstRegisterHint(size_t* free_until,
340 const SsaLivenessAnalysis& liveness) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000341 DCHECK(!IsHighInterval());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000342 if (IsTemp()) return kNoRegister;
343
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100344 if (GetParent() == this && defined_by_ != nullptr) {
345 // This is the first interval for the instruction. Try to find
346 // a register based on its definition.
347 DCHECK_EQ(defined_by_->GetLiveInterval(), this);
348 int hint = FindHintAtDefinition();
349 if (hint != kNoRegister && free_until[hint] > GetStart()) {
350 return hint;
351 }
352 }
353
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100354 if (IsSplit() && liveness.IsAtBlockBoundary(GetStart() / 2)) {
355 // If the start of this interval is at a block boundary, we look at the
356 // location of the interval in blocks preceding the block this interval
357 // starts at. If one location is a register we return it as a hint. This
358 // will avoid a move between the two blocks.
359 HBasicBlock* block = liveness.GetBlockFromPosition(GetStart() / 2);
Nicolas Geoffray82726882015-06-01 13:51:57 +0100360 size_t next_register_use = FirstRegisterUse();
Vladimir Marko60584552015-09-03 13:35:12 +0000361 for (HBasicBlock* predecessor : block->GetPredecessors()) {
362 size_t position = predecessor->GetLifetimeEnd() - 1;
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100363 // We know positions above GetStart() do not have a location yet.
364 if (position < GetStart()) {
365 LiveInterval* existing = GetParent()->GetSiblingAt(position);
366 if (existing != nullptr
367 && existing->HasRegister()
Nicolas Geoffray82726882015-06-01 13:51:57 +0100368 // It's worth using that register if it is available until
369 // the next use.
370 && (free_until[existing->GetRegister()] >= next_register_use)) {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100371 return existing->GetRegister();
372 }
373 }
374 }
375 }
376
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100377 size_t start = GetStart();
378 size_t end = GetEnd();
Vladimir Marko82b07402017-03-01 19:02:04 +0000379 for (const UsePosition& use : GetUses()) {
380 size_t use_position = use.GetPosition();
381 if (use_position > end) {
382 break;
383 }
384 if (use_position >= start && !use.IsSynthesized()) {
385 HInstruction* user = use.GetUser();
386 size_t input_index = use.GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100387 if (user->IsPhi()) {
388 // If the phi has a register, try to use the same.
389 Location phi_location = user->GetLiveInterval()->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000390 if (phi_location.IsRegisterKind()) {
391 DCHECK(SameRegisterKind(phi_location));
392 int reg = RegisterOrLowRegister(phi_location);
393 if (free_until[reg] >= use_position) {
394 return reg;
395 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100396 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100397 // If the instruction dies at the phi assignment, we can try having the
398 // same register.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100399 if (end == user->GetBlock()->GetPredecessors()[input_index]->GetLifetimeEnd()) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100400 HInputsRef inputs = user->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100401 for (size_t i = 0; i < inputs.size(); ++i) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100402 if (i == input_index) {
403 continue;
404 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100405 Location location = inputs[i]->GetLiveInterval()->GetLocationAt(
Vladimir Markoec7802a2015-10-01 20:57:57 +0100406 user->GetBlock()->GetPredecessors()[i]->GetLifetimeEnd() - 1);
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000407 if (location.IsRegisterKind()) {
408 int reg = RegisterOrLowRegister(location);
409 if (free_until[reg] >= use_position) {
410 return reg;
411 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100412 }
413 }
414 }
415 } else {
416 // If the instruction is expected in a register, try to use it.
417 LocationSummary* locations = user->GetLocations();
Vladimir Marko82b07402017-03-01 19:02:04 +0000418 Location expected = locations->InAt(use.GetInputIndex());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100419 // We use the user's lifetime position - 1 (and not `use_position`) because the
420 // register is blocked at the beginning of the user.
421 size_t position = user->GetLifetimePosition() - 1;
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000422 if (expected.IsRegisterKind()) {
423 DCHECK(SameRegisterKind(expected));
424 int reg = RegisterOrLowRegister(expected);
425 if (free_until[reg] >= position) {
426 return reg;
427 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100428 }
429 }
430 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100431 }
432
433 return kNoRegister;
434}
435
436int LiveInterval::FindHintAtDefinition() const {
437 if (defined_by_->IsPhi()) {
438 // Try to use the same register as one of the inputs.
Vladimir Marko60584552015-09-03 13:35:12 +0000439 const ArenaVector<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors();
Vladimir Markoe9004912016-06-16 16:50:52 +0100440 HInputsRef inputs = defined_by_->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100441 for (size_t i = 0; i < inputs.size(); ++i) {
Vladimir Marko60584552015-09-03 13:35:12 +0000442 size_t end = predecessors[i]->GetLifetimeEnd();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100443 LiveInterval* input_interval = inputs[i]->GetLiveInterval()->GetSiblingAt(end - 1);
David Brazdil241a4862015-04-16 17:59:03 +0100444 if (input_interval->GetEnd() == end) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100445 // If the input dies at the end of the predecessor, we know its register can
446 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100447 Location input_location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000448 if (input_location.IsRegisterKind()) {
449 DCHECK(SameRegisterKind(input_location));
450 return RegisterOrLowRegister(input_location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100451 }
452 }
453 }
454 } else {
455 LocationSummary* locations = GetDefinedBy()->GetLocations();
456 Location out = locations->Out();
457 if (out.IsUnallocated() && out.GetPolicy() == Location::kSameAsFirstInput) {
458 // Try to use the same register as the first input.
David Brazdil241a4862015-04-16 17:59:03 +0100459 LiveInterval* input_interval =
460 GetDefinedBy()->InputAt(0)->GetLiveInterval()->GetSiblingAt(GetStart() - 1);
461 if (input_interval->GetEnd() == GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100462 // If the input dies at the start of this instruction, we know its register can
463 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100464 Location location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000465 if (location.IsRegisterKind()) {
466 DCHECK(SameRegisterKind(location));
467 return RegisterOrLowRegister(location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100468 }
469 }
470 }
471 }
472 return kNoRegister;
473}
474
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100475bool LiveInterval::SameRegisterKind(Location other) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000476 if (IsFloatingPoint()) {
477 if (IsLowInterval() || IsHighInterval()) {
478 return other.IsFpuRegisterPair();
479 } else {
480 return other.IsFpuRegister();
481 }
482 } else {
483 if (IsLowInterval() || IsHighInterval()) {
484 return other.IsRegisterPair();
485 } else {
486 return other.IsRegister();
487 }
488 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100489}
490
Aart Bikcc895252017-03-21 10:55:15 -0700491size_t LiveInterval::NumberOfSpillSlotsNeeded() const {
Aart Bikf8f5a162017-02-06 15:35:29 -0800492 // For a SIMD operation, compute the number of needed spill slots.
493 // TODO: do through vector type?
494 HInstruction* definition = GetParent()->GetDefinedBy();
Aart Bik2dd7b672017-12-07 11:11:22 -0800495 if (definition != nullptr && HVecOperation::ReturnsSIMDValue(definition)) {
496 if (definition->IsPhi()) {
497 definition = definition->InputAt(1); // SIMD always appears on back-edge
498 }
Aart Bikf8f5a162017-02-06 15:35:29 -0800499 return definition->AsVecOperation()->GetVectorNumberOfBytes() / kVRegSize;
500 }
Aart Bik5576f372017-03-23 16:17:37 -0700501 // Return number of needed spill slots based on type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100502 return (type_ == DataType::Type::kInt64 || type_ == DataType::Type::kFloat64) ? 2 : 1;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100503}
504
505Location LiveInterval::ToLocation() const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000506 DCHECK(!IsHighInterval());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100507 if (HasRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000508 if (IsFloatingPoint()) {
509 if (HasHighInterval()) {
510 return Location::FpuRegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
511 } else {
512 return Location::FpuRegisterLocation(GetRegister());
513 }
514 } else {
515 if (HasHighInterval()) {
516 return Location::RegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
517 } else {
518 return Location::RegisterLocation(GetRegister());
519 }
520 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100521 } else {
522 HInstruction* defined_by = GetParent()->GetDefinedBy();
523 if (defined_by->IsConstant()) {
524 return defined_by->GetLocations()->Out();
525 } else if (GetParent()->HasSpillSlot()) {
Artem Serovc8150b52019-07-31 18:28:00 +0100526 return Location::StackSlotByNumOfSlots(NumberOfSpillSlotsNeeded(),
527 GetParent()->GetSpillSlot());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100528 } else {
529 return Location();
530 }
531 }
532}
533
David Brazdil5b8e6a52015-02-25 16:17:05 +0000534Location LiveInterval::GetLocationAt(size_t position) {
David Brazdil241a4862015-04-16 17:59:03 +0100535 LiveInterval* sibling = GetSiblingAt(position);
536 DCHECK(sibling != nullptr);
537 return sibling->ToLocation();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100538}
539
David Brazdil241a4862015-04-16 17:59:03 +0100540LiveInterval* LiveInterval::GetSiblingAt(size_t position) {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000541 LiveInterval* current = this;
David Brazdil241a4862015-04-16 17:59:03 +0100542 while (current != nullptr && !current->IsDefinedAt(position)) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100543 current = current->GetNextSibling();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100544 }
David Brazdil241a4862015-04-16 17:59:03 +0100545 return current;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100546}
547
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100548} // namespace art