register_allocator.cc revision d0d4852847432368b090c184d6639e573538dccf
1a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray/*
2a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *
4a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * you may not use this file except in compliance with the License.
6a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * You may obtain a copy of the License at
7a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *
8a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *
10a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * See the License for the specific language governing permissions and
14a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * limitations under the License.
15a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray */
16a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
17a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
18a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
19c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#include <sstream>
20c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
21e77493c7217efdd1a0ecef521a6845a13da0305bIan Rogers#include "base/bit_vector-inl.h"
22a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "code_generator.h"
23a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "ssa_liveness_analysis.h"
24a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
25a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraynamespace art {
26a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
27a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraystatic constexpr size_t kMaxLifetimePosition = -1;
2831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraystatic constexpr size_t kDefaultNumberOfSpillSlots = 4;
29a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
3086dbb9a12119273039ce272b41c809fa548b37b6Nicolas GeoffrayRegisterAllocator::RegisterAllocator(ArenaAllocator* allocator,
3186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                     CodeGenerator* codegen,
3286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                     const SsaLivenessAnalysis& liveness)
33a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : allocator_(allocator),
34a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        codegen_(codegen),
3586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        liveness_(liveness),
363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        unhandled_core_intervals_(allocator, 0),
373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        unhandled_fp_intervals_(allocator, 0),
383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        unhandled_(nullptr),
39a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_(allocator, 0),
40a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_(allocator, 0),
41a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_(allocator, 0),
42102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        physical_core_register_intervals_(allocator, codegen->GetNumberOfCoreRegisters()),
43102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        physical_fp_register_intervals_(allocator, codegen->GetNumberOfFloatingPointRegisters()),
443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        temp_intervals_(allocator, 4),
4531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        spill_slots_(allocator, kDefaultNumberOfSpillSlots),
463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        safepoints_(allocator, 0),
47a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        processing_core_registers_(false),
48a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        number_of_registers_(-1),
49a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        registers_array_(nullptr),
50102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        blocked_core_registers_(codegen->GetBlockedCoreRegisters()),
51102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        blocked_fp_registers_(codegen->GetBlockedFloatingPointRegisters()),
523bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray        reserved_out_slots_(0),
533bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray        maximum_number_of_live_registers_(0) {
5471175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray  codegen->SetupBlockedRegisters();
55102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  physical_core_register_intervals_.SetSize(codegen->GetNumberOfCoreRegisters());
56102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  physical_fp_register_intervals_.SetSize(codegen->GetNumberOfFloatingPointRegisters());
573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Always reserve for the current method and the graph's max out registers.
583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // TODO: compute it instead.
593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  reserved_out_slots_ = 1 + codegen->GetGraph()->GetMaximumNumberOfOutVRegs();
60a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
61a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
6286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph,
6386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                InstructionSet instruction_set) {
6486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!Supports(instruction_set)) {
6586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return false;
6686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
6786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = graph.GetBlocks().Size(); i < e; ++i) {
6886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (HInstructionIterator it(graph.GetBlocks().Get(i)->GetInstructions());
6986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray         !it.Done();
7086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray         it.Advance()) {
7186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = it.Current();
72412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray      if (current->GetType() == Primitive::kPrimLong && instruction_set != kX86_64) return false;
73102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      if ((current->GetType() == Primitive::kPrimFloat || current->GetType() == Primitive::kPrimDouble)
74102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          && instruction_set != kX86_64) {
75102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        return false;
76102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      }
7786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
7886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
7986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  return true;
8086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
8186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
8286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) {
833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (interval == nullptr) return false;
8486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  bool is_core_register = (interval->GetType() != Primitive::kPrimDouble)
8586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      && (interval->GetType() != Primitive::kPrimFloat);
86a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return processing_core_registers == is_core_register;
87a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
88a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
8986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegisters() {
9086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  AllocateRegistersInternal();
9186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  Resolve();
9286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
9386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (kIsDebugBuild) {
9486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = true;
9586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
9686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = false;
9786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
9886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
9986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
10086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
10186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::BlockRegister(Location location,
10286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                      size_t start,
103102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                                      size_t end) {
10456b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray  int reg = location.reg();
105102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  DCHECK(location.IsRegister() || location.IsFpuRegister());
106102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  LiveInterval* interval = location.IsRegister()
107102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? physical_core_register_intervals_.Get(reg)
108102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : physical_fp_register_intervals_.Get(reg);
109102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  Primitive::Type type = location.IsRegister()
110102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? Primitive::kPrimInt
111102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : Primitive::kPrimDouble;
11286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval == nullptr) {
11386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    interval = LiveInterval::MakeFixedInterval(allocator_, reg, type);
114102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (location.IsRegister()) {
115102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      physical_core_register_intervals_.Put(reg, interval);
116102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
117102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      physical_fp_register_intervals_.Put(reg, interval);
118102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
11986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
12086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(interval->GetRegister() == reg);
12186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  interval->AddRange(start, end);
12286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
12386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
12486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegistersInternal() {
1253946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Iterate post-order, to ensure the list is sorted, and the last added interval
1263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // is the one with the lowest start position.
1273946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (HLinearPostOrderIterator it(liveness_); !it.Done(); it.Advance()) {
1283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    HBasicBlock* block = it.Current();
129277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
130277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe         back_it.Advance()) {
131277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      ProcessInstruction(back_it.Current());
1323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
133277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
134277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      ProcessInstruction(inst_it.Current());
1353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
1363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
137a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  number_of_registers_ = codegen_->GetNumberOfCoreRegisters();
139a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
1403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  processing_core_registers_ = true;
1413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  unhandled_ = &unhandled_core_intervals_;
142102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
143102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    LiveInterval* fixed = physical_core_register_intervals_.Get(i);
144102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (fixed != nullptr) {
145296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is added to inactive_ instead of unhandled_.
146296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // It's also the only type of inactive interval whose start position
147296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // can be after the current interval during linear scan.
148296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is never split and never moves to unhandled_.
149102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      inactive_.Add(fixed);
150102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
151102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  }
1523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LinearScan();
153a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
154102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  size_t saved_maximum_number_of_live_registers = maximum_number_of_live_registers_;
155102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  maximum_number_of_live_registers_ = 0;
156102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray
1573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  inactive_.Reset();
1583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  active_.Reset();
1593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  handled_.Reset();
160a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters();
1623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
1633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  processing_core_registers_ = false;
1643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  unhandled_ = &unhandled_fp_intervals_;
165102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
166102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
167102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (fixed != nullptr) {
168296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is added to inactive_ instead of unhandled_.
169296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // It's also the only type of inactive interval whose start position
170296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // can be after the current interval during linear scan.
171296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is never split and never moves to unhandled_.
172102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      inactive_.Add(fixed);
173102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
174102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  }
1753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LinearScan();
176102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  maximum_number_of_live_registers_ += saved_maximum_number_of_live_registers;
1773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray}
17886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::ProcessInstruction(HInstruction* instruction) {
1803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LocationSummary* locations = instruction->GetLocations();
1813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  size_t position = instruction->GetLifetimePosition();
1823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
1833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (locations == nullptr) return;
1843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
1853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Create synthesized intervals for temporaries.
1863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < locations->GetTempCount(); ++i) {
1873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    Location temp = locations->GetTemp(i);
1883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (temp.IsRegister()) {
189102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(temp, position, position + 1);
1903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
191102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      DCHECK(temp.IsUnallocated());
19201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      LiveInterval* interval = LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt);
1933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      temp_intervals_.Add(interval);
1943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      interval->AddRange(position, position + 1);
1953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      unhandled_core_intervals_.Add(interval);
196a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
197a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
19886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1993bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  bool core_register = (instruction->GetType() != Primitive::kPrimDouble)
2003bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      && (instruction->GetType() != Primitive::kPrimFloat);
2013bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
2023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (locations->CanCall()) {
2033bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    if (!instruction->IsSuspendCheck()) {
2043bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      codegen_->MarkNotLeaf();
2053bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
2063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    safepoints_.Add(instruction);
2073bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    if (locations->OnlyCallsOnSlowPath()) {
2083bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // We add a synthesized range at this position to record the live registers
2093bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // at this position. Ideally, we could just update the safepoints when locations
2103bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // are updated, but we currently need to know the full stack size before updating
2113bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // locations (because of parameters and the fact that we don't have a frame pointer).
2123bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // And knowing the full stack size requires to know the maximum number of live
2133bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // registers at calls in slow paths.
2143bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // By adding the following interval in the algorithm, we can compute this
2153bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // maximum before updating locations.
2163bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction);
2173bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      interval->AddRange(position, position + 1);
218102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      unhandled_core_intervals_.Add(interval);
219102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      unhandled_fp_intervals_.Add(interval);
2203bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
2213bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  }
2223bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
2233bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  if (locations->WillCall()) {
2243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Block all registers.
2253946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) {
22656b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray      BlockRegister(Location::RegisterLocation(i),
2273946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                    position,
228102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                    position + 1);
229102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
230102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) {
231102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(Location::FpuRegisterLocation(i),
232102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                    position,
233102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                    position + 1);
2343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
2353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
2363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < instruction->InputCount(); ++i) {
2383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    Location input = locations->InAt(i);
239102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (input.IsRegister() || input.IsFpuRegister()) {
240102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(input, position, position + 1);
2413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
2423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
2433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LiveInterval* current = instruction->GetLiveInterval();
2453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current == nullptr) return;
2463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
247102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  GrowableArray<LiveInterval*>& unhandled = core_register
248102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? unhandled_core_intervals_
249102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : unhandled_fp_intervals_;
250102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray
2517690562d40878f44823d5fb03a2084cfc677ec4aNicolas Geoffray  DCHECK(unhandled.IsEmpty() || current->StartsBeforeOrAt(unhandled.Peek()));
2523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Some instructions define their output in fixed register/stack slot. We need
2533946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // to ensure we know these locations before doing register allocation. For a
2543946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // given register, we create an interval that covers these locations. The register
2553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // will be unavailable at these locations when trying to allocate one for an
2563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // interval.
2573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  //
2583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // The backwards walking ensures the ranges are ordered on increasing start positions.
2593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  Location output = locations->Out();
260d0d4852847432368b090c184d6639e573538dccfCalin Juravle  if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
261d0d4852847432368b090c184d6639e573538dccfCalin Juravle    Location first = locations->InAt(0);
262d0d4852847432368b090c184d6639e573538dccfCalin Juravle    if (first.IsRegister() || first.IsFpuRegister()) {
263d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetFrom(position + 1);
264d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetRegister(first.reg());
265d0d4852847432368b090c184d6639e573538dccfCalin Juravle    }
266d0d4852847432368b090c184d6639e573538dccfCalin Juravle  } else if (output.IsRegister() || output.IsFpuRegister()) {
2673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Shift the interval's start by one to account for the blocked register.
2683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetFrom(position + 1);
26956b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray    current->SetRegister(output.reg());
270102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    BlockRegister(output, position, position + 1);
2713946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
2723946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetSpillSlot(output.GetStackIndex());
2733946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
2743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // If needed, add interval to the list of unhandled intervals.
2763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasSpillSlot() || instruction->IsConstant()) {
277c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    // Split just before first register use.
2783946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    size_t first_register_use = current->FirstRegisterUse();
2793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (first_register_use != kNoLifetime) {
280c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray      LiveInterval* split = Split(current, first_register_use - 1);
281b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      // Don't add directly to `unhandled`, it needs to be sorted and the start
2823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // of this new interval might be after intervals already in the list.
2833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      AddSorted(&unhandled, split);
2843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
2853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // Nothing to do, we won't allocate a register for this value.
2863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
2873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
288b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // Don't add directly to `unhandled`, temp or safepoint intervals
289b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // for this instruction may have been added, and those can be
290b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // processed first.
291b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    AddSorted(&unhandled, current);
2923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
293a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
294a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
29531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayclass AllRangesIterator : public ValueObject {
29631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray public:
29731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  explicit AllRangesIterator(LiveInterval* interval)
29831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      : current_interval_(interval),
29931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_(interval->GetFirstRange()) {}
30031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
30131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  bool Done() const { return current_interval_ == nullptr; }
30231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* CurrentRange() const { return current_range_; }
30331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* CurrentInterval() const { return current_interval_; }
30431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
30531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  void Advance() {
30631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    current_range_ = current_range_->GetNext();
30731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    if (current_range_ == nullptr) {
30831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      current_interval_ = current_interval_->GetNextSibling();
30931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current_interval_ != nullptr) {
31031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_ = current_interval_->GetFirstRange();
31131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
31231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
31331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
31431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
31531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray private:
31631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* current_interval_;
31731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* current_range_;
31831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
31931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
32031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray};
32131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
32286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
32386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // To simplify unit testing, we eagerly create the array of intervals, and
32486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // call the helper method.
32586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  GrowableArray<LiveInterval*> intervals(allocator_, 0);
32686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
32786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
32886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
32986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      intervals.Add(instruction->GetLiveInterval());
33086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
33186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
33286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
333102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  if (processing_core_registers_) {
334102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
335102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      LiveInterval* fixed = physical_core_register_intervals_.Get(i);
336102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      if (fixed != nullptr) {
337102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        intervals.Add(fixed);
338102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      }
339102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
340102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  } else {
341102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
342102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
343102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      if (fixed != nullptr) {
344102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        intervals.Add(fixed);
345102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      }
34686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
34786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
34886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
3493946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0, e = temp_intervals_.Size(); i < e; ++i) {
3503946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* temp = temp_intervals_.Get(i);
3513946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, temp)) {
3523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      intervals.Add(temp);
3533946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
3543946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  return ValidateIntervals(intervals, spill_slots_.Size(), reserved_out_slots_, *codegen_,
3573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                           allocator_, processing_core_registers_, log_fatal_on_failure);
35886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
35986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
36031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraybool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
36131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray                                          size_t number_of_spill_slots,
3623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                                          size_t number_of_out_slots,
363a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          const CodeGenerator& codegen,
364a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          ArenaAllocator* allocator,
365a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool processing_core_registers,
366a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool log_fatal_on_failure) {
367a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t number_of_registers = processing_core_registers
368a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      ? codegen.GetNumberOfCoreRegisters()
369a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : codegen.GetNumberOfFloatingPointRegisters();
37031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  GrowableArray<ArenaBitVector*> liveness_of_values(
37131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      allocator, number_of_registers + number_of_spill_slots);
372a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
373a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Allocate a bit vector per register. A live interval that has a register
374a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // allocated will populate the associated bit vector based on its live ranges.
37531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
37631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true));
377a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
378a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
37931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0, e = intervals.Size(); i < e; ++i) {
38031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) {
38131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      LiveInterval* current = it.CurrentInterval();
38286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* defined_by = current->GetParent()->GetDefinedBy();
38386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->GetParent()->HasSpillSlot()
38486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray           // Parameters have their own stack slot.
38586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray           && !(defined_by != nullptr && defined_by->IsParameterValue())) {
3863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers
3873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            + current->GetParent()->GetSpillSlot() / kVRegSize
3883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            - number_of_out_slots);
38931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
39031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_spill_slot->IsBitSet(j)) {
39131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            if (log_fatal_on_failure) {
39231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              std::ostringstream message;
39331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              message << "Spill slot conflict at " << j;
39431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              LOG(FATAL) << message.str();
39531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            } else {
39631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              return false;
39731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            }
39831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          } else {
39931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_spill_slot->SetBit(j);
40031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          }
40131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        }
402a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
40331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
40431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current->HasRegister()) {
40531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister());
40631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
40731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_register->IsBitSet(j)) {
408a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            if (log_fatal_on_failure) {
409a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              std::ostringstream message;
4103946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "Register conflict at " << j << " ";
4113946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              if (defined_by != nullptr) {
4123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                message << "(" << defined_by->DebugName() << ")";
4133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              }
4143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "for ";
415a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              if (processing_core_registers) {
416a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpCoreRegister(message, current->GetRegister());
417a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              } else {
418a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpFloatingPointRegister(message, current->GetRegister());
419a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              }
420a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              LOG(FATAL) << message.str();
421a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            } else {
422a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              return false;
423a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            }
424a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          } else {
42531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_register->SetBit(j);
426a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          }
427a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        }
42831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
42931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
430a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
431a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
432a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
433a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
43486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
435a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  interval->Dump(stream);
436a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << ": ";
437a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (interval->HasRegister()) {
438102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (interval->IsFloatingPoint()) {
43986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
440102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
441102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      codegen_->DumpCoreRegister(stream, interval->GetRegister());
442a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
443a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
444a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    stream << "spilled";
445a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
446a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << std::endl;
447a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
448a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
449296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yangvoid RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
450296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "inactive: " << std::endl;
451296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < inactive_.Size(); i ++) {
452296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, inactive_.Get(i));
453296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
454296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "active: " << std::endl;
455296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < active_.Size(); i ++) {
456296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, active_.Get(i));
457296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
458296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "unhandled: " << std::endl;
459296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  auto unhandled = (unhandled_ != nullptr) ?
460296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      unhandled_ : &unhandled_core_intervals_;
461296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < unhandled->Size(); i ++) {
462296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, unhandled->Get(i));
463296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
464296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "handled: " << std::endl;
465296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < handled_.Size(); i ++) {
466296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, handled_.Get(i));
467296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
468296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang}
469296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
470a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// By the book implementation of a linear scan register allocator.
471a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::LinearScan() {
4723946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  while (!unhandled_->IsEmpty()) {
473a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (1) Remove interval with the lowest start position from unhandled.
4743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = unhandled_->Pop();
4753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    DCHECK(!current->IsFixed() && !current->HasSpillSlot());
476c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart());
477a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t position = current->GetStart();
478a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
479296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Remember the inactive_ size here since the ones moved to inactive_ from
480296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // active_ below shouldn't need to be re-checked.
481296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    size_t inactive_intervals_to_handle = inactive_.Size();
482296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
483a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (2) Remove currently active intervals that are dead at this position.
484a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move active intervals that have a lifetime hole at this position
485a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     to inactive.
486a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0; i < active_.Size(); ++i) {
487a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = active_.Get(i);
488a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
489a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
490a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
491a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
492a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (!interval->Covers(position)) {
493a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
494a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
495a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Add(interval);
496a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
497a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
498a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
499a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (3) Remove currently inactive intervals that are dead at this position.
500a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move inactive intervals that cover this position to active.
501296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    for (size_t i = 0; i < inactive_intervals_to_handle; ++i) {
502a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = inactive_.Get(i);
503296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK(interval->GetStart() < position || interval->IsFixed());
504a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
505a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
506a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
507296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        --inactive_intervals_to_handle;
508a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
509a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (interval->Covers(position)) {
510a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
511a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
512296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        --inactive_intervals_to_handle;
513a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Add(interval);
514a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
515a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
516a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
5173bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    if (current->IsSlowPathSafepoint()) {
5183bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // Synthesized interval to record the maximum number of live registers
5193bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // at safepoints. No need to allocate a register for it.
5203bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      maximum_number_of_live_registers_ =
5213bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray          std::max(maximum_number_of_live_registers_, active_.Size());
5223bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      continue;
5233bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
5243bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
525a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (4) Try to find an available register.
526a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    bool success = TryAllocateFreeReg(current);
527a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
528a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (5) If no register could be found, we need to spill.
529a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (!success) {
530a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      success = AllocateBlockedReg(current);
531a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
532a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
533a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (6) If the interval had a register allocated, add it to the list of active
534a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     intervals.
535a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (success) {
536a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      active_.Add(current);
537a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
538a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
539a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
540a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
541a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find a free register. If multiple are found, pick the register that
542a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// is free the longest.
543a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
544a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* free_until = registers_array_;
545a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
546a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers to be free.
547a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
548a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    free_until[i] = kMaxLifetimePosition;
549a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
550a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
551296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  // For each active interval, set its register to not free.
552296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
553296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    LiveInterval* interval = active_.Get(i);
554296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(interval->HasRegister());
555296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    free_until[interval->GetRegister()] = 0;
556296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
557296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
558a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, set its register to be free until
559a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // the next intersection with `current`.
560a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
561a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
562296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
563296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
564296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
565296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
566296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
567296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
568296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
569296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
570296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
571296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
572296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
573a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
574296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (free_until[inactive->GetRegister()] == 0) {
575296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Already used by some active interval. No need to intersect.
576296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
577296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
578a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
579a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (next_intersection != kNoLifetime) {
580aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray      free_until[inactive->GetRegister()] =
581aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray          std::min(free_until[inactive->GetRegister()], next_intersection);
582a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
583a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
584a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
585a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  int reg = -1;
5863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasRegister()) {
5873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Some instructions have a fixed register output.
5883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    reg = current->GetRegister();
5893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    DCHECK_NE(free_until[reg], 0u);
5903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
59101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    int hint = current->FindFirstRegisterHint(free_until);
59201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    if (hint != kNoRegister) {
59301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      DCHECK(!IsBlocked(hint));
59401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      reg = hint;
59501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    } else {
59601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      // Pick the register that is free the longest.
59701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      for (size_t i = 0; i < number_of_registers_; ++i) {
59801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        if (IsBlocked(i)) continue;
59901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        if (reg == -1 || free_until[i] > free_until[reg]) {
60001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray          reg = i;
60101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray          if (free_until[i] == kMaxLifetimePosition) break;
60201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        }
6033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
604a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
605a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
606a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
607a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // If we could not find a register, we need to spill.
608a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (reg == -1 || free_until[reg] == 0) {
609a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
610a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
611a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
612a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  current->SetRegister(reg);
613a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (!current->IsDeadAt(free_until[reg])) {
614a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the register is only available for a subset of live ranges
615a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // covered by `current`, split `current` at the position where
616a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // the register is not available anymore.
617a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* split = Split(current, free_until[reg]);
618a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(split != nullptr);
6193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    AddSorted(unhandled_, split);
620a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
621a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
622a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
623a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
624a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::IsBlocked(int reg) const {
625102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return processing_core_registers_
626102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? blocked_core_registers_[reg]
627102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : blocked_fp_registers_[reg];
628a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
629a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
630a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval
631a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register
632a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead.
633a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
634a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t first_register_use = current->FirstRegisterUse();
635412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  if (first_register_use == kNoLifetime) {
63631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
637a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
638a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
639a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
640a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers as not being used.
641a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* next_use = registers_array_;
642a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
643a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    next_use[i] = kMaxLifetimePosition;
644a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
645a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
646a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each active interval, find the next use of its register after the
647a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
648a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
649a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* active = active_.Get(i);
650a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(active->HasRegister());
65186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (active->IsFixed()) {
65286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      next_use[active->GetRegister()] = current->GetStart();
65386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
65486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      size_t use = active->FirstRegisterUseAfter(current->GetStart());
65586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (use != kNoLifetime) {
65686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[active->GetRegister()] = use;
65786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
658a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
659a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
660a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
661a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, find the next use of its register after the
662a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
663a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
664a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
665296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
666296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
667296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
668296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
669296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
670296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
671296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
672296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
673296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
674296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
675a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
67686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
67786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_intersection != kNoLifetime) {
67886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (inactive->IsFixed()) {
67986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[inactive->GetRegister()] =
68086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            std::min(next_intersection, next_use[inactive->GetRegister()]);
68186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else {
68286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t use = inactive->FirstRegisterUseAfter(current->GetStart());
68386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (use != kNoLifetime) {
68486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
68586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
68686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
687a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
688a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
689a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
690a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Pick the register that is used the last.
691a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  int reg = -1;
692a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
693a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (IsBlocked(i)) continue;
694a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (reg == -1 || next_use[i] > next_use[reg]) {
695a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      reg = i;
696a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition) break;
697a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
698a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
699a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
700a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (first_register_use >= next_use[reg]) {
701a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the first use of that instruction is after the last use of the found
702a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // register, we split this interval just before its first register use.
70331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
704c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    LiveInterval* split = Split(current, first_register_use - 1);
705c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    DCHECK_NE(current, split) << "There is not enough registers available for "
706c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray      << split->GetParent()->GetDefinedBy()->DebugName();
7073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    AddSorted(unhandled_, split);
708a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
709a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
710a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Use this register and spill the active and inactives interval that
711a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // have that register.
712a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    current->SetRegister(reg);
713a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
714a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0, e = active_.Size(); i < e; ++i) {
715a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* active = active_.Get(i);
716a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (active->GetRegister() == reg) {
71786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(!active->IsFixed());
718a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        LiveInterval* split = Split(active, current->GetStart());
719a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.DeleteAt(i);
720a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(active);
7213946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        AddSorted(unhandled_, split);
722a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        break;
723a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
724a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
725a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
726296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
727a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* inactive = inactive_.Get(i);
728a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (inactive->GetRegister() == reg) {
729296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        if (!current->IsSplit() && !inactive->IsFixed()) {
730296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Neither current nor inactive are fixed.
731296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Thanks to SSA, a non-split interval starting in a hole of an
732296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // inactive interval should never intersect with that inactive interval.
733296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Only if it's not fixed though, because fixed intervals don't come from SSA.
734296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
735296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          continue;
736296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        }
73786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t next_intersection = inactive->FirstIntersectionWith(current);
73886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (next_intersection != kNoLifetime) {
73986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          if (inactive->IsFixed()) {
74086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            LiveInterval* split = Split(current, next_intersection);
7413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
74286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          } else {
743296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang            LiveInterval* split = Split(inactive, next_intersection);
74486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            inactive_.DeleteAt(i);
745296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang            --i;
746296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang            --e;
74786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            handled_.Add(inactive);
7483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
74986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          }
75086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
751a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
752a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
753a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
754a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return true;
755a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
756a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
757a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) {
759c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray  DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
76086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t insert_at = 0;
7613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = array->Size(); i > 0; --i) {
7623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = array->Get(i - 1);
763a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (current->StartsAfter(interval)) {
76486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      insert_at = i;
765a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      break;
766a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
767a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
7683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  array->InsertAt(insert_at, interval);
769a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
770a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
771a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
772a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(position >= interval->GetStart());
773a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(!interval->IsDeadAt(position));
774a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (position == interval->GetStart()) {
775a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Spill slot will be allocated when handling `interval` again.
776a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    interval->ClearRegister();
777a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return interval;
778a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
779a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* new_interval = interval->SplitAt(position);
780a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return new_interval;
781a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
782a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
783a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
78431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
78531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* parent = interval->GetParent();
78631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
78731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // An instruction gets a spill slot for its entire lifetime. If the parent
78831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // of this interval already has a spill slot, there is nothing to do.
78931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  if (parent->HasSpillSlot()) {
79031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    return;
79131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
79231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
79386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* defined_by = parent->GetDefinedBy();
79486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (defined_by->IsParameterValue()) {
79586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Parameters have their own stack slot.
79686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
79786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
79886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
79986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
80096f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  if (defined_by->IsConstant()) {
80196f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    // Constants don't need a spill slot.
80296f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    return;
80396f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  }
80496f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray
80531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* last_sibling = interval;
80631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  while (last_sibling->GetNextSibling() != nullptr) {
80731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    last_sibling = last_sibling->GetNextSibling();
80831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
80931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  size_t end = last_sibling->GetEnd();
81031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
811412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  // Find an available spill slot.
812412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  size_t slot = 0;
813412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  for (size_t e = spill_slots_.Size(); slot < e; ++slot) {
814412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    // We check if it is less rather than less or equal because the parallel move
815412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    // resolver does not work when a single spill slot needs to be exchanged with
816412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    // a double spill slot. The strict comparison avoids needing to exchange these
817412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    // locations at the same lifetime position.
818412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    if (spill_slots_.Get(slot) < parent->GetStart()
819412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray        && (slot == (e - 1) || spill_slots_.Get(slot + 1) < parent->GetStart())) {
820412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray      break;
821412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    }
822412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
823412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
82401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  if (parent->NeedsTwoSpillSlots()) {
8253c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    if (slot == spill_slots_.Size()) {
8263c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
8273c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Add(end);
8283c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Add(end);
8293c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else if (slot == spill_slots_.Size() - 1) {
8303c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Put(slot, end);
8313c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Add(end);
8323c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
8333c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Put(slot, end);
8343c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Put(slot + 1, end);
83531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
83631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  } else {
8373c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    if (slot == spill_slots_.Size()) {
8383c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
8393c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Add(end);
8403c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
8413c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Put(slot, end);
8423c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    }
84331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
84431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
8453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  parent->SetSpillSlot((slot + reserved_out_slots_) * kVRegSize);
84686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
84786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
8482a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffraystatic bool IsValidDestination(Location destination) {
849102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return destination.IsRegister()
850102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsFpuRegister()
851102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsStackSlot()
852102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsDoubleStackSlot();
8532a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray}
8542a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray
855740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* user,
85686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
85786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
8582a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray  DCHECK(IsValidDestination(destination));
85986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
86086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
861476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  DCHECK(!user->IsPhi());
86286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
863740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  HInstruction* previous = user->GetPrevious();
86486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = nullptr;
86586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (previous == nullptr
866476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain      || !previous->IsParallelMove()
8678e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray      || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
86886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
8698e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    move->SetLifetimePosition(user->GetLifetimePosition());
870740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    user->GetBlock()->InsertInstructionBefore(move, user);
87186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
87286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
87386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
8748e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
875740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination, nullptr));
87686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
87786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
87886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position,
879740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                             HInstruction* instruction,
88086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location source,
88186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location destination) const {
8822a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray  DCHECK(IsValidDestination(destination));
88386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
88486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
88586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
88686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (at == nullptr) {
887296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Block boundary, don't do anything the connection of split siblings will handle it.
88886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
88986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
89086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
89186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if ((position & 1) == 1) {
89286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen after the instruction.
89386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(!at->IsControlFlow());
89486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = at->GetNext()->AsParallelMove();
895e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // This is a parallel move for connecting siblings in a same block. We need to
896e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // differentiate it with moves for connecting blocks, and input moves.
8978e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    if (move == nullptr || move->GetLifetimePosition() > position) {
89886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
89986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
90086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
90186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
90286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
90386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen before the instruction.
90486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* previous = at->GetPrevious();
905740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    if (previous == nullptr
906740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || !previous->IsParallelMove()
907740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || previous->GetLifetimePosition() != position) {
908740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // If the previous is a parallel move, then its position must be lower
909740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // than the given `position`: it was added just after the non-parallel
910740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // move instruction that precedes `instruction`.
911740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      DCHECK(previous == nullptr
912740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || !previous->IsParallelMove()
913740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || previous->GetLifetimePosition() < position);
91486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
91586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
91686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at);
91786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
91886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = previous->AsParallelMove();
91986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
92086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
92101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), position);
922740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
92386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
92486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
92586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
926740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                   HInstruction* instruction,
92786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location source,
92886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location destination) const {
9292a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray  DCHECK(IsValidDestination(destination));
93086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
93186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
93286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK_EQ(block->GetSuccessors().Size(), 1u);
93386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* last = block->GetLastInstruction();
934360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // We insert moves at exit for phi predecessors and connecting blocks.
935360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // A block ending with an if cannot branch to a block with phis because
936360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // we do not allow critical edges. It can also not connect
937360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // a split interval between two blocks: the move has to happen in the successor.
938360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  DCHECK(!last->IsIf());
93986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* previous = last->GetPrevious();
94086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
941e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
942e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and output moves.
943740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  if (previous == nullptr || !previous->IsParallelMove()
944e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray      || previous->AsParallelMove()->GetLifetimePosition() != block->GetLifetimeEnd()) {
94586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
946e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(block->GetLifetimeEnd());
94786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, last);
94886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
94986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
95086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
951740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
95286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
95386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
95486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
955740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                    HInstruction* instruction,
95686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location source,
95786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location destination) const {
9582a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray  DCHECK(IsValidDestination(destination));
95986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
96086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
96186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* first = block->GetFirstInstruction();
96286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = first->AsParallelMove();
963e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
964e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and input moves.
965e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != block->GetLifetimeStart()) {
96686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
96786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move->SetLifetimePosition(block->GetLifetimeStart());
96886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, first);
96986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
970740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
97186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
97286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
97386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
97486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
97586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
9762a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray  DCHECK(IsValidDestination(destination));
97786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
97886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
979476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  if (instruction->IsPhi()) {
980740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
98186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
98286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
98386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
984e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  size_t position = instruction->GetLifetimePosition() + 1;
98586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = instruction->GetNext()->AsParallelMove();
986e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for moving the output of an instruction. We need
987e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // to differentiate with input moves, moves for connecting siblings in a
988e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // and moves for connecting blocks.
989e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
99086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
991e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(position);
99286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
99386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
994740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
99586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
99686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
99786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
99886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
99986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (current->HasSpillSlot() && current->HasRegister()) {
100086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // We spill eagerly, so move must be at definition.
100186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertMoveAfter(interval->GetDefinedBy(),
1002102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                    interval->IsFloatingPoint()
1003102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                        ? Location::FpuRegisterLocation(interval->GetRegister())
1004102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                        : Location::RegisterLocation(interval->GetRegister()),
100501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                    interval->NeedsTwoSpillSlots()
1006412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1007412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
100886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
100986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  UsePosition* use = current->GetFirstUse();
101086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
101186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Walk over all siblings, updating locations of use positions, and
101286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // connecting them when they are adjacent.
101386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  do {
101401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
101586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
101686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Walk over all uses covered by this interval, and update the location
101786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // information.
101886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    while (use != nullptr && use->GetPosition() <= current->GetEnd()) {
10193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      LocationSummary* locations = use->GetUser()->GetLocations();
10203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      if (use->GetIsEnvironment()) {
10213946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetEnvironmentAt(use->GetInputIndex(), source);
10223946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      } else {
102386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        Location expected_location = locations->InAt(use->GetInputIndex());
102486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (expected_location.IsUnallocated()) {
102586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          locations->SetInAt(use->GetInputIndex(), source);
10262a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray        } else if (!expected_location.IsConstant()) {
102786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          AddInputMoveFor(use->GetUser(), source, expected_location);
102886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
102986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
103086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      use = use->GetNext();
103186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
103286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
103386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // If the next interval starts just after this one, and has a register,
103486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // insert a move.
103586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* next_sibling = current->GetNextSibling();
103686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_sibling != nullptr
103786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && next_sibling->HasRegister()
103886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && current->GetEnd() == next_sibling->GetStart()) {
103901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      Location destination = next_sibling->ToLocation();
1040740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
104186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
10423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
10433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // At each safepoint, we record stack and register information.
10443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    for (size_t i = 0, e = safepoints_.Size(); i < e; ++i) {
10453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      HInstruction* safepoint = safepoints_.Get(i);
10463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      size_t position = safepoint->GetLifetimePosition();
10473946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      LocationSummary* locations = safepoint->GetLocations();
1048b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      if (!current->Covers(position)) {
1049b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        continue;
1050b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      }
1051b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      if (interval->GetStart() == position) {
1052b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        // The safepoint is for this instruction, so the location of the instruction
1053b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        // does not need to be saved.
1054b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        continue;
1055b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      }
10563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
10573bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
10583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
10593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
10603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
10613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      switch (source.GetKind()) {
10623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kRegister: {
10633bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray          locations->AddLiveRegister(source);
10643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          if (current->GetType() == Primitive::kPrimNot) {
106556b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray            locations->SetRegisterBit(source.reg());
10663946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          }
10673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
10683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
1069102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        case Location::kFpuRegister: {
1070102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          locations->AddLiveRegister(source);
1071102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          break;
1072102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        }
10733946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kStackSlot:  // Fall-through
10743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kDoubleStackSlot:  // Fall-through
10753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kConstant: {
10763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          // Nothing to do.
10773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
10783946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
10793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        default: {
10803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          LOG(FATAL) << "Unexpected location for object";
10813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
10823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
10833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
108486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = next_sibling;
108586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } while (current != nullptr);
108686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(use == nullptr);
108786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
108886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
108986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
109086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* from,
109186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* to) const {
109286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval->GetNextSibling() == nullptr) {
109386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Nothing to connect. The whole range was allocated to the same location.
109486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
109586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
109686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
109786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t from_position = from->GetLifetimeEnd() - 1;
1098296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  // When an instruction dies at entry of another, and the latter is the beginning
10997690562d40878f44823d5fb03a2084cfc677ec4aNicolas Geoffray  // of a block, the register allocator ensures the former has a register
11007690562d40878f44823d5fb03a2084cfc677ec4aNicolas Geoffray  // at block->GetLifetimeStart() + 1. Since this is at a block boundary, it must
11017690562d40878f44823d5fb03a2084cfc677ec4aNicolas Geoffray  // must be handled in this method.
11027690562d40878f44823d5fb03a2084cfc677ec4aNicolas Geoffray  size_t to_position = to->GetLifetimeStart() + 1;
110386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
110486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* destination = nullptr;
110586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* source = nullptr;
110686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
110786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
110886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
110986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Check the intervals that cover `from` and `to`.
111086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  while ((current != nullptr) && (source == nullptr || destination == nullptr)) {
111186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (current->Covers(from_position)) {
111286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source == nullptr);
111386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      source = current;
111486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
111586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (current->Covers(to_position)) {
111686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(destination == nullptr);
111786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      destination = current;
111886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
111986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
112086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = current->GetNextSibling();
112186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
112286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
112386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (destination == source) {
112486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Interval was not split.
112586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
112686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
112786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
11288ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray  DCHECK(destination != nullptr && source != nullptr);
11298ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray
113086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!destination->HasRegister()) {
113186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Values are eagerly spilled. Spill slot already contains appropriate value.
113286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
113386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
113486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
113586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
113686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // we need to put the moves at the entry of `to`.
113786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (from->GetSuccessors().Size() == 1) {
1138740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtExitOf(from,
1139740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                               interval->GetParent()->GetDefinedBy(),
114001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               source->ToLocation(),
114101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               destination->ToLocation());
114286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
114386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK_EQ(to->GetPredecessors().Size(), 1u);
1144740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(to,
1145740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                interval->GetParent()->GetDefinedBy(),
114601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                source->ToLocation(),
114701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                destination->ToLocation());
114886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
114986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
115086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
115186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() {
11523bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  codegen_->ComputeFrameSize(
11533bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      spill_slots_.Size(), maximum_number_of_live_registers_, reserved_out_slots_);
115486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
115586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Adjust the Out Location of instructions.
115686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
115786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
115886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
115986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* current = instruction->GetLiveInterval();
116086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LocationSummary* locations = instruction->GetLocations();
116186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location location = locations->Out();
1162476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain    if (instruction->IsParameterValue()) {
116386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Now that we know the frame size, adjust the parameter's location.
116486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.IsStackSlot()) {
116586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
116686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
116786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        locations->SetOut(location);
116886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (location.IsDoubleStackSlot()) {
116986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
117086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
117186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        locations->SetOut(location);
117286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (current->HasSpillSlot()) {
117386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
117486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
117586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
117686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
117701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
117886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
117986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (location.IsUnallocated()) {
118086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.GetPolicy() == Location::kSameAsFirstInput) {
1181d0d4852847432368b090c184d6639e573538dccfCalin Juravle        if (locations->InAt(0).IsUnallocated()) {
1182d0d4852847432368b090c184d6639e573538dccfCalin Juravle          locations->SetInAt(0, source);
1183d0d4852847432368b090c184d6639e573538dccfCalin Juravle        } else {
1184d0d4852847432368b090c184d6639e573538dccfCalin Juravle          DCHECK(locations->InAt(0).Equals(source));
1185d0d4852847432368b090c184d6639e573538dccfCalin Juravle        }
118686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
118786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      locations->SetOut(source);
118886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
118986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source.Equals(location));
119086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
119186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
119286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
119386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Connect siblings.
119486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
119586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
119686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ConnectSiblings(instruction->GetLiveInterval());
119786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
119886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
119986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve non-linear control flow across branches. Order does not matter.
120086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
120186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* block = it.Current();
120286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    BitVector* live = liveness_.GetLiveInSet(*block);
120386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (uint32_t idx : live->Indexes()) {
120486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
120586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      LiveInterval* interval = current->GetLiveInterval();
120686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
120786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
120886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
120986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
121086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
121186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
121286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve phi inputs. Order does not matter.
121386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
121486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* current = it.Current();
1215277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1216277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      HInstruction* phi = inst_it.Current();
121786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
121886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HBasicBlock* predecessor = current->GetPredecessors().Get(i);
121986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
122086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HInstruction* input = phi->InputAt(i);
122101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location source = input->GetLiveInterval()->GetLocationAt(
122201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray            predecessor->GetLifetimeEnd() - 1);
122301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location destination = phi->GetLiveInterval()->ToLocation();
1224740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        InsertParallelMoveAtExitOf(predecessor, nullptr, source, destination);
122586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
122686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
122786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
12283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
12293946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Assign temp locations.
12303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  HInstruction* current = nullptr;
12313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  size_t temp_index = 0;
12323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < temp_intervals_.Size(); ++i) {
12333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* temp = temp_intervals_.Get(i);
123401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    HInstruction* at = liveness_.GetTempUser(temp);
123501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    if (at != current) {
12363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      temp_index = 0;
123701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      current = at;
12383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
123901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    LocationSummary* locations = at->GetLocations();
1240102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    DCHECK(temp->GetType() == Primitive::kPrimInt);
12413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    locations->SetTempAt(
124256b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray        temp_index++, Location::RegisterLocation(temp->GetRegister()));
12433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
124431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}
124531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1246a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}  // namespace art
1247