register_allocator.cc revision 76b1e1799a713a19218de26b171b0aef48a59e98
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
19234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray#include <iostream>
20c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#include <sstream>
21c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
22e77493c7217efdd1a0ecef521a6845a13da0305bIan Rogers#include "base/bit_vector-inl.h"
23a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "code_generator.h"
24a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "ssa_liveness_analysis.h"
25a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
26a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraynamespace art {
27a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
28a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraystatic constexpr size_t kMaxLifetimePosition = -1;
2931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraystatic constexpr size_t kDefaultNumberOfSpillSlots = 4;
30a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
31840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray// For simplicity, we implement register pairs as (reg, reg + 1).
32840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray// Note that this is a requirement for double registers on ARM, since we
33840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray// allocate SRegister.
34840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffraystatic int GetHighForLowRegister(int reg) { return reg + 1; }
35840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffraystatic bool IsLowRegister(int reg) { return (reg & 1) == 0; }
36234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraystatic bool IsLowOfUnalignedPairInterval(LiveInterval* low) {
37234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  return GetHighForLowRegister(low->GetRegister()) != low->GetHighInterval()->GetRegister();
38234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray}
39840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
4086dbb9a12119273039ce272b41c809fa548b37b6Nicolas GeoffrayRegisterAllocator::RegisterAllocator(ArenaAllocator* allocator,
4186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                     CodeGenerator* codegen,
4286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                     const SsaLivenessAnalysis& liveness)
43a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : allocator_(allocator),
44a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        codegen_(codegen),
4586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        liveness_(liveness),
463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        unhandled_core_intervals_(allocator, 0),
473946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        unhandled_fp_intervals_(allocator, 0),
483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        unhandled_(nullptr),
49a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_(allocator, 0),
50a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_(allocator, 0),
51a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_(allocator, 0),
52102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        physical_core_register_intervals_(allocator, codegen->GetNumberOfCoreRegisters()),
53102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        physical_fp_register_intervals_(allocator, codegen->GetNumberOfFloatingPointRegisters()),
543946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        temp_intervals_(allocator, 4),
55776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        int_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
56776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        long_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
57776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        float_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
58776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        double_spill_slots_(allocator, kDefaultNumberOfSpillSlots),
593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        safepoints_(allocator, 0),
60a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        processing_core_registers_(false),
61a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        number_of_registers_(-1),
62a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        registers_array_(nullptr),
63102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        blocked_core_registers_(codegen->GetBlockedCoreRegisters()),
64102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        blocked_fp_registers_(codegen->GetBlockedFloatingPointRegisters()),
653bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray        reserved_out_slots_(0),
66f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_core_registers_(0),
67f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_fp_registers_(0) {
68988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray  static constexpr bool kIsBaseline = false;
69988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray  codegen->SetupBlockedRegisters(kIsBaseline);
70102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  physical_core_register_intervals_.SetSize(codegen->GetNumberOfCoreRegisters());
71102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  physical_fp_register_intervals_.SetSize(codegen->GetNumberOfFloatingPointRegisters());
723946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Always reserve for the current method and the graph's max out registers.
733946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // TODO: compute it instead.
743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  reserved_out_slots_ = 1 + codegen->GetGraph()->GetMaximumNumberOfOutVRegs();
75a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
76a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
77234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraybool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph ATTRIBUTE_UNUSED,
7886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                InstructionSet instruction_set) {
79234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  return instruction_set == kArm64
806c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || instruction_set == kX86_64
816c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || instruction_set == kArm
82234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      || instruction_set == kX86
83234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      || instruction_set == kThumb2;
8486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
8586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
8686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) {
873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (interval == nullptr) return false;
8886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  bool is_core_register = (interval->GetType() != Primitive::kPrimDouble)
8986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      && (interval->GetType() != Primitive::kPrimFloat);
90a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return processing_core_registers == is_core_register;
91a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
92a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegisters() {
9486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  AllocateRegistersInternal();
9586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  Resolve();
9686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
9786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (kIsDebugBuild) {
9886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = true;
9986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
10086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = false;
10186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
1025976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // Check that the linear order is still correct with regards to lifetime positions.
1035976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // Since only parallel moves have been inserted during the register allocation,
1045976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // these checks are mostly for making sure these moves have been added correctly.
1055976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    size_t current_liveness = 0;
1060d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray    for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
1075976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      HBasicBlock* block = it.Current();
1085976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1095976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        HInstruction* instruction = inst_it.Current();
1105976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_LE(current_liveness, instruction->GetLifetimePosition());
1115976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        current_liveness = instruction->GetLifetimePosition();
1125976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      }
1135976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      for (HInstructionIterator inst_it(block->GetInstructions());
1145976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray           !inst_it.Done();
1155976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray           inst_it.Advance()) {
1165976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        HInstruction* instruction = inst_it.Current();
1175976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_LE(current_liveness, instruction->GetLifetimePosition()) << instruction->DebugName();
1185976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        current_liveness = instruction->GetLifetimePosition();
1195976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      }
1205976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    }
12186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
12286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
12386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
12486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::BlockRegister(Location location,
12586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                      size_t start,
126102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                                      size_t end) {
12756b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray  int reg = location.reg();
128102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  DCHECK(location.IsRegister() || location.IsFpuRegister());
129102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  LiveInterval* interval = location.IsRegister()
130102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? physical_core_register_intervals_.Get(reg)
131102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : physical_fp_register_intervals_.Get(reg);
132102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  Primitive::Type type = location.IsRegister()
133102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? Primitive::kPrimInt
134840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      : Primitive::kPrimFloat;
13586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval == nullptr) {
13686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    interval = LiveInterval::MakeFixedInterval(allocator_, reg, type);
137102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (location.IsRegister()) {
138102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      physical_core_register_intervals_.Put(reg, interval);
139102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
140102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      physical_fp_register_intervals_.Put(reg, interval);
141102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
14286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
14386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(interval->GetRegister() == reg);
14486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  interval->AddRange(start, end);
14586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
14686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
14786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegistersInternal() {
1483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Iterate post-order, to ensure the list is sorted, and the last added interval
1493946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // is the one with the lowest start position.
1500d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearPostOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
1513946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    HBasicBlock* block = it.Current();
152277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
153277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe         back_it.Advance()) {
154277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      ProcessInstruction(back_it.Current());
1553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
156277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
157277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      ProcessInstruction(inst_it.Current());
1583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
1593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
160a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  number_of_registers_ = codegen_->GetNumberOfCoreRegisters();
162a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
1633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  processing_core_registers_ = true;
1643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  unhandled_ = &unhandled_core_intervals_;
165102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
166102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    LiveInterval* fixed = physical_core_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();
176a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  inactive_.Reset();
1783946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  active_.Reset();
1793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  handled_.Reset();
180a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters();
1823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
1833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  processing_core_registers_ = false;
1843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  unhandled_ = &unhandled_fp_intervals_;
185102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
186102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
187102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (fixed != nullptr) {
188296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is added to inactive_ instead of unhandled_.
189296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // It's also the only type of inactive interval whose start position
190296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // can be after the current interval during linear scan.
191296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is never split and never moves to unhandled_.
192102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      inactive_.Add(fixed);
193102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
194102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  }
1953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LinearScan();
1963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray}
19786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::ProcessInstruction(HInstruction* instruction) {
1993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LocationSummary* locations = instruction->GetLocations();
2003946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  size_t position = instruction->GetLifetimePosition();
2013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (locations == nullptr) return;
2033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Create synthesized intervals for temporaries.
2053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < locations->GetTempCount(); ++i) {
2063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    Location temp = locations->GetTemp(i);
20752839d17c06175e19ca4a093fb878450d1c4310dNicolas Geoffray    if (temp.IsRegister() || temp.IsFpuRegister()) {
208102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(temp, position, position + 1);
2093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
210102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      DCHECK(temp.IsUnallocated());
2115368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      switch (temp.GetPolicy()) {
2125368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        case Location::kRequiresRegister: {
2135368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LiveInterval* interval =
2145368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain              LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt);
2155368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          temp_intervals_.Add(interval);
216f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          interval->AddTempUse(instruction, i);
2175368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          unhandled_core_intervals_.Add(interval);
2185368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          break;
2195368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        }
2205368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
2215368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        case Location::kRequiresFpuRegister: {
2225368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LiveInterval* interval =
2235368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain              LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimDouble);
2245368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          temp_intervals_.Add(interval);
225f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          interval->AddTempUse(instruction, i);
226840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
2275588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray            interval->AddHighInterval(/* is_temp */ true);
228840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            LiveInterval* high = interval->GetHighInterval();
229840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            temp_intervals_.Add(high);
230840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            unhandled_fp_intervals_.Add(high);
231840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          }
2325368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          unhandled_fp_intervals_.Add(interval);
2335368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          break;
2345368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        }
2355368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
2365368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        default:
2375368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LOG(FATAL) << "Unexpected policy for temporary location "
2385368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                     << temp.GetPolicy();
2395368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      }
240a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
241a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
24286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
2433bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  bool core_register = (instruction->GetType() != Primitive::kPrimDouble)
2443bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      && (instruction->GetType() != Primitive::kPrimFloat);
2453bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
2463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (locations->CanCall()) {
247c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray    if (codegen_->IsLeafMethod()) {
248c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      // TODO: We do this here because we do not want the suspend check to artificially
249c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      // create live registers. We should find another place, but this is currently the
250c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      // simplest.
251c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      DCHECK(instruction->IsSuspendCheckEntry());
252c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      instruction->GetBlock()->RemoveInstruction(instruction);
253c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      return;
2543bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
2553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    safepoints_.Add(instruction);
2563bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    if (locations->OnlyCallsOnSlowPath()) {
2573bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // We add a synthesized range at this position to record the live registers
2583bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // at this position. Ideally, we could just update the safepoints when locations
2593bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // are updated, but we currently need to know the full stack size before updating
2603bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // locations (because of parameters and the fact that we don't have a frame pointer).
2613bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // And knowing the full stack size requires to know the maximum number of live
2623bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // registers at calls in slow paths.
2633bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // By adding the following interval in the algorithm, we can compute this
2643bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // maximum before updating locations.
2653bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction);
266acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      interval->AddRange(position, position + 1);
26787d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray      AddSorted(&unhandled_core_intervals_, interval);
26887d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray      AddSorted(&unhandled_fp_intervals_, interval);
2693bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
2703bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  }
2713bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
2723bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  if (locations->WillCall()) {
2733946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Block all registers.
2743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) {
275988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      if (!codegen_->IsCoreCalleeSaveRegister(i)) {
276988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray        BlockRegister(Location::RegisterLocation(i),
277988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      position,
278988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      position + 1);
279988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      }
280102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
281102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) {
282988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      if (!codegen_->IsFloatingPointCalleeSaveRegister(i)) {
283988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray        BlockRegister(Location::FpuRegisterLocation(i),
284988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      position,
285988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      position + 1);
286988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      }
2873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
2883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
2893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < instruction->InputCount(); ++i) {
2913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    Location input = locations->InAt(i);
292102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (input.IsRegister() || input.IsFpuRegister()) {
293102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(input, position, position + 1);
294840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (input.IsPair()) {
295840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      BlockRegister(input.ToLow(), position, position + 1);
296840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      BlockRegister(input.ToHigh(), position, position + 1);
2973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
2983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
2993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3003946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LiveInterval* current = instruction->GetLiveInterval();
3013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current == nullptr) return;
3023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
303102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  GrowableArray<LiveInterval*>& unhandled = core_register
304102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? unhandled_core_intervals_
305102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : unhandled_fp_intervals_;
306102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray
3077690562d40878f44823d5fb03a2084cfc677ec4aNicolas Geoffray  DCHECK(unhandled.IsEmpty() || current->StartsBeforeOrAt(unhandled.Peek()));
30887d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray
309840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (codegen_->NeedsTwoRegisters(current->GetType())) {
310840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->AddHighInterval();
311840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
312840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
3135588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray  for (size_t safepoint_index = safepoints_.Size(); safepoint_index > 0; --safepoint_index) {
3145588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    HInstruction* safepoint = safepoints_.Get(safepoint_index - 1);
3155588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    size_t safepoint_position = safepoint->GetLifetimePosition();
3165588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray
3175588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    // Test that safepoints are ordered in the optimal way.
3185588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    DCHECK(safepoint_index == safepoints_.Size()
3195588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray           || safepoints_.Get(safepoint_index)->GetLifetimePosition() < safepoint_position);
3205588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray
3215588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    if (safepoint_position == current->GetStart()) {
3225588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      // The safepoint is for this instruction, so the location of the instruction
3235588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      // does not need to be saved.
3245588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      DCHECK_EQ(safepoint_index, safepoints_.Size());
3255588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      DCHECK_EQ(safepoint, instruction);
3265588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      continue;
3275588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    } else if (current->IsDeadAt(safepoint_position)) {
3285588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      break;
3295588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    } else if (!current->Covers(safepoint_position)) {
3305588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      // Hole in the interval.
3315588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      continue;
3325588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    }
3335588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    current->AddSafepoint(safepoint);
3345588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray  }
3353fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  current->ResetSearchCache();
3365588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray
3373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Some instructions define their output in fixed register/stack slot. We need
3383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // to ensure we know these locations before doing register allocation. For a
3393946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // given register, we create an interval that covers these locations. The register
3403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // will be unavailable at these locations when trying to allocate one for an
3413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // interval.
3423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  //
3433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // The backwards walking ensures the ranges are ordered on increasing start positions.
3443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  Location output = locations->Out();
345d0d4852847432368b090c184d6639e573538dccfCalin Juravle  if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
346d0d4852847432368b090c184d6639e573538dccfCalin Juravle    Location first = locations->InAt(0);
347d0d4852847432368b090c184d6639e573538dccfCalin Juravle    if (first.IsRegister() || first.IsFpuRegister()) {
348d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetFrom(position + 1);
349d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetRegister(first.reg());
350840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (first.IsPair()) {
351840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      current->SetFrom(position + 1);
352840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      current->SetRegister(first.low());
353840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = current->GetHighInterval();
354840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetRegister(first.high());
355840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetFrom(position + 1);
356d0d4852847432368b090c184d6639e573538dccfCalin Juravle    }
357d0d4852847432368b090c184d6639e573538dccfCalin Juravle  } else if (output.IsRegister() || output.IsFpuRegister()) {
3583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Shift the interval's start by one to account for the blocked register.
3593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetFrom(position + 1);
36056b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray    current->SetRegister(output.reg());
361102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    BlockRegister(output, position, position + 1);
362840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (output.IsPair()) {
363840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->SetFrom(position + 1);
364840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->SetRegister(output.low());
365840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    LiveInterval* high = current->GetHighInterval();
366840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    high->SetRegister(output.high());
367840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    high->SetFrom(position + 1);
368840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    BlockRegister(output.ToLow(), position, position + 1);
369840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    BlockRegister(output.ToHigh(), position, position + 1);
3703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
3713946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetSpillSlot(output.GetStackIndex());
372840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
373840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(output.IsUnallocated() || output.IsConstant());
3743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // If needed, add interval to the list of unhandled intervals.
3773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasSpillSlot() || instruction->IsConstant()) {
378c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    // Split just before first register use.
3793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    size_t first_register_use = current->FirstRegisterUse();
3803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (first_register_use != kNoLifetime) {
3818cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray      LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1);
382b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      // Don't add directly to `unhandled`, it needs to be sorted and the start
3833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // of this new interval might be after intervals already in the list.
3843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      AddSorted(&unhandled, split);
3853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
3863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // Nothing to do, we won't allocate a register for this value.
3873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
3883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
389b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // Don't add directly to `unhandled`, temp or safepoint intervals
390b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // for this instruction may have been added, and those can be
391b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // processed first.
392b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    AddSorted(&unhandled, current);
3933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
394a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
395a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
39631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayclass AllRangesIterator : public ValueObject {
39731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray public:
39831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  explicit AllRangesIterator(LiveInterval* interval)
39931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      : current_interval_(interval),
40031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_(interval->GetFirstRange()) {}
40131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
40231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  bool Done() const { return current_interval_ == nullptr; }
40331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* CurrentRange() const { return current_range_; }
40431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* CurrentInterval() const { return current_interval_; }
40531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
40631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  void Advance() {
40731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    current_range_ = current_range_->GetNext();
40831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    if (current_range_ == nullptr) {
40931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      current_interval_ = current_interval_->GetNextSibling();
41031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current_interval_ != nullptr) {
41131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_ = current_interval_->GetFirstRange();
41231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
41331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
41431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
41531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
41631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray private:
41731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* current_interval_;
41831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* current_range_;
41931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
42031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
42131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray};
42231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
42386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
42486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // To simplify unit testing, we eagerly create the array of intervals, and
42586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // call the helper method.
42686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  GrowableArray<LiveInterval*> intervals(allocator_, 0);
42786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
42886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
42986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
43086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      intervals.Add(instruction->GetLiveInterval());
43186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
43286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
43386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
434102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  if (processing_core_registers_) {
435102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
436102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      LiveInterval* fixed = physical_core_register_intervals_.Get(i);
437102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      if (fixed != nullptr) {
438102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        intervals.Add(fixed);
439102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      }
440102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
441102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  } else {
442102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
443102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
444102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      if (fixed != nullptr) {
445102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        intervals.Add(fixed);
446102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      }
44786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
44886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
44986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
4503946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0, e = temp_intervals_.Size(); i < e; ++i) {
4513946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* temp = temp_intervals_.Get(i);
4523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, temp)) {
4533946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      intervals.Add(temp);
4543946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
4553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
4563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
457776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  return ValidateIntervals(intervals, GetNumberOfSpillSlots(), reserved_out_slots_, *codegen_,
4583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                           allocator_, processing_core_registers_, log_fatal_on_failure);
45986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
46086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
46131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraybool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
46231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray                                          size_t number_of_spill_slots,
4633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                                          size_t number_of_out_slots,
464a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          const CodeGenerator& codegen,
465a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          ArenaAllocator* allocator,
466a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool processing_core_registers,
467a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool log_fatal_on_failure) {
468a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t number_of_registers = processing_core_registers
469a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      ? codegen.GetNumberOfCoreRegisters()
470a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : codegen.GetNumberOfFloatingPointRegisters();
47131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  GrowableArray<ArenaBitVector*> liveness_of_values(
47231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      allocator, number_of_registers + number_of_spill_slots);
473a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
474a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Allocate a bit vector per register. A live interval that has a register
475a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // allocated will populate the associated bit vector based on its live ranges.
47631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
47731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true));
478a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
479a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
48031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0, e = intervals.Size(); i < e; ++i) {
48131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) {
48231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      LiveInterval* current = it.CurrentInterval();
48386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* defined_by = current->GetParent()->GetDefinedBy();
48486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->GetParent()->HasSpillSlot()
48576b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray           // Parameters and current method have their own stack slot.
48676b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray           && !(defined_by != nullptr && (defined_by->IsParameterValue()
48776b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray                                          || defined_by->IsCurrentMethod()))) {
4883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers
4893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            + current->GetParent()->GetSpillSlot() / kVRegSize
4903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            - number_of_out_slots);
49131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
49231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_spill_slot->IsBitSet(j)) {
49331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            if (log_fatal_on_failure) {
49431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              std::ostringstream message;
49531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              message << "Spill slot conflict at " << j;
49631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              LOG(FATAL) << message.str();
49731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            } else {
49831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              return false;
49931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            }
50031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          } else {
50131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_spill_slot->SetBit(j);
50231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          }
50331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        }
504a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
50531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
50631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current->HasRegister()) {
50731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister());
50831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
50931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_register->IsBitSet(j)) {
510829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray            if (current->IsUsingInputRegister() && current->CanUseInputRegister()) {
511829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray              continue;
512829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray            }
513a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            if (log_fatal_on_failure) {
514a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              std::ostringstream message;
5153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "Register conflict at " << j << " ";
5163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              if (defined_by != nullptr) {
5173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                message << "(" << defined_by->DebugName() << ")";
5183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              }
5193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "for ";
520a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              if (processing_core_registers) {
521a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpCoreRegister(message, current->GetRegister());
522a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              } else {
523a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpFloatingPointRegister(message, current->GetRegister());
524a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              }
525a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              LOG(FATAL) << message.str();
526a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            } else {
527a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              return false;
528a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            }
529a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          } else {
53031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_register->SetBit(j);
531a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          }
532a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        }
53331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
53431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
535a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
536a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
537a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
538a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
53986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
540a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  interval->Dump(stream);
541a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << ": ";
542a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (interval->HasRegister()) {
543102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (interval->IsFloatingPoint()) {
54486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
545102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
546102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      codegen_->DumpCoreRegister(stream, interval->GetRegister());
547a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
548a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
549a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    stream << "spilled";
550a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
551a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << std::endl;
552a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
553a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
554296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yangvoid RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
555296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "inactive: " << std::endl;
556296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < inactive_.Size(); i ++) {
557296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, inactive_.Get(i));
558296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
559296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "active: " << std::endl;
560296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < active_.Size(); i ++) {
561296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, active_.Get(i));
562296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
563296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "unhandled: " << std::endl;
564296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  auto unhandled = (unhandled_ != nullptr) ?
565296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      unhandled_ : &unhandled_core_intervals_;
566296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < unhandled->Size(); i ++) {
567296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, unhandled->Get(i));
568296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
569296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "handled: " << std::endl;
570296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < handled_.Size(); i ++) {
571296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, handled_.Get(i));
572296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
573296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang}
574296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
575a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// By the book implementation of a linear scan register allocator.
576a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::LinearScan() {
5773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  while (!unhandled_->IsEmpty()) {
578a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (1) Remove interval with the lowest start position from unhandled.
5793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = unhandled_->Pop();
5803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    DCHECK(!current->IsFixed() && !current->HasSpillSlot());
581c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart());
582840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsLowInterval() || unhandled_->Peek()->IsHighInterval());
58387d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray
584a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t position = current->GetStart();
585a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
586296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Remember the inactive_ size here since the ones moved to inactive_ from
587296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // active_ below shouldn't need to be re-checked.
588296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    size_t inactive_intervals_to_handle = inactive_.Size();
589296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
590a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (2) Remove currently active intervals that are dead at this position.
591a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move active intervals that have a lifetime hole at this position
592a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     to inactive.
593a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0; i < active_.Size(); ++i) {
594a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = active_.Get(i);
595a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
596a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
597a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
598a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
599a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (!interval->Covers(position)) {
600a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
601a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
602a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Add(interval);
603a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
604a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
605a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
606a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (3) Remove currently inactive intervals that are dead at this position.
607a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move inactive intervals that cover this position to active.
608296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    for (size_t i = 0; i < inactive_intervals_to_handle; ++i) {
609a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = inactive_.Get(i);
610296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK(interval->GetStart() < position || interval->IsFixed());
611a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
612a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
613a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
614296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        --inactive_intervals_to_handle;
615a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
616a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (interval->Covers(position)) {
617a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
618a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
619296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        --inactive_intervals_to_handle;
620a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Add(interval);
621a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
622a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
623a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
624acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    if (current->IsSlowPathSafepoint()) {
625acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Synthesized interval to record the maximum number of live registers
626acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // at safepoints. No need to allocate a register for it.
627f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      if (processing_core_registers_) {
628f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_core_registers_ =
629f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell          std::max(maximum_number_of_live_core_registers_, active_.Size());
630f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      } else {
631f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_fp_registers_ =
632f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell          std::max(maximum_number_of_live_fp_registers_, active_.Size());
633f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      }
634acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() > current->GetStart());
635acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      continue;
636acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    }
637acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray
638840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
639840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(!current->HasRegister());
640840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // Allocating the low part was unsucessful. The splitted interval for the high part
641840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // will be handled next (it is in the `unhandled_` list).
642840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
643840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
644840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
645a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (4) Try to find an available register.
646a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    bool success = TryAllocateFreeReg(current);
647a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
648a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (5) If no register could be found, we need to spill.
649a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (!success) {
650a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      success = AllocateBlockedReg(current);
651a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
652a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
653a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (6) If the interval had a register allocated, add it to the list of active
654a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     intervals.
655a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (success) {
656988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      codegen_->AddAllocatedRegister(processing_core_registers_
657988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          ? Location::RegisterLocation(current->GetRegister())
658988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          : Location::FpuRegisterLocation(current->GetRegister()));
659a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      active_.Add(current);
660840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
661840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
662840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
663a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
664a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
665a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
666a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
667829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffraystatic void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) {
668829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  DCHECK(!interval->IsHighInterval());
669829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // Note that the same instruction may occur multiple times in the input list,
670829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // so `free_until` may have changed already.
6713fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  // Since `position` is not the current scan position, we need to use CoversSlow.
672829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  if (interval->IsDeadAt(position)) {
673829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // Set the register to be free. Note that inactive intervals might later
674829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // update this.
675829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    free_until[interval->GetRegister()] = kMaxLifetimePosition;
676829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (interval->HasHighInterval()) {
677829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      DCHECK(interval->GetHighInterval()->IsDeadAt(position));
678829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition;
679829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
6803fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  } else if (!interval->CoversSlow(position)) {
681829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // The interval becomes inactive at `defined_by`. We make its register
682829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // available only until the next use strictly after `defined_by`.
683829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    free_until[interval->GetRegister()] = interval->FirstUseAfter(position);
684829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (interval->HasHighInterval()) {
6853fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil      DCHECK(!interval->GetHighInterval()->CoversSlow(position));
686829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()];
687829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
688829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  }
689829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray}
690829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray
691a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find a free register. If multiple are found, pick the register that
692a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// is free the longest.
693a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
694a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* free_until = registers_array_;
695a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
696a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers to be free.
697a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
698a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    free_until[i] = kMaxLifetimePosition;
699a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
700a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
701296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  // For each active interval, set its register to not free.
702296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
703296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    LiveInterval* interval = active_.Get(i);
704296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(interval->HasRegister());
705296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    free_until[interval->GetRegister()] = 0;
706296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
707296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
708829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // An interval that starts an instruction (that is, it is not split), may
709829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // re-use the registers used by the inputs of that instruciton, based on the
710829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // location summary.
711829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  HInstruction* defined_by = current->GetDefinedBy();
712829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  if (defined_by != nullptr && !current->IsSplit()) {
713829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    LocationSummary* locations = defined_by->GetLocations();
714829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) {
715829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      for (HInputIterator it(defined_by); !it.Done(); it.Advance()) {
716829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // Take the last interval of the input. It is the location of that interval
717829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // that will be used at `defined_by`.
718829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        LiveInterval* interval = it.Current()->GetLiveInterval()->GetLastSibling();
719829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // Note that interval may have not been processed yet.
720829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // TODO: Handle non-split intervals last in the work list.
721829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        if (interval->HasRegister() && interval->SameRegisterKind(*current)) {
722829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // The input must be live until the end of `defined_by`, to comply to
723829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // the linear scan algorithm. So we use `defined_by`'s end lifetime
724829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // position to check whether the input is dead or is inactive after
725829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // `defined_by`.
7263fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil          DCHECK(interval->CoversSlow(defined_by->GetLifetimePosition()));
727829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          size_t position = defined_by->GetLifetimePosition() + 1;
728829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          FreeIfNotCoverAt(interval, position, free_until);
729829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        }
730829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      }
731829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
732829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  }
733829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray
734a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, set its register to be free until
735a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // the next intersection with `current`.
736a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
737a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
738296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
739296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
740296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
741296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
742296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
743296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
744296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
745296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
746296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
747296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
748296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
749a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
750296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (free_until[inactive->GetRegister()] == 0) {
751296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Already used by some active interval. No need to intersect.
752296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
753296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
754a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
755a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (next_intersection != kNoLifetime) {
756aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray      free_until[inactive->GetRegister()] =
757aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray          std::min(free_until[inactive->GetRegister()], next_intersection);
758a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
759a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
760a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7616c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
7623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasRegister()) {
7633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Some instructions have a fixed register output.
7643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    reg = current->GetRegister();
765840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (free_until[reg] == 0) {
766840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(current->IsHighInterval());
767840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // AllocateBlockedReg will spill the holder of the register.
768840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      return false;
769840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
7703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
771840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
772fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray    int hint = current->FindFirstRegisterHint(free_until, liveness_);
77301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    if (hint != kNoRegister) {
77401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      DCHECK(!IsBlocked(hint));
77501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      reg = hint;
776840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (current->IsLowInterval()) {
7776c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      reg = FindAvailableRegisterPair(free_until, current->GetStart());
77801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    } else {
7798826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      reg = FindAvailableRegister(free_until, current);
780a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
781a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
782a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7836c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
784a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // If we could not find a register, we need to spill.
785840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (free_until[reg] == 0) {
786840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return false;
787840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
788840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
789234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (current->IsLowInterval()) {
790234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    // If the high register of this interval is not available, we need to spill.
791234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    int high_reg = current->GetHighInterval()->GetRegister();
792234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (high_reg == kNoRegister) {
793234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      high_reg = GetHighForLowRegister(reg);
794234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    }
795234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (free_until[high_reg] == 0) {
796234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      return false;
797234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    }
798a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
799a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
800a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  current->SetRegister(reg);
801a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (!current->IsDeadAt(free_until[reg])) {
802a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the register is only available for a subset of live ranges
803a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // covered by `current`, split `current` at the position where
804a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // the register is not available anymore.
805a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* split = Split(current, free_until[reg]);
806a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(split != nullptr);
8073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    AddSorted(unhandled_, split);
808a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
809a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
810a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
811a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
812a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::IsBlocked(int reg) const {
813102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return processing_core_registers_
814102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? blocked_core_registers_[reg]
815102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : blocked_fp_registers_[reg];
816a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
817a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
8186c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffrayint RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const {
8196c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
820840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Pick the register pair that is used the last.
821840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
822840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(i)) continue;
823840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (!IsLowRegister(i)) continue;
824840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int high_register = GetHighForLowRegister(i);
825840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(high_register)) continue;
826840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int existing_high_register = GetHighForLowRegister(reg);
8276c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if ((reg == kNoRegister) || (next_use[i] >= next_use[reg]
828840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                        && next_use[high_register] >= next_use[existing_high_register])) {
829840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
830840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition
831840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          && next_use[high_register] == kMaxLifetimePosition) {
832840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        break;
833840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
8346c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) {
8356c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If one of the current register is known to be unavailable, just unconditionally
8366c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // try a new one.
8376c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      reg = i;
838840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
839840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
840840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
841840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
842840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
8438826f67ad53099021f6442364348fa66729288d7Nicolas Geoffraybool RegisterAllocator::IsCallerSaveRegister(int reg) const {
8448826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  return processing_core_registers_
8458826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      ? !codegen_->IsCoreCalleeSaveRegister(reg)
8468826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      : !codegen_->IsFloatingPointCalleeSaveRegister(reg);
8478826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray}
8488826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
8498826f67ad53099021f6442364348fa66729288d7Nicolas Geoffrayint RegisterAllocator::FindAvailableRegister(size_t* next_use, LiveInterval* current) const {
8508826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  // We special case intervals that do not span a safepoint to try to find a caller-save
8518826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  // register if one is available. We iterate from 0 to the number of registers,
8528826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  // so if there are caller-save registers available at the end, we continue the iteration.
8538826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  bool prefers_caller_save = !current->HasWillCallSafepoint();
8546c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
855840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
8568826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (IsBlocked(i)) {
8578826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      // Register cannot be used. Continue.
8588826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      continue;
8598826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    }
8608826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
8618826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    // Best case: we found a register fully available.
8628826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (next_use[i] == kMaxLifetimePosition) {
8638826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      if (prefers_caller_save && !IsCallerSaveRegister(i)) {
8648826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // We can get shorter encodings on some platforms by using
8658826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // small register numbers. So only update the candidate if the previous
8668826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // one was not available for the whole method.
8678826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        if (reg == kNoRegister || next_use[reg] != kMaxLifetimePosition) {
8688826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray          reg = i;
8698826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        }
8708826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // Continue the iteration in the hope of finding a caller save register.
8718826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        continue;
8728826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      } else {
8738826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        reg = i;
8748826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // We know the register is good enough. Return it.
8758826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        break;
8768826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      }
8778826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    }
8788826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
8798826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    // If we had no register before, take this one as a reference.
8808826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (reg == kNoRegister) {
881840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
8828826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      continue;
8838826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    }
8848826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
8858826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    // Pick the register that is used the last.
8868826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (next_use[i] > next_use[reg]) {
8878826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      reg = i;
8888826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      continue;
889840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
890840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
891840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
892840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
893840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
894234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraybool RegisterAllocator::TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
895234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                                 size_t first_register_use,
896234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                                 size_t* next_use) {
8976c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
8986c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    LiveInterval* active = active_.Get(i);
8996c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    DCHECK(active->HasRegister());
900234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (active->IsFixed()) continue;
901234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (active->IsHighInterval()) continue;
902234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (first_register_use > next_use[active->GetRegister()]) continue;
903234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
9046c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // Split the first interval found.
905234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (!active->IsLowInterval() || IsLowOfUnalignedPairInterval(active)) {
9066c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* split = Split(active, position);
9076c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      active_.DeleteAt(i);
9086c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      if (split != active) {
9096c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        handled_.Add(active);
9106c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      }
9116c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
9126c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      return true;
9136c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
9146c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  }
9156c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  return false;
9166c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray}
9176c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
9185b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffraybool RegisterAllocator::PotentiallyRemoveOtherHalf(LiveInterval* interval,
9195b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray                                                   GrowableArray<LiveInterval*>* intervals,
9205b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray                                                   size_t index) {
9215b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  if (interval->IsLowInterval()) {
9225b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    DCHECK_EQ(intervals->Get(index), interval->GetHighInterval());
9235b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    intervals->DeleteAt(index);
9245b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    return true;
9255b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  } else if (interval->IsHighInterval()) {
9265b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    DCHECK_GT(index, 0u);
9275b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    DCHECK_EQ(intervals->Get(index - 1), interval->GetLowInterval());
9285b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    intervals->DeleteAt(index - 1);
9295b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    return true;
9305b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  } else {
9315b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    return false;
9325b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  }
9335b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray}
9345b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray
935a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval
936a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register
937a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead.
938a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
939a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t first_register_use = current->FirstRegisterUse();
940412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  if (first_register_use == kNoLifetime) {
94131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
942a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
943a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
944a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9451ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray  // We use the first use to compare with other intervals. If this interval
9461ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray  // is used after any active intervals, we will spill this interval.
9471ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray  size_t first_use = current->FirstUseAfter(current->GetStart());
9481ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray
949a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers as not being used.
950a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* next_use = registers_array_;
951a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
952a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    next_use[i] = kMaxLifetimePosition;
953a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
954a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
955a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each active interval, find the next use of its register after the
956a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
957a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
958a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* active = active_.Get(i);
959a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(active->HasRegister());
96086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (active->IsFixed()) {
96186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      next_use[active->GetRegister()] = current->GetStart();
96286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
9631ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray      size_t use = active->FirstUseAfter(current->GetStart());
96486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (use != kNoLifetime) {
96586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[active->GetRegister()] = use;
96686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
967a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
968a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
969a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
970a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, find the next use of its register after the
971a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
972a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
973a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
974296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
975296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
976296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
977296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
978296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
979296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
980296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
981296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
982296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
983296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
984a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
98586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
98686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_intersection != kNoLifetime) {
98786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (inactive->IsFixed()) {
98886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[inactive->GetRegister()] =
98986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            std::min(next_intersection, next_use[inactive->GetRegister()]);
99086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else {
9911ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray        size_t use = inactive->FirstUseAfter(current->GetStart());
99286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (use != kNoLifetime) {
99386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
99486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
99586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
996a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
997a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
998a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9996c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
10006c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  bool should_spill = false;
1001840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (current->HasRegister()) {
1002840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(current->IsHighInterval());
1003840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = current->GetRegister();
10046c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // When allocating the low part, we made sure the high register was available.
10051ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray    DCHECK_LT(first_use, next_use[reg]);
1006840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (current->IsLowInterval()) {
1007234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    reg = FindAvailableRegisterPair(next_use, first_register_use);
10086c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // We should spill if both registers are not available.
10091ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray    should_spill = (first_use >= next_use[reg])
10101ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray      || (first_use >= next_use[GetHighForLowRegister(reg)]);
1011840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
1012840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
10138826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    reg = FindAvailableRegister(next_use, current);
10141ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray    should_spill = (first_use >= next_use[reg]);
1015a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1016a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10176c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
10186c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  if (should_spill) {
1019840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
1020234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    bool is_allocation_at_use_site = (current->GetStart() >= (first_register_use - 1));
10216c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (current->IsLowInterval()
10226c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        && is_allocation_at_use_site
1023234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        && TrySplitNonPairOrUnalignedPairIntervalAt(current->GetStart(),
1024234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                    first_register_use,
1025234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                    next_use)) {
10266c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If we're allocating a register for `current` because the instruction at
10276c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // that position requires it, but we think we should spill, then there are
1028234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // non-pair intervals or unaligned pair intervals blocking the allocation.
1029234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // We split the first interval found, and put ourselves first in the
1030234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // `unhandled_` list.
10316c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* existing = unhandled_->Peek();
10326c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK(existing->IsHighInterval());
10336c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK_EQ(existing->GetLowInterval(), current);
10346c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      unhandled_->Add(current);
10356c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else {
10366c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If the first use of that instruction is after the last use of the found
10376c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // register, we split this interval just before its first register use.
10386c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AllocateSpillSlotFor(current);
10398cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray      LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1);
1040234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      if (current == split) {
1041234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DumpInterval(std::cerr, current);
1042234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DumpAllIntervals(std::cerr);
1043234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // This situation has the potential to infinite loop, so we make it a non-debug CHECK.
10441ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray        HInstruction* at = liveness_.GetInstructionFromPosition(first_register_use / 2);
1045234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        CHECK(false) << "There is not enough registers available for "
1046234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << split->GetParent()->GetDefinedBy()->DebugName() << " "
1047234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << split->GetParent()->GetDefinedBy()->GetId()
10481ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray          << " at " << first_register_use - 1 << " "
10491ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray          << (at == nullptr ? "" : at->DebugName());
1050234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      }
10516c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
10526c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
1053a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
1054a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
1055a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Use this register and spill the active and inactives interval that
1056a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // have that register.
1057a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    current->SetRegister(reg);
1058a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1059a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0, e = active_.Size(); i < e; ++i) {
1060a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* active = active_.Get(i);
1061a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (active->GetRegister() == reg) {
106286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(!active->IsFixed());
1063a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        LiveInterval* split = Split(active, current->GetStart());
1064dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        if (split != active) {
1065dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray          handled_.Add(active);
1066dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        }
10675b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray        active_.DeleteAt(i);
10685b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray        PotentiallyRemoveOtherHalf(active, &active_, i);
10693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        AddSorted(unhandled_, split);
1070a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        break;
1071a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
1072a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1073a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10745b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    for (size_t i = 0; i < inactive_.Size(); ++i) {
1075a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* inactive = inactive_.Get(i);
1076a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (inactive->GetRegister() == reg) {
1077296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        if (!current->IsSplit() && !inactive->IsFixed()) {
1078296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Neither current nor inactive are fixed.
1079296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Thanks to SSA, a non-split interval starting in a hole of an
1080296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // inactive interval should never intersect with that inactive interval.
1081296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Only if it's not fixed though, because fixed intervals don't come from SSA.
1082296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
1083296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          continue;
1084296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        }
108586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t next_intersection = inactive->FirstIntersectionWith(current);
108686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (next_intersection != kNoLifetime) {
108786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          if (inactive->IsFixed()) {
108886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            LiveInterval* split = Split(current, next_intersection);
1089dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            DCHECK_NE(split, current);
10903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
109186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          } else {
1092dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // Split at the start of `current`, which will lead to splitting
1093dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // at the end of the lifetime hole of `inactive`.
1094dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            LiveInterval* split = Split(inactive, current->GetStart());
1095dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // If it's inactive, it must start before the current interval.
1096dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            DCHECK_NE(split, inactive);
109786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            inactive_.DeleteAt(i);
10985b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            if (PotentiallyRemoveOtherHalf(inactive, &inactive_, i) && inactive->IsHighInterval()) {
10995b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray              // We have removed an entry prior to `inactive`. So we need to decrement.
11005b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray              --i;
11015b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            }
11025b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            // Decrement because we have removed `inactive` from the list.
1103296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang            --i;
110486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            handled_.Add(inactive);
11053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
110686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          }
110786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
1108a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
1109a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1110a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1111a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return true;
1112a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1113a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1114a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) {
1116c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray  DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
111786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t insert_at = 0;
11183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = array->Size(); i > 0; --i) {
11193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = array->Get(i - 1);
11206c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // High intervals must be processed right after their low equivalent.
11216c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (current->StartsAfter(interval) && !current->IsHighInterval()) {
112286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      insert_at = i;
1123a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      break;
1124acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
1125acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Ensure the slow path interval is the last to be processed at its location: we want the
1126acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // interval to know all live registers at this location.
1127acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      DCHECK(i == 1 || array->Get(i - 2)->StartsAfter(current));
1128acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      insert_at = i;
1129acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      break;
1130a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1131a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1132840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
11333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  array->InsertAt(insert_at, interval);
1134840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Insert the high interval before the low, to ensure the low is processed before.
1135840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->HasHighInterval()) {
1136840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    array->InsertAt(insert_at, interval->GetHighInterval());
1137840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (interval->HasLowInterval()) {
1138840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    array->InsertAt(insert_at + 1, interval->GetLowInterval());
1139840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1140a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1141a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11428cbab3c4de3328b576454ce702d7748f56c44346Nicolas GeoffrayLiveInterval* RegisterAllocator::SplitBetween(LiveInterval* interval, size_t from, size_t to) {
1143fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  HBasicBlock* block_from = liveness_.GetBlockFromPosition(from / 2);
1144fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  HBasicBlock* block_to = liveness_.GetBlockFromPosition(to / 2);
11458cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  DCHECK(block_from != nullptr);
11468cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  DCHECK(block_to != nullptr);
11478cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
11488cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // Both locations are in the same block. We split at the given location.
11498cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  if (block_from == block_to) {
11508cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    return Split(interval, to);
11518cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  }
11528cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
1153fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  /*
1154fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * Non-linear control flow will force moves at every branch instruction to the new location.
1155fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * To avoid having all branches doing the moves, we find the next non-linear position and
1156fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * split the interval at this position. Take the following example (block number is the linear
1157fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * order position):
1158fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *
1159fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *     B1
1160fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *    /  \
1161fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *   B2  B3
1162fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *    \  /
1163fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *     B4
1164fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *
1165fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * B2 needs to split an interval, whose next use is in B4. If we were to split at the
1166fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * beginning of B4, B3 would need to do a move between B3 and B4 to ensure the interval
1167fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * is now in the correct location. It makes performance worst if the interval is spilled
1168fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * and both B2 and B3 need to reload it before entering B4.
1169fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *
1170fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * By splitting at B3, we give a chance to the register allocator to allocate the
1171fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * interval to the same register as in B1, and therefore avoid doing any
1172fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * moves in B3.
1173fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   */
1174fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  if (block_from->GetDominator() != nullptr) {
1175fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray    const GrowableArray<HBasicBlock*>& dominated = block_from->GetDominator()->GetDominatedBlocks();
1176fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray    for (size_t i = 0; i < dominated.Size(); ++i) {
1177fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray      size_t position = dominated.Get(i)->GetLifetimeStart();
1178fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray      if ((position > from) && (block_to->GetLifetimeStart() > position)) {
1179fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        // Even if we found a better block, we continue iterating in case
1180fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        // a dominated block is closer.
1181fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        // Note that dominated blocks are not sorted in liveness order.
1182fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        block_to = dominated.Get(i);
1183fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        DCHECK_NE(block_to, block_from);
1184fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray      }
1185fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray    }
1186fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  }
1187fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray
11888cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // If `to` is in a loop, find the outermost loop header which does not contain `from`.
11898cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  for (HLoopInformationOutwardIterator it(*block_to); !it.Done(); it.Advance()) {
11908cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    HBasicBlock* header = it.Current()->GetHeader();
11918cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    if (block_from->GetLifetimeStart() >= header->GetLifetimeStart()) {
11928cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray      break;
11938cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    }
11948cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    block_to = header;
11958cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  }
11968cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
11978cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // Split at the start of the found block, to piggy back on existing moves
11988cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // due to resolution if non-linear control flow (see `ConnectSplitSiblings`).
11998cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  return Split(interval, block_to->GetLifetimeStart());
12008cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray}
12018cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
1202a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
1203840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  DCHECK_GE(position, interval->GetStart());
1204a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(!interval->IsDeadAt(position));
1205a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (position == interval->GetStart()) {
1206a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Spill slot will be allocated when handling `interval` again.
1207a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    interval->ClearRegister();
1208840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1209840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetHighInterval()->ClearRegister();
1210840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1211840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetLowInterval()->ClearRegister();
1212840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1213a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return interval;
1214a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
1215a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* new_interval = interval->SplitAt(position);
1216840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1217840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
1218840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetHighInterval(high);
1219840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetLowInterval(new_interval);
1220840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1221840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
1222840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetLowInterval(low);
1223840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      low->SetHighInterval(new_interval);
1224840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1225a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return new_interval;
1226a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1227a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1228a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
122931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
1230840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->IsHighInterval()) {
1231840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    // The low interval will contain the spill slot.
1232840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return;
1233840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1234840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
123531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* parent = interval->GetParent();
123631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
123731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // An instruction gets a spill slot for its entire lifetime. If the parent
123831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // of this interval already has a spill slot, there is nothing to do.
123931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  if (parent->HasSpillSlot()) {
124031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    return;
124131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
124231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
124386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* defined_by = parent->GetDefinedBy();
124486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (defined_by->IsParameterValue()) {
124586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Parameters have their own stack slot.
124686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
124786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
124886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
124986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
125076b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray  if (defined_by->IsCurrentMethod()) {
125176b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray    parent->SetSpillSlot(0);
125276b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray    return;
125376b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray  }
125476b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray
125596f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  if (defined_by->IsConstant()) {
125696f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    // Constants don't need a spill slot.
125796f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    return;
125896f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  }
125996f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray
126031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* last_sibling = interval;
126131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  while (last_sibling->GetNextSibling() != nullptr) {
126231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    last_sibling = last_sibling->GetNextSibling();
126331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
126431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  size_t end = last_sibling->GetEnd();
126531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1266776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  GrowableArray<size_t>* spill_slots = nullptr;
1267776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  switch (interval->GetType()) {
1268776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimDouble:
1269776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &double_spill_slots_;
1270776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1271776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimLong:
1272776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &long_spill_slots_;
1273776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1274776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimFloat:
1275776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &float_spill_slots_;
1276776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1277776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimNot:
1278776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimInt:
1279776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimChar:
1280776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimByte:
1281776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimBoolean:
1282776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimShort:
1283776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &int_spill_slots_;
1284776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1285776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimVoid:
1286776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      LOG(FATAL) << "Unexpected type for interval " << interval->GetType();
1287776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  }
1288776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray
1289412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  // Find an available spill slot.
1290412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  size_t slot = 0;
1291776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  for (size_t e = spill_slots->Size(); slot < e; ++slot) {
1292776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (spill_slots->Get(slot) <= parent->GetStart()
1293776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        && (slot == (e - 1) || spill_slots->Get(slot + 1) <= parent->GetStart())) {
1294412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray      break;
1295412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    }
1296412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
1297412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
129801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  if (parent->NeedsTwoSpillSlots()) {
1299776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (slot == spill_slots->Size()) {
13003c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
1301776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
1302776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
1303776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    } else if (slot == spill_slots->Size() - 1) {
1304776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
1305776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
13063c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
1307776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
1308776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot + 1, end);
130931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
131031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  } else {
1311776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (slot == spill_slots->Size()) {
13123c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
1313776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
13143c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
1315776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
13163c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    }
131731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
131831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1319776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // Note that the exact spill slot location will be computed when we resolve,
1320776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // that is when we know the number of spill slots for each type.
1321776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  parent->SetSpillSlot(slot);
132286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
132386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
13242a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffraystatic bool IsValidDestination(Location destination) {
1325102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return destination.IsRegister()
13266c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || destination.IsRegisterPair()
1327102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsFpuRegister()
1328840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      || destination.IsFpuRegisterPair()
1329102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsStackSlot()
1330102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsDoubleStackSlot();
13312a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray}
13322a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray
1333234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddMove(HParallelMove* move,
1334234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location source,
1335234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location destination,
1336234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                HInstruction* instruction,
1337234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Primitive::Type type) const {
1338234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (type == Primitive::kPrimLong
1339234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && codegen_->ShouldSplitLongMoves()
1340234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // The parallel move resolver knows how to deal with long constants.
1341234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && !source.IsConstant()) {
13429021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source.ToLow(), destination.ToLow(), Primitive::kPrimInt, instruction);
13439021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source.ToHigh(), destination.ToHigh(), Primitive::kPrimInt, nullptr);
1344234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  } else {
13459021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source, destination, type, instruction);
1346234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  }
1347234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray}
1348234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1349234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* input,
1350234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                        HInstruction* user,
135186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
135286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
135386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
135486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1355476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  DCHECK(!user->IsPhi());
135686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1357740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  HInstruction* previous = user->GetPrevious();
135886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = nullptr;
135986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (previous == nullptr
1360476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain      || !previous->IsParallelMove()
13618e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray      || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
136286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
13638e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    move->SetLifetimePosition(user->GetLifetimePosition());
1364740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    user->GetBlock()->InsertInstructionBefore(move, user);
136586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
136686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
136786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
13688e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
1369234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, nullptr, input->GetType());
137086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
137186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
137246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionStart(size_t position) {
137346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 0;
137446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
137546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
137646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionEnd(size_t position) {
137746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 1;
137846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
137946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
138086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position,
1381740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                             HInstruction* instruction,
138286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location source,
138386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location destination) const {
13846c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
138586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
138686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
138786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
138886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
138946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  if (at == nullptr) {
139046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    if (IsInstructionStart(position)) {
139146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Block boundary, don't do anything the connection of split siblings will handle it.
139246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      return;
139346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    } else {
139446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Move must happen before the first instruction of the block.
139546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      at = liveness_.GetInstructionFromPosition((position + 1) / 2);
13965976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // Note that parallel moves may have already been inserted, so we explicitly
13975976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // ask for the first instruction of the block: `GetInstructionFromPosition` does
1398234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // not contain the `HParallelMove` instructions.
13995976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      at = at->GetBlock()->GetFirstInstruction();
1400234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1401234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      if (at->GetLifetimePosition() < position) {
1402234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // We may insert moves for split siblings and phi spills at the beginning of the block.
1403234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // Since this is a different lifetime position, we need to go to the next instruction.
1404234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DCHECK(at->IsParallelMove());
1405234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        at = at->GetNext();
1406234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      }
1407234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
14085976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      if (at->GetLifetimePosition() != position) {
14095976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_GT(at->GetLifetimePosition(), position);
141046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move = new (allocator_) HParallelMove(allocator_);
141146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move->SetLifetimePosition(position);
141246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        at->GetBlock()->InsertInstructionBefore(move, at);
14135976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      } else {
14145976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK(at->IsParallelMove());
14155976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        move = at->AsParallelMove();
141646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      }
141746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    }
141846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  } else if (IsInstructionEnd(position)) {
141986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen after the instruction.
142086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(!at->IsControlFlow());
142186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = at->GetNext()->AsParallelMove();
1422e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // This is a parallel move for connecting siblings in a same block. We need to
1423e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // differentiate it with moves for connecting blocks, and input moves.
14248e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    if (move == nullptr || move->GetLifetimePosition() > position) {
142586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
142686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
142786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
142886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
142986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
143086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen before the instruction.
143186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* previous = at->GetPrevious();
1432740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    if (previous == nullptr
1433740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || !previous->IsParallelMove()
1434740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || previous->GetLifetimePosition() != position) {
1435740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // If the previous is a parallel move, then its position must be lower
1436740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // than the given `position`: it was added just after the non-parallel
1437740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // move instruction that precedes `instruction`.
1438740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      DCHECK(previous == nullptr
1439740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || !previous->IsParallelMove()
1440740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || previous->GetLifetimePosition() < position);
144186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
144286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
144386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at);
144486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
144586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = previous->AsParallelMove();
144686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
144786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
144801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), position);
1449234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
145086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
145186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
145286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
1453740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                   HInstruction* instruction,
145486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location source,
145586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location destination) const {
14566c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
145786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
145886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
145986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK_EQ(block->GetSuccessors().Size(), 1u);
146086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* last = block->GetLastInstruction();
1461360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // We insert moves at exit for phi predecessors and connecting blocks.
1462360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // A block ending with an if cannot branch to a block with phis because
1463360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // we do not allow critical edges. It can also not connect
1464360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // a split interval between two blocks: the move has to happen in the successor.
1465360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  DCHECK(!last->IsIf());
146686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* previous = last->GetPrevious();
146786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
1468e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1469e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and output moves.
14705976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray  size_t position = last->GetLifetimePosition();
1471740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  if (previous == nullptr || !previous->IsParallelMove()
14725976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      || previous->AsParallelMove()->GetLifetimePosition() != position) {
147386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
14745976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    move->SetLifetimePosition(position);
147586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, last);
147686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
147786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
147886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1479234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
148086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
148186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
148286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
1483740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                    HInstruction* instruction,
148486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location source,
148586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location destination) const {
14866c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
148786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
148886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
148986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* first = block->GetFirstInstruction();
149086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = first->AsParallelMove();
1491234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  size_t position = block->GetLifetimeStart();
1492e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1493e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and input moves.
1494234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
149586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1496234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->SetLifetimePosition(position);
149786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, first);
149886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1499234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
150086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
150186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
150286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
150386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
150486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
15056c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
150686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
150786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1508476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  if (instruction->IsPhi()) {
1509740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
151086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
151186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
151286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1513e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  size_t position = instruction->GetLifetimePosition() + 1;
151486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = instruction->GetNext()->AsParallelMove();
1515e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for moving the output of an instruction. We need
1516e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // to differentiate with input moves, moves for connecting siblings in a
1517e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // and moves for connecting blocks.
1518e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
151986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1520e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(position);
152186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
152286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1523234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
152486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
152586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
152686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
152786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
152876b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray  if (current->HasSpillSlot()
152976b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      && current->HasRegister()
153076b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      // Currently, we spill unconditionnally the current method in the code generators.
153176b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      && !interval->GetDefinedBy()->IsCurrentMethod()) {
153286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // We spill eagerly, so move must be at definition.
153386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertMoveAfter(interval->GetDefinedBy(),
1534840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                    interval->ToLocation(),
153501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                    interval->NeedsTwoSpillSlots()
1536412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1537412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
153886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
153986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  UsePosition* use = current->GetFirstUse();
15404ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray  UsePosition* env_use = current->GetFirstEnvironmentUse();
154186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
154286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Walk over all siblings, updating locations of use positions, and
154386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // connecting them when they are adjacent.
154486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  do {
154501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
154686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
154786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Walk over all uses covered by this interval, and update the location
154886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // information.
1549d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray
1550d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    LiveRange* range = current->GetFirstRange();
1551d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    while (range != nullptr) {
1552579026039080252878106118645ed70706f4838eNicolas Geoffray      while (use != nullptr && use->GetPosition() < range->GetStart()) {
1553579026039080252878106118645ed70706f4838eNicolas Geoffray        DCHECK(use->IsSynthesized());
1554579026039080252878106118645ed70706f4838eNicolas Geoffray        use = use->GetNext();
1555579026039080252878106118645ed70706f4838eNicolas Geoffray      }
1556d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      while (use != nullptr && use->GetPosition() <= range->GetEnd()) {
15574ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray        DCHECK(!use->GetIsEnvironment());
15583fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil        DCHECK(current->CoversSlow(use->GetPosition()) || (use->GetPosition() == range->GetEnd()));
1559579026039080252878106118645ed70706f4838eNicolas Geoffray        if (!use->IsSynthesized()) {
1560579026039080252878106118645ed70706f4838eNicolas Geoffray          LocationSummary* locations = use->GetUser()->GetLocations();
1561579026039080252878106118645ed70706f4838eNicolas Geoffray          Location expected_location = locations->InAt(use->GetInputIndex());
1562579026039080252878106118645ed70706f4838eNicolas Geoffray          // The expected (actual) location may be invalid in case the input is unused. Currently
1563579026039080252878106118645ed70706f4838eNicolas Geoffray          // this only happens for intrinsics.
1564579026039080252878106118645ed70706f4838eNicolas Geoffray          if (expected_location.IsValid()) {
1565579026039080252878106118645ed70706f4838eNicolas Geoffray            if (expected_location.IsUnallocated()) {
1566579026039080252878106118645ed70706f4838eNicolas Geoffray              locations->SetInAt(use->GetInputIndex(), source);
1567579026039080252878106118645ed70706f4838eNicolas Geoffray            } else if (!expected_location.IsConstant()) {
1568579026039080252878106118645ed70706f4838eNicolas Geoffray              AddInputMoveFor(interval->GetDefinedBy(), use->GetUser(), source, expected_location);
1569579026039080252878106118645ed70706f4838eNicolas Geoffray            }
1570579026039080252878106118645ed70706f4838eNicolas Geoffray          } else {
1571579026039080252878106118645ed70706f4838eNicolas Geoffray            DCHECK(use->GetUser()->IsInvoke());
1572579026039080252878106118645ed70706f4838eNicolas Geoffray            DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
1573d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray          }
157486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
1575d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray        use = use->GetNext();
157686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
15774ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray
15784ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      // Walk over the environment uses, and update their locations.
15794ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      while (env_use != nullptr && env_use->GetPosition() < range->GetStart()) {
15804ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray        env_use = env_use->GetNext();
15814ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      }
15824ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray
15834ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      while (env_use != nullptr && env_use->GetPosition() <= range->GetEnd()) {
15840a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray        DCHECK(current->CoversSlow(env_use->GetPosition())
15850a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray               || (env_use->GetPosition() == range->GetEnd()));
15860a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray        HEnvironment* environment = env_use->GetUser()->GetEnvironment();
15870a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray        environment->SetLocationAt(env_use->GetInputIndex(), source);
15884ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray        env_use = env_use->GetNext();
15894ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      }
15904ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray
1591d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      range = range->GetNext();
159286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
159386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
159486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // If the next interval starts just after this one, and has a register,
159586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // insert a move.
159686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* next_sibling = current->GetNextSibling();
159786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_sibling != nullptr
159886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && next_sibling->HasRegister()
159986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && current->GetEnd() == next_sibling->GetStart()) {
160001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      Location destination = next_sibling->ToLocation();
1601740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
160286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
16033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
160443af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray    for (SafepointPosition* safepoint_position = current->GetFirstSafepoint();
160543af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray         safepoint_position != nullptr;
160643af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray         safepoint_position = safepoint_position->GetNext()) {
16073fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil      DCHECK(current->CoversSlow(safepoint_position->GetPosition()));
16083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
16095588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      LocationSummary* locations = safepoint_position->GetLocations();
16103bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
16113946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
16123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
16133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
16143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      switch (source.GetKind()) {
16153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kRegister: {
16163bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray          locations->AddLiveRegister(source);
1617988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) {
1618988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray            DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1619988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_core_registers_ +
1620988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_fp_registers_);
1621988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          }
16223946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          if (current->GetType() == Primitive::kPrimNot) {
162356b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray            locations->SetRegisterBit(source.reg());
16243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          }
16253946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
16263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
1627102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        case Location::kFpuRegister: {
1628102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          locations->AddLiveRegister(source);
1629102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          break;
1630102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        }
16316c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
16326c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        case Location::kRegisterPair:
1633840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        case Location::kFpuRegisterPair: {
1634840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToLow());
1635840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToHigh());
1636840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          break;
1637840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
16383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kStackSlot:  // Fall-through
16393946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kDoubleStackSlot:  // Fall-through
16403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kConstant: {
16413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          // Nothing to do.
16423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
16433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
16443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        default: {
16453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          LOG(FATAL) << "Unexpected location for object";
16463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
16473946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
16483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
164986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = next_sibling;
165086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } while (current != nullptr);
1651d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray
1652579026039080252878106118645ed70706f4838eNicolas Geoffray  if (kIsDebugBuild) {
1653579026039080252878106118645ed70706f4838eNicolas Geoffray    // Following uses can only be synthesized uses.
1654579026039080252878106118645ed70706f4838eNicolas Geoffray    while (use != nullptr) {
1655579026039080252878106118645ed70706f4838eNicolas Geoffray      DCHECK(use->IsSynthesized());
1656579026039080252878106118645ed70706f4838eNicolas Geoffray      use = use->GetNext();
1657579026039080252878106118645ed70706f4838eNicolas Geoffray    }
1658579026039080252878106118645ed70706f4838eNicolas Geoffray  }
165986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
166086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
166186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
166286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* from,
166386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* to) const {
166486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval->GetNextSibling() == nullptr) {
166586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Nothing to connect. The whole range was allocated to the same location.
166686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
166786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
166886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1669241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  // Find the intervals that cover `from` and `to`.
1670241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  LiveInterval* destination = interval->GetSiblingAt(to->GetLifetimeStart());
1671241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  LiveInterval* source = interval->GetSiblingAt(from->GetLifetimeEnd() - 1);
167286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
167386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (destination == source) {
167486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Interval was not split.
167586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
167686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
16778ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray  DCHECK(destination != nullptr && source != nullptr);
16788ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray
167986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!destination->HasRegister()) {
168086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Values are eagerly spilled. Spill slot already contains appropriate value.
168186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
168286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
168386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
168486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
168586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // we need to put the moves at the entry of `to`.
168686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (from->GetSuccessors().Size() == 1) {
1687740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtExitOf(from,
1688740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                               interval->GetParent()->GetDefinedBy(),
168901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               source->ToLocation(),
169001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               destination->ToLocation());
169186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
169286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK_EQ(to->GetPredecessors().Size(), 1u);
1693740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(to,
1694740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                interval->GetParent()->GetDefinedBy(),
169501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                source->ToLocation(),
169601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                destination->ToLocation());
169786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
169886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
169986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
170086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() {
1701776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(),
17024c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_core_registers_,
17034c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_fp_registers_,
17044c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     reserved_out_slots_,
17050d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray                                     codegen_->GetGraph()->GetLinearOrder());
170686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
170786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Adjust the Out Location of instructions.
170886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
170986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
171086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
171186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* current = instruction->GetLiveInterval();
171286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LocationSummary* locations = instruction->GetLocations();
171386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location location = locations->Out();
1714476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain    if (instruction->IsParameterValue()) {
171586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Now that we know the frame size, adjust the parameter's location.
171686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.IsStackSlot()) {
171786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
171886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1719f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
172086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (location.IsDoubleStackSlot()) {
172186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
172286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1723f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
172486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (current->HasSpillSlot()) {
172586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
172686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
172776b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray    } else if (instruction->IsCurrentMethod()) {
172876b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      // The current method is always at offset 0.
172976b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      DCHECK(!current->HasSpillSlot() || (current->GetSpillSlot() == 0));
1730776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    } else if (current->HasSpillSlot()) {
1731776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // Adjust the stack slot, now that we know the number of them for each type.
1732776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // The way this implementation lays out the stack is the following:
1733776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [parameter slots     ]
1734776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [double spill slots  ]
1735776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [long spill slots    ]
1736776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [float spill slots   ]
1737776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [int/ref values      ]
1738776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [maximum out values  ] (number of arguments for calls)
1739776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [art method          ].
1740776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      uint32_t slot = current->GetSpillSlot();
1741776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      switch (current->GetType()) {
1742776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimDouble:
1743776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += long_spill_slots_.Size();
1744776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1745776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimLong:
1746776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += float_spill_slots_.Size();
1747776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1748776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimFloat:
1749776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += int_spill_slots_.Size();
1750776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1751776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimNot:
1752776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimInt:
1753776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimChar:
1754776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimByte:
1755776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimBoolean:
1756776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimShort:
1757776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += reserved_out_slots_;
1758776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          break;
1759776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimVoid:
1760776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          LOG(FATAL) << "Unexpected type for interval " << current->GetType();
1761776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      }
1762776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      current->SetSpillSlot(slot * kVRegSize);
176386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
176486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
176501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
176686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
176786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (location.IsUnallocated()) {
176886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.GetPolicy() == Location::kSameAsFirstInput) {
1769d0d4852847432368b090c184d6639e573538dccfCalin Juravle        if (locations->InAt(0).IsUnallocated()) {
1770d0d4852847432368b090c184d6639e573538dccfCalin Juravle          locations->SetInAt(0, source);
1771d0d4852847432368b090c184d6639e573538dccfCalin Juravle        } else {
1772d0d4852847432368b090c184d6639e573538dccfCalin Juravle          DCHECK(locations->InAt(0).Equals(source));
1773d0d4852847432368b090c184d6639e573538dccfCalin Juravle        }
177486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1775829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      locations->UpdateOut(source);
177686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
177786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source.Equals(location));
177886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
177986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
178086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
178186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Connect siblings.
178286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
178386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
178486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ConnectSiblings(instruction->GetLiveInterval());
178586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
178686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
178786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve non-linear control flow across branches. Order does not matter.
17880d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
178986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* block = it.Current();
179086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    BitVector* live = liveness_.GetLiveInSet(*block);
179186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (uint32_t idx : live->Indexes()) {
179286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
179386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      LiveInterval* interval = current->GetLiveInterval();
179486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
179586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
179686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
179786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
179886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
179986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
180086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve phi inputs. Order does not matter.
18010d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
180286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* current = it.Current();
1803277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1804277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      HInstruction* phi = inst_it.Current();
180586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
180686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HBasicBlock* predecessor = current->GetPredecessors().Get(i);
180786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
180886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HInstruction* input = phi->InputAt(i);
180901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location source = input->GetLiveInterval()->GetLocationAt(
181001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray            predecessor->GetLifetimeEnd() - 1);
181101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location destination = phi->GetLiveInterval()->ToLocation();
1812234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        InsertParallelMoveAtExitOf(predecessor, phi, source, destination);
181386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
181486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
181586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
18163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
18173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Assign temp locations.
18183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < temp_intervals_.Size(); ++i) {
18193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* temp = temp_intervals_.Get(i);
1820840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (temp->IsHighInterval()) {
1821840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // High intervals can be skipped, they are already handled by the low interval.
1822840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
1823840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
182401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    HInstruction* at = liveness_.GetTempUser(temp);
1825f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray    size_t temp_index = liveness_.GetTempIndex(temp);
182601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    LocationSummary* locations = at->GetLocations();
18275368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    switch (temp->GetType()) {
18285368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimInt:
1829f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray        locations->SetTempAt(temp_index, Location::RegisterLocation(temp->GetRegister()));
18305368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
18315368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
18325368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimDouble:
1833840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1834840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          Location location = Location::FpuRegisterPairLocation(
1835840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray              temp->GetRegister(), temp->GetHighInterval()->GetRegister());
1836f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          locations->SetTempAt(temp_index, location);
1837840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        } else {
1838f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          locations->SetTempAt(temp_index, Location::FpuRegisterLocation(temp->GetRegister()));
1839840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
18405368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
18415368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
18425368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      default:
18435368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        LOG(FATAL) << "Unexpected type for temporary location "
18445368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                   << temp->GetType();
18455368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    }
18463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
184731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}
184831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1849a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}  // namespace art
1850