blob: 925099ade6e7f1f364246f6545c4bd0f696deac0 [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 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100335 current->ResetSearchCache();
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100336
Nicolas Geoffray39468442014-09-02 15:17:15 +0100337 // Some instructions define their output in fixed register/stack slot. We need
338 // to ensure we know these locations before doing register allocation. For a
339 // given register, we create an interval that covers these locations. The register
340 // will be unavailable at these locations when trying to allocate one for an
341 // interval.
342 //
343 // The backwards walking ensures the ranges are ordered on increasing start positions.
344 Location output = locations->Out();
Calin Juravled0d48522014-11-04 16:40:20 +0000345 if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
346 Location first = locations->InAt(0);
347 if (first.IsRegister() || first.IsFpuRegister()) {
348 current->SetFrom(position + 1);
349 current->SetRegister(first.reg());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000350 } else if (first.IsPair()) {
351 current->SetFrom(position + 1);
352 current->SetRegister(first.low());
353 LiveInterval* high = current->GetHighInterval();
354 high->SetRegister(first.high());
355 high->SetFrom(position + 1);
Calin Juravled0d48522014-11-04 16:40:20 +0000356 }
357 } else if (output.IsRegister() || output.IsFpuRegister()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100358 // Shift the interval's start by one to account for the blocked register.
359 current->SetFrom(position + 1);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100360 current->SetRegister(output.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100361 BlockRegister(output, position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000362 } else if (output.IsPair()) {
363 current->SetFrom(position + 1);
364 current->SetRegister(output.low());
365 LiveInterval* high = current->GetHighInterval();
366 high->SetRegister(output.high());
367 high->SetFrom(position + 1);
368 BlockRegister(output.ToLow(), position, position + 1);
369 BlockRegister(output.ToHigh(), position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100370 } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
371 current->SetSpillSlot(output.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000372 } else {
373 DCHECK(output.IsUnallocated() || output.IsConstant());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100374 }
375
376 // If needed, add interval to the list of unhandled intervals.
377 if (current->HasSpillSlot() || instruction->IsConstant()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100378 // Split just before first register use.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100379 size_t first_register_use = current->FirstRegisterUse();
380 if (first_register_use != kNoLifetime) {
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +0100381 LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000382 // Don't add directly to `unhandled`, it needs to be sorted and the start
Nicolas Geoffray39468442014-09-02 15:17:15 +0100383 // of this new interval might be after intervals already in the list.
384 AddSorted(&unhandled, split);
385 } else {
386 // Nothing to do, we won't allocate a register for this value.
387 }
388 } else {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000389 // Don't add directly to `unhandled`, temp or safepoint intervals
390 // for this instruction may have been added, and those can be
391 // processed first.
392 AddSorted(&unhandled, current);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100393 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100394}
395
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100396class AllRangesIterator : public ValueObject {
397 public:
398 explicit AllRangesIterator(LiveInterval* interval)
399 : current_interval_(interval),
400 current_range_(interval->GetFirstRange()) {}
401
402 bool Done() const { return current_interval_ == nullptr; }
403 LiveRange* CurrentRange() const { return current_range_; }
404 LiveInterval* CurrentInterval() const { return current_interval_; }
405
406 void Advance() {
407 current_range_ = current_range_->GetNext();
408 if (current_range_ == nullptr) {
409 current_interval_ = current_interval_->GetNextSibling();
410 if (current_interval_ != nullptr) {
411 current_range_ = current_interval_->GetFirstRange();
412 }
413 }
414 }
415
416 private:
417 LiveInterval* current_interval_;
418 LiveRange* current_range_;
419
420 DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
421};
422
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100423bool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
424 // To simplify unit testing, we eagerly create the array of intervals, and
425 // call the helper method.
426 GrowableArray<LiveInterval*> intervals(allocator_, 0);
427 for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
428 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
429 if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
430 intervals.Add(instruction->GetLiveInterval());
431 }
432 }
433
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100434 if (processing_core_registers_) {
435 for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
436 LiveInterval* fixed = physical_core_register_intervals_.Get(i);
437 if (fixed != nullptr) {
438 intervals.Add(fixed);
439 }
440 }
441 } else {
442 for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
443 LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
444 if (fixed != nullptr) {
445 intervals.Add(fixed);
446 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100447 }
448 }
449
Nicolas Geoffray39468442014-09-02 15:17:15 +0100450 for (size_t i = 0, e = temp_intervals_.Size(); i < e; ++i) {
451 LiveInterval* temp = temp_intervals_.Get(i);
452 if (ShouldProcess(processing_core_registers_, temp)) {
453 intervals.Add(temp);
454 }
455 }
456
Nicolas Geoffray776b3182015-02-23 14:14:57 +0000457 return ValidateIntervals(intervals, GetNumberOfSpillSlots(), reserved_out_slots_, *codegen_,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100458 allocator_, processing_core_registers_, log_fatal_on_failure);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100459}
460
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100461bool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
462 size_t number_of_spill_slots,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100463 size_t number_of_out_slots,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100464 const CodeGenerator& codegen,
465 ArenaAllocator* allocator,
466 bool processing_core_registers,
467 bool log_fatal_on_failure) {
468 size_t number_of_registers = processing_core_registers
469 ? codegen.GetNumberOfCoreRegisters()
470 : codegen.GetNumberOfFloatingPointRegisters();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100471 GrowableArray<ArenaBitVector*> liveness_of_values(
472 allocator, number_of_registers + number_of_spill_slots);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100473
474 // Allocate a bit vector per register. A live interval that has a register
475 // allocated will populate the associated bit vector based on its live ranges.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100476 for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
477 liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100478 }
479
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100480 for (size_t i = 0, e = intervals.Size(); i < e; ++i) {
481 for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) {
482 LiveInterval* current = it.CurrentInterval();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100483 HInstruction* defined_by = current->GetParent()->GetDefinedBy();
484 if (current->GetParent()->HasSpillSlot()
485 // Parameters have their own stack slot.
486 && !(defined_by != nullptr && defined_by->IsParameterValue())) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100487 BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers
488 + current->GetParent()->GetSpillSlot() / kVRegSize
489 - number_of_out_slots);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100490 for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
491 if (liveness_of_spill_slot->IsBitSet(j)) {
492 if (log_fatal_on_failure) {
493 std::ostringstream message;
494 message << "Spill slot conflict at " << j;
495 LOG(FATAL) << message.str();
496 } else {
497 return false;
498 }
499 } else {
500 liveness_of_spill_slot->SetBit(j);
501 }
502 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100503 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100504
505 if (current->HasRegister()) {
506 BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister());
507 for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
508 if (liveness_of_register->IsBitSet(j)) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000509 if (current->IsUsingInputRegister() && current->CanUseInputRegister()) {
510 continue;
511 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100512 if (log_fatal_on_failure) {
513 std::ostringstream message;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100514 message << "Register conflict at " << j << " ";
515 if (defined_by != nullptr) {
516 message << "(" << defined_by->DebugName() << ")";
517 }
518 message << "for ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100519 if (processing_core_registers) {
520 codegen.DumpCoreRegister(message, current->GetRegister());
521 } else {
522 codegen.DumpFloatingPointRegister(message, current->GetRegister());
523 }
524 LOG(FATAL) << message.str();
525 } else {
526 return false;
527 }
528 } else {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100529 liveness_of_register->SetBit(j);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100530 }
531 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100532 }
533 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100534 }
535 return true;
536}
537
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100538void RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100539 interval->Dump(stream);
540 stream << ": ";
541 if (interval->HasRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100542 if (interval->IsFloatingPoint()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100543 codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100544 } else {
545 codegen_->DumpCoreRegister(stream, interval->GetRegister());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100546 }
547 } else {
548 stream << "spilled";
549 }
550 stream << std::endl;
551}
552
Mingyao Yang296bd602014-10-06 16:47:28 -0700553void RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
554 stream << "inactive: " << std::endl;
555 for (size_t i = 0; i < inactive_.Size(); i ++) {
556 DumpInterval(stream, inactive_.Get(i));
557 }
558 stream << "active: " << std::endl;
559 for (size_t i = 0; i < active_.Size(); i ++) {
560 DumpInterval(stream, active_.Get(i));
561 }
562 stream << "unhandled: " << std::endl;
563 auto unhandled = (unhandled_ != nullptr) ?
564 unhandled_ : &unhandled_core_intervals_;
565 for (size_t i = 0; i < unhandled->Size(); i ++) {
566 DumpInterval(stream, unhandled->Get(i));
567 }
568 stream << "handled: " << std::endl;
569 for (size_t i = 0; i < handled_.Size(); i ++) {
570 DumpInterval(stream, handled_.Get(i));
571 }
572}
573
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100574// By the book implementation of a linear scan register allocator.
575void RegisterAllocator::LinearScan() {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100576 while (!unhandled_->IsEmpty()) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100577 // (1) Remove interval with the lowest start position from unhandled.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100578 LiveInterval* current = unhandled_->Pop();
579 DCHECK(!current->IsFixed() && !current->HasSpillSlot());
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100580 DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000581 DCHECK(!current->IsLowInterval() || unhandled_->Peek()->IsHighInterval());
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000582
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100583 size_t position = current->GetStart();
584
Mingyao Yang296bd602014-10-06 16:47:28 -0700585 // Remember the inactive_ size here since the ones moved to inactive_ from
586 // active_ below shouldn't need to be re-checked.
587 size_t inactive_intervals_to_handle = inactive_.Size();
588
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100589 // (2) Remove currently active intervals that are dead at this position.
590 // Move active intervals that have a lifetime hole at this position
591 // to inactive.
592 for (size_t i = 0; i < active_.Size(); ++i) {
593 LiveInterval* interval = active_.Get(i);
594 if (interval->IsDeadAt(position)) {
595 active_.Delete(interval);
596 --i;
597 handled_.Add(interval);
598 } else if (!interval->Covers(position)) {
599 active_.Delete(interval);
600 --i;
601 inactive_.Add(interval);
602 }
603 }
604
605 // (3) Remove currently inactive intervals that are dead at this position.
606 // Move inactive intervals that cover this position to active.
Mingyao Yang296bd602014-10-06 16:47:28 -0700607 for (size_t i = 0; i < inactive_intervals_to_handle; ++i) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100608 LiveInterval* interval = inactive_.Get(i);
Mingyao Yang296bd602014-10-06 16:47:28 -0700609 DCHECK(interval->GetStart() < position || interval->IsFixed());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100610 if (interval->IsDeadAt(position)) {
611 inactive_.Delete(interval);
612 --i;
Mingyao Yang296bd602014-10-06 16:47:28 -0700613 --inactive_intervals_to_handle;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100614 handled_.Add(interval);
615 } else if (interval->Covers(position)) {
616 inactive_.Delete(interval);
617 --i;
Mingyao Yang296bd602014-10-06 16:47:28 -0700618 --inactive_intervals_to_handle;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100619 active_.Add(interval);
620 }
621 }
622
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000623 if (current->IsSlowPathSafepoint()) {
624 // Synthesized interval to record the maximum number of live registers
625 // at safepoints. No need to allocate a register for it.
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500626 if (processing_core_registers_) {
627 maximum_number_of_live_core_registers_ =
628 std::max(maximum_number_of_live_core_registers_, active_.Size());
629 } else {
630 maximum_number_of_live_fp_registers_ =
631 std::max(maximum_number_of_live_fp_registers_, active_.Size());
632 }
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000633 DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() > current->GetStart());
634 continue;
635 }
636
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000637 if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
638 DCHECK(!current->HasRegister());
639 // Allocating the low part was unsucessful. The splitted interval for the high part
640 // will be handled next (it is in the `unhandled_` list).
641 continue;
642 }
643
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100644 // (4) Try to find an available register.
645 bool success = TryAllocateFreeReg(current);
646
647 // (5) If no register could be found, we need to spill.
648 if (!success) {
649 success = AllocateBlockedReg(current);
650 }
651
652 // (6) If the interval had a register allocated, add it to the list of active
653 // intervals.
654 if (success) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000655 codegen_->AddAllocatedRegister(processing_core_registers_
656 ? Location::RegisterLocation(current->GetRegister())
657 : Location::FpuRegisterLocation(current->GetRegister()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100658 active_.Add(current);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000659 if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
660 current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
661 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100662 }
663 }
664}
665
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000666static void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) {
667 DCHECK(!interval->IsHighInterval());
668 // Note that the same instruction may occur multiple times in the input list,
669 // so `free_until` may have changed already.
David Brazdil3fc992f2015-04-16 18:31:55 +0100670 // Since `position` is not the current scan position, we need to use CoversSlow.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000671 if (interval->IsDeadAt(position)) {
672 // Set the register to be free. Note that inactive intervals might later
673 // update this.
674 free_until[interval->GetRegister()] = kMaxLifetimePosition;
675 if (interval->HasHighInterval()) {
676 DCHECK(interval->GetHighInterval()->IsDeadAt(position));
677 free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition;
678 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100679 } else if (!interval->CoversSlow(position)) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000680 // The interval becomes inactive at `defined_by`. We make its register
681 // available only until the next use strictly after `defined_by`.
682 free_until[interval->GetRegister()] = interval->FirstUseAfter(position);
683 if (interval->HasHighInterval()) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100684 DCHECK(!interval->GetHighInterval()->CoversSlow(position));
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000685 free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()];
686 }
687 }
688}
689
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100690// Find a free register. If multiple are found, pick the register that
691// is free the longest.
692bool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
693 size_t* free_until = registers_array_;
694
695 // First set all registers to be free.
696 for (size_t i = 0; i < number_of_registers_; ++i) {
697 free_until[i] = kMaxLifetimePosition;
698 }
699
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100700 // For each active interval, set its register to not free.
701 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
702 LiveInterval* interval = active_.Get(i);
703 DCHECK(interval->HasRegister());
704 free_until[interval->GetRegister()] = 0;
705 }
706
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000707 // An interval that starts an instruction (that is, it is not split), may
708 // re-use the registers used by the inputs of that instruciton, based on the
709 // location summary.
710 HInstruction* defined_by = current->GetDefinedBy();
711 if (defined_by != nullptr && !current->IsSplit()) {
712 LocationSummary* locations = defined_by->GetLocations();
713 if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) {
714 for (HInputIterator it(defined_by); !it.Done(); it.Advance()) {
715 // Take the last interval of the input. It is the location of that interval
716 // that will be used at `defined_by`.
717 LiveInterval* interval = it.Current()->GetLiveInterval()->GetLastSibling();
718 // Note that interval may have not been processed yet.
719 // TODO: Handle non-split intervals last in the work list.
720 if (interval->HasRegister() && interval->SameRegisterKind(*current)) {
721 // The input must be live until the end of `defined_by`, to comply to
722 // the linear scan algorithm. So we use `defined_by`'s end lifetime
723 // position to check whether the input is dead or is inactive after
724 // `defined_by`.
David Brazdil3fc992f2015-04-16 18:31:55 +0100725 DCHECK(interval->CoversSlow(defined_by->GetLifetimePosition()));
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000726 size_t position = defined_by->GetLifetimePosition() + 1;
727 FreeIfNotCoverAt(interval, position, free_until);
728 }
729 }
730 }
731 }
732
Mingyao Yang296bd602014-10-06 16:47:28 -0700733 // For each inactive interval, set its register to be free until
734 // the next intersection with `current`.
735 for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
736 LiveInterval* inactive = inactive_.Get(i);
737 // Temp/Slow-path-safepoint interval has no holes.
738 DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
739 if (!current->IsSplit() && !inactive->IsFixed()) {
740 // Neither current nor inactive are fixed.
741 // Thanks to SSA, a non-split interval starting in a hole of an
742 // inactive interval should never intersect with that inactive interval.
743 // Only if it's not fixed though, because fixed intervals don't come from SSA.
744 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
745 continue;
746 }
747
748 DCHECK(inactive->HasRegister());
749 if (free_until[inactive->GetRegister()] == 0) {
750 // Already used by some active interval. No need to intersect.
751 continue;
752 }
753 size_t next_intersection = inactive->FirstIntersectionWith(current);
754 if (next_intersection != kNoLifetime) {
755 free_until[inactive->GetRegister()] =
756 std::min(free_until[inactive->GetRegister()], next_intersection);
757 }
758 }
759
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000760 int reg = kNoRegister;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100761 if (current->HasRegister()) {
762 // Some instructions have a fixed register output.
763 reg = current->GetRegister();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000764 if (free_until[reg] == 0) {
765 DCHECK(current->IsHighInterval());
766 // AllocateBlockedReg will spill the holder of the register.
767 return false;
768 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100769 } else {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000770 DCHECK(!current->IsHighInterval());
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100771 int hint = current->FindFirstRegisterHint(free_until, liveness_);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100772 if (hint != kNoRegister) {
773 DCHECK(!IsBlocked(hint));
774 reg = hint;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000775 } else if (current->IsLowInterval()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000776 reg = FindAvailableRegisterPair(free_until, current->GetStart());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100777 } else {
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100778 reg = FindAvailableRegister(free_until, current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100779 }
780 }
781
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000782 DCHECK_NE(reg, kNoRegister);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100783 // If we could not find a register, we need to spill.
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000784 if (free_until[reg] == 0) {
785 return false;
786 }
787
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000788 if (current->IsLowInterval()) {
789 // If the high register of this interval is not available, we need to spill.
790 int high_reg = current->GetHighInterval()->GetRegister();
791 if (high_reg == kNoRegister) {
792 high_reg = GetHighForLowRegister(reg);
793 }
794 if (free_until[high_reg] == 0) {
795 return false;
796 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100797 }
798
799 current->SetRegister(reg);
800 if (!current->IsDeadAt(free_until[reg])) {
801 // If the register is only available for a subset of live ranges
802 // covered by `current`, split `current` at the position where
803 // the register is not available anymore.
804 LiveInterval* split = Split(current, free_until[reg]);
805 DCHECK(split != nullptr);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100806 AddSorted(unhandled_, split);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100807 }
808 return true;
809}
810
811bool RegisterAllocator::IsBlocked(int reg) const {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100812 return processing_core_registers_
813 ? blocked_core_registers_[reg]
814 : blocked_fp_registers_[reg];
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100815}
816
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000817int RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const {
818 int reg = kNoRegister;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000819 // Pick the register pair that is used the last.
820 for (size_t i = 0; i < number_of_registers_; ++i) {
821 if (IsBlocked(i)) continue;
822 if (!IsLowRegister(i)) continue;
823 int high_register = GetHighForLowRegister(i);
824 if (IsBlocked(high_register)) continue;
825 int existing_high_register = GetHighForLowRegister(reg);
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 && next_use[high_register] >= next_use[existing_high_register])) {
828 reg = i;
829 if (next_use[i] == kMaxLifetimePosition
830 && next_use[high_register] == kMaxLifetimePosition) {
831 break;
832 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000833 } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) {
834 // If one of the current register is known to be unavailable, just unconditionally
835 // try a new one.
836 reg = i;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000837 }
838 }
839 return reg;
840}
841
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100842bool RegisterAllocator::IsCallerSaveRegister(int reg) const {
843 return processing_core_registers_
844 ? !codegen_->IsCoreCalleeSaveRegister(reg)
845 : !codegen_->IsFloatingPointCalleeSaveRegister(reg);
846}
847
848int RegisterAllocator::FindAvailableRegister(size_t* next_use, LiveInterval* current) const {
849 // We special case intervals that do not span a safepoint to try to find a caller-save
850 // register if one is available. We iterate from 0 to the number of registers,
851 // so if there are caller-save registers available at the end, we continue the iteration.
852 bool prefers_caller_save = !current->HasWillCallSafepoint();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000853 int reg = kNoRegister;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000854 for (size_t i = 0; i < number_of_registers_; ++i) {
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100855 if (IsBlocked(i)) {
856 // Register cannot be used. Continue.
857 continue;
858 }
859
860 // Best case: we found a register fully available.
861 if (next_use[i] == kMaxLifetimePosition) {
862 if (prefers_caller_save && !IsCallerSaveRegister(i)) {
863 // We can get shorter encodings on some platforms by using
864 // small register numbers. So only update the candidate if the previous
865 // one was not available for the whole method.
866 if (reg == kNoRegister || next_use[reg] != kMaxLifetimePosition) {
867 reg = i;
868 }
869 // Continue the iteration in the hope of finding a caller save register.
870 continue;
871 } else {
872 reg = i;
873 // We know the register is good enough. Return it.
874 break;
875 }
876 }
877
878 // If we had no register before, take this one as a reference.
879 if (reg == kNoRegister) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000880 reg = i;
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100881 continue;
882 }
883
884 // Pick the register that is used the last.
885 if (next_use[i] > next_use[reg]) {
886 reg = i;
887 continue;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000888 }
889 }
890 return reg;
891}
892
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000893bool RegisterAllocator::TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
894 size_t first_register_use,
895 size_t* next_use) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000896 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
897 LiveInterval* active = active_.Get(i);
898 DCHECK(active->HasRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000899 if (active->IsFixed()) continue;
900 if (active->IsHighInterval()) continue;
901 if (first_register_use > next_use[active->GetRegister()]) continue;
902
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000903 // Split the first interval found.
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000904 if (!active->IsLowInterval() || IsLowOfUnalignedPairInterval(active)) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000905 LiveInterval* split = Split(active, position);
906 active_.DeleteAt(i);
907 if (split != active) {
908 handled_.Add(active);
909 }
910 AddSorted(unhandled_, split);
911 return true;
912 }
913 }
914 return false;
915}
916
Nicolas Geoffray5b168de2015-03-27 10:27:22 +0000917bool RegisterAllocator::PotentiallyRemoveOtherHalf(LiveInterval* interval,
918 GrowableArray<LiveInterval*>* intervals,
919 size_t index) {
920 if (interval->IsLowInterval()) {
921 DCHECK_EQ(intervals->Get(index), interval->GetHighInterval());
922 intervals->DeleteAt(index);
923 return true;
924 } else if (interval->IsHighInterval()) {
925 DCHECK_GT(index, 0u);
926 DCHECK_EQ(intervals->Get(index - 1), interval->GetLowInterval());
927 intervals->DeleteAt(index - 1);
928 return true;
929 } else {
930 return false;
931 }
932}
933
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100934// Find the register that is used the last, and spill the interval
935// that holds it. If the first use of `current` is after that register
936// we spill `current` instead.
937bool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
938 size_t first_register_use = current->FirstRegisterUse();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100939 if (first_register_use == kNoLifetime) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100940 AllocateSpillSlotFor(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100941 return false;
942 }
943
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100944 // We use the first use to compare with other intervals. If this interval
945 // is used after any active intervals, we will spill this interval.
946 size_t first_use = current->FirstUseAfter(current->GetStart());
947
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100948 // First set all registers as not being used.
949 size_t* next_use = registers_array_;
950 for (size_t i = 0; i < number_of_registers_; ++i) {
951 next_use[i] = kMaxLifetimePosition;
952 }
953
954 // For each active interval, find the next use of its register after the
955 // start of current.
956 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
957 LiveInterval* active = active_.Get(i);
958 DCHECK(active->HasRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100959 if (active->IsFixed()) {
960 next_use[active->GetRegister()] = current->GetStart();
961 } else {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100962 size_t use = active->FirstUseAfter(current->GetStart());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100963 if (use != kNoLifetime) {
964 next_use[active->GetRegister()] = use;
965 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100966 }
967 }
968
969 // For each inactive interval, find the next use of its register after the
970 // start of current.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100971 for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
972 LiveInterval* inactive = inactive_.Get(i);
Mingyao Yang296bd602014-10-06 16:47:28 -0700973 // Temp/Slow-path-safepoint interval has no holes.
974 DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
975 if (!current->IsSplit() && !inactive->IsFixed()) {
976 // Neither current nor inactive are fixed.
977 // Thanks to SSA, a non-split interval starting in a hole of an
978 // inactive interval should never intersect with that inactive interval.
979 // Only if it's not fixed though, because fixed intervals don't come from SSA.
980 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
981 continue;
982 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100983 DCHECK(inactive->HasRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100984 size_t next_intersection = inactive->FirstIntersectionWith(current);
985 if (next_intersection != kNoLifetime) {
986 if (inactive->IsFixed()) {
987 next_use[inactive->GetRegister()] =
988 std::min(next_intersection, next_use[inactive->GetRegister()]);
989 } else {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100990 size_t use = inactive->FirstUseAfter(current->GetStart());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100991 if (use != kNoLifetime) {
992 next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
993 }
994 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100995 }
996 }
997
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000998 int reg = kNoRegister;
999 bool should_spill = false;
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001000 if (current->HasRegister()) {
1001 DCHECK(current->IsHighInterval());
1002 reg = current->GetRegister();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001003 // When allocating the low part, we made sure the high register was available.
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01001004 DCHECK_LT(first_use, next_use[reg]);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001005 } else if (current->IsLowInterval()) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001006 reg = FindAvailableRegisterPair(next_use, first_register_use);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001007 // We should spill if both registers are not available.
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01001008 should_spill = (first_use >= next_use[reg])
1009 || (first_use >= next_use[GetHighForLowRegister(reg)]);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001010 } else {
1011 DCHECK(!current->IsHighInterval());
Nicolas Geoffray8826f672015-04-17 09:15:11 +01001012 reg = FindAvailableRegister(next_use, current);
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01001013 should_spill = (first_use >= next_use[reg]);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001014 }
1015
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001016 DCHECK_NE(reg, kNoRegister);
1017 if (should_spill) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001018 DCHECK(!current->IsHighInterval());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001019 bool is_allocation_at_use_site = (current->GetStart() >= (first_register_use - 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001020 if (current->IsLowInterval()
1021 && is_allocation_at_use_site
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001022 && TrySplitNonPairOrUnalignedPairIntervalAt(current->GetStart(),
1023 first_register_use,
1024 next_use)) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001025 // If we're allocating a register for `current` because the instruction at
1026 // that position requires it, but we think we should spill, then there are
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001027 // non-pair intervals or unaligned pair intervals blocking the allocation.
1028 // We split the first interval found, and put ourselves first in the
1029 // `unhandled_` list.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001030 LiveInterval* existing = unhandled_->Peek();
1031 DCHECK(existing->IsHighInterval());
1032 DCHECK_EQ(existing->GetLowInterval(), current);
1033 unhandled_->Add(current);
1034 } else {
1035 // If the first use of that instruction is after the last use of the found
1036 // register, we split this interval just before its first register use.
1037 AllocateSpillSlotFor(current);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001038 LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001039 if (current == split) {
1040 DumpInterval(std::cerr, current);
1041 DumpAllIntervals(std::cerr);
1042 // This situation has the potential to infinite loop, so we make it a non-debug CHECK.
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01001043 HInstruction* at = liveness_.GetInstructionFromPosition(first_register_use / 2);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001044 CHECK(false) << "There is not enough registers available for "
1045 << split->GetParent()->GetDefinedBy()->DebugName() << " "
1046 << split->GetParent()->GetDefinedBy()->GetId()
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01001047 << " at " << first_register_use - 1 << " "
1048 << (at == nullptr ? "" : at->DebugName());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001049 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001050 AddSorted(unhandled_, split);
1051 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001052 return false;
1053 } else {
1054 // Use this register and spill the active and inactives interval that
1055 // have that register.
1056 current->SetRegister(reg);
1057
1058 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
1059 LiveInterval* active = active_.Get(i);
1060 if (active->GetRegister() == reg) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001061 DCHECK(!active->IsFixed());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001062 LiveInterval* split = Split(active, current->GetStart());
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001063 if (split != active) {
1064 handled_.Add(active);
1065 }
Nicolas Geoffray5b168de2015-03-27 10:27:22 +00001066 active_.DeleteAt(i);
1067 PotentiallyRemoveOtherHalf(active, &active_, i);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001068 AddSorted(unhandled_, split);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001069 break;
1070 }
1071 }
1072
Nicolas Geoffray5b168de2015-03-27 10:27:22 +00001073 for (size_t i = 0; i < inactive_.Size(); ++i) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001074 LiveInterval* inactive = inactive_.Get(i);
1075 if (inactive->GetRegister() == reg) {
Mingyao Yang296bd602014-10-06 16:47:28 -07001076 if (!current->IsSplit() && !inactive->IsFixed()) {
1077 // Neither current nor inactive are fixed.
1078 // Thanks to SSA, a non-split interval starting in a hole of an
1079 // inactive interval should never intersect with that inactive interval.
1080 // Only if it's not fixed though, because fixed intervals don't come from SSA.
1081 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
1082 continue;
1083 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001084 size_t next_intersection = inactive->FirstIntersectionWith(current);
1085 if (next_intersection != kNoLifetime) {
1086 if (inactive->IsFixed()) {
1087 LiveInterval* split = Split(current, next_intersection);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001088 DCHECK_NE(split, current);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001089 AddSorted(unhandled_, split);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001090 } else {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001091 // Split at the start of `current`, which will lead to splitting
1092 // at the end of the lifetime hole of `inactive`.
1093 LiveInterval* split = Split(inactive, current->GetStart());
1094 // If it's inactive, it must start before the current interval.
1095 DCHECK_NE(split, inactive);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001096 inactive_.DeleteAt(i);
Nicolas Geoffray5b168de2015-03-27 10:27:22 +00001097 if (PotentiallyRemoveOtherHalf(inactive, &inactive_, i) && inactive->IsHighInterval()) {
1098 // We have removed an entry prior to `inactive`. So we need to decrement.
1099 --i;
1100 }
1101 // Decrement because we have removed `inactive` from the list.
Mingyao Yang296bd602014-10-06 16:47:28 -07001102 --i;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001103 handled_.Add(inactive);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001104 AddSorted(unhandled_, split);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001105 }
1106 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001107 }
1108 }
1109
1110 return true;
1111 }
1112}
1113
Nicolas Geoffray39468442014-09-02 15:17:15 +01001114void RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01001115 DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001116 size_t insert_at = 0;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001117 for (size_t i = array->Size(); i > 0; --i) {
1118 LiveInterval* current = array->Get(i - 1);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001119 // High intervals must be processed right after their low equivalent.
1120 if (current->StartsAfter(interval) && !current->IsHighInterval()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001121 insert_at = i;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001122 break;
Nicolas Geoffrayacd03392014-11-26 15:46:52 +00001123 } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
1124 // Ensure the slow path interval is the last to be processed at its location: we want the
1125 // interval to know all live registers at this location.
1126 DCHECK(i == 1 || array->Get(i - 2)->StartsAfter(current));
1127 insert_at = i;
1128 break;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001129 }
1130 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001131
Nicolas Geoffray39468442014-09-02 15:17:15 +01001132 array->InsertAt(insert_at, interval);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001133 // Insert the high interval before the low, to ensure the low is processed before.
1134 if (interval->HasHighInterval()) {
1135 array->InsertAt(insert_at, interval->GetHighInterval());
1136 } else if (interval->HasLowInterval()) {
1137 array->InsertAt(insert_at + 1, interval->GetLowInterval());
1138 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001139}
1140
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001141LiveInterval* RegisterAllocator::SplitBetween(LiveInterval* interval, size_t from, size_t to) {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001142 HBasicBlock* block_from = liveness_.GetBlockFromPosition(from / 2);
1143 HBasicBlock* block_to = liveness_.GetBlockFromPosition(to / 2);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001144 DCHECK(block_from != nullptr);
1145 DCHECK(block_to != nullptr);
1146
1147 // Both locations are in the same block. We split at the given location.
1148 if (block_from == block_to) {
1149 return Split(interval, to);
1150 }
1151
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001152 /*
1153 * Non-linear control flow will force moves at every branch instruction to the new location.
1154 * To avoid having all branches doing the moves, we find the next non-linear position and
1155 * split the interval at this position. Take the following example (block number is the linear
1156 * order position):
1157 *
1158 * B1
1159 * / \
1160 * B2 B3
1161 * \ /
1162 * B4
1163 *
1164 * B2 needs to split an interval, whose next use is in B4. If we were to split at the
1165 * beginning of B4, B3 would need to do a move between B3 and B4 to ensure the interval
1166 * is now in the correct location. It makes performance worst if the interval is spilled
1167 * and both B2 and B3 need to reload it before entering B4.
1168 *
1169 * By splitting at B3, we give a chance to the register allocator to allocate the
1170 * interval to the same register as in B1, and therefore avoid doing any
1171 * moves in B3.
1172 */
1173 if (block_from->GetDominator() != nullptr) {
1174 const GrowableArray<HBasicBlock*>& dominated = block_from->GetDominator()->GetDominatedBlocks();
1175 for (size_t i = 0; i < dominated.Size(); ++i) {
1176 size_t position = dominated.Get(i)->GetLifetimeStart();
1177 if ((position > from) && (block_to->GetLifetimeStart() > position)) {
1178 // Even if we found a better block, we continue iterating in case
1179 // a dominated block is closer.
1180 // Note that dominated blocks are not sorted in liveness order.
1181 block_to = dominated.Get(i);
1182 DCHECK_NE(block_to, block_from);
1183 }
1184 }
1185 }
1186
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001187 // If `to` is in a loop, find the outermost loop header which does not contain `from`.
1188 for (HLoopInformationOutwardIterator it(*block_to); !it.Done(); it.Advance()) {
1189 HBasicBlock* header = it.Current()->GetHeader();
1190 if (block_from->GetLifetimeStart() >= header->GetLifetimeStart()) {
1191 break;
1192 }
1193 block_to = header;
1194 }
1195
1196 // Split at the start of the found block, to piggy back on existing moves
1197 // due to resolution if non-linear control flow (see `ConnectSplitSiblings`).
1198 return Split(interval, block_to->GetLifetimeStart());
1199}
1200
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001201LiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001202 DCHECK_GE(position, interval->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001203 DCHECK(!interval->IsDeadAt(position));
1204 if (position == interval->GetStart()) {
1205 // Spill slot will be allocated when handling `interval` again.
1206 interval->ClearRegister();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001207 if (interval->HasHighInterval()) {
1208 interval->GetHighInterval()->ClearRegister();
1209 } else if (interval->HasLowInterval()) {
1210 interval->GetLowInterval()->ClearRegister();
1211 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001212 return interval;
1213 } else {
1214 LiveInterval* new_interval = interval->SplitAt(position);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001215 if (interval->HasHighInterval()) {
1216 LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
1217 new_interval->SetHighInterval(high);
1218 high->SetLowInterval(new_interval);
1219 } else if (interval->HasLowInterval()) {
1220 LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
1221 new_interval->SetLowInterval(low);
1222 low->SetHighInterval(new_interval);
1223 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001224 return new_interval;
1225 }
1226}
1227
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001228void RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001229 if (interval->IsHighInterval()) {
1230 // The low interval will contain the spill slot.
1231 return;
1232 }
1233
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001234 LiveInterval* parent = interval->GetParent();
1235
1236 // An instruction gets a spill slot for its entire lifetime. If the parent
1237 // of this interval already has a spill slot, there is nothing to do.
1238 if (parent->HasSpillSlot()) {
1239 return;
1240 }
1241
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001242 HInstruction* defined_by = parent->GetDefinedBy();
1243 if (defined_by->IsParameterValue()) {
1244 // Parameters have their own stack slot.
1245 parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
1246 return;
1247 }
1248
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001249 if (defined_by->IsConstant()) {
1250 // Constants don't need a spill slot.
1251 return;
1252 }
1253
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001254 LiveInterval* last_sibling = interval;
1255 while (last_sibling->GetNextSibling() != nullptr) {
1256 last_sibling = last_sibling->GetNextSibling();
1257 }
1258 size_t end = last_sibling->GetEnd();
1259
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001260 GrowableArray<size_t>* spill_slots = nullptr;
1261 switch (interval->GetType()) {
1262 case Primitive::kPrimDouble:
1263 spill_slots = &double_spill_slots_;
1264 break;
1265 case Primitive::kPrimLong:
1266 spill_slots = &long_spill_slots_;
1267 break;
1268 case Primitive::kPrimFloat:
1269 spill_slots = &float_spill_slots_;
1270 break;
1271 case Primitive::kPrimNot:
1272 case Primitive::kPrimInt:
1273 case Primitive::kPrimChar:
1274 case Primitive::kPrimByte:
1275 case Primitive::kPrimBoolean:
1276 case Primitive::kPrimShort:
1277 spill_slots = &int_spill_slots_;
1278 break;
1279 case Primitive::kPrimVoid:
1280 LOG(FATAL) << "Unexpected type for interval " << interval->GetType();
1281 }
1282
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001283 // Find an available spill slot.
1284 size_t slot = 0;
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001285 for (size_t e = spill_slots->Size(); slot < e; ++slot) {
1286 if (spill_slots->Get(slot) <= parent->GetStart()
1287 && (slot == (e - 1) || spill_slots->Get(slot + 1) <= parent->GetStart())) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001288 break;
1289 }
1290 }
1291
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001292 if (parent->NeedsTwoSpillSlots()) {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001293 if (slot == spill_slots->Size()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001294 // We need a new spill slot.
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001295 spill_slots->Add(end);
1296 spill_slots->Add(end);
1297 } else if (slot == spill_slots->Size() - 1) {
1298 spill_slots->Put(slot, end);
1299 spill_slots->Add(end);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001300 } else {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001301 spill_slots->Put(slot, end);
1302 spill_slots->Put(slot + 1, end);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001303 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001304 } else {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001305 if (slot == spill_slots->Size()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001306 // We need a new spill slot.
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001307 spill_slots->Add(end);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001308 } else {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001309 spill_slots->Put(slot, end);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001310 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001311 }
1312
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001313 // Note that the exact spill slot location will be computed when we resolve,
1314 // that is when we know the number of spill slots for each type.
1315 parent->SetSpillSlot(slot);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001316}
1317
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001318static bool IsValidDestination(Location destination) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001319 return destination.IsRegister()
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001320 || destination.IsRegisterPair()
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001321 || destination.IsFpuRegister()
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001322 || destination.IsFpuRegisterPair()
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001323 || destination.IsStackSlot()
1324 || destination.IsDoubleStackSlot();
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001325}
1326
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001327void RegisterAllocator::AddMove(HParallelMove* move,
1328 Location source,
1329 Location destination,
1330 HInstruction* instruction,
1331 Primitive::Type type) const {
1332 if (type == Primitive::kPrimLong
1333 && codegen_->ShouldSplitLongMoves()
1334 // The parallel move resolver knows how to deal with long constants.
1335 && !source.IsConstant()) {
Nicolas Geoffray90218252015-04-15 11:56:51 +01001336 move->AddMove(source.ToLow(), destination.ToLow(), Primitive::kPrimInt, instruction);
1337 move->AddMove(source.ToHigh(), destination.ToHigh(), Primitive::kPrimInt, nullptr);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001338 } else {
Nicolas Geoffray90218252015-04-15 11:56:51 +01001339 move->AddMove(source, destination, type, instruction);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001340 }
1341}
1342
1343void RegisterAllocator::AddInputMoveFor(HInstruction* input,
1344 HInstruction* user,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001345 Location source,
1346 Location destination) const {
1347 if (source.Equals(destination)) return;
1348
Roland Levillain476df552014-10-09 17:51:36 +01001349 DCHECK(!user->IsPhi());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001350
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001351 HInstruction* previous = user->GetPrevious();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001352 HParallelMove* move = nullptr;
1353 if (previous == nullptr
Roland Levillain476df552014-10-09 17:51:36 +01001354 || !previous->IsParallelMove()
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001355 || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001356 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001357 move->SetLifetimePosition(user->GetLifetimePosition());
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001358 user->GetBlock()->InsertInstructionBefore(move, user);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001359 } else {
1360 move = previous->AsParallelMove();
1361 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001362 DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001363 AddMove(move, source, destination, nullptr, input->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001364}
1365
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001366static bool IsInstructionStart(size_t position) {
1367 return (position & 1) == 0;
1368}
1369
1370static bool IsInstructionEnd(size_t position) {
1371 return (position & 1) == 1;
1372}
1373
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001374void RegisterAllocator::InsertParallelMoveAt(size_t position,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001375 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001376 Location source,
1377 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001378 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001379 if (source.Equals(destination)) return;
1380
1381 HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001382 HParallelMove* move;
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001383 if (at == nullptr) {
1384 if (IsInstructionStart(position)) {
1385 // Block boundary, don't do anything the connection of split siblings will handle it.
1386 return;
1387 } else {
1388 // Move must happen before the first instruction of the block.
1389 at = liveness_.GetInstructionFromPosition((position + 1) / 2);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001390 // Note that parallel moves may have already been inserted, so we explicitly
1391 // ask for the first instruction of the block: `GetInstructionFromPosition` does
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001392 // not contain the `HParallelMove` instructions.
Nicolas Geoffray59768572014-12-01 09:50:04 +00001393 at = at->GetBlock()->GetFirstInstruction();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001394
1395 if (at->GetLifetimePosition() < position) {
1396 // We may insert moves for split siblings and phi spills at the beginning of the block.
1397 // Since this is a different lifetime position, we need to go to the next instruction.
1398 DCHECK(at->IsParallelMove());
1399 at = at->GetNext();
1400 }
1401
Nicolas Geoffray59768572014-12-01 09:50:04 +00001402 if (at->GetLifetimePosition() != position) {
1403 DCHECK_GT(at->GetLifetimePosition(), position);
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001404 move = new (allocator_) HParallelMove(allocator_);
1405 move->SetLifetimePosition(position);
1406 at->GetBlock()->InsertInstructionBefore(move, at);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001407 } else {
1408 DCHECK(at->IsParallelMove());
1409 move = at->AsParallelMove();
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001410 }
1411 }
1412 } else if (IsInstructionEnd(position)) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001413 // Move must happen after the instruction.
1414 DCHECK(!at->IsControlFlow());
1415 move = at->GetNext()->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001416 // This is a parallel move for connecting siblings in a same block. We need to
1417 // differentiate it with moves for connecting blocks, and input moves.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001418 if (move == nullptr || move->GetLifetimePosition() > position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001419 move = new (allocator_) HParallelMove(allocator_);
1420 move->SetLifetimePosition(position);
1421 at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
1422 }
1423 } else {
1424 // Move must happen before the instruction.
1425 HInstruction* previous = at->GetPrevious();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001426 if (previous == nullptr
1427 || !previous->IsParallelMove()
1428 || previous->GetLifetimePosition() != position) {
1429 // If the previous is a parallel move, then its position must be lower
1430 // than the given `position`: it was added just after the non-parallel
1431 // move instruction that precedes `instruction`.
1432 DCHECK(previous == nullptr
1433 || !previous->IsParallelMove()
1434 || previous->GetLifetimePosition() < position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001435 move = new (allocator_) HParallelMove(allocator_);
1436 move->SetLifetimePosition(position);
1437 at->GetBlock()->InsertInstructionBefore(move, at);
1438 } else {
1439 move = previous->AsParallelMove();
1440 }
1441 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001442 DCHECK_EQ(move->GetLifetimePosition(), position);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001443 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001444}
1445
1446void RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001447 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001448 Location source,
1449 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001450 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001451 if (source.Equals(destination)) return;
1452
1453 DCHECK_EQ(block->GetSuccessors().Size(), 1u);
1454 HInstruction* last = block->GetLastInstruction();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001455 // We insert moves at exit for phi predecessors and connecting blocks.
1456 // A block ending with an if cannot branch to a block with phis because
1457 // we do not allow critical edges. It can also not connect
1458 // a split interval between two blocks: the move has to happen in the successor.
1459 DCHECK(!last->IsIf());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001460 HInstruction* previous = last->GetPrevious();
1461 HParallelMove* move;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001462 // This is a parallel move for connecting blocks. We need to differentiate
1463 // it with moves for connecting siblings in a same block, and output moves.
Nicolas Geoffray59768572014-12-01 09:50:04 +00001464 size_t position = last->GetLifetimePosition();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001465 if (previous == nullptr || !previous->IsParallelMove()
Nicolas Geoffray59768572014-12-01 09:50:04 +00001466 || previous->AsParallelMove()->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001467 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001468 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001469 block->InsertInstructionBefore(move, last);
1470 } else {
1471 move = previous->AsParallelMove();
1472 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001473 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001474}
1475
1476void RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001477 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001478 Location source,
1479 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001480 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001481 if (source.Equals(destination)) return;
1482
1483 HInstruction* first = block->GetFirstInstruction();
1484 HParallelMove* move = first->AsParallelMove();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001485 size_t position = block->GetLifetimeStart();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001486 // This is a parallel move for connecting blocks. We need to differentiate
1487 // it with moves for connecting siblings in a same block, and input moves.
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001488 if (move == nullptr || move->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001489 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001490 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001491 block->InsertInstructionBefore(move, first);
1492 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001493 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001494}
1495
1496void RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
1497 Location source,
1498 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001499 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001500 if (source.Equals(destination)) return;
1501
Roland Levillain476df552014-10-09 17:51:36 +01001502 if (instruction->IsPhi()) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001503 InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001504 return;
1505 }
1506
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001507 size_t position = instruction->GetLifetimePosition() + 1;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001508 HParallelMove* move = instruction->GetNext()->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001509 // This is a parallel move for moving the output of an instruction. We need
1510 // to differentiate with input moves, moves for connecting siblings in a
1511 // and moves for connecting blocks.
1512 if (move == nullptr || move->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001513 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001514 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001515 instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
1516 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001517 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001518}
1519
1520void RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
1521 LiveInterval* current = interval;
1522 if (current->HasSpillSlot() && current->HasRegister()) {
1523 // We spill eagerly, so move must be at definition.
1524 InsertMoveAfter(interval->GetDefinedBy(),
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001525 interval->ToLocation(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001526 interval->NeedsTwoSpillSlots()
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001527 ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1528 : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001529 }
1530 UsePosition* use = current->GetFirstUse();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001531 UsePosition* env_use = current->GetFirstEnvironmentUse();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001532
1533 // Walk over all siblings, updating locations of use positions, and
1534 // connecting them when they are adjacent.
1535 do {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001536 Location source = current->ToLocation();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001537
1538 // Walk over all uses covered by this interval, and update the location
1539 // information.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001540
1541 LiveRange* range = current->GetFirstRange();
1542 while (range != nullptr) {
Nicolas Geoffray57902602015-04-21 14:28:41 +01001543 while (use != nullptr && use->GetPosition() < range->GetStart()) {
1544 DCHECK(use->IsSynthesized());
1545 use = use->GetNext();
1546 }
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001547 while (use != nullptr && use->GetPosition() <= range->GetEnd()) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001548 DCHECK(!use->GetIsEnvironment());
David Brazdil3fc992f2015-04-16 18:31:55 +01001549 DCHECK(current->CoversSlow(use->GetPosition()) || (use->GetPosition() == range->GetEnd()));
Nicolas Geoffray57902602015-04-21 14:28:41 +01001550 if (!use->IsSynthesized()) {
1551 LocationSummary* locations = use->GetUser()->GetLocations();
1552 Location expected_location = locations->InAt(use->GetInputIndex());
1553 // The expected (actual) location may be invalid in case the input is unused. Currently
1554 // this only happens for intrinsics.
1555 if (expected_location.IsValid()) {
1556 if (expected_location.IsUnallocated()) {
1557 locations->SetInAt(use->GetInputIndex(), source);
1558 } else if (!expected_location.IsConstant()) {
1559 AddInputMoveFor(interval->GetDefinedBy(), use->GetUser(), source, expected_location);
1560 }
1561 } else {
1562 DCHECK(use->GetUser()->IsInvoke());
1563 DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001564 }
1565 }
1566 use = use->GetNext();
1567 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001568
1569 // Walk over the environment uses, and update their locations.
1570 while (env_use != nullptr && env_use->GetPosition() < range->GetStart()) {
1571 env_use = env_use->GetNext();
1572 }
1573
1574 while (env_use != nullptr && env_use->GetPosition() <= range->GetEnd()) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001575 DCHECK(current->CoversSlow(env_use->GetPosition())
1576 || (env_use->GetPosition() == range->GetEnd()));
1577 HEnvironment* environment = env_use->GetUser()->GetEnvironment();
1578 environment->SetLocationAt(env_use->GetInputIndex(), source);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001579 env_use = env_use->GetNext();
1580 }
1581
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001582 range = range->GetNext();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001583 }
1584
1585 // If the next interval starts just after this one, and has a register,
1586 // insert a move.
1587 LiveInterval* next_sibling = current->GetNextSibling();
1588 if (next_sibling != nullptr
1589 && next_sibling->HasRegister()
1590 && current->GetEnd() == next_sibling->GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001591 Location destination = next_sibling->ToLocation();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001592 InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001593 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001594
Nicolas Geoffray43af7282015-04-16 13:01:01 +01001595 for (SafepointPosition* safepoint_position = current->GetFirstSafepoint();
1596 safepoint_position != nullptr;
1597 safepoint_position = safepoint_position->GetNext()) {
David Brazdil3fc992f2015-04-16 18:31:55 +01001598 DCHECK(current->CoversSlow(safepoint_position->GetPosition()));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001599
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001600 LocationSummary* locations = safepoint_position->GetLocations();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001601 if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001602 locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
1603 }
1604
1605 switch (source.GetKind()) {
1606 case Location::kRegister: {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001607 locations->AddLiveRegister(source);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001608 if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) {
1609 DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1610 maximum_number_of_live_core_registers_ +
1611 maximum_number_of_live_fp_registers_);
1612 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001613 if (current->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001614 locations->SetRegisterBit(source.reg());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001615 }
1616 break;
1617 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001618 case Location::kFpuRegister: {
1619 locations->AddLiveRegister(source);
1620 break;
1621 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001622
1623 case Location::kRegisterPair:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001624 case Location::kFpuRegisterPair: {
1625 locations->AddLiveRegister(source.ToLow());
1626 locations->AddLiveRegister(source.ToHigh());
1627 break;
1628 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001629 case Location::kStackSlot: // Fall-through
1630 case Location::kDoubleStackSlot: // Fall-through
1631 case Location::kConstant: {
1632 // Nothing to do.
1633 break;
1634 }
1635 default: {
1636 LOG(FATAL) << "Unexpected location for object";
1637 }
1638 }
1639 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001640 current = next_sibling;
1641 } while (current != nullptr);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001642
Nicolas Geoffray57902602015-04-21 14:28:41 +01001643 if (kIsDebugBuild) {
1644 // Following uses can only be synthesized uses.
1645 while (use != nullptr) {
1646 DCHECK(use->IsSynthesized());
1647 use = use->GetNext();
1648 }
1649 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001650}
1651
1652void RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
1653 HBasicBlock* from,
1654 HBasicBlock* to) const {
1655 if (interval->GetNextSibling() == nullptr) {
1656 // Nothing to connect. The whole range was allocated to the same location.
1657 return;
1658 }
1659
David Brazdil241a4862015-04-16 17:59:03 +01001660 // Find the intervals that cover `from` and `to`.
1661 LiveInterval* destination = interval->GetSiblingAt(to->GetLifetimeStart());
1662 LiveInterval* source = interval->GetSiblingAt(from->GetLifetimeEnd() - 1);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001663
1664 if (destination == source) {
1665 // Interval was not split.
1666 return;
1667 }
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +01001668 DCHECK(destination != nullptr && source != nullptr);
1669
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001670 if (!destination->HasRegister()) {
1671 // Values are eagerly spilled. Spill slot already contains appropriate value.
1672 return;
1673 }
1674
1675 // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
1676 // we need to put the moves at the entry of `to`.
1677 if (from->GetSuccessors().Size() == 1) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001678 InsertParallelMoveAtExitOf(from,
1679 interval->GetParent()->GetDefinedBy(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001680 source->ToLocation(),
1681 destination->ToLocation());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001682 } else {
1683 DCHECK_EQ(to->GetPredecessors().Size(), 1u);
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001684 InsertParallelMoveAtEntryOf(to,
1685 interval->GetParent()->GetDefinedBy(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001686 source->ToLocation(),
1687 destination->ToLocation());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001688 }
1689}
1690
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001691void RegisterAllocator::Resolve() {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001692 codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(),
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +00001693 maximum_number_of_live_core_registers_,
1694 maximum_number_of_live_fp_registers_,
1695 reserved_out_slots_,
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001696 codegen_->GetGraph()->GetLinearOrder());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001697
1698 // Adjust the Out Location of instructions.
1699 // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
1700 for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
1701 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
1702 LiveInterval* current = instruction->GetLiveInterval();
1703 LocationSummary* locations = instruction->GetLocations();
1704 Location location = locations->Out();
Roland Levillain476df552014-10-09 17:51:36 +01001705 if (instruction->IsParameterValue()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001706 // Now that we know the frame size, adjust the parameter's location.
1707 if (location.IsStackSlot()) {
1708 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1709 current->SetSpillSlot(location.GetStackIndex());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001710 locations->UpdateOut(location);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001711 } else if (location.IsDoubleStackSlot()) {
1712 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1713 current->SetSpillSlot(location.GetStackIndex());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001714 locations->UpdateOut(location);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001715 } else if (current->HasSpillSlot()) {
1716 current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
1717 }
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001718 } else if (current->HasSpillSlot()) {
1719 // Adjust the stack slot, now that we know the number of them for each type.
1720 // The way this implementation lays out the stack is the following:
1721 // [parameter slots ]
1722 // [double spill slots ]
1723 // [long spill slots ]
1724 // [float spill slots ]
1725 // [int/ref values ]
1726 // [maximum out values ] (number of arguments for calls)
1727 // [art method ].
1728 uint32_t slot = current->GetSpillSlot();
1729 switch (current->GetType()) {
1730 case Primitive::kPrimDouble:
1731 slot += long_spill_slots_.Size();
1732 FALLTHROUGH_INTENDED;
1733 case Primitive::kPrimLong:
1734 slot += float_spill_slots_.Size();
1735 FALLTHROUGH_INTENDED;
1736 case Primitive::kPrimFloat:
1737 slot += int_spill_slots_.Size();
1738 FALLTHROUGH_INTENDED;
1739 case Primitive::kPrimNot:
1740 case Primitive::kPrimInt:
1741 case Primitive::kPrimChar:
1742 case Primitive::kPrimByte:
1743 case Primitive::kPrimBoolean:
1744 case Primitive::kPrimShort:
1745 slot += reserved_out_slots_;
1746 break;
1747 case Primitive::kPrimVoid:
1748 LOG(FATAL) << "Unexpected type for interval " << current->GetType();
1749 }
1750 current->SetSpillSlot(slot * kVRegSize);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001751 }
1752
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001753 Location source = current->ToLocation();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001754
1755 if (location.IsUnallocated()) {
1756 if (location.GetPolicy() == Location::kSameAsFirstInput) {
Calin Juravled0d48522014-11-04 16:40:20 +00001757 if (locations->InAt(0).IsUnallocated()) {
1758 locations->SetInAt(0, source);
1759 } else {
1760 DCHECK(locations->InAt(0).Equals(source));
1761 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001762 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001763 locations->UpdateOut(source);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001764 } else {
1765 DCHECK(source.Equals(location));
1766 }
1767 }
1768
1769 // Connect siblings.
1770 for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
1771 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
1772 ConnectSiblings(instruction->GetLiveInterval());
1773 }
1774
1775 // Resolve non-linear control flow across branches. Order does not matter.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001776 for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001777 HBasicBlock* block = it.Current();
1778 BitVector* live = liveness_.GetLiveInSet(*block);
1779 for (uint32_t idx : live->Indexes()) {
1780 HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
1781 LiveInterval* interval = current->GetLiveInterval();
1782 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
1783 ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
1784 }
1785 }
1786 }
1787
1788 // Resolve phi inputs. Order does not matter.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001789 for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001790 HBasicBlock* current = it.Current();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001791 for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1792 HInstruction* phi = inst_it.Current();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001793 for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
1794 HBasicBlock* predecessor = current->GetPredecessors().Get(i);
1795 DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
1796 HInstruction* input = phi->InputAt(i);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001797 Location source = input->GetLiveInterval()->GetLocationAt(
1798 predecessor->GetLifetimeEnd() - 1);
1799 Location destination = phi->GetLiveInterval()->ToLocation();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001800 InsertParallelMoveAtExitOf(predecessor, phi, source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001801 }
1802 }
1803 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001804
1805 // Assign temp locations.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001806 for (size_t i = 0; i < temp_intervals_.Size(); ++i) {
1807 LiveInterval* temp = temp_intervals_.Get(i);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001808 if (temp->IsHighInterval()) {
1809 // High intervals can be skipped, they are already handled by the low interval.
1810 continue;
1811 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001812 HInstruction* at = liveness_.GetTempUser(temp);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001813 size_t temp_index = liveness_.GetTempIndex(temp);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001814 LocationSummary* locations = at->GetLocations();
Roland Levillain5368c212014-11-27 15:03:41 +00001815 switch (temp->GetType()) {
1816 case Primitive::kPrimInt:
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001817 locations->SetTempAt(temp_index, Location::RegisterLocation(temp->GetRegister()));
Roland Levillain5368c212014-11-27 15:03:41 +00001818 break;
1819
1820 case Primitive::kPrimDouble:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001821 if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1822 Location location = Location::FpuRegisterPairLocation(
1823 temp->GetRegister(), temp->GetHighInterval()->GetRegister());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001824 locations->SetTempAt(temp_index, location);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001825 } else {
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001826 locations->SetTempAt(temp_index, Location::FpuRegisterLocation(temp->GetRegister()));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001827 }
Roland Levillain5368c212014-11-27 15:03:41 +00001828 break;
1829
1830 default:
1831 LOG(FATAL) << "Unexpected type for temporary location "
1832 << temp->GetType();
1833 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001834 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001835}
1836
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001837} // namespace art