register_allocator.cc revision 3fc992f9dfe8f49ff350132323cc635f102b7b62
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) {
381c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray      LiveInterval* split = Split(current, 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()
48586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray           // Parameters have their own stack slot.
48686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray           && !(defined_by != nullptr && defined_by->IsParameterValue())) {
4873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers
4883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            + current->GetParent()->GetSpillSlot() / kVRegSize
4893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            - number_of_out_slots);
49031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
49131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_spill_slot->IsBitSet(j)) {
49231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            if (log_fatal_on_failure) {
49331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              std::ostringstream message;
49431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              message << "Spill slot conflict at " << j;
49531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              LOG(FATAL) << message.str();
49631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            } else {
49731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              return false;
49831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            }
49931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          } else {
50031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_spill_slot->SetBit(j);
50131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          }
50231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        }
503a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
50431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
50531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current->HasRegister()) {
50631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister());
50731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
50831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_register->IsBitSet(j)) {
509829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray            if (current->IsUsingInputRegister() && current->CanUseInputRegister()) {
510829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray              continue;
511829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray            }
512a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            if (log_fatal_on_failure) {
513a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              std::ostringstream message;
5143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "Register conflict at " << j << " ";
5153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              if (defined_by != nullptr) {
5163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                message << "(" << defined_by->DebugName() << ")";
5173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              }
5183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "for ";
519a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              if (processing_core_registers) {
520a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpCoreRegister(message, current->GetRegister());
521a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              } else {
522a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpFloatingPointRegister(message, current->GetRegister());
523a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              }
524a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              LOG(FATAL) << message.str();
525a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            } else {
526a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              return false;
527a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            }
528a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          } else {
52931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_register->SetBit(j);
530a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          }
531a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        }
53231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
53331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
534a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
535a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
536a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
537a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
53886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
539a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  interval->Dump(stream);
540a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << ": ";
541a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (interval->HasRegister()) {
542102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (interval->IsFloatingPoint()) {
54386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
544102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
545102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      codegen_->DumpCoreRegister(stream, interval->GetRegister());
546a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
547a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
548a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    stream << "spilled";
549a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
550a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << std::endl;
551a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
552a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
553296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yangvoid RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
554296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "inactive: " << std::endl;
555296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < inactive_.Size(); i ++) {
556296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, inactive_.Get(i));
557296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
558296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "active: " << std::endl;
559296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < active_.Size(); i ++) {
560296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, active_.Get(i));
561296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
562296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "unhandled: " << std::endl;
563296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  auto unhandled = (unhandled_ != nullptr) ?
564296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      unhandled_ : &unhandled_core_intervals_;
565296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < unhandled->Size(); i ++) {
566296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, unhandled->Get(i));
567296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
568296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "handled: " << std::endl;
569296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < handled_.Size(); i ++) {
570296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, handled_.Get(i));
571296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
572296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang}
573296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
574a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// By the book implementation of a linear scan register allocator.
575a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::LinearScan() {
5763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  while (!unhandled_->IsEmpty()) {
577a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (1) Remove interval with the lowest start position from unhandled.
5783946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = unhandled_->Pop();
5793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    DCHECK(!current->IsFixed() && !current->HasSpillSlot());
580c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart());
581840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsLowInterval() || unhandled_->Peek()->IsHighInterval());
58287d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray
583a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t position = current->GetStart();
584a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
585296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Remember the inactive_ size here since the ones moved to inactive_ from
586296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // active_ below shouldn't need to be re-checked.
587296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    size_t inactive_intervals_to_handle = inactive_.Size();
588296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
589a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (2) Remove currently active intervals that are dead at this position.
590a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move active intervals that have a lifetime hole at this position
591a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     to inactive.
592a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0; i < active_.Size(); ++i) {
593a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = active_.Get(i);
594a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
595a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
596a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
597a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
598a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (!interval->Covers(position)) {
599a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
600a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
601a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Add(interval);
602a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
603a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
604a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
605a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (3) Remove currently inactive intervals that are dead at this position.
606a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move inactive intervals that cover this position to active.
607296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    for (size_t i = 0; i < inactive_intervals_to_handle; ++i) {
608a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = inactive_.Get(i);
609296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK(interval->GetStart() < position || interval->IsFixed());
610a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
611a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
612a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
613296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        --inactive_intervals_to_handle;
614a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
615a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (interval->Covers(position)) {
616a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
617a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
618296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        --inactive_intervals_to_handle;
619a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Add(interval);
620a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
621a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
622a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
623acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    if (current->IsSlowPathSafepoint()) {
624acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Synthesized interval to record the maximum number of live registers
625acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // at safepoints. No need to allocate a register for it.
626f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      if (processing_core_registers_) {
627f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_core_registers_ =
628f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell          std::max(maximum_number_of_live_core_registers_, active_.Size());
629f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      } else {
630f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_fp_registers_ =
631f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell          std::max(maximum_number_of_live_fp_registers_, active_.Size());
632f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      }
633acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() > current->GetStart());
634acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      continue;
635acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    }
636acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray
637840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
638840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(!current->HasRegister());
639840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // Allocating the low part was unsucessful. The splitted interval for the high part
640840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // will be handled next (it is in the `unhandled_` list).
641840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
642840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
643840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
644a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (4) Try to find an available register.
645a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    bool success = TryAllocateFreeReg(current);
646a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
647a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (5) If no register could be found, we need to spill.
648a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (!success) {
649a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      success = AllocateBlockedReg(current);
650a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
651a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
652a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (6) If the interval had a register allocated, add it to the list of active
653a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     intervals.
654a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (success) {
655988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      codegen_->AddAllocatedRegister(processing_core_registers_
656988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          ? Location::RegisterLocation(current->GetRegister())
657988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          : Location::FpuRegisterLocation(current->GetRegister()));
658a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      active_.Add(current);
659840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
660840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
661840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
662a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
663a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
664a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
665a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
666829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffraystatic void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) {
667829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  DCHECK(!interval->IsHighInterval());
668829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // Note that the same instruction may occur multiple times in the input list,
669829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // so `free_until` may have changed already.
6703fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  // Since `position` is not the current scan position, we need to use CoversSlow.
671829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  if (interval->IsDeadAt(position)) {
672829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // Set the register to be free. Note that inactive intervals might later
673829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // update this.
674829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    free_until[interval->GetRegister()] = kMaxLifetimePosition;
675829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (interval->HasHighInterval()) {
676829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      DCHECK(interval->GetHighInterval()->IsDeadAt(position));
677829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition;
678829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
6793fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  } else if (!interval->CoversSlow(position)) {
680829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // The interval becomes inactive at `defined_by`. We make its register
681829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // available only until the next use strictly after `defined_by`.
682829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    free_until[interval->GetRegister()] = interval->FirstUseAfter(position);
683829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (interval->HasHighInterval()) {
6843fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil      DCHECK(!interval->GetHighInterval()->CoversSlow(position));
685829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()];
686829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
687829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  }
688829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray}
689829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray
690a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find a free register. If multiple are found, pick the register that
691a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// is free the longest.
692a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
693a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* free_until = registers_array_;
694a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
695a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers to be free.
696a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
697a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    free_until[i] = kMaxLifetimePosition;
698a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
699a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
700296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  // For each active interval, set its register to not free.
701296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
702296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    LiveInterval* interval = active_.Get(i);
703296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(interval->HasRegister());
704296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    free_until[interval->GetRegister()] = 0;
705296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
706296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
707829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // An interval that starts an instruction (that is, it is not split), may
708829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // re-use the registers used by the inputs of that instruciton, based on the
709829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // location summary.
710829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  HInstruction* defined_by = current->GetDefinedBy();
711829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  if (defined_by != nullptr && !current->IsSplit()) {
712829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    LocationSummary* locations = defined_by->GetLocations();
713829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) {
714829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      for (HInputIterator it(defined_by); !it.Done(); it.Advance()) {
715829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // Take the last interval of the input. It is the location of that interval
716829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // that will be used at `defined_by`.
717829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        LiveInterval* interval = it.Current()->GetLiveInterval()->GetLastSibling();
718829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // Note that interval may have not been processed yet.
719829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // TODO: Handle non-split intervals last in the work list.
720829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        if (interval->HasRegister() && interval->SameRegisterKind(*current)) {
721829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // The input must be live until the end of `defined_by`, to comply to
722829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // the linear scan algorithm. So we use `defined_by`'s end lifetime
723829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // position to check whether the input is dead or is inactive after
724829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // `defined_by`.
7253fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil          DCHECK(interval->CoversSlow(defined_by->GetLifetimePosition()));
726829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          size_t position = defined_by->GetLifetimePosition() + 1;
727829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          FreeIfNotCoverAt(interval, position, free_until);
728829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        }
729829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      }
730829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
731829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  }
732829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray
733a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, set its register to be free until
734a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // the next intersection with `current`.
735a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
736a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
737296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
738296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
739296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
740296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
741296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
742296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
743296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
744296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
745296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
746296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
747296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
748a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
749296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (free_until[inactive->GetRegister()] == 0) {
750296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Already used by some active interval. No need to intersect.
751296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
752296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
753a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
754a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (next_intersection != kNoLifetime) {
755aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray      free_until[inactive->GetRegister()] =
756aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray          std::min(free_until[inactive->GetRegister()], next_intersection);
757a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
758a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
759a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7606c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
7613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasRegister()) {
7623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Some instructions have a fixed register output.
7633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    reg = current->GetRegister();
764840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (free_until[reg] == 0) {
765840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(current->IsHighInterval());
766840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // AllocateBlockedReg will spill the holder of the register.
767840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      return false;
768840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
7693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
770840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
77101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    int hint = current->FindFirstRegisterHint(free_until);
77201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    if (hint != kNoRegister) {
77301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      DCHECK(!IsBlocked(hint));
77401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      reg = hint;
775840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (current->IsLowInterval()) {
7766c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      reg = FindAvailableRegisterPair(free_until, current->GetStart());
77701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    } else {
778840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = FindAvailableRegister(free_until);
779a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
780a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
781a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7826c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
783a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // If we could not find a register, we need to spill.
784840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (free_until[reg] == 0) {
785840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return false;
786840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
787840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
788234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (current->IsLowInterval()) {
789234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    // If the high register of this interval is not available, we need to spill.
790234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    int high_reg = current->GetHighInterval()->GetRegister();
791234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (high_reg == kNoRegister) {
792234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      high_reg = GetHighForLowRegister(reg);
793234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    }
794234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (free_until[high_reg] == 0) {
795234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      return false;
796234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    }
797a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
798a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
799a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  current->SetRegister(reg);
800a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (!current->IsDeadAt(free_until[reg])) {
801a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the register is only available for a subset of live ranges
802a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // covered by `current`, split `current` at the position where
803a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // the register is not available anymore.
804a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* split = Split(current, free_until[reg]);
805a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(split != nullptr);
8063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    AddSorted(unhandled_, split);
807a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
808a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
809a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
810a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
811a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::IsBlocked(int reg) const {
812102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return processing_core_registers_
813102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? blocked_core_registers_[reg]
814102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : blocked_fp_registers_[reg];
815a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
816a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
8176c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffrayint RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const {
8186c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
819840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Pick the register pair that is used the last.
820840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
821840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(i)) continue;
822840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (!IsLowRegister(i)) continue;
823840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int high_register = GetHighForLowRegister(i);
824840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(high_register)) continue;
825840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int existing_high_register = GetHighForLowRegister(reg);
8266c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if ((reg == kNoRegister) || (next_use[i] >= next_use[reg]
827840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                        && next_use[high_register] >= next_use[existing_high_register])) {
828840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
829840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition
830840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          && next_use[high_register] == kMaxLifetimePosition) {
831840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        break;
832840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
8336c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) {
8346c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If one of the current register is known to be unavailable, just unconditionally
8356c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // try a new one.
8366c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      reg = i;
837840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
838840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
839840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
840840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
841840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
842840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffrayint RegisterAllocator::FindAvailableRegister(size_t* next_use) const {
8436c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
844840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Pick the register that is used the last.
845840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
846840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(i)) continue;
8476c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (reg == kNoRegister || next_use[i] > next_use[reg]) {
848840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
849840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition) break;
850840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
851840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
852840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
853840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
854840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
855234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraybool RegisterAllocator::TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
856234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                                 size_t first_register_use,
857234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                                 size_t* next_use) {
8586c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
8596c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    LiveInterval* active = active_.Get(i);
8606c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    DCHECK(active->HasRegister());
861234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (active->IsFixed()) continue;
862234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (active->IsHighInterval()) continue;
863234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (first_register_use > next_use[active->GetRegister()]) continue;
864234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
8656c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // Split the first interval found.
866234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (!active->IsLowInterval() || IsLowOfUnalignedPairInterval(active)) {
8676c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* split = Split(active, position);
8686c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      active_.DeleteAt(i);
8696c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      if (split != active) {
8706c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        handled_.Add(active);
8716c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      }
8726c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
8736c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      return true;
8746c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
8756c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  }
8766c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  return false;
8776c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray}
8786c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
8795b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffraybool RegisterAllocator::PotentiallyRemoveOtherHalf(LiveInterval* interval,
8805b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray                                                   GrowableArray<LiveInterval*>* intervals,
8815b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray                                                   size_t index) {
8825b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  if (interval->IsLowInterval()) {
8835b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    DCHECK_EQ(intervals->Get(index), interval->GetHighInterval());
8845b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    intervals->DeleteAt(index);
8855b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    return true;
8865b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  } else if (interval->IsHighInterval()) {
8875b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    DCHECK_GT(index, 0u);
8885b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    DCHECK_EQ(intervals->Get(index - 1), interval->GetLowInterval());
8895b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    intervals->DeleteAt(index - 1);
8905b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    return true;
8915b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  } else {
8925b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    return false;
8935b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  }
8945b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray}
8955b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray
896a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval
897a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register
898a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead.
899a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
900a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t first_register_use = current->FirstRegisterUse();
901412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  if (first_register_use == kNoLifetime) {
90231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
903a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
904a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
905a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
906a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers as not being used.
907a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* next_use = registers_array_;
908a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
909a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    next_use[i] = kMaxLifetimePosition;
910a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
911a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
912a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each active interval, find the next use of its register after the
913a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
914a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
915a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* active = active_.Get(i);
916a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(active->HasRegister());
91786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (active->IsFixed()) {
91886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      next_use[active->GetRegister()] = current->GetStart();
91986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
92086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      size_t use = active->FirstRegisterUseAfter(current->GetStart());
92186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (use != kNoLifetime) {
92286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[active->GetRegister()] = use;
92386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
924a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
925a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
926a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
927a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, find the next use of its register after the
928a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
929a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
930a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
931296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
932296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
933296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
934296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
935296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
936296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
937296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
938296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
939296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
940296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
941a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
94286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
94386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_intersection != kNoLifetime) {
94486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (inactive->IsFixed()) {
94586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[inactive->GetRegister()] =
94686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            std::min(next_intersection, next_use[inactive->GetRegister()]);
94786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else {
94886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t use = inactive->FirstRegisterUseAfter(current->GetStart());
94986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (use != kNoLifetime) {
95086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
95186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
95286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
953a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
954a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
955a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9566c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
9576c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  bool should_spill = false;
958840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (current->HasRegister()) {
959840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(current->IsHighInterval());
960840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = current->GetRegister();
9616c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // When allocating the low part, we made sure the high register was available.
9626c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    DCHECK_LT(first_register_use, next_use[reg]);
963840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (current->IsLowInterval()) {
964234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    reg = FindAvailableRegisterPair(next_use, first_register_use);
9656c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // We should spill if both registers are not available.
9666c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    should_spill = (first_register_use >= next_use[reg])
9676c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || (first_register_use >= next_use[GetHighForLowRegister(reg)]);
968840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
969840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
970840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = FindAvailableRegister(next_use);
9716c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    should_spill = (first_register_use >= next_use[reg]);
972a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
973a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9746c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
9756c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  if (should_spill) {
976840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
977234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    bool is_allocation_at_use_site = (current->GetStart() >= (first_register_use - 1));
9786c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (current->IsLowInterval()
9796c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        && is_allocation_at_use_site
980234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        && TrySplitNonPairOrUnalignedPairIntervalAt(current->GetStart(),
981234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                    first_register_use,
982234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                    next_use)) {
9836c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If we're allocating a register for `current` because the instruction at
9846c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // that position requires it, but we think we should spill, then there are
985234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // non-pair intervals or unaligned pair intervals blocking the allocation.
986234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // We split the first interval found, and put ourselves first in the
987234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // `unhandled_` list.
9886c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* existing = unhandled_->Peek();
9896c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK(existing->IsHighInterval());
9906c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK_EQ(existing->GetLowInterval(), current);
9916c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      unhandled_->Add(current);
9926c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else {
9936c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If the first use of that instruction is after the last use of the found
9946c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // register, we split this interval just before its first register use.
9956c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AllocateSpillSlotFor(current);
9966c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* split = Split(current, first_register_use - 1);
997234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      if (current == split) {
998234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DumpInterval(std::cerr, current);
999234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DumpAllIntervals(std::cerr);
1000234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // This situation has the potential to infinite loop, so we make it a non-debug CHECK.
1001234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        CHECK(false) << "There is not enough registers available for "
1002234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << split->GetParent()->GetDefinedBy()->DebugName() << " "
1003234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << split->GetParent()->GetDefinedBy()->GetId()
1004234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << " at " << first_register_use - 1;
1005234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      }
10066c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
10076c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
1008a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
1009a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
1010a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Use this register and spill the active and inactives interval that
1011a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // have that register.
1012a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    current->SetRegister(reg);
1013a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1014a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0, e = active_.Size(); i < e; ++i) {
1015a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* active = active_.Get(i);
1016a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (active->GetRegister() == reg) {
101786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(!active->IsFixed());
1018a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        LiveInterval* split = Split(active, current->GetStart());
1019dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        if (split != active) {
1020dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray          handled_.Add(active);
1021dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        }
10225b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray        active_.DeleteAt(i);
10235b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray        PotentiallyRemoveOtherHalf(active, &active_, i);
10243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        AddSorted(unhandled_, split);
1025a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        break;
1026a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
1027a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1028a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10295b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    for (size_t i = 0; i < inactive_.Size(); ++i) {
1030a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* inactive = inactive_.Get(i);
1031a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (inactive->GetRegister() == reg) {
1032296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        if (!current->IsSplit() && !inactive->IsFixed()) {
1033296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Neither current nor inactive are fixed.
1034296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Thanks to SSA, a non-split interval starting in a hole of an
1035296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // inactive interval should never intersect with that inactive interval.
1036296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Only if it's not fixed though, because fixed intervals don't come from SSA.
1037296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
1038296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          continue;
1039296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        }
104086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t next_intersection = inactive->FirstIntersectionWith(current);
104186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (next_intersection != kNoLifetime) {
104286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          if (inactive->IsFixed()) {
104386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            LiveInterval* split = Split(current, next_intersection);
1044dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            DCHECK_NE(split, current);
10453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
104686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          } else {
1047dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // Split at the start of `current`, which will lead to splitting
1048dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // at the end of the lifetime hole of `inactive`.
1049dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            LiveInterval* split = Split(inactive, current->GetStart());
1050dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // If it's inactive, it must start before the current interval.
1051dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            DCHECK_NE(split, inactive);
105286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            inactive_.DeleteAt(i);
10535b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            if (PotentiallyRemoveOtherHalf(inactive, &inactive_, i) && inactive->IsHighInterval()) {
10545b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray              // We have removed an entry prior to `inactive`. So we need to decrement.
10555b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray              --i;
10565b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            }
10575b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            // Decrement because we have removed `inactive` from the list.
1058296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang            --i;
105986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            handled_.Add(inactive);
10603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
106186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          }
106286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
1063a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
1064a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1065a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1066a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return true;
1067a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1068a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1069a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) {
1071c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray  DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
107286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t insert_at = 0;
10733946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = array->Size(); i > 0; --i) {
10743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = array->Get(i - 1);
10756c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // High intervals must be processed right after their low equivalent.
10766c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (current->StartsAfter(interval) && !current->IsHighInterval()) {
107786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      insert_at = i;
1078a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      break;
1079acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
1080acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Ensure the slow path interval is the last to be processed at its location: we want the
1081acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // interval to know all live registers at this location.
1082acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      DCHECK(i == 1 || array->Get(i - 2)->StartsAfter(current));
1083acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      insert_at = i;
1084acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      break;
1085a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1086a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1087840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
10883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  array->InsertAt(insert_at, interval);
1089840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Insert the high interval before the low, to ensure the low is processed before.
1090840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->HasHighInterval()) {
1091840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    array->InsertAt(insert_at, interval->GetHighInterval());
1092840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (interval->HasLowInterval()) {
1093840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    array->InsertAt(insert_at + 1, interval->GetLowInterval());
1094840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1095a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1096a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1097a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
1098840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  DCHECK_GE(position, interval->GetStart());
1099a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(!interval->IsDeadAt(position));
1100a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (position == interval->GetStart()) {
1101a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Spill slot will be allocated when handling `interval` again.
1102a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    interval->ClearRegister();
1103840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1104840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetHighInterval()->ClearRegister();
1105840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1106840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetLowInterval()->ClearRegister();
1107840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1108a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return interval;
1109a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
1110a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* new_interval = interval->SplitAt(position);
1111840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1112840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
1113840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetHighInterval(high);
1114840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetLowInterval(new_interval);
1115840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1116840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
1117840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetLowInterval(low);
1118840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      low->SetHighInterval(new_interval);
1119840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1120a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return new_interval;
1121a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1122a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1123a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
112431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
1125840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->IsHighInterval()) {
1126840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    // The low interval will contain the spill slot.
1127840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return;
1128840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1129840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
113031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* parent = interval->GetParent();
113131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
113231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // An instruction gets a spill slot for its entire lifetime. If the parent
113331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // of this interval already has a spill slot, there is nothing to do.
113431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  if (parent->HasSpillSlot()) {
113531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    return;
113631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
113731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
113886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* defined_by = parent->GetDefinedBy();
113986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (defined_by->IsParameterValue()) {
114086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Parameters have their own stack slot.
114186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
114286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
114386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
114486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
114596f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  if (defined_by->IsConstant()) {
114696f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    // Constants don't need a spill slot.
114796f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    return;
114896f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  }
114996f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray
115031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* last_sibling = interval;
115131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  while (last_sibling->GetNextSibling() != nullptr) {
115231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    last_sibling = last_sibling->GetNextSibling();
115331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
115431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  size_t end = last_sibling->GetEnd();
115531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1156776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  GrowableArray<size_t>* spill_slots = nullptr;
1157776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  switch (interval->GetType()) {
1158776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimDouble:
1159776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &double_spill_slots_;
1160776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1161776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimLong:
1162776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &long_spill_slots_;
1163776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1164776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimFloat:
1165776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &float_spill_slots_;
1166776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1167776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimNot:
1168776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimInt:
1169776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimChar:
1170776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimByte:
1171776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimBoolean:
1172776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimShort:
1173776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &int_spill_slots_;
1174776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1175776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimVoid:
1176776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      LOG(FATAL) << "Unexpected type for interval " << interval->GetType();
1177776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  }
1178776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray
1179412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  // Find an available spill slot.
1180412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  size_t slot = 0;
1181776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  for (size_t e = spill_slots->Size(); slot < e; ++slot) {
1182776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (spill_slots->Get(slot) <= parent->GetStart()
1183776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        && (slot == (e - 1) || spill_slots->Get(slot + 1) <= parent->GetStart())) {
1184412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray      break;
1185412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    }
1186412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
1187412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
118801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  if (parent->NeedsTwoSpillSlots()) {
1189776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (slot == spill_slots->Size()) {
11903c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
1191776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
1192776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
1193776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    } else if (slot == spill_slots->Size() - 1) {
1194776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
1195776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
11963c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
1197776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
1198776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot + 1, end);
119931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
120031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  } else {
1201776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (slot == spill_slots->Size()) {
12023c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
1203776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
12043c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
1205776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
12063c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    }
120731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
120831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1209776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // Note that the exact spill slot location will be computed when we resolve,
1210776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // that is when we know the number of spill slots for each type.
1211776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  parent->SetSpillSlot(slot);
121286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
121386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
12142a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffraystatic bool IsValidDestination(Location destination) {
1215102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return destination.IsRegister()
12166c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || destination.IsRegisterPair()
1217102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsFpuRegister()
1218840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      || destination.IsFpuRegisterPair()
1219102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsStackSlot()
1220102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsDoubleStackSlot();
12212a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray}
12222a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray
1223234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddMove(HParallelMove* move,
1224234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location source,
1225234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location destination,
1226234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                HInstruction* instruction,
1227234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Primitive::Type type) const {
1228234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (type == Primitive::kPrimLong
1229234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && codegen_->ShouldSplitLongMoves()
1230234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // The parallel move resolver knows how to deal with long constants.
1231234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && !source.IsConstant()) {
12329021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source.ToLow(), destination.ToLow(), Primitive::kPrimInt, instruction);
12339021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source.ToHigh(), destination.ToHigh(), Primitive::kPrimInt, nullptr);
1234234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  } else {
12359021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source, destination, type, instruction);
1236234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  }
1237234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray}
1238234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1239234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* input,
1240234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                        HInstruction* user,
124186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
124286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
124386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
124486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1245476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  DCHECK(!user->IsPhi());
124686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1247740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  HInstruction* previous = user->GetPrevious();
124886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = nullptr;
124986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (previous == nullptr
1250476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain      || !previous->IsParallelMove()
12518e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray      || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
125286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
12538e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    move->SetLifetimePosition(user->GetLifetimePosition());
1254740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    user->GetBlock()->InsertInstructionBefore(move, user);
125586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
125686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
125786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
12588e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
1259234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, nullptr, input->GetType());
126086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
126186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
126246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionStart(size_t position) {
126346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 0;
126446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
126546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
126646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionEnd(size_t position) {
126746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 1;
126846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
126946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
127086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position,
1271740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                             HInstruction* instruction,
127286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location source,
127386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location destination) const {
12746c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
127586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
127686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
127786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
127886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
127946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  if (at == nullptr) {
128046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    if (IsInstructionStart(position)) {
128146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Block boundary, don't do anything the connection of split siblings will handle it.
128246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      return;
128346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    } else {
128446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Move must happen before the first instruction of the block.
128546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      at = liveness_.GetInstructionFromPosition((position + 1) / 2);
12865976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // Note that parallel moves may have already been inserted, so we explicitly
12875976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // ask for the first instruction of the block: `GetInstructionFromPosition` does
1288234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // not contain the `HParallelMove` instructions.
12895976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      at = at->GetBlock()->GetFirstInstruction();
1290234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1291234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      if (at->GetLifetimePosition() < position) {
1292234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // We may insert moves for split siblings and phi spills at the beginning of the block.
1293234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // Since this is a different lifetime position, we need to go to the next instruction.
1294234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DCHECK(at->IsParallelMove());
1295234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        at = at->GetNext();
1296234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      }
1297234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
12985976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      if (at->GetLifetimePosition() != position) {
12995976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_GT(at->GetLifetimePosition(), position);
130046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move = new (allocator_) HParallelMove(allocator_);
130146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move->SetLifetimePosition(position);
130246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        at->GetBlock()->InsertInstructionBefore(move, at);
13035976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      } else {
13045976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK(at->IsParallelMove());
13055976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        move = at->AsParallelMove();
130646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      }
130746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    }
130846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  } else if (IsInstructionEnd(position)) {
130986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen after the instruction.
131086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(!at->IsControlFlow());
131186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = at->GetNext()->AsParallelMove();
1312e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // This is a parallel move for connecting siblings in a same block. We need to
1313e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // differentiate it with moves for connecting blocks, and input moves.
13148e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    if (move == nullptr || move->GetLifetimePosition() > position) {
131586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
131686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
131786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
131886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
131986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
132086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen before the instruction.
132186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* previous = at->GetPrevious();
1322740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    if (previous == nullptr
1323740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || !previous->IsParallelMove()
1324740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || previous->GetLifetimePosition() != position) {
1325740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // If the previous is a parallel move, then its position must be lower
1326740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // than the given `position`: it was added just after the non-parallel
1327740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // move instruction that precedes `instruction`.
1328740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      DCHECK(previous == nullptr
1329740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || !previous->IsParallelMove()
1330740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || previous->GetLifetimePosition() < position);
133186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
133286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
133386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at);
133486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
133586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = previous->AsParallelMove();
133686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
133786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
133801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), position);
1339234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
134086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
134186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
134286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
1343740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                   HInstruction* instruction,
134486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location source,
134586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location destination) const {
13466c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
134786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
134886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
134986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK_EQ(block->GetSuccessors().Size(), 1u);
135086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* last = block->GetLastInstruction();
1351360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // We insert moves at exit for phi predecessors and connecting blocks.
1352360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // A block ending with an if cannot branch to a block with phis because
1353360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // we do not allow critical edges. It can also not connect
1354360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // a split interval between two blocks: the move has to happen in the successor.
1355360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  DCHECK(!last->IsIf());
135686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* previous = last->GetPrevious();
135786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
1358e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1359e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and output moves.
13605976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray  size_t position = last->GetLifetimePosition();
1361740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  if (previous == nullptr || !previous->IsParallelMove()
13625976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      || previous->AsParallelMove()->GetLifetimePosition() != position) {
136386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
13645976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    move->SetLifetimePosition(position);
136586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, last);
136686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
136786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
136886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1369234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
137086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
137186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
137286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
1373740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                    HInstruction* instruction,
137486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location source,
137586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location destination) const {
13766c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
137786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
137886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
137986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* first = block->GetFirstInstruction();
138086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = first->AsParallelMove();
1381234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  size_t position = block->GetLifetimeStart();
1382e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1383e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and input moves.
1384234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
138586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1386234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->SetLifetimePosition(position);
138786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, first);
138886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1389234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
139086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
139186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
139286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
139386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
139486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
13956c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
139686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
139786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1398476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  if (instruction->IsPhi()) {
1399740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
140086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
140186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
140286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1403e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  size_t position = instruction->GetLifetimePosition() + 1;
140486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = instruction->GetNext()->AsParallelMove();
1405e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for moving the output of an instruction. We need
1406e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // to differentiate with input moves, moves for connecting siblings in a
1407e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // and moves for connecting blocks.
1408e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
140986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1410e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(position);
141186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
141286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1413234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
141486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
141586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
141686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
141786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
141886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (current->HasSpillSlot() && current->HasRegister()) {
141986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // We spill eagerly, so move must be at definition.
142086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertMoveAfter(interval->GetDefinedBy(),
1421840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                    interval->ToLocation(),
142201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                    interval->NeedsTwoSpillSlots()
1423412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1424412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
142586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
142686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  UsePosition* use = current->GetFirstUse();
142786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
142886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Walk over all siblings, updating locations of use positions, and
142986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // connecting them when they are adjacent.
143086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  do {
143101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
143286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
143386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Walk over all uses covered by this interval, and update the location
143486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // information.
1435d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray
1436d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    LiveRange* range = current->GetFirstRange();
1437d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    while (range != nullptr) {
1438d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      while (use != nullptr && use->GetPosition() < range->GetStart()) {
1439d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray        DCHECK(use->GetIsEnvironment());
1440d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray        use = use->GetNext();
1441d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      }
1442d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      while (use != nullptr && use->GetPosition() <= range->GetEnd()) {
14433fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil        DCHECK(current->CoversSlow(use->GetPosition()) || (use->GetPosition() == range->GetEnd()));
1444d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray        LocationSummary* locations = use->GetUser()->GetLocations();
1445d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray        if (use->GetIsEnvironment()) {
1446d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray          locations->SetEnvironmentAt(use->GetInputIndex(), source);
144771fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe        } else {
1448d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray          Location expected_location = locations->InAt(use->GetInputIndex());
1449d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray          // The expected (actual) location may be invalid in case the input is unused. Currently
1450d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray          // this only happens for intrinsics.
1451d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray          if (expected_location.IsValid()) {
1452d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray            if (expected_location.IsUnallocated()) {
1453d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray              locations->SetInAt(use->GetInputIndex(), source);
1454d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray            } else if (!expected_location.IsConstant()) {
1455d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray              AddInputMoveFor(interval->GetDefinedBy(), use->GetUser(), source, expected_location);
1456d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray            }
1457d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray          } else {
1458d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray            DCHECK(use->GetUser()->IsInvoke());
1459d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray            DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
1460d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray          }
146186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
1462d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray        use = use->GetNext();
146386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1464d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      range = range->GetNext();
146586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
146686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
146786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // If the next interval starts just after this one, and has a register,
146886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // insert a move.
146986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* next_sibling = current->GetNextSibling();
147086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_sibling != nullptr
147186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && next_sibling->HasRegister()
147286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && current->GetEnd() == next_sibling->GetStart()) {
147301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      Location destination = next_sibling->ToLocation();
1474740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
147586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
14763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
147743af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray    for (SafepointPosition* safepoint_position = current->GetFirstSafepoint();
147843af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray         safepoint_position != nullptr;
147943af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray         safepoint_position = safepoint_position->GetNext()) {
14803fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil      DCHECK(current->CoversSlow(safepoint_position->GetPosition()));
14813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
14825588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      LocationSummary* locations = safepoint_position->GetLocations();
14833bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
14843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
14853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
14863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
14873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      switch (source.GetKind()) {
14883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kRegister: {
14893bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray          locations->AddLiveRegister(source);
1490988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) {
1491988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray            DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1492988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_core_registers_ +
1493988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_fp_registers_);
1494988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          }
14953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          if (current->GetType() == Primitive::kPrimNot) {
149656b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray            locations->SetRegisterBit(source.reg());
14973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          }
14983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
14993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
1500102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        case Location::kFpuRegister: {
1501102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          locations->AddLiveRegister(source);
1502102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          break;
1503102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        }
15046c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
15056c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        case Location::kRegisterPair:
1506840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        case Location::kFpuRegisterPair: {
1507840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToLow());
1508840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToHigh());
1509840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          break;
1510840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
15113946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kStackSlot:  // Fall-through
15123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kDoubleStackSlot:  // Fall-through
15133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kConstant: {
15143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          // Nothing to do.
15153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
15163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
15173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        default: {
15183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          LOG(FATAL) << "Unexpected location for object";
15193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
15203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
15213946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
152286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = next_sibling;
152386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } while (current != nullptr);
1524d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray
1525d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray  if (kIsDebugBuild) {
1526d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    // Following uses can only be environment uses. The location for
1527d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    // these environments will be none.
1528d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    while (use != nullptr) {
1529d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      DCHECK(use->GetIsEnvironment());
1530d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      use = use->GetNext();
1531d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    }
15325aab7cb5368b2ae4f513e8d58208b1eac990aa15Nicolas Geoffray  }
153386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
153486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
153586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
153686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* from,
153786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* to) const {
153886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval->GetNextSibling() == nullptr) {
153986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Nothing to connect. The whole range was allocated to the same location.
154086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
154186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
154286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1543241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  // Find the intervals that cover `from` and `to`.
1544241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  LiveInterval* destination = interval->GetSiblingAt(to->GetLifetimeStart());
1545241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  LiveInterval* source = interval->GetSiblingAt(from->GetLifetimeEnd() - 1);
154686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
154786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (destination == source) {
154886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Interval was not split.
154986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
155086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
15518ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray  DCHECK(destination != nullptr && source != nullptr);
15528ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray
155386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!destination->HasRegister()) {
155486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Values are eagerly spilled. Spill slot already contains appropriate value.
155586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
155686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
155786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
155886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
155986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // we need to put the moves at the entry of `to`.
156086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (from->GetSuccessors().Size() == 1) {
1561740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtExitOf(from,
1562740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                               interval->GetParent()->GetDefinedBy(),
156301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               source->ToLocation(),
156401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               destination->ToLocation());
156586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
156686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK_EQ(to->GetPredecessors().Size(), 1u);
1567740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(to,
1568740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                interval->GetParent()->GetDefinedBy(),
156901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                source->ToLocation(),
157001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                destination->ToLocation());
157186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
157286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
157386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
157486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() {
1575776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(),
15764c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_core_registers_,
15774c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_fp_registers_,
15784c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     reserved_out_slots_,
15790d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray                                     codegen_->GetGraph()->GetLinearOrder());
158086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
158186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Adjust the Out Location of instructions.
158286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
158386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
158486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
158586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* current = instruction->GetLiveInterval();
158686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LocationSummary* locations = instruction->GetLocations();
158786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location location = locations->Out();
1588476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain    if (instruction->IsParameterValue()) {
158986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Now that we know the frame size, adjust the parameter's location.
159086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.IsStackSlot()) {
159186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
159286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1593f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
159486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (location.IsDoubleStackSlot()) {
159586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
159686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1597f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
159886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (current->HasSpillSlot()) {
159986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
160086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1601776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    } else if (current->HasSpillSlot()) {
1602776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // Adjust the stack slot, now that we know the number of them for each type.
1603776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // The way this implementation lays out the stack is the following:
1604776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [parameter slots     ]
1605776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [double spill slots  ]
1606776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [long spill slots    ]
1607776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [float spill slots   ]
1608776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [int/ref values      ]
1609776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [maximum out values  ] (number of arguments for calls)
1610776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [art method          ].
1611776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      uint32_t slot = current->GetSpillSlot();
1612776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      switch (current->GetType()) {
1613776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimDouble:
1614776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += long_spill_slots_.Size();
1615776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1616776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimLong:
1617776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += float_spill_slots_.Size();
1618776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1619776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimFloat:
1620776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += int_spill_slots_.Size();
1621776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1622776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimNot:
1623776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimInt:
1624776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimChar:
1625776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimByte:
1626776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimBoolean:
1627776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimShort:
1628776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += reserved_out_slots_;
1629776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          break;
1630776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimVoid:
1631776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          LOG(FATAL) << "Unexpected type for interval " << current->GetType();
1632776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      }
1633776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      current->SetSpillSlot(slot * kVRegSize);
163486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
163586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
163601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
163786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
163886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (location.IsUnallocated()) {
163986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.GetPolicy() == Location::kSameAsFirstInput) {
1640d0d4852847432368b090c184d6639e573538dccfCalin Juravle        if (locations->InAt(0).IsUnallocated()) {
1641d0d4852847432368b090c184d6639e573538dccfCalin Juravle          locations->SetInAt(0, source);
1642d0d4852847432368b090c184d6639e573538dccfCalin Juravle        } else {
1643d0d4852847432368b090c184d6639e573538dccfCalin Juravle          DCHECK(locations->InAt(0).Equals(source));
1644d0d4852847432368b090c184d6639e573538dccfCalin Juravle        }
164586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1646829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      locations->UpdateOut(source);
164786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
164886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source.Equals(location));
164986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
165086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
165186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
165286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Connect siblings.
165386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
165486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
165586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ConnectSiblings(instruction->GetLiveInterval());
165686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
165786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
165886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve non-linear control flow across branches. Order does not matter.
16590d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
166086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* block = it.Current();
166186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    BitVector* live = liveness_.GetLiveInSet(*block);
166286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (uint32_t idx : live->Indexes()) {
166386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
166486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      LiveInterval* interval = current->GetLiveInterval();
166586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
166686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
166786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
166886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
166986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
167086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
167186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve phi inputs. Order does not matter.
16720d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
167386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* current = it.Current();
1674277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1675277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      HInstruction* phi = inst_it.Current();
167686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
167786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HBasicBlock* predecessor = current->GetPredecessors().Get(i);
167886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
167986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HInstruction* input = phi->InputAt(i);
168001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location source = input->GetLiveInterval()->GetLocationAt(
168101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray            predecessor->GetLifetimeEnd() - 1);
168201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location destination = phi->GetLiveInterval()->ToLocation();
1683234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        InsertParallelMoveAtExitOf(predecessor, phi, source, destination);
168486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
168586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
168686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
16873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
16883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Assign temp locations.
16893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < temp_intervals_.Size(); ++i) {
16903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* temp = temp_intervals_.Get(i);
1691840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (temp->IsHighInterval()) {
1692840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // High intervals can be skipped, they are already handled by the low interval.
1693840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
1694840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
169501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    HInstruction* at = liveness_.GetTempUser(temp);
1696f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray    size_t temp_index = liveness_.GetTempIndex(temp);
169701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    LocationSummary* locations = at->GetLocations();
16985368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    switch (temp->GetType()) {
16995368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimInt:
1700f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray        locations->SetTempAt(temp_index, Location::RegisterLocation(temp->GetRegister()));
17015368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
17025368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
17035368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimDouble:
1704840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1705840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          Location location = Location::FpuRegisterPairLocation(
1706840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray              temp->GetRegister(), temp->GetHighInterval()->GetRegister());
1707f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          locations->SetTempAt(temp_index, location);
1708840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        } else {
1709f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          locations->SetTempAt(temp_index, Location::FpuRegisterLocation(temp->GetRegister()));
1710840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
17115368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
17125368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
17135368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      default:
17145368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        LOG(FATAL) << "Unexpected type for temporary location "
17155368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                   << temp->GetType();
17165368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    }
17173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
171831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}
171931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1720a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}  // namespace art
1721