blob: 54e62a5b2cec597f6bc0f6064fb56e0a055140f7 [file] [log] [blame]
Nicolas Geoffraya7062e02014-05-22 12:50:17 +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 "register_allocator.h"
18
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Ian Rogerse77493c2014-08-20 15:08:45 -070021#include "base/bit_vector-inl.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010022#include "code_generator.h"
23#include "ssa_liveness_analysis.h"
24
25namespace art {
26
27static constexpr size_t kMaxLifetimePosition = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010028static constexpr size_t kDefaultNumberOfSpillSlots = 4;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010029
Nicolas Geoffray840e5462015-01-07 16:01:24 +000030// For simplicity, we implement register pairs as (reg, reg + 1).
31// Note that this is a requirement for double registers on ARM, since we
32// allocate SRegister.
33static int GetHighForLowRegister(int reg) { return reg + 1; }
34static bool IsLowRegister(int reg) { return (reg & 1) == 0; }
35
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010036RegisterAllocator::RegisterAllocator(ArenaAllocator* allocator,
37 CodeGenerator* codegen,
38 const SsaLivenessAnalysis& liveness)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010039 : allocator_(allocator),
40 codegen_(codegen),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010041 liveness_(liveness),
Nicolas Geoffray39468442014-09-02 15:17:15 +010042 unhandled_core_intervals_(allocator, 0),
43 unhandled_fp_intervals_(allocator, 0),
44 unhandled_(nullptr),
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010045 handled_(allocator, 0),
46 active_(allocator, 0),
47 inactive_(allocator, 0),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010048 physical_core_register_intervals_(allocator, codegen->GetNumberOfCoreRegisters()),
49 physical_fp_register_intervals_(allocator, codegen->GetNumberOfFloatingPointRegisters()),
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 temp_intervals_(allocator, 4),
Nicolas Geoffray776b3182015-02-23 14:14:57 +000051 int_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
52 long_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
53 float_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
54 double_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 safepoints_(allocator, 0),
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010056 processing_core_registers_(false),
57 number_of_registers_(-1),
58 registers_array_(nullptr),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010059 blocked_core_registers_(codegen->GetBlockedCoreRegisters()),
60 blocked_fp_registers_(codegen->GetBlockedFloatingPointRegisters()),
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +010061 reserved_out_slots_(0),
Mark Mendellf85a9ca2015-01-13 09:20:58 -050062 maximum_number_of_live_core_registers_(0),
63 maximum_number_of_live_fp_registers_(0) {
Nicolas Geoffray98893962015-01-21 12:32:32 +000064 static constexpr bool kIsBaseline = false;
65 codegen->SetupBlockedRegisters(kIsBaseline);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010066 physical_core_register_intervals_.SetSize(codegen->GetNumberOfCoreRegisters());
67 physical_fp_register_intervals_.SetSize(codegen->GetNumberOfFloatingPointRegisters());
Nicolas Geoffray39468442014-09-02 15:17:15 +010068 // Always reserve for the current method and the graph's max out registers.
69 // TODO: compute it instead.
70 reserved_out_slots_ = 1 + codegen->GetGraph()->GetMaximumNumberOfOutVRegs();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010071}
72
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010073bool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph,
74 InstructionSet instruction_set) {
75 if (!Supports(instruction_set)) {
76 return false;
77 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000078 if (instruction_set == kArm64
79 || instruction_set == kX86_64
80 || instruction_set == kArm
81 || instruction_set == kThumb2) {
Alexandre Rames3e69f162014-12-10 10:36:50 +000082 return true;
83 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010084 for (size_t i = 0, e = graph.GetBlocks().Size(); i < e; ++i) {
85 for (HInstructionIterator it(graph.GetBlocks().Get(i)->GetInstructions());
86 !it.Done();
87 it.Advance()) {
88 HInstruction* current = it.Current();
Mark Mendell7c8d0092015-01-26 11:21:33 -050089 if (instruction_set == kX86 && current->GetType() == Primitive::kPrimLong) {
90 return false;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010091 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010092 }
93 }
94 return true;
95}
96
97static bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) {
Nicolas Geoffray39468442014-09-02 15:17:15 +010098 if (interval == nullptr) return false;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010099 bool is_core_register = (interval->GetType() != Primitive::kPrimDouble)
100 && (interval->GetType() != Primitive::kPrimFloat);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100101 return processing_core_registers == is_core_register;
102}
103
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100104void RegisterAllocator::AllocateRegisters() {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100105 AllocateRegistersInternal();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100106 Resolve();
107
108 if (kIsDebugBuild) {
109 processing_core_registers_ = true;
110 ValidateInternal(true);
111 processing_core_registers_ = false;
112 ValidateInternal(true);
Nicolas Geoffray59768572014-12-01 09:50:04 +0000113 // Check that the linear order is still correct with regards to lifetime positions.
114 // Since only parallel moves have been inserted during the register allocation,
115 // these checks are mostly for making sure these moves have been added correctly.
116 size_t current_liveness = 0;
117 for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
118 HBasicBlock* block = it.Current();
119 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
120 HInstruction* instruction = inst_it.Current();
121 DCHECK_LE(current_liveness, instruction->GetLifetimePosition());
122 current_liveness = instruction->GetLifetimePosition();
123 }
124 for (HInstructionIterator inst_it(block->GetInstructions());
125 !inst_it.Done();
126 inst_it.Advance()) {
127 HInstruction* instruction = inst_it.Current();
128 DCHECK_LE(current_liveness, instruction->GetLifetimePosition()) << instruction->DebugName();
129 current_liveness = instruction->GetLifetimePosition();
130 }
131 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100132 }
133}
134
135void RegisterAllocator::BlockRegister(Location location,
136 size_t start,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100137 size_t end) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100138 int reg = location.reg();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100139 DCHECK(location.IsRegister() || location.IsFpuRegister());
140 LiveInterval* interval = location.IsRegister()
141 ? physical_core_register_intervals_.Get(reg)
142 : physical_fp_register_intervals_.Get(reg);
143 Primitive::Type type = location.IsRegister()
144 ? Primitive::kPrimInt
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000145 : Primitive::kPrimFloat;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100146 if (interval == nullptr) {
147 interval = LiveInterval::MakeFixedInterval(allocator_, reg, type);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100148 if (location.IsRegister()) {
149 physical_core_register_intervals_.Put(reg, interval);
150 } else {
151 physical_fp_register_intervals_.Put(reg, interval);
152 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100153 }
154 DCHECK(interval->GetRegister() == reg);
155 interval->AddRange(start, end);
156}
157
158void RegisterAllocator::AllocateRegistersInternal() {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100159 // Iterate post-order, to ensure the list is sorted, and the last added interval
160 // is the one with the lowest start position.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100161 for (HLinearPostOrderIterator it(liveness_); !it.Done(); it.Advance()) {
162 HBasicBlock* block = it.Current();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800163 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
164 back_it.Advance()) {
165 ProcessInstruction(back_it.Current());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100166 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800167 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
168 ProcessInstruction(inst_it.Current());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100169 }
170 }
171
Nicolas Geoffray39468442014-09-02 15:17:15 +0100172 number_of_registers_ = codegen_->GetNumberOfCoreRegisters();
173 registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
174 processing_core_registers_ = true;
175 unhandled_ = &unhandled_core_intervals_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100176 for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
177 LiveInterval* fixed = physical_core_register_intervals_.Get(i);
178 if (fixed != nullptr) {
Mingyao Yang296bd602014-10-06 16:47:28 -0700179 // Fixed interval is added to inactive_ instead of unhandled_.
180 // It's also the only type of inactive interval whose start position
181 // can be after the current interval during linear scan.
182 // Fixed interval is never split and never moves to unhandled_.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100183 inactive_.Add(fixed);
184 }
185 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100186 LinearScan();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100187
188 inactive_.Reset();
189 active_.Reset();
190 handled_.Reset();
191
192 number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters();
193 registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
194 processing_core_registers_ = false;
195 unhandled_ = &unhandled_fp_intervals_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100196 for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
197 LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
198 if (fixed != nullptr) {
Mingyao Yang296bd602014-10-06 16:47:28 -0700199 // Fixed interval is added to inactive_ instead of unhandled_.
200 // It's also the only type of inactive interval whose start position
201 // can be after the current interval during linear scan.
202 // Fixed interval is never split and never moves to unhandled_.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100203 inactive_.Add(fixed);
204 }
205 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100206 LinearScan();
207}
208
209void RegisterAllocator::ProcessInstruction(HInstruction* instruction) {
210 LocationSummary* locations = instruction->GetLocations();
211 size_t position = instruction->GetLifetimePosition();
212
213 if (locations == nullptr) return;
214
215 // Create synthesized intervals for temporaries.
216 for (size_t i = 0; i < locations->GetTempCount(); ++i) {
217 Location temp = locations->GetTemp(i);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000218 if (temp.IsRegister() || temp.IsFpuRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100219 BlockRegister(temp, position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100220 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100221 DCHECK(temp.IsUnallocated());
Roland Levillain5368c212014-11-27 15:03:41 +0000222 switch (temp.GetPolicy()) {
223 case Location::kRequiresRegister: {
224 LiveInterval* interval =
225 LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt);
226 temp_intervals_.Add(interval);
227 interval->AddRange(position, position + 1);
228 unhandled_core_intervals_.Add(interval);
229 break;
230 }
231
232 case Location::kRequiresFpuRegister: {
233 LiveInterval* interval =
234 LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimDouble);
235 temp_intervals_.Add(interval);
236 interval->AddRange(position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000237 if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
238 interval->AddHighInterval(true);
239 LiveInterval* high = interval->GetHighInterval();
240 temp_intervals_.Add(high);
241 unhandled_fp_intervals_.Add(high);
242 }
Roland Levillain5368c212014-11-27 15:03:41 +0000243 unhandled_fp_intervals_.Add(interval);
244 break;
245 }
246
247 default:
248 LOG(FATAL) << "Unexpected policy for temporary location "
249 << temp.GetPolicy();
250 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100251 }
252 }
253
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100254 bool core_register = (instruction->GetType() != Primitive::kPrimDouble)
255 && (instruction->GetType() != Primitive::kPrimFloat);
256
Nicolas Geoffray39468442014-09-02 15:17:15 +0100257 if (locations->CanCall()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000258 if (codegen_->IsLeafMethod()) {
259 // TODO: We do this here because we do not want the suspend check to artificially
260 // create live registers. We should find another place, but this is currently the
261 // simplest.
262 DCHECK(instruction->IsSuspendCheckEntry());
263 instruction->GetBlock()->RemoveInstruction(instruction);
264 return;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100265 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100266 safepoints_.Add(instruction);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100267 if (locations->OnlyCallsOnSlowPath()) {
268 // We add a synthesized range at this position to record the live registers
269 // at this position. Ideally, we could just update the safepoints when locations
270 // are updated, but we currently need to know the full stack size before updating
271 // locations (because of parameters and the fact that we don't have a frame pointer).
272 // And knowing the full stack size requires to know the maximum number of live
273 // registers at calls in slow paths.
274 // By adding the following interval in the algorithm, we can compute this
275 // maximum before updating locations.
276 LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction);
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000277 interval->AddRange(position, position + 1);
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000278 AddSorted(&unhandled_core_intervals_, interval);
279 AddSorted(&unhandled_fp_intervals_, interval);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100280 }
281 }
282
283 if (locations->WillCall()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100284 // Block all registers.
285 for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000286 if (!codegen_->IsCoreCalleeSaveRegister(i)) {
287 BlockRegister(Location::RegisterLocation(i),
288 position,
289 position + 1);
290 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100291 }
292 for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000293 if (!codegen_->IsFloatingPointCalleeSaveRegister(i)) {
294 BlockRegister(Location::FpuRegisterLocation(i),
295 position,
296 position + 1);
297 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100298 }
299 }
300
301 for (size_t i = 0; i < instruction->InputCount(); ++i) {
302 Location input = locations->InAt(i);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100303 if (input.IsRegister() || input.IsFpuRegister()) {
304 BlockRegister(input, position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000305 } else if (input.IsPair()) {
306 BlockRegister(input.ToLow(), position, position + 1);
307 BlockRegister(input.ToHigh(), position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100308 }
309 }
310
Nicolas Geoffray39468442014-09-02 15:17:15 +0100311 LiveInterval* current = instruction->GetLiveInterval();
312 if (current == nullptr) return;
313
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100314 GrowableArray<LiveInterval*>& unhandled = core_register
315 ? unhandled_core_intervals_
316 : unhandled_fp_intervals_;
317
Nicolas Geoffray76905622014-09-25 14:39:26 +0100318 DCHECK(unhandled.IsEmpty() || current->StartsBeforeOrAt(unhandled.Peek()));
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000319
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000320 if (codegen_->NeedsTwoRegisters(current->GetType())) {
321 current->AddHighInterval();
322 }
323
Nicolas Geoffray39468442014-09-02 15:17:15 +0100324 // Some instructions define their output in fixed register/stack slot. We need
325 // to ensure we know these locations before doing register allocation. For a
326 // given register, we create an interval that covers these locations. The register
327 // will be unavailable at these locations when trying to allocate one for an
328 // interval.
329 //
330 // The backwards walking ensures the ranges are ordered on increasing start positions.
331 Location output = locations->Out();
Calin Juravled0d48522014-11-04 16:40:20 +0000332 if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
333 Location first = locations->InAt(0);
334 if (first.IsRegister() || first.IsFpuRegister()) {
335 current->SetFrom(position + 1);
336 current->SetRegister(first.reg());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000337 } else if (first.IsPair()) {
338 current->SetFrom(position + 1);
339 current->SetRegister(first.low());
340 LiveInterval* high = current->GetHighInterval();
341 high->SetRegister(first.high());
342 high->SetFrom(position + 1);
Calin Juravled0d48522014-11-04 16:40:20 +0000343 }
344 } else if (output.IsRegister() || output.IsFpuRegister()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100345 // Shift the interval's start by one to account for the blocked register.
346 current->SetFrom(position + 1);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100347 current->SetRegister(output.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100348 BlockRegister(output, position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000349 } else if (output.IsPair()) {
350 current->SetFrom(position + 1);
351 current->SetRegister(output.low());
352 LiveInterval* high = current->GetHighInterval();
353 high->SetRegister(output.high());
354 high->SetFrom(position + 1);
355 BlockRegister(output.ToLow(), position, position + 1);
356 BlockRegister(output.ToHigh(), position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100357 } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
358 current->SetSpillSlot(output.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000359 } else {
360 DCHECK(output.IsUnallocated() || output.IsConstant());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100361 }
362
363 // If needed, add interval to the list of unhandled intervals.
364 if (current->HasSpillSlot() || instruction->IsConstant()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100365 // Split just before first register use.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100366 size_t first_register_use = current->FirstRegisterUse();
367 if (first_register_use != kNoLifetime) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100368 LiveInterval* split = Split(current, first_register_use - 1);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000369 // Don't add directly to `unhandled`, it needs to be sorted and the start
Nicolas Geoffray39468442014-09-02 15:17:15 +0100370 // of this new interval might be after intervals already in the list.
371 AddSorted(&unhandled, split);
372 } else {
373 // Nothing to do, we won't allocate a register for this value.
374 }
375 } else {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000376 // Don't add directly to `unhandled`, temp or safepoint intervals
377 // for this instruction may have been added, and those can be
378 // processed first.
379 AddSorted(&unhandled, current);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100380 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100381}
382
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100383class AllRangesIterator : public ValueObject {
384 public:
385 explicit AllRangesIterator(LiveInterval* interval)
386 : current_interval_(interval),
387 current_range_(interval->GetFirstRange()) {}
388
389 bool Done() const { return current_interval_ == nullptr; }
390 LiveRange* CurrentRange() const { return current_range_; }
391 LiveInterval* CurrentInterval() const { return current_interval_; }
392
393 void Advance() {
394 current_range_ = current_range_->GetNext();
395 if (current_range_ == nullptr) {
396 current_interval_ = current_interval_->GetNextSibling();
397 if (current_interval_ != nullptr) {
398 current_range_ = current_interval_->GetFirstRange();
399 }
400 }
401 }
402
403 private:
404 LiveInterval* current_interval_;
405 LiveRange* current_range_;
406
407 DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
408};
409
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100410bool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
411 // To simplify unit testing, we eagerly create the array of intervals, and
412 // call the helper method.
413 GrowableArray<LiveInterval*> intervals(allocator_, 0);
414 for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
415 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
416 if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
417 intervals.Add(instruction->GetLiveInterval());
418 }
419 }
420
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100421 if (processing_core_registers_) {
422 for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
423 LiveInterval* fixed = physical_core_register_intervals_.Get(i);
424 if (fixed != nullptr) {
425 intervals.Add(fixed);
426 }
427 }
428 } else {
429 for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
430 LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
431 if (fixed != nullptr) {
432 intervals.Add(fixed);
433 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100434 }
435 }
436
Nicolas Geoffray39468442014-09-02 15:17:15 +0100437 for (size_t i = 0, e = temp_intervals_.Size(); i < e; ++i) {
438 LiveInterval* temp = temp_intervals_.Get(i);
439 if (ShouldProcess(processing_core_registers_, temp)) {
440 intervals.Add(temp);
441 }
442 }
443
Nicolas Geoffray776b3182015-02-23 14:14:57 +0000444 return ValidateIntervals(intervals, GetNumberOfSpillSlots(), reserved_out_slots_, *codegen_,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100445 allocator_, processing_core_registers_, log_fatal_on_failure);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100446}
447
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100448bool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
449 size_t number_of_spill_slots,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100450 size_t number_of_out_slots,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100451 const CodeGenerator& codegen,
452 ArenaAllocator* allocator,
453 bool processing_core_registers,
454 bool log_fatal_on_failure) {
455 size_t number_of_registers = processing_core_registers
456 ? codegen.GetNumberOfCoreRegisters()
457 : codegen.GetNumberOfFloatingPointRegisters();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100458 GrowableArray<ArenaBitVector*> liveness_of_values(
459 allocator, number_of_registers + number_of_spill_slots);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100460
461 // Allocate a bit vector per register. A live interval that has a register
462 // allocated will populate the associated bit vector based on its live ranges.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100463 for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
464 liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100465 }
466
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100467 for (size_t i = 0, e = intervals.Size(); i < e; ++i) {
468 for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) {
469 LiveInterval* current = it.CurrentInterval();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100470 HInstruction* defined_by = current->GetParent()->GetDefinedBy();
471 if (current->GetParent()->HasSpillSlot()
472 // Parameters have their own stack slot.
473 && !(defined_by != nullptr && defined_by->IsParameterValue())) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100474 BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers
475 + current->GetParent()->GetSpillSlot() / kVRegSize
476 - number_of_out_slots);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100477 for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
478 if (liveness_of_spill_slot->IsBitSet(j)) {
479 if (log_fatal_on_failure) {
480 std::ostringstream message;
481 message << "Spill slot conflict at " << j;
482 LOG(FATAL) << message.str();
483 } else {
484 return false;
485 }
486 } else {
487 liveness_of_spill_slot->SetBit(j);
488 }
489 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100490 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100491
492 if (current->HasRegister()) {
493 BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister());
494 for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
495 if (liveness_of_register->IsBitSet(j)) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000496 if (current->IsUsingInputRegister() && current->CanUseInputRegister()) {
497 continue;
498 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100499 if (log_fatal_on_failure) {
500 std::ostringstream message;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100501 message << "Register conflict at " << j << " ";
502 if (defined_by != nullptr) {
503 message << "(" << defined_by->DebugName() << ")";
504 }
505 message << "for ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100506 if (processing_core_registers) {
507 codegen.DumpCoreRegister(message, current->GetRegister());
508 } else {
509 codegen.DumpFloatingPointRegister(message, current->GetRegister());
510 }
511 LOG(FATAL) << message.str();
512 } else {
513 return false;
514 }
515 } else {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100516 liveness_of_register->SetBit(j);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100517 }
518 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100519 }
520 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100521 }
522 return true;
523}
524
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100525void RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100526 interval->Dump(stream);
527 stream << ": ";
528 if (interval->HasRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100529 if (interval->IsFloatingPoint()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100530 codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100531 } else {
532 codegen_->DumpCoreRegister(stream, interval->GetRegister());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100533 }
534 } else {
535 stream << "spilled";
536 }
537 stream << std::endl;
538}
539
Mingyao Yang296bd602014-10-06 16:47:28 -0700540void RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
541 stream << "inactive: " << std::endl;
542 for (size_t i = 0; i < inactive_.Size(); i ++) {
543 DumpInterval(stream, inactive_.Get(i));
544 }
545 stream << "active: " << std::endl;
546 for (size_t i = 0; i < active_.Size(); i ++) {
547 DumpInterval(stream, active_.Get(i));
548 }
549 stream << "unhandled: " << std::endl;
550 auto unhandled = (unhandled_ != nullptr) ?
551 unhandled_ : &unhandled_core_intervals_;
552 for (size_t i = 0; i < unhandled->Size(); i ++) {
553 DumpInterval(stream, unhandled->Get(i));
554 }
555 stream << "handled: " << std::endl;
556 for (size_t i = 0; i < handled_.Size(); i ++) {
557 DumpInterval(stream, handled_.Get(i));
558 }
559}
560
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100561// By the book implementation of a linear scan register allocator.
562void RegisterAllocator::LinearScan() {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100563 while (!unhandled_->IsEmpty()) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100564 // (1) Remove interval with the lowest start position from unhandled.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100565 LiveInterval* current = unhandled_->Pop();
566 DCHECK(!current->IsFixed() && !current->HasSpillSlot());
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100567 DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000568 DCHECK(!current->IsLowInterval() || unhandled_->Peek()->IsHighInterval());
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000569
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100570 size_t position = current->GetStart();
571
Mingyao Yang296bd602014-10-06 16:47:28 -0700572 // Remember the inactive_ size here since the ones moved to inactive_ from
573 // active_ below shouldn't need to be re-checked.
574 size_t inactive_intervals_to_handle = inactive_.Size();
575
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100576 // (2) Remove currently active intervals that are dead at this position.
577 // Move active intervals that have a lifetime hole at this position
578 // to inactive.
579 for (size_t i = 0; i < active_.Size(); ++i) {
580 LiveInterval* interval = active_.Get(i);
581 if (interval->IsDeadAt(position)) {
582 active_.Delete(interval);
583 --i;
584 handled_.Add(interval);
585 } else if (!interval->Covers(position)) {
586 active_.Delete(interval);
587 --i;
588 inactive_.Add(interval);
589 }
590 }
591
592 // (3) Remove currently inactive intervals that are dead at this position.
593 // Move inactive intervals that cover this position to active.
Mingyao Yang296bd602014-10-06 16:47:28 -0700594 for (size_t i = 0; i < inactive_intervals_to_handle; ++i) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100595 LiveInterval* interval = inactive_.Get(i);
Mingyao Yang296bd602014-10-06 16:47:28 -0700596 DCHECK(interval->GetStart() < position || interval->IsFixed());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100597 if (interval->IsDeadAt(position)) {
598 inactive_.Delete(interval);
599 --i;
Mingyao Yang296bd602014-10-06 16:47:28 -0700600 --inactive_intervals_to_handle;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100601 handled_.Add(interval);
602 } else if (interval->Covers(position)) {
603 inactive_.Delete(interval);
604 --i;
Mingyao Yang296bd602014-10-06 16:47:28 -0700605 --inactive_intervals_to_handle;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100606 active_.Add(interval);
607 }
608 }
609
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000610 if (current->IsSlowPathSafepoint()) {
611 // Synthesized interval to record the maximum number of live registers
612 // at safepoints. No need to allocate a register for it.
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500613 if (processing_core_registers_) {
614 maximum_number_of_live_core_registers_ =
615 std::max(maximum_number_of_live_core_registers_, active_.Size());
616 } else {
617 maximum_number_of_live_fp_registers_ =
618 std::max(maximum_number_of_live_fp_registers_, active_.Size());
619 }
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000620 DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() > current->GetStart());
621 continue;
622 }
623
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000624 if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
625 DCHECK(!current->HasRegister());
626 // Allocating the low part was unsucessful. The splitted interval for the high part
627 // will be handled next (it is in the `unhandled_` list).
628 continue;
629 }
630
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100631 // (4) Try to find an available register.
632 bool success = TryAllocateFreeReg(current);
633
634 // (5) If no register could be found, we need to spill.
635 if (!success) {
636 success = AllocateBlockedReg(current);
637 }
638
639 // (6) If the interval had a register allocated, add it to the list of active
640 // intervals.
641 if (success) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000642 codegen_->AddAllocatedRegister(processing_core_registers_
643 ? Location::RegisterLocation(current->GetRegister())
644 : Location::FpuRegisterLocation(current->GetRegister()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100645 active_.Add(current);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000646 if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
647 current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
648 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100649 }
650 }
651}
652
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000653static void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) {
654 DCHECK(!interval->IsHighInterval());
655 // Note that the same instruction may occur multiple times in the input list,
656 // so `free_until` may have changed already.
657 if (interval->IsDeadAt(position)) {
658 // Set the register to be free. Note that inactive intervals might later
659 // update this.
660 free_until[interval->GetRegister()] = kMaxLifetimePosition;
661 if (interval->HasHighInterval()) {
662 DCHECK(interval->GetHighInterval()->IsDeadAt(position));
663 free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition;
664 }
665 } else if (!interval->Covers(position)) {
666 // The interval becomes inactive at `defined_by`. We make its register
667 // available only until the next use strictly after `defined_by`.
668 free_until[interval->GetRegister()] = interval->FirstUseAfter(position);
669 if (interval->HasHighInterval()) {
670 DCHECK(!interval->GetHighInterval()->Covers(position));
671 free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()];
672 }
673 }
674}
675
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100676// Find a free register. If multiple are found, pick the register that
677// is free the longest.
678bool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
679 size_t* free_until = registers_array_;
680
681 // First set all registers to be free.
682 for (size_t i = 0; i < number_of_registers_; ++i) {
683 free_until[i] = kMaxLifetimePosition;
684 }
685
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100686 // For each active interval, set its register to not free.
687 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
688 LiveInterval* interval = active_.Get(i);
689 DCHECK(interval->HasRegister());
690 free_until[interval->GetRegister()] = 0;
691 }
692
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000693 // An interval that starts an instruction (that is, it is not split), may
694 // re-use the registers used by the inputs of that instruciton, based on the
695 // location summary.
696 HInstruction* defined_by = current->GetDefinedBy();
697 if (defined_by != nullptr && !current->IsSplit()) {
698 LocationSummary* locations = defined_by->GetLocations();
699 if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) {
700 for (HInputIterator it(defined_by); !it.Done(); it.Advance()) {
701 // Take the last interval of the input. It is the location of that interval
702 // that will be used at `defined_by`.
703 LiveInterval* interval = it.Current()->GetLiveInterval()->GetLastSibling();
704 // Note that interval may have not been processed yet.
705 // TODO: Handle non-split intervals last in the work list.
706 if (interval->HasRegister() && interval->SameRegisterKind(*current)) {
707 // The input must be live until the end of `defined_by`, to comply to
708 // the linear scan algorithm. So we use `defined_by`'s end lifetime
709 // position to check whether the input is dead or is inactive after
710 // `defined_by`.
711 DCHECK(interval->Covers(defined_by->GetLifetimePosition()));
712 size_t position = defined_by->GetLifetimePosition() + 1;
713 FreeIfNotCoverAt(interval, position, free_until);
714 }
715 }
716 }
717 }
718
Mingyao Yang296bd602014-10-06 16:47:28 -0700719 // For each inactive interval, set its register to be free until
720 // the next intersection with `current`.
721 for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
722 LiveInterval* inactive = inactive_.Get(i);
723 // Temp/Slow-path-safepoint interval has no holes.
724 DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
725 if (!current->IsSplit() && !inactive->IsFixed()) {
726 // Neither current nor inactive are fixed.
727 // Thanks to SSA, a non-split interval starting in a hole of an
728 // inactive interval should never intersect with that inactive interval.
729 // Only if it's not fixed though, because fixed intervals don't come from SSA.
730 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
731 continue;
732 }
733
734 DCHECK(inactive->HasRegister());
735 if (free_until[inactive->GetRegister()] == 0) {
736 // Already used by some active interval. No need to intersect.
737 continue;
738 }
739 size_t next_intersection = inactive->FirstIntersectionWith(current);
740 if (next_intersection != kNoLifetime) {
741 free_until[inactive->GetRegister()] =
742 std::min(free_until[inactive->GetRegister()], next_intersection);
743 }
744 }
745
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000746 int reg = kNoRegister;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100747 if (current->HasRegister()) {
748 // Some instructions have a fixed register output.
749 reg = current->GetRegister();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000750 if (free_until[reg] == 0) {
751 DCHECK(current->IsHighInterval());
752 // AllocateBlockedReg will spill the holder of the register.
753 return false;
754 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100755 } else {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000756 DCHECK(!current->IsHighInterval());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100757 int hint = current->FindFirstRegisterHint(free_until);
758 if (hint != kNoRegister) {
759 DCHECK(!IsBlocked(hint));
760 reg = hint;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000761 } else if (current->IsLowInterval()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000762 reg = FindAvailableRegisterPair(free_until, current->GetStart());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100763 } else {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000764 reg = FindAvailableRegister(free_until);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100765 }
766 }
767
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000768 DCHECK_NE(reg, kNoRegister);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100769 // If we could not find a register, we need to spill.
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000770 if (free_until[reg] == 0) {
771 return false;
772 }
773
774 if (current->IsLowInterval() && free_until[GetHighForLowRegister(reg)] == 0) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100775 return false;
776 }
777
778 current->SetRegister(reg);
779 if (!current->IsDeadAt(free_until[reg])) {
780 // If the register is only available for a subset of live ranges
781 // covered by `current`, split `current` at the position where
782 // the register is not available anymore.
783 LiveInterval* split = Split(current, free_until[reg]);
784 DCHECK(split != nullptr);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100785 AddSorted(unhandled_, split);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100786 }
787 return true;
788}
789
790bool RegisterAllocator::IsBlocked(int reg) const {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100791 return processing_core_registers_
792 ? blocked_core_registers_[reg]
793 : blocked_fp_registers_[reg];
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100794}
795
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000796int RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const {
797 int reg = kNoRegister;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000798 // Pick the register pair that is used the last.
799 for (size_t i = 0; i < number_of_registers_; ++i) {
800 if (IsBlocked(i)) continue;
801 if (!IsLowRegister(i)) continue;
802 int high_register = GetHighForLowRegister(i);
803 if (IsBlocked(high_register)) continue;
804 int existing_high_register = GetHighForLowRegister(reg);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000805 if ((reg == kNoRegister) || (next_use[i] >= next_use[reg]
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000806 && next_use[high_register] >= next_use[existing_high_register])) {
807 reg = i;
808 if (next_use[i] == kMaxLifetimePosition
809 && next_use[high_register] == kMaxLifetimePosition) {
810 break;
811 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000812 } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) {
813 // If one of the current register is known to be unavailable, just unconditionally
814 // try a new one.
815 reg = i;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000816 }
817 }
818 return reg;
819}
820
821int RegisterAllocator::FindAvailableRegister(size_t* next_use) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000822 int reg = kNoRegister;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000823 // Pick the register that is used the last.
824 for (size_t i = 0; i < number_of_registers_; ++i) {
825 if (IsBlocked(i)) continue;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000826 if (reg == kNoRegister || next_use[i] > next_use[reg]) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000827 reg = i;
828 if (next_use[i] == kMaxLifetimePosition) break;
829 }
830 }
831 return reg;
832}
833
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000834bool RegisterAllocator::TrySplitNonPairIntervalAt(size_t position,
835 size_t first_register_use,
836 size_t* next_use) {
837 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
838 LiveInterval* active = active_.Get(i);
839 DCHECK(active->HasRegister());
840 // Split the first interval found.
841 if (first_register_use <= next_use[active->GetRegister()]
842 && !active->IsLowInterval()
843 && !active->IsHighInterval()) {
844 LiveInterval* split = Split(active, position);
845 active_.DeleteAt(i);
846 if (split != active) {
847 handled_.Add(active);
848 }
849 AddSorted(unhandled_, split);
850 return true;
851 }
852 }
853 return false;
854}
855
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100856// Find the register that is used the last, and spill the interval
857// that holds it. If the first use of `current` is after that register
858// we spill `current` instead.
859bool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
860 size_t first_register_use = current->FirstRegisterUse();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100861 if (first_register_use == kNoLifetime) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100862 AllocateSpillSlotFor(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100863 return false;
864 }
865
866 // First set all registers as not being used.
867 size_t* next_use = registers_array_;
868 for (size_t i = 0; i < number_of_registers_; ++i) {
869 next_use[i] = kMaxLifetimePosition;
870 }
871
872 // For each active interval, find the next use of its register after the
873 // start of current.
874 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
875 LiveInterval* active = active_.Get(i);
876 DCHECK(active->HasRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100877 if (active->IsFixed()) {
878 next_use[active->GetRegister()] = current->GetStart();
879 } else {
880 size_t use = active->FirstRegisterUseAfter(current->GetStart());
881 if (use != kNoLifetime) {
882 next_use[active->GetRegister()] = use;
883 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100884 }
885 }
886
887 // For each inactive interval, find the next use of its register after the
888 // start of current.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100889 for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
890 LiveInterval* inactive = inactive_.Get(i);
Mingyao Yang296bd602014-10-06 16:47:28 -0700891 // Temp/Slow-path-safepoint interval has no holes.
892 DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
893 if (!current->IsSplit() && !inactive->IsFixed()) {
894 // Neither current nor inactive are fixed.
895 // Thanks to SSA, a non-split interval starting in a hole of an
896 // inactive interval should never intersect with that inactive interval.
897 // Only if it's not fixed though, because fixed intervals don't come from SSA.
898 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
899 continue;
900 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100901 DCHECK(inactive->HasRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100902 size_t next_intersection = inactive->FirstIntersectionWith(current);
903 if (next_intersection != kNoLifetime) {
904 if (inactive->IsFixed()) {
905 next_use[inactive->GetRegister()] =
906 std::min(next_intersection, next_use[inactive->GetRegister()]);
907 } else {
908 size_t use = inactive->FirstRegisterUseAfter(current->GetStart());
909 if (use != kNoLifetime) {
910 next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
911 }
912 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100913 }
914 }
915
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000916 int reg = kNoRegister;
917 bool should_spill = false;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000918 if (current->HasRegister()) {
919 DCHECK(current->IsHighInterval());
920 reg = current->GetRegister();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000921 // When allocating the low part, we made sure the high register was available.
922 DCHECK_LT(first_register_use, next_use[reg]);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000923 } else if (current->IsLowInterval()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000924 reg = FindAvailableRegisterPair(next_use, current->GetStart());
925 // We should spill if both registers are not available.
926 should_spill = (first_register_use >= next_use[reg])
927 || (first_register_use >= next_use[GetHighForLowRegister(reg)]);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000928 } else {
929 DCHECK(!current->IsHighInterval());
930 reg = FindAvailableRegister(next_use);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000931 should_spill = (first_register_use >= next_use[reg]);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100932 }
933
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000934 DCHECK_NE(reg, kNoRegister);
935 if (should_spill) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000936 DCHECK(!current->IsHighInterval());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000937 bool is_allocation_at_use_site = (current->GetStart() == (first_register_use - 1));
938 if (current->IsLowInterval()
939 && is_allocation_at_use_site
940 && TrySplitNonPairIntervalAt(current->GetStart(), first_register_use, next_use)) {
941 // If we're allocating a register for `current` because the instruction at
942 // that position requires it, but we think we should spill, then there are
943 // non-pair intervals blocking the allocation. We split the first
944 // interval found, and put ourselves first in the `unhandled_` list.
945 LiveInterval* existing = unhandled_->Peek();
946 DCHECK(existing->IsHighInterval());
947 DCHECK_EQ(existing->GetLowInterval(), current);
948 unhandled_->Add(current);
949 } else {
950 // If the first use of that instruction is after the last use of the found
951 // register, we split this interval just before its first register use.
952 AllocateSpillSlotFor(current);
953 LiveInterval* split = Split(current, first_register_use - 1);
954 DCHECK_NE(current, split) << "There is not enough registers available for "
955 << split->GetParent()->GetDefinedBy()->DebugName() << " "
956 << split->GetParent()->GetDefinedBy()->GetId()
957 << " at " << first_register_use - 1;
958 AddSorted(unhandled_, split);
959 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100960 return false;
961 } else {
962 // Use this register and spill the active and inactives interval that
963 // have that register.
964 current->SetRegister(reg);
965
966 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
967 LiveInterval* active = active_.Get(i);
968 if (active->GetRegister() == reg) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100969 DCHECK(!active->IsFixed());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100970 LiveInterval* split = Split(active, current->GetStart());
971 active_.DeleteAt(i);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000972 if (split != active) {
973 handled_.Add(active);
974 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100975 AddSorted(unhandled_, split);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000976
977 if (active->IsLowInterval() || active->IsHighInterval()) {
978 LiveInterval* other_half = active->IsLowInterval()
979 ? active->GetHighInterval()
980 : active->GetLowInterval();
981 // We also need to remove the other half from the list of actives.
982 bool found = false;
983 for (size_t j = 0; j < active_.Size(); ++j) {
984 if (active_.Get(j) == other_half) {
985 found = true;
986 active_.DeleteAt(j);
987 handled_.Add(other_half);
988 break;
989 }
990 }
991 DCHECK(found);
992 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100993 break;
994 }
995 }
996
Mingyao Yang296bd602014-10-06 16:47:28 -0700997 for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100998 LiveInterval* inactive = inactive_.Get(i);
999 if (inactive->GetRegister() == reg) {
Mingyao Yang296bd602014-10-06 16:47:28 -07001000 if (!current->IsSplit() && !inactive->IsFixed()) {
1001 // Neither current nor inactive are fixed.
1002 // Thanks to SSA, a non-split interval starting in a hole of an
1003 // inactive interval should never intersect with that inactive interval.
1004 // Only if it's not fixed though, because fixed intervals don't come from SSA.
1005 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
1006 continue;
1007 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001008 size_t next_intersection = inactive->FirstIntersectionWith(current);
1009 if (next_intersection != kNoLifetime) {
1010 if (inactive->IsFixed()) {
1011 LiveInterval* split = Split(current, next_intersection);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001012 DCHECK_NE(split, current);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001013 AddSorted(unhandled_, split);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001014 } else {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001015 // Split at the start of `current`, which will lead to splitting
1016 // at the end of the lifetime hole of `inactive`.
1017 LiveInterval* split = Split(inactive, current->GetStart());
1018 // If it's inactive, it must start before the current interval.
1019 DCHECK_NE(split, inactive);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001020 inactive_.DeleteAt(i);
Mingyao Yang296bd602014-10-06 16:47:28 -07001021 --i;
1022 --e;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001023 handled_.Add(inactive);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001024 AddSorted(unhandled_, split);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001025
1026 if (inactive->IsLowInterval() || inactive->IsHighInterval()) {
1027 LiveInterval* other_half = inactive->IsLowInterval()
1028 ? inactive->GetHighInterval()
1029 : inactive->GetLowInterval();
1030
1031 // We also need to remove the other half from the list of inactives.
1032 bool found = false;
1033 for (size_t j = 0; j < inactive_.Size(); ++j) {
1034 if (inactive_.Get(j) == other_half) {
1035 found = true;
1036 inactive_.DeleteAt(j);
1037 --e;
1038 handled_.Add(other_half);
1039 break;
1040 }
1041 }
1042 DCHECK(found);
1043 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001044 }
1045 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001046 }
1047 }
1048
1049 return true;
1050 }
1051}
1052
Nicolas Geoffray39468442014-09-02 15:17:15 +01001053void RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01001054 DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001055 size_t insert_at = 0;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001056 for (size_t i = array->Size(); i > 0; --i) {
1057 LiveInterval* current = array->Get(i - 1);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001058 // High intervals must be processed right after their low equivalent.
1059 if (current->StartsAfter(interval) && !current->IsHighInterval()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001060 insert_at = i;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001061 break;
Nicolas Geoffrayacd03392014-11-26 15:46:52 +00001062 } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
1063 // Ensure the slow path interval is the last to be processed at its location: we want the
1064 // interval to know all live registers at this location.
1065 DCHECK(i == 1 || array->Get(i - 2)->StartsAfter(current));
1066 insert_at = i;
1067 break;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001068 }
1069 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001070
Nicolas Geoffray39468442014-09-02 15:17:15 +01001071 array->InsertAt(insert_at, interval);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001072 // Insert the high interval before the low, to ensure the low is processed before.
1073 if (interval->HasHighInterval()) {
1074 array->InsertAt(insert_at, interval->GetHighInterval());
1075 } else if (interval->HasLowInterval()) {
1076 array->InsertAt(insert_at + 1, interval->GetLowInterval());
1077 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001078}
1079
1080LiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001081 DCHECK_GE(position, interval->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001082 DCHECK(!interval->IsDeadAt(position));
1083 if (position == interval->GetStart()) {
1084 // Spill slot will be allocated when handling `interval` again.
1085 interval->ClearRegister();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001086 if (interval->HasHighInterval()) {
1087 interval->GetHighInterval()->ClearRegister();
1088 } else if (interval->HasLowInterval()) {
1089 interval->GetLowInterval()->ClearRegister();
1090 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001091 return interval;
1092 } else {
1093 LiveInterval* new_interval = interval->SplitAt(position);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001094 if (interval->HasHighInterval()) {
1095 LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
1096 new_interval->SetHighInterval(high);
1097 high->SetLowInterval(new_interval);
1098 } else if (interval->HasLowInterval()) {
1099 LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
1100 new_interval->SetLowInterval(low);
1101 low->SetHighInterval(new_interval);
1102 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001103 return new_interval;
1104 }
1105}
1106
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001107void RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001108 if (interval->IsHighInterval()) {
1109 // The low interval will contain the spill slot.
1110 return;
1111 }
1112
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001113 LiveInterval* parent = interval->GetParent();
1114
1115 // An instruction gets a spill slot for its entire lifetime. If the parent
1116 // of this interval already has a spill slot, there is nothing to do.
1117 if (parent->HasSpillSlot()) {
1118 return;
1119 }
1120
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001121 HInstruction* defined_by = parent->GetDefinedBy();
1122 if (defined_by->IsParameterValue()) {
1123 // Parameters have their own stack slot.
1124 parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
1125 return;
1126 }
1127
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001128 if (defined_by->IsConstant()) {
1129 // Constants don't need a spill slot.
1130 return;
1131 }
1132
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001133 LiveInterval* last_sibling = interval;
1134 while (last_sibling->GetNextSibling() != nullptr) {
1135 last_sibling = last_sibling->GetNextSibling();
1136 }
1137 size_t end = last_sibling->GetEnd();
1138
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001139 GrowableArray<size_t>* spill_slots = nullptr;
1140 switch (interval->GetType()) {
1141 case Primitive::kPrimDouble:
1142 spill_slots = &double_spill_slots_;
1143 break;
1144 case Primitive::kPrimLong:
1145 spill_slots = &long_spill_slots_;
1146 break;
1147 case Primitive::kPrimFloat:
1148 spill_slots = &float_spill_slots_;
1149 break;
1150 case Primitive::kPrimNot:
1151 case Primitive::kPrimInt:
1152 case Primitive::kPrimChar:
1153 case Primitive::kPrimByte:
1154 case Primitive::kPrimBoolean:
1155 case Primitive::kPrimShort:
1156 spill_slots = &int_spill_slots_;
1157 break;
1158 case Primitive::kPrimVoid:
1159 LOG(FATAL) << "Unexpected type for interval " << interval->GetType();
1160 }
1161
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001162 // Find an available spill slot.
1163 size_t slot = 0;
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001164 for (size_t e = spill_slots->Size(); slot < e; ++slot) {
1165 if (spill_slots->Get(slot) <= parent->GetStart()
1166 && (slot == (e - 1) || spill_slots->Get(slot + 1) <= parent->GetStart())) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001167 break;
1168 }
1169 }
1170
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001171 if (parent->NeedsTwoSpillSlots()) {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001172 if (slot == spill_slots->Size()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001173 // We need a new spill slot.
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001174 spill_slots->Add(end);
1175 spill_slots->Add(end);
1176 } else if (slot == spill_slots->Size() - 1) {
1177 spill_slots->Put(slot, end);
1178 spill_slots->Add(end);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001179 } else {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001180 spill_slots->Put(slot, end);
1181 spill_slots->Put(slot + 1, end);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001182 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001183 } else {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001184 if (slot == spill_slots->Size()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001185 // We need a new spill slot.
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001186 spill_slots->Add(end);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001187 } else {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001188 spill_slots->Put(slot, end);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001189 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001190 }
1191
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001192 // Note that the exact spill slot location will be computed when we resolve,
1193 // that is when we know the number of spill slots for each type.
1194 parent->SetSpillSlot(slot);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001195}
1196
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001197static bool IsValidDestination(Location destination) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001198 return destination.IsRegister()
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001199 || destination.IsRegisterPair()
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001200 || destination.IsFpuRegister()
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001201 || destination.IsFpuRegisterPair()
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001202 || destination.IsStackSlot()
1203 || destination.IsDoubleStackSlot();
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001204}
1205
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001206void RegisterAllocator::AddInputMoveFor(HInstruction* user,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001207 Location source,
1208 Location destination) const {
1209 if (source.Equals(destination)) return;
1210
Roland Levillain476df552014-10-09 17:51:36 +01001211 DCHECK(!user->IsPhi());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001212
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001213 HInstruction* previous = user->GetPrevious();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001214 HParallelMove* move = nullptr;
1215 if (previous == nullptr
Roland Levillain476df552014-10-09 17:51:36 +01001216 || !previous->IsParallelMove()
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001217 || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001218 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001219 move->SetLifetimePosition(user->GetLifetimePosition());
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001220 user->GetBlock()->InsertInstructionBefore(move, user);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001221 } else {
1222 move = previous->AsParallelMove();
1223 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001224 DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00001225 move->AddMove(source, destination, nullptr);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001226}
1227
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001228static bool IsInstructionStart(size_t position) {
1229 return (position & 1) == 0;
1230}
1231
1232static bool IsInstructionEnd(size_t position) {
1233 return (position & 1) == 1;
1234}
1235
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001236void RegisterAllocator::InsertParallelMoveAt(size_t position,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001237 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001238 Location source,
1239 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001240 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001241 if (source.Equals(destination)) return;
1242
1243 HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001244 HParallelMove* move;
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001245 if (at == nullptr) {
1246 if (IsInstructionStart(position)) {
1247 // Block boundary, don't do anything the connection of split siblings will handle it.
1248 return;
1249 } else {
1250 // Move must happen before the first instruction of the block.
1251 at = liveness_.GetInstructionFromPosition((position + 1) / 2);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001252 // Note that parallel moves may have already been inserted, so we explicitly
1253 // ask for the first instruction of the block: `GetInstructionFromPosition` does
1254 // not contain the moves.
1255 at = at->GetBlock()->GetFirstInstruction();
1256 if (at->GetLifetimePosition() != position) {
1257 DCHECK_GT(at->GetLifetimePosition(), position);
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001258 move = new (allocator_) HParallelMove(allocator_);
1259 move->SetLifetimePosition(position);
1260 at->GetBlock()->InsertInstructionBefore(move, at);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001261 } else {
1262 DCHECK(at->IsParallelMove());
1263 move = at->AsParallelMove();
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001264 }
1265 }
1266 } else if (IsInstructionEnd(position)) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001267 // Move must happen after the instruction.
1268 DCHECK(!at->IsControlFlow());
1269 move = at->GetNext()->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001270 // This is a parallel move for connecting siblings in a same block. We need to
1271 // differentiate it with moves for connecting blocks, and input moves.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001272 if (move == nullptr || move->GetLifetimePosition() > position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001273 move = new (allocator_) HParallelMove(allocator_);
1274 move->SetLifetimePosition(position);
1275 at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
1276 }
1277 } else {
1278 // Move must happen before the instruction.
1279 HInstruction* previous = at->GetPrevious();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001280 if (previous == nullptr
1281 || !previous->IsParallelMove()
1282 || previous->GetLifetimePosition() != position) {
1283 // If the previous is a parallel move, then its position must be lower
1284 // than the given `position`: it was added just after the non-parallel
1285 // move instruction that precedes `instruction`.
1286 DCHECK(previous == nullptr
1287 || !previous->IsParallelMove()
1288 || previous->GetLifetimePosition() < position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001289 move = new (allocator_) HParallelMove(allocator_);
1290 move->SetLifetimePosition(position);
1291 at->GetBlock()->InsertInstructionBefore(move, at);
1292 } else {
1293 move = previous->AsParallelMove();
1294 }
1295 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001296 DCHECK_EQ(move->GetLifetimePosition(), position);
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00001297 move->AddMove(source, destination, instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001298}
1299
1300void RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001301 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001302 Location source,
1303 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001304 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001305 if (source.Equals(destination)) return;
1306
1307 DCHECK_EQ(block->GetSuccessors().Size(), 1u);
1308 HInstruction* last = block->GetLastInstruction();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001309 // We insert moves at exit for phi predecessors and connecting blocks.
1310 // A block ending with an if cannot branch to a block with phis because
1311 // we do not allow critical edges. It can also not connect
1312 // a split interval between two blocks: the move has to happen in the successor.
1313 DCHECK(!last->IsIf());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001314 HInstruction* previous = last->GetPrevious();
1315 HParallelMove* move;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001316 // This is a parallel move for connecting blocks. We need to differentiate
1317 // it with moves for connecting siblings in a same block, and output moves.
Nicolas Geoffray59768572014-12-01 09:50:04 +00001318 size_t position = last->GetLifetimePosition();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001319 if (previous == nullptr || !previous->IsParallelMove()
Nicolas Geoffray59768572014-12-01 09:50:04 +00001320 || previous->AsParallelMove()->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001321 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001322 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001323 block->InsertInstructionBefore(move, last);
1324 } else {
1325 move = previous->AsParallelMove();
1326 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00001327 move->AddMove(source, destination, instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001328}
1329
1330void RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001331 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001332 Location source,
1333 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001334 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001335 if (source.Equals(destination)) return;
1336
1337 HInstruction* first = block->GetFirstInstruction();
1338 HParallelMove* move = first->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001339 // This is a parallel move for connecting blocks. We need to differentiate
1340 // it with moves for connecting siblings in a same block, and input moves.
1341 if (move == nullptr || move->GetLifetimePosition() != block->GetLifetimeStart()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001342 move = new (allocator_) HParallelMove(allocator_);
1343 move->SetLifetimePosition(block->GetLifetimeStart());
1344 block->InsertInstructionBefore(move, first);
1345 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00001346 move->AddMove(source, destination, instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001347}
1348
1349void RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
1350 Location source,
1351 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001352 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001353 if (source.Equals(destination)) return;
1354
Roland Levillain476df552014-10-09 17:51:36 +01001355 if (instruction->IsPhi()) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001356 InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001357 return;
1358 }
1359
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001360 size_t position = instruction->GetLifetimePosition() + 1;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001361 HParallelMove* move = instruction->GetNext()->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001362 // This is a parallel move for moving the output of an instruction. We need
1363 // to differentiate with input moves, moves for connecting siblings in a
1364 // and moves for connecting blocks.
1365 if (move == nullptr || move->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001366 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001367 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001368 instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
1369 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00001370 move->AddMove(source, destination, instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001371}
1372
1373void RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
1374 LiveInterval* current = interval;
1375 if (current->HasSpillSlot() && current->HasRegister()) {
1376 // We spill eagerly, so move must be at definition.
1377 InsertMoveAfter(interval->GetDefinedBy(),
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001378 interval->ToLocation(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001379 interval->NeedsTwoSpillSlots()
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001380 ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1381 : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001382 }
1383 UsePosition* use = current->GetFirstUse();
1384
1385 // Walk over all siblings, updating locations of use positions, and
1386 // connecting them when they are adjacent.
1387 do {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001388 Location source = current->ToLocation();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001389
1390 // Walk over all uses covered by this interval, and update the location
1391 // information.
1392 while (use != nullptr && use->GetPosition() <= current->GetEnd()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001393 LocationSummary* locations = use->GetUser()->GetLocations();
1394 if (use->GetIsEnvironment()) {
1395 locations->SetEnvironmentAt(use->GetInputIndex(), source);
1396 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001397 Location expected_location = locations->InAt(use->GetInputIndex());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001398 // The expected (actual) location may be invalid in case the input is unused. Currently
1399 // this only happens for intrinsics.
1400 if (expected_location.IsValid()) {
1401 if (expected_location.IsUnallocated()) {
1402 locations->SetInAt(use->GetInputIndex(), source);
1403 } else if (!expected_location.IsConstant()) {
1404 AddInputMoveFor(use->GetUser(), source, expected_location);
1405 }
1406 } else {
1407 DCHECK(use->GetUser()->IsInvoke());
1408 DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001409 }
1410 }
1411 use = use->GetNext();
1412 }
1413
1414 // If the next interval starts just after this one, and has a register,
1415 // insert a move.
1416 LiveInterval* next_sibling = current->GetNextSibling();
1417 if (next_sibling != nullptr
1418 && next_sibling->HasRegister()
1419 && current->GetEnd() == next_sibling->GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001420 Location destination = next_sibling->ToLocation();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001421 InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001422 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001423
1424 // At each safepoint, we record stack and register information.
1425 for (size_t i = 0, e = safepoints_.Size(); i < e; ++i) {
1426 HInstruction* safepoint = safepoints_.Get(i);
1427 size_t position = safepoint->GetLifetimePosition();
1428 LocationSummary* locations = safepoint->GetLocations();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001429 if (!current->Covers(position)) {
1430 continue;
1431 }
1432 if (interval->GetStart() == position) {
1433 // The safepoint is for this instruction, so the location of the instruction
1434 // does not need to be saved.
1435 continue;
1436 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001437
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001438 if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001439 locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
1440 }
1441
1442 switch (source.GetKind()) {
1443 case Location::kRegister: {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001444 locations->AddLiveRegister(source);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001445 if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) {
1446 DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1447 maximum_number_of_live_core_registers_ +
1448 maximum_number_of_live_fp_registers_);
1449 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001450 if (current->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001451 locations->SetRegisterBit(source.reg());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001452 }
1453 break;
1454 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001455 case Location::kFpuRegister: {
1456 locations->AddLiveRegister(source);
1457 break;
1458 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001459
1460 case Location::kRegisterPair:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001461 case Location::kFpuRegisterPair: {
1462 locations->AddLiveRegister(source.ToLow());
1463 locations->AddLiveRegister(source.ToHigh());
1464 break;
1465 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001466 case Location::kStackSlot: // Fall-through
1467 case Location::kDoubleStackSlot: // Fall-through
1468 case Location::kConstant: {
1469 // Nothing to do.
1470 break;
1471 }
1472 default: {
1473 LOG(FATAL) << "Unexpected location for object";
1474 }
1475 }
1476 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001477 current = next_sibling;
1478 } while (current != nullptr);
1479 DCHECK(use == nullptr);
1480}
1481
1482void RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
1483 HBasicBlock* from,
1484 HBasicBlock* to) const {
1485 if (interval->GetNextSibling() == nullptr) {
1486 // Nothing to connect. The whole range was allocated to the same location.
1487 return;
1488 }
1489
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001490 // Intervals end at the lifetime end of a block. The decrement by one
1491 // ensures the `Cover` call will return true.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001492 size_t from_position = from->GetLifetimeEnd() - 1;
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001493 size_t to_position = to->GetLifetimeStart();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001494
1495 LiveInterval* destination = nullptr;
1496 LiveInterval* source = nullptr;
1497
1498 LiveInterval* current = interval;
1499
1500 // Check the intervals that cover `from` and `to`.
1501 while ((current != nullptr) && (source == nullptr || destination == nullptr)) {
1502 if (current->Covers(from_position)) {
1503 DCHECK(source == nullptr);
1504 source = current;
1505 }
1506 if (current->Covers(to_position)) {
1507 DCHECK(destination == nullptr);
1508 destination = current;
1509 }
1510
1511 current = current->GetNextSibling();
1512 }
1513
1514 if (destination == source) {
1515 // Interval was not split.
1516 return;
1517 }
1518
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +01001519 DCHECK(destination != nullptr && source != nullptr);
1520
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001521 if (!destination->HasRegister()) {
1522 // Values are eagerly spilled. Spill slot already contains appropriate value.
1523 return;
1524 }
1525
1526 // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
1527 // we need to put the moves at the entry of `to`.
1528 if (from->GetSuccessors().Size() == 1) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001529 InsertParallelMoveAtExitOf(from,
1530 interval->GetParent()->GetDefinedBy(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001531 source->ToLocation(),
1532 destination->ToLocation());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001533 } else {
1534 DCHECK_EQ(to->GetPredecessors().Size(), 1u);
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001535 InsertParallelMoveAtEntryOf(to,
1536 interval->GetParent()->GetDefinedBy(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001537 source->ToLocation(),
1538 destination->ToLocation());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001539 }
1540}
1541
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001542void RegisterAllocator::Resolve() {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001543 codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(),
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +00001544 maximum_number_of_live_core_registers_,
1545 maximum_number_of_live_fp_registers_,
1546 reserved_out_slots_,
1547 liveness_.GetLinearOrder());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001548
1549 // Adjust the Out Location of instructions.
1550 // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
1551 for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
1552 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
1553 LiveInterval* current = instruction->GetLiveInterval();
1554 LocationSummary* locations = instruction->GetLocations();
1555 Location location = locations->Out();
Roland Levillain476df552014-10-09 17:51:36 +01001556 if (instruction->IsParameterValue()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001557 // Now that we know the frame size, adjust the parameter's location.
1558 if (location.IsStackSlot()) {
1559 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1560 current->SetSpillSlot(location.GetStackIndex());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001561 locations->UpdateOut(location);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001562 } else if (location.IsDoubleStackSlot()) {
1563 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1564 current->SetSpillSlot(location.GetStackIndex());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001565 locations->UpdateOut(location);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001566 } else if (current->HasSpillSlot()) {
1567 current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
1568 }
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001569 } else if (current->HasSpillSlot()) {
1570 // Adjust the stack slot, now that we know the number of them for each type.
1571 // The way this implementation lays out the stack is the following:
1572 // [parameter slots ]
1573 // [double spill slots ]
1574 // [long spill slots ]
1575 // [float spill slots ]
1576 // [int/ref values ]
1577 // [maximum out values ] (number of arguments for calls)
1578 // [art method ].
1579 uint32_t slot = current->GetSpillSlot();
1580 switch (current->GetType()) {
1581 case Primitive::kPrimDouble:
1582 slot += long_spill_slots_.Size();
1583 FALLTHROUGH_INTENDED;
1584 case Primitive::kPrimLong:
1585 slot += float_spill_slots_.Size();
1586 FALLTHROUGH_INTENDED;
1587 case Primitive::kPrimFloat:
1588 slot += int_spill_slots_.Size();
1589 FALLTHROUGH_INTENDED;
1590 case Primitive::kPrimNot:
1591 case Primitive::kPrimInt:
1592 case Primitive::kPrimChar:
1593 case Primitive::kPrimByte:
1594 case Primitive::kPrimBoolean:
1595 case Primitive::kPrimShort:
1596 slot += reserved_out_slots_;
1597 break;
1598 case Primitive::kPrimVoid:
1599 LOG(FATAL) << "Unexpected type for interval " << current->GetType();
1600 }
1601 current->SetSpillSlot(slot * kVRegSize);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001602 }
1603
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001604 Location source = current->ToLocation();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001605
1606 if (location.IsUnallocated()) {
1607 if (location.GetPolicy() == Location::kSameAsFirstInput) {
Calin Juravled0d48522014-11-04 16:40:20 +00001608 if (locations->InAt(0).IsUnallocated()) {
1609 locations->SetInAt(0, source);
1610 } else {
1611 DCHECK(locations->InAt(0).Equals(source));
1612 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001613 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001614 locations->UpdateOut(source);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001615 } else {
1616 DCHECK(source.Equals(location));
1617 }
1618 }
1619
1620 // Connect siblings.
1621 for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
1622 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
1623 ConnectSiblings(instruction->GetLiveInterval());
1624 }
1625
1626 // Resolve non-linear control flow across branches. Order does not matter.
1627 for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
1628 HBasicBlock* block = it.Current();
1629 BitVector* live = liveness_.GetLiveInSet(*block);
1630 for (uint32_t idx : live->Indexes()) {
1631 HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
1632 LiveInterval* interval = current->GetLiveInterval();
1633 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
1634 ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
1635 }
1636 }
1637 }
1638
1639 // Resolve phi inputs. Order does not matter.
1640 for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
1641 HBasicBlock* current = it.Current();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001642 for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1643 HInstruction* phi = inst_it.Current();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001644 for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
1645 HBasicBlock* predecessor = current->GetPredecessors().Get(i);
1646 DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
1647 HInstruction* input = phi->InputAt(i);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001648 Location source = input->GetLiveInterval()->GetLocationAt(
1649 predecessor->GetLifetimeEnd() - 1);
1650 Location destination = phi->GetLiveInterval()->ToLocation();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001651 InsertParallelMoveAtExitOf(predecessor, nullptr, source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001652 }
1653 }
1654 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001655
1656 // Assign temp locations.
1657 HInstruction* current = nullptr;
1658 size_t temp_index = 0;
1659 for (size_t i = 0; i < temp_intervals_.Size(); ++i) {
1660 LiveInterval* temp = temp_intervals_.Get(i);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001661 if (temp->IsHighInterval()) {
1662 // High intervals can be skipped, they are already handled by the low interval.
1663 continue;
1664 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001665 HInstruction* at = liveness_.GetTempUser(temp);
1666 if (at != current) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001667 temp_index = 0;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001668 current = at;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001669 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001670 LocationSummary* locations = at->GetLocations();
Roland Levillain5368c212014-11-27 15:03:41 +00001671 switch (temp->GetType()) {
1672 case Primitive::kPrimInt:
1673 locations->SetTempAt(
1674 temp_index++, Location::RegisterLocation(temp->GetRegister()));
1675 break;
1676
1677 case Primitive::kPrimDouble:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001678 if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1679 Location location = Location::FpuRegisterPairLocation(
1680 temp->GetRegister(), temp->GetHighInterval()->GetRegister());
1681 locations->SetTempAt(temp_index++, location);
1682 } else {
1683 locations->SetTempAt(
1684 temp_index++, Location::FpuRegisterLocation(temp->GetRegister()));
1685 }
Roland Levillain5368c212014-11-27 15:03:41 +00001686 break;
1687
1688 default:
1689 LOG(FATAL) << "Unexpected type for temporary location "
1690 << temp->GetType();
1691 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001692 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001693}
1694
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001695} // namespace art