register_allocator.cc revision e27f31a81636ad74bd3376ee39cf215941b85c0e
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
19a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "code_generator.h"
20a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "ssa_liveness_analysis.h"
21a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
22a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraynamespace art {
23a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
24a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraystatic constexpr size_t kMaxLifetimePosition = -1;
2531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraystatic constexpr size_t kDefaultNumberOfSpillSlots = 4;
26a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
2786dbb9a12119273039ce272b41c809fa548b37b6Nicolas GeoffrayRegisterAllocator::RegisterAllocator(ArenaAllocator* allocator,
2886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                     CodeGenerator* codegen,
2986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                     const SsaLivenessAnalysis& liveness)
30a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : allocator_(allocator),
31a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        codegen_(codegen),
3286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        liveness_(liveness),
33a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        unhandled_(allocator, 0),
34a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_(allocator, 0),
35a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_(allocator, 0),
36a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_(allocator, 0),
3786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        physical_register_intervals_(allocator, codegen->GetNumberOfRegisters()),
3831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        spill_slots_(allocator, kDefaultNumberOfSpillSlots),
39a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        processing_core_registers_(false),
40a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        number_of_registers_(-1),
41a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        registers_array_(nullptr),
4286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        blocked_registers_(allocator->AllocArray<bool>(codegen->GetNumberOfRegisters())) {
4386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  codegen->SetupBlockedRegisters(blocked_registers_);
4486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  physical_register_intervals_.SetSize(codegen->GetNumberOfRegisters());
45a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
46a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
4786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph,
4886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                InstructionSet instruction_set) {
4986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!Supports(instruction_set)) {
5086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return false;
5186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
5286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = graph.GetBlocks().Size(); i < e; ++i) {
5386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (HInstructionIterator it(graph.GetBlocks().Get(i)->GetInstructions());
5486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray         !it.Done();
5586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray         it.Advance()) {
5686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = it.Current();
5786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->NeedsEnvironment()) return false;
5886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->GetType() == Primitive::kPrimLong) return false;
5986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->GetType() == Primitive::kPrimFloat) return false;
6086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->GetType() == Primitive::kPrimDouble) return false;
6186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
6286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
6386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  return true;
6486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
6586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
6686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) {
6786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  bool is_core_register = (interval->GetType() != Primitive::kPrimDouble)
6886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      && (interval->GetType() != Primitive::kPrimFloat);
69a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return processing_core_registers == is_core_register;
70a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
71a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegisters() {
7386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  processing_core_registers_ = true;
7486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  AllocateRegistersInternal();
7586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  processing_core_registers_ = false;
7686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  AllocateRegistersInternal();
7786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
7886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  Resolve();
7986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
8086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (kIsDebugBuild) {
8186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = true;
8286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
8386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = false;
8486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
8586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
8686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
8786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
8886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::BlockRegister(Location location,
8986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                      size_t start,
9086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                      size_t end,
9186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                      Primitive::Type type) {
9286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  int reg = location.reg().RegId();
9386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* interval = physical_register_intervals_.Get(reg);
9486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval == nullptr) {
9586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    interval = LiveInterval::MakeFixedInterval(allocator_, reg, type);
9686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    physical_register_intervals_.Put(reg, interval);
9786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    inactive_.Add(interval);
9886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
9986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(interval->GetRegister() == reg);
10086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  interval->AddRange(start, end);
10186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
10286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
10386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegistersInternal() {
104a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  number_of_registers_ = processing_core_registers_
10586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      ? codegen_->GetNumberOfCoreRegisters()
10686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      : codegen_->GetNumberOfFloatingPointRegisters();
107a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
108a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_);
109a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
110a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Iterate post-order, to ensure the list is sorted, and the last added interval
111a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // is the one with the lowest start position.
11286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = liveness_.GetNumberOfSsaValues(); i > 0; --i) {
11386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i - 1);
11486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* current = instruction->GetLiveInterval();
11586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, current)) {
116a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      DCHECK(unhandled_.IsEmpty() || current->StartsBefore(unhandled_.Peek()));
117a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      LocationSummary* locations = instruction->GetLocations();
11986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (locations->GetTempCount() != 0) {
12086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        // Note that we already filtered out instructions requiring temporaries in
12186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        // RegisterAllocator::CanAllocateRegistersFor.
12286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        LOG(FATAL) << "Unimplemented";
12386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
124a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
12586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Some instructions define their output in fixed register/stack slot. We need
12686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // to ensure we know these locations before doing register allocation. For a
12786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // given register, we create an interval that covers these locations. The register
12886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // will be unavailable at these locations when trying to allocate one for an
12986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // interval.
13086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      //
13186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // The backwards walking ensures the ranges are ordered on increasing start positions.
13286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      Location output = locations->Out();
13386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      size_t position = instruction->GetLifetimePosition();
13486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (output.IsRegister()) {
13586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        // Shift the interval's start by one to account for the blocked register.
13686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetFrom(position + 1);
13786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetRegister(output.reg().RegId());
13886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        BlockRegister(output, position, position + 1, instruction->GetType());
13986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (output.IsStackSlot()) {
14086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(output.GetStackIndex());
14186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
14286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0; i < instruction->InputCount(); ++i) {
14386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        Location input = locations->InAt(i);
14486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (input.IsRegister()) {
14586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          BlockRegister(input, position, position + 1, instruction->InputAt(i)->GetType());
14686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
14786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
14886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
14986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Add the interval to the correct list.
15086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->HasRegister()) {
15186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(instruction->IsParameterValue());
15286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        inactive_.Add(current);
15386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (current->HasSpillSlot()) {
15486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(instruction->IsParameterValue());
15586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        // Split before first register use.
15686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t first_register_use = current->FirstRegisterUse();
15786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (first_register_use != kNoLifetime) {
15886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          LiveInterval* split = Split(current, first_register_use - 1);
15986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          // The new interval may start at a late
16086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          AddToUnhandled(split);
16186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        } else {
16286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          // Nothing to do, we won't allocate a register for this value.
16386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
16486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else {
16586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(unhandled_.IsEmpty() || current->StartsBefore(unhandled_.Peek()));
16686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        unhandled_.Add(current);
16786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
168a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
169a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
17086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
17186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LinearScan();
172a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
173a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
17431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayclass AllRangesIterator : public ValueObject {
17531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray public:
17631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  explicit AllRangesIterator(LiveInterval* interval)
17731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      : current_interval_(interval),
17831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_(interval->GetFirstRange()) {}
17931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
18031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  bool Done() const { return current_interval_ == nullptr; }
18131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* CurrentRange() const { return current_range_; }
18231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* CurrentInterval() const { return current_interval_; }
18331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
18431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  void Advance() {
18531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    current_range_ = current_range_->GetNext();
18631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    if (current_range_ == nullptr) {
18731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      current_interval_ = current_interval_->GetNextSibling();
18831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current_interval_ != nullptr) {
18931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_ = current_interval_->GetFirstRange();
19031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
19131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
19231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
19331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
19431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray private:
19531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* current_interval_;
19631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* current_range_;
19731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
19831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
19931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray};
20031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
20186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
20286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // To simplify unit testing, we eagerly create the array of intervals, and
20386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // call the helper method.
20486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  GrowableArray<LiveInterval*> intervals(allocator_, 0);
20586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
20686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
20786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
20886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      intervals.Add(instruction->GetLiveInterval());
20986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
21086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
21186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
21286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = physical_register_intervals_.Size(); i < e; ++i) {
21386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* fixed = physical_register_intervals_.Get(i);
21486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (fixed != nullptr && ShouldProcess(processing_core_registers_, fixed)) {
21586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      intervals.Add(fixed);
21686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
21786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
21886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
21986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  return ValidateIntervals(intervals, spill_slots_.Size(), *codegen_, allocator_,
22086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                           processing_core_registers_, log_fatal_on_failure);
22186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
22286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
22331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraybool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
22431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray                                          size_t number_of_spill_slots,
225a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          const CodeGenerator& codegen,
226a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          ArenaAllocator* allocator,
227a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool processing_core_registers,
228a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool log_fatal_on_failure) {
229a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t number_of_registers = processing_core_registers
230a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      ? codegen.GetNumberOfCoreRegisters()
231a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : codegen.GetNumberOfFloatingPointRegisters();
23231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  GrowableArray<ArenaBitVector*> liveness_of_values(
23331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      allocator, number_of_registers + number_of_spill_slots);
234a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
235a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Allocate a bit vector per register. A live interval that has a register
236a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // allocated will populate the associated bit vector based on its live ranges.
23731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
23831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true));
239a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
240a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
24131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0, e = intervals.Size(); i < e; ++i) {
24231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) {
24331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      LiveInterval* current = it.CurrentInterval();
24486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* defined_by = current->GetParent()->GetDefinedBy();
24586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->GetParent()->HasSpillSlot()
24686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray           // Parameters have their own stack slot.
24786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray           && !(defined_by != nullptr && defined_by->IsParameterValue())) {
24831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        BitVector* liveness_of_spill_slot = liveness_of_values.Get(
24931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            number_of_registers + current->GetParent()->GetSpillSlot() / kVRegSize);
25031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
25131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_spill_slot->IsBitSet(j)) {
25231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            if (log_fatal_on_failure) {
25331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              std::ostringstream message;
25431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              message << "Spill slot conflict at " << j;
25531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              LOG(FATAL) << message.str();
25631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            } else {
25731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              return false;
25831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            }
25931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          } else {
26031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_spill_slot->SetBit(j);
26131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          }
26231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        }
263a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
26431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
26531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current->HasRegister()) {
26631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister());
26731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
26831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_register->IsBitSet(j)) {
269a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            if (log_fatal_on_failure) {
270a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              std::ostringstream message;
271a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              message << "Register conflict at " << j << " for ";
272a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              if (processing_core_registers) {
273a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpCoreRegister(message, current->GetRegister());
274a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              } else {
275a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpFloatingPointRegister(message, current->GetRegister());
276a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              }
277a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              LOG(FATAL) << message.str();
278a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            } else {
279a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              return false;
280a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            }
281a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          } else {
28231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_register->SetBit(j);
283a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          }
284a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        }
28531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
28631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
287a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
288a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
289a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
290a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
29186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
292a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  interval->Dump(stream);
293a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << ": ";
294a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (interval->HasRegister()) {
295a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (processing_core_registers_) {
29686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      codegen_->DumpCoreRegister(stream, interval->GetRegister());
297a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    } else {
29886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
299a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
300a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
301a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    stream << "spilled";
302a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
303a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << std::endl;
304a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
305a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
306a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// By the book implementation of a linear scan register allocator.
307a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::LinearScan() {
308a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  while (!unhandled_.IsEmpty()) {
309a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (1) Remove interval with the lowest start position from unhandled.
310a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* current = unhandled_.Pop();
31186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(!current->IsFixed() && !current->HasRegister() && !current->HasSpillSlot());
312a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t position = current->GetStart();
313a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
314a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (2) Remove currently active intervals that are dead at this position.
315a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move active intervals that have a lifetime hole at this position
316a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     to inactive.
317a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0; i < active_.Size(); ++i) {
318a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = active_.Get(i);
319a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
320a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
321a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
322a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
323a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (!interval->Covers(position)) {
324a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Delete(interval);
325a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
326a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Add(interval);
327a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
328a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
329a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
330a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (3) Remove currently inactive intervals that are dead at this position.
331a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move inactive intervals that cover this position to active.
332a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0; i < inactive_.Size(); ++i) {
333a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* interval = inactive_.Get(i);
334a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (interval->IsDeadAt(position)) {
335a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
336a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
337a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(interval);
338a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      } else if (interval->Covers(position)) {
339a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        inactive_.Delete(interval);
340a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        --i;
341a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.Add(interval);
342a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
343a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
344a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
345a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (4) Try to find an available register.
346a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    bool success = TryAllocateFreeReg(current);
347a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
348a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (5) If no register could be found, we need to spill.
349a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (!success) {
350a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      success = AllocateBlockedReg(current);
351a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
352a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
353a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (6) If the interval had a register allocated, add it to the list of active
354a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     intervals.
355a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (success) {
356a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      active_.Add(current);
357a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
358a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
359a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
360a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
361a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find a free register. If multiple are found, pick the register that
362a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// is free the longest.
363a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
364a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* free_until = registers_array_;
365a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
366a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers to be free.
367a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
368a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    free_until[i] = kMaxLifetimePosition;
369a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
370a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
371a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, set its register to be free until
372a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // the next intersection with `current`.
373a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Thanks to SSA, this should only be needed for intervals
374a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // that are the result of a split.
375a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
376a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
377a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
378a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
379a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (next_intersection != kNoLifetime) {
380a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      free_until[inactive->GetRegister()] = next_intersection;
381a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
382a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
383a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
38486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // For each active interval, set its register to not free.
38586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
38686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* interval = active_.Get(i);
38786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(interval->HasRegister());
38886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    free_until[interval->GetRegister()] = 0;
38986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
39086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
391a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Pick the register that is free the longest.
392a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  int reg = -1;
393a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
394a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (IsBlocked(i)) continue;
395a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (reg == -1 || free_until[i] > free_until[reg]) {
396a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      reg = i;
397a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (free_until[i] == kMaxLifetimePosition) break;
398a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
399a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
400a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
401a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // If we could not find a register, we need to spill.
402a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (reg == -1 || free_until[reg] == 0) {
403a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
404a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
405a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
406a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  current->SetRegister(reg);
407a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (!current->IsDeadAt(free_until[reg])) {
408a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the register is only available for a subset of live ranges
409a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // covered by `current`, split `current` at the position where
410a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // the register is not available anymore.
411a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* split = Split(current, free_until[reg]);
412a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(split != nullptr);
413a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    AddToUnhandled(split);
414a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
415a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
416a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
417a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
418a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::IsBlocked(int reg) const {
419a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // TODO: This only works for core registers and needs to be adjusted for
420a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // floating point registers.
421a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(processing_core_registers_);
422a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return blocked_registers_[reg];
423a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
424a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
425a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval
426a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register
427a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead.
428a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
429a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t first_register_use = current->FirstRegisterUse();
430a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (current->FirstRegisterUse() == kNoLifetime) {
43131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
432a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
433a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
434a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
435a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers as not being used.
436a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* next_use = registers_array_;
437a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
438a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    next_use[i] = kMaxLifetimePosition;
439a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
440a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
441a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each active interval, find the next use of its register after the
442a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
443a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = active_.Size(); i < e; ++i) {
444a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* active = active_.Get(i);
445a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(active->HasRegister());
44686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (active->IsFixed()) {
44786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      next_use[active->GetRegister()] = current->GetStart();
44886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
44986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      size_t use = active->FirstRegisterUseAfter(current->GetStart());
45086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (use != kNoLifetime) {
45186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[active->GetRegister()] = use;
45286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
453a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
454a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
455a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
456a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, find the next use of its register after the
457a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
458a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Thanks to SSA, this should only be needed for intervals
459a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // that are the result of a split.
460a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0, e = inactive_.Size(); i < e; ++i) {
461a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* inactive = inactive_.Get(i);
462a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
46386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
46486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_intersection != kNoLifetime) {
46586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (inactive->IsFixed()) {
46686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[inactive->GetRegister()] =
46786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            std::min(next_intersection, next_use[inactive->GetRegister()]);
46886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else {
46986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t use = inactive->FirstRegisterUseAfter(current->GetStart());
47086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (use != kNoLifetime) {
47186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
47286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
47386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
474a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
475a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
476a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
477a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Pick the register that is used the last.
478a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  int reg = -1;
479a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
480a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (IsBlocked(i)) continue;
481a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (reg == -1 || next_use[i] > next_use[reg]) {
482a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      reg = i;
483a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition) break;
484a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
485a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
486a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
487a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (first_register_use >= next_use[reg]) {
488a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the first use of that instruction is after the last use of the found
489a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // register, we split this interval just before its first register use.
49031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
491a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* split = Split(current, first_register_use - 1);
492a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    AddToUnhandled(split);
493a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
494a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
495a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Use this register and spill the active and inactives interval that
496a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // have that register.
497a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    current->SetRegister(reg);
498a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
499a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0, e = active_.Size(); i < e; ++i) {
500a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* active = active_.Get(i);
501a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (active->GetRegister() == reg) {
50286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(!active->IsFixed());
503a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        LiveInterval* split = Split(active, current->GetStart());
504a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        active_.DeleteAt(i);
505a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        handled_.Add(active);
506a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        AddToUnhandled(split);
507a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        break;
508a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
509a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
510a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
511a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    for (size_t i = 0; i < inactive_.Size(); ++i) {
512a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      LiveInterval* inactive = inactive_.Get(i);
513a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (inactive->GetRegister() == reg) {
51486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        size_t next_intersection = inactive->FirstIntersectionWith(current);
51586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (next_intersection != kNoLifetime) {
51686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          if (inactive->IsFixed()) {
51786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            LiveInterval* split = Split(current, next_intersection);
51886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            AddToUnhandled(split);
51986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          } else {
52086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            LiveInterval* split = Split(inactive, current->GetStart());
52186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            inactive_.DeleteAt(i);
52286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            handled_.Add(inactive);
52386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            AddToUnhandled(split);
52486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            --i;
52586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          }
52686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
527a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
528a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
529a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
530a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return true;
531a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
532a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
533a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
534a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::AddToUnhandled(LiveInterval* interval) {
53586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t insert_at = 0;
536a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = unhandled_.Size(); i > 0; --i) {
537a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* current = unhandled_.Get(i - 1);
538a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (current->StartsAfter(interval)) {
53986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      insert_at = i;
540a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      break;
541a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
542a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
54386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  unhandled_.InsertAt(insert_at, interval);
544a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
545a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
546a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
547a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(position >= interval->GetStart());
548a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(!interval->IsDeadAt(position));
549a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (position == interval->GetStart()) {
550a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Spill slot will be allocated when handling `interval` again.
551a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    interval->ClearRegister();
552a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return interval;
553a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
554a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* new_interval = interval->SplitAt(position);
555a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return new_interval;
556a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
557a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
558a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
55931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
56031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* parent = interval->GetParent();
56131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
56231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // An instruction gets a spill slot for its entire lifetime. If the parent
56331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // of this interval already has a spill slot, there is nothing to do.
56431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  if (parent->HasSpillSlot()) {
56531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    return;
56631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
56731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
56886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* defined_by = parent->GetDefinedBy();
56986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (defined_by->IsParameterValue()) {
57086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Parameters have their own stack slot.
57186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
57286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
57386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
57486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
57531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* last_sibling = interval;
57631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  while (last_sibling->GetNextSibling() != nullptr) {
57731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    last_sibling = last_sibling->GetNextSibling();
57831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
57931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  size_t end = last_sibling->GetEnd();
58031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
58131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // Find an available spill slot.
58231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  size_t slot = 0;
58331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t e = spill_slots_.Size(); slot < e; ++slot) {
58431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    if (spill_slots_.Get(slot) <= parent->GetStart()) {
58531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      break;
58631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
58731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
58831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
58931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  if (slot == spill_slots_.Size()) {
59031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    // We need a new spill slot.
59131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    spill_slots_.Add(end);
59231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  } else {
59331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    spill_slots_.Put(slot, end);
59431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
59531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
59686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  parent->SetSpillSlot(slot * kVRegSize);
59786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
59886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
59986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic Location ConvertToLocation(LiveInterval* interval) {
60086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval->HasRegister()) {
60186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return Location::RegisterLocation(ManagedRegister(interval->GetRegister()));
60286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
60386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(interval->GetParent()->HasSpillSlot());
60486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return Location::StackSlot(interval->GetParent()->GetSpillSlot());
60586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
60686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
60786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
60886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray// We create a special marker for inputs moves to differentiate them from
60986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray// moves created during resolution. They must be different instructions
61086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray// because the input moves work on the assumption that the interval moves
61186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray// have been executed.
61286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic constexpr size_t kInputMoveLifetimePosition = 0;
61386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic bool IsInputMove(HInstruction* instruction) {
61486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  return instruction->GetLifetimePosition() == kInputMoveLifetimePosition;
61586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
61686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
61786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* instruction,
61886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
61986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
62086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
62186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
62286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(instruction->AsPhi() == nullptr);
62386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
62486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* previous = instruction->GetPrevious();
62586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = nullptr;
62686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (previous == nullptr
62786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      || previous->AsParallelMove() == nullptr
62886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      || !IsInputMove(previous)) {
62986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
63086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move->SetLifetimePosition(kInputMoveLifetimePosition);
63186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    instruction->GetBlock()->InsertInstructionBefore(move, instruction);
63286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
63386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
63486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
63586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(IsInputMove(move));
63686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination));
63786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
63886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
63986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position,
64086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location source,
64186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location destination) const {
64286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
64386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
64486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
64586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (at == nullptr) {
64686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Block boundary, don't no anything the connection of split siblings will handle it.
64786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
64886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
64986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
65086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if ((position & 1) == 1) {
65186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen after the instruction.
65286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(!at->IsControlFlow());
65386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = at->GetNext()->AsParallelMove();
654e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // This is a parallel move for connecting siblings in a same block. We need to
655e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // differentiate it with moves for connecting blocks, and input moves.
656e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    if (move == nullptr || move->GetLifetimePosition() != position) {
65786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
65886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
65986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
66086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
66186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
66286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen before the instruction.
66386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* previous = at->GetPrevious();
66486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (previous != nullptr && previous->AsParallelMove() != nullptr) {
665e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray      // This is a parallel move for connecting siblings in a same block. We need to
666e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray      // differentiate it with moves for connecting blocks, and input moves.
667e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray      if (previous->GetLifetimePosition() != position) {
66886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        previous = previous->GetPrevious();
66986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
67086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
67186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (previous == nullptr || previous->AsParallelMove() == nullptr) {
67286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
67386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
67486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at);
67586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
67686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = previous->AsParallelMove();
67786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
67886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
67986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination));
68086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
68186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
68286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
68386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location source,
68486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location destination) const {
68586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
68686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
68786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK_EQ(block->GetSuccessors().Size(), 1u);
68886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* last = block->GetLastInstruction();
68986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* previous = last->GetPrevious();
69086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
691e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
692e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and output moves.
693e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (previous == nullptr || previous->AsParallelMove() == nullptr
694e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray      || previous->AsParallelMove()->GetLifetimePosition() != block->GetLifetimeEnd()) {
69586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
696e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(block->GetLifetimeEnd());
69786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, last);
69886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
69986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
70086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
70186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination));
70286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
70386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
70486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
70586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location source,
70686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location destination) const {
70786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
70886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
70986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* first = block->GetFirstInstruction();
71086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = first->AsParallelMove();
711e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
712e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and input moves.
713e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != block->GetLifetimeStart()) {
71486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
71586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move->SetLifetimePosition(block->GetLifetimeStart());
71686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, first);
71786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
71886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination));
71986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
72086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
72186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
72286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
72386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
72486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
72586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
72686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (instruction->AsPhi() != nullptr) {
72786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertParallelMoveAtEntryOf(instruction->GetBlock(), source, destination);
72886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
72986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
73086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
731e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  size_t position = instruction->GetLifetimePosition() + 1;
73286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = instruction->GetNext()->AsParallelMove();
733e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for moving the output of an instruction. We need
734e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // to differentiate with input moves, moves for connecting siblings in a
735e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // and moves for connecting blocks.
736e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
73786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
738e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(position);
73986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
74086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
74186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  move->AddMove(new (allocator_) MoveOperands(source, destination));
74286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
74386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
74486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
74586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
74686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (current->HasSpillSlot() && current->HasRegister()) {
74786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // We spill eagerly, so move must be at definition.
74886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertMoveAfter(interval->GetDefinedBy(),
74986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                    Location::RegisterLocation(ManagedRegister(interval->GetRegister())),
75086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                    Location::StackSlot(interval->GetParent()->GetSpillSlot()));
75186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
75286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  UsePosition* use = current->GetFirstUse();
75386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
75486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Walk over all siblings, updating locations of use positions, and
75586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // connecting them when they are adjacent.
75686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  do {
75786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location source = ConvertToLocation(current);
75886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
75986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Walk over all uses covered by this interval, and update the location
76086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // information.
76186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    while (use != nullptr && use->GetPosition() <= current->GetEnd()) {
76286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (!use->GetIsEnvironment()) {
76386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        LocationSummary* locations = use->GetUser()->GetLocations();
76486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        Location expected_location = locations->InAt(use->GetInputIndex());
76586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (expected_location.IsUnallocated()) {
76686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          locations->SetInAt(use->GetInputIndex(), source);
76786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        } else {
76886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          AddInputMoveFor(use->GetUser(), source, expected_location);
76986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
77086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
77186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      use = use->GetNext();
77286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
77386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
77486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // If the next interval starts just after this one, and has a register,
77586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // insert a move.
77686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* next_sibling = current->GetNextSibling();
77786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_sibling != nullptr
77886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && next_sibling->HasRegister()
77986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && current->GetEnd() == next_sibling->GetStart()) {
78086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      Location destination = ConvertToLocation(next_sibling);
78186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      InsertParallelMoveAt(current->GetEnd(), source, destination);
78286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
78386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = next_sibling;
78486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } while (current != nullptr);
78586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(use == nullptr);
78686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
78786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
78886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
78986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* from,
79086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* to) const {
79186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval->GetNextSibling() == nullptr) {
79286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Nothing to connect. The whole range was allocated to the same location.
79386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
79486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
79586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
79686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t from_position = from->GetLifetimeEnd() - 1;
79786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t to_position = to->GetLifetimeStart();
79886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
79986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* destination = nullptr;
80086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* source = nullptr;
80186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
80286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
80386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
80486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Check the intervals that cover `from` and `to`.
80586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  while ((current != nullptr) && (source == nullptr || destination == nullptr)) {
80686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (current->Covers(from_position)) {
80786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source == nullptr);
80886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      source = current;
80986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
81086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (current->Covers(to_position)) {
81186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(destination == nullptr);
81286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      destination = current;
81386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
81486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
81586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = current->GetNextSibling();
81686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
81786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
81886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (destination == source) {
81986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Interval was not split.
82086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
82186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
82286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
82386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!destination->HasRegister()) {
82486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Values are eagerly spilled. Spill slot already contains appropriate value.
82586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
82686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
82786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
82886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
82986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // we need to put the moves at the entry of `to`.
83086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (from->GetSuccessors().Size() == 1) {
83186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertParallelMoveAtExitOf(from, ConvertToLocation(source), ConvertToLocation(destination));
83286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
83386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK_EQ(to->GetPredecessors().Size(), 1u);
83486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertParallelMoveAtEntryOf(to, ConvertToLocation(source), ConvertToLocation(destination));
83586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
83686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
83786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
83886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray// Returns the location of `interval`, or siblings of `interval`, at `position`.
83986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic Location FindLocationAt(LiveInterval* interval, size_t position) {
84086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
84186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  while (!current->Covers(position)) {
84286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = current->GetNextSibling();
84386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(current != nullptr);
84486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
84586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  return ConvertToLocation(current);
84686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
84786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
84886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() {
84986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  codegen_->ComputeFrameSize(spill_slots_.Size());
85086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
85186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Adjust the Out Location of instructions.
85286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
85386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
85486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
85586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* current = instruction->GetLiveInterval();
85686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LocationSummary* locations = instruction->GetLocations();
85786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location location = locations->Out();
85886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (instruction->AsParameterValue() != nullptr) {
85986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Now that we know the frame size, adjust the parameter's location.
86086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.IsStackSlot()) {
86186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
86286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
86386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        locations->SetOut(location);
86486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (location.IsDoubleStackSlot()) {
86586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
86686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
86786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        locations->SetOut(location);
86886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (current->HasSpillSlot()) {
86986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
87086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
87186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
87286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
87386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location source = ConvertToLocation(current);
87486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
87586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (location.IsUnallocated()) {
87686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.GetPolicy() == Location::kSameAsFirstInput) {
87786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        locations->SetInAt(0, source);
87886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
87986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      locations->SetOut(source);
88086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
88186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source.Equals(location));
88286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
88386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
88486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
88586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Connect siblings.
88686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
88786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
88886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ConnectSiblings(instruction->GetLiveInterval());
88986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
89086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
89186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve non-linear control flow across branches. Order does not matter.
89286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
89386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* block = it.Current();
89486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    BitVector* live = liveness_.GetLiveInSet(*block);
89586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (uint32_t idx : live->Indexes()) {
89686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx);
89786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      LiveInterval* interval = current->GetLiveInterval();
89886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
89986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block);
90086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
90186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
90286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
90386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
90486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve phi inputs. Order does not matter.
90586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) {
90686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* current = it.Current();
90786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (HInstructionIterator it(current->GetPhis()); !it.Done(); it.Advance()) {
90886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* phi = it.Current();
90986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) {
91086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HBasicBlock* predecessor = current->GetPredecessors().Get(i);
91186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u);
91286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        HInstruction* input = phi->InputAt(i);
91386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        Location source = FindLocationAt(input->GetLiveInterval(),
91486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                         predecessor->GetLastInstruction()->GetLifetimePosition());
91586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        Location destination = ConvertToLocation(phi->GetLiveInterval());
91686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        InsertParallelMoveAtExitOf(predecessor, source, destination);
91786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
91886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
91986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
92031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}
92131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
922a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}  // namespace art
923