register_allocator.cc revision dd8f887e81b894bc8075d8bacdb223747b6a8018
1a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray/*
2a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *
4a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * you may not use this file except in compliance with the License.
6a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * You may obtain a copy of the License at
7a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *
8a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *
10a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * See the License for the specific language governing permissions and
14a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * limitations under the License.
15a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray */
16a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
17a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
18a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
19c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#include <sstream>
20c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
21e77493c7217efdd1a0ecef521a6845a13da0305bIan Rogers#include "base/bit_vector-inl.h"
22a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "code_generator.h"
23a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "ssa_liveness_analysis.h"
24a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
25a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraynamespace art {
26a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
27a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraystatic constexpr size_t kMaxLifetimePosition = -1;
2831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraystatic constexpr size_t kDefaultNumberOfSpillSlots = 4;
29a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
30840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray// For simplicity, we implement register pairs as (reg, reg + 1).
31840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray// Note that this is a requirement for double registers on ARM, since we
32840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray// allocate SRegister.
33840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffraystatic int GetHighForLowRegister(int reg) { return reg + 1; }
34840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffraystatic bool IsLowRegister(int reg) { return (reg & 1) == 0; }
35840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
3686dbb9a12119273039ce272b41c809fa548b37b6Nicolas GeoffrayRegisterAllocator::RegisterAllocator(ArenaAllocator* allocator,
3786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                     CodeGenerator* codegen,
3886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                     const SsaLivenessAnalysis& liveness)
39a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : allocator_(allocator),
40a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        codegen_(codegen),
4186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        liveness_(liveness),
423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        unhandled_core_intervals_(allocator, 0),
433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        unhandled_fp_intervals_(allocator, 0),
443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        unhandled_(nullptr),
45a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_(allocator, 0),
46a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_(allocator, 0),
47a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_(allocator, 0),
48102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        physical_core_register_intervals_(allocator, codegen->GetNumberOfCoreRegisters()),
49102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        physical_fp_register_intervals_(allocator, codegen->GetNumberOfFloatingPointRegisters()),
503946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        temp_intervals_(allocator, 4),
5131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        spill_slots_(allocator, kDefaultNumberOfSpillSlots),
523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        safepoints_(allocator, 0),
53a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        processing_core_registers_(false),
54a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        number_of_registers_(-1),
55a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        registers_array_(nullptr),
56102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        blocked_core_registers_(codegen->GetBlockedCoreRegisters()),
57102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        blocked_fp_registers_(codegen->GetBlockedFloatingPointRegisters()),
583bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray        reserved_out_slots_(0),
593bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray        maximum_number_of_live_registers_(0) {
6071175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray  codegen->SetupBlockedRegisters();
61102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  physical_core_register_intervals_.SetSize(codegen->GetNumberOfCoreRegisters());
62102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  physical_fp_register_intervals_.SetSize(codegen->GetNumberOfFloatingPointRegisters());
633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Always reserve for the current method and the graph's max out registers.
643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // TODO: compute it instead.
653946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  reserved_out_slots_ = 1 + codegen->GetGraph()->GetMaximumNumberOfOutVRegs();
66a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
67a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
6886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph,
6986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                InstructionSet instruction_set) {
7086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!Supports(instruction_set)) {
7186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return false;
7286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
733e69f16ae3fddfd24f4f0e29deb106d564ab296cAlexandre Rames  if (instruction_set == kArm64 || instruction_set == kX86_64) {
743e69f16ae3fddfd24f4f0e29deb106d564ab296cAlexandre Rames    return true;
753e69f16ae3fddfd24f4f0e29deb106d564ab296cAlexandre Rames  }
7686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = graph.GetBlocks().Size(); i < e; ++i) {
7786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (HInstructionIterator it(graph.GetBlocks().Get(i)->GetInstructions());
7886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray         !it.Done();
7986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray         it.Advance()) {
8086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = it.Current();
81840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (instruction_set == kX86) {
82840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        if (current->GetType() == Primitive::kPrimLong ||
83840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            current->GetType() == Primitive::kPrimFloat ||
84840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            current->GetType() == Primitive::kPrimDouble) {
85840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          return false;
86840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
87840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      } else if (instruction_set == kArm || instruction_set == kThumb2) {
88840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        if (current->GetType() == Primitive::kPrimLong) {
89840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          return false;
90840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
91102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      }
9286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
9386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
9486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  return true;
9586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
9686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
9786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) {
983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (interval == nullptr) return false;
9986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  bool is_core_register = (interval->GetType() != Primitive::kPrimDouble)
10086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      && (interval->GetType() != Primitive::kPrimFloat);
101a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return processing_core_registers == is_core_register;
102a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
103a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegisters() {
10586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  AllocateRegistersInternal();
10686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  Resolve();
10786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
10886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (kIsDebugBuild) {
10986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = true;
11086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
11186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = false;
11286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
1135976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // Check that the linear order is still correct with regards to lifetime positions.
1145976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // Since only parallel moves have been inserted during the register allocation,
1155976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // these checks are mostly for making sure these moves have been added correctly.
1165976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    size_t current_liveness = 0;
1175976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
1185976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      HBasicBlock* block = it.Current();
1195976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1205976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        HInstruction* instruction = inst_it.Current();
1215976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_LE(current_liveness, instruction->GetLifetimePosition());
1225976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        current_liveness = instruction->GetLifetimePosition();
1235976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      }
1245976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      for (HInstructionIterator inst_it(block->GetInstructions());
1255976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray           !inst_it.Done();
1265976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray           inst_it.Advance()) {
1275976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        HInstruction* instruction = inst_it.Current();
1285976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_LE(current_liveness, instruction->GetLifetimePosition()) << instruction->DebugName();
1295976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        current_liveness = instruction->GetLifetimePosition();
1305976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      }
1315976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    }
13286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
13386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
13486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
13586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::BlockRegister(Location location,
13686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                      size_t start,
137102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                                      size_t end) {
13856b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray  int reg = location.reg();
139102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  DCHECK(location.IsRegister() || location.IsFpuRegister());
140102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  LiveInterval* interval = location.IsRegister()
141102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? physical_core_register_intervals_.Get(reg)
142102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : physical_fp_register_intervals_.Get(reg);
143102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  Primitive::Type type = location.IsRegister()
144102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? Primitive::kPrimInt
145840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      : Primitive::kPrimFloat;
14686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval == nullptr) {
14786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    interval = LiveInterval::MakeFixedInterval(allocator_, reg, type);
148102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (location.IsRegister()) {
149102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      physical_core_register_intervals_.Put(reg, interval);
150102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
151102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      physical_fp_register_intervals_.Put(reg, interval);
152102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
15386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
15486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(interval->GetRegister() == reg);
15586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  interval->AddRange(start, end);
15686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
15786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
15886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegistersInternal() {
1593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Iterate post-order, to ensure the list is sorted, and the last added interval
1603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // is the one with the lowest start position.
1613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (HLinearPostOrderIterator it(liveness_); !it.Done(); it.Advance()) {
1623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    HBasicBlock* block = it.Current();
163277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
164277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe         back_it.Advance()) {
165277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      ProcessInstruction(back_it.Current());
1663946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
167277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
168277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      ProcessInstruction(inst_it.Current());
1693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
1703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
171a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1723946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  number_of_registers_ = codegen_->GetNumberOfCoreRegisters();
173a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
1743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  processing_core_registers_ = true;
1753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  unhandled_ = &unhandled_core_intervals_;
176102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
177102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    LiveInterval* fixed = physical_core_register_intervals_.Get(i);
178102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (fixed != nullptr) {
179296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is added to inactive_ instead of unhandled_.
180296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // It's also the only type of inactive interval whose start position
181296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // can be after the current interval during linear scan.
182296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is never split and never moves to unhandled_.
183102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      inactive_.Add(fixed);
184102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
185102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  }
1863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LinearScan();
187a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
188102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  size_t saved_maximum_number_of_live_registers = maximum_number_of_live_registers_;
189102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  maximum_number_of_live_registers_ = 0;
190102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray
1913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  inactive_.Reset();
1923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  active_.Reset();
1933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  handled_.Reset();
194a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters();
1963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
1973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  processing_core_registers_ = false;
1983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  unhandled_ = &unhandled_fp_intervals_;
199102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
200102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
201102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (fixed != nullptr) {
202296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is added to inactive_ instead of unhandled_.
203296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // It's also the only type of inactive interval whose start position
204296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // can be after the current interval during linear scan.
205296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is never split and never moves to unhandled_.
206102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      inactive_.Add(fixed);
207102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
208102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  }
2093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LinearScan();
210102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  maximum_number_of_live_registers_ += saved_maximum_number_of_live_registers;
2113946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray}
21286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
2133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::ProcessInstruction(HInstruction* instruction) {
2143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LocationSummary* locations = instruction->GetLocations();
2153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  size_t position = instruction->GetLifetimePosition();
2163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (locations == nullptr) return;
2183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Create synthesized intervals for temporaries.
2203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < locations->GetTempCount(); ++i) {
2213946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    Location temp = locations->GetTemp(i);
22252839d17c06175e19ca4a093fb878450d1c4310dNicolas Geoffray    if (temp.IsRegister() || temp.IsFpuRegister()) {
223102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(temp, position, position + 1);
2243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
225102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      DCHECK(temp.IsUnallocated());
2265368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      switch (temp.GetPolicy()) {
2275368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        case Location::kRequiresRegister: {
2285368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LiveInterval* interval =
2295368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain              LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt);
2305368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          temp_intervals_.Add(interval);
2315368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          interval->AddRange(position, position + 1);
2325368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          unhandled_core_intervals_.Add(interval);
2335368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          break;
2345368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        }
2355368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
2365368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        case Location::kRequiresFpuRegister: {
2375368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LiveInterval* interval =
2385368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain              LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimDouble);
2395368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          temp_intervals_.Add(interval);
2405368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          interval->AddRange(position, position + 1);
241840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
242840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            interval->AddHighInterval(true);
243840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            LiveInterval* high = interval->GetHighInterval();
244840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            temp_intervals_.Add(high);
245840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            unhandled_fp_intervals_.Add(high);
246840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          }
2475368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          unhandled_fp_intervals_.Add(interval);
2485368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          break;
2495368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        }
2505368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
2515368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        default:
2525368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LOG(FATAL) << "Unexpected policy for temporary location "
2535368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                     << temp.GetPolicy();
2545368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      }
255a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
256a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
25786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
2583bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  bool core_register = (instruction->GetType() != Primitive::kPrimDouble)
2593bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      && (instruction->GetType() != Primitive::kPrimFloat);
2603bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
2613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (locations->CanCall()) {
2623bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    if (!instruction->IsSuspendCheck()) {
2633bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      codegen_->MarkNotLeaf();
2643bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
2653946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    safepoints_.Add(instruction);
2663bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    if (locations->OnlyCallsOnSlowPath()) {
2673bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // We add a synthesized range at this position to record the live registers
2683bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // at this position. Ideally, we could just update the safepoints when locations
2693bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // are updated, but we currently need to know the full stack size before updating
2703bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // locations (because of parameters and the fact that we don't have a frame pointer).
2713bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // And knowing the full stack size requires to know the maximum number of live
2723bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // registers at calls in slow paths.
2733bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // By adding the following interval in the algorithm, we can compute this
2743bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // maximum before updating locations.
2753bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction);
276acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      interval->AddRange(position, position + 1);
27787d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray      AddSorted(&unhandled_core_intervals_, interval);
27887d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray      AddSorted(&unhandled_fp_intervals_, interval);
2793bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
2803bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  }
2813bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
2823bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  if (locations->WillCall()) {
2833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Block all registers.
2843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) {
28556b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray      BlockRegister(Location::RegisterLocation(i),
2863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                    position,
287102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                    position + 1);
288102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
289102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) {
290102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(Location::FpuRegisterLocation(i),
291102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                    position,
292102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray                    position + 1);
2933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
2943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
2953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < instruction->InputCount(); ++i) {
2973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    Location input = locations->InAt(i);
298102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (input.IsRegister() || input.IsFpuRegister()) {
299102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(input, position, position + 1);
300840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (input.IsPair()) {
301840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      BlockRegister(input.ToLow(), position, position + 1);
302840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      BlockRegister(input.ToHigh(), position, position + 1);
3033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
3043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LiveInterval* current = instruction->GetLiveInterval();
3073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current == nullptr) return;
3083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
309102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  GrowableArray<LiveInterval*>& unhandled = core_register
310102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? unhandled_core_intervals_
311102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : unhandled_fp_intervals_;
312102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray
3137690562d40878f44823d5fb03a2084cfc677ec4aNicolas Geoffray  DCHECK(unhandled.IsEmpty() || current->StartsBeforeOrAt(unhandled.Peek()));
31487d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray
315840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (codegen_->NeedsTwoRegisters(current->GetType())) {
316840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->AddHighInterval();
317840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
318840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
3193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Some instructions define their output in fixed register/stack slot. We need
3203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // to ensure we know these locations before doing register allocation. For a
3213946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // given register, we create an interval that covers these locations. The register
3223946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // will be unavailable at these locations when trying to allocate one for an
3233946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // interval.
3243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  //
3253946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // The backwards walking ensures the ranges are ordered on increasing start positions.
3263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  Location output = locations->Out();
327d0d4852847432368b090c184d6639e573538dccfCalin Juravle  if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
328d0d4852847432368b090c184d6639e573538dccfCalin Juravle    Location first = locations->InAt(0);
329d0d4852847432368b090c184d6639e573538dccfCalin Juravle    if (first.IsRegister() || first.IsFpuRegister()) {
330d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetFrom(position + 1);
331d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetRegister(first.reg());
332840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (first.IsPair()) {
333840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      current->SetFrom(position + 1);
334840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      current->SetRegister(first.low());
335840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = current->GetHighInterval();
336840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetRegister(first.high());
337840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetFrom(position + 1);
338d0d4852847432368b090c184d6639e573538dccfCalin Juravle    }
339d0d4852847432368b090c184d6639e573538dccfCalin Juravle  } else if (output.IsRegister() || output.IsFpuRegister()) {
3403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Shift the interval's start by one to account for the blocked register.
3413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetFrom(position + 1);
34256b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray    current->SetRegister(output.reg());
343102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    BlockRegister(output, position, position + 1);
344840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (output.IsPair()) {
345840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->SetFrom(position + 1);
346840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->SetRegister(output.low());
347840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    LiveInterval* high = current->GetHighInterval();
348840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    high->SetRegister(output.high());
349840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    high->SetFrom(position + 1);
350840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    BlockRegister(output.ToLow(), position, position + 1);
351840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    BlockRegister(output.ToHigh(), position, position + 1);
3523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
3533946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetSpillSlot(output.GetStackIndex());
354840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
355840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(output.IsUnallocated() || output.IsConstant());
3563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // If needed, add interval to the list of unhandled intervals.
3593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasSpillSlot() || instruction->IsConstant()) {
360c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    // Split just before first register use.
3613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    size_t first_register_use = current->FirstRegisterUse();
3623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (first_register_use != kNoLifetime) {
363c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray      LiveInterval* split = Split(current, first_register_use - 1);
364b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      // Don't add directly to `unhandled`, it needs to be sorted and the start
3653946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // of this new interval might be after intervals already in the list.
3663946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      AddSorted(&unhandled, split);
3673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
3683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // Nothing to do, we won't allocate a register for this value.
3693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
3703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
371b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // Don't add directly to `unhandled`, temp or safepoint intervals
372b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // for this instruction may have been added, and those can be
373b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // processed first.
374b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    AddSorted(&unhandled, current);
3753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
376a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
377a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
37831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayclass AllRangesIterator : public ValueObject {
37931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray public:
38031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  explicit AllRangesIterator(LiveInterval* interval)
38131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      : current_interval_(interval),
38231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_(interval->GetFirstRange()) {}
38331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
38431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  bool Done() const { return current_interval_ == nullptr; }
38531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* CurrentRange() const { return current_range_; }
38631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* CurrentInterval() const { return current_interval_; }
38731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
38831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  void Advance() {
38931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    current_range_ = current_range_->GetNext();
39031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    if (current_range_ == nullptr) {
39131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      current_interval_ = current_interval_->GetNextSibling();
39231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current_interval_ != nullptr) {
39331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_ = current_interval_->GetFirstRange();
39431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
39531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
39631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
39731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
39831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray private:
39931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* current_interval_;
40031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* current_range_;
40131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
40231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
40331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray};
40431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
40586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
40686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // To simplify unit testing, we eagerly create the array of intervals, and
40786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // call the helper method.
40886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  GrowableArray<LiveInterval*> intervals(allocator_, 0);
40986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
41086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
41186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
41286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      intervals.Add(instruction->GetLiveInterval());
41386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
41486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
41586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
416102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  if (processing_core_registers_) {
417102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) {
418102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      LiveInterval* fixed = physical_core_register_intervals_.Get(i);
419102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      if (fixed != nullptr) {
420102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        intervals.Add(fixed);
421102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      }
422102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
423102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  } else {
424102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) {
425102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      LiveInterval* fixed = physical_fp_register_intervals_.Get(i);
426102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      if (fixed != nullptr) {
427102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        intervals.Add(fixed);
428102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      }
42986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
43086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
43186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
4323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0, e = temp_intervals_.Size(); i < e; ++i) {
4333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* temp = temp_intervals_.Get(i);
4343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, temp)) {
4353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      intervals.Add(temp);
4363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
4373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
4383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
4393946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  return ValidateIntervals(intervals, spill_slots_.Size(), reserved_out_slots_, *codegen_,
4403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                           allocator_, processing_core_registers_, log_fatal_on_failure);
44186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
44286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
44331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraybool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
44431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray                                          size_t number_of_spill_slots,
4453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                                          size_t number_of_out_slots,
446a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          const CodeGenerator& codegen,
447a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          ArenaAllocator* allocator,
448a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool processing_core_registers,
449a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool log_fatal_on_failure) {
450a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t number_of_registers = processing_core_registers
451a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      ? codegen.GetNumberOfCoreRegisters()
452a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : codegen.GetNumberOfFloatingPointRegisters();
45331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  GrowableArray<ArenaBitVector*> liveness_of_values(
45431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      allocator, number_of_registers + number_of_spill_slots);
455a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
456a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Allocate a bit vector per register. A live interval that has a register
457a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // allocated will populate the associated bit vector based on its live ranges.
45831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
45931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true));
460a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
461a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
46231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0, e = intervals.Size(); i < e; ++i) {
46331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) {
46431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      LiveInterval* current = it.CurrentInterval();
46586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* defined_by = current->GetParent()->GetDefinedBy();
46686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->GetParent()->HasSpillSlot()
46786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray           // Parameters have their own stack slot.
46886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray           && !(defined_by != nullptr && defined_by->IsParameterValue())) {
4693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers
4703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            + current->GetParent()->GetSpillSlot() / kVRegSize
4713946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            - number_of_out_slots);
47231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
47331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_spill_slot->IsBitSet(j)) {
47431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            if (log_fatal_on_failure) {
47531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              std::ostringstream message;
47631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              message << "Spill slot conflict at " << j;
47731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              LOG(FATAL) << message.str();
47831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            } else {
47931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              return false;
48031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            }
48131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          } else {
48231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_spill_slot->SetBit(j);
48331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          }
48431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        }
485a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
48631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
48731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current->HasRegister()) {
48831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister());
48931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
49031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_register->IsBitSet(j)) {
491a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            if (log_fatal_on_failure) {
492a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              std::ostringstream message;
4933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "Register conflict at " << j << " ";
4943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              if (defined_by != nullptr) {
4953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                message << "(" << defined_by->DebugName() << ")";
4963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              }
4973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "for ";
498a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              if (processing_core_registers) {
499a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpCoreRegister(message, current->GetRegister());
500a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              } else {
501a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpFloatingPointRegister(message, current->GetRegister());
502a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              }
503a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              LOG(FATAL) << message.str();
504a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            } else {
505a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              return false;
506a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            }
507a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          } else {
50831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_register->SetBit(j);
509a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          }
510a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        }
51131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
51231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
513a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
514a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
515a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
516a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
51786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
518a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  interval->Dump(stream);
519a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << ": ";
520a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (interval->HasRegister()) {
521102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (interval->IsFloatingPoint()) {
52286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
523102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
524102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      codegen_->DumpCoreRegister(stream, interval->GetRegister());
525a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
526a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
527a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    stream << "spilled";
528a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
529a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << std::endl;
530a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
531a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
532296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yangvoid RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
533296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "inactive: " << std::endl;
534296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < inactive_.Size(); i ++) {
535296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, inactive_.Get(i));
536296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
537296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "active: " << std::endl;
538296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < active_.Size(); i ++) {
539296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, active_.Get(i));
540296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
541296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "unhandled: " << std::endl;
542296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  auto unhandled = (unhandled_ != nullptr) ?
543296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      unhandled_ : &unhandled_core_intervals_;
544296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < unhandled->Size(); i ++) {
545296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, unhandled->Get(i));
546296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
547296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "handled: " << std::endl;
548296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0; i < handled_.Size(); i ++) {
549296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DumpInterval(stream, handled_.Get(i));
550296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
551296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang}
552296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
553a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// By the book implementation of a linear scan register allocator.
554a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::LinearScan() {
5553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  while (!unhandled_->IsEmpty()) {
556a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (1) Remove interval with the lowest start position from unhandled.
5573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = unhandled_->Pop();
5583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    DCHECK(!current->IsFixed() && !current->HasSpillSlot());
559c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart());
560840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsLowInterval() || unhandled_->Peek()->IsHighInterval());
56187d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray
562a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t position = current->GetStart();
563a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
564296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Remember the inactive_ size here since the ones moved to inactive_ from
565296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // active_ below shouldn't need to be re-checked.
566296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    size_t inactive_intervals_to_handle = inactive_.Size();
567296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
568a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (2) Remove currently active intervals that are dead at this position.
569a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move active intervals that have a lifetime hole at this position
570a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     to inactive.
571a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0; i < active_.Size(); ++i) {
572a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = active_.Get(i);
573a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
574a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
575a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
576a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
577a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (!interval->Covers(position)) {
578a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
579a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
580a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Add(interval);
581a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
582a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
583a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
584a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (3) Remove currently inactive intervals that are dead at this position.
585a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move inactive intervals that cover this position to active.
586296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    for (size_t i = 0; i < inactive_intervals_to_handle; ++i) {
587a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = inactive_.Get(i);
588296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK(interval->GetStart() < position || interval->IsFixed());
589a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
590a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
591a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
592296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        --inactive_intervals_to_handle;
593a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
594a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (interval->Covers(position)) {
595a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
596a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
597296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        --inactive_intervals_to_handle;
598a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Add(interval);
599a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
600a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
601a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
602acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    if (current->IsSlowPathSafepoint()) {
603acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Synthesized interval to record the maximum number of live registers
604acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // at safepoints. No need to allocate a register for it.
605acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      maximum_number_of_live_registers_ =
606acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray          std::max(maximum_number_of_live_registers_, active_.Size());
607acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() > current->GetStart());
608acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      continue;
609acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    }
610acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray
611840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
612840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(!current->HasRegister());
613840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // Allocating the low part was unsucessful. The splitted interval for the high part
614840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // will be handled next (it is in the `unhandled_` list).
615840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
616840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
617840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
618a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (4) Try to find an available register.
619a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    bool success = TryAllocateFreeReg(current);
620a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
621a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (5) If no register could be found, we need to spill.
622a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (!success) {
623a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      success = AllocateBlockedReg(current);
624a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
625a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
626a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (6) If the interval had a register allocated, add it to the list of active
627a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     intervals.
628a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (success) {
629a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      active_.Add(current);
630840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
631840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
632840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
633a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
634a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
635a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
636a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
637a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find a free register. If multiple are found, pick the register that
638a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// is free the longest.
639a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
640a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* free_until = registers_array_;
641a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
642a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers to be free.
643a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
644a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    free_until[i] = kMaxLifetimePosition;
645a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
646a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
647296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  // For each active interval, set its register to not free.
648296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
649296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    LiveInterval* interval = active_.Get(i);
650296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(interval->HasRegister());
651296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    free_until[interval->GetRegister()] = 0;
652296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
653296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
654a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, set its register to be free until
655a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // the next intersection with `current`.
656a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
657a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
658296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
659296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
660296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
661296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
662296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
663296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
664296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
665296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
666296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
667296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
668296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
669a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
670296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (free_until[inactive->GetRegister()] == 0) {
671296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Already used by some active interval. No need to intersect.
672296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
673296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
674a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
675a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (next_intersection != kNoLifetime) {
676aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray      free_until[inactive->GetRegister()] =
677aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray          std::min(free_until[inactive->GetRegister()], next_intersection);
678a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
679a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
680a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
681a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  int reg = -1;
6823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasRegister()) {
6833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Some instructions have a fixed register output.
6843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    reg = current->GetRegister();
685840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (free_until[reg] == 0) {
686840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(current->IsHighInterval());
687840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // AllocateBlockedReg will spill the holder of the register.
688840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      return false;
689840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
6903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
691840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
69201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    int hint = current->FindFirstRegisterHint(free_until);
69301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    if (hint != kNoRegister) {
69401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      DCHECK(!IsBlocked(hint));
69501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      reg = hint;
696840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (current->IsLowInterval()) {
697840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = FindAvailableRegisterPair(free_until);
69801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    } else {
699840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = FindAvailableRegister(free_until);
700a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
701a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
702a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
703840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  DCHECK_NE(reg, -1);
704a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // If we could not find a register, we need to spill.
705840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (free_until[reg] == 0) {
706840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return false;
707840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
708840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
709840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (current->IsLowInterval() && free_until[GetHighForLowRegister(reg)] == 0) {
710a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
711a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
712a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
713a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  current->SetRegister(reg);
714a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (!current->IsDeadAt(free_until[reg])) {
715a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the register is only available for a subset of live ranges
716a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // covered by `current`, split `current` at the position where
717a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // the register is not available anymore.
718a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* split = Split(current, free_until[reg]);
719a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(split != nullptr);
7203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    AddSorted(unhandled_, split);
721a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
722a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
723a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
724a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
725a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::IsBlocked(int reg) const {
726102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return processing_core_registers_
727102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? blocked_core_registers_[reg]
728102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : blocked_fp_registers_[reg];
729a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
730a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
731840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffrayint RegisterAllocator::FindAvailableRegisterPair(size_t* next_use) const {
732840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  int reg = -1;
733840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Pick the register pair that is used the last.
734840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
735840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(i)) continue;
736840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (!IsLowRegister(i)) continue;
737840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int high_register = GetHighForLowRegister(i);
738840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(high_register)) continue;
739840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int existing_high_register = GetHighForLowRegister(reg);
740840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if ((reg == -1) || (next_use[i] >= next_use[reg]
741840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                        && next_use[high_register] >= next_use[existing_high_register])) {
742840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
743840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition
744840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          && next_use[high_register] == kMaxLifetimePosition) {
745840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        break;
746840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
747840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
748840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
749840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
750840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
751840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
752840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffrayint RegisterAllocator::FindAvailableRegister(size_t* next_use) const {
753840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  int reg = -1;
754840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Pick the register that is used the last.
755840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
756840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(i)) continue;
757840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (reg == -1 || next_use[i] > next_use[reg]) {
758840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
759840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition) break;
760840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
761840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
762840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
763840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
764840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
765a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval
766a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register
767a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead.
768a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
769a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t first_register_use = current->FirstRegisterUse();
770412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  if (first_register_use == kNoLifetime) {
77131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
772a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
773a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
774a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
775a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers as not being used.
776a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* next_use = registers_array_;
777a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
778a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    next_use[i] = kMaxLifetimePosition;
779a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
780a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
781a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each active interval, find the next use of its register after the
782a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
783a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
784a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* active = active_.Get(i);
785a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(active->HasRegister());
78686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (active->IsFixed()) {
78786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      next_use[active->GetRegister()] = current->GetStart();
78886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
78986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      size_t use = active->FirstRegisterUseAfter(current->GetStart());
79086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (use != kNoLifetime) {
79186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[active->GetRegister()] = use;
79286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
793a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
794a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
795a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
796a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, find the next use of its register after the
797a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
798a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
799a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
800296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
801296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
802296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
803296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
804296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
805296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
806296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
807296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
808296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
809296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
810a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
81186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
81286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_intersection != kNoLifetime) {
81386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (inactive->IsFixed()) {
81486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[inactive->GetRegister()] =
81586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            std::min(next_intersection, next_use[inactive->GetRegister()]);
81686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else {
81786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t use = inactive->FirstRegisterUseAfter(current->GetStart());
81886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (use != kNoLifetime) {
81986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
82086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
82186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
822a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
823a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
824a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
825a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  int reg = -1;
826840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (current->HasRegister()) {
827840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(current->IsHighInterval());
828840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = current->GetRegister();
829840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (current->IsLowInterval()) {
830840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = FindAvailableRegisterPair(next_use);
831840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
832840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
833840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = FindAvailableRegister(next_use);
834a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
835a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
836840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if ((first_register_use >= next_use[reg])
837840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      || (current->IsLowInterval() && first_register_use >= next_use[GetHighForLowRegister(reg)])) {
838840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
839a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the first use of that instruction is after the last use of the found
840a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // register, we split this interval just before its first register use.
84131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
842c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    LiveInterval* split = Split(current, first_register_use - 1);
843c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    DCHECK_NE(current, split) << "There is not enough registers available for "
844c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray      << split->GetParent()->GetDefinedBy()->DebugName();
8453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    AddSorted(unhandled_, split);
846a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
847a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
848a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Use this register and spill the active and inactives interval that
849a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // have that register.
850a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    current->SetRegister(reg);
851a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
852a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0, e = active_.Size(); i < e; ++i) {
853a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* active = active_.Get(i);
854a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (active->GetRegister() == reg) {
85586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(!active->IsFixed());
856a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        LiveInterval* split = Split(active, current->GetStart());
857a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.DeleteAt(i);
858dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        if (split != active) {
859dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray          handled_.Add(active);
860dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        }
8613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        AddSorted(unhandled_, split);
862a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        break;
863a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
864a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
865a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
866296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
867a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* inactive = inactive_.Get(i);
868a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (inactive->GetRegister() == reg) {
869296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        if (!current->IsSplit() && !inactive->IsFixed()) {
870296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Neither current nor inactive are fixed.
871296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Thanks to SSA, a non-split interval starting in a hole of an
872296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // inactive interval should never intersect with that inactive interval.
873296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Only if it's not fixed though, because fixed intervals don't come from SSA.
874296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
875296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          continue;
876296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        }
87786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t next_intersection = inactive->FirstIntersectionWith(current);
87886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (next_intersection != kNoLifetime) {
87986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          if (inactive->IsFixed()) {
88086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            LiveInterval* split = Split(current, next_intersection);
881dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            DCHECK_NE(split, current);
8823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
88386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          } else {
884dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // Split at the start of `current`, which will lead to splitting
885dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // at the end of the lifetime hole of `inactive`.
886dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            LiveInterval* split = Split(inactive, current->GetStart());
887dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            // If it's inactive, it must start before the current interval.
888dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray            DCHECK_NE(split, inactive);
88986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            inactive_.DeleteAt(i);
890296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang            --i;
891296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang            --e;
89286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            handled_.Add(inactive);
8933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            AddSorted(unhandled_, split);
89486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          }
89586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
896a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
897a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
898a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
899a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return true;
900a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
901a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
902a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) {
904c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray  DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
90586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t insert_at = 0;
9063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = array->Size(); i > 0; --i) {
9073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* current = array->Get(i - 1);
908a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (current->StartsAfter(interval)) {
90986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      insert_at = i;
910a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      break;
911acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
912acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Ensure the slow path interval is the last to be processed at its location: we want the
913acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // interval to know all live registers at this location.
914acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      DCHECK(i == 1 || array->Get(i - 2)->StartsAfter(current));
915acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      insert_at = i;
916acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      break;
917a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
918a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
919840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
9203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  array->InsertAt(insert_at, interval);
921840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Insert the high interval before the low, to ensure the low is processed before.
922840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->HasHighInterval()) {
923840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    array->InsertAt(insert_at, interval->GetHighInterval());
924840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (interval->HasLowInterval()) {
925840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    array->InsertAt(insert_at + 1, interval->GetLowInterval());
926840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
927a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
928a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
929a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
930840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  DCHECK_GE(position, interval->GetStart());
931a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(!interval->IsDeadAt(position));
932a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (position == interval->GetStart()) {
933a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Spill slot will be allocated when handling `interval` again.
934a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    interval->ClearRegister();
935840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
936840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetHighInterval()->ClearRegister();
937840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
938840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetLowInterval()->ClearRegister();
939840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
940a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return interval;
941a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
942a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* new_interval = interval->SplitAt(position);
943840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
944840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
945840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetHighInterval(high);
946840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetLowInterval(new_interval);
947840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
948840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
949840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetLowInterval(low);
950840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      low->SetHighInterval(new_interval);
951840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
952a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return new_interval;
953a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
954a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
955a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
95631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
957840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->IsHighInterval()) {
958840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    // The low interval will contain the spill slot.
959840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return;
960840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
961840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
96231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* parent = interval->GetParent();
96331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
96431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // An instruction gets a spill slot for its entire lifetime. If the parent
96531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // of this interval already has a spill slot, there is nothing to do.
96631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  if (parent->HasSpillSlot()) {
96731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    return;
96831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
96931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
97086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* defined_by = parent->GetDefinedBy();
97186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (defined_by->IsParameterValue()) {
97286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Parameters have their own stack slot.
97386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
97486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
97586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
97686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
97796f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  if (defined_by->IsConstant()) {
97896f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    // Constants don't need a spill slot.
97996f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    return;
98096f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  }
98196f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray
98231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* last_sibling = interval;
98331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  while (last_sibling->GetNextSibling() != nullptr) {
98431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    last_sibling = last_sibling->GetNextSibling();
98531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
98631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  size_t end = last_sibling->GetEnd();
98731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
988412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  // Find an available spill slot.
989412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  size_t slot = 0;
990412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  for (size_t e = spill_slots_.Size(); slot < e; ++slot) {
991412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    // We check if it is less rather than less or equal because the parallel move
992412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    // resolver does not work when a single spill slot needs to be exchanged with
993412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    // a double spill slot. The strict comparison avoids needing to exchange these
994412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    // locations at the same lifetime position.
995412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    if (spill_slots_.Get(slot) < parent->GetStart()
996412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray        && (slot == (e - 1) || spill_slots_.Get(slot + 1) < parent->GetStart())) {
997412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray      break;
998412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    }
999412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
1000412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
100101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  if (parent->NeedsTwoSpillSlots()) {
10023c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    if (slot == spill_slots_.Size()) {
10033c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
10043c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Add(end);
10053c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Add(end);
10063c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else if (slot == spill_slots_.Size() - 1) {
10073c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Put(slot, end);
10083c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Add(end);
10093c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
10103c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Put(slot, end);
10113c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Put(slot + 1, end);
101231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
101331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  } else {
10143c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    if (slot == spill_slots_.Size()) {
10153c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
10163c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Add(end);
10173c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
10183c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      spill_slots_.Put(slot, end);
10193c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    }
102031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
102131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
10223946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  parent->SetSpillSlot((slot + reserved_out_slots_) * kVRegSize);
102386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
102486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
10252a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffraystatic bool IsValidDestination(Location destination) {
1026102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return destination.IsRegister()
1027102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsFpuRegister()
1028840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      || destination.IsFpuRegisterPair()
1029102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsStackSlot()
1030102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsDoubleStackSlot();
10312a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray}
10322a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray
1033740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* user,
103486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
103586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
103686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
103786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1038476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  DCHECK(!user->IsPhi());
103986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1040740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  HInstruction* previous = user->GetPrevious();
104186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = nullptr;
104286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (previous == nullptr
1043476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain      || !previous->IsParallelMove()
10448e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray      || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
104586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
10468e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    move->SetLifetimePosition(user->GetLifetimePosition());
1047740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    user->GetBlock()->InsertInstructionBefore(move, user);
104886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
104986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
105086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
10518e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
1052740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination, nullptr));
105386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
105486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
105546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionStart(size_t position) {
105646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 0;
105746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
105846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
105946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionEnd(size_t position) {
106046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 1;
106146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
106246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
106386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position,
1064740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                             HInstruction* instruction,
106586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location source,
106686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location destination) const {
10672a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray  DCHECK(IsValidDestination(destination));
106886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
106986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
107086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
107186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
107246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  if (at == nullptr) {
107346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    if (IsInstructionStart(position)) {
107446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Block boundary, don't do anything the connection of split siblings will handle it.
107546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      return;
107646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    } else {
107746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Move must happen before the first instruction of the block.
107846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      at = liveness_.GetInstructionFromPosition((position + 1) / 2);
10795976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // Note that parallel moves may have already been inserted, so we explicitly
10805976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // ask for the first instruction of the block: `GetInstructionFromPosition` does
10815976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // not contain the moves.
10825976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      at = at->GetBlock()->GetFirstInstruction();
10835976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      if (at->GetLifetimePosition() != position) {
10845976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_GT(at->GetLifetimePosition(), position);
108546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move = new (allocator_) HParallelMove(allocator_);
108646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move->SetLifetimePosition(position);
108746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        at->GetBlock()->InsertInstructionBefore(move, at);
10885976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      } else {
10895976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK(at->IsParallelMove());
10905976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        move = at->AsParallelMove();
109146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      }
109246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    }
109346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  } else if (IsInstructionEnd(position)) {
109486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen after the instruction.
109586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(!at->IsControlFlow());
109686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = at->GetNext()->AsParallelMove();
1097e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // This is a parallel move for connecting siblings in a same block. We need to
1098e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // differentiate it with moves for connecting blocks, and input moves.
10998e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    if (move == nullptr || move->GetLifetimePosition() > position) {
110086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
110186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
110286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
110386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
110486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
110586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen before the instruction.
110686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* previous = at->GetPrevious();
1107740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    if (previous == nullptr
1108740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || !previous->IsParallelMove()
1109740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || previous->GetLifetimePosition() != position) {
1110740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // If the previous is a parallel move, then its position must be lower
1111740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // than the given `position`: it was added just after the non-parallel
1112740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // move instruction that precedes `instruction`.
1113740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      DCHECK(previous == nullptr
1114740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || !previous->IsParallelMove()
1115740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || previous->GetLifetimePosition() < position);
111686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
111786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
111886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at);
111986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
112086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = previous->AsParallelMove();
112186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
112286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
112301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), position);
1124740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
112586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
112686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
112786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
1128740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                   HInstruction* instruction,
112986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location source,
113086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location destination) const {
11312a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray  DCHECK(IsValidDestination(destination));
113286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
113386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
113486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK_EQ(block->GetSuccessors().Size(), 1u);
113586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* last = block->GetLastInstruction();
1136360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // We insert moves at exit for phi predecessors and connecting blocks.
1137360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // A block ending with an if cannot branch to a block with phis because
1138360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // we do not allow critical edges. It can also not connect
1139360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // a split interval between two blocks: the move has to happen in the successor.
1140360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  DCHECK(!last->IsIf());
114186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* previous = last->GetPrevious();
114286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
1143e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1144e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and output moves.
11455976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray  size_t position = last->GetLifetimePosition();
1146740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  if (previous == nullptr || !previous->IsParallelMove()
11475976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      || previous->AsParallelMove()->GetLifetimePosition() != position) {
114886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
11495976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    move->SetLifetimePosition(position);
115086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, last);
115186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
115286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
115386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1154740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
115586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
115686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
115786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
1158740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                    HInstruction* instruction,
115986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location source,
116086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location destination) const {
11612a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray  DCHECK(IsValidDestination(destination));
116286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
116386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
116486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* first = block->GetFirstInstruction();
116586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = first->AsParallelMove();
1166e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1167e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and input moves.
1168e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != block->GetLifetimeStart()) {
116986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
117086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move->SetLifetimePosition(block->GetLifetimeStart());
117186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, first);
117286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1173740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
117486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
117586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
117686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
117786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
117886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
11792a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray  DCHECK(IsValidDestination(destination));
118086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
118186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1182476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  if (instruction->IsPhi()) {
1183740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
118486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
118586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
118686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1187e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  size_t position = instruction->GetLifetimePosition() + 1;
118886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = instruction->GetNext()->AsParallelMove();
1189e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for moving the output of an instruction. We need
1190e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // to differentiate with input moves, moves for connecting siblings in a
1191e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // and moves for connecting blocks.
1192e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
119386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1194e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(position);
119586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
119686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1197740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination, instruction));
119886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
119986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
120086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
120186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
120286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (current->HasSpillSlot() && current->HasRegister()) {
120386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // We spill eagerly, so move must be at definition.
120486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertMoveAfter(interval->GetDefinedBy(),
1205840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                    interval->ToLocation(),
120601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                    interval->NeedsTwoSpillSlots()
1207412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1208412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
120986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
121086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  UsePosition* use = current->GetFirstUse();
121186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
121286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Walk over all siblings, updating locations of use positions, and
121386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // connecting them when they are adjacent.
121486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  do {
121501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
121686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
121786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Walk over all uses covered by this interval, and update the location
121886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // information.
121986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    while (use != nullptr && use->GetPosition() <= current->GetEnd()) {
12203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      LocationSummary* locations = use->GetUser()->GetLocations();
12213946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      if (use->GetIsEnvironment()) {
12223946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetEnvironmentAt(use->GetInputIndex(), source);
12233946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      } else {
122486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        Location expected_location = locations->InAt(use->GetInputIndex());
122586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (expected_location.IsUnallocated()) {
122686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          locations->SetInAt(use->GetInputIndex(), source);
12272a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray        } else if (!expected_location.IsConstant()) {
122886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          AddInputMoveFor(use->GetUser(), source, expected_location);
122986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
123086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
123186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      use = use->GetNext();
123286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
123386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
123486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // If the next interval starts just after this one, and has a register,
123586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // insert a move.
123686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* next_sibling = current->GetNextSibling();
123786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_sibling != nullptr
123886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && next_sibling->HasRegister()
123986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && current->GetEnd() == next_sibling->GetStart()) {
124001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      Location destination = next_sibling->ToLocation();
1241740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
124286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
12433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
12443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // At each safepoint, we record stack and register information.
12453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    for (size_t i = 0, e = safepoints_.Size(); i < e; ++i) {
12463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      HInstruction* safepoint = safepoints_.Get(i);
12473946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      size_t position = safepoint->GetLifetimePosition();
12483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      LocationSummary* locations = safepoint->GetLocations();
1249b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      if (!current->Covers(position)) {
1250b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        continue;
1251b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      }
1252b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      if (interval->GetStart() == position) {
1253b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        // The safepoint is for this instruction, so the location of the instruction
1254b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        // does not need to be saved.
1255b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray        continue;
1256b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      }
12573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
12583bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
12593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
12603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
12613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
12623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      switch (source.GetKind()) {
12633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kRegister: {
12643bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray          locations->AddLiveRegister(source);
126587d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray          DCHECK_LE(locations->GetNumberOfLiveRegisters(), maximum_number_of_live_registers_);
1266acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray
12673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          if (current->GetType() == Primitive::kPrimNot) {
126856b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray            locations->SetRegisterBit(source.reg());
12693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          }
12703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
12713946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
1272102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        case Location::kFpuRegister: {
1273102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          locations->AddLiveRegister(source);
1274102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          break;
1275102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        }
1276840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        case Location::kFpuRegisterPair: {
1277840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToLow());
1278840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToHigh());
1279840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          break;
1280840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
12813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kStackSlot:  // Fall-through
12823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kDoubleStackSlot:  // Fall-through
12833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kConstant: {
12843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          // Nothing to do.
12853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
12863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
12873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        default: {
12883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          LOG(FATAL) << "Unexpected location for object";
12893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
12903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
12913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
129286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = next_sibling;
129386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } while (current != nullptr);
129486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(use == nullptr);
129586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
129686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
129786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
129886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* from,
129986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* to) const {
130086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval->GetNextSibling() == nullptr) {
130186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Nothing to connect. The whole range was allocated to the same location.
130286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
130386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
130486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
130546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  // Intervals end at the lifetime end of a block. The decrement by one
130646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  // ensures the `Cover` call will return true.
130786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t from_position = from->GetLifetimeEnd() - 1;
130846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  size_t to_position = to->GetLifetimeStart();
130986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
131086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* destination = nullptr;
131186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* source = nullptr;
131286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
131386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
131486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
131586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Check the intervals that cover `from` and `to`.
131686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  while ((current != nullptr) && (source == nullptr || destination == nullptr)) {
131786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (current->Covers(from_position)) {
131886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source == nullptr);
131986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      source = current;
132086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
132186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (current->Covers(to_position)) {
132286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(destination == nullptr);
132386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      destination = current;
132486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
132586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
132686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = current->GetNextSibling();
132786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
132886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
132986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (destination == source) {
133086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Interval was not split.
133186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
133286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
133386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
13348ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray  DCHECK(destination != nullptr && source != nullptr);
13358ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray
133686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!destination->HasRegister()) {
133786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Values are eagerly spilled. Spill slot already contains appropriate value.
133886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
133986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
134086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
134186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
134286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // we need to put the moves at the entry of `to`.
134386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (from->GetSuccessors().Size() == 1) {
1344740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtExitOf(from,
1345740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                               interval->GetParent()->GetDefinedBy(),
134601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               source->ToLocation(),
134701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               destination->ToLocation());
134886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
134986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK_EQ(to->GetPredecessors().Size(), 1u);
1350740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(to,
1351740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                interval->GetParent()->GetDefinedBy(),
135201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                source->ToLocation(),
135301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                destination->ToLocation());
135486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
135586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
135686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
135786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() {
13583bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  codegen_->ComputeFrameSize(
13593bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      spill_slots_.Size(), maximum_number_of_live_registers_, reserved_out_slots_);
136086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
136186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Adjust the Out Location of instructions.
136286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
136386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
136486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
136586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* current = instruction->GetLiveInterval();
136686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LocationSummary* locations = instruction->GetLocations();
136786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location location = locations->Out();
1368476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain    if (instruction->IsParameterValue()) {
136986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Now that we know the frame size, adjust the parameter's location.
137086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.IsStackSlot()) {
137186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
137286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1373f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
137486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (location.IsDoubleStackSlot()) {
137586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
137686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1377f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
137886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (current->HasSpillSlot()) {
137986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
138086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
138186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
138286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
138301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
138486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
138586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (location.IsUnallocated()) {
138686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.GetPolicy() == Location::kSameAsFirstInput) {
1387d0d4852847432368b090c184d6639e573538dccfCalin Juravle        if (locations->InAt(0).IsUnallocated()) {
1388d0d4852847432368b090c184d6639e573538dccfCalin Juravle          locations->SetInAt(0, source);
1389d0d4852847432368b090c184d6639e573538dccfCalin Juravle        } else {
1390d0d4852847432368b090c184d6639e573538dccfCalin Juravle          DCHECK(locations->InAt(0).Equals(source));
1391d0d4852847432368b090c184d6639e573538dccfCalin Juravle        }
139286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
139386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      locations->SetOut(source);
139486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
139586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source.Equals(location));
139686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
139786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
139886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
139986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Connect siblings.
140086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
140186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
140286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ConnectSiblings(instruction->GetLiveInterval());
140386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
140486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
140586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve non-linear control flow across branches. Order does not matter.
140686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
140786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* block = it.Current();
140886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    BitVector* live = liveness_.GetLiveInSet(*block);
140986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (uint32_t idx : live->Indexes()) {
141086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
141186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      LiveInterval* interval = current->GetLiveInterval();
141286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
141386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
141486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
141586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
141686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
141786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
141886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve phi inputs. Order does not matter.
141986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
142086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* current = it.Current();
1421277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1422277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      HInstruction* phi = inst_it.Current();
142386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
142486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HBasicBlock* predecessor = current->GetPredecessors().Get(i);
142586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
142686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HInstruction* input = phi->InputAt(i);
142701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location source = input->GetLiveInterval()->GetLocationAt(
142801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray            predecessor->GetLifetimeEnd() - 1);
142901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray        Location destination = phi->GetLiveInterval()->ToLocation();
1430740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        InsertParallelMoveAtExitOf(predecessor, nullptr, source, destination);
143186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
143286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
143386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
14343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
14353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Assign temp locations.
14363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  HInstruction* current = nullptr;
14373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  size_t temp_index = 0;
14383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < temp_intervals_.Size(); ++i) {
14393946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    LiveInterval* temp = temp_intervals_.Get(i);
1440840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (temp->IsHighInterval()) {
1441840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // High intervals can be skipped, they are already handled by the low interval.
1442840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
1443840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
144401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    HInstruction* at = liveness_.GetTempUser(temp);
144501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    if (at != current) {
14463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      temp_index = 0;
144701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      current = at;
14483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
144901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    LocationSummary* locations = at->GetLocations();
14505368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    switch (temp->GetType()) {
14515368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimInt:
14525368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        locations->SetTempAt(
14535368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain            temp_index++, Location::RegisterLocation(temp->GetRegister()));
14545368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
14555368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
14565368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimDouble:
1457840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1458840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          Location location = Location::FpuRegisterPairLocation(
1459840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray              temp->GetRegister(), temp->GetHighInterval()->GetRegister());
1460840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->SetTempAt(temp_index++, location);
1461840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        } else {
1462840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->SetTempAt(
1463840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray              temp_index++, Location::FpuRegisterLocation(temp->GetRegister()));
1464840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
14655368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
14665368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
14675368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      default:
14685368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        LOG(FATAL) << "Unexpected type for temporary location "
14695368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                   << temp->GetType();
14705368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    }
14713946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
147231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}
147331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1474a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}  // namespace art
1475