blob: 1d155f9b5dae01fd6c1ce31bc15ecf6110ae552e [file] [log] [blame]
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "register_allocator.h"
18
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Ian Rogerse77493c2014-08-20 15:08:45 -070021#include "base/bit_vector-inl.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010022#include "code_generator.h"
23#include "ssa_liveness_analysis.h"
24
25namespace art {
26
27static constexpr size_t kMaxLifetimePosition = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010028static constexpr size_t kDefaultNumberOfSpillSlots = 4;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010029
Nicolas Geoffray840e5462015-01-07 16:01:24 +000030// For simplicity, we implement register pairs as (reg, reg + 1).
31// Note that this is a requirement for double registers on ARM, since we
32// allocate SRegister.
33static int GetHighForLowRegister(int reg) { return reg + 1; }
34static bool IsLowRegister(int reg) { return (reg & 1) == 0; }
35
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010036RegisterAllocator::RegisterAllocator(ArenaAllocator* allocator,
37 CodeGenerator* codegen,
38 const SsaLivenessAnalysis& liveness)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010039 : allocator_(allocator),
40 codegen_(codegen),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010041 liveness_(liveness),
Nicolas Geoffray39468442014-09-02 15:17:15 +010042 unhandled_core_intervals_(allocator, 0),
43 unhandled_fp_intervals_(allocator, 0),
44 unhandled_(nullptr),
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010045 handled_(allocator, 0),
46 active_(allocator, 0),
47 inactive_(allocator, 0),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010048 physical_core_register_intervals_(allocator, codegen->GetNumberOfCoreRegisters()),
49 physical_fp_register_intervals_(allocator, codegen->GetNumberOfFloatingPointRegisters()),
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 temp_intervals_(allocator, 4),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010051 spill_slots_(allocator, kDefaultNumberOfSpillSlots),
Nicolas Geoffray39468442014-09-02 15:17:15 +010052 safepoints_(allocator, 0),
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010053 processing_core_registers_(false),
54 number_of_registers_(-1),
55 registers_array_(nullptr),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010056 blocked_core_registers_(codegen->GetBlockedCoreRegisters()),
57 blocked_fp_registers_(codegen->GetBlockedFloatingPointRegisters()),
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +010058 reserved_out_slots_(0),
Mark Mendellf85a9ca2015-01-13 09:20:58 -050059 maximum_number_of_live_core_registers_(0),
60 maximum_number_of_live_fp_registers_(0) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +010061 codegen->SetupBlockedRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010062 physical_core_register_intervals_.SetSize(codegen->GetNumberOfCoreRegisters());
63 physical_fp_register_intervals_.SetSize(codegen->GetNumberOfFloatingPointRegisters());
Nicolas Geoffray39468442014-09-02 15:17:15 +010064 // Always reserve for the current method and the graph's max out registers.
65 // TODO: compute it instead.
66 reserved_out_slots_ = 1 + codegen->GetGraph()->GetMaximumNumberOfOutVRegs();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010067}
68
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010069bool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph,
70 InstructionSet instruction_set) {
71 if (!Supports(instruction_set)) {
72 return false;
73 }
Alexandre Rames3e69f162014-12-10 10:36:50 +000074 if (instruction_set == kArm64 || instruction_set == kX86_64) {
75 return true;
76 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010077 for (size_t i = 0, e = graph.GetBlocks().Size(); i < e; ++i) {
78 for (HInstructionIterator it(graph.GetBlocks().Get(i)->GetInstructions());
79 !it.Done();
80 it.Advance()) {
81 HInstruction* current = it.Current();
Nicolas Geoffray840e5462015-01-07 16:01:24 +000082 if (instruction_set == kX86) {
83 if (current->GetType() == Primitive::kPrimLong ||
84 current->GetType() == Primitive::kPrimFloat ||
85 current->GetType() == Primitive::kPrimDouble) {
86 return false;
87 }
88 } else if (instruction_set == kArm || instruction_set == kThumb2) {
89 if (current->GetType() == Primitive::kPrimLong) {
90 return false;
91 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010092 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010093 }
94 }
95 return true;
96}
97
98static bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) {
Nicolas Geoffray39468442014-09-02 15:17:15 +010099 if (interval == nullptr) return false;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100100 bool is_core_register = (interval->GetType() != Primitive::kPrimDouble)
101 && (interval->GetType() != Primitive::kPrimFloat);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100102 return processing_core_registers == is_core_register;
103}
104
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100105void RegisterAllocator::AllocateRegisters() {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100106 AllocateRegistersInternal();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100107 Resolve();
108
109 if (kIsDebugBuild) {
110 processing_core_registers_ = true;
111 ValidateInternal(true);
112 processing_core_registers_ = false;
113 ValidateInternal(true);
Nicolas Geoffray59768572014-12-01 09:50:04 +0000114 // Check that the linear order is still correct with regards to lifetime positions.
115 // Since only parallel moves have been inserted during the register allocation,
116 // these checks are mostly for making sure these moves have been added correctly.
117 size_t current_liveness = 0;
118 for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
119 HBasicBlock* block = it.Current();
120 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
121 HInstruction* instruction = inst_it.Current();
122 DCHECK_LE(current_liveness, instruction->GetLifetimePosition());
123 current_liveness = instruction->GetLifetimePosition();
124 }
125 for (HInstructionIterator inst_it(block->GetInstructions());
126 !inst_it.Done();
127 inst_it.Advance()) {
128 HInstruction* instruction = inst_it.Current();
129 DCHECK_LE(current_liveness, instruction->GetLifetimePosition()) << instruction->DebugName();
130 current_liveness = instruction->GetLifetimePosition();
131 }
132 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100133 }
134}
135
136void RegisterAllocator::BlockRegister(Location location,
137 size_t start,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100138 size_t end) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100139 int reg = location.reg();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100140 DCHECK(location.IsRegister() || location.IsFpuRegister());
141 LiveInterval* interval = location.IsRegister()
142 ? physical_core_register_intervals_.Get(reg)
143 : physical_fp_register_intervals_.Get(reg);
144 Primitive::Type type = location.IsRegister()
145 ? Primitive::kPrimInt
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000146 : Primitive::kPrimFloat;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100147 if (interval == nullptr) {
148 interval = LiveInterval::MakeFixedInterval(allocator_, reg, type);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100149 if (location.IsRegister()) {
150 physical_core_register_intervals_.Put(reg, interval);
151 } else {
152 physical_fp_register_intervals_.Put(reg, interval);
153 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100154 }
155 DCHECK(interval->GetRegister() == reg);
156 interval->AddRange(start, end);
157}
158
159void RegisterAllocator::AllocateRegistersInternal() {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100160 // Iterate post-order, to ensure the list is sorted, and the last added interval
161 // is the one with the lowest start position.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100162 for (HLinearPostOrderIterator it(liveness_); !it.Done(); it.Advance()) {
163 HBasicBlock* block = it.Current();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800164 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
165 back_it.Advance()) {
166 ProcessInstruction(back_it.Current());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100167 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800168 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
169 ProcessInstruction(inst_it.Current());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100170 }
171 }
172
Nicolas Geoffray39468442014-09-02 15:17:15 +0100173 number_of_registers_ = codegen_->GetNumberOfCoreRegisters();
174 registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
175 processing_core_registers_ = true;
176 unhandled_ = &unhandled_core_intervals_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100177 for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
178 LiveInterval* fixed = physical_core_register_intervals_.Get(i);
179 if (fixed != nullptr) {
Mingyao Yang296bd602014-10-06 16:47:28 -0700180 // Fixed interval is added to inactive_ instead of unhandled_.
181 // It's also the only type of inactive interval whose start position
182 // can be after the current interval during linear scan.
183 // Fixed interval is never split and never moves to unhandled_.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100184 inactive_.Add(fixed);
185 }
186 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100187 LinearScan();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100188
189 inactive_.Reset();
190 active_.Reset();
191 handled_.Reset();
192
193 number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters();
194 registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
195 processing_core_registers_ = false;
196 unhandled_ = &unhandled_fp_intervals_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100197 for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
198 LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
199 if (fixed != nullptr) {
Mingyao Yang296bd602014-10-06 16:47:28 -0700200 // Fixed interval is added to inactive_ instead of unhandled_.
201 // It's also the only type of inactive interval whose start position
202 // can be after the current interval during linear scan.
203 // Fixed interval is never split and never moves to unhandled_.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100204 inactive_.Add(fixed);
205 }
206 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100207 LinearScan();
208}
209
210void RegisterAllocator::ProcessInstruction(HInstruction* instruction) {
211 LocationSummary* locations = instruction->GetLocations();
212 size_t position = instruction->GetLifetimePosition();
213
214 if (locations == nullptr) return;
215
216 // Create synthesized intervals for temporaries.
217 for (size_t i = 0; i < locations->GetTempCount(); ++i) {
218 Location temp = locations->GetTemp(i);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000219 if (temp.IsRegister() || temp.IsFpuRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100220 BlockRegister(temp, position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100221 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100222 DCHECK(temp.IsUnallocated());
Roland Levillain5368c212014-11-27 15:03:41 +0000223 switch (temp.GetPolicy()) {
224 case Location::kRequiresRegister: {
225 LiveInterval* interval =
226 LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt);
227 temp_intervals_.Add(interval);
228 interval->AddRange(position, position + 1);
229 unhandled_core_intervals_.Add(interval);
230 break;
231 }
232
233 case Location::kRequiresFpuRegister: {
234 LiveInterval* interval =
235 LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimDouble);
236 temp_intervals_.Add(interval);
237 interval->AddRange(position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000238 if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
239 interval->AddHighInterval(true);
240 LiveInterval* high = interval->GetHighInterval();
241 temp_intervals_.Add(high);
242 unhandled_fp_intervals_.Add(high);
243 }
Roland Levillain5368c212014-11-27 15:03:41 +0000244 unhandled_fp_intervals_.Add(interval);
245 break;
246 }
247
248 default:
249 LOG(FATAL) << "Unexpected policy for temporary location "
250 << temp.GetPolicy();
251 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100252 }
253 }
254
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100255 bool core_register = (instruction->GetType() != Primitive::kPrimDouble)
256 && (instruction->GetType() != Primitive::kPrimFloat);
257
Nicolas Geoffray39468442014-09-02 15:17:15 +0100258 if (locations->CanCall()) {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100259 if (!instruction->IsSuspendCheck()) {
260 codegen_->MarkNotLeaf();
261 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100262 safepoints_.Add(instruction);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100263 if (locations->OnlyCallsOnSlowPath()) {
264 // We add a synthesized range at this position to record the live registers
265 // at this position. Ideally, we could just update the safepoints when locations
266 // are updated, but we currently need to know the full stack size before updating
267 // locations (because of parameters and the fact that we don't have a frame pointer).
268 // And knowing the full stack size requires to know the maximum number of live
269 // registers at calls in slow paths.
270 // By adding the following interval in the algorithm, we can compute this
271 // maximum before updating locations.
272 LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction);
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000273 interval->AddRange(position, position + 1);
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000274 AddSorted(&unhandled_core_intervals_, interval);
275 AddSorted(&unhandled_fp_intervals_, interval);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100276 }
277 }
278
279 if (locations->WillCall()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100280 // Block all registers.
281 for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100282 BlockRegister(Location::RegisterLocation(i),
Nicolas Geoffray39468442014-09-02 15:17:15 +0100283 position,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100284 position + 1);
285 }
286 for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) {
287 BlockRegister(Location::FpuRegisterLocation(i),
288 position,
289 position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100290 }
291 }
292
293 for (size_t i = 0; i < instruction->InputCount(); ++i) {
294 Location input = locations->InAt(i);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100295 if (input.IsRegister() || input.IsFpuRegister()) {
296 BlockRegister(input, position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000297 } else if (input.IsPair()) {
298 BlockRegister(input.ToLow(), position, position + 1);
299 BlockRegister(input.ToHigh(), position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100300 }
301 }
302
Nicolas Geoffray39468442014-09-02 15:17:15 +0100303 LiveInterval* current = instruction->GetLiveInterval();
304 if (current == nullptr) return;
305
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100306 GrowableArray<LiveInterval*>& unhandled = core_register
307 ? unhandled_core_intervals_
308 : unhandled_fp_intervals_;
309
Nicolas Geoffray76905622014-09-25 14:39:26 +0100310 DCHECK(unhandled.IsEmpty() || current->StartsBeforeOrAt(unhandled.Peek()));
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000311
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000312 if (codegen_->NeedsTwoRegisters(current->GetType())) {
313 current->AddHighInterval();
314 }
315
Nicolas Geoffray39468442014-09-02 15:17:15 +0100316 // Some instructions define their output in fixed register/stack slot. We need
317 // to ensure we know these locations before doing register allocation. For a
318 // given register, we create an interval that covers these locations. The register
319 // will be unavailable at these locations when trying to allocate one for an
320 // interval.
321 //
322 // The backwards walking ensures the ranges are ordered on increasing start positions.
323 Location output = locations->Out();
Calin Juravled0d48522014-11-04 16:40:20 +0000324 if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
325 Location first = locations->InAt(0);
326 if (first.IsRegister() || first.IsFpuRegister()) {
327 current->SetFrom(position + 1);
328 current->SetRegister(first.reg());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000329 } else if (first.IsPair()) {
330 current->SetFrom(position + 1);
331 current->SetRegister(first.low());
332 LiveInterval* high = current->GetHighInterval();
333 high->SetRegister(first.high());
334 high->SetFrom(position + 1);
Calin Juravled0d48522014-11-04 16:40:20 +0000335 }
336 } else if (output.IsRegister() || output.IsFpuRegister()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100337 // Shift the interval's start by one to account for the blocked register.
338 current->SetFrom(position + 1);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100339 current->SetRegister(output.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100340 BlockRegister(output, position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000341 } else if (output.IsPair()) {
342 current->SetFrom(position + 1);
343 current->SetRegister(output.low());
344 LiveInterval* high = current->GetHighInterval();
345 high->SetRegister(output.high());
346 high->SetFrom(position + 1);
347 BlockRegister(output.ToLow(), position, position + 1);
348 BlockRegister(output.ToHigh(), position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100349 } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
350 current->SetSpillSlot(output.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000351 } else {
352 DCHECK(output.IsUnallocated() || output.IsConstant());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100353 }
354
355 // If needed, add interval to the list of unhandled intervals.
356 if (current->HasSpillSlot() || instruction->IsConstant()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100357 // Split just before first register use.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100358 size_t first_register_use = current->FirstRegisterUse();
359 if (first_register_use != kNoLifetime) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100360 LiveInterval* split = Split(current, first_register_use - 1);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000361 // Don't add directly to `unhandled`, it needs to be sorted and the start
Nicolas Geoffray39468442014-09-02 15:17:15 +0100362 // of this new interval might be after intervals already in the list.
363 AddSorted(&unhandled, split);
364 } else {
365 // Nothing to do, we won't allocate a register for this value.
366 }
367 } else {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000368 // Don't add directly to `unhandled`, temp or safepoint intervals
369 // for this instruction may have been added, and those can be
370 // processed first.
371 AddSorted(&unhandled, current);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100372 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100373}
374
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100375class AllRangesIterator : public ValueObject {
376 public:
377 explicit AllRangesIterator(LiveInterval* interval)
378 : current_interval_(interval),
379 current_range_(interval->GetFirstRange()) {}
380
381 bool Done() const { return current_interval_ == nullptr; }
382 LiveRange* CurrentRange() const { return current_range_; }
383 LiveInterval* CurrentInterval() const { return current_interval_; }
384
385 void Advance() {
386 current_range_ = current_range_->GetNext();
387 if (current_range_ == nullptr) {
388 current_interval_ = current_interval_->GetNextSibling();
389 if (current_interval_ != nullptr) {
390 current_range_ = current_interval_->GetFirstRange();
391 }
392 }
393 }
394
395 private:
396 LiveInterval* current_interval_;
397 LiveRange* current_range_;
398
399 DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
400};
401
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100402bool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
403 // To simplify unit testing, we eagerly create the array of intervals, and
404 // call the helper method.
405 GrowableArray<LiveInterval*> intervals(allocator_, 0);
406 for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
407 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
408 if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
409 intervals.Add(instruction->GetLiveInterval());
410 }
411 }
412
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100413 if (processing_core_registers_) {
414 for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
415 LiveInterval* fixed = physical_core_register_intervals_.Get(i);
416 if (fixed != nullptr) {
417 intervals.Add(fixed);
418 }
419 }
420 } else {
421 for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
422 LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
423 if (fixed != nullptr) {
424 intervals.Add(fixed);
425 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100426 }
427 }
428
Nicolas Geoffray39468442014-09-02 15:17:15 +0100429 for (size_t i = 0, e = temp_intervals_.Size(); i < e; ++i) {
430 LiveInterval* temp = temp_intervals_.Get(i);
431 if (ShouldProcess(processing_core_registers_, temp)) {
432 intervals.Add(temp);
433 }
434 }
435
436 return ValidateIntervals(intervals, spill_slots_.Size(), reserved_out_slots_, *codegen_,
437 allocator_, processing_core_registers_, log_fatal_on_failure);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100438}
439
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100440bool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
441 size_t number_of_spill_slots,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100442 size_t number_of_out_slots,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100443 const CodeGenerator& codegen,
444 ArenaAllocator* allocator,
445 bool processing_core_registers,
446 bool log_fatal_on_failure) {
447 size_t number_of_registers = processing_core_registers
448 ? codegen.GetNumberOfCoreRegisters()
449 : codegen.GetNumberOfFloatingPointRegisters();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100450 GrowableArray<ArenaBitVector*> liveness_of_values(
451 allocator, number_of_registers + number_of_spill_slots);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100452
453 // Allocate a bit vector per register. A live interval that has a register
454 // allocated will populate the associated bit vector based on its live ranges.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100455 for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
456 liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100457 }
458
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100459 for (size_t i = 0, e = intervals.Size(); i < e; ++i) {
460 for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) {
461 LiveInterval* current = it.CurrentInterval();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100462 HInstruction* defined_by = current->GetParent()->GetDefinedBy();
463 if (current->GetParent()->HasSpillSlot()
464 // Parameters have their own stack slot.
465 && !(defined_by != nullptr && defined_by->IsParameterValue())) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100466 BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers
467 + current->GetParent()->GetSpillSlot() / kVRegSize
468 - number_of_out_slots);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100469 for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
470 if (liveness_of_spill_slot->IsBitSet(j)) {
471 if (log_fatal_on_failure) {
472 std::ostringstream message;
473 message << "Spill slot conflict at " << j;
474 LOG(FATAL) << message.str();
475 } else {
476 return false;
477 }
478 } else {
479 liveness_of_spill_slot->SetBit(j);
480 }
481 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100482 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100483
484 if (current->HasRegister()) {
485 BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister());
486 for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
487 if (liveness_of_register->IsBitSet(j)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100488 if (log_fatal_on_failure) {
489 std::ostringstream message;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100490 message << "Register conflict at " << j << " ";
491 if (defined_by != nullptr) {
492 message << "(" << defined_by->DebugName() << ")";
493 }
494 message << "for ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100495 if (processing_core_registers) {
496 codegen.DumpCoreRegister(message, current->GetRegister());
497 } else {
498 codegen.DumpFloatingPointRegister(message, current->GetRegister());
499 }
500 LOG(FATAL) << message.str();
501 } else {
502 return false;
503 }
504 } else {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100505 liveness_of_register->SetBit(j);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100506 }
507 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100508 }
509 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100510 }
511 return true;
512}
513
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100514void RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100515 interval->Dump(stream);
516 stream << ": ";
517 if (interval->HasRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100518 if (interval->IsFloatingPoint()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100519 codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100520 } else {
521 codegen_->DumpCoreRegister(stream, interval->GetRegister());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100522 }
523 } else {
524 stream << "spilled";
525 }
526 stream << std::endl;
527}
528
Mingyao Yang296bd602014-10-06 16:47:28 -0700529void RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
530 stream << "inactive: " << std::endl;
531 for (size_t i = 0; i < inactive_.Size(); i ++) {
532 DumpInterval(stream, inactive_.Get(i));
533 }
534 stream << "active: " << std::endl;
535 for (size_t i = 0; i < active_.Size(); i ++) {
536 DumpInterval(stream, active_.Get(i));
537 }
538 stream << "unhandled: " << std::endl;
539 auto unhandled = (unhandled_ != nullptr) ?
540 unhandled_ : &unhandled_core_intervals_;
541 for (size_t i = 0; i < unhandled->Size(); i ++) {
542 DumpInterval(stream, unhandled->Get(i));
543 }
544 stream << "handled: " << std::endl;
545 for (size_t i = 0; i < handled_.Size(); i ++) {
546 DumpInterval(stream, handled_.Get(i));
547 }
548}
549
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100550// By the book implementation of a linear scan register allocator.
551void RegisterAllocator::LinearScan() {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100552 while (!unhandled_->IsEmpty()) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100553 // (1) Remove interval with the lowest start position from unhandled.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100554 LiveInterval* current = unhandled_->Pop();
555 DCHECK(!current->IsFixed() && !current->HasSpillSlot());
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100556 DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000557 DCHECK(!current->IsLowInterval() || unhandled_->Peek()->IsHighInterval());
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000558
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100559 size_t position = current->GetStart();
560
Mingyao Yang296bd602014-10-06 16:47:28 -0700561 // Remember the inactive_ size here since the ones moved to inactive_ from
562 // active_ below shouldn't need to be re-checked.
563 size_t inactive_intervals_to_handle = inactive_.Size();
564
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100565 // (2) Remove currently active intervals that are dead at this position.
566 // Move active intervals that have a lifetime hole at this position
567 // to inactive.
568 for (size_t i = 0; i < active_.Size(); ++i) {
569 LiveInterval* interval = active_.Get(i);
570 if (interval->IsDeadAt(position)) {
571 active_.Delete(interval);
572 --i;
573 handled_.Add(interval);
574 } else if (!interval->Covers(position)) {
575 active_.Delete(interval);
576 --i;
577 inactive_.Add(interval);
578 }
579 }
580
581 // (3) Remove currently inactive intervals that are dead at this position.
582 // Move inactive intervals that cover this position to active.
Mingyao Yang296bd602014-10-06 16:47:28 -0700583 for (size_t i = 0; i < inactive_intervals_to_handle; ++i) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100584 LiveInterval* interval = inactive_.Get(i);
Mingyao Yang296bd602014-10-06 16:47:28 -0700585 DCHECK(interval->GetStart() < position || interval->IsFixed());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100586 if (interval->IsDeadAt(position)) {
587 inactive_.Delete(interval);
588 --i;
Mingyao Yang296bd602014-10-06 16:47:28 -0700589 --inactive_intervals_to_handle;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100590 handled_.Add(interval);
591 } else if (interval->Covers(position)) {
592 inactive_.Delete(interval);
593 --i;
Mingyao Yang296bd602014-10-06 16:47:28 -0700594 --inactive_intervals_to_handle;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100595 active_.Add(interval);
596 }
597 }
598
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000599 if (current->IsSlowPathSafepoint()) {
600 // Synthesized interval to record the maximum number of live registers
601 // at safepoints. No need to allocate a register for it.
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500602 if (processing_core_registers_) {
603 maximum_number_of_live_core_registers_ =
604 std::max(maximum_number_of_live_core_registers_, active_.Size());
605 } else {
606 maximum_number_of_live_fp_registers_ =
607 std::max(maximum_number_of_live_fp_registers_, active_.Size());
608 }
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000609 DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() > current->GetStart());
610 continue;
611 }
612
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000613 if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
614 DCHECK(!current->HasRegister());
615 // Allocating the low part was unsucessful. The splitted interval for the high part
616 // will be handled next (it is in the `unhandled_` list).
617 continue;
618 }
619
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100620 // (4) Try to find an available register.
621 bool success = TryAllocateFreeReg(current);
622
623 // (5) If no register could be found, we need to spill.
624 if (!success) {
625 success = AllocateBlockedReg(current);
626 }
627
628 // (6) If the interval had a register allocated, add it to the list of active
629 // intervals.
630 if (success) {
631 active_.Add(current);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000632 if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
633 current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
634 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100635 }
636 }
637}
638
639// Find a free register. If multiple are found, pick the register that
640// is free the longest.
641bool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
642 size_t* free_until = registers_array_;
643
644 // First set all registers to be free.
645 for (size_t i = 0; i < number_of_registers_; ++i) {
646 free_until[i] = kMaxLifetimePosition;
647 }
648
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100649 // For each active interval, set its register to not free.
650 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
651 LiveInterval* interval = active_.Get(i);
652 DCHECK(interval->HasRegister());
653 free_until[interval->GetRegister()] = 0;
654 }
655
Mingyao Yang296bd602014-10-06 16:47:28 -0700656 // For each inactive interval, set its register to be free until
657 // the next intersection with `current`.
658 for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
659 LiveInterval* inactive = inactive_.Get(i);
660 // Temp/Slow-path-safepoint interval has no holes.
661 DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
662 if (!current->IsSplit() && !inactive->IsFixed()) {
663 // Neither current nor inactive are fixed.
664 // Thanks to SSA, a non-split interval starting in a hole of an
665 // inactive interval should never intersect with that inactive interval.
666 // Only if it's not fixed though, because fixed intervals don't come from SSA.
667 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
668 continue;
669 }
670
671 DCHECK(inactive->HasRegister());
672 if (free_until[inactive->GetRegister()] == 0) {
673 // Already used by some active interval. No need to intersect.
674 continue;
675 }
676 size_t next_intersection = inactive->FirstIntersectionWith(current);
677 if (next_intersection != kNoLifetime) {
678 free_until[inactive->GetRegister()] =
679 std::min(free_until[inactive->GetRegister()], next_intersection);
680 }
681 }
682
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100683 int reg = -1;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100684 if (current->HasRegister()) {
685 // Some instructions have a fixed register output.
686 reg = current->GetRegister();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000687 if (free_until[reg] == 0) {
688 DCHECK(current->IsHighInterval());
689 // AllocateBlockedReg will spill the holder of the register.
690 return false;
691 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100692 } else {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000693 DCHECK(!current->IsHighInterval());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100694 int hint = current->FindFirstRegisterHint(free_until);
695 if (hint != kNoRegister) {
696 DCHECK(!IsBlocked(hint));
697 reg = hint;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000698 } else if (current->IsLowInterval()) {
699 reg = FindAvailableRegisterPair(free_until);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100700 } else {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000701 reg = FindAvailableRegister(free_until);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100702 }
703 }
704
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000705 DCHECK_NE(reg, -1);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100706 // If we could not find a register, we need to spill.
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000707 if (free_until[reg] == 0) {
708 return false;
709 }
710
711 if (current->IsLowInterval() && free_until[GetHighForLowRegister(reg)] == 0) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100712 return false;
713 }
714
715 current->SetRegister(reg);
716 if (!current->IsDeadAt(free_until[reg])) {
717 // If the register is only available for a subset of live ranges
718 // covered by `current`, split `current` at the position where
719 // the register is not available anymore.
720 LiveInterval* split = Split(current, free_until[reg]);
721 DCHECK(split != nullptr);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100722 AddSorted(unhandled_, split);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100723 }
724 return true;
725}
726
727bool RegisterAllocator::IsBlocked(int reg) const {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100728 return processing_core_registers_
729 ? blocked_core_registers_[reg]
730 : blocked_fp_registers_[reg];
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100731}
732
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000733int RegisterAllocator::FindAvailableRegisterPair(size_t* next_use) const {
734 int reg = -1;
735 // Pick the register pair that is used the last.
736 for (size_t i = 0; i < number_of_registers_; ++i) {
737 if (IsBlocked(i)) continue;
738 if (!IsLowRegister(i)) continue;
739 int high_register = GetHighForLowRegister(i);
740 if (IsBlocked(high_register)) continue;
741 int existing_high_register = GetHighForLowRegister(reg);
742 if ((reg == -1) || (next_use[i] >= next_use[reg]
743 && next_use[high_register] >= next_use[existing_high_register])) {
744 reg = i;
745 if (next_use[i] == kMaxLifetimePosition
746 && next_use[high_register] == kMaxLifetimePosition) {
747 break;
748 }
749 }
750 }
751 return reg;
752}
753
754int RegisterAllocator::FindAvailableRegister(size_t* next_use) const {
755 int reg = -1;
756 // Pick the register that is used the last.
757 for (size_t i = 0; i < number_of_registers_; ++i) {
758 if (IsBlocked(i)) continue;
759 if (reg == -1 || next_use[i] > next_use[reg]) {
760 reg = i;
761 if (next_use[i] == kMaxLifetimePosition) break;
762 }
763 }
764 return reg;
765}
766
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100767// Find the register that is used the last, and spill the interval
768// that holds it. If the first use of `current` is after that register
769// we spill `current` instead.
770bool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
771 size_t first_register_use = current->FirstRegisterUse();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100772 if (first_register_use == kNoLifetime) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100773 AllocateSpillSlotFor(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100774 return false;
775 }
776
777 // First set all registers as not being used.
778 size_t* next_use = registers_array_;
779 for (size_t i = 0; i < number_of_registers_; ++i) {
780 next_use[i] = kMaxLifetimePosition;
781 }
782
783 // For each active interval, find the next use of its register after the
784 // start of current.
785 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
786 LiveInterval* active = active_.Get(i);
787 DCHECK(active->HasRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100788 if (active->IsFixed()) {
789 next_use[active->GetRegister()] = current->GetStart();
790 } else {
791 size_t use = active->FirstRegisterUseAfter(current->GetStart());
792 if (use != kNoLifetime) {
793 next_use[active->GetRegister()] = use;
794 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100795 }
796 }
797
798 // For each inactive interval, find the next use of its register after the
799 // start of current.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100800 for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
801 LiveInterval* inactive = inactive_.Get(i);
Mingyao Yang296bd602014-10-06 16:47:28 -0700802 // Temp/Slow-path-safepoint interval has no holes.
803 DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
804 if (!current->IsSplit() && !inactive->IsFixed()) {
805 // Neither current nor inactive are fixed.
806 // Thanks to SSA, a non-split interval starting in a hole of an
807 // inactive interval should never intersect with that inactive interval.
808 // Only if it's not fixed though, because fixed intervals don't come from SSA.
809 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
810 continue;
811 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100812 DCHECK(inactive->HasRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100813 size_t next_intersection = inactive->FirstIntersectionWith(current);
814 if (next_intersection != kNoLifetime) {
815 if (inactive->IsFixed()) {
816 next_use[inactive->GetRegister()] =
817 std::min(next_intersection, next_use[inactive->GetRegister()]);
818 } else {
819 size_t use = inactive->FirstRegisterUseAfter(current->GetStart());
820 if (use != kNoLifetime) {
821 next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
822 }
823 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100824 }
825 }
826
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100827 int reg = -1;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000828 if (current->HasRegister()) {
829 DCHECK(current->IsHighInterval());
830 reg = current->GetRegister();
831 } else if (current->IsLowInterval()) {
832 reg = FindAvailableRegisterPair(next_use);
833 } else {
834 DCHECK(!current->IsHighInterval());
835 reg = FindAvailableRegister(next_use);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100836 }
837
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000838 if ((first_register_use >= next_use[reg])
839 || (current->IsLowInterval() && first_register_use >= next_use[GetHighForLowRegister(reg)])) {
840 DCHECK(!current->IsHighInterval());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100841 // If the first use of that instruction is after the last use of the found
842 // register, we split this interval just before its first register use.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100843 AllocateSpillSlotFor(current);
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100844 LiveInterval* split = Split(current, first_register_use - 1);
845 DCHECK_NE(current, split) << "There is not enough registers available for "
846 << split->GetParent()->GetDefinedBy()->DebugName();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100847 AddSorted(unhandled_, split);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100848 return false;
849 } else {
850 // Use this register and spill the active and inactives interval that
851 // have that register.
852 current->SetRegister(reg);
853
854 for (size_t i = 0, e = active_.Size(); i < e; ++i) {
855 LiveInterval* active = active_.Get(i);
856 if (active->GetRegister() == reg) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100857 DCHECK(!active->IsFixed());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100858 LiveInterval* split = Split(active, current->GetStart());
859 active_.DeleteAt(i);
860 handled_.Add(active);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100861 AddSorted(unhandled_, split);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100862 break;
863 }
864 }
865
Mingyao Yang296bd602014-10-06 16:47:28 -0700866 for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100867 LiveInterval* inactive = inactive_.Get(i);
868 if (inactive->GetRegister() == reg) {
Mingyao Yang296bd602014-10-06 16:47:28 -0700869 if (!current->IsSplit() && !inactive->IsFixed()) {
870 // Neither current nor inactive are fixed.
871 // Thanks to SSA, a non-split interval starting in a hole of an
872 // inactive interval should never intersect with that inactive interval.
873 // Only if it's not fixed though, because fixed intervals don't come from SSA.
874 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
875 continue;
876 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100877 size_t next_intersection = inactive->FirstIntersectionWith(current);
878 if (next_intersection != kNoLifetime) {
879 if (inactive->IsFixed()) {
880 LiveInterval* split = Split(current, next_intersection);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100881 AddSorted(unhandled_, split);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100882 } else {
Mingyao Yang296bd602014-10-06 16:47:28 -0700883 LiveInterval* split = Split(inactive, next_intersection);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100884 inactive_.DeleteAt(i);
Mingyao Yang296bd602014-10-06 16:47:28 -0700885 --i;
886 --e;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100887 handled_.Add(inactive);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100888 AddSorted(unhandled_, split);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100889 }
890 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100891 }
892 }
893
894 return true;
895 }
896}
897
Nicolas Geoffray39468442014-09-02 15:17:15 +0100898void RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100899 DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100900 size_t insert_at = 0;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100901 for (size_t i = array->Size(); i > 0; --i) {
902 LiveInterval* current = array->Get(i - 1);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100903 if (current->StartsAfter(interval)) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100904 insert_at = i;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100905 break;
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000906 } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
907 // Ensure the slow path interval is the last to be processed at its location: we want the
908 // interval to know all live registers at this location.
909 DCHECK(i == 1 || array->Get(i - 2)->StartsAfter(current));
910 insert_at = i;
911 break;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100912 }
913 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000914
Nicolas Geoffray39468442014-09-02 15:17:15 +0100915 array->InsertAt(insert_at, interval);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000916 // Insert the high interval before the low, to ensure the low is processed before.
917 if (interval->HasHighInterval()) {
918 array->InsertAt(insert_at, interval->GetHighInterval());
919 } else if (interval->HasLowInterval()) {
920 array->InsertAt(insert_at + 1, interval->GetLowInterval());
921 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100922}
923
924LiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000925 DCHECK_GE(position, interval->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100926 DCHECK(!interval->IsDeadAt(position));
927 if (position == interval->GetStart()) {
928 // Spill slot will be allocated when handling `interval` again.
929 interval->ClearRegister();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000930 if (interval->HasHighInterval()) {
931 interval->GetHighInterval()->ClearRegister();
932 } else if (interval->HasLowInterval()) {
933 interval->GetLowInterval()->ClearRegister();
934 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100935 return interval;
936 } else {
937 LiveInterval* new_interval = interval->SplitAt(position);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000938 if (interval->HasHighInterval()) {
939 LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
940 new_interval->SetHighInterval(high);
941 high->SetLowInterval(new_interval);
942 } else if (interval->HasLowInterval()) {
943 LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
944 new_interval->SetLowInterval(low);
945 low->SetHighInterval(new_interval);
946 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100947 return new_interval;
948 }
949}
950
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100951void RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000952 if (interval->IsHighInterval()) {
953 // The low interval will contain the spill slot.
954 return;
955 }
956
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100957 LiveInterval* parent = interval->GetParent();
958
959 // An instruction gets a spill slot for its entire lifetime. If the parent
960 // of this interval already has a spill slot, there is nothing to do.
961 if (parent->HasSpillSlot()) {
962 return;
963 }
964
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100965 HInstruction* defined_by = parent->GetDefinedBy();
966 if (defined_by->IsParameterValue()) {
967 // Parameters have their own stack slot.
968 parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
969 return;
970 }
971
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100972 if (defined_by->IsConstant()) {
973 // Constants don't need a spill slot.
974 return;
975 }
976
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100977 LiveInterval* last_sibling = interval;
978 while (last_sibling->GetNextSibling() != nullptr) {
979 last_sibling = last_sibling->GetNextSibling();
980 }
981 size_t end = last_sibling->GetEnd();
982
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100983 // Find an available spill slot.
984 size_t slot = 0;
985 for (size_t e = spill_slots_.Size(); slot < e; ++slot) {
986 // We check if it is less rather than less or equal because the parallel move
987 // resolver does not work when a single spill slot needs to be exchanged with
988 // a double spill slot. The strict comparison avoids needing to exchange these
989 // locations at the same lifetime position.
990 if (spill_slots_.Get(slot) < parent->GetStart()
991 && (slot == (e - 1) || spill_slots_.Get(slot + 1) < parent->GetStart())) {
992 break;
993 }
994 }
995
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100996 if (parent->NeedsTwoSpillSlots()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100997 if (slot == spill_slots_.Size()) {
998 // We need a new spill slot.
999 spill_slots_.Add(end);
1000 spill_slots_.Add(end);
1001 } else if (slot == spill_slots_.Size() - 1) {
1002 spill_slots_.Put(slot, end);
1003 spill_slots_.Add(end);
1004 } else {
1005 spill_slots_.Put(slot, end);
1006 spill_slots_.Put(slot + 1, end);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001007 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001008 } else {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001009 if (slot == spill_slots_.Size()) {
1010 // We need a new spill slot.
1011 spill_slots_.Add(end);
1012 } else {
1013 spill_slots_.Put(slot, end);
1014 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001015 }
1016
Nicolas Geoffray39468442014-09-02 15:17:15 +01001017 parent->SetSpillSlot((slot + reserved_out_slots_) * kVRegSize);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001018}
1019
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001020static bool IsValidDestination(Location destination) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001021 return destination.IsRegister()
1022 || destination.IsFpuRegister()
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001023 || destination.IsFpuRegisterPair()
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001024 || destination.IsStackSlot()
1025 || destination.IsDoubleStackSlot();
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001026}
1027
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001028void RegisterAllocator::AddInputMoveFor(HInstruction* user,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001029 Location source,
1030 Location destination) const {
1031 if (source.Equals(destination)) return;
1032
Roland Levillain476df552014-10-09 17:51:36 +01001033 DCHECK(!user->IsPhi());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001034
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001035 HInstruction* previous = user->GetPrevious();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001036 HParallelMove* move = nullptr;
1037 if (previous == nullptr
Roland Levillain476df552014-10-09 17:51:36 +01001038 || !previous->IsParallelMove()
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001039 || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001040 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001041 move->SetLifetimePosition(user->GetLifetimePosition());
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001042 user->GetBlock()->InsertInstructionBefore(move, user);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001043 } else {
1044 move = previous->AsParallelMove();
1045 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001046 DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001047 move->AddMove(new (allocator_) MoveOperands(source, destination, nullptr));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001048}
1049
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001050static bool IsInstructionStart(size_t position) {
1051 return (position & 1) == 0;
1052}
1053
1054static bool IsInstructionEnd(size_t position) {
1055 return (position & 1) == 1;
1056}
1057
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001058void RegisterAllocator::InsertParallelMoveAt(size_t position,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001059 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001060 Location source,
1061 Location destination) const {
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001062 DCHECK(IsValidDestination(destination));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001063 if (source.Equals(destination)) return;
1064
1065 HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001066 HParallelMove* move;
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001067 if (at == nullptr) {
1068 if (IsInstructionStart(position)) {
1069 // Block boundary, don't do anything the connection of split siblings will handle it.
1070 return;
1071 } else {
1072 // Move must happen before the first instruction of the block.
1073 at = liveness_.GetInstructionFromPosition((position + 1) / 2);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001074 // Note that parallel moves may have already been inserted, so we explicitly
1075 // ask for the first instruction of the block: `GetInstructionFromPosition` does
1076 // not contain the moves.
1077 at = at->GetBlock()->GetFirstInstruction();
1078 if (at->GetLifetimePosition() != position) {
1079 DCHECK_GT(at->GetLifetimePosition(), position);
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001080 move = new (allocator_) HParallelMove(allocator_);
1081 move->SetLifetimePosition(position);
1082 at->GetBlock()->InsertInstructionBefore(move, at);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001083 } else {
1084 DCHECK(at->IsParallelMove());
1085 move = at->AsParallelMove();
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001086 }
1087 }
1088 } else if (IsInstructionEnd(position)) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001089 // Move must happen after the instruction.
1090 DCHECK(!at->IsControlFlow());
1091 move = at->GetNext()->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001092 // This is a parallel move for connecting siblings in a same block. We need to
1093 // differentiate it with moves for connecting blocks, and input moves.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001094 if (move == nullptr || move->GetLifetimePosition() > position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001095 move = new (allocator_) HParallelMove(allocator_);
1096 move->SetLifetimePosition(position);
1097 at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
1098 }
1099 } else {
1100 // Move must happen before the instruction.
1101 HInstruction* previous = at->GetPrevious();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001102 if (previous == nullptr
1103 || !previous->IsParallelMove()
1104 || previous->GetLifetimePosition() != position) {
1105 // If the previous is a parallel move, then its position must be lower
1106 // than the given `position`: it was added just after the non-parallel
1107 // move instruction that precedes `instruction`.
1108 DCHECK(previous == nullptr
1109 || !previous->IsParallelMove()
1110 || previous->GetLifetimePosition() < position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001111 move = new (allocator_) HParallelMove(allocator_);
1112 move->SetLifetimePosition(position);
1113 at->GetBlock()->InsertInstructionBefore(move, at);
1114 } else {
1115 move = previous->AsParallelMove();
1116 }
1117 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001118 DCHECK_EQ(move->GetLifetimePosition(), position);
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001119 move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001120}
1121
1122void RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001123 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001124 Location source,
1125 Location destination) const {
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001126 DCHECK(IsValidDestination(destination));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001127 if (source.Equals(destination)) return;
1128
1129 DCHECK_EQ(block->GetSuccessors().Size(), 1u);
1130 HInstruction* last = block->GetLastInstruction();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001131 // We insert moves at exit for phi predecessors and connecting blocks.
1132 // A block ending with an if cannot branch to a block with phis because
1133 // we do not allow critical edges. It can also not connect
1134 // a split interval between two blocks: the move has to happen in the successor.
1135 DCHECK(!last->IsIf());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001136 HInstruction* previous = last->GetPrevious();
1137 HParallelMove* move;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001138 // This is a parallel move for connecting blocks. We need to differentiate
1139 // it with moves for connecting siblings in a same block, and output moves.
Nicolas Geoffray59768572014-12-01 09:50:04 +00001140 size_t position = last->GetLifetimePosition();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001141 if (previous == nullptr || !previous->IsParallelMove()
Nicolas Geoffray59768572014-12-01 09:50:04 +00001142 || previous->AsParallelMove()->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001143 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001144 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001145 block->InsertInstructionBefore(move, last);
1146 } else {
1147 move = previous->AsParallelMove();
1148 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001149 move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001150}
1151
1152void RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001153 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001154 Location source,
1155 Location destination) const {
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001156 DCHECK(IsValidDestination(destination));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001157 if (source.Equals(destination)) return;
1158
1159 HInstruction* first = block->GetFirstInstruction();
1160 HParallelMove* move = first->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001161 // This is a parallel move for connecting blocks. We need to differentiate
1162 // it with moves for connecting siblings in a same block, and input moves.
1163 if (move == nullptr || move->GetLifetimePosition() != block->GetLifetimeStart()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001164 move = new (allocator_) HParallelMove(allocator_);
1165 move->SetLifetimePosition(block->GetLifetimeStart());
1166 block->InsertInstructionBefore(move, first);
1167 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001168 move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001169}
1170
1171void RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
1172 Location source,
1173 Location destination) const {
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001174 DCHECK(IsValidDestination(destination));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001175 if (source.Equals(destination)) return;
1176
Roland Levillain476df552014-10-09 17:51:36 +01001177 if (instruction->IsPhi()) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001178 InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001179 return;
1180 }
1181
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001182 size_t position = instruction->GetLifetimePosition() + 1;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001183 HParallelMove* move = instruction->GetNext()->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001184 // This is a parallel move for moving the output of an instruction. We need
1185 // to differentiate with input moves, moves for connecting siblings in a
1186 // and moves for connecting blocks.
1187 if (move == nullptr || move->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001188 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001189 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001190 instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
1191 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001192 move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001193}
1194
1195void RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
1196 LiveInterval* current = interval;
1197 if (current->HasSpillSlot() && current->HasRegister()) {
1198 // We spill eagerly, so move must be at definition.
1199 InsertMoveAfter(interval->GetDefinedBy(),
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001200 interval->ToLocation(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001201 interval->NeedsTwoSpillSlots()
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001202 ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1203 : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001204 }
1205 UsePosition* use = current->GetFirstUse();
1206
1207 // Walk over all siblings, updating locations of use positions, and
1208 // connecting them when they are adjacent.
1209 do {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001210 Location source = current->ToLocation();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001211
1212 // Walk over all uses covered by this interval, and update the location
1213 // information.
1214 while (use != nullptr && use->GetPosition() <= current->GetEnd()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001215 LocationSummary* locations = use->GetUser()->GetLocations();
1216 if (use->GetIsEnvironment()) {
1217 locations->SetEnvironmentAt(use->GetInputIndex(), source);
1218 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001219 Location expected_location = locations->InAt(use->GetInputIndex());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001220 // The expected (actual) location may be invalid in case the input is unused. Currently
1221 // this only happens for intrinsics.
1222 if (expected_location.IsValid()) {
1223 if (expected_location.IsUnallocated()) {
1224 locations->SetInAt(use->GetInputIndex(), source);
1225 } else if (!expected_location.IsConstant()) {
1226 AddInputMoveFor(use->GetUser(), source, expected_location);
1227 }
1228 } else {
1229 DCHECK(use->GetUser()->IsInvoke());
1230 DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001231 }
1232 }
1233 use = use->GetNext();
1234 }
1235
1236 // If the next interval starts just after this one, and has a register,
1237 // insert a move.
1238 LiveInterval* next_sibling = current->GetNextSibling();
1239 if (next_sibling != nullptr
1240 && next_sibling->HasRegister()
1241 && current->GetEnd() == next_sibling->GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001242 Location destination = next_sibling->ToLocation();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001243 InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001244 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001245
1246 // At each safepoint, we record stack and register information.
1247 for (size_t i = 0, e = safepoints_.Size(); i < e; ++i) {
1248 HInstruction* safepoint = safepoints_.Get(i);
1249 size_t position = safepoint->GetLifetimePosition();
1250 LocationSummary* locations = safepoint->GetLocations();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001251 if (!current->Covers(position)) {
1252 continue;
1253 }
1254 if (interval->GetStart() == position) {
1255 // The safepoint is for this instruction, so the location of the instruction
1256 // does not need to be saved.
1257 continue;
1258 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001259
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001260 if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001261 locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
1262 }
1263
1264 switch (source.GetKind()) {
1265 case Location::kRegister: {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001266 locations->AddLiveRegister(source);
Mark Mendellf85a9ca2015-01-13 09:20:58 -05001267 DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1268 maximum_number_of_live_core_registers_ +
1269 maximum_number_of_live_fp_registers_);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001270 if (current->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001271 locations->SetRegisterBit(source.reg());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001272 }
1273 break;
1274 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001275 case Location::kFpuRegister: {
1276 locations->AddLiveRegister(source);
1277 break;
1278 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001279 case Location::kFpuRegisterPair: {
1280 locations->AddLiveRegister(source.ToLow());
1281 locations->AddLiveRegister(source.ToHigh());
1282 break;
1283 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001284 case Location::kStackSlot: // Fall-through
1285 case Location::kDoubleStackSlot: // Fall-through
1286 case Location::kConstant: {
1287 // Nothing to do.
1288 break;
1289 }
1290 default: {
1291 LOG(FATAL) << "Unexpected location for object";
1292 }
1293 }
1294 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001295 current = next_sibling;
1296 } while (current != nullptr);
1297 DCHECK(use == nullptr);
1298}
1299
1300void RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
1301 HBasicBlock* from,
1302 HBasicBlock* to) const {
1303 if (interval->GetNextSibling() == nullptr) {
1304 // Nothing to connect. The whole range was allocated to the same location.
1305 return;
1306 }
1307
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001308 // Intervals end at the lifetime end of a block. The decrement by one
1309 // ensures the `Cover` call will return true.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001310 size_t from_position = from->GetLifetimeEnd() - 1;
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001311 size_t to_position = to->GetLifetimeStart();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001312
1313 LiveInterval* destination = nullptr;
1314 LiveInterval* source = nullptr;
1315
1316 LiveInterval* current = interval;
1317
1318 // Check the intervals that cover `from` and `to`.
1319 while ((current != nullptr) && (source == nullptr || destination == nullptr)) {
1320 if (current->Covers(from_position)) {
1321 DCHECK(source == nullptr);
1322 source = current;
1323 }
1324 if (current->Covers(to_position)) {
1325 DCHECK(destination == nullptr);
1326 destination = current;
1327 }
1328
1329 current = current->GetNextSibling();
1330 }
1331
1332 if (destination == source) {
1333 // Interval was not split.
1334 return;
1335 }
1336
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +01001337 DCHECK(destination != nullptr && source != nullptr);
1338
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001339 if (!destination->HasRegister()) {
1340 // Values are eagerly spilled. Spill slot already contains appropriate value.
1341 return;
1342 }
1343
1344 // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
1345 // we need to put the moves at the entry of `to`.
1346 if (from->GetSuccessors().Size() == 1) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001347 InsertParallelMoveAtExitOf(from,
1348 interval->GetParent()->GetDefinedBy(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001349 source->ToLocation(),
1350 destination->ToLocation());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001351 } else {
1352 DCHECK_EQ(to->GetPredecessors().Size(), 1u);
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001353 InsertParallelMoveAtEntryOf(to,
1354 interval->GetParent()->GetDefinedBy(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001355 source->ToLocation(),
1356 destination->ToLocation());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001357 }
1358}
1359
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001360void RegisterAllocator::Resolve() {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001361 codegen_->ComputeFrameSize(
Mark Mendellf85a9ca2015-01-13 09:20:58 -05001362 spill_slots_.Size(), maximum_number_of_live_core_registers_,
1363 maximum_number_of_live_fp_registers_, reserved_out_slots_);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001364
1365 // Adjust the Out Location of instructions.
1366 // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
1367 for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
1368 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
1369 LiveInterval* current = instruction->GetLiveInterval();
1370 LocationSummary* locations = instruction->GetLocations();
1371 Location location = locations->Out();
Roland Levillain476df552014-10-09 17:51:36 +01001372 if (instruction->IsParameterValue()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001373 // Now that we know the frame size, adjust the parameter's location.
1374 if (location.IsStackSlot()) {
1375 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1376 current->SetSpillSlot(location.GetStackIndex());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001377 locations->UpdateOut(location);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001378 } else if (location.IsDoubleStackSlot()) {
1379 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1380 current->SetSpillSlot(location.GetStackIndex());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001381 locations->UpdateOut(location);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001382 } else if (current->HasSpillSlot()) {
1383 current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
1384 }
1385 }
1386
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001387 Location source = current->ToLocation();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001388
1389 if (location.IsUnallocated()) {
1390 if (location.GetPolicy() == Location::kSameAsFirstInput) {
Calin Juravled0d48522014-11-04 16:40:20 +00001391 if (locations->InAt(0).IsUnallocated()) {
1392 locations->SetInAt(0, source);
1393 } else {
1394 DCHECK(locations->InAt(0).Equals(source));
1395 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001396 }
1397 locations->SetOut(source);
1398 } else {
1399 DCHECK(source.Equals(location));
1400 }
1401 }
1402
1403 // Connect siblings.
1404 for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
1405 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
1406 ConnectSiblings(instruction->GetLiveInterval());
1407 }
1408
1409 // Resolve non-linear control flow across branches. Order does not matter.
1410 for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
1411 HBasicBlock* block = it.Current();
1412 BitVector* live = liveness_.GetLiveInSet(*block);
1413 for (uint32_t idx : live->Indexes()) {
1414 HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
1415 LiveInterval* interval = current->GetLiveInterval();
1416 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
1417 ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
1418 }
1419 }
1420 }
1421
1422 // Resolve phi inputs. Order does not matter.
1423 for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
1424 HBasicBlock* current = it.Current();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001425 for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1426 HInstruction* phi = inst_it.Current();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001427 for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
1428 HBasicBlock* predecessor = current->GetPredecessors().Get(i);
1429 DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
1430 HInstruction* input = phi->InputAt(i);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001431 Location source = input->GetLiveInterval()->GetLocationAt(
1432 predecessor->GetLifetimeEnd() - 1);
1433 Location destination = phi->GetLiveInterval()->ToLocation();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001434 InsertParallelMoveAtExitOf(predecessor, nullptr, source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001435 }
1436 }
1437 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001438
1439 // Assign temp locations.
1440 HInstruction* current = nullptr;
1441 size_t temp_index = 0;
1442 for (size_t i = 0; i < temp_intervals_.Size(); ++i) {
1443 LiveInterval* temp = temp_intervals_.Get(i);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001444 if (temp->IsHighInterval()) {
1445 // High intervals can be skipped, they are already handled by the low interval.
1446 continue;
1447 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001448 HInstruction* at = liveness_.GetTempUser(temp);
1449 if (at != current) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001450 temp_index = 0;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001451 current = at;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001452 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001453 LocationSummary* locations = at->GetLocations();
Roland Levillain5368c212014-11-27 15:03:41 +00001454 switch (temp->GetType()) {
1455 case Primitive::kPrimInt:
1456 locations->SetTempAt(
1457 temp_index++, Location::RegisterLocation(temp->GetRegister()));
1458 break;
1459
1460 case Primitive::kPrimDouble:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001461 if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1462 Location location = Location::FpuRegisterPairLocation(
1463 temp->GetRegister(), temp->GetHighInterval()->GetRegister());
1464 locations->SetTempAt(temp_index++, location);
1465 } else {
1466 locations->SetTempAt(
1467 temp_index++, Location::FpuRegisterLocation(temp->GetRegister()));
1468 }
Roland Levillain5368c212014-11-27 15:03:41 +00001469 break;
1470
1471 default:
1472 LOG(FATAL) << "Unexpected type for temporary location "
1473 << temp->GetType();
1474 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001475 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001476}
1477
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001478} // namespace art