blob: 09a664834ffa5603987c1b142e66e91b439631b4 [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();
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000076 size_t number_of_forward_predecessors = block->GetPredecessors().Size();
77 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);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000092 for (size_t i = 0, e = current->GetSuccessors().Size(); i < e; ++i) {
93 HBasicBlock* successor = current->GetSuccessors().Get(i);
94 int block_id = successor->GetBlockId();
95 size_t number_of_remaining_predecessors = forward_predecessors.Get(block_id);
96 if (number_of_remaining_predecessors == 1) {
97 AddToListForLinearization(&worklist, successor);
98 }
99 forward_predecessors.Put(block_id, number_of_remaining_predecessors - 1);
100 }
101 } while (!worklist.IsEmpty());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100102}
103
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100104void SsaLivenessAnalysis::NumberInstructions() {
105 int ssa_index = 0;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100106 size_t lifetime_position = 0;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100107 // Each instruction gets a lifetime position, and a block gets a lifetime
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100108 // start and end position. Non-phi instructions have a distinct lifetime position than
109 // the block they are in. Phi instructions have the lifetime start of their block as
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100110 // lifetime position.
111 //
112 // Because the register allocator will insert moves in the graph, we need
113 // to differentiate between the start and end of an instruction. Adding 2 to
114 // the lifetime position for each instruction ensures the start of an
115 // instruction is different than the end of the previous instruction.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100116 for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100117 HBasicBlock* block = it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100118 block->SetLifetimeStart(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100119
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800120 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
121 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000122 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100123 LocationSummary* locations = current->GetLocations();
124 if (locations != nullptr && locations->Out().IsValid()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100125 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100126 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100127 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100128 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100129 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100130 current->SetLifetimePosition(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100131 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100132 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100133
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100134 // Add a null marker to notify we are starting a block.
135 instructions_from_lifetime_position_.Add(nullptr);
136
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800137 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
138 inst_it.Advance()) {
139 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000140 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100141 LocationSummary* locations = current->GetLocations();
142 if (locations != nullptr && locations->Out().IsValid()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100143 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100144 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100145 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100146 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100147 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100148 instructions_from_lifetime_position_.Add(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100149 current->SetLifetimePosition(lifetime_position);
150 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100151 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100152
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100153 block->SetLifetimeEnd(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100154 }
155 number_of_ssa_values_ = ssa_index;
156}
157
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100158void SsaLivenessAnalysis::ComputeLiveness() {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100159 for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100160 HBasicBlock* block = it.Current();
161 block_infos_.Put(
162 block->GetBlockId(),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100163 new (graph_->GetArena()) BlockInfo(graph_->GetArena(), *block, number_of_ssa_values_));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100164 }
165
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100166 // Compute the live ranges, as well as the initial live_in, live_out, and kill sets.
167 // This method does not handle backward branches for the sets, therefore live_in
168 // and live_out sets are not yet correct.
169 ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100170
171 // Do a fixed point calculation to take into account backward branches,
172 // that will update live_in of loop headers, and therefore live_out and live_in
173 // of blocks in the loop.
174 ComputeLiveInAndLiveOutSets();
175}
176
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100177void SsaLivenessAnalysis::ComputeLiveRanges() {
178 // Do a post order visit, adding inputs of instructions live in the block where
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100179 // that instruction is defined, and killing instructions that are being visited.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100180 for (HLinearPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100181 HBasicBlock* block = it.Current();
182
183 BitVector* kill = GetKillSet(*block);
184 BitVector* live_in = GetLiveInSet(*block);
185
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100186 // Set phi inputs of successors of this block corresponding to this block
187 // as live_in.
188 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
189 HBasicBlock* successor = block->GetSuccessors().Get(i);
190 live_in->Union(GetLiveInSet(*successor));
191 size_t phi_input_index = successor->GetPredecessorIndexOf(block);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800192 for (HInstructionIterator inst_it(successor->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
193 HInstruction* phi = inst_it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100194 HInstruction* input = phi->InputAt(phi_input_index);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100195 input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100196 // A phi input whose last user is the phi dies at the end of the predecessor block,
197 // and not at the phi's lifetime position.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100198 live_in->SetBit(input->GetSsaIndex());
199 }
200 }
201
202 // Add a range that covers this block to all instructions live_in because of successors.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100203 // Instructions defined in this block will have their start of the range adjusted.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100204 for (uint32_t idx : live_in->Indexes()) {
205 HInstruction* current = instructions_from_ssa_index_.Get(idx);
206 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100207 }
208
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800209 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
210 back_it.Advance()) {
211 HInstruction* current = back_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100212 if (current->HasSsaIndex()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100213 // Kill the instruction and shorten its interval.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100214 kill->SetBit(current->GetSsaIndex());
215 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100216 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100217 }
218
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000219 // Process the environment first, because we know their uses come after
220 // or at the same liveness position of inputs.
221 if (current->HasEnvironment()) {
222 // Handle environment uses. See statements (b) and (c) of the
223 // SsaLivenessAnalysis.
224 HEnvironment* environment = current->GetEnvironment();
225 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
226 HInstruction* instruction = environment->GetInstructionAt(i);
227 bool should_be_live = ShouldBeLiveForEnvironment(instruction);
228 if (should_be_live) {
229 DCHECK(instruction->HasSsaIndex());
230 live_in->SetBit(instruction->GetSsaIndex());
231 }
232 if (instruction != nullptr) {
233 instruction->GetLiveInterval()->AddUse(
234 current, i, /* is_environment */ true, should_be_live);
235 }
236 }
237 }
238
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100239 // All inputs of an instruction must be live.
240 for (size_t i = 0, e = current->InputCount(); i < e; ++i) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100241 HInstruction* input = current->InputAt(i);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100242 // Some instructions 'inline' their inputs, that is they do not need
243 // to be materialized.
244 if (input->HasSsaIndex()) {
245 live_in->SetBit(input->GetSsaIndex());
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000246 input->GetLiveInterval()->AddUse(current, i, /* is_environment */ false);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100247 }
248 }
249 }
250
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100251 // Kill phis defined in this block.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800252 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
253 HInstruction* current = inst_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100254 if (current->HasSsaIndex()) {
255 kill->SetBit(current->GetSsaIndex());
256 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100257 LiveInterval* interval = current->GetLiveInterval();
258 DCHECK((interval->GetFirstRange() == nullptr)
259 || (interval->GetStart() == current->GetLifetimePosition()));
260 interval->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100261 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100262 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100263
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100264 if (block->IsLoopHeader()) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100265 size_t last_position = block->GetLoopInformation()->GetLifetimeEnd();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100266 // For all live_in instructions at the loop header, we need to create a range
267 // that covers the full loop.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100268 for (uint32_t idx : live_in->Indexes()) {
269 HInstruction* current = instructions_from_ssa_index_.Get(idx);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100270 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(), last_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100271 }
272 }
273 }
274}
275
276void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
277 bool changed;
278 do {
279 changed = false;
280
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100281 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100282 const HBasicBlock& block = *it.Current();
283
284 // The live_in set depends on the kill set (which does not
285 // change in this loop), and the live_out set. If the live_out
286 // set does not change, there is no need to update the live_in set.
287 if (UpdateLiveOut(block) && UpdateLiveIn(block)) {
288 changed = true;
289 }
290 }
291 } while (changed);
292}
293
294bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
295 BitVector* live_out = GetLiveOutSet(block);
296 bool changed = false;
297 // The live_out set of a block is the union of live_in sets of its successors.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100298 for (size_t i = 0, e = block.GetSuccessors().Size(); i < e; ++i) {
299 HBasicBlock* successor = block.GetSuccessors().Get(i);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100300 if (live_out->Union(GetLiveInSet(*successor))) {
301 changed = true;
302 }
303 }
304 return changed;
305}
306
307
308bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
309 BitVector* live_out = GetLiveOutSet(block);
310 BitVector* kill = GetKillSet(block);
311 BitVector* live_in = GetLiveInSet(block);
312 // If live_out is updated (because of backward branches), we need to make
313 // sure instructions in live_out are also in live_in, unless they are killed
314 // by this block.
315 return live_in->UnionIfNotIn(live_out, kill);
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);
343 for (size_t i = 0; i < block->GetPredecessors().Size(); ++i) {
344 size_t position = block->GetPredecessors().Get(i)->GetLifetimeEnd() - 1;
345 // We know positions above GetStart() do not have a location yet.
346 if (position < GetStart()) {
347 LiveInterval* existing = GetParent()->GetSiblingAt(position);
348 if (existing != nullptr
349 && existing->HasRegister()
350 && (free_until[existing->GetRegister()] > GetStart())) {
351 return existing->GetRegister();
352 }
353 }
354 }
355 }
356
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100357 UsePosition* use = first_use_;
358 size_t start = GetStart();
359 size_t end = GetEnd();
360 while (use != nullptr && use->GetPosition() <= end) {
361 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100362 if (use_position >= start && !use->IsSynthesized()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100363 HInstruction* user = use->GetUser();
364 size_t input_index = use->GetInputIndex();
365 if (user->IsPhi()) {
366 // If the phi has a register, try to use the same.
367 Location phi_location = user->GetLiveInterval()->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000368 if (phi_location.IsRegisterKind()) {
369 DCHECK(SameRegisterKind(phi_location));
370 int reg = RegisterOrLowRegister(phi_location);
371 if (free_until[reg] >= use_position) {
372 return reg;
373 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100374 }
375 const GrowableArray<HBasicBlock*>& predecessors = user->GetBlock()->GetPredecessors();
376 // If the instruction dies at the phi assignment, we can try having the
377 // same register.
378 if (end == predecessors.Get(input_index)->GetLifetimeEnd()) {
379 for (size_t i = 0, e = user->InputCount(); i < e; ++i) {
380 if (i == input_index) {
381 continue;
382 }
383 HInstruction* input = user->InputAt(i);
384 Location location = input->GetLiveInterval()->GetLocationAt(
385 predecessors.Get(i)->GetLifetimeEnd() - 1);
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000386 if (location.IsRegisterKind()) {
387 int reg = RegisterOrLowRegister(location);
388 if (free_until[reg] >= use_position) {
389 return reg;
390 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100391 }
392 }
393 }
394 } else {
395 // If the instruction is expected in a register, try to use it.
396 LocationSummary* locations = user->GetLocations();
397 Location expected = locations->InAt(use->GetInputIndex());
398 // We use the user's lifetime position - 1 (and not `use_position`) because the
399 // register is blocked at the beginning of the user.
400 size_t position = user->GetLifetimePosition() - 1;
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000401 if (expected.IsRegisterKind()) {
402 DCHECK(SameRegisterKind(expected));
403 int reg = RegisterOrLowRegister(expected);
404 if (free_until[reg] >= position) {
405 return reg;
406 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100407 }
408 }
409 }
410 use = use->GetNext();
411 }
412
413 return kNoRegister;
414}
415
416int LiveInterval::FindHintAtDefinition() const {
417 if (defined_by_->IsPhi()) {
418 // Try to use the same register as one of the inputs.
419 const GrowableArray<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors();
420 for (size_t i = 0, e = defined_by_->InputCount(); i < e; ++i) {
421 HInstruction* input = defined_by_->InputAt(i);
422 size_t end = predecessors.Get(i)->GetLifetimeEnd();
David Brazdil241a4862015-04-16 17:59:03 +0100423 LiveInterval* input_interval = input->GetLiveInterval()->GetSiblingAt(end - 1);
424 if (input_interval->GetEnd() == end) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100425 // If the input dies at the end of the predecessor, we know its register can
426 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100427 Location input_location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000428 if (input_location.IsRegisterKind()) {
429 DCHECK(SameRegisterKind(input_location));
430 return RegisterOrLowRegister(input_location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100431 }
432 }
433 }
434 } else {
435 LocationSummary* locations = GetDefinedBy()->GetLocations();
436 Location out = locations->Out();
437 if (out.IsUnallocated() && out.GetPolicy() == Location::kSameAsFirstInput) {
438 // Try to use the same register as the first input.
David Brazdil241a4862015-04-16 17:59:03 +0100439 LiveInterval* input_interval =
440 GetDefinedBy()->InputAt(0)->GetLiveInterval()->GetSiblingAt(GetStart() - 1);
441 if (input_interval->GetEnd() == GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100442 // If the input dies at the start of this instruction, we know its register can
443 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100444 Location location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000445 if (location.IsRegisterKind()) {
446 DCHECK(SameRegisterKind(location));
447 return RegisterOrLowRegister(location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100448 }
449 }
450 }
451 }
452 return kNoRegister;
453}
454
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100455bool LiveInterval::SameRegisterKind(Location other) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000456 if (IsFloatingPoint()) {
457 if (IsLowInterval() || IsHighInterval()) {
458 return other.IsFpuRegisterPair();
459 } else {
460 return other.IsFpuRegister();
461 }
462 } else {
463 if (IsLowInterval() || IsHighInterval()) {
464 return other.IsRegisterPair();
465 } else {
466 return other.IsRegister();
467 }
468 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100469}
470
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100471bool LiveInterval::NeedsTwoSpillSlots() const {
472 return type_ == Primitive::kPrimLong || type_ == Primitive::kPrimDouble;
473}
474
475Location LiveInterval::ToLocation() const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000476 DCHECK(!IsHighInterval());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100477 if (HasRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000478 if (IsFloatingPoint()) {
479 if (HasHighInterval()) {
480 return Location::FpuRegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
481 } else {
482 return Location::FpuRegisterLocation(GetRegister());
483 }
484 } else {
485 if (HasHighInterval()) {
486 return Location::RegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
487 } else {
488 return Location::RegisterLocation(GetRegister());
489 }
490 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100491 } else {
492 HInstruction* defined_by = GetParent()->GetDefinedBy();
493 if (defined_by->IsConstant()) {
494 return defined_by->GetLocations()->Out();
495 } else if (GetParent()->HasSpillSlot()) {
496 if (NeedsTwoSpillSlots()) {
497 return Location::DoubleStackSlot(GetParent()->GetSpillSlot());
498 } else {
499 return Location::StackSlot(GetParent()->GetSpillSlot());
500 }
501 } else {
502 return Location();
503 }
504 }
505}
506
David Brazdil5b8e6a52015-02-25 16:17:05 +0000507Location LiveInterval::GetLocationAt(size_t position) {
David Brazdil241a4862015-04-16 17:59:03 +0100508 LiveInterval* sibling = GetSiblingAt(position);
509 DCHECK(sibling != nullptr);
510 return sibling->ToLocation();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100511}
512
David Brazdil241a4862015-04-16 17:59:03 +0100513LiveInterval* LiveInterval::GetSiblingAt(size_t position) {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000514 LiveInterval* current = this;
David Brazdil241a4862015-04-16 17:59:03 +0100515 while (current != nullptr && !current->IsDefinedAt(position)) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100516 current = current->GetNextSibling();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100517 }
David Brazdil241a4862015-04-16 17:59:03 +0100518 return current;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100519}
520
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100521} // namespace art