blob: 7af43028848dac2488f8ac62aef6202e57899fb2 [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
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010046static void AddToListForLinearization(ArenaVector<HBasicBlock*>* worklist, HBasicBlock* block) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000047 HLoopInformation* block_loop = block->GetLoopInformation();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010048 auto insert_pos = worklist->rbegin(); // insert_pos.base() will be the actual position.
49 for (auto end = worklist->rend(); insert_pos != end; ++insert_pos) {
50 HBasicBlock* current = *insert_pos;
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000051 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 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010059 worklist->insert(insert_pos.base(), 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.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010072 ArenaVector<uint32_t> forward_predecessors(graph_->GetBlocks().size(),
73 graph_->GetArena()->Adapter(kArenaAllocSsaLiveness));
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010074 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 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010080 forward_predecessors[block->GetBlockId()] = number_of_forward_predecessors;
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000081 }
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.
Vladimir Markofa6b93c2015-09-15 10:15:55 +010087 graph_->linear_order_.reserve(graph_->GetReversePostOrder().size());
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010088 ArenaVector<HBasicBlock*> worklist(graph_->GetArena()->Adapter(kArenaAllocSsaLiveness));
89 worklist.push_back(graph_->GetEntryBlock());
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000090 do {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010091 HBasicBlock* current = worklist.back();
92 worklist.pop_back();
Vladimir Markofa6b93c2015-09-15 10:15:55 +010093 graph_->linear_order_.push_back(current);
Vladimir Marko60584552015-09-03 13:35:12 +000094 for (HBasicBlock* successor : current->GetSuccessors()) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000095 int block_id = successor->GetBlockId();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010096 size_t number_of_remaining_predecessors = forward_predecessors[block_id];
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000097 if (number_of_remaining_predecessors == 1) {
98 AddToListForLinearization(&worklist, successor);
99 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100100 forward_predecessors[block_id] = number_of_remaining_predecessors - 1;
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000101 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100102 } while (!worklist.empty());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100103}
104
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100105void SsaLivenessAnalysis::NumberInstructions() {
106 int ssa_index = 0;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100107 size_t lifetime_position = 0;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100108 // Each instruction gets a lifetime position, and a block gets a lifetime
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100109 // start and end position. Non-phi instructions have a distinct lifetime position than
110 // the block they are in. Phi instructions have the lifetime start of their block as
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100111 // lifetime position.
112 //
113 // Because the register allocator will insert moves in the graph, we need
114 // to differentiate between the start and end of an instruction. Adding 2 to
115 // the lifetime position for each instruction ensures the start of an
116 // instruction is different than the end of the previous instruction.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100117 for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100118 HBasicBlock* block = it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100119 block->SetLifetimeStart(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100120
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800121 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
122 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000123 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100124 LocationSummary* locations = current->GetLocations();
125 if (locations != nullptr && locations->Out().IsValid()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100126 instructions_from_ssa_index_.push_back(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100127 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100128 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100129 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100130 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100131 current->SetLifetimePosition(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100132 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100133 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100134
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100135 // Add a null marker to notify we are starting a block.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100136 instructions_from_lifetime_position_.push_back(nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100137
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800138 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
139 inst_it.Advance()) {
140 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000141 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100142 LocationSummary* locations = current->GetLocations();
143 if (locations != nullptr && locations->Out().IsValid()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100144 instructions_from_ssa_index_.push_back(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100145 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100147 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100148 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100149 instructions_from_lifetime_position_.push_back(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100150 current->SetLifetimePosition(lifetime_position);
151 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100152 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100153
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100154 block->SetLifetimeEnd(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100155 }
156 number_of_ssa_values_ = ssa_index;
157}
158
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100159void SsaLivenessAnalysis::ComputeLiveness() {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100160 for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100161 HBasicBlock* block = it.Current();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100162 block_infos_[block->GetBlockId()] =
163 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
David Brazdil674f5192016-02-02 16:50:46 +0000177static void RecursivelyProcessInputs(HInstruction* current,
178 HInstruction* actual_user,
179 BitVector* live_in) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100180 HInputsRef inputs = current->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100181 for (size_t i = 0; i < inputs.size(); ++i) {
182 HInstruction* input = inputs[i];
David Brazdil674f5192016-02-02 16:50:46 +0000183 bool has_in_location = current->GetLocations()->InAt(i).IsValid();
184 bool has_out_location = input->GetLocations()->Out().IsValid();
185
186 if (has_in_location) {
187 DCHECK(has_out_location)
188 << "Instruction " << current->DebugName() << current->GetId()
189 << " expects an input value at index " << i << " but "
190 << input->DebugName() << input->GetId() << " does not produce one.";
191 DCHECK(input->HasSsaIndex());
192 // `input` generates a result used by `current`. Add use and update
193 // the live-in set.
194 input->GetLiveInterval()->AddUse(current, /* environment */ nullptr, i, actual_user);
195 live_in->SetBit(input->GetSsaIndex());
196 } else if (has_out_location) {
197 // `input` generates a result but it is not used by `current`.
198 } else {
199 // `input` is inlined into `current`. Walk over its inputs and record
200 // uses at `current`.
201 DCHECK(input->IsEmittedAtUseSite());
202 // Check that the inlined input is not a phi. Recursing on loop phis could
203 // lead to an infinite loop.
204 DCHECK(!input->IsPhi());
205 RecursivelyProcessInputs(input, actual_user, live_in);
206 }
207 }
208}
209
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100210void SsaLivenessAnalysis::ComputeLiveRanges() {
211 // Do a post order visit, adding inputs of instructions live in the block where
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100212 // that instruction is defined, and killing instructions that are being visited.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100213 for (HLinearPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100214 HBasicBlock* block = it.Current();
215
216 BitVector* kill = GetKillSet(*block);
217 BitVector* live_in = GetLiveInSet(*block);
218
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100219 // Set phi inputs of successors of this block corresponding to this block
220 // as live_in.
Vladimir Marko60584552015-09-03 13:35:12 +0000221 for (HBasicBlock* successor : block->GetSuccessors()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100222 live_in->Union(GetLiveInSet(*successor));
David Brazdil77a48ae2015-09-15 12:34:04 +0000223 if (successor->IsCatchBlock()) {
224 // Inputs of catch phis will be kept alive through their environment
225 // uses, allowing the runtime to copy their values to the corresponding
226 // catch phi spill slots when an exception is thrown.
227 // The only instructions which may not be recorded in the environments
228 // are constants created by the SSA builder as typed equivalents of
229 // untyped constants from the bytecode, or phis with only such constants
David Brazdilbadd8262016-02-02 16:28:56 +0000230 // as inputs (verified by GraphChecker). Their raw binary value must
David Brazdil77a48ae2015-09-15 12:34:04 +0000231 // therefore be the same and we only need to keep alive one.
232 } else {
233 size_t phi_input_index = successor->GetPredecessorIndexOf(block);
234 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
235 HInstruction* phi = phi_it.Current();
236 HInstruction* input = phi->InputAt(phi_input_index);
237 input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block);
238 // A phi input whose last user is the phi dies at the end of the predecessor block,
239 // and not at the phi's lifetime position.
240 live_in->SetBit(input->GetSsaIndex());
241 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100242 }
243 }
244
245 // Add a range that covers this block to all instructions live_in because of successors.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100246 // Instructions defined in this block will have their start of the range adjusted.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100247 for (uint32_t idx : live_in->Indexes()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100248 HInstruction* current = GetInstructionFromSsaIndex(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100249 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100250 }
251
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800252 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
253 back_it.Advance()) {
254 HInstruction* current = back_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100255 if (current->HasSsaIndex()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100256 // Kill the instruction and shorten its interval.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100257 kill->SetBit(current->GetSsaIndex());
258 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100259 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100260 }
261
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000262 // Process the environment first, because we know their uses come after
263 // or at the same liveness position of inputs.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100264 for (HEnvironment* environment = current->GetEnvironment();
265 environment != nullptr;
266 environment = environment->GetParent()) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000267 // Handle environment uses. See statements (b) and (c) of the
268 // SsaLivenessAnalysis.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000269 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
270 HInstruction* instruction = environment->GetInstructionAt(i);
Mingyao Yang718493c2015-07-22 15:56:34 -0700271 bool should_be_live = ShouldBeLiveForEnvironment(current, instruction);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000272 if (should_be_live) {
273 DCHECK(instruction->HasSsaIndex());
274 live_in->SetBit(instruction->GetSsaIndex());
275 }
276 if (instruction != nullptr) {
277 instruction->GetLiveInterval()->AddUse(
David Brazdilb3e773e2016-01-26 11:28:37 +0000278 current, environment, i, /* actual_user */ nullptr, should_be_live);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000279 }
280 }
281 }
282
David Brazdilb3e773e2016-01-26 11:28:37 +0000283 // Process inputs of instructions.
284 if (current->IsEmittedAtUseSite()) {
285 if (kIsDebugBuild) {
286 DCHECK(!current->GetLocations()->Out().IsValid());
Vladimir Marko46817b82016-03-29 12:21:58 +0100287 for (const HUseListNode<HInstruction*>& use : current->GetUses()) {
288 HInstruction* user = use.GetUser();
289 size_t index = use.GetIndex();
David Brazdilb3e773e2016-01-26 11:28:37 +0000290 DCHECK(!user->GetLocations()->InAt(index).IsValid());
291 }
292 DCHECK(!current->HasEnvironmentUses());
293 }
294 } else {
David Brazdil674f5192016-02-02 16:50:46 +0000295 RecursivelyProcessInputs(current, current, live_in);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100296 }
297 }
298
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100299 // Kill phis defined in this block.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800300 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
301 HInstruction* current = inst_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100302 if (current->HasSsaIndex()) {
303 kill->SetBit(current->GetSsaIndex());
304 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100305 LiveInterval* interval = current->GetLiveInterval();
306 DCHECK((interval->GetFirstRange() == nullptr)
307 || (interval->GetStart() == current->GetLifetimePosition()));
308 interval->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100309 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100310 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100311
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100312 if (block->IsLoopHeader()) {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100313 if (kIsDebugBuild) {
314 CheckNoLiveInIrreducibleLoop(*block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000315 }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100316 size_t last_position = block->GetLoopInformation()->GetLifetimeEnd();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100317 // For all live_in instructions at the loop header, we need to create a range
318 // that covers the full loop.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100319 for (uint32_t idx : live_in->Indexes()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100320 HInstruction* current = GetInstructionFromSsaIndex(idx);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100321 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(), last_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100322 }
323 }
324 }
325}
326
327void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
328 bool changed;
329 do {
330 changed = false;
331
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100332 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100333 const HBasicBlock& block = *it.Current();
334
335 // The live_in set depends on the kill set (which does not
336 // change in this loop), and the live_out set. If the live_out
337 // set does not change, there is no need to update the live_in set.
338 if (UpdateLiveOut(block) && UpdateLiveIn(block)) {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100339 if (kIsDebugBuild) {
340 CheckNoLiveInIrreducibleLoop(block);
341 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100342 changed = true;
343 }
344 }
345 } while (changed);
346}
347
348bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
349 BitVector* live_out = GetLiveOutSet(block);
350 bool changed = false;
351 // The live_out set of a block is the union of live_in sets of its successors.
Vladimir Marko60584552015-09-03 13:35:12 +0000352 for (HBasicBlock* successor : block.GetSuccessors()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100353 if (live_out->Union(GetLiveInSet(*successor))) {
354 changed = true;
355 }
356 }
357 return changed;
358}
359
360
361bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
362 BitVector* live_out = GetLiveOutSet(block);
363 BitVector* kill = GetKillSet(block);
364 BitVector* live_in = GetLiveInSet(block);
365 // If live_out is updated (because of backward branches), we need to make
366 // sure instructions in live_out are also in live_in, unless they are killed
367 // by this block.
368 return live_in->UnionIfNotIn(live_out, kill);
369}
370
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000371static int RegisterOrLowRegister(Location location) {
372 return location.IsPair() ? location.low() : location.reg();
373}
374
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100375int LiveInterval::FindFirstRegisterHint(size_t* free_until,
376 const SsaLivenessAnalysis& liveness) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000377 DCHECK(!IsHighInterval());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000378 if (IsTemp()) return kNoRegister;
379
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100380 if (GetParent() == this && defined_by_ != nullptr) {
381 // This is the first interval for the instruction. Try to find
382 // a register based on its definition.
383 DCHECK_EQ(defined_by_->GetLiveInterval(), this);
384 int hint = FindHintAtDefinition();
385 if (hint != kNoRegister && free_until[hint] > GetStart()) {
386 return hint;
387 }
388 }
389
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100390 if (IsSplit() && liveness.IsAtBlockBoundary(GetStart() / 2)) {
391 // If the start of this interval is at a block boundary, we look at the
392 // location of the interval in blocks preceding the block this interval
393 // starts at. If one location is a register we return it as a hint. This
394 // will avoid a move between the two blocks.
395 HBasicBlock* block = liveness.GetBlockFromPosition(GetStart() / 2);
Nicolas Geoffray82726882015-06-01 13:51:57 +0100396 size_t next_register_use = FirstRegisterUse();
Vladimir Marko60584552015-09-03 13:35:12 +0000397 for (HBasicBlock* predecessor : block->GetPredecessors()) {
398 size_t position = predecessor->GetLifetimeEnd() - 1;
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100399 // We know positions above GetStart() do not have a location yet.
400 if (position < GetStart()) {
401 LiveInterval* existing = GetParent()->GetSiblingAt(position);
402 if (existing != nullptr
403 && existing->HasRegister()
Nicolas Geoffray82726882015-06-01 13:51:57 +0100404 // It's worth using that register if it is available until
405 // the next use.
406 && (free_until[existing->GetRegister()] >= next_register_use)) {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100407 return existing->GetRegister();
408 }
409 }
410 }
411 }
412
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100413 UsePosition* use = first_use_;
414 size_t start = GetStart();
415 size_t end = GetEnd();
416 while (use != nullptr && use->GetPosition() <= end) {
417 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100418 if (use_position >= start && !use->IsSynthesized()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100419 HInstruction* user = use->GetUser();
420 size_t input_index = use->GetInputIndex();
421 if (user->IsPhi()) {
422 // If the phi has a register, try to use the same.
423 Location phi_location = user->GetLiveInterval()->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000424 if (phi_location.IsRegisterKind()) {
425 DCHECK(SameRegisterKind(phi_location));
426 int reg = RegisterOrLowRegister(phi_location);
427 if (free_until[reg] >= use_position) {
428 return reg;
429 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100430 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100431 // If the instruction dies at the phi assignment, we can try having the
432 // same register.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100433 if (end == user->GetBlock()->GetPredecessors()[input_index]->GetLifetimeEnd()) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100434 HInputsRef inputs = user->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100435 for (size_t i = 0; i < inputs.size(); ++i) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100436 if (i == input_index) {
437 continue;
438 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100439 Location location = inputs[i]->GetLiveInterval()->GetLocationAt(
Vladimir Markoec7802a2015-10-01 20:57:57 +0100440 user->GetBlock()->GetPredecessors()[i]->GetLifetimeEnd() - 1);
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000441 if (location.IsRegisterKind()) {
442 int reg = RegisterOrLowRegister(location);
443 if (free_until[reg] >= use_position) {
444 return reg;
445 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100446 }
447 }
448 }
449 } else {
450 // If the instruction is expected in a register, try to use it.
451 LocationSummary* locations = user->GetLocations();
452 Location expected = locations->InAt(use->GetInputIndex());
453 // We use the user's lifetime position - 1 (and not `use_position`) because the
454 // register is blocked at the beginning of the user.
455 size_t position = user->GetLifetimePosition() - 1;
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000456 if (expected.IsRegisterKind()) {
457 DCHECK(SameRegisterKind(expected));
458 int reg = RegisterOrLowRegister(expected);
459 if (free_until[reg] >= position) {
460 return reg;
461 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100462 }
463 }
464 }
465 use = use->GetNext();
466 }
467
468 return kNoRegister;
469}
470
471int LiveInterval::FindHintAtDefinition() const {
472 if (defined_by_->IsPhi()) {
473 // Try to use the same register as one of the inputs.
Vladimir Marko60584552015-09-03 13:35:12 +0000474 const ArenaVector<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors();
Vladimir Markoe9004912016-06-16 16:50:52 +0100475 HInputsRef inputs = defined_by_->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100476 for (size_t i = 0; i < inputs.size(); ++i) {
Vladimir Marko60584552015-09-03 13:35:12 +0000477 size_t end = predecessors[i]->GetLifetimeEnd();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100478 LiveInterval* input_interval = inputs[i]->GetLiveInterval()->GetSiblingAt(end - 1);
David Brazdil241a4862015-04-16 17:59:03 +0100479 if (input_interval->GetEnd() == end) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100480 // If the input dies at the end of the predecessor, we know its register can
481 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100482 Location input_location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000483 if (input_location.IsRegisterKind()) {
484 DCHECK(SameRegisterKind(input_location));
485 return RegisterOrLowRegister(input_location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100486 }
487 }
488 }
489 } else {
490 LocationSummary* locations = GetDefinedBy()->GetLocations();
491 Location out = locations->Out();
492 if (out.IsUnallocated() && out.GetPolicy() == Location::kSameAsFirstInput) {
493 // Try to use the same register as the first input.
David Brazdil241a4862015-04-16 17:59:03 +0100494 LiveInterval* input_interval =
495 GetDefinedBy()->InputAt(0)->GetLiveInterval()->GetSiblingAt(GetStart() - 1);
496 if (input_interval->GetEnd() == GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100497 // If the input dies at the start of this instruction, we know its register can
498 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100499 Location location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000500 if (location.IsRegisterKind()) {
501 DCHECK(SameRegisterKind(location));
502 return RegisterOrLowRegister(location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100503 }
504 }
505 }
506 }
507 return kNoRegister;
508}
509
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100510bool LiveInterval::SameRegisterKind(Location other) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000511 if (IsFloatingPoint()) {
512 if (IsLowInterval() || IsHighInterval()) {
513 return other.IsFpuRegisterPair();
514 } else {
515 return other.IsFpuRegister();
516 }
517 } else {
518 if (IsLowInterval() || IsHighInterval()) {
519 return other.IsRegisterPair();
520 } else {
521 return other.IsRegister();
522 }
523 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100524}
525
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100526bool LiveInterval::NeedsTwoSpillSlots() const {
527 return type_ == Primitive::kPrimLong || type_ == Primitive::kPrimDouble;
528}
529
530Location LiveInterval::ToLocation() const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000531 DCHECK(!IsHighInterval());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100532 if (HasRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000533 if (IsFloatingPoint()) {
534 if (HasHighInterval()) {
535 return Location::FpuRegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
536 } else {
537 return Location::FpuRegisterLocation(GetRegister());
538 }
539 } else {
540 if (HasHighInterval()) {
541 return Location::RegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
542 } else {
543 return Location::RegisterLocation(GetRegister());
544 }
545 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100546 } else {
547 HInstruction* defined_by = GetParent()->GetDefinedBy();
548 if (defined_by->IsConstant()) {
549 return defined_by->GetLocations()->Out();
550 } else if (GetParent()->HasSpillSlot()) {
551 if (NeedsTwoSpillSlots()) {
552 return Location::DoubleStackSlot(GetParent()->GetSpillSlot());
553 } else {
554 return Location::StackSlot(GetParent()->GetSpillSlot());
555 }
556 } else {
557 return Location();
558 }
559 }
560}
561
David Brazdil5b8e6a52015-02-25 16:17:05 +0000562Location LiveInterval::GetLocationAt(size_t position) {
David Brazdil241a4862015-04-16 17:59:03 +0100563 LiveInterval* sibling = GetSiblingAt(position);
564 DCHECK(sibling != nullptr);
565 return sibling->ToLocation();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100566}
567
David Brazdil241a4862015-04-16 17:59:03 +0100568LiveInterval* LiveInterval::GetSiblingAt(size_t position) {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000569 LiveInterval* current = this;
David Brazdil241a4862015-04-16 17:59:03 +0100570 while (current != nullptr && !current->IsDefinedAt(position)) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100571 current = current->GetNextSibling();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100572 }
David Brazdil241a4862015-04-16 17:59:03 +0100573 return current;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100574}
575
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100576} // namespace art