blob: 2fbd051c030e5a309add7abeb773e94d15608520 [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
Nicolas Geoffray234d69d2015-03-09 10:28:50 +000019#include <iostream>
Ian Rogersc7dd2952014-10-21 23:31:19 -070020#include <sstream>
21
Ian Rogerse77493c2014-08-20 15:08:45 -070022#include "base/bit_vector-inl.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010023#include "code_generator.h"
24#include "ssa_liveness_analysis.h"
25
26namespace art {
27
28static constexpr size_t kMaxLifetimePosition = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010029static constexpr size_t kDefaultNumberOfSpillSlots = 4;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010030
Nicolas Geoffray840e5462015-01-07 16:01:24 +000031// For simplicity, we implement register pairs as (reg, reg + 1).
32// Note that this is a requirement for double registers on ARM, since we
33// allocate SRegister.
34static int GetHighForLowRegister(int reg) { return reg + 1; }
35static bool IsLowRegister(int reg) { return (reg & 1) == 0; }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +000036static bool IsLowOfUnalignedPairInterval(LiveInterval* low) {
37 return GetHighForLowRegister(low->GetRegister()) != low->GetHighInterval()->GetRegister();
38}
Nicolas Geoffray840e5462015-01-07 16:01:24 +000039
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010040RegisterAllocator::RegisterAllocator(ArenaAllocator* allocator,
41 CodeGenerator* codegen,
42 const SsaLivenessAnalysis& liveness)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010043 : allocator_(allocator),
44 codegen_(codegen),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010045 liveness_(liveness),
Nicolas Geoffray39468442014-09-02 15:17:15 +010046 unhandled_core_intervals_(allocator, 0),
47 unhandled_fp_intervals_(allocator, 0),
48 unhandled_(nullptr),
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010049 handled_(allocator, 0),
50 active_(allocator, 0),
51 inactive_(allocator, 0),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010052 physical_core_register_intervals_(allocator, codegen->GetNumberOfCoreRegisters()),
53 physical_fp_register_intervals_(allocator, codegen->GetNumberOfFloatingPointRegisters()),
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 temp_intervals_(allocator, 4),
Nicolas Geoffray776b3182015-02-23 14:14:57 +000055 int_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
56 long_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
57 float_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
58 double_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 safepoints_(allocator, 0),
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010060 processing_core_registers_(false),
61 number_of_registers_(-1),
62 registers_array_(nullptr),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010063 blocked_core_registers_(codegen->GetBlockedCoreRegisters()),
64 blocked_fp_registers_(codegen->GetBlockedFloatingPointRegisters()),
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +010065 reserved_out_slots_(0),
Mark Mendellf85a9ca2015-01-13 09:20:58 -050066 maximum_number_of_live_core_registers_(0),
67 maximum_number_of_live_fp_registers_(0) {
Nicolas Geoffray98893962015-01-21 12:32:32 +000068 static constexpr bool kIsBaseline = false;
69 codegen->SetupBlockedRegisters(kIsBaseline);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010070 physical_core_register_intervals_.SetSize(codegen->GetNumberOfCoreRegisters());
71 physical_fp_register_intervals_.SetSize(codegen->GetNumberOfFloatingPointRegisters());
Nicolas Geoffray39468442014-09-02 15:17:15 +010072 // Always reserve for the current method and the graph's max out registers.
73 // TODO: compute it instead.
74 reserved_out_slots_ = 1 + codegen->GetGraph()->GetMaximumNumberOfOutVRegs();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010075}
76
Nicolas Geoffray234d69d2015-03-09 10:28:50 +000077bool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph ATTRIBUTE_UNUSED,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010078 InstructionSet instruction_set) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +000079 return instruction_set == kArm64
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000080 || instruction_set == kX86_64
81 || instruction_set == kArm
Nicolas Geoffray234d69d2015-03-09 10:28:50 +000082 || instruction_set == kX86
83 || instruction_set == kThumb2;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010084}
85
86static bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) {
Nicolas Geoffray39468442014-09-02 15:17:15 +010087 if (interval == nullptr) return false;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010088 bool is_core_register = (interval->GetType() != Primitive::kPrimDouble)
89 && (interval->GetType() != Primitive::kPrimFloat);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010090 return processing_core_registers == is_core_register;
91}
92
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010093void RegisterAllocator::AllocateRegisters() {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010094 AllocateRegistersInternal();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010095 Resolve();
96
97 if (kIsDebugBuild) {
98 processing_core_registers_ = true;
99 ValidateInternal(true);
100 processing_core_registers_ = false;
101 ValidateInternal(true);
Nicolas Geoffray59768572014-12-01 09:50:04 +0000102 // Check that the linear order is still correct with regards to lifetime positions.
103 // Since only parallel moves have been inserted during the register allocation,
104 // these checks are mostly for making sure these moves have been added correctly.
105 size_t current_liveness = 0;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100106 for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
Nicolas Geoffray59768572014-12-01 09:50:04 +0000107 HBasicBlock* block = it.Current();
108 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
109 HInstruction* instruction = inst_it.Current();
110 DCHECK_LE(current_liveness, instruction->GetLifetimePosition());
111 current_liveness = instruction->GetLifetimePosition();
112 }
113 for (HInstructionIterator inst_it(block->GetInstructions());
114 !inst_it.Done();
115 inst_it.Advance()) {
116 HInstruction* instruction = inst_it.Current();
117 DCHECK_LE(current_liveness, instruction->GetLifetimePosition()) << instruction->DebugName();
118 current_liveness = instruction->GetLifetimePosition();
119 }
120 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100121 }
122}
123
124void RegisterAllocator::BlockRegister(Location location,
125 size_t start,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100126 size_t end) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100127 int reg = location.reg();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100128 DCHECK(location.IsRegister() || location.IsFpuRegister());
129 LiveInterval* interval = location.IsRegister()
130 ? physical_core_register_intervals_.Get(reg)
131 : physical_fp_register_intervals_.Get(reg);
132 Primitive::Type type = location.IsRegister()
133 ? Primitive::kPrimInt
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000134 : Primitive::kPrimFloat;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100135 if (interval == nullptr) {
136 interval = LiveInterval::MakeFixedInterval(allocator_, reg, type);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100137 if (location.IsRegister()) {
138 physical_core_register_intervals_.Put(reg, interval);
139 } else {
140 physical_fp_register_intervals_.Put(reg, interval);
141 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100142 }
143 DCHECK(interval->GetRegister() == reg);
144 interval->AddRange(start, end);
145}
146
147void RegisterAllocator::AllocateRegistersInternal() {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100148 // Iterate post-order, to ensure the list is sorted, and the last added interval
149 // is the one with the lowest start position.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100150 for (HLinearPostOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100151 HBasicBlock* block = it.Current();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800152 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
153 back_it.Advance()) {
154 ProcessInstruction(back_it.Current());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100155 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800156 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
157 ProcessInstruction(inst_it.Current());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100158 }
159 }
160
Nicolas Geoffray39468442014-09-02 15:17:15 +0100161 number_of_registers_ = codegen_->GetNumberOfCoreRegisters();
162 registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
163 processing_core_registers_ = true;
164 unhandled_ = &unhandled_core_intervals_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100165 for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
166 LiveInterval* fixed = physical_core_register_intervals_.Get(i);
167 if (fixed != nullptr) {
Mingyao Yang296bd602014-10-06 16:47:28 -0700168 // Fixed interval is added to inactive_ instead of unhandled_.
169 // It's also the only type of inactive interval whose start position
170 // can be after the current interval during linear scan.
171 // Fixed interval is never split and never moves to unhandled_.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100172 inactive_.Add(fixed);
173 }
174 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100175 LinearScan();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100176
177 inactive_.Reset();
178 active_.Reset();
179 handled_.Reset();
180
181 number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters();
182 registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
183 processing_core_registers_ = false;
184 unhandled_ = &unhandled_fp_intervals_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100185 for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
186 LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
187 if (fixed != nullptr) {
Mingyao Yang296bd602014-10-06 16:47:28 -0700188 // Fixed interval is added to inactive_ instead of unhandled_.
189 // It's also the only type of inactive interval whose start position
190 // can be after the current interval during linear scan.
191 // Fixed interval is never split and never moves to unhandled_.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100192 inactive_.Add(fixed);
193 }
194 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100195 LinearScan();
196}
197
198void RegisterAllocator::ProcessInstruction(HInstruction* instruction) {
199 LocationSummary* locations = instruction->GetLocations();
200 size_t position = instruction->GetLifetimePosition();
201
202 if (locations == nullptr) return;
203
204 // Create synthesized intervals for temporaries.
205 for (size_t i = 0; i < locations->GetTempCount(); ++i) {
206 Location temp = locations->GetTemp(i);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000207 if (temp.IsRegister() || temp.IsFpuRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100208 BlockRegister(temp, position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100209 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100210 DCHECK(temp.IsUnallocated());
Roland Levillain5368c212014-11-27 15:03:41 +0000211 switch (temp.GetPolicy()) {
212 case Location::kRequiresRegister: {
213 LiveInterval* interval =
214 LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt);
215 temp_intervals_.Add(interval);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000216 interval->AddTempUse(instruction, i);
Roland Levillain5368c212014-11-27 15:03:41 +0000217 unhandled_core_intervals_.Add(interval);
218 break;
219 }
220
221 case Location::kRequiresFpuRegister: {
222 LiveInterval* interval =
223 LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimDouble);
224 temp_intervals_.Add(interval);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000225 interval->AddTempUse(instruction, i);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000226 if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100227 interval->AddHighInterval(/* is_temp */ true);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000228 LiveInterval* high = interval->GetHighInterval();
229 temp_intervals_.Add(high);
230 unhandled_fp_intervals_.Add(high);
231 }
Roland Levillain5368c212014-11-27 15:03:41 +0000232 unhandled_fp_intervals_.Add(interval);
233 break;
234 }
235
236 default:
237 LOG(FATAL) << "Unexpected policy for temporary location "
238 << temp.GetPolicy();
239 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100240 }
241 }
242
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100243 bool core_register = (instruction->GetType() != Primitive::kPrimDouble)
244 && (instruction->GetType() != Primitive::kPrimFloat);
245
Nicolas Geoffray39468442014-09-02 15:17:15 +0100246 if (locations->CanCall()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000247 if (codegen_->IsLeafMethod()) {
248 // TODO: We do this here because we do not want the suspend check to artificially
249 // create live registers. We should find another place, but this is currently the
250 // simplest.
251 DCHECK(instruction->IsSuspendCheckEntry());
252 instruction->GetBlock()->RemoveInstruction(instruction);
253 return;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100254 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100255 safepoints_.Add(instruction);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100256 if (locations->OnlyCallsOnSlowPath()) {
257 // We add a synthesized range at this position to record the live registers
258 // at this position. Ideally, we could just update the safepoints when locations
259 // are updated, but we currently need to know the full stack size before updating
260 // locations (because of parameters and the fact that we don't have a frame pointer).
261 // And knowing the full stack size requires to know the maximum number of live
262 // registers at calls in slow paths.
263 // By adding the following interval in the algorithm, we can compute this
264 // maximum before updating locations.
265 LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction);
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000266 interval->AddRange(position, position + 1);
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000267 AddSorted(&unhandled_core_intervals_, interval);
268 AddSorted(&unhandled_fp_intervals_, interval);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100269 }
270 }
271
272 if (locations->WillCall()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100273 // Block all registers.
274 for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000275 if (!codegen_->IsCoreCalleeSaveRegister(i)) {
276 BlockRegister(Location::RegisterLocation(i),
277 position,
278 position + 1);
279 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100280 }
281 for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000282 if (!codegen_->IsFloatingPointCalleeSaveRegister(i)) {
283 BlockRegister(Location::FpuRegisterLocation(i),
284 position,
285 position + 1);
286 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100287 }
288 }
289
290 for (size_t i = 0; i < instruction->InputCount(); ++i) {
291 Location input = locations->InAt(i);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100292 if (input.IsRegister() || input.IsFpuRegister()) {
293 BlockRegister(input, position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000294 } else if (input.IsPair()) {
295 BlockRegister(input.ToLow(), position, position + 1);
296 BlockRegister(input.ToHigh(), position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100297 }
298 }
299
Nicolas Geoffray39468442014-09-02 15:17:15 +0100300 LiveInterval* current = instruction->GetLiveInterval();
301 if (current == nullptr) return;
302
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100303 GrowableArray<LiveInterval*>& unhandled = core_register
304 ? unhandled_core_intervals_
305 : unhandled_fp_intervals_;
306
Nicolas Geoffray76905622014-09-25 14:39:26 +0100307 DCHECK(unhandled.IsEmpty() || current->StartsBeforeOrAt(unhandled.Peek()));
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000308
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000309 if (codegen_->NeedsTwoRegisters(current->GetType())) {
310 current->AddHighInterval();
311 }
312
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100313 for (size_t safepoint_index = safepoints_.Size(); safepoint_index > 0; --safepoint_index) {
314 HInstruction* safepoint = safepoints_.Get(safepoint_index - 1);
315 size_t safepoint_position = safepoint->GetLifetimePosition();
316
317 // Test that safepoints are ordered in the optimal way.
318 DCHECK(safepoint_index == safepoints_.Size()
319 || safepoints_.Get(safepoint_index)->GetLifetimePosition() < safepoint_position);
320
321 if (safepoint_position == current->GetStart()) {
322 // The safepoint is for this instruction, so the location of the instruction
323 // does not need to be saved.
324 DCHECK_EQ(safepoint_index, safepoints_.Size());
325 DCHECK_EQ(safepoint, instruction);
326 continue;
327 } else if (current->IsDeadAt(safepoint_position)) {
328 break;
329 } else if (!current->Covers(safepoint_position)) {
330 // Hole in the interval.
331 continue;
332 }
333 current->AddSafepoint(safepoint);
334 }
335
Nicolas Geoffray39468442014-09-02 15:17:15 +0100336 // Some instructions define their output in fixed register/stack slot. We need
337 // to ensure we know these locations before doing register allocation. For a
338 // given register, we create an interval that covers these locations. The register
339 // will be unavailable at these locations when trying to allocate one for an
340 // interval.
341 //
342 // The backwards walking ensures the ranges are ordered on increasing start positions.
343 Location output = locations->Out();
Calin Juravled0d48522014-11-04 16:40:20 +0000344 if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
345 Location first = locations->InAt(0);
346 if (first.IsRegister() || first.IsFpuRegister()) {
347 current->SetFrom(position + 1);
348 current->SetRegister(first.reg());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000349 } else if (first.IsPair()) {
350 current->SetFrom(position + 1);
351 current->SetRegister(first.low());
352 LiveInterval* high = current->GetHighInterval();
353 high->SetRegister(first.high());
354 high->SetFrom(position + 1);
Calin Juravled0d48522014-11-04 16:40:20 +0000355 }
356 } else if (output.IsRegister() || output.IsFpuRegister()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100357 // Shift the interval's start by one to account for the blocked register.
358 current->SetFrom(position + 1);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100359 current->SetRegister(output.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100360 BlockRegister(output, position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000361 } else if (output.IsPair()) {
362 current->SetFrom(position + 1);
363 current->SetRegister(output.low());
364 LiveInterval* high = current->GetHighInterval();
365 high->SetRegister(output.high());
366 high->SetFrom(position + 1);
367 BlockRegister(output.ToLow(), position, position + 1);
368 BlockRegister(output.ToHigh(), position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100369 } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
370 current->SetSpillSlot(output.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000371 } else {
372 DCHECK(output.IsUnallocated() || output.IsConstant());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100373 }
374
375 // If needed, add interval to the list of unhandled intervals.
376 if (current->HasSpillSlot() || instruction->IsConstant()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100377 // Split just before first register use.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100378 size_t first_register_use = current->FirstRegisterUse();
379 if (first_register_use != kNoLifetime) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100380 LiveInterval* split = Split(current, first_register_use - 1);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000381 // Don't add directly to `unhandled`, it needs to be sorted and the start
Nicolas Geoffray39468442014-09-02 15:17:15 +0100382 // of this new interval might be after intervals already in the list.
383 AddSorted(&unhandled, split);
384 } else {
385 // Nothing to do, we won't allocate a register for this value.
386 }
387 } else {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000388 // Don't add directly to `unhandled`, temp or safepoint intervals
389 // for this instruction may have been added, and those can be
390 // processed first.
391 AddSorted(&unhandled, current);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100392 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100393}
394
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100395class AllRangesIterator : public ValueObject {
396 public:
397 explicit AllRangesIterator(LiveInterval* interval)
398 : current_interval_(interval),
399 current_range_(interval->GetFirstRange()) {}
400
401 bool Done() const { return current_interval_ == nullptr; }
402 LiveRange* CurrentRange() const { return current_range_; }
403 LiveInterval* CurrentInterval() const { return current_interval_; }
404
405 void Advance() {
406 current_range_ = current_range_->GetNext();
407 if (current_range_ == nullptr) {
408 current_interval_ = current_interval_->GetNextSibling();
409 if (current_interval_ != nullptr) {
410 current_range_ = current_interval_->GetFirstRange();
411 }
412 }
413 }
414
415 private:
416 LiveInterval* current_interval_;
417 LiveRange* current_range_;
418
419 DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
420};
421
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100422bool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
423 // To simplify unit testing, we eagerly create the array of intervals, and
424 // call the helper method.
425 GrowableArray<LiveInterval*> intervals(allocator_, 0);
426 for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
427 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
428 if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
429 intervals.Add(instruction->GetLiveInterval());
430 }
431 }
432
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100433 if (processing_core_registers_) {
434 for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
435 LiveInterval* fixed = physical_core_register_intervals_.Get(i);
436 if (fixed != nullptr) {
437 intervals.Add(fixed);
438 }
439 }
440 } else {
441 for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
442 LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
443 if (fixed != nullptr) {
444 intervals.Add(fixed);
445 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100446 }
447 }
448
Nicolas Geoffray39468442014-09-02 15:17:15 +0100449 for (size_t i = 0, e = temp_intervals_.Size(); i < e; ++i) {
450 LiveInterval* temp = temp_intervals_.Get(i);
451 if (ShouldProcess(processing_core_registers_, temp)) {
452 intervals.Add(temp);
453 }
454 }
455
Nicolas Geoffray776b3182015-02-23 14:14:57 +0000456 return ValidateIntervals(intervals, GetNumberOfSpillSlots(), reserved_out_slots_, *codegen_,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100457 allocator_, processing_core_registers_, log_fatal_on_failure);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100458}
459
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100460bool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
461 size_t number_of_spill_slots,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100462 size_t number_of_out_slots,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100463 const CodeGenerator& codegen,
464 ArenaAllocator* allocator,
465 bool processing_core_registers,
466 bool log_fatal_on_failure) {
467 size_t number_of_registers = processing_core_registers
468 ? codegen.GetNumberOfCoreRegisters()
469 : codegen.GetNumberOfFloatingPointRegisters();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100470 GrowableArray<ArenaBitVector*> liveness_of_values(
471 allocator, number_of_registers + number_of_spill_slots);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100472
473 // Allocate a bit vector per register. A live interval that has a register
474 // allocated will populate the associated bit vector based on its live ranges.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100475 for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
476 liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100477 }
478
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100479 for (size_t i = 0, e = intervals.Size(); i < e; ++i) {
480 for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) {
481 LiveInterval* current = it.CurrentInterval();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100482 HInstruction* defined_by = current->GetParent()->GetDefinedBy();
483 if (current->GetParent()->HasSpillSlot()
484 // Parameters have their own stack slot.
485 && !(defined_by != nullptr && defined_by->IsParameterValue())) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100486 BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers
487 + current->GetParent()->GetSpillSlot() / kVRegSize
488 - number_of_out_slots);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100489 for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
490 if (liveness_of_spill_slot->IsBitSet(j)) {
491 if (log_fatal_on_failure) {
492 std::ostringstream message;
493 message << "Spill slot conflict at " << j;
494 LOG(FATAL) << message.str();
495 } else {
496 return false;
497 }
498 } else {
499 liveness_of_spill_slot->SetBit(j);
500 }
501 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100502 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100503
504 if (current->HasRegister()) {
505 BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister());
506 for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
507 if (liveness_of_register->IsBitSet(j)) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000508 if (current->IsUsingInputRegister() && current->CanUseInputRegister()) {
509 continue;
510 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100511 if (log_fatal_on_failure) {
512 std::ostringstream message;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100513 message << "Register conflict at " << j << " ";
514 if (defined_by != nullptr) {
515 message << "(" << defined_by->DebugName() << ")";
516 }
517 message << "for ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100518 if (processing_core_registers) {
519 codegen.DumpCoreRegister(message, current->GetRegister());
520 } else {
521 codegen.DumpFloatingPointRegister(message, current->GetRegister());
522 }
523 LOG(FATAL) << message.str();
524 } else {
525 return false;
526 }
527 } else {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100528 liveness_of_register->SetBit(j);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100529 }
530 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100531 }
532 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100533 }
534 return true;
535}
536
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100537void RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100538 interval->Dump(stream);
539 stream << ": ";
540 if (interval->HasRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100541 if (interval->IsFloatingPoint()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100542 codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100543 } else {
544 codegen_->DumpCoreRegister(stream, interval->GetRegister());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100545 }
546 } else {
547 stream << "spilled";
548 }
549 stream << std::endl;
550}
551
Mingyao Yang296bd602014-10-06 16:47:28 -0700552void RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
553 stream << "inactive: " << std::endl;
554 for (size_t i = 0; i < inactive_.Size(); i ++) {
555 DumpInterval(stream, inactive_.Get(i));
556 }
557 stream << "active: " << std::endl;
558 for (size_t i = 0; i < active_.Size(); i ++) {
559 DumpInterval(stream, active_.Get(i));
560 }
561 stream << "unhandled: " << std::endl;
562 auto unhandled = (unhandled_ != nullptr) ?
563 unhandled_ : &unhandled_core_intervals_;
564 for (size_t i = 0; i < unhandled->Size(); i ++) {
565 DumpInterval(stream, unhandled->Get(i));
566 }
567 stream << "handled: " << std::endl;
568 for (size_t i = 0; i < handled_.Size(); i ++) {
569 DumpInterval(stream, handled_.Get(i));
570 }
571}
572
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100573// By the book implementation of a linear scan register allocator.
574void RegisterAllocator::LinearScan() {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100575 while (!unhandled_->IsEmpty()) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100576 // (1) Remove interval with the lowest start position from unhandled.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100577 LiveInterval* current = unhandled_->Pop();
578 DCHECK(!current->IsFixed() && !current->HasSpillSlot());
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100579 DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000580 DCHECK(!current->IsLowInterval() || unhandled_->Peek()->IsHighInterval());
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000581
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100582 size_t position = current->GetStart();
583
Mingyao Yang296bd602014-10-06 16:47:28 -0700584 // Remember the inactive_ size here since the ones moved to inactive_ from
585 // active_ below shouldn't need to be re-checked.
586 size_t inactive_intervals_to_handle = inactive_.Size();
587
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100588 // (2) Remove currently active intervals that are dead at this position.
589 // Move active intervals that have a lifetime hole at this position
590 // to inactive.
591 for (size_t i = 0; i < active_.Size(); ++i) {
592 LiveInterval* interval = active_.Get(i);
593 if (interval->IsDeadAt(position)) {
594 active_.Delete(interval);
595 --i;
596 handled_.Add(interval);
597 } else if (!interval->Covers(position)) {
598 active_.Delete(interval);
599 --i;
600 inactive_.Add(interval);
601 }
602 }
603
604 // (3) Remove currently inactive intervals that are dead at this position.
605 // Move inactive intervals that cover this position to active.
Mingyao Yang296bd602014-10-06 16:47:28 -0700606 for (size_t i = 0; i < inactive_intervals_to_handle; ++i) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100607 LiveInterval* interval = inactive_.Get(i);
Mingyao Yang296bd602014-10-06 16:47:28 -0700608 DCHECK(interval->GetStart() < position || interval->IsFixed());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100609 if (interval->IsDeadAt(position)) {
610 inactive_.Delete(interval);
611 --i;
Mingyao Yang296bd602014-10-06 16:47:28 -0700612 --inactive_intervals_to_handle;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100613 handled_.Add(interval);
614 } else if (interval->Covers(position)) {
615 inactive_.Delete(interval);
616 --i;
Mingyao Yang296bd602014-10-06 16:47:28 -0700617 --inactive_intervals_to_handle;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100618 active_.Add(interval);
619 }
620 }
621
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000622 if (current->IsSlowPathSafepoint()) {
623 // Synthesized interval to record the maximum number of live registers
624 // at safepoints. No need to allocate a register for it.
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500625 if (processing_core_registers_) {
626 maximum_number_of_live_core_registers_ =
627 std::max(maximum_number_of_live_core_registers_, active_.Size());
628 } else {
629 maximum_number_of_live_fp_registers_ =
630 std::max(maximum_number_of_live_fp_registers_, active_.Size());
631 }
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000632 DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() > current->GetStart());
633 continue;
634 }
635
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000636 if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
637 DCHECK(!current->HasRegister());
638 // Allocating the low part was unsucessful. The splitted interval for the high part
639 // will be handled next (it is in the `unhandled_` list).
640 continue;
641 }
642
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100643 // (4) Try to find an available register.
644 bool success = TryAllocateFreeReg(current);
645
646 // (5) If no register could be found, we need to spill.
647 if (!success) {
648 success = AllocateBlockedReg(current);
649 }
650
651 // (6) If the interval had a register allocated, add it to the list of active
652 // intervals.
653 if (success) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000654 codegen_->AddAllocatedRegister(processing_core_registers_
655 ? Location::RegisterLocation(current->GetRegister())
656 : Location::FpuRegisterLocation(current->GetRegister()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100657 active_.Add(current);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000658 if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
659 current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
660 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100661 }
662 }
663}
664
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000665static void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) {
666 DCHECK(!interval->IsHighInterval());
667 // Note that the same instruction may occur multiple times in the input list,
668 // so `free_until` may have changed already.
669 if (interval->IsDeadAt(position)) {
670 // Set the register to be free. Note that inactive intervals might later
671 // update this.
672 free_until[interval->GetRegister()] = kMaxLifetimePosition;
673 if (interval->HasHighInterval()) {
674 DCHECK(interval->GetHighInterval()->IsDeadAt(position));
675 free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition;
676 }
677 } else if (!interval->Covers(position)) {
678 // The interval becomes inactive at `defined_by`. We make its register
679 // available only until the next use strictly after `defined_by`.
680 free_until[interval->GetRegister()] = interval->FirstUseAfter(position);
681 if (interval->HasHighInterval()) {
682 DCHECK(!interval->GetHighInterval()->Covers(position));
683 free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()];
684 }
685 }
686}
687
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100688// Find a free register. If multiple are found, pick the register that
689// is free the longest.
690bool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
691 size_t* free_until = registers_array_;
692
693 // First set all registers to be free.
694 for (size_t i = 0; i < number_of_registers_; ++i) {
695 free_until[i] = kMaxLifetimePosition;
696 }
697
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100698 // For each active interval, set its register to not free.
699 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
700 LiveInterval* interval = active_.Get(i);
701 DCHECK(interval->HasRegister());
702 free_until[interval->GetRegister()] = 0;
703 }
704
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000705 // An interval that starts an instruction (that is, it is not split), may
706 // re-use the registers used by the inputs of that instruciton, based on the
707 // location summary.
708 HInstruction* defined_by = current->GetDefinedBy();
709 if (defined_by != nullptr && !current->IsSplit()) {
710 LocationSummary* locations = defined_by->GetLocations();
711 if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) {
712 for (HInputIterator it(defined_by); !it.Done(); it.Advance()) {
713 // Take the last interval of the input. It is the location of that interval
714 // that will be used at `defined_by`.
715 LiveInterval* interval = it.Current()->GetLiveInterval()->GetLastSibling();
716 // Note that interval may have not been processed yet.
717 // TODO: Handle non-split intervals last in the work list.
718 if (interval->HasRegister() && interval->SameRegisterKind(*current)) {
719 // The input must be live until the end of `defined_by`, to comply to
720 // the linear scan algorithm. So we use `defined_by`'s end lifetime
721 // position to check whether the input is dead or is inactive after
722 // `defined_by`.
723 DCHECK(interval->Covers(defined_by->GetLifetimePosition()));
724 size_t position = defined_by->GetLifetimePosition() + 1;
725 FreeIfNotCoverAt(interval, position, free_until);
726 }
727 }
728 }
729 }
730
Mingyao Yang296bd602014-10-06 16:47:28 -0700731 // For each inactive interval, set its register to be free until
732 // the next intersection with `current`.
733 for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
734 LiveInterval* inactive = inactive_.Get(i);
735 // Temp/Slow-path-safepoint interval has no holes.
736 DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
737 if (!current->IsSplit() && !inactive->IsFixed()) {
738 // Neither current nor inactive are fixed.
739 // Thanks to SSA, a non-split interval starting in a hole of an
740 // inactive interval should never intersect with that inactive interval.
741 // Only if it's not fixed though, because fixed intervals don't come from SSA.
742 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
743 continue;
744 }
745
746 DCHECK(inactive->HasRegister());
747 if (free_until[inactive->GetRegister()] == 0) {
748 // Already used by some active interval. No need to intersect.
749 continue;
750 }
751 size_t next_intersection = inactive->FirstIntersectionWith(current);
752 if (next_intersection != kNoLifetime) {
753 free_until[inactive->GetRegister()] =
754 std::min(free_until[inactive->GetRegister()], next_intersection);
755 }
756 }
757
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000758 int reg = kNoRegister;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100759 if (current->HasRegister()) {
760 // Some instructions have a fixed register output.
761 reg = current->GetRegister();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000762 if (free_until[reg] == 0) {
763 DCHECK(current->IsHighInterval());
764 // AllocateBlockedReg will spill the holder of the register.
765 return false;
766 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100767 } else {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000768 DCHECK(!current->IsHighInterval());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100769 int hint = current->FindFirstRegisterHint(free_until);
770 if (hint != kNoRegister) {
771 DCHECK(!IsBlocked(hint));
772 reg = hint;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000773 } else if (current->IsLowInterval()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000774 reg = FindAvailableRegisterPair(free_until, current->GetStart());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100775 } else {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000776 reg = FindAvailableRegister(free_until);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100777 }
778 }
779
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000780 DCHECK_NE(reg, kNoRegister);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100781 // If we could not find a register, we need to spill.
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000782 if (free_until[reg] == 0) {
783 return false;
784 }
785
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000786 if (current->IsLowInterval()) {
787 // If the high register of this interval is not available, we need to spill.
788 int high_reg = current->GetHighInterval()->GetRegister();
789 if (high_reg == kNoRegister) {
790 high_reg = GetHighForLowRegister(reg);
791 }
792 if (free_until[high_reg] == 0) {
793 return false;
794 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100795 }
796
797 current->SetRegister(reg);
798 if (!current->IsDeadAt(free_until[reg])) {
799 // If the register is only available for a subset of live ranges
800 // covered by `current`, split `current` at the position where
801 // the register is not available anymore.
802 LiveInterval* split = Split(current, free_until[reg]);
803 DCHECK(split != nullptr);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100804 AddSorted(unhandled_, split);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100805 }
806 return true;
807}
808
809bool RegisterAllocator::IsBlocked(int reg) const {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100810 return processing_core_registers_
811 ? blocked_core_registers_[reg]
812 : blocked_fp_registers_[reg];
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100813}
814
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000815int RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const {
816 int reg = kNoRegister;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000817 // Pick the register pair that is used the last.
818 for (size_t i = 0; i < number_of_registers_; ++i) {
819 if (IsBlocked(i)) continue;
820 if (!IsLowRegister(i)) continue;
821 int high_register = GetHighForLowRegister(i);
822 if (IsBlocked(high_register)) continue;
823 int existing_high_register = GetHighForLowRegister(reg);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000824 if ((reg == kNoRegister) || (next_use[i] >= next_use[reg]
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000825 && next_use[high_register] >= next_use[existing_high_register])) {
826 reg = i;
827 if (next_use[i] == kMaxLifetimePosition
828 && next_use[high_register] == kMaxLifetimePosition) {
829 break;
830 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000831 } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) {
832 // If one of the current register is known to be unavailable, just unconditionally
833 // try a new one.
834 reg = i;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000835 }
836 }
837 return reg;
838}
839
840int RegisterAllocator::FindAvailableRegister(size_t* next_use) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000841 int reg = kNoRegister;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000842 // Pick the register that is used the last.
843 for (size_t i = 0; i < number_of_registers_; ++i) {
844 if (IsBlocked(i)) continue;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000845 if (reg == kNoRegister || next_use[i] > next_use[reg]) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000846 reg = i;
847 if (next_use[i] == kMaxLifetimePosition) break;
848 }
849 }
850 return reg;
851}
852
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000853bool RegisterAllocator::TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
854 size_t first_register_use,
855 size_t* next_use) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000856 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
857 LiveInterval* active = active_.Get(i);
858 DCHECK(active->HasRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000859 if (active->IsFixed()) continue;
860 if (active->IsHighInterval()) continue;
861 if (first_register_use > next_use[active->GetRegister()]) continue;
862
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000863 // Split the first interval found.
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000864 if (!active->IsLowInterval() || IsLowOfUnalignedPairInterval(active)) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000865 LiveInterval* split = Split(active, position);
866 active_.DeleteAt(i);
867 if (split != active) {
868 handled_.Add(active);
869 }
870 AddSorted(unhandled_, split);
871 return true;
872 }
873 }
874 return false;
875}
876
Nicolas Geoffray5b168de2015-03-27 10:27:22 +0000877bool RegisterAllocator::PotentiallyRemoveOtherHalf(LiveInterval* interval,
878 GrowableArray<LiveInterval*>* intervals,
879 size_t index) {
880 if (interval->IsLowInterval()) {
881 DCHECK_EQ(intervals->Get(index), interval->GetHighInterval());
882 intervals->DeleteAt(index);
883 return true;
884 } else if (interval->IsHighInterval()) {
885 DCHECK_GT(index, 0u);
886 DCHECK_EQ(intervals->Get(index - 1), interval->GetLowInterval());
887 intervals->DeleteAt(index - 1);
888 return true;
889 } else {
890 return false;
891 }
892}
893
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100894// Find the register that is used the last, and spill the interval
895// that holds it. If the first use of `current` is after that register
896// we spill `current` instead.
897bool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
898 size_t first_register_use = current->FirstRegisterUse();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100899 if (first_register_use == kNoLifetime) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100900 AllocateSpillSlotFor(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100901 return false;
902 }
903
904 // First set all registers as not being used.
905 size_t* next_use = registers_array_;
906 for (size_t i = 0; i < number_of_registers_; ++i) {
907 next_use[i] = kMaxLifetimePosition;
908 }
909
910 // For each active interval, find the next use of its register after the
911 // start of current.
912 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
913 LiveInterval* active = active_.Get(i);
914 DCHECK(active->HasRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100915 if (active->IsFixed()) {
916 next_use[active->GetRegister()] = current->GetStart();
917 } else {
918 size_t use = active->FirstRegisterUseAfter(current->GetStart());
919 if (use != kNoLifetime) {
920 next_use[active->GetRegister()] = use;
921 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100922 }
923 }
924
925 // For each inactive interval, find the next use of its register after the
926 // start of current.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100927 for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
928 LiveInterval* inactive = inactive_.Get(i);
Mingyao Yang296bd602014-10-06 16:47:28 -0700929 // Temp/Slow-path-safepoint interval has no holes.
930 DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
931 if (!current->IsSplit() && !inactive->IsFixed()) {
932 // Neither current nor inactive are fixed.
933 // Thanks to SSA, a non-split interval starting in a hole of an
934 // inactive interval should never intersect with that inactive interval.
935 // Only if it's not fixed though, because fixed intervals don't come from SSA.
936 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
937 continue;
938 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100939 DCHECK(inactive->HasRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100940 size_t next_intersection = inactive->FirstIntersectionWith(current);
941 if (next_intersection != kNoLifetime) {
942 if (inactive->IsFixed()) {
943 next_use[inactive->GetRegister()] =
944 std::min(next_intersection, next_use[inactive->GetRegister()]);
945 } else {
946 size_t use = inactive->FirstRegisterUseAfter(current->GetStart());
947 if (use != kNoLifetime) {
948 next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
949 }
950 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100951 }
952 }
953
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000954 int reg = kNoRegister;
955 bool should_spill = false;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000956 if (current->HasRegister()) {
957 DCHECK(current->IsHighInterval());
958 reg = current->GetRegister();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000959 // When allocating the low part, we made sure the high register was available.
960 DCHECK_LT(first_register_use, next_use[reg]);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000961 } else if (current->IsLowInterval()) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000962 reg = FindAvailableRegisterPair(next_use, first_register_use);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000963 // We should spill if both registers are not available.
964 should_spill = (first_register_use >= next_use[reg])
965 || (first_register_use >= next_use[GetHighForLowRegister(reg)]);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000966 } else {
967 DCHECK(!current->IsHighInterval());
968 reg = FindAvailableRegister(next_use);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000969 should_spill = (first_register_use >= next_use[reg]);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100970 }
971
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000972 DCHECK_NE(reg, kNoRegister);
973 if (should_spill) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000974 DCHECK(!current->IsHighInterval());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000975 bool is_allocation_at_use_site = (current->GetStart() >= (first_register_use - 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000976 if (current->IsLowInterval()
977 && is_allocation_at_use_site
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000978 && TrySplitNonPairOrUnalignedPairIntervalAt(current->GetStart(),
979 first_register_use,
980 next_use)) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000981 // If we're allocating a register for `current` because the instruction at
982 // that position requires it, but we think we should spill, then there are
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000983 // non-pair intervals or unaligned pair intervals blocking the allocation.
984 // We split the first interval found, and put ourselves first in the
985 // `unhandled_` list.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000986 LiveInterval* existing = unhandled_->Peek();
987 DCHECK(existing->IsHighInterval());
988 DCHECK_EQ(existing->GetLowInterval(), current);
989 unhandled_->Add(current);
990 } else {
991 // If the first use of that instruction is after the last use of the found
992 // register, we split this interval just before its first register use.
993 AllocateSpillSlotFor(current);
994 LiveInterval* split = Split(current, first_register_use - 1);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000995 if (current == split) {
996 DumpInterval(std::cerr, current);
997 DumpAllIntervals(std::cerr);
998 // This situation has the potential to infinite loop, so we make it a non-debug CHECK.
999 CHECK(false) << "There is not enough registers available for "
1000 << split->GetParent()->GetDefinedBy()->DebugName() << " "
1001 << split->GetParent()->GetDefinedBy()->GetId()
1002 << " at " << first_register_use - 1;
1003 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001004 AddSorted(unhandled_, split);
1005 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001006 return false;
1007 } else {
1008 // Use this register and spill the active and inactives interval that
1009 // have that register.
1010 current->SetRegister(reg);
1011
1012 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
1013 LiveInterval* active = active_.Get(i);
1014 if (active->GetRegister() == reg) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001015 DCHECK(!active->IsFixed());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001016 LiveInterval* split = Split(active, current->GetStart());
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001017 if (split != active) {
1018 handled_.Add(active);
1019 }
Nicolas Geoffray5b168de2015-03-27 10:27:22 +00001020 active_.DeleteAt(i);
1021 PotentiallyRemoveOtherHalf(active, &active_, i);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001022 AddSorted(unhandled_, split);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001023 break;
1024 }
1025 }
1026
Nicolas Geoffray5b168de2015-03-27 10:27:22 +00001027 for (size_t i = 0; i < inactive_.Size(); ++i) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001028 LiveInterval* inactive = inactive_.Get(i);
1029 if (inactive->GetRegister() == reg) {
Mingyao Yang296bd602014-10-06 16:47:28 -07001030 if (!current->IsSplit() && !inactive->IsFixed()) {
1031 // Neither current nor inactive are fixed.
1032 // Thanks to SSA, a non-split interval starting in a hole of an
1033 // inactive interval should never intersect with that inactive interval.
1034 // Only if it's not fixed though, because fixed intervals don't come from SSA.
1035 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
1036 continue;
1037 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001038 size_t next_intersection = inactive->FirstIntersectionWith(current);
1039 if (next_intersection != kNoLifetime) {
1040 if (inactive->IsFixed()) {
1041 LiveInterval* split = Split(current, next_intersection);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001042 DCHECK_NE(split, current);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001043 AddSorted(unhandled_, split);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001044 } else {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001045 // Split at the start of `current`, which will lead to splitting
1046 // at the end of the lifetime hole of `inactive`.
1047 LiveInterval* split = Split(inactive, current->GetStart());
1048 // If it's inactive, it must start before the current interval.
1049 DCHECK_NE(split, inactive);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001050 inactive_.DeleteAt(i);
Nicolas Geoffray5b168de2015-03-27 10:27:22 +00001051 if (PotentiallyRemoveOtherHalf(inactive, &inactive_, i) && inactive->IsHighInterval()) {
1052 // We have removed an entry prior to `inactive`. So we need to decrement.
1053 --i;
1054 }
1055 // Decrement because we have removed `inactive` from the list.
Mingyao Yang296bd602014-10-06 16:47:28 -07001056 --i;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001057 handled_.Add(inactive);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001058 AddSorted(unhandled_, split);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001059 }
1060 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001061 }
1062 }
1063
1064 return true;
1065 }
1066}
1067
Nicolas Geoffray39468442014-09-02 15:17:15 +01001068void RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01001069 DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001070 size_t insert_at = 0;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001071 for (size_t i = array->Size(); i > 0; --i) {
1072 LiveInterval* current = array->Get(i - 1);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001073 // High intervals must be processed right after their low equivalent.
1074 if (current->StartsAfter(interval) && !current->IsHighInterval()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001075 insert_at = i;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001076 break;
Nicolas Geoffrayacd03392014-11-26 15:46:52 +00001077 } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
1078 // Ensure the slow path interval is the last to be processed at its location: we want the
1079 // interval to know all live registers at this location.
1080 DCHECK(i == 1 || array->Get(i - 2)->StartsAfter(current));
1081 insert_at = i;
1082 break;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001083 }
1084 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001085
Nicolas Geoffray39468442014-09-02 15:17:15 +01001086 array->InsertAt(insert_at, interval);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001087 // Insert the high interval before the low, to ensure the low is processed before.
1088 if (interval->HasHighInterval()) {
1089 array->InsertAt(insert_at, interval->GetHighInterval());
1090 } else if (interval->HasLowInterval()) {
1091 array->InsertAt(insert_at + 1, interval->GetLowInterval());
1092 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001093}
1094
1095LiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001096 DCHECK_GE(position, interval->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001097 DCHECK(!interval->IsDeadAt(position));
1098 if (position == interval->GetStart()) {
1099 // Spill slot will be allocated when handling `interval` again.
1100 interval->ClearRegister();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001101 if (interval->HasHighInterval()) {
1102 interval->GetHighInterval()->ClearRegister();
1103 } else if (interval->HasLowInterval()) {
1104 interval->GetLowInterval()->ClearRegister();
1105 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001106 return interval;
1107 } else {
1108 LiveInterval* new_interval = interval->SplitAt(position);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001109 if (interval->HasHighInterval()) {
1110 LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
1111 new_interval->SetHighInterval(high);
1112 high->SetLowInterval(new_interval);
1113 } else if (interval->HasLowInterval()) {
1114 LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
1115 new_interval->SetLowInterval(low);
1116 low->SetHighInterval(new_interval);
1117 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001118 return new_interval;
1119 }
1120}
1121
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001122void RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001123 if (interval->IsHighInterval()) {
1124 // The low interval will contain the spill slot.
1125 return;
1126 }
1127
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001128 LiveInterval* parent = interval->GetParent();
1129
1130 // An instruction gets a spill slot for its entire lifetime. If the parent
1131 // of this interval already has a spill slot, there is nothing to do.
1132 if (parent->HasSpillSlot()) {
1133 return;
1134 }
1135
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001136 HInstruction* defined_by = parent->GetDefinedBy();
1137 if (defined_by->IsParameterValue()) {
1138 // Parameters have their own stack slot.
1139 parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
1140 return;
1141 }
1142
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001143 if (defined_by->IsConstant()) {
1144 // Constants don't need a spill slot.
1145 return;
1146 }
1147
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001148 LiveInterval* last_sibling = interval;
1149 while (last_sibling->GetNextSibling() != nullptr) {
1150 last_sibling = last_sibling->GetNextSibling();
1151 }
1152 size_t end = last_sibling->GetEnd();
1153
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001154 GrowableArray<size_t>* spill_slots = nullptr;
1155 switch (interval->GetType()) {
1156 case Primitive::kPrimDouble:
1157 spill_slots = &double_spill_slots_;
1158 break;
1159 case Primitive::kPrimLong:
1160 spill_slots = &long_spill_slots_;
1161 break;
1162 case Primitive::kPrimFloat:
1163 spill_slots = &float_spill_slots_;
1164 break;
1165 case Primitive::kPrimNot:
1166 case Primitive::kPrimInt:
1167 case Primitive::kPrimChar:
1168 case Primitive::kPrimByte:
1169 case Primitive::kPrimBoolean:
1170 case Primitive::kPrimShort:
1171 spill_slots = &int_spill_slots_;
1172 break;
1173 case Primitive::kPrimVoid:
1174 LOG(FATAL) << "Unexpected type for interval " << interval->GetType();
1175 }
1176
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001177 // Find an available spill slot.
1178 size_t slot = 0;
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001179 for (size_t e = spill_slots->Size(); slot < e; ++slot) {
1180 if (spill_slots->Get(slot) <= parent->GetStart()
1181 && (slot == (e - 1) || spill_slots->Get(slot + 1) <= parent->GetStart())) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001182 break;
1183 }
1184 }
1185
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001186 if (parent->NeedsTwoSpillSlots()) {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001187 if (slot == spill_slots->Size()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001188 // We need a new spill slot.
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001189 spill_slots->Add(end);
1190 spill_slots->Add(end);
1191 } else if (slot == spill_slots->Size() - 1) {
1192 spill_slots->Put(slot, end);
1193 spill_slots->Add(end);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001194 } else {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001195 spill_slots->Put(slot, end);
1196 spill_slots->Put(slot + 1, end);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001197 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001198 } else {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001199 if (slot == spill_slots->Size()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001200 // We need a new spill slot.
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001201 spill_slots->Add(end);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001202 } else {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001203 spill_slots->Put(slot, end);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001204 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001205 }
1206
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001207 // Note that the exact spill slot location will be computed when we resolve,
1208 // that is when we know the number of spill slots for each type.
1209 parent->SetSpillSlot(slot);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001210}
1211
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001212static bool IsValidDestination(Location destination) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001213 return destination.IsRegister()
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001214 || destination.IsRegisterPair()
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001215 || destination.IsFpuRegister()
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001216 || destination.IsFpuRegisterPair()
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001217 || destination.IsStackSlot()
1218 || destination.IsDoubleStackSlot();
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001219}
1220
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001221void RegisterAllocator::AddMove(HParallelMove* move,
1222 Location source,
1223 Location destination,
1224 HInstruction* instruction,
1225 Primitive::Type type) const {
1226 if (type == Primitive::kPrimLong
1227 && codegen_->ShouldSplitLongMoves()
1228 // The parallel move resolver knows how to deal with long constants.
1229 && !source.IsConstant()) {
Nicolas Geoffray90218252015-04-15 11:56:51 +01001230 move->AddMove(source.ToLow(), destination.ToLow(), Primitive::kPrimInt, instruction);
1231 move->AddMove(source.ToHigh(), destination.ToHigh(), Primitive::kPrimInt, nullptr);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001232 } else {
Nicolas Geoffray90218252015-04-15 11:56:51 +01001233 move->AddMove(source, destination, type, instruction);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001234 }
1235}
1236
1237void RegisterAllocator::AddInputMoveFor(HInstruction* input,
1238 HInstruction* user,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001239 Location source,
1240 Location destination) const {
1241 if (source.Equals(destination)) return;
1242
Roland Levillain476df552014-10-09 17:51:36 +01001243 DCHECK(!user->IsPhi());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001244
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001245 HInstruction* previous = user->GetPrevious();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001246 HParallelMove* move = nullptr;
1247 if (previous == nullptr
Roland Levillain476df552014-10-09 17:51:36 +01001248 || !previous->IsParallelMove()
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001249 || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001250 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001251 move->SetLifetimePosition(user->GetLifetimePosition());
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001252 user->GetBlock()->InsertInstructionBefore(move, user);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001253 } else {
1254 move = previous->AsParallelMove();
1255 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001256 DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001257 AddMove(move, source, destination, nullptr, input->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001258}
1259
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001260static bool IsInstructionStart(size_t position) {
1261 return (position & 1) == 0;
1262}
1263
1264static bool IsInstructionEnd(size_t position) {
1265 return (position & 1) == 1;
1266}
1267
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001268void RegisterAllocator::InsertParallelMoveAt(size_t position,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001269 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001270 Location source,
1271 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001272 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001273 if (source.Equals(destination)) return;
1274
1275 HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001276 HParallelMove* move;
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001277 if (at == nullptr) {
1278 if (IsInstructionStart(position)) {
1279 // Block boundary, don't do anything the connection of split siblings will handle it.
1280 return;
1281 } else {
1282 // Move must happen before the first instruction of the block.
1283 at = liveness_.GetInstructionFromPosition((position + 1) / 2);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001284 // Note that parallel moves may have already been inserted, so we explicitly
1285 // ask for the first instruction of the block: `GetInstructionFromPosition` does
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001286 // not contain the `HParallelMove` instructions.
Nicolas Geoffray59768572014-12-01 09:50:04 +00001287 at = at->GetBlock()->GetFirstInstruction();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001288
1289 if (at->GetLifetimePosition() < position) {
1290 // We may insert moves for split siblings and phi spills at the beginning of the block.
1291 // Since this is a different lifetime position, we need to go to the next instruction.
1292 DCHECK(at->IsParallelMove());
1293 at = at->GetNext();
1294 }
1295
Nicolas Geoffray59768572014-12-01 09:50:04 +00001296 if (at->GetLifetimePosition() != position) {
1297 DCHECK_GT(at->GetLifetimePosition(), position);
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001298 move = new (allocator_) HParallelMove(allocator_);
1299 move->SetLifetimePosition(position);
1300 at->GetBlock()->InsertInstructionBefore(move, at);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001301 } else {
1302 DCHECK(at->IsParallelMove());
1303 move = at->AsParallelMove();
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001304 }
1305 }
1306 } else if (IsInstructionEnd(position)) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001307 // Move must happen after the instruction.
1308 DCHECK(!at->IsControlFlow());
1309 move = at->GetNext()->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001310 // This is a parallel move for connecting siblings in a same block. We need to
1311 // differentiate it with moves for connecting blocks, and input moves.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001312 if (move == nullptr || move->GetLifetimePosition() > position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001313 move = new (allocator_) HParallelMove(allocator_);
1314 move->SetLifetimePosition(position);
1315 at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
1316 }
1317 } else {
1318 // Move must happen before the instruction.
1319 HInstruction* previous = at->GetPrevious();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001320 if (previous == nullptr
1321 || !previous->IsParallelMove()
1322 || previous->GetLifetimePosition() != position) {
1323 // If the previous is a parallel move, then its position must be lower
1324 // than the given `position`: it was added just after the non-parallel
1325 // move instruction that precedes `instruction`.
1326 DCHECK(previous == nullptr
1327 || !previous->IsParallelMove()
1328 || previous->GetLifetimePosition() < position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001329 move = new (allocator_) HParallelMove(allocator_);
1330 move->SetLifetimePosition(position);
1331 at->GetBlock()->InsertInstructionBefore(move, at);
1332 } else {
1333 move = previous->AsParallelMove();
1334 }
1335 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001336 DCHECK_EQ(move->GetLifetimePosition(), position);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001337 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001338}
1339
1340void RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001341 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001342 Location source,
1343 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001344 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001345 if (source.Equals(destination)) return;
1346
1347 DCHECK_EQ(block->GetSuccessors().Size(), 1u);
1348 HInstruction* last = block->GetLastInstruction();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001349 // We insert moves at exit for phi predecessors and connecting blocks.
1350 // A block ending with an if cannot branch to a block with phis because
1351 // we do not allow critical edges. It can also not connect
1352 // a split interval between two blocks: the move has to happen in the successor.
1353 DCHECK(!last->IsIf());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001354 HInstruction* previous = last->GetPrevious();
1355 HParallelMove* move;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001356 // This is a parallel move for connecting blocks. We need to differentiate
1357 // it with moves for connecting siblings in a same block, and output moves.
Nicolas Geoffray59768572014-12-01 09:50:04 +00001358 size_t position = last->GetLifetimePosition();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001359 if (previous == nullptr || !previous->IsParallelMove()
Nicolas Geoffray59768572014-12-01 09:50:04 +00001360 || previous->AsParallelMove()->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001361 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001362 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001363 block->InsertInstructionBefore(move, last);
1364 } else {
1365 move = previous->AsParallelMove();
1366 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001367 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001368}
1369
1370void RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001371 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001372 Location source,
1373 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001374 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001375 if (source.Equals(destination)) return;
1376
1377 HInstruction* first = block->GetFirstInstruction();
1378 HParallelMove* move = first->AsParallelMove();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001379 size_t position = block->GetLifetimeStart();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001380 // This is a parallel move for connecting blocks. We need to differentiate
1381 // it with moves for connecting siblings in a same block, and input moves.
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001382 if (move == nullptr || move->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001383 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001384 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001385 block->InsertInstructionBefore(move, first);
1386 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001387 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001388}
1389
1390void RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
1391 Location source,
1392 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001393 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001394 if (source.Equals(destination)) return;
1395
Roland Levillain476df552014-10-09 17:51:36 +01001396 if (instruction->IsPhi()) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001397 InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001398 return;
1399 }
1400
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001401 size_t position = instruction->GetLifetimePosition() + 1;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001402 HParallelMove* move = instruction->GetNext()->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001403 // This is a parallel move for moving the output of an instruction. We need
1404 // to differentiate with input moves, moves for connecting siblings in a
1405 // and moves for connecting blocks.
1406 if (move == nullptr || move->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001407 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001408 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001409 instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
1410 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001411 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001412}
1413
1414void RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
1415 LiveInterval* current = interval;
1416 if (current->HasSpillSlot() && current->HasRegister()) {
1417 // We spill eagerly, so move must be at definition.
1418 InsertMoveAfter(interval->GetDefinedBy(),
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001419 interval->ToLocation(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001420 interval->NeedsTwoSpillSlots()
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001421 ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1422 : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001423 }
1424 UsePosition* use = current->GetFirstUse();
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001425 SafepointPosition* safepoint_position = interval->GetFirstSafepoint();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001426
1427 // Walk over all siblings, updating locations of use positions, and
1428 // connecting them when they are adjacent.
1429 do {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001430 Location source = current->ToLocation();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001431
1432 // Walk over all uses covered by this interval, and update the location
1433 // information.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001434
1435 LiveRange* range = current->GetFirstRange();
1436 while (range != nullptr) {
1437 while (use != nullptr && use->GetPosition() < range->GetStart()) {
1438 DCHECK(use->GetIsEnvironment());
1439 use = use->GetNext();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001440 }
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001441 while (use != nullptr && use->GetPosition() <= range->GetEnd()) {
1442 DCHECK(current->Covers(use->GetPosition()) || (use->GetPosition() == range->GetEnd()));
1443 LocationSummary* locations = use->GetUser()->GetLocations();
1444 if (use->GetIsEnvironment()) {
1445 locations->SetEnvironmentAt(use->GetInputIndex(), source);
1446 } else {
1447 Location expected_location = locations->InAt(use->GetInputIndex());
1448 // The expected (actual) location may be invalid in case the input is unused. Currently
1449 // this only happens for intrinsics.
1450 if (expected_location.IsValid()) {
1451 if (expected_location.IsUnallocated()) {
1452 locations->SetInAt(use->GetInputIndex(), source);
1453 } else if (!expected_location.IsConstant()) {
1454 AddInputMoveFor(interval->GetDefinedBy(), use->GetUser(), source, expected_location);
1455 }
1456 } else {
1457 DCHECK(use->GetUser()->IsInvoke());
1458 DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
1459 }
1460 }
1461 use = use->GetNext();
1462 }
1463 range = range->GetNext();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001464 }
1465
1466 // If the next interval starts just after this one, and has a register,
1467 // insert a move.
1468 LiveInterval* next_sibling = current->GetNextSibling();
1469 if (next_sibling != nullptr
1470 && next_sibling->HasRegister()
1471 && current->GetEnd() == next_sibling->GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001472 Location destination = next_sibling->ToLocation();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001473 InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001474 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001475
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001476 for (; safepoint_position != nullptr; safepoint_position = safepoint_position->GetNext()) {
1477 if (!current->Covers(safepoint_position->GetPosition())) {
1478 DCHECK(next_sibling != nullptr);
David Brazdil9a9ab612015-03-03 10:44:24 +00001479 break;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001480 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001481
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001482 LocationSummary* locations = safepoint_position->GetLocations();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001483 if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001484 locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
1485 }
1486
1487 switch (source.GetKind()) {
1488 case Location::kRegister: {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001489 locations->AddLiveRegister(source);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001490 if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) {
1491 DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1492 maximum_number_of_live_core_registers_ +
1493 maximum_number_of_live_fp_registers_);
1494 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001495 if (current->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001496 locations->SetRegisterBit(source.reg());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001497 }
1498 break;
1499 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001500 case Location::kFpuRegister: {
1501 locations->AddLiveRegister(source);
1502 break;
1503 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001504
1505 case Location::kRegisterPair:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001506 case Location::kFpuRegisterPair: {
1507 locations->AddLiveRegister(source.ToLow());
1508 locations->AddLiveRegister(source.ToHigh());
1509 break;
1510 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001511 case Location::kStackSlot: // Fall-through
1512 case Location::kDoubleStackSlot: // Fall-through
1513 case Location::kConstant: {
1514 // Nothing to do.
1515 break;
1516 }
1517 default: {
1518 LOG(FATAL) << "Unexpected location for object";
1519 }
1520 }
1521 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001522 current = next_sibling;
1523 } while (current != nullptr);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001524
1525 if (kIsDebugBuild) {
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001526 DCHECK(safepoint_position == nullptr);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001527 // Following uses can only be environment uses. The location for
1528 // these environments will be none.
1529 while (use != nullptr) {
1530 DCHECK(use->GetIsEnvironment());
1531 use = use->GetNext();
1532 }
Nicolas Geoffray5aab7cb2015-03-31 23:06:28 +01001533 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001534}
1535
1536void RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
1537 HBasicBlock* from,
1538 HBasicBlock* to) const {
1539 if (interval->GetNextSibling() == nullptr) {
1540 // Nothing to connect. The whole range was allocated to the same location.
1541 return;
1542 }
1543
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001544 // Intervals end at the lifetime end of a block. The decrement by one
1545 // ensures the `Cover` call will return true.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001546 size_t from_position = from->GetLifetimeEnd() - 1;
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001547 size_t to_position = to->GetLifetimeStart();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001548
1549 LiveInterval* destination = nullptr;
1550 LiveInterval* source = nullptr;
1551
1552 LiveInterval* current = interval;
1553
1554 // Check the intervals that cover `from` and `to`.
1555 while ((current != nullptr) && (source == nullptr || destination == nullptr)) {
1556 if (current->Covers(from_position)) {
1557 DCHECK(source == nullptr);
1558 source = current;
1559 }
1560 if (current->Covers(to_position)) {
1561 DCHECK(destination == nullptr);
1562 destination = current;
1563 }
1564
1565 current = current->GetNextSibling();
1566 }
1567
1568 if (destination == source) {
1569 // Interval was not split.
1570 return;
1571 }
1572
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +01001573 DCHECK(destination != nullptr && source != nullptr);
1574
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001575 if (!destination->HasRegister()) {
1576 // Values are eagerly spilled. Spill slot already contains appropriate value.
1577 return;
1578 }
1579
1580 // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
1581 // we need to put the moves at the entry of `to`.
1582 if (from->GetSuccessors().Size() == 1) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001583 InsertParallelMoveAtExitOf(from,
1584 interval->GetParent()->GetDefinedBy(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001585 source->ToLocation(),
1586 destination->ToLocation());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001587 } else {
1588 DCHECK_EQ(to->GetPredecessors().Size(), 1u);
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001589 InsertParallelMoveAtEntryOf(to,
1590 interval->GetParent()->GetDefinedBy(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001591 source->ToLocation(),
1592 destination->ToLocation());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001593 }
1594}
1595
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001596void RegisterAllocator::Resolve() {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001597 codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(),
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +00001598 maximum_number_of_live_core_registers_,
1599 maximum_number_of_live_fp_registers_,
1600 reserved_out_slots_,
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001601 codegen_->GetGraph()->GetLinearOrder());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001602
1603 // Adjust the Out Location of instructions.
1604 // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
1605 for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
1606 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
1607 LiveInterval* current = instruction->GetLiveInterval();
1608 LocationSummary* locations = instruction->GetLocations();
1609 Location location = locations->Out();
Roland Levillain476df552014-10-09 17:51:36 +01001610 if (instruction->IsParameterValue()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001611 // Now that we know the frame size, adjust the parameter's location.
1612 if (location.IsStackSlot()) {
1613 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1614 current->SetSpillSlot(location.GetStackIndex());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001615 locations->UpdateOut(location);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001616 } else if (location.IsDoubleStackSlot()) {
1617 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1618 current->SetSpillSlot(location.GetStackIndex());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001619 locations->UpdateOut(location);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001620 } else if (current->HasSpillSlot()) {
1621 current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
1622 }
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001623 } else if (current->HasSpillSlot()) {
1624 // Adjust the stack slot, now that we know the number of them for each type.
1625 // The way this implementation lays out the stack is the following:
1626 // [parameter slots ]
1627 // [double spill slots ]
1628 // [long spill slots ]
1629 // [float spill slots ]
1630 // [int/ref values ]
1631 // [maximum out values ] (number of arguments for calls)
1632 // [art method ].
1633 uint32_t slot = current->GetSpillSlot();
1634 switch (current->GetType()) {
1635 case Primitive::kPrimDouble:
1636 slot += long_spill_slots_.Size();
1637 FALLTHROUGH_INTENDED;
1638 case Primitive::kPrimLong:
1639 slot += float_spill_slots_.Size();
1640 FALLTHROUGH_INTENDED;
1641 case Primitive::kPrimFloat:
1642 slot += int_spill_slots_.Size();
1643 FALLTHROUGH_INTENDED;
1644 case Primitive::kPrimNot:
1645 case Primitive::kPrimInt:
1646 case Primitive::kPrimChar:
1647 case Primitive::kPrimByte:
1648 case Primitive::kPrimBoolean:
1649 case Primitive::kPrimShort:
1650 slot += reserved_out_slots_;
1651 break;
1652 case Primitive::kPrimVoid:
1653 LOG(FATAL) << "Unexpected type for interval " << current->GetType();
1654 }
1655 current->SetSpillSlot(slot * kVRegSize);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001656 }
1657
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001658 Location source = current->ToLocation();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001659
1660 if (location.IsUnallocated()) {
1661 if (location.GetPolicy() == Location::kSameAsFirstInput) {
Calin Juravled0d48522014-11-04 16:40:20 +00001662 if (locations->InAt(0).IsUnallocated()) {
1663 locations->SetInAt(0, source);
1664 } else {
1665 DCHECK(locations->InAt(0).Equals(source));
1666 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001667 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001668 locations->UpdateOut(source);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001669 } else {
1670 DCHECK(source.Equals(location));
1671 }
1672 }
1673
1674 // Connect siblings.
1675 for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
1676 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
1677 ConnectSiblings(instruction->GetLiveInterval());
1678 }
1679
1680 // Resolve non-linear control flow across branches. Order does not matter.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001681 for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001682 HBasicBlock* block = it.Current();
1683 BitVector* live = liveness_.GetLiveInSet(*block);
1684 for (uint32_t idx : live->Indexes()) {
1685 HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
1686 LiveInterval* interval = current->GetLiveInterval();
1687 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
1688 ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
1689 }
1690 }
1691 }
1692
1693 // Resolve phi inputs. Order does not matter.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001694 for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001695 HBasicBlock* current = it.Current();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001696 for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1697 HInstruction* phi = inst_it.Current();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001698 for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
1699 HBasicBlock* predecessor = current->GetPredecessors().Get(i);
1700 DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
1701 HInstruction* input = phi->InputAt(i);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001702 Location source = input->GetLiveInterval()->GetLocationAt(
1703 predecessor->GetLifetimeEnd() - 1);
1704 Location destination = phi->GetLiveInterval()->ToLocation();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001705 InsertParallelMoveAtExitOf(predecessor, phi, source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001706 }
1707 }
1708 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001709
1710 // Assign temp locations.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001711 for (size_t i = 0; i < temp_intervals_.Size(); ++i) {
1712 LiveInterval* temp = temp_intervals_.Get(i);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001713 if (temp->IsHighInterval()) {
1714 // High intervals can be skipped, they are already handled by the low interval.
1715 continue;
1716 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001717 HInstruction* at = liveness_.GetTempUser(temp);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001718 size_t temp_index = liveness_.GetTempIndex(temp);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001719 LocationSummary* locations = at->GetLocations();
Roland Levillain5368c212014-11-27 15:03:41 +00001720 switch (temp->GetType()) {
1721 case Primitive::kPrimInt:
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001722 locations->SetTempAt(temp_index, Location::RegisterLocation(temp->GetRegister()));
Roland Levillain5368c212014-11-27 15:03:41 +00001723 break;
1724
1725 case Primitive::kPrimDouble:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001726 if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1727 Location location = Location::FpuRegisterPairLocation(
1728 temp->GetRegister(), temp->GetHighInterval()->GetRegister());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001729 locations->SetTempAt(temp_index, location);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001730 } else {
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001731 locations->SetTempAt(temp_index, Location::FpuRegisterLocation(temp->GetRegister()));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001732 }
Roland Levillain5368c212014-11-27 15:03:41 +00001733 break;
1734
1735 default:
1736 LOG(FATAL) << "Unexpected type for temporary location "
1737 << temp->GetType();
1738 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001739 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001740}
1741
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001742} // namespace art