register_allocator.cc revision f01d34445953e6b9c9b13de1dd32a5c0ee5abab5
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;
1065976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    for (HLinearOrderIterator it(liveness_); !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.
1503946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (HLinearPostOrderIterator it(liveness_); !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)) {
227840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            interval->AddHighInterval(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
3133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Some instructions define their output in fixed register/stack slot. We need
3143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // to ensure we know these locations before doing register allocation. For a
3153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // given register, we create an interval that covers these locations. The register
3163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // will be unavailable at these locations when trying to allocate one for an
3173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // interval.
3183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  //
3193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // The backwards walking ensures the ranges are ordered on increasing start positions.
3203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  Location output = locations->Out();
321d0d4852847432368b090c184d6639e573538dccfCalin Juravle  if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
322d0d4852847432368b090c184d6639e573538dccfCalin Juravle    Location first = locations->InAt(0);
323d0d4852847432368b090c184d6639e573538dccfCalin Juravle    if (first.IsRegister() || first.IsFpuRegister()) {
324d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetFrom(position + 1);
325d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetRegister(first.reg());
326840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (first.IsPair()) {
327840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      current->SetFrom(position + 1);
328840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      current->SetRegister(first.low());
329840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = current->GetHighInterval();
330840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetRegister(first.high());
331840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetFrom(position + 1);
332d0d4852847432368b090c184d6639e573538dccfCalin Juravle    }
333d0d4852847432368b090c184d6639e573538dccfCalin Juravle  } else if (output.IsRegister() || output.IsFpuRegister()) {
3343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Shift the interval's start by one to account for the blocked register.
3353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetFrom(position + 1);
33656b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray    current->SetRegister(output.reg());
337102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    BlockRegister(output, position, position + 1);
338840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (output.IsPair()) {
339840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->SetFrom(position + 1);
340840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->SetRegister(output.low());
341840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    LiveInterval* high = current->GetHighInterval();
342840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    high->SetRegister(output.high());
343840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    high->SetFrom(position + 1);
344840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    BlockRegister(output.ToLow(), position, position + 1);
345840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    BlockRegister(output.ToHigh(), position, position + 1);
3463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
3473946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetSpillSlot(output.GetStackIndex());
348840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
349840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(output.IsUnallocated() || output.IsConstant());
3503946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3513946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // If needed, add interval to the list of unhandled intervals.
3533946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasSpillSlot() || instruction->IsConstant()) {
354c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    // Split just before first register use.
3553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    size_t first_register_use = current->FirstRegisterUse();
3563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (first_register_use != kNoLifetime) {
357c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray      LiveInterval* split = Split(current, first_register_use - 1);
358b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      // Don't add directly to `unhandled`, it needs to be sorted and the start
3593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // of this new interval might be after intervals already in the list.
3603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      AddSorted(&unhandled, split);
3613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
3623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // Nothing to do, we won't allocate a register for this value.
3633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
3643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
365b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // Don't add directly to `unhandled`, temp or safepoint intervals
366b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // for this instruction may have been added, and those can be
367b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // processed first.
368b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    AddSorted(&unhandled, current);
3693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
370a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
371a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
37231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayclass AllRangesIterator : public ValueObject {
37331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray public:
37431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  explicit AllRangesIterator(LiveInterval* interval)
37531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      : current_interval_(interval),
37631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_(interval->GetFirstRange()) {}
37731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
37831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  bool Done() const { return current_interval_ == nullptr; }
37931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* CurrentRange() const { return current_range_; }
38031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* CurrentInterval() const { return current_interval_; }
38131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
38231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  void Advance() {
38331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    current_range_ = current_range_->GetNext();
38431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    if (current_range_ == nullptr) {
38531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      current_interval_ = current_interval_->GetNextSibling();
38631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current_interval_ != nullptr) {
38731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_ = current_interval_->GetFirstRange();
38831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
38931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
39031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
39131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
39231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray private:
39331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* current_interval_;
39431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* current_range_;
39531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
39631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
39731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray};
39831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
39986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
40086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // To simplify unit testing, we eagerly create the array of intervals, and
40186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // call the helper method.
40286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  GrowableArray<LiveInterval*> intervals(allocator_, 0);
40386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
40486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
40586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
40686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      intervals.Add(instruction->GetLiveInterval());
40786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
40886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
40986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
410102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  if (processing_core_registers_) {
411102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
412102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      LiveInterval* fixed = physical_core_register_intervals_.Get(i);
413102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      if (fixed != nullptr) {
414102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        intervals.Add(fixed);
415102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      }
416102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
417102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  } else {
418102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
419102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
420102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      if (fixed != nullptr) {
421102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        intervals.Add(fixed);
422102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      }
42386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
42486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
42586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
4263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0, e = temp_intervals_.Size(); i < e; ++i) {
4273946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* temp = temp_intervals_.Get(i);
4283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, temp)) {
4293946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      intervals.Add(temp);
4303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
4313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
4323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
433776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  return ValidateIntervals(intervals, GetNumberOfSpillSlots(), reserved_out_slots_, *codegen_,
4343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                           allocator_, processing_core_registers_, log_fatal_on_failure);
43586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
43686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
43731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraybool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
43831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray                                          size_t number_of_spill_slots,
4393946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                                          size_t number_of_out_slots,
440a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          const CodeGenerator& codegen,
441a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          ArenaAllocator* allocator,
442a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool processing_core_registers,
443a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool log_fatal_on_failure) {
444a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t number_of_registers = processing_core_registers
445a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      ? codegen.GetNumberOfCoreRegisters()
446a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : codegen.GetNumberOfFloatingPointRegisters();
44731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  GrowableArray<ArenaBitVector*> liveness_of_values(
44831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      allocator, number_of_registers + number_of_spill_slots);
449a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
450a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Allocate a bit vector per register. A live interval that has a register
451a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // allocated will populate the associated bit vector based on its live ranges.
45231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
45331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true));
454a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
455a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
45631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0, e = intervals.Size(); i < e; ++i) {
45731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) {
45831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      LiveInterval* current = it.CurrentInterval();
45986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* defined_by = current->GetParent()->GetDefinedBy();
46086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->GetParent()->HasSpillSlot()
46186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray           // Parameters have their own stack slot.
46286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray           && !(defined_by != nullptr && defined_by->IsParameterValue())) {
4633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers
4643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            + current->GetParent()->GetSpillSlot() / kVRegSize
4653946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            - number_of_out_slots);
46631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
46731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_spill_slot->IsBitSet(j)) {
46831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            if (log_fatal_on_failure) {
46931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              std::ostringstream message;
47031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              message << "Spill slot conflict at " << j;
47131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              LOG(FATAL) << message.str();
47231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            } else {
47331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              return false;
47431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            }
47531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          } else {
47631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_spill_slot->SetBit(j);
47731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          }
47831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        }
479a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
48031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
48131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current->HasRegister()) {
48231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister());
48331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
48431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_register->IsBitSet(j)) {
485829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray            if (current->IsUsingInputRegister() && current->CanUseInputRegister()) {
486829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray              continue;
487829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray            }
488a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            if (log_fatal_on_failure) {
489a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              std::ostringstream message;
4903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "Register conflict at " << j << " ";
4913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              if (defined_by != nullptr) {
4923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                message << "(" << defined_by->DebugName() << ")";
4933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              }
4943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "for ";
495a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              if (processing_core_registers) {
496a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpCoreRegister(message, current->GetRegister());
497a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              } else {
498a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpFloatingPointRegister(message, current->GetRegister());
499a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              }
500a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              LOG(FATAL) << message.str();
501a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            } else {
502a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              return false;
503a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            }
504a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          } else {
50531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_register->SetBit(j);
506a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          }
507a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        }
50831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
50931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
510a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
511a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
512a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
513a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
51486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
515a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  interval->Dump(stream);
516a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << ": ";
517a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (interval->HasRegister()) {
518102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (interval->IsFloatingPoint()) {
51986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
520102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
521102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      codegen_->DumpCoreRegister(stream, interval->GetRegister());
522a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
523a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
524a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    stream << "spilled";
525a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
526a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << std::endl;
527a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
528a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
529296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yangvoid RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
530296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "inactive: " << std::endl;
531296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < inactive_.Size(); i ++) {
532296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, inactive_.Get(i));
533296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
534296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "active: " << std::endl;
535296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < active_.Size(); i ++) {
536296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, active_.Get(i));
537296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
538296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "unhandled: " << std::endl;
539296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  auto unhandled = (unhandled_ != nullptr) ?
540296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      unhandled_ : &unhandled_core_intervals_;
541296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < unhandled->Size(); i ++) {
542296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, unhandled->Get(i));
543296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
544296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "handled: " << std::endl;
545296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < handled_.Size(); i ++) {
546296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, handled_.Get(i));
547296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
548296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang}
549296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
550a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// By the book implementation of a linear scan register allocator.
551a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::LinearScan() {
5523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  while (!unhandled_->IsEmpty()) {
553a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (1) Remove interval with the lowest start position from unhandled.
5543946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = unhandled_->Pop();
5553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    DCHECK(!current->IsFixed() && !current->HasSpillSlot());
556c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart());
557840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsLowInterval() || unhandled_->Peek()->IsHighInterval());
55887d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray
559a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t position = current->GetStart();
560a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
561296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Remember the inactive_ size here since the ones moved to inactive_ from
562296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // active_ below shouldn't need to be re-checked.
563296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    size_t inactive_intervals_to_handle = inactive_.Size();
564296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
565a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (2) Remove currently active intervals that are dead at this position.
566a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move active intervals that have a lifetime hole at this position
567a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     to inactive.
568a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0; i < active_.Size(); ++i) {
569a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = active_.Get(i);
570a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
571a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
572a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
573a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
574a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (!interval->Covers(position)) {
575a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
576a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
577a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Add(interval);
578a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
579a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
580a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
581a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (3) Remove currently inactive intervals that are dead at this position.
582a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move inactive intervals that cover this position to active.
583296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    for (size_t i = 0; i < inactive_intervals_to_handle; ++i) {
584a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = inactive_.Get(i);
585296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK(interval->GetStart() < position || interval->IsFixed());
586a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
587a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
588a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
589296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        --inactive_intervals_to_handle;
590a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
591a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (interval->Covers(position)) {
592a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
593a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
594296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        --inactive_intervals_to_handle;
595a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Add(interval);
596a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
597a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
598a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
599acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    if (current->IsSlowPathSafepoint()) {
600acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Synthesized interval to record the maximum number of live registers
601acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // at safepoints. No need to allocate a register for it.
602f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      if (processing_core_registers_) {
603f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_core_registers_ =
604f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell          std::max(maximum_number_of_live_core_registers_, active_.Size());
605f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      } else {
606f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_fp_registers_ =
607f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell          std::max(maximum_number_of_live_fp_registers_, active_.Size());
608f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      }
609acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() > current->GetStart());
610acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      continue;
611acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    }
612acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray
613840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
614840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(!current->HasRegister());
615840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // Allocating the low part was unsucessful. The splitted interval for the high part
616840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // will be handled next (it is in the `unhandled_` list).
617840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
618840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
619840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
620a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (4) Try to find an available register.
621a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    bool success = TryAllocateFreeReg(current);
622a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
623a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (5) If no register could be found, we need to spill.
624a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (!success) {
625a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      success = AllocateBlockedReg(current);
626a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
627a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
628a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (6) If the interval had a register allocated, add it to the list of active
629a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     intervals.
630a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (success) {
631988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      codegen_->AddAllocatedRegister(processing_core_registers_
632988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          ? Location::RegisterLocation(current->GetRegister())
633988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          : Location::FpuRegisterLocation(current->GetRegister()));
634a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      active_.Add(current);
635840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
636840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
637840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
638a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
639a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
640a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
641a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
642829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffraystatic void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) {
643829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  DCHECK(!interval->IsHighInterval());
644829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // Note that the same instruction may occur multiple times in the input list,
645829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // so `free_until` may have changed already.
646829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  if (interval->IsDeadAt(position)) {
647829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // Set the register to be free. Note that inactive intervals might later
648829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // update this.
649829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    free_until[interval->GetRegister()] = kMaxLifetimePosition;
650829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (interval->HasHighInterval()) {
651829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      DCHECK(interval->GetHighInterval()->IsDeadAt(position));
652829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition;
653829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
654829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  } else if (!interval->Covers(position)) {
655829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // The interval becomes inactive at `defined_by`. We make its register
656829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // available only until the next use strictly after `defined_by`.
657829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    free_until[interval->GetRegister()] = interval->FirstUseAfter(position);
658829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (interval->HasHighInterval()) {
659829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      DCHECK(!interval->GetHighInterval()->Covers(position));
660829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()];
661829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
662829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  }
663829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray}
664829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray
665a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find a free register. If multiple are found, pick the register that
666a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// is free the longest.
667a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
668a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* free_until = registers_array_;
669a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
670a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers to be free.
671a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
672a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    free_until[i] = kMaxLifetimePosition;
673a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
674a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
675296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  // For each active interval, set its register to not free.
676296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
677296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    LiveInterval* interval = active_.Get(i);
678296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(interval->HasRegister());
679296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    free_until[interval->GetRegister()] = 0;
680296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
681296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
682829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // An interval that starts an instruction (that is, it is not split), may
683829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // re-use the registers used by the inputs of that instruciton, based on the
684829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // location summary.
685829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  HInstruction* defined_by = current->GetDefinedBy();
686829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  if (defined_by != nullptr && !current->IsSplit()) {
687829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    LocationSummary* locations = defined_by->GetLocations();
688829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) {
689829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      for (HInputIterator it(defined_by); !it.Done(); it.Advance()) {
690829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // Take the last interval of the input. It is the location of that interval
691829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // that will be used at `defined_by`.
692829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        LiveInterval* interval = it.Current()->GetLiveInterval()->GetLastSibling();
693829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // Note that interval may have not been processed yet.
694829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // TODO: Handle non-split intervals last in the work list.
695829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        if (interval->HasRegister() && interval->SameRegisterKind(*current)) {
696829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // The input must be live until the end of `defined_by`, to comply to
697829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // the linear scan algorithm. So we use `defined_by`'s end lifetime
698829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // position to check whether the input is dead or is inactive after
699829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // `defined_by`.
700829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          DCHECK(interval->Covers(defined_by->GetLifetimePosition()));
701829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          size_t position = defined_by->GetLifetimePosition() + 1;
702829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          FreeIfNotCoverAt(interval, position, free_until);
703829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        }
704829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      }
705829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
706829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  }
707829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray
708a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, set its register to be free until
709a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // the next intersection with `current`.
710a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
711a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
712296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
713296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
714296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
715296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
716296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
717296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
718296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
719296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
720296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
721296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
722296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
723a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
724296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (free_until[inactive->GetRegister()] == 0) {
725296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Already used by some active interval. No need to intersect.
726296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
727296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
728a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
729a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (next_intersection != kNoLifetime) {
730aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray      free_until[inactive->GetRegister()] =
731aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray          std::min(free_until[inactive->GetRegister()], next_intersection);
732a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
733a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
734a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7356c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
7363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasRegister()) {
7373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Some instructions have a fixed register output.
7383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    reg = current->GetRegister();
739840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (free_until[reg] == 0) {
740840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(current->IsHighInterval());
741840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // AllocateBlockedReg will spill the holder of the register.
742840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      return false;
743840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
7443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
745840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
74601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    int hint = current->FindFirstRegisterHint(free_until);
74701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    if (hint != kNoRegister) {
74801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      DCHECK(!IsBlocked(hint));
74901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      reg = hint;
750840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (current->IsLowInterval()) {
7516c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      reg = FindAvailableRegisterPair(free_until, current->GetStart());
75201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    } else {
753840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = FindAvailableRegister(free_until);
754a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
755a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
756a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7576c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
758a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // If we could not find a register, we need to spill.
759840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (free_until[reg] == 0) {
760840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return false;
761840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
762840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
763234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (current->IsLowInterval()) {
764234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    // If the high register of this interval is not available, we need to spill.
765234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    int high_reg = current->GetHighInterval()->GetRegister();
766234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (high_reg == kNoRegister) {
767234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      high_reg = GetHighForLowRegister(reg);
768234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    }
769234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (free_until[high_reg] == 0) {
770234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      return false;
771234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    }
772a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
773a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
774a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  current->SetRegister(reg);
775a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (!current->IsDeadAt(free_until[reg])) {
776a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the register is only available for a subset of live ranges
777a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // covered by `current`, split `current` at the position where
778a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // the register is not available anymore.
779a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* split = Split(current, free_until[reg]);
780a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(split != nullptr);
7813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    AddSorted(unhandled_, split);
782a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
783a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
784a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
785a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
786a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::IsBlocked(int reg) const {
787102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return processing_core_registers_
788102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? blocked_core_registers_[reg]
789102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : blocked_fp_registers_[reg];
790a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
791a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7926c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffrayint RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const {
7936c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
794840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Pick the register pair that is used the last.
795840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
796840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(i)) continue;
797840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (!IsLowRegister(i)) continue;
798840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int high_register = GetHighForLowRegister(i);
799840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(high_register)) continue;
800840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int existing_high_register = GetHighForLowRegister(reg);
8016c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if ((reg == kNoRegister) || (next_use[i] >= next_use[reg]
802840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                        && next_use[high_register] >= next_use[existing_high_register])) {
803840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
804840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition
805840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          && next_use[high_register] == kMaxLifetimePosition) {
806840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        break;
807840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
8086c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) {
8096c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If one of the current register is known to be unavailable, just unconditionally
8106c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // try a new one.
8116c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      reg = i;
812840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
813840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
814840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
815840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
816840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
817840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffrayint RegisterAllocator::FindAvailableRegister(size_t* next_use) const {
8186c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
819840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Pick the register that is used the last.
820840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
821840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(i)) continue;
8226c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (reg == kNoRegister || next_use[i] > next_use[reg]) {
823840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
824840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition) break;
825840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
826840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
827840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
828840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
829840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
830234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraybool RegisterAllocator::TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
831234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                                 size_t first_register_use,
832234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                                 size_t* next_use) {
8336c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
8346c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    LiveInterval* active = active_.Get(i);
8356c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    DCHECK(active->HasRegister());
836234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (active->IsFixed()) continue;
837234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (active->IsHighInterval()) continue;
838234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (first_register_use > next_use[active->GetRegister()]) continue;
839234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
8406c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // Split the first interval found.
841234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (!active->IsLowInterval() || IsLowOfUnalignedPairInterval(active)) {
8426c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* split = Split(active, position);
8436c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      active_.DeleteAt(i);
8446c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      if (split != active) {
8456c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        handled_.Add(active);
8466c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      }
8476c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
8486c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      return true;
8496c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
8506c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  }
8516c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  return false;
8526c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray}
8536c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
854a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval
855a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register
856a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead.
857a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
858a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t first_register_use = current->FirstRegisterUse();
859412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  if (first_register_use == kNoLifetime) {
86031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
861a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
862a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
863a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
864a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers as not being used.
865a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* next_use = registers_array_;
866a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
867a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    next_use[i] = kMaxLifetimePosition;
868a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
869a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
870a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each active interval, find the next use of its register after the
871a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
872a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
873a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* active = active_.Get(i);
874a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(active->HasRegister());
87586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (active->IsFixed()) {
87686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      next_use[active->GetRegister()] = current->GetStart();
87786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
87886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      size_t use = active->FirstRegisterUseAfter(current->GetStart());
87986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (use != kNoLifetime) {
88086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[active->GetRegister()] = use;
88186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
882a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
883a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
884a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
885a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, find the next use of its register after the
886a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
887a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
888a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
889296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
890296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
891296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
892296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
893296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
894296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
895296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
896296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
897296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
898296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
899a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
90086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
90186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_intersection != kNoLifetime) {
90286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (inactive->IsFixed()) {
90386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[inactive->GetRegister()] =
90486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            std::min(next_intersection, next_use[inactive->GetRegister()]);
90586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else {
90686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t use = inactive->FirstRegisterUseAfter(current->GetStart());
90786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (use != kNoLifetime) {
90886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
90986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
91086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
911a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
912a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
913a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9146c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
9156c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  bool should_spill = false;
916840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (current->HasRegister()) {
917840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(current->IsHighInterval());
918840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = current->GetRegister();
9196c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // When allocating the low part, we made sure the high register was available.
9206c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    DCHECK_LT(first_register_use, next_use[reg]);
921840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (current->IsLowInterval()) {
922234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    reg = FindAvailableRegisterPair(next_use, first_register_use);
9236c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // We should spill if both registers are not available.
9246c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    should_spill = (first_register_use >= next_use[reg])
9256c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || (first_register_use >= next_use[GetHighForLowRegister(reg)]);
926840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
927840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
928840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = FindAvailableRegister(next_use);
9296c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    should_spill = (first_register_use >= next_use[reg]);
930a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
931a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9326c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
9336c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  if (should_spill) {
934840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
935234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    bool is_allocation_at_use_site = (current->GetStart() >= (first_register_use - 1));
9366c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (current->IsLowInterval()
9376c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        && is_allocation_at_use_site
938234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        && TrySplitNonPairOrUnalignedPairIntervalAt(current->GetStart(),
939234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                    first_register_use,
940234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                    next_use)) {
9416c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If we're allocating a register for `current` because the instruction at
9426c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // that position requires it, but we think we should spill, then there are
943234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // non-pair intervals or unaligned pair intervals blocking the allocation.
944234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // We split the first interval found, and put ourselves first in the
945234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // `unhandled_` list.
9466c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* existing = unhandled_->Peek();
9476c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK(existing->IsHighInterval());
9486c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK_EQ(existing->GetLowInterval(), current);
9496c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      unhandled_->Add(current);
9506c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else {
9516c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If the first use of that instruction is after the last use of the found
9526c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // register, we split this interval just before its first register use.
9536c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AllocateSpillSlotFor(current);
9546c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* split = Split(current, first_register_use - 1);
955234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      if (current == split) {
956234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DumpInterval(std::cerr, current);
957234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DumpAllIntervals(std::cerr);
958234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // This situation has the potential to infinite loop, so we make it a non-debug CHECK.
959234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        CHECK(false) << "There is not enough registers available for "
960234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << split->GetParent()->GetDefinedBy()->DebugName() << " "
961234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << split->GetParent()->GetDefinedBy()->GetId()
962234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << " at " << first_register_use - 1;
963234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      }
9646c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
9656c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
966a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
967a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
968a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Use this register and spill the active and inactives interval that
969a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // have that register.
970a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    current->SetRegister(reg);
971a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
972a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0, e = active_.Size(); i < e; ++i) {
973a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* active = active_.Get(i);
974a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (active->GetRegister() == reg) {
97586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(!active->IsFixed());
976a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        LiveInterval* split = Split(active, current->GetStart());
977a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.DeleteAt(i);
978dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        if (split != active) {
979dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray          handled_.Add(active);
980dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        }
9813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        AddSorted(unhandled_, split);
9826c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
9836c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        if (active->IsLowInterval() || active->IsHighInterval()) {
9846c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray          LiveInterval* other_half = active->IsLowInterval()
9856c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              ? active->GetHighInterval()
9866c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              : active->GetLowInterval();
9876c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray          // We also need to remove the other half from the list of actives.
9886c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray          bool found = false;
9896c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray          for (size_t j = 0; j < active_.Size(); ++j) {
9906c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray            if (active_.Get(j) == other_half) {
9916c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              found = true;
9926c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              active_.DeleteAt(j);
9936c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              handled_.Add(other_half);
9946c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              break;
9956c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray            }
9966c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray          }
9976c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray          DCHECK(found);
9986c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        }
999a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        break;
1000a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
1001a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1002a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1003296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
1004a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* inactive = inactive_.Get(i);
1005a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (inactive->GetRegister() == reg) {
1006296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        if (!current->IsSplit() && !inactive->IsFixed()) {
1007296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Neither current nor inactive are fixed.
1008296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Thanks to SSA, a non-split interval starting in a hole of an
1009296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // inactive interval should never intersect with that inactive interval.
1010296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Only if it's not fixed though, because fixed intervals don't come from SSA.
1011296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
1012296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          continue;
1013296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        }
101486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t next_intersection = inactive->FirstIntersectionWith(current);
101586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (next_intersection != kNoLifetime) {
101686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          if (inactive->IsFixed()) {
101786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            LiveInterval* split = Split(current, next_intersection);
1018dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            DCHECK_NE(split, current);
10193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
102086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          } else {
1021dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // Split at the start of `current`, which will lead to splitting
1022dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // at the end of the lifetime hole of `inactive`.
1023dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            LiveInterval* split = Split(inactive, current->GetStart());
1024dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // If it's inactive, it must start before the current interval.
1025dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            DCHECK_NE(split, inactive);
102686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            inactive_.DeleteAt(i);
1027296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang            --i;
1028296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang            --e;
102986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            handled_.Add(inactive);
10303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
10316c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
10326c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray            if (inactive->IsLowInterval() || inactive->IsHighInterval()) {
10336c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              LiveInterval* other_half = inactive->IsLowInterval()
10346c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray                  ? inactive->GetHighInterval()
10356c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray                  : inactive->GetLowInterval();
10366c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
10376c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              // We also need to remove the other half from the list of inactives.
10386c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              bool found = false;
10396c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              for (size_t j = 0; j < inactive_.Size(); ++j) {
10406c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray                if (inactive_.Get(j) == other_half) {
10416c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray                  found = true;
10426c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray                  inactive_.DeleteAt(j);
10436c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray                  --e;
10446c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray                  handled_.Add(other_half);
10456c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray                  break;
10466c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray                }
10476c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              }
10486c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray              DCHECK(found);
10496c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray            }
105086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          }
105186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
1052a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
1053a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1054a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1055a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return true;
1056a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1057a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1058a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) {
1060c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray  DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
106186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t insert_at = 0;
10623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = array->Size(); i > 0; --i) {
10633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = array->Get(i - 1);
10646c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // High intervals must be processed right after their low equivalent.
10656c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (current->StartsAfter(interval) && !current->IsHighInterval()) {
106686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      insert_at = i;
1067a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      break;
1068acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
1069acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Ensure the slow path interval is the last to be processed at its location: we want the
1070acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // interval to know all live registers at this location.
1071acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      DCHECK(i == 1 || array->Get(i - 2)->StartsAfter(current));
1072acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      insert_at = i;
1073acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      break;
1074a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1075a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1076840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
10773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  array->InsertAt(insert_at, interval);
1078840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Insert the high interval before the low, to ensure the low is processed before.
1079840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->HasHighInterval()) {
1080840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    array->InsertAt(insert_at, interval->GetHighInterval());
1081840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (interval->HasLowInterval()) {
1082840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    array->InsertAt(insert_at + 1, interval->GetLowInterval());
1083840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1084a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1085a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1086a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
1087840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  DCHECK_GE(position, interval->GetStart());
1088a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(!interval->IsDeadAt(position));
1089a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (position == interval->GetStart()) {
1090a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Spill slot will be allocated when handling `interval` again.
1091a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    interval->ClearRegister();
1092840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1093840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetHighInterval()->ClearRegister();
1094840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1095840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetLowInterval()->ClearRegister();
1096840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1097a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return interval;
1098a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
1099a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* new_interval = interval->SplitAt(position);
1100840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1101840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
1102840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetHighInterval(high);
1103840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetLowInterval(new_interval);
1104840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1105840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
1106840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetLowInterval(low);
1107840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      low->SetHighInterval(new_interval);
1108840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1109a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return new_interval;
1110a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1111a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1112a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
111331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
1114840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->IsHighInterval()) {
1115840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    // The low interval will contain the spill slot.
1116840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return;
1117840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1118840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
111931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* parent = interval->GetParent();
112031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
112131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // An instruction gets a spill slot for its entire lifetime. If the parent
112231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // of this interval already has a spill slot, there is nothing to do.
112331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  if (parent->HasSpillSlot()) {
112431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    return;
112531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
112631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
112786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* defined_by = parent->GetDefinedBy();
112886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (defined_by->IsParameterValue()) {
112986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Parameters have their own stack slot.
113086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
113186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
113286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
113386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
113496f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  if (defined_by->IsConstant()) {
113596f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    // Constants don't need a spill slot.
113696f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    return;
113796f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  }
113896f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray
113931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* last_sibling = interval;
114031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  while (last_sibling->GetNextSibling() != nullptr) {
114131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    last_sibling = last_sibling->GetNextSibling();
114231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
114331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  size_t end = last_sibling->GetEnd();
114431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1145776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  GrowableArray<size_t>* spill_slots = nullptr;
1146776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  switch (interval->GetType()) {
1147776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimDouble:
1148776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &double_spill_slots_;
1149776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1150776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimLong:
1151776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &long_spill_slots_;
1152776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1153776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimFloat:
1154776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &float_spill_slots_;
1155776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1156776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimNot:
1157776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimInt:
1158776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimChar:
1159776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimByte:
1160776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimBoolean:
1161776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimShort:
1162776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &int_spill_slots_;
1163776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1164776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimVoid:
1165776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      LOG(FATAL) << "Unexpected type for interval " << interval->GetType();
1166776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  }
1167776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray
1168412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  // Find an available spill slot.
1169412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  size_t slot = 0;
1170776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  for (size_t e = spill_slots->Size(); slot < e; ++slot) {
1171776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (spill_slots->Get(slot) <= parent->GetStart()
1172776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        && (slot == (e - 1) || spill_slots->Get(slot + 1) <= parent->GetStart())) {
1173412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray      break;
1174412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    }
1175412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
1176412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
117701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  if (parent->NeedsTwoSpillSlots()) {
1178776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (slot == spill_slots->Size()) {
11793c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
1180776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
1181776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
1182776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    } else if (slot == spill_slots->Size() - 1) {
1183776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
1184776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
11853c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
1186776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
1187776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot + 1, end);
118831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
118931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  } else {
1190776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (slot == spill_slots->Size()) {
11913c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
1192776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
11933c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
1194776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
11953c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    }
119631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
119731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1198776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // Note that the exact spill slot location will be computed when we resolve,
1199776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // that is when we know the number of spill slots for each type.
1200776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  parent->SetSpillSlot(slot);
120186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
120286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
12032a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffraystatic bool IsValidDestination(Location destination) {
1204102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return destination.IsRegister()
12056c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || destination.IsRegisterPair()
1206102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsFpuRegister()
1207840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      || destination.IsFpuRegisterPair()
1208102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsStackSlot()
1209102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsDoubleStackSlot();
12102a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray}
12112a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray
1212234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddMove(HParallelMove* move,
1213234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location source,
1214234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location destination,
1215234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                HInstruction* instruction,
1216234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Primitive::Type type) const {
1217234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (type == Primitive::kPrimLong
1218234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && codegen_->ShouldSplitLongMoves()
1219234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // The parallel move resolver knows how to deal with long constants.
1220234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && !source.IsConstant()) {
1221234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->AddMove(source.ToLow(), destination.ToLow(), instruction);
1222234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->AddMove(source.ToHigh(), destination.ToHigh(), nullptr);
1223234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  } else {
1224234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->AddMove(source, destination, instruction);
1225234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  }
1226234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray}
1227234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1228234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* input,
1229234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                        HInstruction* user,
123086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
123186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
123286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
123386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1234476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  DCHECK(!user->IsPhi());
123586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1236740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  HInstruction* previous = user->GetPrevious();
123786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = nullptr;
123886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (previous == nullptr
1239476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain      || !previous->IsParallelMove()
12408e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray      || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
124186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
12428e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    move->SetLifetimePosition(user->GetLifetimePosition());
1243740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    user->GetBlock()->InsertInstructionBefore(move, user);
124486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
124586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
124686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
12478e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
1248234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, nullptr, input->GetType());
124986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
125086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
125146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionStart(size_t position) {
125246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 0;
125346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
125446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
125546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionEnd(size_t position) {
125646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 1;
125746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
125846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
125986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position,
1260740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                             HInstruction* instruction,
126186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location source,
126286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location destination) const {
12636c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
126486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
126586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
126686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
126786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
126846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  if (at == nullptr) {
126946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    if (IsInstructionStart(position)) {
127046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Block boundary, don't do anything the connection of split siblings will handle it.
127146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      return;
127246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    } else {
127346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Move must happen before the first instruction of the block.
127446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      at = liveness_.GetInstructionFromPosition((position + 1) / 2);
12755976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // Note that parallel moves may have already been inserted, so we explicitly
12765976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // ask for the first instruction of the block: `GetInstructionFromPosition` does
1277234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // not contain the `HParallelMove` instructions.
12785976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      at = at->GetBlock()->GetFirstInstruction();
1279234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1280234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      if (at->GetLifetimePosition() < position) {
1281234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // We may insert moves for split siblings and phi spills at the beginning of the block.
1282234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // Since this is a different lifetime position, we need to go to the next instruction.
1283234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DCHECK(at->IsParallelMove());
1284234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        at = at->GetNext();
1285234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      }
1286234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
12875976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      if (at->GetLifetimePosition() != position) {
12885976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_GT(at->GetLifetimePosition(), position);
128946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move = new (allocator_) HParallelMove(allocator_);
129046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move->SetLifetimePosition(position);
129146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        at->GetBlock()->InsertInstructionBefore(move, at);
12925976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      } else {
12935976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK(at->IsParallelMove());
12945976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        move = at->AsParallelMove();
129546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      }
129646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    }
129746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  } else if (IsInstructionEnd(position)) {
129886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen after the instruction.
129986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(!at->IsControlFlow());
130086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = at->GetNext()->AsParallelMove();
1301e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // This is a parallel move for connecting siblings in a same block. We need to
1302e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // differentiate it with moves for connecting blocks, and input moves.
13038e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    if (move == nullptr || move->GetLifetimePosition() > position) {
130486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
130586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
130686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
130786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
130886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
130986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen before the instruction.
131086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* previous = at->GetPrevious();
1311740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    if (previous == nullptr
1312740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || !previous->IsParallelMove()
1313740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || previous->GetLifetimePosition() != position) {
1314740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // If the previous is a parallel move, then its position must be lower
1315740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // than the given `position`: it was added just after the non-parallel
1316740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // move instruction that precedes `instruction`.
1317740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      DCHECK(previous == nullptr
1318740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || !previous->IsParallelMove()
1319740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || previous->GetLifetimePosition() < position);
132086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
132186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
132286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at);
132386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
132486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = previous->AsParallelMove();
132586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
132686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
132701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), position);
1328234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
132986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
133086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
133186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
1332740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                   HInstruction* instruction,
133386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location source,
133486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location destination) const {
13356c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
133686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
133786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
133886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK_EQ(block->GetSuccessors().Size(), 1u);
133986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* last = block->GetLastInstruction();
1340360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // We insert moves at exit for phi predecessors and connecting blocks.
1341360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // A block ending with an if cannot branch to a block with phis because
1342360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // we do not allow critical edges. It can also not connect
1343360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // a split interval between two blocks: the move has to happen in the successor.
1344360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  DCHECK(!last->IsIf());
134586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* previous = last->GetPrevious();
134686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
1347e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1348e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and output moves.
13495976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray  size_t position = last->GetLifetimePosition();
1350740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  if (previous == nullptr || !previous->IsParallelMove()
13515976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      || previous->AsParallelMove()->GetLifetimePosition() != position) {
135286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
13535976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    move->SetLifetimePosition(position);
135486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, last);
135586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
135686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
135786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1358234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
135986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
136086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
136186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
1362740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                    HInstruction* instruction,
136386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location source,
136486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location destination) const {
13656c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
136686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
136786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
136886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* first = block->GetFirstInstruction();
136986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = first->AsParallelMove();
1370234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  size_t position = block->GetLifetimeStart();
1371e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1372e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and input moves.
1373234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
137486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1375234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->SetLifetimePosition(position);
137686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, first);
137786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1378234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
137986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
138086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
138186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
138286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
138386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
13846c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
138586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
138686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1387476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  if (instruction->IsPhi()) {
1388740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
138986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
139086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
139186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1392e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  size_t position = instruction->GetLifetimePosition() + 1;
139386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = instruction->GetNext()->AsParallelMove();
1394e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for moving the output of an instruction. We need
1395e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // to differentiate with input moves, moves for connecting siblings in a
1396e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // and moves for connecting blocks.
1397e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
139886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1399e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(position);
140086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
140186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1402234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
140386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
140486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
140586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
140686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
140786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (current->HasSpillSlot() && current->HasRegister()) {
140886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // We spill eagerly, so move must be at definition.
140986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertMoveAfter(interval->GetDefinedBy(),
1410840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                    interval->ToLocation(),
141101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                    interval->NeedsTwoSpillSlots()
1412412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1413412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
141486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
141586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  UsePosition* use = current->GetFirstUse();
14169a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil  size_t safepoint_index = safepoints_.Size();
141786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
141886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Walk over all siblings, updating locations of use positions, and
141986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // connecting them when they are adjacent.
142086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  do {
142101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
142286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
142386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Walk over all uses covered by this interval, and update the location
142486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // information.
142586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    while (use != nullptr && use->GetPosition() <= current->GetEnd()) {
14263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      LocationSummary* locations = use->GetUser()->GetLocations();
14273946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      if (use->GetIsEnvironment()) {
14283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetEnvironmentAt(use->GetInputIndex(), source);
14293946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      } else {
143086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        Location expected_location = locations->InAt(use->GetInputIndex());
143171fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe        // The expected (actual) location may be invalid in case the input is unused. Currently
143271fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe        // this only happens for intrinsics.
143371fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe        if (expected_location.IsValid()) {
143471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe          if (expected_location.IsUnallocated()) {
143571fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe            locations->SetInAt(use->GetInputIndex(), source);
143671fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe          } else if (!expected_location.IsConstant()) {
1437234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray            AddInputMoveFor(interval->GetDefinedBy(), use->GetUser(), source, expected_location);
143871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe          }
143971fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe        } else {
144071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe          DCHECK(use->GetUser()->IsInvoke());
144171fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe          DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
144286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
144386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
144486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      use = use->GetNext();
144586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
144686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
144786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // If the next interval starts just after this one, and has a register,
144886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // insert a move.
144986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* next_sibling = current->GetNextSibling();
145086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_sibling != nullptr
145186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && next_sibling->HasRegister()
145286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && current->GetEnd() == next_sibling->GetStart()) {
145301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      Location destination = next_sibling->ToLocation();
1454740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
145586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
14563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
14573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // At each safepoint, we record stack and register information.
14589a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil    // We iterate backwards to test safepoints in ascending order of positions,
14599a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil    // which is what LiveInterval::Covers is optimized for.
1460df45205204125727fa71b17b3f6bb3d8eb9bc20cDavid Brazdil    for (; safepoint_index > 0; --safepoint_index) {
1461df45205204125727fa71b17b3f6bb3d8eb9bc20cDavid Brazdil      HInstruction* safepoint = safepoints_.Get(safepoint_index - 1);
14623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      size_t position = safepoint->GetLifetimePosition();
14639a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil
14649a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil      // Test that safepoints are ordered in the optimal way.
1465df45205204125727fa71b17b3f6bb3d8eb9bc20cDavid Brazdil      DCHECK(safepoint_index == safepoints_.Size()
1466df45205204125727fa71b17b3f6bb3d8eb9bc20cDavid Brazdil             || safepoints_.Get(safepoint_index)->GetLifetimePosition() <= position);
14679a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil
14689a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil      if (current->IsDeadAt(position)) {
14699a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil        break;
14709a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil      } else if (!current->Covers(position)) {
1471b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        continue;
14729a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil      } else if (interval->GetStart() == position) {
1473b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        // The safepoint is for this instruction, so the location of the instruction
1474b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        // does not need to be saved.
1475b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        continue;
1476b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      }
14773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
14789a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil      LocationSummary* locations = safepoint->GetLocations();
14793bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
14803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
14813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
14823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
14833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      switch (source.GetKind()) {
14843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kRegister: {
14853bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray          locations->AddLiveRegister(source);
1486988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) {
1487988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray            DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1488988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_core_registers_ +
1489988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_fp_registers_);
1490988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          }
14913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          if (current->GetType() == Primitive::kPrimNot) {
149256b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray            locations->SetRegisterBit(source.reg());
14933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          }
14943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
14953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
1496102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        case Location::kFpuRegister: {
1497102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          locations->AddLiveRegister(source);
1498102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          break;
1499102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        }
15006c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
15016c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        case Location::kRegisterPair:
1502840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        case Location::kFpuRegisterPair: {
1503840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToLow());
1504840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToHigh());
1505840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          break;
1506840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
15073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kStackSlot:  // Fall-through
15083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kDoubleStackSlot:  // Fall-through
15093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kConstant: {
15103946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          // Nothing to do.
15113946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
15123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
15133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        default: {
15143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          LOG(FATAL) << "Unexpected location for object";
15153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
15163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
15173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
151886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = next_sibling;
151986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } while (current != nullptr);
152086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(use == nullptr);
152186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
152286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
152386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
152486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* from,
152586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* to) const {
152686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval->GetNextSibling() == nullptr) {
152786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Nothing to connect. The whole range was allocated to the same location.
152886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
152986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
153086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
153146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  // Intervals end at the lifetime end of a block. The decrement by one
153246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  // ensures the `Cover` call will return true.
153386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t from_position = from->GetLifetimeEnd() - 1;
153446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  size_t to_position = to->GetLifetimeStart();
153586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
153686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* destination = nullptr;
153786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* source = nullptr;
153886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
153986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
154086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
154186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Check the intervals that cover `from` and `to`.
154286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  while ((current != nullptr) && (source == nullptr || destination == nullptr)) {
154386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (current->Covers(from_position)) {
154486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source == nullptr);
154586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      source = current;
154686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
154786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (current->Covers(to_position)) {
154886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(destination == nullptr);
154986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      destination = current;
155086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
155186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
155286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = current->GetNextSibling();
155386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
155486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
155586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (destination == source) {
155686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Interval was not split.
155786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
155886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
155986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
15608ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray  DCHECK(destination != nullptr && source != nullptr);
15618ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray
156286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!destination->HasRegister()) {
156386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Values are eagerly spilled. Spill slot already contains appropriate value.
156486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
156586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
156686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
156786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
156886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // we need to put the moves at the entry of `to`.
156986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (from->GetSuccessors().Size() == 1) {
1570740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtExitOf(from,
1571740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                               interval->GetParent()->GetDefinedBy(),
157201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               source->ToLocation(),
157301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               destination->ToLocation());
157486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
157586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK_EQ(to->GetPredecessors().Size(), 1u);
1576740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(to,
1577740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                interval->GetParent()->GetDefinedBy(),
157801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                source->ToLocation(),
157901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                destination->ToLocation());
158086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
158186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
158286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
158386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() {
1584776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(),
15854c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_core_registers_,
15864c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_fp_registers_,
15874c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     reserved_out_slots_,
15884c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     liveness_.GetLinearOrder());
158986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
159086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Adjust the Out Location of instructions.
159186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
159286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
159386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
159486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* current = instruction->GetLiveInterval();
159586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LocationSummary* locations = instruction->GetLocations();
159686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location location = locations->Out();
1597476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain    if (instruction->IsParameterValue()) {
159886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Now that we know the frame size, adjust the parameter's location.
159986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.IsStackSlot()) {
160086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
160186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1602f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
160386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (location.IsDoubleStackSlot()) {
160486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
160586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1606f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
160786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (current->HasSpillSlot()) {
160886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
160986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1610776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    } else if (current->HasSpillSlot()) {
1611776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // Adjust the stack slot, now that we know the number of them for each type.
1612776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // The way this implementation lays out the stack is the following:
1613776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [parameter slots     ]
1614776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [double spill slots  ]
1615776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [long spill slots    ]
1616776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [float spill slots   ]
1617776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [int/ref values      ]
1618776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [maximum out values  ] (number of arguments for calls)
1619776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [art method          ].
1620776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      uint32_t slot = current->GetSpillSlot();
1621776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      switch (current->GetType()) {
1622776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimDouble:
1623776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += long_spill_slots_.Size();
1624776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1625776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimLong:
1626776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += float_spill_slots_.Size();
1627776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1628776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimFloat:
1629776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += int_spill_slots_.Size();
1630776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1631776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimNot:
1632776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimInt:
1633776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimChar:
1634776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimByte:
1635776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimBoolean:
1636776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimShort:
1637776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += reserved_out_slots_;
1638776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          break;
1639776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimVoid:
1640776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          LOG(FATAL) << "Unexpected type for interval " << current->GetType();
1641776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      }
1642776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      current->SetSpillSlot(slot * kVRegSize);
164386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
164486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
164501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
164686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
164786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (location.IsUnallocated()) {
164886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.GetPolicy() == Location::kSameAsFirstInput) {
1649d0d4852847432368b090c184d6639e573538dccfCalin Juravle        if (locations->InAt(0).IsUnallocated()) {
1650d0d4852847432368b090c184d6639e573538dccfCalin Juravle          locations->SetInAt(0, source);
1651d0d4852847432368b090c184d6639e573538dccfCalin Juravle        } else {
1652d0d4852847432368b090c184d6639e573538dccfCalin Juravle          DCHECK(locations->InAt(0).Equals(source));
1653d0d4852847432368b090c184d6639e573538dccfCalin Juravle        }
165486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1655829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      locations->UpdateOut(source);
165686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
165786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source.Equals(location));
165886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
165986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
166086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
166186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Connect siblings.
166286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
166386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
166486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ConnectSiblings(instruction->GetLiveInterval());
166586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
166686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
166786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve non-linear control flow across branches. Order does not matter.
166886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
166986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* block = it.Current();
167086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    BitVector* live = liveness_.GetLiveInSet(*block);
167186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (uint32_t idx : live->Indexes()) {
167286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
167386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      LiveInterval* interval = current->GetLiveInterval();
167486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
167586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
167686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
167786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
167886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
167986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
168086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve phi inputs. Order does not matter.
168186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
168286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* current = it.Current();
1683277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1684277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      HInstruction* phi = inst_it.Current();
168586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
168686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HBasicBlock* predecessor = current->GetPredecessors().Get(i);
168786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
168886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HInstruction* input = phi->InputAt(i);
168901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location source = input->GetLiveInterval()->GetLocationAt(
169001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray            predecessor->GetLifetimeEnd() - 1);
169101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location destination = phi->GetLiveInterval()->ToLocation();
1692234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        InsertParallelMoveAtExitOf(predecessor, phi, source, destination);
169386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
169486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
169586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
16963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
16973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Assign temp locations.
16983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < temp_intervals_.Size(); ++i) {
16993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* temp = temp_intervals_.Get(i);
1700840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (temp->IsHighInterval()) {
1701840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // High intervals can be skipped, they are already handled by the low interval.
1702840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
1703840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
170401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    HInstruction* at = liveness_.GetTempUser(temp);
1705f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray    size_t temp_index = liveness_.GetTempIndex(temp);
170601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    LocationSummary* locations = at->GetLocations();
17075368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    switch (temp->GetType()) {
17085368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimInt:
1709f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray        locations->SetTempAt(temp_index, Location::RegisterLocation(temp->GetRegister()));
17105368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
17115368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
17125368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimDouble:
1713840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1714840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          Location location = Location::FpuRegisterPairLocation(
1715840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray              temp->GetRegister(), temp->GetHighInterval()->GetRegister());
1716f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          locations->SetTempAt(temp_index, location);
1717840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        } else {
1718f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          locations->SetTempAt(temp_index, Location::FpuRegisterLocation(temp->GetRegister()));
1719840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
17205368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
17215368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
17225368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      default:
17235368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        LOG(FATAL) << "Unexpected type for temporary location "
17245368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                   << temp->GetType();
17255368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    }
17263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
172731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}
172831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1729a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}  // namespace art
1730