register_allocator.cc revision 5b168deeae2c5a8a566ce5c140741f0e2227af21
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);
2165368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          interval->AddRange(position, position + 1);
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);
2255368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          interval->AddRange(position, position + 1);
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
8545b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffraybool RegisterAllocator::PotentiallyRemoveOtherHalf(LiveInterval* interval,
8555b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray                                                   GrowableArray<LiveInterval*>* intervals,
8565b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray                                                   size_t index) {
8575b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  if (interval->IsLowInterval()) {
8585b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    DCHECK_EQ(intervals->Get(index), interval->GetHighInterval());
8595b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    intervals->DeleteAt(index);
8605b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    return true;
8615b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  } else if (interval->IsHighInterval()) {
8625b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    DCHECK_GT(index, 0u);
8635b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    DCHECK_EQ(intervals->Get(index - 1), interval->GetLowInterval());
8645b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    intervals->DeleteAt(index - 1);
8655b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    return true;
8665b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  } else {
8675b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    return false;
8685b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray  }
8695b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray}
8705b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray
871a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval
872a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register
873a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead.
874a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
875a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t first_register_use = current->FirstRegisterUse();
876412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  if (first_register_use == kNoLifetime) {
87731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
878a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
879a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
880a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
881a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers as not being used.
882a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* next_use = registers_array_;
883a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
884a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    next_use[i] = kMaxLifetimePosition;
885a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
886a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
887a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each active interval, find the next use of its register after the
888a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
889a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
890a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* active = active_.Get(i);
891a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(active->HasRegister());
89286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (active->IsFixed()) {
89386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      next_use[active->GetRegister()] = current->GetStart();
89486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
89586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      size_t use = active->FirstRegisterUseAfter(current->GetStart());
89686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (use != kNoLifetime) {
89786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[active->GetRegister()] = use;
89886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
899a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
900a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
901a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
902a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, find the next use of its register after the
903a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
904a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
905a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
906296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
907296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
908296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
909296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
910296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
911296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
912296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
913296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
914296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
915296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
916a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
91786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
91886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_intersection != kNoLifetime) {
91986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (inactive->IsFixed()) {
92086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[inactive->GetRegister()] =
92186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            std::min(next_intersection, next_use[inactive->GetRegister()]);
92286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else {
92386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t use = inactive->FirstRegisterUseAfter(current->GetStart());
92486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (use != kNoLifetime) {
92586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
92686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
92786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
928a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
929a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
930a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9316c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
9326c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  bool should_spill = false;
933840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (current->HasRegister()) {
934840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(current->IsHighInterval());
935840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = current->GetRegister();
9366c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // When allocating the low part, we made sure the high register was available.
9376c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    DCHECK_LT(first_register_use, next_use[reg]);
938840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (current->IsLowInterval()) {
939234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    reg = FindAvailableRegisterPair(next_use, first_register_use);
9406c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // We should spill if both registers are not available.
9416c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    should_spill = (first_register_use >= next_use[reg])
9426c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || (first_register_use >= next_use[GetHighForLowRegister(reg)]);
943840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
944840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
945840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = FindAvailableRegister(next_use);
9466c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    should_spill = (first_register_use >= next_use[reg]);
947a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
948a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9496c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
9506c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  if (should_spill) {
951840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
952234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    bool is_allocation_at_use_site = (current->GetStart() >= (first_register_use - 1));
9536c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (current->IsLowInterval()
9546c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        && is_allocation_at_use_site
955234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        && TrySplitNonPairOrUnalignedPairIntervalAt(current->GetStart(),
956234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                    first_register_use,
957234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                    next_use)) {
9586c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If we're allocating a register for `current` because the instruction at
9596c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // that position requires it, but we think we should spill, then there are
960234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // non-pair intervals or unaligned pair intervals blocking the allocation.
961234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // We split the first interval found, and put ourselves first in the
962234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // `unhandled_` list.
9636c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* existing = unhandled_->Peek();
9646c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK(existing->IsHighInterval());
9656c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK_EQ(existing->GetLowInterval(), current);
9666c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      unhandled_->Add(current);
9676c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else {
9686c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If the first use of that instruction is after the last use of the found
9696c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // register, we split this interval just before its first register use.
9706c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AllocateSpillSlotFor(current);
9716c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* split = Split(current, first_register_use - 1);
972234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      if (current == split) {
973234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DumpInterval(std::cerr, current);
974234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DumpAllIntervals(std::cerr);
975234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // This situation has the potential to infinite loop, so we make it a non-debug CHECK.
976234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        CHECK(false) << "There is not enough registers available for "
977234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << split->GetParent()->GetDefinedBy()->DebugName() << " "
978234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << split->GetParent()->GetDefinedBy()->GetId()
979234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray          << " at " << first_register_use - 1;
980234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      }
9816c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
9826c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
983a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
984a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
985a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Use this register and spill the active and inactives interval that
986a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // have that register.
987a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    current->SetRegister(reg);
988a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
989a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0, e = active_.Size(); i < e; ++i) {
990a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* active = active_.Get(i);
991a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (active->GetRegister() == reg) {
99286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(!active->IsFixed());
993a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        LiveInterval* split = Split(active, current->GetStart());
994dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        if (split != active) {
995dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray          handled_.Add(active);
996dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        }
9975b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray        active_.DeleteAt(i);
9985b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray        PotentiallyRemoveOtherHalf(active, &active_, i);
9993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        AddSorted(unhandled_, split);
1000a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        break;
1001a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
1002a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1003a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10045b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray    for (size_t i = 0; i < inactive_.Size(); ++i) {
1005a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* inactive = inactive_.Get(i);
1006a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (inactive->GetRegister() == reg) {
1007296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        if (!current->IsSplit() && !inactive->IsFixed()) {
1008296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Neither current nor inactive are fixed.
1009296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Thanks to SSA, a non-split interval starting in a hole of an
1010296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // inactive interval should never intersect with that inactive interval.
1011296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Only if it's not fixed though, because fixed intervals don't come from SSA.
1012296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
1013296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          continue;
1014296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        }
101586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t next_intersection = inactive->FirstIntersectionWith(current);
101686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (next_intersection != kNoLifetime) {
101786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          if (inactive->IsFixed()) {
101886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            LiveInterval* split = Split(current, next_intersection);
1019dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            DCHECK_NE(split, current);
10203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
102186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          } else {
1022dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // Split at the start of `current`, which will lead to splitting
1023dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // at the end of the lifetime hole of `inactive`.
1024dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            LiveInterval* split = Split(inactive, current->GetStart());
1025dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // If it's inactive, it must start before the current interval.
1026dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            DCHECK_NE(split, inactive);
102786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            inactive_.DeleteAt(i);
10285b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            if (PotentiallyRemoveOtherHalf(inactive, &inactive_, i) && inactive->IsHighInterval()) {
10295b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray              // We have removed an entry prior to `inactive`. So we need to decrement.
10305b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray              --i;
10315b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            }
10325b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            // Decrement because we have removed `inactive` from the list.
1033296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang            --i;
103486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            handled_.Add(inactive);
10353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
103686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          }
103786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
1038a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
1039a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1040a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1041a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return true;
1042a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1043a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1044a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) {
1046c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray  DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
104786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t insert_at = 0;
10483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = array->Size(); i > 0; --i) {
10493946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = array->Get(i - 1);
10506c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // High intervals must be processed right after their low equivalent.
10516c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (current->StartsAfter(interval) && !current->IsHighInterval()) {
105286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      insert_at = i;
1053a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      break;
1054acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
1055acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Ensure the slow path interval is the last to be processed at its location: we want the
1056acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // interval to know all live registers at this location.
1057acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      DCHECK(i == 1 || array->Get(i - 2)->StartsAfter(current));
1058acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      insert_at = i;
1059acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      break;
1060a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1061a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1062840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
10633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  array->InsertAt(insert_at, interval);
1064840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Insert the high interval before the low, to ensure the low is processed before.
1065840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->HasHighInterval()) {
1066840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    array->InsertAt(insert_at, interval->GetHighInterval());
1067840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (interval->HasLowInterval()) {
1068840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    array->InsertAt(insert_at + 1, interval->GetLowInterval());
1069840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1070a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1071a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1072a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
1073840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  DCHECK_GE(position, interval->GetStart());
1074a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(!interval->IsDeadAt(position));
1075a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (position == interval->GetStart()) {
1076a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Spill slot will be allocated when handling `interval` again.
1077a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    interval->ClearRegister();
1078840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1079840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetHighInterval()->ClearRegister();
1080840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1081840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetLowInterval()->ClearRegister();
1082840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1083a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return interval;
1084a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
1085a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* new_interval = interval->SplitAt(position);
1086840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1087840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
1088840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetHighInterval(high);
1089840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetLowInterval(new_interval);
1090840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1091840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
1092840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetLowInterval(low);
1093840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      low->SetHighInterval(new_interval);
1094840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1095a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return new_interval;
1096a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1097a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1098a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
109931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
1100840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->IsHighInterval()) {
1101840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    // The low interval will contain the spill slot.
1102840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return;
1103840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1104840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
110531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* parent = interval->GetParent();
110631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
110731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // An instruction gets a spill slot for its entire lifetime. If the parent
110831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // of this interval already has a spill slot, there is nothing to do.
110931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  if (parent->HasSpillSlot()) {
111031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    return;
111131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
111231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
111386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* defined_by = parent->GetDefinedBy();
111486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (defined_by->IsParameterValue()) {
111586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Parameters have their own stack slot.
111686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
111786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
111886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
111986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
112096f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  if (defined_by->IsConstant()) {
112196f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    // Constants don't need a spill slot.
112296f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    return;
112396f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  }
112496f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray
112531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* last_sibling = interval;
112631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  while (last_sibling->GetNextSibling() != nullptr) {
112731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    last_sibling = last_sibling->GetNextSibling();
112831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
112931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  size_t end = last_sibling->GetEnd();
113031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1131776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  GrowableArray<size_t>* spill_slots = nullptr;
1132776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  switch (interval->GetType()) {
1133776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimDouble:
1134776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &double_spill_slots_;
1135776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1136776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimLong:
1137776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &long_spill_slots_;
1138776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1139776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimFloat:
1140776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &float_spill_slots_;
1141776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1142776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimNot:
1143776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimInt:
1144776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimChar:
1145776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimByte:
1146776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimBoolean:
1147776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimShort:
1148776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &int_spill_slots_;
1149776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1150776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimVoid:
1151776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      LOG(FATAL) << "Unexpected type for interval " << interval->GetType();
1152776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  }
1153776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray
1154412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  // Find an available spill slot.
1155412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  size_t slot = 0;
1156776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  for (size_t e = spill_slots->Size(); slot < e; ++slot) {
1157776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (spill_slots->Get(slot) <= parent->GetStart()
1158776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        && (slot == (e - 1) || spill_slots->Get(slot + 1) <= parent->GetStart())) {
1159412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray      break;
1160412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    }
1161412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
1162412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
116301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  if (parent->NeedsTwoSpillSlots()) {
1164776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (slot == spill_slots->Size()) {
11653c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
1166776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
1167776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
1168776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    } else if (slot == spill_slots->Size() - 1) {
1169776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
1170776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
11713c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
1172776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
1173776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot + 1, end);
117431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
117531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  } else {
1176776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    if (slot == spill_slots->Size()) {
11773c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
1178776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Add(end);
11793c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
1180776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots->Put(slot, end);
11813c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    }
118231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
118331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1184776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // Note that the exact spill slot location will be computed when we resolve,
1185776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // that is when we know the number of spill slots for each type.
1186776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  parent->SetSpillSlot(slot);
118786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
118886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
11892a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffraystatic bool IsValidDestination(Location destination) {
1190102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return destination.IsRegister()
11916c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || destination.IsRegisterPair()
1192102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsFpuRegister()
1193840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      || destination.IsFpuRegisterPair()
1194102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsStackSlot()
1195102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsDoubleStackSlot();
11962a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray}
11972a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray
1198234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddMove(HParallelMove* move,
1199234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location source,
1200234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location destination,
1201234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                HInstruction* instruction,
1202234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Primitive::Type type) const {
1203234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (type == Primitive::kPrimLong
1204234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && codegen_->ShouldSplitLongMoves()
1205234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // The parallel move resolver knows how to deal with long constants.
1206234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && !source.IsConstant()) {
1207234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->AddMove(source.ToLow(), destination.ToLow(), instruction);
1208234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->AddMove(source.ToHigh(), destination.ToHigh(), nullptr);
1209234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  } else {
1210234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->AddMove(source, destination, instruction);
1211234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  }
1212234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray}
1213234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1214234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* input,
1215234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                        HInstruction* user,
121686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
121786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
121886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
121986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1220476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  DCHECK(!user->IsPhi());
122186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1222740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  HInstruction* previous = user->GetPrevious();
122386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = nullptr;
122486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (previous == nullptr
1225476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain      || !previous->IsParallelMove()
12268e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray      || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
122786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
12288e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    move->SetLifetimePosition(user->GetLifetimePosition());
1229740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    user->GetBlock()->InsertInstructionBefore(move, user);
123086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
123186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
123286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
12338e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
1234234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, nullptr, input->GetType());
123586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
123686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
123746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionStart(size_t position) {
123846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 0;
123946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
124046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
124146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionEnd(size_t position) {
124246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 1;
124346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
124446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
124586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position,
1246740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                             HInstruction* instruction,
124786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location source,
124886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location destination) const {
12496c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
125086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
125186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
125286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
125386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
125446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  if (at == nullptr) {
125546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    if (IsInstructionStart(position)) {
125646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Block boundary, don't do anything the connection of split siblings will handle it.
125746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      return;
125846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    } else {
125946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Move must happen before the first instruction of the block.
126046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      at = liveness_.GetInstructionFromPosition((position + 1) / 2);
12615976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // Note that parallel moves may have already been inserted, so we explicitly
12625976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // ask for the first instruction of the block: `GetInstructionFromPosition` does
1263234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // not contain the `HParallelMove` instructions.
12645976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      at = at->GetBlock()->GetFirstInstruction();
1265234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1266234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      if (at->GetLifetimePosition() < position) {
1267234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // We may insert moves for split siblings and phi spills at the beginning of the block.
1268234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // Since this is a different lifetime position, we need to go to the next instruction.
1269234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DCHECK(at->IsParallelMove());
1270234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        at = at->GetNext();
1271234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      }
1272234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
12735976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      if (at->GetLifetimePosition() != position) {
12745976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_GT(at->GetLifetimePosition(), position);
127546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move = new (allocator_) HParallelMove(allocator_);
127646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move->SetLifetimePosition(position);
127746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        at->GetBlock()->InsertInstructionBefore(move, at);
12785976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      } else {
12795976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK(at->IsParallelMove());
12805976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        move = at->AsParallelMove();
128146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      }
128246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    }
128346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  } else if (IsInstructionEnd(position)) {
128486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen after the instruction.
128586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(!at->IsControlFlow());
128686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = at->GetNext()->AsParallelMove();
1287e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // This is a parallel move for connecting siblings in a same block. We need to
1288e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // differentiate it with moves for connecting blocks, and input moves.
12898e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    if (move == nullptr || move->GetLifetimePosition() > position) {
129086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
129186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
129286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
129386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
129486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
129586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen before the instruction.
129686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* previous = at->GetPrevious();
1297740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    if (previous == nullptr
1298740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || !previous->IsParallelMove()
1299740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || previous->GetLifetimePosition() != position) {
1300740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // If the previous is a parallel move, then its position must be lower
1301740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // than the given `position`: it was added just after the non-parallel
1302740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // move instruction that precedes `instruction`.
1303740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      DCHECK(previous == nullptr
1304740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || !previous->IsParallelMove()
1305740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || previous->GetLifetimePosition() < position);
130686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
130786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
130886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at);
130986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
131086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = previous->AsParallelMove();
131186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
131286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
131301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), position);
1314234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
131586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
131686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
131786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
1318740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                   HInstruction* instruction,
131986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location source,
132086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location destination) const {
13216c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
132286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
132386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
132486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK_EQ(block->GetSuccessors().Size(), 1u);
132586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* last = block->GetLastInstruction();
1326360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // We insert moves at exit for phi predecessors and connecting blocks.
1327360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // A block ending with an if cannot branch to a block with phis because
1328360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // we do not allow critical edges. It can also not connect
1329360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // a split interval between two blocks: the move has to happen in the successor.
1330360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  DCHECK(!last->IsIf());
133186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* previous = last->GetPrevious();
133286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
1333e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1334e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and output moves.
13355976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray  size_t position = last->GetLifetimePosition();
1336740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  if (previous == nullptr || !previous->IsParallelMove()
13375976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      || previous->AsParallelMove()->GetLifetimePosition() != position) {
133886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
13395976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    move->SetLifetimePosition(position);
134086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, last);
134186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
134286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
134386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1344234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
134586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
134686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
134786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
1348740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                    HInstruction* instruction,
134986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location source,
135086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location destination) const {
13516c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
135286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
135386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
135486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* first = block->GetFirstInstruction();
135586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = first->AsParallelMove();
1356234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  size_t position = block->GetLifetimeStart();
1357e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1358e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and input moves.
1359234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
136086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1361234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->SetLifetimePosition(position);
136286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, first);
136386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1364234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
136586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
136686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
136786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
136886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
136986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
13706c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
137186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
137286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1373476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  if (instruction->IsPhi()) {
1374740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
137586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
137686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
137786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1378e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  size_t position = instruction->GetLifetimePosition() + 1;
137986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = instruction->GetNext()->AsParallelMove();
1380e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for moving the output of an instruction. We need
1381e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // to differentiate with input moves, moves for connecting siblings in a
1382e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // and moves for connecting blocks.
1383e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
138486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1385e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(position);
138686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
138786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1388234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
138986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
139086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
139186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
139286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
139386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (current->HasSpillSlot() && current->HasRegister()) {
139486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // We spill eagerly, so move must be at definition.
139586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertMoveAfter(interval->GetDefinedBy(),
1396840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                    interval->ToLocation(),
139701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                    interval->NeedsTwoSpillSlots()
1398412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1399412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
140086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
140186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  UsePosition* use = current->GetFirstUse();
14029a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil  size_t safepoint_index = safepoints_.Size();
140386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
140486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Walk over all siblings, updating locations of use positions, and
140586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // connecting them when they are adjacent.
140686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  do {
140701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
140886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
140986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Walk over all uses covered by this interval, and update the location
141086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // information.
141186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    while (use != nullptr && use->GetPosition() <= current->GetEnd()) {
14123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      LocationSummary* locations = use->GetUser()->GetLocations();
14133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      if (use->GetIsEnvironment()) {
14143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetEnvironmentAt(use->GetInputIndex(), source);
14153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      } else {
141686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        Location expected_location = locations->InAt(use->GetInputIndex());
141771fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe        // The expected (actual) location may be invalid in case the input is unused. Currently
141871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe        // this only happens for intrinsics.
141971fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe        if (expected_location.IsValid()) {
142071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe          if (expected_location.IsUnallocated()) {
142171fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe            locations->SetInAt(use->GetInputIndex(), source);
142271fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe          } else if (!expected_location.IsConstant()) {
1423234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray            AddInputMoveFor(interval->GetDefinedBy(), use->GetUser(), source, expected_location);
142471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe          }
142571fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe        } else {
142671fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe          DCHECK(use->GetUser()->IsInvoke());
142771fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe          DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
142886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
142986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
143086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      use = use->GetNext();
143186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
143286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
143386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // If the next interval starts just after this one, and has a register,
143486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // insert a move.
143586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* next_sibling = current->GetNextSibling();
143686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_sibling != nullptr
143786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && next_sibling->HasRegister()
143886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && current->GetEnd() == next_sibling->GetStart()) {
143901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      Location destination = next_sibling->ToLocation();
1440740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
144186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
14423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
14433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // At each safepoint, we record stack and register information.
14449a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil    // We iterate backwards to test safepoints in ascending order of positions,
14459a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil    // which is what LiveInterval::Covers is optimized for.
1446df45205204125727fa71b17b3f6bb3d8eb9bc20cDavid Brazdil    for (; safepoint_index > 0; --safepoint_index) {
1447df45205204125727fa71b17b3f6bb3d8eb9bc20cDavid Brazdil      HInstruction* safepoint = safepoints_.Get(safepoint_index - 1);
14483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      size_t position = safepoint->GetLifetimePosition();
14499a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil
14509a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil      // Test that safepoints are ordered in the optimal way.
1451df45205204125727fa71b17b3f6bb3d8eb9bc20cDavid Brazdil      DCHECK(safepoint_index == safepoints_.Size()
1452df45205204125727fa71b17b3f6bb3d8eb9bc20cDavid Brazdil             || safepoints_.Get(safepoint_index)->GetLifetimePosition() <= position);
14539a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil
14549a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil      if (current->IsDeadAt(position)) {
14559a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil        break;
14569a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil      } else if (!current->Covers(position)) {
1457b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        continue;
14589a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil      } else if (interval->GetStart() == position) {
1459b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        // The safepoint is for this instruction, so the location of the instruction
1460b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        // does not need to be saved.
1461b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        continue;
1462b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      }
14633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
14649a9ab61ca425298f161872ed8efcf0a89b158ab2David Brazdil      LocationSummary* locations = safepoint->GetLocations();
14653bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
14663946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
14673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
14683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
14693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      switch (source.GetKind()) {
14703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kRegister: {
14713bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray          locations->AddLiveRegister(source);
1472988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) {
1473988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray            DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1474988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_core_registers_ +
1475988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_fp_registers_);
1476988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          }
14773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          if (current->GetType() == Primitive::kPrimNot) {
147856b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray            locations->SetRegisterBit(source.reg());
14793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          }
14803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
14813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
1482102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        case Location::kFpuRegister: {
1483102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          locations->AddLiveRegister(source);
1484102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          break;
1485102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        }
14866c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
14876c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        case Location::kRegisterPair:
1488840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        case Location::kFpuRegisterPair: {
1489840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToLow());
1490840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToHigh());
1491840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          break;
1492840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
14933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kStackSlot:  // Fall-through
14943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kDoubleStackSlot:  // Fall-through
14953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kConstant: {
14963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          // Nothing to do.
14973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
14983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
14993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        default: {
15003946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          LOG(FATAL) << "Unexpected location for object";
15013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
15023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
15033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
150486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = next_sibling;
150586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } while (current != nullptr);
150686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(use == nullptr);
150786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
150886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
150986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
151086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* from,
151186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* to) const {
151286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval->GetNextSibling() == nullptr) {
151386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Nothing to connect. The whole range was allocated to the same location.
151486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
151586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
151686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
151746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  // Intervals end at the lifetime end of a block. The decrement by one
151846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  // ensures the `Cover` call will return true.
151986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t from_position = from->GetLifetimeEnd() - 1;
152046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  size_t to_position = to->GetLifetimeStart();
152186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
152286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* destination = nullptr;
152386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* source = nullptr;
152486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
152586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
152686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
152786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Check the intervals that cover `from` and `to`.
152886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  while ((current != nullptr) && (source == nullptr || destination == nullptr)) {
152986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (current->Covers(from_position)) {
153086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source == nullptr);
153186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      source = current;
153286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
153386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (current->Covers(to_position)) {
153486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(destination == nullptr);
153586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      destination = current;
153686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
153786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
153886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = current->GetNextSibling();
153986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
154086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
154186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (destination == source) {
154286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Interval was not split.
154386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
154486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
154586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
15468ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray  DCHECK(destination != nullptr && source != nullptr);
15478ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray
154886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!destination->HasRegister()) {
154986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Values are eagerly spilled. Spill slot already contains appropriate value.
155086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
155186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
155286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
155386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
155486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // we need to put the moves at the entry of `to`.
155586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (from->GetSuccessors().Size() == 1) {
1556740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtExitOf(from,
1557740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                               interval->GetParent()->GetDefinedBy(),
155801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               source->ToLocation(),
155901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               destination->ToLocation());
156086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
156186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK_EQ(to->GetPredecessors().Size(), 1u);
1562740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(to,
1563740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                interval->GetParent()->GetDefinedBy(),
156401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                source->ToLocation(),
156501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                destination->ToLocation());
156686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
156786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
156886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
156986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() {
1570776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(),
15714c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_core_registers_,
15724c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_fp_registers_,
15734c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     reserved_out_slots_,
15744c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     liveness_.GetLinearOrder());
157586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
157686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Adjust the Out Location of instructions.
157786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
157886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
157986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
158086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* current = instruction->GetLiveInterval();
158186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LocationSummary* locations = instruction->GetLocations();
158286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location location = locations->Out();
1583476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain    if (instruction->IsParameterValue()) {
158486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Now that we know the frame size, adjust the parameter's location.
158586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.IsStackSlot()) {
158686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
158786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1588f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
158986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (location.IsDoubleStackSlot()) {
159086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
159186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1592f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
159386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (current->HasSpillSlot()) {
159486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
159586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1596776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    } else if (current->HasSpillSlot()) {
1597776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // Adjust the stack slot, now that we know the number of them for each type.
1598776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // The way this implementation lays out the stack is the following:
1599776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [parameter slots     ]
1600776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [double spill slots  ]
1601776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [long spill slots    ]
1602776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [float spill slots   ]
1603776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [int/ref values      ]
1604776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [maximum out values  ] (number of arguments for calls)
1605776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // [art method          ].
1606776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      uint32_t slot = current->GetSpillSlot();
1607776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      switch (current->GetType()) {
1608776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimDouble:
1609776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += long_spill_slots_.Size();
1610776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1611776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimLong:
1612776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += float_spill_slots_.Size();
1613776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1614776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimFloat:
1615776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += int_spill_slots_.Size();
1616776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1617776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimNot:
1618776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimInt:
1619776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimChar:
1620776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimByte:
1621776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimBoolean:
1622776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimShort:
1623776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += reserved_out_slots_;
1624776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          break;
1625776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimVoid:
1626776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          LOG(FATAL) << "Unexpected type for interval " << current->GetType();
1627776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      }
1628776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      current->SetSpillSlot(slot * kVRegSize);
162986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
163086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
163101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
163286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
163386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (location.IsUnallocated()) {
163486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.GetPolicy() == Location::kSameAsFirstInput) {
1635d0d4852847432368b090c184d6639e573538dccfCalin Juravle        if (locations->InAt(0).IsUnallocated()) {
1636d0d4852847432368b090c184d6639e573538dccfCalin Juravle          locations->SetInAt(0, source);
1637d0d4852847432368b090c184d6639e573538dccfCalin Juravle        } else {
1638d0d4852847432368b090c184d6639e573538dccfCalin Juravle          DCHECK(locations->InAt(0).Equals(source));
1639d0d4852847432368b090c184d6639e573538dccfCalin Juravle        }
164086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1641829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      locations->UpdateOut(source);
164286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
164386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source.Equals(location));
164486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
164586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
164686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
164786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Connect siblings.
164886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
164986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
165086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ConnectSiblings(instruction->GetLiveInterval());
165186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
165286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
165386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve non-linear control flow across branches. Order does not matter.
165486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
165586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* block = it.Current();
165686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    BitVector* live = liveness_.GetLiveInSet(*block);
165786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (uint32_t idx : live->Indexes()) {
165886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
165986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      LiveInterval* interval = current->GetLiveInterval();
166086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
166186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
166286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
166386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
166486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
166586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
166686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve phi inputs. Order does not matter.
166786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
166886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* current = it.Current();
1669277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1670277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      HInstruction* phi = inst_it.Current();
167186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
167286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HBasicBlock* predecessor = current->GetPredecessors().Get(i);
167386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
167486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HInstruction* input = phi->InputAt(i);
167501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location source = input->GetLiveInterval()->GetLocationAt(
167601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray            predecessor->GetLifetimeEnd() - 1);
167701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location destination = phi->GetLiveInterval()->ToLocation();
1678234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        InsertParallelMoveAtExitOf(predecessor, phi, source, destination);
167986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
168086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
168186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
16823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
16833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Assign temp locations.
16843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  HInstruction* current = nullptr;
16853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  size_t temp_index = 0;
16863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < temp_intervals_.Size(); ++i) {
16873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* temp = temp_intervals_.Get(i);
1688840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (temp->IsHighInterval()) {
1689840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // High intervals can be skipped, they are already handled by the low interval.
1690840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
1691840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
169201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    HInstruction* at = liveness_.GetTempUser(temp);
169301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    if (at != current) {
16943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      temp_index = 0;
169501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      current = at;
16963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
169701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    LocationSummary* locations = at->GetLocations();
16985368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    switch (temp->GetType()) {
16995368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimInt:
17005368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        locations->SetTempAt(
17015368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain            temp_index++, Location::RegisterLocation(temp->GetRegister()));
17025368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
17035368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
17045368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimDouble:
1705840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1706840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          Location location = Location::FpuRegisterPairLocation(
1707840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray              temp->GetRegister(), temp->GetHighInterval()->GetRegister());
1708840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->SetTempAt(temp_index++, location);
1709840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        } else {
1710840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->SetTempAt(
1711840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray              temp_index++, Location::FpuRegisterLocation(temp->GetRegister()));
1712840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
17135368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
17145368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
17155368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      default:
17165368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        LOG(FATAL) << "Unexpected type for temporary location "
17175368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                   << temp->GetType();
17185368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    }
17193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
172031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}
172131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1722a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}  // namespace art
1723