blob: 63635f3127f0848c851c10d1e216710491d6f060 [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() {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010026 LinearizeGraph();
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
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010031static bool IsLoop(HLoopInformation* info) {
32 return info != nullptr;
33}
34
35static bool InSameLoop(HLoopInformation* first_loop, HLoopInformation* second_loop) {
36 return first_loop == second_loop;
37}
38
39static bool IsInnerLoop(HLoopInformation* outer, HLoopInformation* inner) {
40 return (inner != outer)
41 && (inner != nullptr)
42 && (outer != nullptr)
43 && inner->IsIn(*outer);
44}
45
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000046static void AddToListForLinearization(GrowableArray<HBasicBlock*>* worklist, HBasicBlock* block) {
47 size_t insert_at = worklist->Size();
48 HLoopInformation* block_loop = block->GetLoopInformation();
49 for (; insert_at > 0; --insert_at) {
50 HBasicBlock* current = worklist->Get(insert_at - 1);
51 HLoopInformation* current_loop = current->GetLoopInformation();
52 if (InSameLoop(block_loop, current_loop)
53 || !IsLoop(current_loop)
54 || IsInnerLoop(current_loop, block_loop)) {
55 // The block can be processed immediately.
56 break;
Nicolas Geoffraye50fa582014-11-24 17:44:15 +000057 }
Nicolas Geoffraye50fa582014-11-24 17:44:15 +000058 }
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000059 worklist->InsertAt(insert_at, block);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010060}
61
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010062void SsaLivenessAnalysis::LinearizeGraph() {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000063 // Create a reverse post ordering with the following properties:
64 // - Blocks in a loop are consecutive,
65 // - Back-edge is the last block before loop exits.
66
67 // (1): Record the number of forward predecessors for each block. This is to
68 // ensure the resulting order is reverse post order. We could use the
69 // current reverse post order in the graph, but it would require making
70 // order queries to a GrowableArray, which is not the best data structure
71 // for it.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010072 GrowableArray<uint32_t> forward_predecessors(graph_->GetArena(), graph_->GetBlocks().Size());
73 forward_predecessors.SetSize(graph_->GetBlocks().Size());
74 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
David Brazdil46e2a392015-03-16 17:31:52 +000075 HBasicBlock* block = it.Current();
Vladimir Marko60584552015-09-03 13:35:12 +000076 size_t number_of_forward_predecessors = block->GetPredecessors().size();
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000077 if (block->IsLoopHeader()) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010078 number_of_forward_predecessors -= block->GetLoopInformation()->NumberOfBackEdges();
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000079 }
80 forward_predecessors.Put(block->GetBlockId(), number_of_forward_predecessors);
81 }
82
83 // (2): Following a worklist approach, first start with the entry block, and
84 // iterate over the successors. When all non-back edge predecessors of a
85 // successor block are visited, the successor block is added in the worklist
86 // following an order that satisfies the requirements to build our linear graph.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010087 GrowableArray<HBasicBlock*> worklist(graph_->GetArena(), 1);
88 worklist.Add(graph_->GetEntryBlock());
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000089 do {
90 HBasicBlock* current = worklist.Pop();
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010091 graph_->linear_order_.Add(current);
Vladimir Marko60584552015-09-03 13:35:12 +000092 for (HBasicBlock* successor : current->GetSuccessors()) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000093 int block_id = successor->GetBlockId();
94 size_t number_of_remaining_predecessors = forward_predecessors.Get(block_id);
95 if (number_of_remaining_predecessors == 1) {
96 AddToListForLinearization(&worklist, successor);
97 }
98 forward_predecessors.Put(block_id, number_of_remaining_predecessors - 1);
99 }
100 } while (!worklist.IsEmpty());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100101}
102
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100103void SsaLivenessAnalysis::NumberInstructions() {
104 int ssa_index = 0;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100105 size_t lifetime_position = 0;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100106 // Each instruction gets a lifetime position, and a block gets a lifetime
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100107 // start and end position. Non-phi instructions have a distinct lifetime position than
108 // the block they are in. Phi instructions have the lifetime start of their block as
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100109 // lifetime position.
110 //
111 // Because the register allocator will insert moves in the graph, we need
112 // to differentiate between the start and end of an instruction. Adding 2 to
113 // the lifetime position for each instruction ensures the start of an
114 // instruction is different than the end of the previous instruction.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100115 for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100116 HBasicBlock* block = it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100117 block->SetLifetimeStart(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100118
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800119 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
120 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000121 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100122 LocationSummary* locations = current->GetLocations();
123 if (locations != nullptr && locations->Out().IsValid()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100124 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100125 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100126 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100127 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100128 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100129 current->SetLifetimePosition(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100130 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100131 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100132
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100133 // Add a null marker to notify we are starting a block.
134 instructions_from_lifetime_position_.Add(nullptr);
135
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800136 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
137 inst_it.Advance()) {
138 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000139 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100140 LocationSummary* locations = current->GetLocations();
141 if (locations != nullptr && locations->Out().IsValid()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100142 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100143 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100144 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100145 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100146 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100147 instructions_from_lifetime_position_.Add(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100148 current->SetLifetimePosition(lifetime_position);
149 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100150 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100151
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100152 block->SetLifetimeEnd(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100153 }
154 number_of_ssa_values_ = ssa_index;
155}
156
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100157void SsaLivenessAnalysis::ComputeLiveness() {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100158 for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100159 HBasicBlock* block = it.Current();
160 block_infos_.Put(
161 block->GetBlockId(),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100162 new (graph_->GetArena()) BlockInfo(graph_->GetArena(), *block, number_of_ssa_values_));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163 }
164
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100165 // Compute the live ranges, as well as the initial live_in, live_out, and kill sets.
166 // This method does not handle backward branches for the sets, therefore live_in
167 // and live_out sets are not yet correct.
168 ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100169
170 // Do a fixed point calculation to take into account backward branches,
171 // that will update live_in of loop headers, and therefore live_out and live_in
172 // of blocks in the loop.
173 ComputeLiveInAndLiveOutSets();
174}
175
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100176void SsaLivenessAnalysis::ComputeLiveRanges() {
177 // Do a post order visit, adding inputs of instructions live in the block where
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100178 // that instruction is defined, and killing instructions that are being visited.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100179 for (HLinearPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100180 HBasicBlock* block = it.Current();
181
182 BitVector* kill = GetKillSet(*block);
183 BitVector* live_in = GetLiveInSet(*block);
184
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100185 // Set phi inputs of successors of this block corresponding to this block
186 // as live_in.
Vladimir Marko60584552015-09-03 13:35:12 +0000187 for (HBasicBlock* successor : block->GetSuccessors()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100188 live_in->Union(GetLiveInSet(*successor));
David Brazdil77a48ae2015-09-15 12:34:04 +0000189 if (successor->IsCatchBlock()) {
190 // Inputs of catch phis will be kept alive through their environment
191 // uses, allowing the runtime to copy their values to the corresponding
192 // catch phi spill slots when an exception is thrown.
193 // The only instructions which may not be recorded in the environments
194 // are constants created by the SSA builder as typed equivalents of
195 // untyped constants from the bytecode, or phis with only such constants
196 // as inputs (verified by SSAChecker). Their raw binary value must
197 // therefore be the same and we only need to keep alive one.
198 } else {
199 size_t phi_input_index = successor->GetPredecessorIndexOf(block);
200 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
201 HInstruction* phi = phi_it.Current();
202 HInstruction* input = phi->InputAt(phi_input_index);
203 input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block);
204 // A phi input whose last user is the phi dies at the end of the predecessor block,
205 // and not at the phi's lifetime position.
206 live_in->SetBit(input->GetSsaIndex());
207 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100208 }
209 }
210
211 // Add a range that covers this block to all instructions live_in because of successors.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100212 // Instructions defined in this block will have their start of the range adjusted.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100213 for (uint32_t idx : live_in->Indexes()) {
214 HInstruction* current = instructions_from_ssa_index_.Get(idx);
215 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100216 }
217
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800218 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
219 back_it.Advance()) {
220 HInstruction* current = back_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100221 if (current->HasSsaIndex()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100222 // Kill the instruction and shorten its interval.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100223 kill->SetBit(current->GetSsaIndex());
224 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100225 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100226 }
227
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000228 // Process the environment first, because we know their uses come after
229 // or at the same liveness position of inputs.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100230 for (HEnvironment* environment = current->GetEnvironment();
231 environment != nullptr;
232 environment = environment->GetParent()) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000233 // Handle environment uses. See statements (b) and (c) of the
234 // SsaLivenessAnalysis.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000235 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
236 HInstruction* instruction = environment->GetInstructionAt(i);
Mingyao Yang718493c2015-07-22 15:56:34 -0700237 bool should_be_live = ShouldBeLiveForEnvironment(current, instruction);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000238 if (should_be_live) {
239 DCHECK(instruction->HasSsaIndex());
240 live_in->SetBit(instruction->GetSsaIndex());
241 }
242 if (instruction != nullptr) {
243 instruction->GetLiveInterval()->AddUse(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100244 current, environment, i, should_be_live);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000245 }
246 }
247 }
248
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100249 // All inputs of an instruction must be live.
250 for (size_t i = 0, e = current->InputCount(); i < e; ++i) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100251 HInstruction* input = current->InputAt(i);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100252 // Some instructions 'inline' their inputs, that is they do not need
253 // to be materialized.
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100254 if (input->HasSsaIndex() && current->GetLocations()->InAt(i).IsValid()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100255 live_in->SetBit(input->GetSsaIndex());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100256 input->GetLiveInterval()->AddUse(current, /* environment */ nullptr, i);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100257 }
258 }
259 }
260
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100261 // Kill phis defined in this block.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800262 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
263 HInstruction* current = inst_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100264 if (current->HasSsaIndex()) {
265 kill->SetBit(current->GetSsaIndex());
266 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100267 LiveInterval* interval = current->GetLiveInterval();
268 DCHECK((interval->GetFirstRange() == nullptr)
269 || (interval->GetStart() == current->GetLifetimePosition()));
270 interval->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100271 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100272 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100273
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274 if (block->IsLoopHeader()) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100275 size_t last_position = block->GetLoopInformation()->GetLifetimeEnd();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276 // For all live_in instructions at the loop header, we need to create a range
277 // that covers the full loop.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100278 for (uint32_t idx : live_in->Indexes()) {
279 HInstruction* current = instructions_from_ssa_index_.Get(idx);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100280 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(), last_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100281 }
282 }
283 }
284}
285
286void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
287 bool changed;
288 do {
289 changed = false;
290
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100291 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100292 const HBasicBlock& block = *it.Current();
293
294 // The live_in set depends on the kill set (which does not
295 // change in this loop), and the live_out set. If the live_out
296 // set does not change, there is no need to update the live_in set.
297 if (UpdateLiveOut(block) && UpdateLiveIn(block)) {
298 changed = true;
299 }
300 }
301 } while (changed);
302}
303
304bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
305 BitVector* live_out = GetLiveOutSet(block);
306 bool changed = false;
307 // The live_out set of a block is the union of live_in sets of its successors.
Vladimir Marko60584552015-09-03 13:35:12 +0000308 for (HBasicBlock* successor : block.GetSuccessors()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100309 if (live_out->Union(GetLiveInSet(*successor))) {
310 changed = true;
311 }
312 }
313 return changed;
314}
315
316
317bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
318 BitVector* live_out = GetLiveOutSet(block);
319 BitVector* kill = GetKillSet(block);
320 BitVector* live_in = GetLiveInSet(block);
321 // If live_out is updated (because of backward branches), we need to make
322 // sure instructions in live_out are also in live_in, unless they are killed
323 // by this block.
324 return live_in->UnionIfNotIn(live_out, kill);
325}
326
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000327static int RegisterOrLowRegister(Location location) {
328 return location.IsPair() ? location.low() : location.reg();
329}
330
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100331int LiveInterval::FindFirstRegisterHint(size_t* free_until,
332 const SsaLivenessAnalysis& liveness) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000333 DCHECK(!IsHighInterval());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000334 if (IsTemp()) return kNoRegister;
335
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100336 if (GetParent() == this && defined_by_ != nullptr) {
337 // This is the first interval for the instruction. Try to find
338 // a register based on its definition.
339 DCHECK_EQ(defined_by_->GetLiveInterval(), this);
340 int hint = FindHintAtDefinition();
341 if (hint != kNoRegister && free_until[hint] > GetStart()) {
342 return hint;
343 }
344 }
345
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100346 if (IsSplit() && liveness.IsAtBlockBoundary(GetStart() / 2)) {
347 // If the start of this interval is at a block boundary, we look at the
348 // location of the interval in blocks preceding the block this interval
349 // starts at. If one location is a register we return it as a hint. This
350 // will avoid a move between the two blocks.
351 HBasicBlock* block = liveness.GetBlockFromPosition(GetStart() / 2);
Nicolas Geoffray82726882015-06-01 13:51:57 +0100352 size_t next_register_use = FirstRegisterUse();
Vladimir Marko60584552015-09-03 13:35:12 +0000353 for (HBasicBlock* predecessor : block->GetPredecessors()) {
354 size_t position = predecessor->GetLifetimeEnd() - 1;
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100355 // We know positions above GetStart() do not have a location yet.
356 if (position < GetStart()) {
357 LiveInterval* existing = GetParent()->GetSiblingAt(position);
358 if (existing != nullptr
359 && existing->HasRegister()
Nicolas Geoffray82726882015-06-01 13:51:57 +0100360 // It's worth using that register if it is available until
361 // the next use.
362 && (free_until[existing->GetRegister()] >= next_register_use)) {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100363 return existing->GetRegister();
364 }
365 }
366 }
367 }
368
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100369 UsePosition* use = first_use_;
370 size_t start = GetStart();
371 size_t end = GetEnd();
372 while (use != nullptr && use->GetPosition() <= end) {
373 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100374 if (use_position >= start && !use->IsSynthesized()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100375 HInstruction* user = use->GetUser();
376 size_t input_index = use->GetInputIndex();
377 if (user->IsPhi()) {
378 // If the phi has a register, try to use the same.
379 Location phi_location = user->GetLiveInterval()->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000380 if (phi_location.IsRegisterKind()) {
381 DCHECK(SameRegisterKind(phi_location));
382 int reg = RegisterOrLowRegister(phi_location);
383 if (free_until[reg] >= use_position) {
384 return reg;
385 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100386 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100387 // If the instruction dies at the phi assignment, we can try having the
388 // same register.
Vladimir Marko60584552015-09-03 13:35:12 +0000389 if (end == user->GetBlock()->GetPredecessor(input_index)->GetLifetimeEnd()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100390 for (size_t i = 0, e = user->InputCount(); i < e; ++i) {
391 if (i == input_index) {
392 continue;
393 }
394 HInstruction* input = user->InputAt(i);
395 Location location = input->GetLiveInterval()->GetLocationAt(
Vladimir Marko60584552015-09-03 13:35:12 +0000396 user->GetBlock()->GetPredecessor(i)->GetLifetimeEnd() - 1);
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000397 if (location.IsRegisterKind()) {
398 int reg = RegisterOrLowRegister(location);
399 if (free_until[reg] >= use_position) {
400 return reg;
401 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100402 }
403 }
404 }
405 } else {
406 // If the instruction is expected in a register, try to use it.
407 LocationSummary* locations = user->GetLocations();
408 Location expected = locations->InAt(use->GetInputIndex());
409 // We use the user's lifetime position - 1 (and not `use_position`) because the
410 // register is blocked at the beginning of the user.
411 size_t position = user->GetLifetimePosition() - 1;
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000412 if (expected.IsRegisterKind()) {
413 DCHECK(SameRegisterKind(expected));
414 int reg = RegisterOrLowRegister(expected);
415 if (free_until[reg] >= position) {
416 return reg;
417 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100418 }
419 }
420 }
421 use = use->GetNext();
422 }
423
424 return kNoRegister;
425}
426
427int LiveInterval::FindHintAtDefinition() const {
428 if (defined_by_->IsPhi()) {
429 // Try to use the same register as one of the inputs.
Vladimir Marko60584552015-09-03 13:35:12 +0000430 const ArenaVector<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100431 for (size_t i = 0, e = defined_by_->InputCount(); i < e; ++i) {
432 HInstruction* input = defined_by_->InputAt(i);
Vladimir Marko60584552015-09-03 13:35:12 +0000433 DCHECK_LT(i, predecessors.size());
434 size_t end = predecessors[i]->GetLifetimeEnd();
David Brazdil241a4862015-04-16 17:59:03 +0100435 LiveInterval* input_interval = input->GetLiveInterval()->GetSiblingAt(end - 1);
436 if (input_interval->GetEnd() == end) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100437 // If the input dies at the end of the predecessor, we know its register can
438 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100439 Location input_location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000440 if (input_location.IsRegisterKind()) {
441 DCHECK(SameRegisterKind(input_location));
442 return RegisterOrLowRegister(input_location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100443 }
444 }
445 }
446 } else {
447 LocationSummary* locations = GetDefinedBy()->GetLocations();
448 Location out = locations->Out();
449 if (out.IsUnallocated() && out.GetPolicy() == Location::kSameAsFirstInput) {
450 // Try to use the same register as the first input.
David Brazdil241a4862015-04-16 17:59:03 +0100451 LiveInterval* input_interval =
452 GetDefinedBy()->InputAt(0)->GetLiveInterval()->GetSiblingAt(GetStart() - 1);
453 if (input_interval->GetEnd() == GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100454 // If the input dies at the start of this instruction, we know its register can
455 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100456 Location location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000457 if (location.IsRegisterKind()) {
458 DCHECK(SameRegisterKind(location));
459 return RegisterOrLowRegister(location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100460 }
461 }
462 }
463 }
464 return kNoRegister;
465}
466
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100467bool LiveInterval::SameRegisterKind(Location other) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000468 if (IsFloatingPoint()) {
469 if (IsLowInterval() || IsHighInterval()) {
470 return other.IsFpuRegisterPair();
471 } else {
472 return other.IsFpuRegister();
473 }
474 } else {
475 if (IsLowInterval() || IsHighInterval()) {
476 return other.IsRegisterPair();
477 } else {
478 return other.IsRegister();
479 }
480 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100481}
482
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100483bool LiveInterval::NeedsTwoSpillSlots() const {
484 return type_ == Primitive::kPrimLong || type_ == Primitive::kPrimDouble;
485}
486
487Location LiveInterval::ToLocation() const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000488 DCHECK(!IsHighInterval());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100489 if (HasRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000490 if (IsFloatingPoint()) {
491 if (HasHighInterval()) {
492 return Location::FpuRegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
493 } else {
494 return Location::FpuRegisterLocation(GetRegister());
495 }
496 } else {
497 if (HasHighInterval()) {
498 return Location::RegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
499 } else {
500 return Location::RegisterLocation(GetRegister());
501 }
502 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100503 } else {
504 HInstruction* defined_by = GetParent()->GetDefinedBy();
505 if (defined_by->IsConstant()) {
506 return defined_by->GetLocations()->Out();
507 } else if (GetParent()->HasSpillSlot()) {
508 if (NeedsTwoSpillSlots()) {
509 return Location::DoubleStackSlot(GetParent()->GetSpillSlot());
510 } else {
511 return Location::StackSlot(GetParent()->GetSpillSlot());
512 }
513 } else {
514 return Location();
515 }
516 }
517}
518
David Brazdil5b8e6a52015-02-25 16:17:05 +0000519Location LiveInterval::GetLocationAt(size_t position) {
David Brazdil241a4862015-04-16 17:59:03 +0100520 LiveInterval* sibling = GetSiblingAt(position);
521 DCHECK(sibling != nullptr);
522 return sibling->ToLocation();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100523}
524
David Brazdil241a4862015-04-16 17:59:03 +0100525LiveInterval* LiveInterval::GetSiblingAt(size_t position) {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000526 LiveInterval* current = this;
David Brazdil241a4862015-04-16 17:59:03 +0100527 while (current != nullptr && !current->IsDefinedAt(position)) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100528 current = current->GetNextSibling();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100529 }
David Brazdil241a4862015-04-16 17:59:03 +0100530 return current;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100531}
532
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100533} // namespace art