blob: e6b2a8bb3e8058e2b10b09b6472f5fd0f9cfd53d [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),
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010046 unhandled_core_intervals_(allocator->Adapter(kArenaAllocRegisterAllocator)),
47 unhandled_fp_intervals_(allocator->Adapter(kArenaAllocRegisterAllocator)),
Nicolas Geoffray39468442014-09-02 15:17:15 +010048 unhandled_(nullptr),
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010049 handled_(allocator->Adapter(kArenaAllocRegisterAllocator)),
50 active_(allocator->Adapter(kArenaAllocRegisterAllocator)),
51 inactive_(allocator->Adapter(kArenaAllocRegisterAllocator)),
52 physical_core_register_intervals_(allocator->Adapter(kArenaAllocRegisterAllocator)),
53 physical_fp_register_intervals_(allocator->Adapter(kArenaAllocRegisterAllocator)),
54 temp_intervals_(allocator->Adapter(kArenaAllocRegisterAllocator)),
55 int_spill_slots_(allocator->Adapter(kArenaAllocRegisterAllocator)),
56 long_spill_slots_(allocator->Adapter(kArenaAllocRegisterAllocator)),
57 float_spill_slots_(allocator->Adapter(kArenaAllocRegisterAllocator)),
58 double_spill_slots_(allocator->Adapter(kArenaAllocRegisterAllocator)),
David Brazdil77a48ae2015-09-15 12:34:04 +000059 catch_phi_spill_slots_(0),
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010060 safepoints_(allocator->Adapter(kArenaAllocRegisterAllocator)),
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010061 processing_core_registers_(false),
62 number_of_registers_(-1),
63 registers_array_(nullptr),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010064 blocked_core_registers_(codegen->GetBlockedCoreRegisters()),
65 blocked_fp_registers_(codegen->GetBlockedFloatingPointRegisters()),
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +010066 reserved_out_slots_(0),
Mark Mendellf85a9ca2015-01-13 09:20:58 -050067 maximum_number_of_live_core_registers_(0),
68 maximum_number_of_live_fp_registers_(0) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010069 temp_intervals_.reserve(4);
70 int_spill_slots_.reserve(kDefaultNumberOfSpillSlots);
71 long_spill_slots_.reserve(kDefaultNumberOfSpillSlots);
72 float_spill_slots_.reserve(kDefaultNumberOfSpillSlots);
73 double_spill_slots_.reserve(kDefaultNumberOfSpillSlots);
74
Nicolas Geoffray98893962015-01-21 12:32:32 +000075 static constexpr bool kIsBaseline = false;
76 codegen->SetupBlockedRegisters(kIsBaseline);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010077 physical_core_register_intervals_.resize(codegen->GetNumberOfCoreRegisters(), nullptr);
78 physical_fp_register_intervals_.resize(codegen->GetNumberOfFloatingPointRegisters(), nullptr);
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 // Always reserve for the current method and the graph's max out registers.
80 // TODO: compute it instead.
Mathieu Chartiere401d142015-04-22 13:56:20 -070081 // ArtMethod* takes 2 vregs for 64 bits.
82 reserved_out_slots_ = InstructionSetPointerSize(codegen->GetInstructionSet()) / kVRegSize +
83 codegen->GetGraph()->GetMaximumNumberOfOutVRegs();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010084}
85
Nicolas Geoffray234d69d2015-03-09 10:28:50 +000086bool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph ATTRIBUTE_UNUSED,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010087 InstructionSet instruction_set) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +000088 return instruction_set == kArm64
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000089 || instruction_set == kX86_64
Alexey Frunze4dda3372015-06-01 18:31:49 -070090 || instruction_set == kMips64
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000091 || instruction_set == kArm
Nicolas Geoffray234d69d2015-03-09 10:28:50 +000092 || instruction_set == kX86
93 || instruction_set == kThumb2;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010094}
95
96static bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) {
Nicolas Geoffray39468442014-09-02 15:17:15 +010097 if (interval == nullptr) return false;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010098 bool is_core_register = (interval->GetType() != Primitive::kPrimDouble)
99 && (interval->GetType() != Primitive::kPrimFloat);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100100 return processing_core_registers == is_core_register;
101}
102
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100103void RegisterAllocator::AllocateRegisters() {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100104 AllocateRegistersInternal();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100105 Resolve();
106
107 if (kIsDebugBuild) {
108 processing_core_registers_ = true;
109 ValidateInternal(true);
110 processing_core_registers_ = false;
111 ValidateInternal(true);
Nicolas Geoffray59768572014-12-01 09:50:04 +0000112 // Check that the linear order is still correct with regards to lifetime positions.
113 // Since only parallel moves have been inserted during the register allocation,
114 // these checks are mostly for making sure these moves have been added correctly.
115 size_t current_liveness = 0;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100116 for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
Nicolas Geoffray59768572014-12-01 09:50:04 +0000117 HBasicBlock* block = it.Current();
118 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
119 HInstruction* instruction = inst_it.Current();
120 DCHECK_LE(current_liveness, instruction->GetLifetimePosition());
121 current_liveness = instruction->GetLifetimePosition();
122 }
123 for (HInstructionIterator inst_it(block->GetInstructions());
124 !inst_it.Done();
125 inst_it.Advance()) {
126 HInstruction* instruction = inst_it.Current();
127 DCHECK_LE(current_liveness, instruction->GetLifetimePosition()) << instruction->DebugName();
128 current_liveness = instruction->GetLifetimePosition();
129 }
130 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100131 }
132}
133
David Brazdil77a48ae2015-09-15 12:34:04 +0000134void RegisterAllocator::BlockRegister(Location location, size_t start, size_t end) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100135 int reg = location.reg();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100136 DCHECK(location.IsRegister() || location.IsFpuRegister());
137 LiveInterval* interval = location.IsRegister()
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100138 ? physical_core_register_intervals_[reg]
139 : physical_fp_register_intervals_[reg];
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100140 Primitive::Type type = location.IsRegister()
141 ? Primitive::kPrimInt
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000142 : Primitive::kPrimFloat;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100143 if (interval == nullptr) {
144 interval = LiveInterval::MakeFixedInterval(allocator_, reg, type);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100145 if (location.IsRegister()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100146 physical_core_register_intervals_[reg] = interval;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100147 } else {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100148 physical_fp_register_intervals_[reg] = interval;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100149 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100150 }
151 DCHECK(interval->GetRegister() == reg);
152 interval->AddRange(start, end);
153}
154
David Brazdil77a48ae2015-09-15 12:34:04 +0000155void RegisterAllocator::BlockRegisters(size_t start, size_t end, bool caller_save_only) {
156 for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) {
157 if (!caller_save_only || !codegen_->IsCoreCalleeSaveRegister(i)) {
158 BlockRegister(Location::RegisterLocation(i), start, end);
159 }
160 }
161 for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) {
162 if (!caller_save_only || !codegen_->IsFloatingPointCalleeSaveRegister(i)) {
163 BlockRegister(Location::FpuRegisterLocation(i), start, end);
164 }
165 }
166}
167
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100168void RegisterAllocator::AllocateRegistersInternal() {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100169 // Iterate post-order, to ensure the list is sorted, and the last added interval
170 // is the one with the lowest start position.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100171 for (HLinearPostOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100172 HBasicBlock* block = it.Current();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800173 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
174 back_it.Advance()) {
175 ProcessInstruction(back_it.Current());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100176 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800177 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
178 ProcessInstruction(inst_it.Current());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100179 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000180
181 if (block->IsCatchBlock()) {
182 // By blocking all registers at the top of each catch block, we force
183 // intervals used after catch to spill.
184 size_t position = block->GetLifetimeStart();
185 BlockRegisters(position, position + 1);
186 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100187 }
188
Nicolas Geoffray39468442014-09-02 15:17:15 +0100189 number_of_registers_ = codegen_->GetNumberOfCoreRegisters();
190 registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
191 processing_core_registers_ = true;
192 unhandled_ = &unhandled_core_intervals_;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100193 for (LiveInterval* fixed : physical_core_register_intervals_) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100194 if (fixed != nullptr) {
Mingyao Yang296bd602014-10-06 16:47:28 -0700195 // Fixed interval is added to inactive_ instead of unhandled_.
196 // It's also the only type of inactive interval whose start position
197 // can be after the current interval during linear scan.
198 // Fixed interval is never split and never moves to unhandled_.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100199 inactive_.push_back(fixed);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100200 }
201 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100202 LinearScan();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100203
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100204 inactive_.clear();
205 active_.clear();
206 handled_.clear();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100207
208 number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters();
209 registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
210 processing_core_registers_ = false;
211 unhandled_ = &unhandled_fp_intervals_;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100212 for (LiveInterval* fixed : physical_fp_register_intervals_) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100213 if (fixed != nullptr) {
Mingyao Yang296bd602014-10-06 16:47:28 -0700214 // Fixed interval is added to inactive_ instead of unhandled_.
215 // It's also the only type of inactive interval whose start position
216 // can be after the current interval during linear scan.
217 // Fixed interval is never split and never moves to unhandled_.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100218 inactive_.push_back(fixed);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100219 }
220 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100221 LinearScan();
222}
223
224void RegisterAllocator::ProcessInstruction(HInstruction* instruction) {
225 LocationSummary* locations = instruction->GetLocations();
226 size_t position = instruction->GetLifetimePosition();
227
228 if (locations == nullptr) return;
229
230 // Create synthesized intervals for temporaries.
231 for (size_t i = 0; i < locations->GetTempCount(); ++i) {
232 Location temp = locations->GetTemp(i);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000233 if (temp.IsRegister() || temp.IsFpuRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100234 BlockRegister(temp, position, position + 1);
Nicolas Geoffray45b83af2015-07-06 15:12:53 +0000235 // Ensure that an explicit temporary register is marked as being allocated.
236 codegen_->AddAllocatedRegister(temp);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100237 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100238 DCHECK(temp.IsUnallocated());
Roland Levillain5368c212014-11-27 15:03:41 +0000239 switch (temp.GetPolicy()) {
240 case Location::kRequiresRegister: {
241 LiveInterval* interval =
242 LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100243 temp_intervals_.push_back(interval);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000244 interval->AddTempUse(instruction, i);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100245 unhandled_core_intervals_.push_back(interval);
Roland Levillain5368c212014-11-27 15:03:41 +0000246 break;
247 }
248
249 case Location::kRequiresFpuRegister: {
250 LiveInterval* interval =
251 LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimDouble);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100252 temp_intervals_.push_back(interval);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000253 interval->AddTempUse(instruction, i);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000254 if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100255 interval->AddHighInterval(/* is_temp */ true);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000256 LiveInterval* high = interval->GetHighInterval();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100257 temp_intervals_.push_back(high);
258 unhandled_fp_intervals_.push_back(high);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000259 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100260 unhandled_fp_intervals_.push_back(interval);
Roland Levillain5368c212014-11-27 15:03:41 +0000261 break;
262 }
263
264 default:
265 LOG(FATAL) << "Unexpected policy for temporary location "
266 << temp.GetPolicy();
267 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100268 }
269 }
270
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100271 bool core_register = (instruction->GetType() != Primitive::kPrimDouble)
272 && (instruction->GetType() != Primitive::kPrimFloat);
273
Alexandre Rames8158f282015-08-07 10:26:17 +0100274 if (locations->NeedsSafepoint()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000275 if (codegen_->IsLeafMethod()) {
276 // TODO: We do this here because we do not want the suspend check to artificially
277 // create live registers. We should find another place, but this is currently the
278 // simplest.
279 DCHECK(instruction->IsSuspendCheckEntry());
280 instruction->GetBlock()->RemoveInstruction(instruction);
281 return;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100282 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100283 safepoints_.push_back(instruction);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100284 if (locations->OnlyCallsOnSlowPath()) {
285 // We add a synthesized range at this position to record the live registers
286 // at this position. Ideally, we could just update the safepoints when locations
287 // are updated, but we currently need to know the full stack size before updating
288 // locations (because of parameters and the fact that we don't have a frame pointer).
289 // And knowing the full stack size requires to know the maximum number of live
290 // registers at calls in slow paths.
291 // By adding the following interval in the algorithm, we can compute this
292 // maximum before updating locations.
293 LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction);
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000294 interval->AddRange(position, position + 1);
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000295 AddSorted(&unhandled_core_intervals_, interval);
296 AddSorted(&unhandled_fp_intervals_, interval);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100297 }
298 }
299
300 if (locations->WillCall()) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000301 BlockRegisters(position, position + 1, /* caller_save_only */ true);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100302 }
303
304 for (size_t i = 0; i < instruction->InputCount(); ++i) {
305 Location input = locations->InAt(i);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100306 if (input.IsRegister() || input.IsFpuRegister()) {
307 BlockRegister(input, position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000308 } else if (input.IsPair()) {
309 BlockRegister(input.ToLow(), position, position + 1);
310 BlockRegister(input.ToHigh(), position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100311 }
312 }
313
Nicolas Geoffray39468442014-09-02 15:17:15 +0100314 LiveInterval* current = instruction->GetLiveInterval();
315 if (current == nullptr) return;
316
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100317 ArenaVector<LiveInterval*>& unhandled = core_register
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100318 ? unhandled_core_intervals_
319 : unhandled_fp_intervals_;
320
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100321 DCHECK(unhandled.empty() || current->StartsBeforeOrAt(unhandled.back()));
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000322
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000323 if (codegen_->NeedsTwoRegisters(current->GetType())) {
324 current->AddHighInterval();
325 }
326
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100327 for (size_t safepoint_index = safepoints_.size(); safepoint_index > 0; --safepoint_index) {
328 HInstruction* safepoint = safepoints_[safepoint_index - 1u];
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100329 size_t safepoint_position = safepoint->GetLifetimePosition();
330
331 // Test that safepoints are ordered in the optimal way.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100332 DCHECK(safepoint_index == safepoints_.size() ||
333 safepoints_[safepoint_index]->GetLifetimePosition() < safepoint_position);
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100334
335 if (safepoint_position == current->GetStart()) {
336 // The safepoint is for this instruction, so the location of the instruction
337 // does not need to be saved.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100338 DCHECK_EQ(safepoint_index, safepoints_.size());
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100339 DCHECK_EQ(safepoint, instruction);
340 continue;
341 } else if (current->IsDeadAt(safepoint_position)) {
342 break;
343 } else if (!current->Covers(safepoint_position)) {
344 // Hole in the interval.
345 continue;
346 }
347 current->AddSafepoint(safepoint);
348 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100349 current->ResetSearchCache();
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100350
Nicolas Geoffray39468442014-09-02 15:17:15 +0100351 // Some instructions define their output in fixed register/stack slot. We need
352 // to ensure we know these locations before doing register allocation. For a
353 // given register, we create an interval that covers these locations. The register
354 // will be unavailable at these locations when trying to allocate one for an
355 // interval.
356 //
357 // The backwards walking ensures the ranges are ordered on increasing start positions.
358 Location output = locations->Out();
Calin Juravled0d48522014-11-04 16:40:20 +0000359 if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
360 Location first = locations->InAt(0);
361 if (first.IsRegister() || first.IsFpuRegister()) {
362 current->SetFrom(position + 1);
363 current->SetRegister(first.reg());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000364 } else if (first.IsPair()) {
365 current->SetFrom(position + 1);
366 current->SetRegister(first.low());
367 LiveInterval* high = current->GetHighInterval();
368 high->SetRegister(first.high());
369 high->SetFrom(position + 1);
Calin Juravled0d48522014-11-04 16:40:20 +0000370 }
371 } else if (output.IsRegister() || output.IsFpuRegister()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100372 // Shift the interval's start by one to account for the blocked register.
373 current->SetFrom(position + 1);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100374 current->SetRegister(output.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100375 BlockRegister(output, position, position + 1);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000376 } else if (output.IsPair()) {
377 current->SetFrom(position + 1);
378 current->SetRegister(output.low());
379 LiveInterval* high = current->GetHighInterval();
380 high->SetRegister(output.high());
381 high->SetFrom(position + 1);
382 BlockRegister(output.ToLow(), position, position + 1);
383 BlockRegister(output.ToHigh(), position, position + 1);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100384 } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
385 current->SetSpillSlot(output.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000386 } else {
387 DCHECK(output.IsUnallocated() || output.IsConstant());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100388 }
389
David Brazdil77a48ae2015-09-15 12:34:04 +0000390 if (instruction->IsPhi() && instruction->AsPhi()->IsCatchPhi()) {
391 AllocateSpillSlotForCatchPhi(instruction->AsPhi());
392 }
393
Nicolas Geoffray39468442014-09-02 15:17:15 +0100394 // If needed, add interval to the list of unhandled intervals.
395 if (current->HasSpillSlot() || instruction->IsConstant()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100396 // Split just before first register use.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100397 size_t first_register_use = current->FirstRegisterUse();
398 if (first_register_use != kNoLifetime) {
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +0100399 LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000400 // Don't add directly to `unhandled`, it needs to be sorted and the start
Nicolas Geoffray39468442014-09-02 15:17:15 +0100401 // of this new interval might be after intervals already in the list.
402 AddSorted(&unhandled, split);
403 } else {
404 // Nothing to do, we won't allocate a register for this value.
405 }
406 } else {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000407 // Don't add directly to `unhandled`, temp or safepoint intervals
408 // for this instruction may have been added, and those can be
409 // processed first.
410 AddSorted(&unhandled, current);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100411 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100412}
413
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100414class AllRangesIterator : public ValueObject {
415 public:
416 explicit AllRangesIterator(LiveInterval* interval)
417 : current_interval_(interval),
418 current_range_(interval->GetFirstRange()) {}
419
420 bool Done() const { return current_interval_ == nullptr; }
421 LiveRange* CurrentRange() const { return current_range_; }
422 LiveInterval* CurrentInterval() const { return current_interval_; }
423
424 void Advance() {
425 current_range_ = current_range_->GetNext();
426 if (current_range_ == nullptr) {
427 current_interval_ = current_interval_->GetNextSibling();
428 if (current_interval_ != nullptr) {
429 current_range_ = current_interval_->GetFirstRange();
430 }
431 }
432 }
433
434 private:
435 LiveInterval* current_interval_;
436 LiveRange* current_range_;
437
438 DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
439};
440
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100441bool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
442 // To simplify unit testing, we eagerly create the array of intervals, and
443 // call the helper method.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100444 ArenaVector<LiveInterval*> intervals(allocator_->Adapter(kArenaAllocRegisterAllocator));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100445 for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
446 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
447 if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100448 intervals.push_back(instruction->GetLiveInterval());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100449 }
450 }
451
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100452 const ArenaVector<LiveInterval*>* physical_register_intervals = processing_core_registers_
453 ? &physical_core_register_intervals_
454 : &physical_fp_register_intervals_;
455 for (LiveInterval* fixed : *physical_register_intervals) {
456 if (fixed != nullptr) {
457 intervals.push_back(fixed);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100458 }
459 }
460
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100461 for (LiveInterval* temp : temp_intervals_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100462 if (ShouldProcess(processing_core_registers_, temp)) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100463 intervals.push_back(temp);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100464 }
465 }
466
Nicolas Geoffray776b3182015-02-23 14:14:57 +0000467 return ValidateIntervals(intervals, GetNumberOfSpillSlots(), reserved_out_slots_, *codegen_,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100468 allocator_, processing_core_registers_, log_fatal_on_failure);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100469}
470
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100471bool RegisterAllocator::ValidateIntervals(const ArenaVector<LiveInterval*>& intervals,
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100472 size_t number_of_spill_slots,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100473 size_t number_of_out_slots,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100474 const CodeGenerator& codegen,
475 ArenaAllocator* allocator,
476 bool processing_core_registers,
477 bool log_fatal_on_failure) {
478 size_t number_of_registers = processing_core_registers
479 ? codegen.GetNumberOfCoreRegisters()
480 : codegen.GetNumberOfFloatingPointRegisters();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100481 ArenaVector<ArenaBitVector*> liveness_of_values(
482 allocator->Adapter(kArenaAllocRegisterAllocator));
483 liveness_of_values.reserve(number_of_registers + number_of_spill_slots);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100484
485 // Allocate a bit vector per register. A live interval that has a register
486 // allocated will populate the associated bit vector based on its live ranges.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100487 for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100488 liveness_of_values.push_back(new (allocator) ArenaBitVector(allocator, 0, true));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100489 }
490
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100491 for (LiveInterval* start_interval : intervals) {
492 for (AllRangesIterator it(start_interval); !it.Done(); it.Advance()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100493 LiveInterval* current = it.CurrentInterval();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100494 HInstruction* defined_by = current->GetParent()->GetDefinedBy();
495 if (current->GetParent()->HasSpillSlot()
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100496 // Parameters and current method have their own stack slot.
497 && !(defined_by != nullptr && (defined_by->IsParameterValue()
498 || defined_by->IsCurrentMethod()))) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100499 BitVector* liveness_of_spill_slot = liveness_of_values[number_of_registers
Nicolas Geoffray39468442014-09-02 15:17:15 +0100500 + current->GetParent()->GetSpillSlot() / kVRegSize
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100501 - number_of_out_slots];
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100502 for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
503 if (liveness_of_spill_slot->IsBitSet(j)) {
504 if (log_fatal_on_failure) {
505 std::ostringstream message;
506 message << "Spill slot conflict at " << j;
507 LOG(FATAL) << message.str();
508 } else {
509 return false;
510 }
511 } else {
512 liveness_of_spill_slot->SetBit(j);
513 }
514 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100515 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100516
517 if (current->HasRegister()) {
Nicolas Geoffray45b83af2015-07-06 15:12:53 +0000518 if (kIsDebugBuild && log_fatal_on_failure && !current->IsFixed()) {
519 // Only check when an error is fatal. Only tests code ask for non-fatal failures
520 // and test code may not properly fill the right information to the code generator.
521 CHECK(codegen.HasAllocatedRegister(processing_core_registers, current->GetRegister()));
522 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100523 BitVector* liveness_of_register = liveness_of_values[current->GetRegister()];
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100524 for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
525 if (liveness_of_register->IsBitSet(j)) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000526 if (current->IsUsingInputRegister() && current->CanUseInputRegister()) {
527 continue;
528 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100529 if (log_fatal_on_failure) {
530 std::ostringstream message;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100531 message << "Register conflict at " << j << " ";
532 if (defined_by != nullptr) {
533 message << "(" << defined_by->DebugName() << ")";
534 }
535 message << "for ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100536 if (processing_core_registers) {
537 codegen.DumpCoreRegister(message, current->GetRegister());
538 } else {
539 codegen.DumpFloatingPointRegister(message, current->GetRegister());
540 }
541 LOG(FATAL) << message.str();
542 } else {
543 return false;
544 }
545 } else {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100546 liveness_of_register->SetBit(j);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100547 }
548 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100549 }
550 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100551 }
552 return true;
553}
554
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100555void RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100556 interval->Dump(stream);
557 stream << ": ";
558 if (interval->HasRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100559 if (interval->IsFloatingPoint()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100560 codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100561 } else {
562 codegen_->DumpCoreRegister(stream, interval->GetRegister());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100563 }
564 } else {
565 stream << "spilled";
566 }
567 stream << std::endl;
568}
569
Mingyao Yang296bd602014-10-06 16:47:28 -0700570void RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
571 stream << "inactive: " << std::endl;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100572 for (LiveInterval* inactive_interval : inactive_) {
573 DumpInterval(stream, inactive_interval);
Mingyao Yang296bd602014-10-06 16:47:28 -0700574 }
575 stream << "active: " << std::endl;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100576 for (LiveInterval* active_interval : active_) {
577 DumpInterval(stream, active_interval);
Mingyao Yang296bd602014-10-06 16:47:28 -0700578 }
579 stream << "unhandled: " << std::endl;
580 auto unhandled = (unhandled_ != nullptr) ?
581 unhandled_ : &unhandled_core_intervals_;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100582 for (LiveInterval* unhandled_interval : *unhandled) {
583 DumpInterval(stream, unhandled_interval);
Mingyao Yang296bd602014-10-06 16:47:28 -0700584 }
585 stream << "handled: " << std::endl;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100586 for (LiveInterval* handled_interval : handled_) {
587 DumpInterval(stream, handled_interval);
Mingyao Yang296bd602014-10-06 16:47:28 -0700588 }
589}
590
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100591// By the book implementation of a linear scan register allocator.
592void RegisterAllocator::LinearScan() {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100593 while (!unhandled_->empty()) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100594 // (1) Remove interval with the lowest start position from unhandled.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100595 LiveInterval* current = unhandled_->back();
596 unhandled_->pop_back();
Nicolas Geoffray2e92bc22015-08-20 19:52:26 +0100597
598 // Make sure the interval is an expected state.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100599 DCHECK(!current->IsFixed() && !current->HasSpillSlot());
Nicolas Geoffray2e92bc22015-08-20 19:52:26 +0100600 // Make sure we are going in the right order.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100601 DCHECK(unhandled_->empty() || unhandled_->back()->GetStart() >= current->GetStart());
Nicolas Geoffray2e92bc22015-08-20 19:52:26 +0100602 // Make sure a low interval is always with a high.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100603 DCHECK(!current->IsLowInterval() || unhandled_->back()->IsHighInterval());
Nicolas Geoffray2e92bc22015-08-20 19:52:26 +0100604 // Make sure a high interval is always with a low.
605 DCHECK(current->IsLowInterval() ||
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100606 unhandled_->empty() ||
607 !unhandled_->back()->IsHighInterval());
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000608
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100609 size_t position = current->GetStart();
610
Mingyao Yang296bd602014-10-06 16:47:28 -0700611 // Remember the inactive_ size here since the ones moved to inactive_ from
612 // active_ below shouldn't need to be re-checked.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100613 size_t inactive_intervals_to_handle = inactive_.size();
Mingyao Yang296bd602014-10-06 16:47:28 -0700614
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100615 // (2) Remove currently active intervals that are dead at this position.
616 // Move active intervals that have a lifetime hole at this position
617 // to inactive.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100618 // Note: Copy elements we keep to the beginning, just like
619 // v.erase(std::remove(v.begin(), v.end(), value), v.end());
620 auto active_kept_end = active_.begin();
621 for (auto it = active_.begin(), end = active_.end(); it != end; ++it) {
622 LiveInterval* interval = *it;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100623 if (interval->IsDeadAt(position)) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100624 handled_.push_back(interval);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100625 } else if (!interval->Covers(position)) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100626 inactive_.push_back(interval);
627 } else {
628 *active_kept_end++ = interval; // Keep this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100629 }
630 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100631 // We have copied what we want to keep to [active_.begin(), active_kept_end),
632 // the rest of the data in active_ is junk - drop it.
633 active_.erase(active_kept_end, active_.end());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100634
635 // (3) Remove currently inactive intervals that are dead at this position.
636 // Move inactive intervals that cover this position to active.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100637 // Note: Copy elements we keep to the beginning, just like
638 // v.erase(std::remove(v.begin(), v.begin() + num, value), v.begin() + num);
639 auto inactive_kept_end = inactive_.begin();
640 auto inactive_to_handle_end = inactive_.begin() + inactive_intervals_to_handle;
641 for (auto it = inactive_.begin(); it != inactive_to_handle_end; ++it) {
642 LiveInterval* interval = *it;
Mingyao Yang296bd602014-10-06 16:47:28 -0700643 DCHECK(interval->GetStart() < position || interval->IsFixed());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100644 if (interval->IsDeadAt(position)) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100645 handled_.push_back(interval);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100646 } else if (interval->Covers(position)) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100647 active_.push_back(interval);
648 } else {
649 *inactive_kept_end++ = interval; // Keep this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100650 }
651 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100652 // We have copied what we want to keep to [inactive_.begin(), inactive_kept_end),
653 // the rest of the data in the processed interval is junk - drop it.
654 inactive_.erase(inactive_kept_end, inactive_to_handle_end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100655
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000656 if (current->IsSlowPathSafepoint()) {
657 // Synthesized interval to record the maximum number of live registers
658 // at safepoints. No need to allocate a register for it.
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500659 if (processing_core_registers_) {
660 maximum_number_of_live_core_registers_ =
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100661 std::max(maximum_number_of_live_core_registers_, active_.size());
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500662 } else {
663 maximum_number_of_live_fp_registers_ =
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100664 std::max(maximum_number_of_live_fp_registers_, active_.size());
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500665 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100666 DCHECK(unhandled_->empty() || unhandled_->back()->GetStart() > current->GetStart());
Nicolas Geoffrayacd03392014-11-26 15:46:52 +0000667 continue;
668 }
669
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000670 if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
671 DCHECK(!current->HasRegister());
672 // Allocating the low part was unsucessful. The splitted interval for the high part
673 // will be handled next (it is in the `unhandled_` list).
674 continue;
675 }
676
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100677 // (4) Try to find an available register.
678 bool success = TryAllocateFreeReg(current);
679
680 // (5) If no register could be found, we need to spill.
681 if (!success) {
682 success = AllocateBlockedReg(current);
683 }
684
685 // (6) If the interval had a register allocated, add it to the list of active
686 // intervals.
687 if (success) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000688 codegen_->AddAllocatedRegister(processing_core_registers_
689 ? Location::RegisterLocation(current->GetRegister())
690 : Location::FpuRegisterLocation(current->GetRegister()));
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100691 active_.push_back(current);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000692 if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
693 current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
694 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100695 }
696 }
697}
698
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000699static void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) {
700 DCHECK(!interval->IsHighInterval());
701 // Note that the same instruction may occur multiple times in the input list,
702 // so `free_until` may have changed already.
David Brazdil3fc992f2015-04-16 18:31:55 +0100703 // Since `position` is not the current scan position, we need to use CoversSlow.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000704 if (interval->IsDeadAt(position)) {
705 // Set the register to be free. Note that inactive intervals might later
706 // update this.
707 free_until[interval->GetRegister()] = kMaxLifetimePosition;
708 if (interval->HasHighInterval()) {
709 DCHECK(interval->GetHighInterval()->IsDeadAt(position));
710 free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition;
711 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100712 } else if (!interval->CoversSlow(position)) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000713 // The interval becomes inactive at `defined_by`. We make its register
714 // available only until the next use strictly after `defined_by`.
715 free_until[interval->GetRegister()] = interval->FirstUseAfter(position);
716 if (interval->HasHighInterval()) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100717 DCHECK(!interval->GetHighInterval()->CoversSlow(position));
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000718 free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()];
719 }
720 }
721}
722
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100723// Find a free register. If multiple are found, pick the register that
724// is free the longest.
725bool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
726 size_t* free_until = registers_array_;
727
728 // First set all registers to be free.
729 for (size_t i = 0; i < number_of_registers_; ++i) {
730 free_until[i] = kMaxLifetimePosition;
731 }
732
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100733 // For each active interval, set its register to not free.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100734 for (LiveInterval* interval : active_) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100735 DCHECK(interval->HasRegister());
736 free_until[interval->GetRegister()] = 0;
737 }
738
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000739 // An interval that starts an instruction (that is, it is not split), may
740 // re-use the registers used by the inputs of that instruciton, based on the
741 // location summary.
742 HInstruction* defined_by = current->GetDefinedBy();
743 if (defined_by != nullptr && !current->IsSplit()) {
744 LocationSummary* locations = defined_by->GetLocations();
745 if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) {
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100746 for (size_t i = 0, e = defined_by->InputCount(); i < e; ++i) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000747 // Take the last interval of the input. It is the location of that interval
748 // that will be used at `defined_by`.
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100749 LiveInterval* interval = defined_by->InputAt(i)->GetLiveInterval()->GetLastSibling();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000750 // Note that interval may have not been processed yet.
751 // TODO: Handle non-split intervals last in the work list.
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100752 if (locations->InAt(i).IsValid()
753 && interval->HasRegister()
754 && interval->SameRegisterKind(*current)) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000755 // The input must be live until the end of `defined_by`, to comply to
756 // the linear scan algorithm. So we use `defined_by`'s end lifetime
757 // position to check whether the input is dead or is inactive after
758 // `defined_by`.
David Brazdil3fc992f2015-04-16 18:31:55 +0100759 DCHECK(interval->CoversSlow(defined_by->GetLifetimePosition()));
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000760 size_t position = defined_by->GetLifetimePosition() + 1;
761 FreeIfNotCoverAt(interval, position, free_until);
762 }
763 }
764 }
765 }
766
Mingyao Yang296bd602014-10-06 16:47:28 -0700767 // For each inactive interval, set its register to be free until
768 // the next intersection with `current`.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100769 for (LiveInterval* inactive : inactive_) {
Mingyao Yang296bd602014-10-06 16:47:28 -0700770 // Temp/Slow-path-safepoint interval has no holes.
771 DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
772 if (!current->IsSplit() && !inactive->IsFixed()) {
773 // Neither current nor inactive are fixed.
774 // Thanks to SSA, a non-split interval starting in a hole of an
775 // inactive interval should never intersect with that inactive interval.
776 // Only if it's not fixed though, because fixed intervals don't come from SSA.
777 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
778 continue;
779 }
780
781 DCHECK(inactive->HasRegister());
782 if (free_until[inactive->GetRegister()] == 0) {
783 // Already used by some active interval. No need to intersect.
784 continue;
785 }
786 size_t next_intersection = inactive->FirstIntersectionWith(current);
787 if (next_intersection != kNoLifetime) {
788 free_until[inactive->GetRegister()] =
789 std::min(free_until[inactive->GetRegister()], next_intersection);
790 }
791 }
792
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000793 int reg = kNoRegister;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100794 if (current->HasRegister()) {
795 // Some instructions have a fixed register output.
796 reg = current->GetRegister();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000797 if (free_until[reg] == 0) {
798 DCHECK(current->IsHighInterval());
799 // AllocateBlockedReg will spill the holder of the register.
800 return false;
801 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100802 } else {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000803 DCHECK(!current->IsHighInterval());
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100804 int hint = current->FindFirstRegisterHint(free_until, liveness_);
Nicolas Geoffrayf2975812015-08-07 18:13:03 -0700805 if ((hint != kNoRegister)
806 // For simplicity, if the hint we are getting for a pair cannot be used,
807 // we are just going to allocate a new pair.
808 && !(current->IsLowInterval() && IsBlocked(GetHighForLowRegister(hint)))) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100809 DCHECK(!IsBlocked(hint));
810 reg = hint;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000811 } else if (current->IsLowInterval()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000812 reg = FindAvailableRegisterPair(free_until, current->GetStart());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100813 } else {
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100814 reg = FindAvailableRegister(free_until, current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100815 }
816 }
817
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000818 DCHECK_NE(reg, kNoRegister);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100819 // If we could not find a register, we need to spill.
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000820 if (free_until[reg] == 0) {
821 return false;
822 }
823
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000824 if (current->IsLowInterval()) {
825 // If the high register of this interval is not available, we need to spill.
826 int high_reg = current->GetHighInterval()->GetRegister();
827 if (high_reg == kNoRegister) {
828 high_reg = GetHighForLowRegister(reg);
829 }
830 if (free_until[high_reg] == 0) {
831 return false;
832 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100833 }
834
835 current->SetRegister(reg);
836 if (!current->IsDeadAt(free_until[reg])) {
837 // If the register is only available for a subset of live ranges
Nicolas Geoffray82726882015-06-01 13:51:57 +0100838 // covered by `current`, split `current` before the position where
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100839 // the register is not available anymore.
Nicolas Geoffray82726882015-06-01 13:51:57 +0100840 LiveInterval* split = SplitBetween(current, current->GetStart(), free_until[reg]);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100841 DCHECK(split != nullptr);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100842 AddSorted(unhandled_, split);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100843 }
844 return true;
845}
846
847bool RegisterAllocator::IsBlocked(int reg) const {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100848 return processing_core_registers_
849 ? blocked_core_registers_[reg]
850 : blocked_fp_registers_[reg];
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100851}
852
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000853int RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const {
854 int reg = kNoRegister;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000855 // Pick the register pair that is used the last.
856 for (size_t i = 0; i < number_of_registers_; ++i) {
857 if (IsBlocked(i)) continue;
858 if (!IsLowRegister(i)) continue;
859 int high_register = GetHighForLowRegister(i);
860 if (IsBlocked(high_register)) continue;
861 int existing_high_register = GetHighForLowRegister(reg);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000862 if ((reg == kNoRegister) || (next_use[i] >= next_use[reg]
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000863 && next_use[high_register] >= next_use[existing_high_register])) {
864 reg = i;
865 if (next_use[i] == kMaxLifetimePosition
866 && next_use[high_register] == kMaxLifetimePosition) {
867 break;
868 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000869 } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) {
870 // If one of the current register is known to be unavailable, just unconditionally
871 // try a new one.
872 reg = i;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000873 }
874 }
875 return reg;
876}
877
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100878bool RegisterAllocator::IsCallerSaveRegister(int reg) const {
879 return processing_core_registers_
880 ? !codegen_->IsCoreCalleeSaveRegister(reg)
881 : !codegen_->IsFloatingPointCalleeSaveRegister(reg);
882}
883
884int RegisterAllocator::FindAvailableRegister(size_t* next_use, LiveInterval* current) const {
885 // We special case intervals that do not span a safepoint to try to find a caller-save
886 // register if one is available. We iterate from 0 to the number of registers,
887 // so if there are caller-save registers available at the end, we continue the iteration.
888 bool prefers_caller_save = !current->HasWillCallSafepoint();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000889 int reg = kNoRegister;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000890 for (size_t i = 0; i < number_of_registers_; ++i) {
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100891 if (IsBlocked(i)) {
892 // Register cannot be used. Continue.
893 continue;
894 }
895
896 // Best case: we found a register fully available.
897 if (next_use[i] == kMaxLifetimePosition) {
898 if (prefers_caller_save && !IsCallerSaveRegister(i)) {
899 // We can get shorter encodings on some platforms by using
900 // small register numbers. So only update the candidate if the previous
901 // one was not available for the whole method.
902 if (reg == kNoRegister || next_use[reg] != kMaxLifetimePosition) {
903 reg = i;
904 }
905 // Continue the iteration in the hope of finding a caller save register.
906 continue;
907 } else {
908 reg = i;
909 // We know the register is good enough. Return it.
910 break;
911 }
912 }
913
914 // If we had no register before, take this one as a reference.
915 if (reg == kNoRegister) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000916 reg = i;
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100917 continue;
918 }
919
920 // Pick the register that is used the last.
921 if (next_use[i] > next_use[reg]) {
922 reg = i;
923 continue;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000924 }
925 }
926 return reg;
927}
928
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100929// Remove interval and its other half if any. Return iterator to the following element.
930static ArenaVector<LiveInterval*>::iterator RemoveIntervalAndPotentialOtherHalf(
931 ArenaVector<LiveInterval*>* intervals, ArenaVector<LiveInterval*>::iterator pos) {
932 DCHECK(intervals->begin() <= pos && pos < intervals->end());
933 LiveInterval* interval = *pos;
934 if (interval->IsLowInterval()) {
935 DCHECK(pos + 1 < intervals->end());
936 DCHECK_EQ(*(pos + 1), interval->GetHighInterval());
937 return intervals->erase(pos, pos + 2);
938 } else if (interval->IsHighInterval()) {
939 DCHECK(intervals->begin() < pos);
940 DCHECK_EQ(*(pos - 1), interval->GetLowInterval());
941 return intervals->erase(pos - 1, pos + 1);
942 } else {
943 return intervals->erase(pos);
944 }
945}
946
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000947bool RegisterAllocator::TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
948 size_t first_register_use,
949 size_t* next_use) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100950 for (auto it = active_.begin(), end = active_.end(); it != end; ++it) {
951 LiveInterval* active = *it;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000952 DCHECK(active->HasRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000953 if (active->IsFixed()) continue;
954 if (active->IsHighInterval()) continue;
955 if (first_register_use > next_use[active->GetRegister()]) continue;
956
Nicolas Geoffray2e92bc22015-08-20 19:52:26 +0100957 // Split the first interval found that is either:
958 // 1) A non-pair interval.
959 // 2) A pair interval whose high is not low + 1.
960 // 3) A pair interval whose low is not even.
961 if (!active->IsLowInterval() ||
962 IsLowOfUnalignedPairInterval(active) ||
963 !IsLowRegister(active->GetRegister())) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000964 LiveInterval* split = Split(active, position);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000965 if (split != active) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100966 handled_.push_back(active);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000967 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100968 RemoveIntervalAndPotentialOtherHalf(&active_, it);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000969 AddSorted(unhandled_, split);
970 return true;
971 }
972 }
973 return false;
974}
975
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100976// Find the register that is used the last, and spill the interval
977// that holds it. If the first use of `current` is after that register
978// we spill `current` instead.
979bool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
980 size_t first_register_use = current->FirstRegisterUse();
Nicolas Geoffrayda2b2542015-08-06 19:56:45 -0700981 if (current->HasRegister()) {
982 DCHECK(current->IsHighInterval());
983 // The low interval has allocated the register for the high interval. In
984 // case the low interval had to split both intervals, we may end up in a
985 // situation where the high interval does not have a register use anymore.
986 // We must still proceed in order to split currently active and inactive
987 // uses of the high interval's register, and put the high interval in the
988 // active set.
989 DCHECK(first_register_use != kNoLifetime || (current->GetNextSibling() != nullptr));
990 } else if (first_register_use == kNoLifetime) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100991 AllocateSpillSlotFor(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100992 return false;
993 }
994
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100995 // We use the first use to compare with other intervals. If this interval
996 // is used after any active intervals, we will spill this interval.
997 size_t first_use = current->FirstUseAfter(current->GetStart());
998
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100999 // First set all registers as not being used.
1000 size_t* next_use = registers_array_;
1001 for (size_t i = 0; i < number_of_registers_; ++i) {
1002 next_use[i] = kMaxLifetimePosition;
1003 }
1004
1005 // For each active interval, find the next use of its register after the
1006 // start of current.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001007 for (LiveInterval* active : active_) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001008 DCHECK(active->HasRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001009 if (active->IsFixed()) {
1010 next_use[active->GetRegister()] = current->GetStart();
1011 } else {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01001012 size_t use = active->FirstUseAfter(current->GetStart());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001013 if (use != kNoLifetime) {
1014 next_use[active->GetRegister()] = use;
1015 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001016 }
1017 }
1018
1019 // For each inactive interval, find the next use of its register after the
1020 // start of current.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001021 for (LiveInterval* inactive : inactive_) {
Mingyao Yang296bd602014-10-06 16:47:28 -07001022 // Temp/Slow-path-safepoint interval has no holes.
1023 DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
1024 if (!current->IsSplit() && !inactive->IsFixed()) {
1025 // Neither current nor inactive are fixed.
1026 // Thanks to SSA, a non-split interval starting in a hole of an
1027 // inactive interval should never intersect with that inactive interval.
1028 // Only if it's not fixed though, because fixed intervals don't come from SSA.
1029 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
1030 continue;
1031 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001032 DCHECK(inactive->HasRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001033 size_t next_intersection = inactive->FirstIntersectionWith(current);
1034 if (next_intersection != kNoLifetime) {
1035 if (inactive->IsFixed()) {
1036 next_use[inactive->GetRegister()] =
1037 std::min(next_intersection, next_use[inactive->GetRegister()]);
1038 } else {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01001039 size_t use = inactive->FirstUseAfter(current->GetStart());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001040 if (use != kNoLifetime) {
1041 next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
1042 }
1043 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001044 }
1045 }
1046
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001047 int reg = kNoRegister;
1048 bool should_spill = false;
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001049 if (current->HasRegister()) {
1050 DCHECK(current->IsHighInterval());
1051 reg = current->GetRegister();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001052 // When allocating the low part, we made sure the high register was available.
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01001053 DCHECK_LT(first_use, next_use[reg]);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001054 } else if (current->IsLowInterval()) {
Nicolas Geoffrayda2b2542015-08-06 19:56:45 -07001055 reg = FindAvailableRegisterPair(next_use, first_use);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001056 // We should spill if both registers are not available.
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01001057 should_spill = (first_use >= next_use[reg])
1058 || (first_use >= next_use[GetHighForLowRegister(reg)]);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001059 } else {
1060 DCHECK(!current->IsHighInterval());
Nicolas Geoffray8826f672015-04-17 09:15:11 +01001061 reg = FindAvailableRegister(next_use, current);
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01001062 should_spill = (first_use >= next_use[reg]);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001063 }
1064
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001065 DCHECK_NE(reg, kNoRegister);
1066 if (should_spill) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001067 DCHECK(!current->IsHighInterval());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001068 bool is_allocation_at_use_site = (current->GetStart() >= (first_register_use - 1));
Nicolas Geoffrayda2b2542015-08-06 19:56:45 -07001069 if (is_allocation_at_use_site) {
1070 if (!current->IsLowInterval()) {
1071 DumpInterval(std::cerr, current);
1072 DumpAllIntervals(std::cerr);
1073 // This situation has the potential to infinite loop, so we make it a non-debug CHECK.
1074 HInstruction* at = liveness_.GetInstructionFromPosition(first_register_use / 2);
1075 CHECK(false) << "There is not enough registers available for "
1076 << current->GetParent()->GetDefinedBy()->DebugName() << " "
1077 << current->GetParent()->GetDefinedBy()->GetId()
1078 << " at " << first_register_use - 1 << " "
1079 << (at == nullptr ? "" : at->DebugName());
1080 }
1081
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001082 // If we're allocating a register for `current` because the instruction at
1083 // that position requires it, but we think we should spill, then there are
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001084 // non-pair intervals or unaligned pair intervals blocking the allocation.
1085 // We split the first interval found, and put ourselves first in the
1086 // `unhandled_` list.
Nicolas Geoffrayda2b2542015-08-06 19:56:45 -07001087 bool success = TrySplitNonPairOrUnalignedPairIntervalAt(current->GetStart(),
1088 first_register_use,
1089 next_use);
1090 DCHECK(success);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001091 LiveInterval* existing = unhandled_->back();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001092 DCHECK(existing->IsHighInterval());
1093 DCHECK_EQ(existing->GetLowInterval(), current);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001094 unhandled_->push_back(current);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001095 } else {
1096 // If the first use of that instruction is after the last use of the found
1097 // register, we split this interval just before its first register use.
1098 AllocateSpillSlotFor(current);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001099 LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1);
Nicolas Geoffrayda2b2542015-08-06 19:56:45 -07001100 DCHECK(current != split);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001101 AddSorted(unhandled_, split);
1102 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001103 return false;
1104 } else {
1105 // Use this register and spill the active and inactives interval that
1106 // have that register.
1107 current->SetRegister(reg);
1108
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001109 for (auto it = active_.begin(), end = active_.end(); it != end; ++it) {
1110 LiveInterval* active = *it;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001111 if (active->GetRegister() == reg) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001112 DCHECK(!active->IsFixed());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001113 LiveInterval* split = Split(active, current->GetStart());
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001114 if (split != active) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001115 handled_.push_back(active);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001116 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001117 RemoveIntervalAndPotentialOtherHalf(&active_, it);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001118 AddSorted(unhandled_, split);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001119 break;
1120 }
1121 }
1122
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001123 // NOTE: Retrieve end() on each iteration because we're removing elements in the loop body.
1124 for (auto it = inactive_.begin(); it != inactive_.end(); ) {
1125 LiveInterval* inactive = *it;
1126 bool erased = false;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001127 if (inactive->GetRegister() == reg) {
Mingyao Yang296bd602014-10-06 16:47:28 -07001128 if (!current->IsSplit() && !inactive->IsFixed()) {
1129 // Neither current nor inactive are fixed.
1130 // Thanks to SSA, a non-split interval starting in a hole of an
1131 // inactive interval should never intersect with that inactive interval.
1132 // Only if it's not fixed though, because fixed intervals don't come from SSA.
1133 DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001134 } else {
1135 size_t next_intersection = inactive->FirstIntersectionWith(current);
1136 if (next_intersection != kNoLifetime) {
1137 if (inactive->IsFixed()) {
1138 LiveInterval* split = Split(current, next_intersection);
1139 DCHECK_NE(split, current);
1140 AddSorted(unhandled_, split);
1141 } else {
1142 // Split at the start of `current`, which will lead to splitting
1143 // at the end of the lifetime hole of `inactive`.
1144 LiveInterval* split = Split(inactive, current->GetStart());
1145 // If it's inactive, it must start before the current interval.
1146 DCHECK_NE(split, inactive);
1147 it = RemoveIntervalAndPotentialOtherHalf(&inactive_, it);
1148 erased = true;
1149 handled_.push_back(inactive);
1150 AddSorted(unhandled_, split);
Nicolas Geoffray5b168de2015-03-27 10:27:22 +00001151 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001152 }
1153 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001154 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001155 // If we have erased the element, `it` already points to the next element.
1156 // Otherwise we need to move to the next element.
1157 if (!erased) {
1158 ++it;
1159 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001160 }
1161
1162 return true;
1163 }
1164}
1165
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001166void RegisterAllocator::AddSorted(ArenaVector<LiveInterval*>* array, LiveInterval* interval) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01001167 DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001168 size_t insert_at = 0;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001169 for (size_t i = array->size(); i > 0; --i) {
1170 LiveInterval* current = (*array)[i - 1u];
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001171 // High intervals must be processed right after their low equivalent.
1172 if (current->StartsAfter(interval) && !current->IsHighInterval()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001173 insert_at = i;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001174 break;
Nicolas Geoffrayacd03392014-11-26 15:46:52 +00001175 } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
1176 // Ensure the slow path interval is the last to be processed at its location: we want the
1177 // interval to know all live registers at this location.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001178 DCHECK(i == 1 || (*array)[i - 2u]->StartsAfter(current));
Nicolas Geoffrayacd03392014-11-26 15:46:52 +00001179 insert_at = i;
1180 break;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001181 }
1182 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001183
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001184 // Insert the high interval before the low, to ensure the low is processed before.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001185 auto insert_pos = array->begin() + insert_at;
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001186 if (interval->HasHighInterval()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001187 array->insert(insert_pos, { interval->GetHighInterval(), interval });
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001188 } else if (interval->HasLowInterval()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001189 array->insert(insert_pos, { interval, interval->GetLowInterval() });
1190 } else {
1191 array->insert(insert_pos, interval);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001192 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001193}
1194
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001195LiveInterval* RegisterAllocator::SplitBetween(LiveInterval* interval, size_t from, size_t to) {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001196 HBasicBlock* block_from = liveness_.GetBlockFromPosition(from / 2);
1197 HBasicBlock* block_to = liveness_.GetBlockFromPosition(to / 2);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001198 DCHECK(block_from != nullptr);
1199 DCHECK(block_to != nullptr);
1200
1201 // Both locations are in the same block. We split at the given location.
1202 if (block_from == block_to) {
1203 return Split(interval, to);
1204 }
1205
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001206 /*
1207 * Non-linear control flow will force moves at every branch instruction to the new location.
1208 * To avoid having all branches doing the moves, we find the next non-linear position and
1209 * split the interval at this position. Take the following example (block number is the linear
1210 * order position):
1211 *
1212 * B1
1213 * / \
1214 * B2 B3
1215 * \ /
1216 * B4
1217 *
1218 * B2 needs to split an interval, whose next use is in B4. If we were to split at the
1219 * beginning of B4, B3 would need to do a move between B3 and B4 to ensure the interval
1220 * is now in the correct location. It makes performance worst if the interval is spilled
1221 * and both B2 and B3 need to reload it before entering B4.
1222 *
1223 * By splitting at B3, we give a chance to the register allocator to allocate the
1224 * interval to the same register as in B1, and therefore avoid doing any
1225 * moves in B3.
1226 */
1227 if (block_from->GetDominator() != nullptr) {
Vladimir Marko60584552015-09-03 13:35:12 +00001228 for (HBasicBlock* dominated : block_from->GetDominator()->GetDominatedBlocks()) {
1229 size_t position = dominated->GetLifetimeStart();
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001230 if ((position > from) && (block_to->GetLifetimeStart() > position)) {
1231 // Even if we found a better block, we continue iterating in case
1232 // a dominated block is closer.
1233 // Note that dominated blocks are not sorted in liveness order.
Vladimir Marko60584552015-09-03 13:35:12 +00001234 block_to = dominated;
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001235 DCHECK_NE(block_to, block_from);
1236 }
1237 }
1238 }
1239
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001240 // If `to` is in a loop, find the outermost loop header which does not contain `from`.
1241 for (HLoopInformationOutwardIterator it(*block_to); !it.Done(); it.Advance()) {
1242 HBasicBlock* header = it.Current()->GetHeader();
1243 if (block_from->GetLifetimeStart() >= header->GetLifetimeStart()) {
1244 break;
1245 }
1246 block_to = header;
1247 }
1248
1249 // Split at the start of the found block, to piggy back on existing moves
1250 // due to resolution if non-linear control flow (see `ConnectSplitSiblings`).
1251 return Split(interval, block_to->GetLifetimeStart());
1252}
1253
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001254LiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001255 DCHECK_GE(position, interval->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001256 DCHECK(!interval->IsDeadAt(position));
1257 if (position == interval->GetStart()) {
1258 // Spill slot will be allocated when handling `interval` again.
1259 interval->ClearRegister();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001260 if (interval->HasHighInterval()) {
1261 interval->GetHighInterval()->ClearRegister();
1262 } else if (interval->HasLowInterval()) {
1263 interval->GetLowInterval()->ClearRegister();
1264 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001265 return interval;
1266 } else {
1267 LiveInterval* new_interval = interval->SplitAt(position);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001268 if (interval->HasHighInterval()) {
1269 LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
1270 new_interval->SetHighInterval(high);
1271 high->SetLowInterval(new_interval);
1272 } else if (interval->HasLowInterval()) {
1273 LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
1274 new_interval->SetLowInterval(low);
1275 low->SetHighInterval(new_interval);
1276 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001277 return new_interval;
1278 }
1279}
1280
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001281void RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001282 if (interval->IsHighInterval()) {
Nicolas Geoffrayda2b2542015-08-06 19:56:45 -07001283 // The low interval already took care of allocating the spill slot.
1284 DCHECK(!interval->GetLowInterval()->HasRegister());
1285 DCHECK(interval->GetLowInterval()->GetParent()->HasSpillSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001286 return;
1287 }
1288
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001289 LiveInterval* parent = interval->GetParent();
1290
1291 // An instruction gets a spill slot for its entire lifetime. If the parent
1292 // of this interval already has a spill slot, there is nothing to do.
1293 if (parent->HasSpillSlot()) {
1294 return;
1295 }
1296
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001297 HInstruction* defined_by = parent->GetDefinedBy();
David Brazdil77a48ae2015-09-15 12:34:04 +00001298 DCHECK(!defined_by->IsPhi() || !defined_by->AsPhi()->IsCatchPhi());
1299
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001300 if (defined_by->IsParameterValue()) {
1301 // Parameters have their own stack slot.
1302 parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
1303 return;
1304 }
1305
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001306 if (defined_by->IsCurrentMethod()) {
1307 parent->SetSpillSlot(0);
1308 return;
1309 }
1310
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001311 if (defined_by->IsConstant()) {
1312 // Constants don't need a spill slot.
1313 return;
1314 }
1315
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001316 ArenaVector<size_t>* spill_slots = nullptr;
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001317 switch (interval->GetType()) {
1318 case Primitive::kPrimDouble:
1319 spill_slots = &double_spill_slots_;
1320 break;
1321 case Primitive::kPrimLong:
1322 spill_slots = &long_spill_slots_;
1323 break;
1324 case Primitive::kPrimFloat:
1325 spill_slots = &float_spill_slots_;
1326 break;
1327 case Primitive::kPrimNot:
1328 case Primitive::kPrimInt:
1329 case Primitive::kPrimChar:
1330 case Primitive::kPrimByte:
1331 case Primitive::kPrimBoolean:
1332 case Primitive::kPrimShort:
1333 spill_slots = &int_spill_slots_;
1334 break;
1335 case Primitive::kPrimVoid:
1336 LOG(FATAL) << "Unexpected type for interval " << interval->GetType();
1337 }
1338
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001339 // Find an available spill slot.
1340 size_t slot = 0;
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001341 for (size_t e = spill_slots->size(); slot < e; ++slot) {
1342 if ((*spill_slots)[slot] <= parent->GetStart()
1343 && (slot == (e - 1) || (*spill_slots)[slot + 1] <= parent->GetStart())) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001344 break;
1345 }
1346 }
1347
David Brazdil77a48ae2015-09-15 12:34:04 +00001348 size_t end = interval->GetLastSibling()->GetEnd();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001349 if (parent->NeedsTwoSpillSlots()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001350 if (slot + 2u > spill_slots->size()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001351 // We need a new spill slot.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001352 spill_slots->resize(slot + 2u, end);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001353 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001354 (*spill_slots)[slot] = end;
1355 (*spill_slots)[slot + 1] = end;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001356 } else {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001357 if (slot == spill_slots->size()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001358 // We need a new spill slot.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001359 spill_slots->push_back(end);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001360 } else {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001361 (*spill_slots)[slot] = end;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001362 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001363 }
1364
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001365 // Note that the exact spill slot location will be computed when we resolve,
1366 // that is when we know the number of spill slots for each type.
1367 parent->SetSpillSlot(slot);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001368}
1369
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001370static bool IsValidDestination(Location destination) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001371 return destination.IsRegister()
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001372 || destination.IsRegisterPair()
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001373 || destination.IsFpuRegister()
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001374 || destination.IsFpuRegisterPair()
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001375 || destination.IsStackSlot()
1376 || destination.IsDoubleStackSlot();
Nicolas Geoffray2a877f32014-09-10 10:49:34 +01001377}
1378
David Brazdil77a48ae2015-09-15 12:34:04 +00001379void RegisterAllocator::AllocateSpillSlotForCatchPhi(HPhi* phi) {
1380 LiveInterval* interval = phi->GetLiveInterval();
1381
1382 HInstruction* previous_phi = phi->GetPrevious();
1383 DCHECK(previous_phi == nullptr ||
1384 previous_phi->AsPhi()->GetRegNumber() <= phi->GetRegNumber())
1385 << "Phis expected to be sorted by vreg number, so that equivalent phis are adjacent.";
1386
1387 if (phi->IsVRegEquivalentOf(previous_phi)) {
1388 // This is an equivalent of the previous phi. We need to assign the same
1389 // catch phi slot.
1390 DCHECK(previous_phi->GetLiveInterval()->HasSpillSlot());
1391 interval->SetSpillSlot(previous_phi->GetLiveInterval()->GetSpillSlot());
1392 } else {
1393 // Allocate a new spill slot for this catch phi.
1394 // TODO: Reuse spill slots when intervals of phis from different catch
1395 // blocks do not overlap.
1396 interval->SetSpillSlot(catch_phi_spill_slots_);
1397 catch_phi_spill_slots_ += interval->NeedsTwoSpillSlots() ? 2 : 1;
1398 }
1399}
1400
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001401void RegisterAllocator::AddMove(HParallelMove* move,
1402 Location source,
1403 Location destination,
1404 HInstruction* instruction,
1405 Primitive::Type type) const {
1406 if (type == Primitive::kPrimLong
1407 && codegen_->ShouldSplitLongMoves()
1408 // The parallel move resolver knows how to deal with long constants.
1409 && !source.IsConstant()) {
Nicolas Geoffray90218252015-04-15 11:56:51 +01001410 move->AddMove(source.ToLow(), destination.ToLow(), Primitive::kPrimInt, instruction);
1411 move->AddMove(source.ToHigh(), destination.ToHigh(), Primitive::kPrimInt, nullptr);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001412 } else {
Nicolas Geoffray90218252015-04-15 11:56:51 +01001413 move->AddMove(source, destination, type, instruction);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001414 }
1415}
1416
1417void RegisterAllocator::AddInputMoveFor(HInstruction* input,
1418 HInstruction* user,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001419 Location source,
1420 Location destination) const {
1421 if (source.Equals(destination)) return;
1422
Roland Levillain476df552014-10-09 17:51:36 +01001423 DCHECK(!user->IsPhi());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001424
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001425 HInstruction* previous = user->GetPrevious();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001426 HParallelMove* move = nullptr;
1427 if (previous == nullptr
Roland Levillain476df552014-10-09 17:51:36 +01001428 || !previous->IsParallelMove()
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001429 || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001430 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001431 move->SetLifetimePosition(user->GetLifetimePosition());
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001432 user->GetBlock()->InsertInstructionBefore(move, user);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001433 } else {
1434 move = previous->AsParallelMove();
1435 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001436 DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001437 AddMove(move, source, destination, nullptr, input->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001438}
1439
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001440static bool IsInstructionStart(size_t position) {
1441 return (position & 1) == 0;
1442}
1443
1444static bool IsInstructionEnd(size_t position) {
1445 return (position & 1) == 1;
1446}
1447
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001448void RegisterAllocator::InsertParallelMoveAt(size_t position,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001449 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001450 Location source,
1451 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001452 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001453 if (source.Equals(destination)) return;
1454
1455 HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001456 HParallelMove* move;
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001457 if (at == nullptr) {
1458 if (IsInstructionStart(position)) {
1459 // Block boundary, don't do anything the connection of split siblings will handle it.
1460 return;
1461 } else {
1462 // Move must happen before the first instruction of the block.
1463 at = liveness_.GetInstructionFromPosition((position + 1) / 2);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001464 // Note that parallel moves may have already been inserted, so we explicitly
1465 // ask for the first instruction of the block: `GetInstructionFromPosition` does
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001466 // not contain the `HParallelMove` instructions.
Nicolas Geoffray59768572014-12-01 09:50:04 +00001467 at = at->GetBlock()->GetFirstInstruction();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001468
1469 if (at->GetLifetimePosition() < position) {
1470 // We may insert moves for split siblings and phi spills at the beginning of the block.
1471 // Since this is a different lifetime position, we need to go to the next instruction.
1472 DCHECK(at->IsParallelMove());
1473 at = at->GetNext();
1474 }
1475
Nicolas Geoffray59768572014-12-01 09:50:04 +00001476 if (at->GetLifetimePosition() != position) {
1477 DCHECK_GT(at->GetLifetimePosition(), position);
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001478 move = new (allocator_) HParallelMove(allocator_);
1479 move->SetLifetimePosition(position);
1480 at->GetBlock()->InsertInstructionBefore(move, at);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001481 } else {
1482 DCHECK(at->IsParallelMove());
1483 move = at->AsParallelMove();
Nicolas Geoffray46fbaab2014-11-26 18:30:23 +00001484 }
1485 }
1486 } else if (IsInstructionEnd(position)) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001487 // Move must happen after the instruction.
1488 DCHECK(!at->IsControlFlow());
1489 move = at->GetNext()->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001490 // This is a parallel move for connecting siblings in a same block. We need to
1491 // differentiate it with moves for connecting blocks, and input moves.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001492 if (move == nullptr || move->GetLifetimePosition() > position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001493 move = new (allocator_) HParallelMove(allocator_);
1494 move->SetLifetimePosition(position);
1495 at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
1496 }
1497 } else {
1498 // Move must happen before the instruction.
1499 HInstruction* previous = at->GetPrevious();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001500 if (previous == nullptr
1501 || !previous->IsParallelMove()
1502 || previous->GetLifetimePosition() != position) {
1503 // If the previous is a parallel move, then its position must be lower
1504 // than the given `position`: it was added just after the non-parallel
1505 // move instruction that precedes `instruction`.
1506 DCHECK(previous == nullptr
1507 || !previous->IsParallelMove()
1508 || previous->GetLifetimePosition() < position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001509 move = new (allocator_) HParallelMove(allocator_);
1510 move->SetLifetimePosition(position);
1511 at->GetBlock()->InsertInstructionBefore(move, at);
1512 } else {
1513 move = previous->AsParallelMove();
1514 }
1515 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001516 DCHECK_EQ(move->GetLifetimePosition(), position);
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::InsertParallelMoveAtExitOf(HBasicBlock* block,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001521 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001522 Location source,
1523 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001524 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001525 if (source.Equals(destination)) return;
1526
David Brazdil77a48ae2015-09-15 12:34:04 +00001527 DCHECK_EQ(block->NumberOfNormalSuccessors(), 1u);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001528 HInstruction* last = block->GetLastInstruction();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001529 // We insert moves at exit for phi predecessors and connecting blocks.
1530 // A block ending with an if cannot branch to a block with phis because
1531 // we do not allow critical edges. It can also not connect
1532 // a split interval between two blocks: the move has to happen in the successor.
1533 DCHECK(!last->IsIf());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001534 HInstruction* previous = last->GetPrevious();
1535 HParallelMove* move;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001536 // This is a parallel move for connecting blocks. We need to differentiate
1537 // it with moves for connecting siblings in a same block, and output moves.
Nicolas Geoffray59768572014-12-01 09:50:04 +00001538 size_t position = last->GetLifetimePosition();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001539 if (previous == nullptr || !previous->IsParallelMove()
Nicolas Geoffray59768572014-12-01 09:50:04 +00001540 || previous->AsParallelMove()->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001541 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray59768572014-12-01 09:50:04 +00001542 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001543 block->InsertInstructionBefore(move, last);
1544 } else {
1545 move = previous->AsParallelMove();
1546 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001547 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001548}
1549
1550void RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001551 HInstruction* instruction,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001552 Location source,
1553 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001554 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001555 if (source.Equals(destination)) return;
1556
1557 HInstruction* first = block->GetFirstInstruction();
1558 HParallelMove* move = first->AsParallelMove();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001559 size_t position = block->GetLifetimeStart();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001560 // This is a parallel move for connecting blocks. We need to differentiate
1561 // it with moves for connecting siblings in a same block, and input moves.
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001562 if (move == nullptr || move->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001563 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001564 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001565 block->InsertInstructionBefore(move, first);
1566 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001567 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001568}
1569
1570void RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
1571 Location source,
1572 Location destination) const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001573 DCHECK(IsValidDestination(destination)) << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001574 if (source.Equals(destination)) return;
1575
Roland Levillain476df552014-10-09 17:51:36 +01001576 if (instruction->IsPhi()) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001577 InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001578 return;
1579 }
1580
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001581 size_t position = instruction->GetLifetimePosition() + 1;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001582 HParallelMove* move = instruction->GetNext()->AsParallelMove();
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001583 // This is a parallel move for moving the output of an instruction. We need
1584 // to differentiate with input moves, moves for connecting siblings in a
1585 // and moves for connecting blocks.
1586 if (move == nullptr || move->GetLifetimePosition() != position) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001587 move = new (allocator_) HParallelMove(allocator_);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001588 move->SetLifetimePosition(position);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001589 instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
1590 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001591 AddMove(move, source, destination, instruction, instruction->GetType());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001592}
1593
1594void RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
1595 LiveInterval* current = interval;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001596 if (current->HasSpillSlot()
1597 && current->HasRegister()
1598 // Currently, we spill unconditionnally the current method in the code generators.
1599 && !interval->GetDefinedBy()->IsCurrentMethod()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001600 // We spill eagerly, so move must be at definition.
1601 InsertMoveAfter(interval->GetDefinedBy(),
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001602 interval->ToLocation(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001603 interval->NeedsTwoSpillSlots()
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001604 ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1605 : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001606 }
1607 UsePosition* use = current->GetFirstUse();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001608 UsePosition* env_use = current->GetFirstEnvironmentUse();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001609
1610 // Walk over all siblings, updating locations of use positions, and
1611 // connecting them when they are adjacent.
1612 do {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001613 Location source = current->ToLocation();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001614
1615 // Walk over all uses covered by this interval, and update the location
1616 // information.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001617
1618 LiveRange* range = current->GetFirstRange();
1619 while (range != nullptr) {
Nicolas Geoffray57902602015-04-21 14:28:41 +01001620 while (use != nullptr && use->GetPosition() < range->GetStart()) {
1621 DCHECK(use->IsSynthesized());
1622 use = use->GetNext();
1623 }
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001624 while (use != nullptr && use->GetPosition() <= range->GetEnd()) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001625 DCHECK(!use->GetIsEnvironment());
David Brazdil3fc992f2015-04-16 18:31:55 +01001626 DCHECK(current->CoversSlow(use->GetPosition()) || (use->GetPosition() == range->GetEnd()));
Nicolas Geoffray57902602015-04-21 14:28:41 +01001627 if (!use->IsSynthesized()) {
1628 LocationSummary* locations = use->GetUser()->GetLocations();
1629 Location expected_location = locations->InAt(use->GetInputIndex());
1630 // The expected (actual) location may be invalid in case the input is unused. Currently
1631 // this only happens for intrinsics.
1632 if (expected_location.IsValid()) {
1633 if (expected_location.IsUnallocated()) {
1634 locations->SetInAt(use->GetInputIndex(), source);
1635 } else if (!expected_location.IsConstant()) {
1636 AddInputMoveFor(interval->GetDefinedBy(), use->GetUser(), source, expected_location);
1637 }
1638 } else {
1639 DCHECK(use->GetUser()->IsInvoke());
1640 DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001641 }
1642 }
1643 use = use->GetNext();
1644 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001645
1646 // Walk over the environment uses, and update their locations.
1647 while (env_use != nullptr && env_use->GetPosition() < range->GetStart()) {
1648 env_use = env_use->GetNext();
1649 }
1650
1651 while (env_use != nullptr && env_use->GetPosition() <= range->GetEnd()) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001652 DCHECK(current->CoversSlow(env_use->GetPosition())
1653 || (env_use->GetPosition() == range->GetEnd()));
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001654 HEnvironment* environment = env_use->GetEnvironment();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001655 environment->SetLocationAt(env_use->GetInputIndex(), source);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001656 env_use = env_use->GetNext();
1657 }
1658
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001659 range = range->GetNext();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001660 }
1661
1662 // If the next interval starts just after this one, and has a register,
1663 // insert a move.
1664 LiveInterval* next_sibling = current->GetNextSibling();
1665 if (next_sibling != nullptr
1666 && next_sibling->HasRegister()
1667 && current->GetEnd() == next_sibling->GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001668 Location destination = next_sibling->ToLocation();
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001669 InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001670 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001671
Nicolas Geoffray43af7282015-04-16 13:01:01 +01001672 for (SafepointPosition* safepoint_position = current->GetFirstSafepoint();
1673 safepoint_position != nullptr;
1674 safepoint_position = safepoint_position->GetNext()) {
David Brazdil3fc992f2015-04-16 18:31:55 +01001675 DCHECK(current->CoversSlow(safepoint_position->GetPosition()));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001676
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001677 LocationSummary* locations = safepoint_position->GetLocations();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001678 if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001679 locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
1680 }
1681
1682 switch (source.GetKind()) {
1683 case Location::kRegister: {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001684 locations->AddLiveRegister(source);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001685 if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) {
1686 DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1687 maximum_number_of_live_core_registers_ +
1688 maximum_number_of_live_fp_registers_);
1689 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001690 if (current->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001691 locations->SetRegisterBit(source.reg());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001692 }
1693 break;
1694 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001695 case Location::kFpuRegister: {
1696 locations->AddLiveRegister(source);
1697 break;
1698 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001699
1700 case Location::kRegisterPair:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001701 case Location::kFpuRegisterPair: {
1702 locations->AddLiveRegister(source.ToLow());
1703 locations->AddLiveRegister(source.ToHigh());
1704 break;
1705 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001706 case Location::kStackSlot: // Fall-through
1707 case Location::kDoubleStackSlot: // Fall-through
1708 case Location::kConstant: {
1709 // Nothing to do.
1710 break;
1711 }
1712 default: {
1713 LOG(FATAL) << "Unexpected location for object";
1714 }
1715 }
1716 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001717 current = next_sibling;
1718 } while (current != nullptr);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +00001719
Nicolas Geoffray57902602015-04-21 14:28:41 +01001720 if (kIsDebugBuild) {
1721 // Following uses can only be synthesized uses.
1722 while (use != nullptr) {
1723 DCHECK(use->IsSynthesized());
1724 use = use->GetNext();
1725 }
1726 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001727}
1728
1729void RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
1730 HBasicBlock* from,
1731 HBasicBlock* to) const {
1732 if (interval->GetNextSibling() == nullptr) {
1733 // Nothing to connect. The whole range was allocated to the same location.
1734 return;
1735 }
1736
David Brazdil241a4862015-04-16 17:59:03 +01001737 // Find the intervals that cover `from` and `to`.
1738 LiveInterval* destination = interval->GetSiblingAt(to->GetLifetimeStart());
1739 LiveInterval* source = interval->GetSiblingAt(from->GetLifetimeEnd() - 1);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001740
1741 if (destination == source) {
1742 // Interval was not split.
1743 return;
1744 }
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +01001745 DCHECK(destination != nullptr && source != nullptr);
1746
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001747 if (!destination->HasRegister()) {
1748 // Values are eagerly spilled. Spill slot already contains appropriate value.
1749 return;
1750 }
1751
1752 // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
1753 // we need to put the moves at the entry of `to`.
David Brazdil77a48ae2015-09-15 12:34:04 +00001754 if (from->NumberOfNormalSuccessors() == 1) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001755 InsertParallelMoveAtExitOf(from,
1756 interval->GetParent()->GetDefinedBy(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001757 source->ToLocation(),
1758 destination->ToLocation());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001759 } else {
Vladimir Marko60584552015-09-03 13:35:12 +00001760 DCHECK_EQ(to->GetPredecessors().size(), 1u);
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001761 InsertParallelMoveAtEntryOf(to,
1762 interval->GetParent()->GetDefinedBy(),
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001763 source->ToLocation(),
1764 destination->ToLocation());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001765 }
1766}
1767
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001768void RegisterAllocator::Resolve() {
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001769 codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(),
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +00001770 maximum_number_of_live_core_registers_,
1771 maximum_number_of_live_fp_registers_,
1772 reserved_out_slots_,
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001773 codegen_->GetGraph()->GetLinearOrder());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001774
1775 // Adjust the Out Location of instructions.
1776 // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
1777 for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
1778 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
1779 LiveInterval* current = instruction->GetLiveInterval();
1780 LocationSummary* locations = instruction->GetLocations();
1781 Location location = locations->Out();
Roland Levillain476df552014-10-09 17:51:36 +01001782 if (instruction->IsParameterValue()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001783 // Now that we know the frame size, adjust the parameter's location.
1784 if (location.IsStackSlot()) {
1785 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1786 current->SetSpillSlot(location.GetStackIndex());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001787 locations->UpdateOut(location);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001788 } else if (location.IsDoubleStackSlot()) {
1789 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1790 current->SetSpillSlot(location.GetStackIndex());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001791 locations->UpdateOut(location);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001792 } else if (current->HasSpillSlot()) {
1793 current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
1794 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001795 } else if (instruction->IsCurrentMethod()) {
1796 // The current method is always at offset 0.
1797 DCHECK(!current->HasSpillSlot() || (current->GetSpillSlot() == 0));
David Brazdil77a48ae2015-09-15 12:34:04 +00001798 } else if (instruction->IsPhi() && instruction->AsPhi()->IsCatchPhi()) {
1799 DCHECK(current->HasSpillSlot());
1800 size_t slot = current->GetSpillSlot()
1801 + GetNumberOfSpillSlots()
1802 + reserved_out_slots_
1803 - catch_phi_spill_slots_;
1804 current->SetSpillSlot(slot * kVRegSize);
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001805 } else if (current->HasSpillSlot()) {
1806 // Adjust the stack slot, now that we know the number of them for each type.
1807 // The way this implementation lays out the stack is the following:
David Brazdil77a48ae2015-09-15 12:34:04 +00001808 // [parameter slots ]
1809 // [catch phi spill slots ]
1810 // [double spill slots ]
1811 // [long spill slots ]
1812 // [float spill slots ]
1813 // [int/ref values ]
1814 // [maximum out values ] (number of arguments for calls)
1815 // [art method ].
1816 size_t slot = current->GetSpillSlot();
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001817 switch (current->GetType()) {
1818 case Primitive::kPrimDouble:
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001819 slot += long_spill_slots_.size();
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001820 FALLTHROUGH_INTENDED;
1821 case Primitive::kPrimLong:
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001822 slot += float_spill_slots_.size();
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001823 FALLTHROUGH_INTENDED;
1824 case Primitive::kPrimFloat:
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001825 slot += int_spill_slots_.size();
Nicolas Geoffray776b3182015-02-23 14:14:57 +00001826 FALLTHROUGH_INTENDED;
1827 case Primitive::kPrimNot:
1828 case Primitive::kPrimInt:
1829 case Primitive::kPrimChar:
1830 case Primitive::kPrimByte:
1831 case Primitive::kPrimBoolean:
1832 case Primitive::kPrimShort:
1833 slot += reserved_out_slots_;
1834 break;
1835 case Primitive::kPrimVoid:
1836 LOG(FATAL) << "Unexpected type for interval " << current->GetType();
1837 }
1838 current->SetSpillSlot(slot * kVRegSize);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001839 }
1840
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001841 Location source = current->ToLocation();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001842
1843 if (location.IsUnallocated()) {
1844 if (location.GetPolicy() == Location::kSameAsFirstInput) {
Calin Juravled0d48522014-11-04 16:40:20 +00001845 if (locations->InAt(0).IsUnallocated()) {
1846 locations->SetInAt(0, source);
1847 } else {
1848 DCHECK(locations->InAt(0).Equals(source));
1849 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001850 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001851 locations->UpdateOut(source);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001852 } else {
1853 DCHECK(source.Equals(location));
1854 }
1855 }
1856
1857 // Connect siblings.
1858 for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
1859 HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
1860 ConnectSiblings(instruction->GetLiveInterval());
1861 }
1862
1863 // Resolve non-linear control flow across branches. Order does not matter.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001864 for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001865 HBasicBlock* block = it.Current();
David Brazdil77a48ae2015-09-15 12:34:04 +00001866 if (block->IsCatchBlock()) {
1867 // Instructions live at the top of catch blocks were forced to spill.
1868 if (kIsDebugBuild) {
1869 BitVector* live = liveness_.GetLiveInSet(*block);
1870 for (uint32_t idx : live->Indexes()) {
1871 LiveInterval* interval = liveness_.GetInstructionFromSsaIndex(idx)->GetLiveInterval();
1872 DCHECK(!interval->GetSiblingAt(block->GetLifetimeStart())->HasRegister());
1873 }
1874 }
1875 } else {
1876 BitVector* live = liveness_.GetLiveInSet(*block);
1877 for (uint32_t idx : live->Indexes()) {
1878 LiveInterval* interval = liveness_.GetInstructionFromSsaIndex(idx)->GetLiveInterval();
1879 for (HBasicBlock* predecessor : block->GetPredecessors()) {
1880 ConnectSplitSiblings(interval, predecessor, block);
1881 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001882 }
1883 }
1884 }
1885
1886 // Resolve phi inputs. Order does not matter.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001887 for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001888 HBasicBlock* current = it.Current();
David Brazdil77a48ae2015-09-15 12:34:04 +00001889 if (current->IsCatchBlock()) {
1890 // Catch phi values are set at runtime by the exception delivery mechanism.
1891 } else {
1892 for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1893 HInstruction* phi = inst_it.Current();
1894 for (size_t i = 0, e = current->GetPredecessors().size(); i < e; ++i) {
1895 HBasicBlock* predecessor = current->GetPredecessor(i);
1896 DCHECK_EQ(predecessor->NumberOfNormalSuccessors(), 1u);
1897 HInstruction* input = phi->InputAt(i);
1898 Location source = input->GetLiveInterval()->GetLocationAt(
1899 predecessor->GetLifetimeEnd() - 1);
1900 Location destination = phi->GetLiveInterval()->ToLocation();
1901 InsertParallelMoveAtExitOf(predecessor, phi, source, destination);
1902 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001903 }
1904 }
1905 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001906
1907 // Assign temp locations.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001908 for (LiveInterval* temp : temp_intervals_) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001909 if (temp->IsHighInterval()) {
1910 // High intervals can be skipped, they are already handled by the low interval.
1911 continue;
1912 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001913 HInstruction* at = liveness_.GetTempUser(temp);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001914 size_t temp_index = liveness_.GetTempIndex(temp);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001915 LocationSummary* locations = at->GetLocations();
Roland Levillain5368c212014-11-27 15:03:41 +00001916 switch (temp->GetType()) {
1917 case Primitive::kPrimInt:
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001918 locations->SetTempAt(temp_index, Location::RegisterLocation(temp->GetRegister()));
Roland Levillain5368c212014-11-27 15:03:41 +00001919 break;
1920
1921 case Primitive::kPrimDouble:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001922 if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1923 Location location = Location::FpuRegisterPairLocation(
1924 temp->GetRegister(), temp->GetHighInterval()->GetRegister());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001925 locations->SetTempAt(temp_index, location);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001926 } else {
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001927 locations->SetTempAt(temp_index, Location::FpuRegisterLocation(temp->GetRegister()));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001928 }
Roland Levillain5368c212014-11-27 15:03:41 +00001929 break;
1930
1931 default:
1932 LOG(FATAL) << "Unexpected type for temporary location "
1933 << temp->GetType();
1934 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001935 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001936}
1937
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001938} // namespace art