register_allocator.cc revision 04eb70f0282703a150e3e2c7e5b3f678fec25397
1a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray/*
2a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *
4a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * you may not use this file except in compliance with the License.
6a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * You may obtain a copy of the License at
7a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *
8a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray *
10a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * See the License for the specific language governing permissions and
14a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * limitations under the License.
15a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray */
16a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
17a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
18a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
19234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray#include <iostream>
20c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#include <sstream>
21c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
22e77493c7217efdd1a0ecef521a6845a13da0305bIan Rogers#include "base/bit_vector-inl.h"
23a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "code_generator.h"
24a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "ssa_liveness_analysis.h"
25a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
26a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraynamespace art {
27a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
28a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraystatic constexpr size_t kMaxLifetimePosition = -1;
2931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraystatic constexpr size_t kDefaultNumberOfSpillSlots = 4;
30a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
31840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray// For simplicity, we implement register pairs as (reg, reg + 1).
32840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray// Note that this is a requirement for double registers on ARM, since we
33840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray// allocate SRegister.
34840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffraystatic int GetHighForLowRegister(int reg) { return reg + 1; }
35840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffraystatic bool IsLowRegister(int reg) { return (reg & 1) == 0; }
36234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraystatic bool IsLowOfUnalignedPairInterval(LiveInterval* low) {
37234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  return GetHighForLowRegister(low->GetRegister()) != low->GetHighInterval()->GetRegister();
38234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray}
39840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
4086dbb9a12119273039ce272b41c809fa548b37b6Nicolas GeoffrayRegisterAllocator::RegisterAllocator(ArenaAllocator* allocator,
4186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                     CodeGenerator* codegen,
4286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                     const SsaLivenessAnalysis& liveness)
43a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : allocator_(allocator),
44a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        codegen_(codegen),
4586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        liveness_(liveness),
462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        unhandled_core_intervals_(allocator->Adapter(kArenaAllocRegisterAllocator)),
472aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        unhandled_fp_intervals_(allocator->Adapter(kArenaAllocRegisterAllocator)),
483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        unhandled_(nullptr),
492aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        handled_(allocator->Adapter(kArenaAllocRegisterAllocator)),
502aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        active_(allocator->Adapter(kArenaAllocRegisterAllocator)),
512aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        inactive_(allocator->Adapter(kArenaAllocRegisterAllocator)),
522aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        physical_core_register_intervals_(allocator->Adapter(kArenaAllocRegisterAllocator)),
532aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        physical_fp_register_intervals_(allocator->Adapter(kArenaAllocRegisterAllocator)),
542aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        temp_intervals_(allocator->Adapter(kArenaAllocRegisterAllocator)),
552aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        int_spill_slots_(allocator->Adapter(kArenaAllocRegisterAllocator)),
562aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        long_spill_slots_(allocator->Adapter(kArenaAllocRegisterAllocator)),
572aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        float_spill_slots_(allocator->Adapter(kArenaAllocRegisterAllocator)),
582aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        double_spill_slots_(allocator->Adapter(kArenaAllocRegisterAllocator)),
5977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        catch_phi_spill_slots_(0),
602aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        safepoints_(allocator->Adapter(kArenaAllocRegisterAllocator)),
61a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        processing_core_registers_(false),
62a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        number_of_registers_(-1),
63a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        registers_array_(nullptr),
64102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        blocked_core_registers_(codegen->GetBlockedCoreRegisters()),
65102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        blocked_fp_registers_(codegen->GetBlockedFloatingPointRegisters()),
663bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray        reserved_out_slots_(0),
67f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_core_registers_(0),
68f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_fp_registers_(0) {
692aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  temp_intervals_.reserve(4);
702aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  int_spill_slots_.reserve(kDefaultNumberOfSpillSlots);
712aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  long_spill_slots_.reserve(kDefaultNumberOfSpillSlots);
722aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  float_spill_slots_.reserve(kDefaultNumberOfSpillSlots);
732aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  double_spill_slots_.reserve(kDefaultNumberOfSpillSlots);
742aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko
7558282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  codegen->SetupBlockedRegisters();
762aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  physical_core_register_intervals_.resize(codegen->GetNumberOfCoreRegisters(), nullptr);
772aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  physical_fp_register_intervals_.resize(codegen->GetNumberOfFloatingPointRegisters(), nullptr);
783946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Always reserve for the current method and the graph's max out registers.
793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // TODO: compute it instead.
80e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  // ArtMethod* takes 2 vregs for 64 bits.
81e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  reserved_out_slots_ = InstructionSetPointerSize(codegen->GetInstructionSet()) / kVRegSize +
82e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      codegen->GetGraph()->GetMaximumNumberOfOutVRegs();
83a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
84a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
85234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraybool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph ATTRIBUTE_UNUSED,
8686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                InstructionSet instruction_set) {
87f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic  return instruction_set == kArm
88f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kArm64
89f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kMips
904dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze      || instruction_set == kMips64
91f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kThumb2
92234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      || instruction_set == kX86
93f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kX86_64;
9486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
9586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
9686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) {
973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (interval == nullptr) return false;
9886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  bool is_core_register = (interval->GetType() != Primitive::kPrimDouble)
9986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      && (interval->GetType() != Primitive::kPrimFloat);
100a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return processing_core_registers == is_core_register;
101a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
102a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegisters() {
10486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  AllocateRegistersInternal();
10586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  Resolve();
10686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
10786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (kIsDebugBuild) {
10886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = true;
10986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
11086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = false;
11186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
1125976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // Check that the linear order is still correct with regards to lifetime positions.
1135976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // Since only parallel moves have been inserted during the register allocation,
1145976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // these checks are mostly for making sure these moves have been added correctly.
1155976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    size_t current_liveness = 0;
1160d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray    for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
1175976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      HBasicBlock* block = it.Current();
1185976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1195976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        HInstruction* instruction = inst_it.Current();
1205976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_LE(current_liveness, instruction->GetLifetimePosition());
1215976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        current_liveness = instruction->GetLifetimePosition();
1225976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      }
1235976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      for (HInstructionIterator inst_it(block->GetInstructions());
1245976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray           !inst_it.Done();
1255976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray           inst_it.Advance()) {
1265976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        HInstruction* instruction = inst_it.Current();
1275976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_LE(current_liveness, instruction->GetLifetimePosition()) << instruction->DebugName();
1285976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        current_liveness = instruction->GetLifetimePosition();
1295976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      }
1305976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    }
13186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
13286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
13386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
13477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilvoid RegisterAllocator::BlockRegister(Location location, size_t start, size_t end) {
13556b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray  int reg = location.reg();
136102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  DCHECK(location.IsRegister() || location.IsFpuRegister());
137102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  LiveInterval* interval = location.IsRegister()
1382aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      ? physical_core_register_intervals_[reg]
1392aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      : physical_fp_register_intervals_[reg];
140102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  Primitive::Type type = location.IsRegister()
141102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? Primitive::kPrimInt
142840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      : Primitive::kPrimFloat;
14386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval == nullptr) {
14486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    interval = LiveInterval::MakeFixedInterval(allocator_, reg, type);
145102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (location.IsRegister()) {
1462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      physical_core_register_intervals_[reg] = interval;
147102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
1482aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      physical_fp_register_intervals_[reg] = interval;
149102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
15086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
15186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(interval->GetRegister() == reg);
15286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  interval->AddRange(start, end);
15386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
15486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
15577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilvoid RegisterAllocator::BlockRegisters(size_t start, size_t end, bool caller_save_only) {
15677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) {
15777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    if (!caller_save_only || !codegen_->IsCoreCalleeSaveRegister(i)) {
15877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      BlockRegister(Location::RegisterLocation(i), start, end);
15977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    }
16077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  }
16177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) {
16277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    if (!caller_save_only || !codegen_->IsFloatingPointCalleeSaveRegister(i)) {
16377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      BlockRegister(Location::FpuRegisterLocation(i), start, end);
16477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    }
16577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  }
16677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil}
16777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
16886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegistersInternal() {
1693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Iterate post-order, to ensure the list is sorted, and the last added interval
1703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // is the one with the lowest start position.
1710d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearPostOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
1723946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    HBasicBlock* block = it.Current();
173277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
174277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe         back_it.Advance()) {
175277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      ProcessInstruction(back_it.Current());
1763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
177277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
178277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      ProcessInstruction(inst_it.Current());
1793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
18077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
18115bd22849ee6a1ffb3fb3630f686c2870bdf1bbcNicolas Geoffray    if (block->IsCatchBlock() ||
18215bd22849ee6a1ffb3fb3630f686c2870bdf1bbcNicolas Geoffray        (block->GetLoopInformation() != nullptr && block->GetLoopInformation()->IsIrreducible())) {
18315bd22849ee6a1ffb3fb3630f686c2870bdf1bbcNicolas Geoffray      // By blocking all registers at the top of each catch block or irreducible loop, we force
18415bd22849ee6a1ffb3fb3630f686c2870bdf1bbcNicolas Geoffray      // intervals belonging to the live-in set of the catch/header block to be spilled.
18515bd22849ee6a1ffb3fb3630f686c2870bdf1bbcNicolas Geoffray      // TODO(ngeoffray): Phis in this block could be allocated in register.
18677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      size_t position = block->GetLifetimeStart();
18777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      BlockRegisters(position, position + 1);
18877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    }
1893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
190a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  number_of_registers_ = codegen_->GetNumberOfCoreRegisters();
1925233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_,
1935233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko                                                    kArenaAllocRegisterAllocator);
1943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  processing_core_registers_ = true;
1953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  unhandled_ = &unhandled_core_intervals_;
1962aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* fixed : physical_core_register_intervals_) {
197102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (fixed != nullptr) {
198296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is added to inactive_ instead of unhandled_.
199296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // It's also the only type of inactive interval whose start position
200296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // can be after the current interval during linear scan.
201296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is never split and never moves to unhandled_.
2022aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      inactive_.push_back(fixed);
203102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
204102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  }
2053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LinearScan();
206a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
2072aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  inactive_.clear();
2082aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  active_.clear();
2092aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  handled_.clear();
210a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
2113946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters();
2125233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_,
2135233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko                                                    kArenaAllocRegisterAllocator);
2143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  processing_core_registers_ = false;
2153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  unhandled_ = &unhandled_fp_intervals_;
2162aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* fixed : physical_fp_register_intervals_) {
217102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (fixed != nullptr) {
218296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is added to inactive_ instead of unhandled_.
219296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // It's also the only type of inactive interval whose start position
220296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // can be after the current interval during linear scan.
221296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is never split and never moves to unhandled_.
2222aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      inactive_.push_back(fixed);
223102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
224102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  }
2253946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LinearScan();
2263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray}
22786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
2283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::ProcessInstruction(HInstruction* instruction) {
2293946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LocationSummary* locations = instruction->GetLocations();
2303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  size_t position = instruction->GetLifetimePosition();
2313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (locations == nullptr) return;
2333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Create synthesized intervals for temporaries.
2353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < locations->GetTempCount(); ++i) {
2363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    Location temp = locations->GetTemp(i);
23752839d17c06175e19ca4a093fb878450d1c4310dNicolas Geoffray    if (temp.IsRegister() || temp.IsFpuRegister()) {
238102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(temp, position, position + 1);
23945b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray      // Ensure that an explicit temporary register is marked as being allocated.
24045b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray      codegen_->AddAllocatedRegister(temp);
2413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
242102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      DCHECK(temp.IsUnallocated());
2435368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      switch (temp.GetPolicy()) {
2445368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        case Location::kRequiresRegister: {
2455368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LiveInterval* interval =
2465368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain              LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt);
2472aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          temp_intervals_.push_back(interval);
248f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          interval->AddTempUse(instruction, i);
2492aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          unhandled_core_intervals_.push_back(interval);
2505368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          break;
2515368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        }
2525368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
2535368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        case Location::kRequiresFpuRegister: {
2545368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LiveInterval* interval =
2555368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain              LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimDouble);
2562aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          temp_intervals_.push_back(interval);
257f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          interval->AddTempUse(instruction, i);
258840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
2595588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray            interval->AddHighInterval(/* is_temp */ true);
260840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            LiveInterval* high = interval->GetHighInterval();
2612aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko            temp_intervals_.push_back(high);
2622aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko            unhandled_fp_intervals_.push_back(high);
263840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          }
2642aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          unhandled_fp_intervals_.push_back(interval);
2655368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          break;
2665368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        }
2675368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
2685368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        default:
2695368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LOG(FATAL) << "Unexpected policy for temporary location "
2705368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                     << temp.GetPolicy();
2715368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      }
272a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
273a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
27486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
2753bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  bool core_register = (instruction->GetType() != Primitive::kPrimDouble)
2763bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      && (instruction->GetType() != Primitive::kPrimFloat);
2773bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
2788158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames  if (locations->NeedsSafepoint()) {
279c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray    if (codegen_->IsLeafMethod()) {
280c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      // TODO: We do this here because we do not want the suspend check to artificially
281c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      // create live registers. We should find another place, but this is currently the
282c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      // simplest.
283c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      DCHECK(instruction->IsSuspendCheckEntry());
284c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      instruction->GetBlock()->RemoveInstruction(instruction);
285c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      return;
2863bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
2872aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    safepoints_.push_back(instruction);
2883bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    if (locations->OnlyCallsOnSlowPath()) {
2893bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // We add a synthesized range at this position to record the live registers
2903bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // at this position. Ideally, we could just update the safepoints when locations
2913bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // are updated, but we currently need to know the full stack size before updating
2923bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // locations (because of parameters and the fact that we don't have a frame pointer).
2933bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // And knowing the full stack size requires to know the maximum number of live
2943bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // registers at calls in slow paths.
2953bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // By adding the following interval in the algorithm, we can compute this
2963bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // maximum before updating locations.
2973bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction);
298acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      interval->AddRange(position, position + 1);
29987d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray      AddSorted(&unhandled_core_intervals_, interval);
30087d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray      AddSorted(&unhandled_fp_intervals_, interval);
3013bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
3023bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  }
3033bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
3043bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  if (locations->WillCall()) {
30577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    BlockRegisters(position, position + 1, /* caller_save_only */ true);
3063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < instruction->InputCount(); ++i) {
3093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    Location input = locations->InAt(i);
310102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (input.IsRegister() || input.IsFpuRegister()) {
311102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(input, position, position + 1);
312840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (input.IsPair()) {
313840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      BlockRegister(input.ToLow(), position, position + 1);
314840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      BlockRegister(input.ToHigh(), position, position + 1);
3153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
3163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LiveInterval* current = instruction->GetLiveInterval();
3193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current == nullptr) return;
3203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3212aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  ArenaVector<LiveInterval*>& unhandled = core_register
322102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? unhandled_core_intervals_
323102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : unhandled_fp_intervals_;
324102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray
3252aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  DCHECK(unhandled.empty() || current->StartsBeforeOrAt(unhandled.back()));
32687d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray
327840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (codegen_->NeedsTwoRegisters(current->GetType())) {
328840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->AddHighInterval();
329840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
330840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
3312aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (size_t safepoint_index = safepoints_.size(); safepoint_index > 0; --safepoint_index) {
3322aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    HInstruction* safepoint = safepoints_[safepoint_index - 1u];
3335588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    size_t safepoint_position = safepoint->GetLifetimePosition();
3345588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray
3355588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    // Test that safepoints are ordered in the optimal way.
3362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK(safepoint_index == safepoints_.size() ||
3372aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko           safepoints_[safepoint_index]->GetLifetimePosition() < safepoint_position);
3385588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray
3395588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    if (safepoint_position == current->GetStart()) {
3405588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      // The safepoint is for this instruction, so the location of the instruction
3415588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      // does not need to be saved.
3422aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      DCHECK_EQ(safepoint_index, safepoints_.size());
3435588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      DCHECK_EQ(safepoint, instruction);
3445588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      continue;
3455588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    } else if (current->IsDeadAt(safepoint_position)) {
3465588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      break;
3475588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    } else if (!current->Covers(safepoint_position)) {
3485588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      // Hole in the interval.
3495588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      continue;
3505588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    }
3515588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    current->AddSafepoint(safepoint);
3525588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray  }
3533fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  current->ResetSearchCache();
3545588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray
3553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Some instructions define their output in fixed register/stack slot. We need
3563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // to ensure we know these locations before doing register allocation. For a
3573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // given register, we create an interval that covers these locations. The register
3583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // will be unavailable at these locations when trying to allocate one for an
3593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // interval.
3603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  //
3613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // The backwards walking ensures the ranges are ordered on increasing start positions.
3623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  Location output = locations->Out();
363d0d4852847432368b090c184d6639e573538dccfCalin Juravle  if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
364d0d4852847432368b090c184d6639e573538dccfCalin Juravle    Location first = locations->InAt(0);
365d0d4852847432368b090c184d6639e573538dccfCalin Juravle    if (first.IsRegister() || first.IsFpuRegister()) {
366d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetFrom(position + 1);
367d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetRegister(first.reg());
368840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (first.IsPair()) {
369840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      current->SetFrom(position + 1);
370840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      current->SetRegister(first.low());
371840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = current->GetHighInterval();
372840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetRegister(first.high());
373840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetFrom(position + 1);
374d0d4852847432368b090c184d6639e573538dccfCalin Juravle    }
375d0d4852847432368b090c184d6639e573538dccfCalin Juravle  } else if (output.IsRegister() || output.IsFpuRegister()) {
3763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Shift the interval's start by one to account for the blocked register.
3773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetFrom(position + 1);
37856b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray    current->SetRegister(output.reg());
379102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    BlockRegister(output, position, position + 1);
380840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (output.IsPair()) {
381840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->SetFrom(position + 1);
382840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->SetRegister(output.low());
383840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    LiveInterval* high = current->GetHighInterval();
384840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    high->SetRegister(output.high());
385840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    high->SetFrom(position + 1);
386840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    BlockRegister(output.ToLow(), position, position + 1);
387840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    BlockRegister(output.ToHigh(), position, position + 1);
3883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
3893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetSpillSlot(output.GetStackIndex());
390840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
391840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(output.IsUnallocated() || output.IsConstant());
3923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
39477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  if (instruction->IsPhi() && instruction->AsPhi()->IsCatchPhi()) {
39577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    AllocateSpillSlotForCatchPhi(instruction->AsPhi());
39677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  }
39777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
3983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // If needed, add interval to the list of unhandled intervals.
3993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasSpillSlot() || instruction->IsConstant()) {
400c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    // Split just before first register use.
4013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    size_t first_register_use = current->FirstRegisterUse();
4023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (first_register_use != kNoLifetime) {
4038cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray      LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1);
404b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      // Don't add directly to `unhandled`, it needs to be sorted and the start
4053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // of this new interval might be after intervals already in the list.
4063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      AddSorted(&unhandled, split);
4073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
4083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // Nothing to do, we won't allocate a register for this value.
4093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
4103946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
411b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // Don't add directly to `unhandled`, temp or safepoint intervals
412b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // for this instruction may have been added, and those can be
413b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // processed first.
414b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    AddSorted(&unhandled, current);
4153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
416a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
417a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
41831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayclass AllRangesIterator : public ValueObject {
41931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray public:
42031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  explicit AllRangesIterator(LiveInterval* interval)
42131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      : current_interval_(interval),
42231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_(interval->GetFirstRange()) {}
42331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
42431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  bool Done() const { return current_interval_ == nullptr; }
42531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* CurrentRange() const { return current_range_; }
42631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* CurrentInterval() const { return current_interval_; }
42731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
42831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  void Advance() {
42931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    current_range_ = current_range_->GetNext();
43031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    if (current_range_ == nullptr) {
43131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      current_interval_ = current_interval_->GetNextSibling();
43231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current_interval_ != nullptr) {
43331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_ = current_interval_->GetFirstRange();
43431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
43531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
43631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
43731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
43831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray private:
43931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* current_interval_;
44031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* current_range_;
44131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
44231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
44331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray};
44431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
44586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
44686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // To simplify unit testing, we eagerly create the array of intervals, and
44786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // call the helper method.
4482aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  ArenaVector<LiveInterval*> intervals(allocator_->Adapter(kArenaAllocRegisterAllocator));
44986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
45086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
45186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
4522aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      intervals.push_back(instruction->GetLiveInterval());
45386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
45486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
45586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
4562aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  const ArenaVector<LiveInterval*>* physical_register_intervals = processing_core_registers_
4572aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      ? &physical_core_register_intervals_
4582aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      : &physical_fp_register_intervals_;
4592aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* fixed : *physical_register_intervals) {
4602aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    if (fixed != nullptr) {
4612aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      intervals.push_back(fixed);
46286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
46386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
46486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
4652aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* temp : temp_intervals_) {
4663946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, temp)) {
4672aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      intervals.push_back(temp);
4683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
4693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
4703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
471776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  return ValidateIntervals(intervals, GetNumberOfSpillSlots(), reserved_out_slots_, *codegen_,
4723946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                           allocator_, processing_core_registers_, log_fatal_on_failure);
47386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
47486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
4752aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Markobool RegisterAllocator::ValidateIntervals(const ArenaVector<LiveInterval*>& intervals,
47631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray                                          size_t number_of_spill_slots,
4773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                                          size_t number_of_out_slots,
478a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          const CodeGenerator& codegen,
479a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          ArenaAllocator* allocator,
480a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool processing_core_registers,
481a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool log_fatal_on_failure) {
482a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t number_of_registers = processing_core_registers
483a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      ? codegen.GetNumberOfCoreRegisters()
484a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : codegen.GetNumberOfFloatingPointRegisters();
4852aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  ArenaVector<ArenaBitVector*> liveness_of_values(
4862aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      allocator->Adapter(kArenaAllocRegisterAllocator));
4872aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  liveness_of_values.reserve(number_of_registers + number_of_spill_slots);
488a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
489a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Allocate a bit vector per register. A live interval that has a register
490a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // allocated will populate the associated bit vector based on its live ranges.
49131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
4922aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    liveness_of_values.push_back(new (allocator) ArenaBitVector(allocator, 0, true));
493a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
494a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
4952aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* start_interval : intervals) {
4962aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    for (AllRangesIterator it(start_interval); !it.Done(); it.Advance()) {
49731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      LiveInterval* current = it.CurrentInterval();
49886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* defined_by = current->GetParent()->GetDefinedBy();
49986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->GetParent()->HasSpillSlot()
50076b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray           // Parameters and current method have their own stack slot.
50176b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray           && !(defined_by != nullptr && (defined_by->IsParameterValue()
50276b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray                                          || defined_by->IsCurrentMethod()))) {
5032aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        BitVector* liveness_of_spill_slot = liveness_of_values[number_of_registers
5043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            + current->GetParent()->GetSpillSlot() / kVRegSize
5052aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko            - number_of_out_slots];
50631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
50731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_spill_slot->IsBitSet(j)) {
50831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            if (log_fatal_on_failure) {
50931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              std::ostringstream message;
51031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              message << "Spill slot conflict at " << j;
51131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              LOG(FATAL) << message.str();
51231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            } else {
51331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              return false;
51431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            }
51531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          } else {
51631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_spill_slot->SetBit(j);
51731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          }
51831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        }
519a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
52031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
52131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current->HasRegister()) {
52245b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray        if (kIsDebugBuild && log_fatal_on_failure && !current->IsFixed()) {
52345b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray          // Only check when an error is fatal. Only tests code ask for non-fatal failures
52445b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray          // and test code may not properly fill the right information to the code generator.
52545b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray          CHECK(codegen.HasAllocatedRegister(processing_core_registers, current->GetRegister()));
52645b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray        }
5272aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        BitVector* liveness_of_register = liveness_of_values[current->GetRegister()];
52831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
52931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_register->IsBitSet(j)) {
530829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray            if (current->IsUsingInputRegister() && current->CanUseInputRegister()) {
531829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray              continue;
532829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray            }
533a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            if (log_fatal_on_failure) {
534a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              std::ostringstream message;
5353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "Register conflict at " << j << " ";
5363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              if (defined_by != nullptr) {
5373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                message << "(" << defined_by->DebugName() << ")";
5383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              }
5393946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "for ";
540a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              if (processing_core_registers) {
541a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpCoreRegister(message, current->GetRegister());
542a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              } else {
543a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpFloatingPointRegister(message, current->GetRegister());
544a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              }
545a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              LOG(FATAL) << message.str();
546a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            } else {
547a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              return false;
548a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            }
549a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          } else {
55031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_register->SetBit(j);
551a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          }
552a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        }
55331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
55431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
555a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
556a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
557a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
558a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
55986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
560a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  interval->Dump(stream);
561a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << ": ";
562a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (interval->HasRegister()) {
563102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (interval->IsFloatingPoint()) {
56486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
565102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
566102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      codegen_->DumpCoreRegister(stream, interval->GetRegister());
567a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
568a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
569a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    stream << "spilled";
570a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
571a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << std::endl;
572a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
573a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
574296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yangvoid RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
575296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "inactive: " << std::endl;
5762aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* inactive_interval : inactive_) {
5772aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DumpInterval(stream, inactive_interval);
578296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
579296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "active: " << std::endl;
5802aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* active_interval : active_) {
5812aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DumpInterval(stream, active_interval);
582296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
583296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "unhandled: " << std::endl;
584296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  auto unhandled = (unhandled_ != nullptr) ?
585296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      unhandled_ : &unhandled_core_intervals_;
5862aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* unhandled_interval : *unhandled) {
5872aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DumpInterval(stream, unhandled_interval);
588296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
589296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "handled: " << std::endl;
5902aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* handled_interval : handled_) {
5912aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DumpInterval(stream, handled_interval);
592296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
593296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang}
594296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
595a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// By the book implementation of a linear scan register allocator.
596a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::LinearScan() {
5972aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  while (!unhandled_->empty()) {
598a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (1) Remove interval with the lowest start position from unhandled.
5992aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    LiveInterval* current = unhandled_->back();
6002aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    unhandled_->pop_back();
6012e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray
6022e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // Make sure the interval is an expected state.
6033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    DCHECK(!current->IsFixed() && !current->HasSpillSlot());
6042e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // Make sure we are going in the right order.
6052aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK(unhandled_->empty() || unhandled_->back()->GetStart() >= current->GetStart());
6062e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // Make sure a low interval is always with a high.
6072aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK(!current->IsLowInterval() || unhandled_->back()->IsHighInterval());
6082e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // Make sure a high interval is always with a low.
6092e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    DCHECK(current->IsLowInterval() ||
6102aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko           unhandled_->empty() ||
6112aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko           !unhandled_->back()->IsHighInterval());
61287d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray
613a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t position = current->GetStart();
614a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
615296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Remember the inactive_ size here since the ones moved to inactive_ from
616296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // active_ below shouldn't need to be re-checked.
6172aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    size_t inactive_intervals_to_handle = inactive_.size();
618296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
619a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (2) Remove currently active intervals that are dead at this position.
620a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move active intervals that have a lifetime hole at this position
621a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     to inactive.
622b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko    auto active_kept_end = std::remove_if(
623b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        active_.begin(),
624b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        active_.end(),
625b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        [this, position](LiveInterval* interval) {
626b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          if (interval->IsDeadAt(position)) {
627b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            handled_.push_back(interval);
628b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return true;
629b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          } else if (!interval->Covers(position)) {
630b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            inactive_.push_back(interval);
631b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return true;
632b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          } else {
633b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return false;  // Keep this interval.
634b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          }
635b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        });
6362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    active_.erase(active_kept_end, active_.end());
637a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
638a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (3) Remove currently inactive intervals that are dead at this position.
639a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move inactive intervals that cover this position to active.
6402aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    auto inactive_to_handle_end = inactive_.begin() + inactive_intervals_to_handle;
641b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko    auto inactive_kept_end = std::remove_if(
642b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        inactive_.begin(),
643b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        inactive_to_handle_end,
644b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        [this, position](LiveInterval* interval) {
645b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          DCHECK(interval->GetStart() < position || interval->IsFixed());
646b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          if (interval->IsDeadAt(position)) {
647b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            handled_.push_back(interval);
648b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return true;
649b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          } else if (interval->Covers(position)) {
650b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            active_.push_back(interval);
651b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return true;
652b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          } else {
653b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return false;  // Keep this interval.
654b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          }
655b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        });
6562aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    inactive_.erase(inactive_kept_end, inactive_to_handle_end);
657a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
658acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    if (current->IsSlowPathSafepoint()) {
659acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Synthesized interval to record the maximum number of live registers
660acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // at safepoints. No need to allocate a register for it.
661f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      if (processing_core_registers_) {
662f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_core_registers_ =
6632aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          std::max(maximum_number_of_live_core_registers_, active_.size());
664f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      } else {
665f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_fp_registers_ =
6662aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          std::max(maximum_number_of_live_fp_registers_, active_.size());
667f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      }
6682aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      DCHECK(unhandled_->empty() || unhandled_->back()->GetStart() > current->GetStart());
669acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      continue;
670acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    }
671acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray
672840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
673840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(!current->HasRegister());
674840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // Allocating the low part was unsucessful. The splitted interval for the high part
675840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // will be handled next (it is in the `unhandled_` list).
676840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
677840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
678840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
679a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (4) Try to find an available register.
680a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    bool success = TryAllocateFreeReg(current);
681a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
682a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (5) If no register could be found, we need to spill.
683a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (!success) {
684a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      success = AllocateBlockedReg(current);
685a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
686a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
687a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (6) If the interval had a register allocated, add it to the list of active
688a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     intervals.
689a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (success) {
690988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      codegen_->AddAllocatedRegister(processing_core_registers_
691988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          ? Location::RegisterLocation(current->GetRegister())
692988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          : Location::FpuRegisterLocation(current->GetRegister()));
6932aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      active_.push_back(current);
694840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
695840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
696840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
697a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
698a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
699a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
700a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
701829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffraystatic void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) {
702829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  DCHECK(!interval->IsHighInterval());
703829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // Note that the same instruction may occur multiple times in the input list,
704829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // so `free_until` may have changed already.
7053fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  // Since `position` is not the current scan position, we need to use CoversSlow.
706829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  if (interval->IsDeadAt(position)) {
707829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // Set the register to be free. Note that inactive intervals might later
708829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // update this.
709829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    free_until[interval->GetRegister()] = kMaxLifetimePosition;
710829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (interval->HasHighInterval()) {
711829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      DCHECK(interval->GetHighInterval()->IsDeadAt(position));
712829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition;
713829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
7143fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  } else if (!interval->CoversSlow(position)) {
715829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // The interval becomes inactive at `defined_by`. We make its register
716829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // available only until the next use strictly after `defined_by`.
717829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    free_until[interval->GetRegister()] = interval->FirstUseAfter(position);
718829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (interval->HasHighInterval()) {
7193fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil      DCHECK(!interval->GetHighInterval()->CoversSlow(position));
720829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()];
721829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
722829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  }
723829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray}
724829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray
725a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find a free register. If multiple are found, pick the register that
726a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// is free the longest.
727a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
728a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* free_until = registers_array_;
729a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
730a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers to be free.
731a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
732a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    free_until[i] = kMaxLifetimePosition;
733a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
734a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
735296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  // For each active interval, set its register to not free.
7362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* interval : active_) {
737296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(interval->HasRegister());
738296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    free_until[interval->GetRegister()] = 0;
739296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
740296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
741829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // An interval that starts an instruction (that is, it is not split), may
742829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // re-use the registers used by the inputs of that instruciton, based on the
743829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // location summary.
744829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  HInstruction* defined_by = current->GetDefinedBy();
745829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  if (defined_by != nullptr && !current->IsSplit()) {
746829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    LocationSummary* locations = defined_by->GetLocations();
747829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) {
74894015b939060f5041d408d48717f22443e55b6adNicolas Geoffray      for (size_t i = 0, e = defined_by->InputCount(); i < e; ++i) {
749829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // Take the last interval of the input. It is the location of that interval
750829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // that will be used at `defined_by`.
75194015b939060f5041d408d48717f22443e55b6adNicolas Geoffray        LiveInterval* interval = defined_by->InputAt(i)->GetLiveInterval()->GetLastSibling();
752829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // Note that interval may have not been processed yet.
753829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // TODO: Handle non-split intervals last in the work list.
75494015b939060f5041d408d48717f22443e55b6adNicolas Geoffray        if (locations->InAt(i).IsValid()
75594015b939060f5041d408d48717f22443e55b6adNicolas Geoffray            && interval->HasRegister()
75694015b939060f5041d408d48717f22443e55b6adNicolas Geoffray            && interval->SameRegisterKind(*current)) {
757829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // The input must be live until the end of `defined_by`, to comply to
758829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // the linear scan algorithm. So we use `defined_by`'s end lifetime
759829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // position to check whether the input is dead or is inactive after
760829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // `defined_by`.
7613fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil          DCHECK(interval->CoversSlow(defined_by->GetLifetimePosition()));
762829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          size_t position = defined_by->GetLifetimePosition() + 1;
763829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          FreeIfNotCoverAt(interval, position, free_until);
764829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        }
765829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      }
766829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
767829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  }
768829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray
769a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, set its register to be free until
770a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // the next intersection with `current`.
7712aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* inactive : inactive_) {
772296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
773296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
774296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
775296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
776296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
777296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
778296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
779296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
780296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
781296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
782296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
783a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
784296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (free_until[inactive->GetRegister()] == 0) {
785296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Already used by some active interval. No need to intersect.
786296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
787296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
788a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
789a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (next_intersection != kNoLifetime) {
790aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray      free_until[inactive->GetRegister()] =
791aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray          std::min(free_until[inactive->GetRegister()], next_intersection);
792a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
793a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
794a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7956c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
7963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasRegister()) {
7973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Some instructions have a fixed register output.
7983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    reg = current->GetRegister();
799840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (free_until[reg] == 0) {
800840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(current->IsHighInterval());
801840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // AllocateBlockedReg will spill the holder of the register.
802840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      return false;
803840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
8043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
805840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
806fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray    int hint = current->FindFirstRegisterHint(free_until, liveness_);
807f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray    if ((hint != kNoRegister)
808f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray        // For simplicity, if the hint we are getting for a pair cannot be used,
809f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray        // we are just going to allocate a new pair.
810f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray        && !(current->IsLowInterval() && IsBlocked(GetHighForLowRegister(hint)))) {
81101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      DCHECK(!IsBlocked(hint));
81201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      reg = hint;
813840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (current->IsLowInterval()) {
8146c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      reg = FindAvailableRegisterPair(free_until, current->GetStart());
81501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    } else {
8168826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      reg = FindAvailableRegister(free_until, current);
817a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
818a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
819a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
8206c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
821a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // If we could not find a register, we need to spill.
822840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (free_until[reg] == 0) {
823840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return false;
824840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
825840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
826234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (current->IsLowInterval()) {
827234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    // If the high register of this interval is not available, we need to spill.
828234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    int high_reg = current->GetHighInterval()->GetRegister();
829234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (high_reg == kNoRegister) {
830234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      high_reg = GetHighForLowRegister(reg);
831234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    }
832234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (free_until[high_reg] == 0) {
833234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      return false;
834234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    }
835a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
836a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
837a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  current->SetRegister(reg);
838a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (!current->IsDeadAt(free_until[reg])) {
839a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the register is only available for a subset of live ranges
8408272688499c2232355db34d94057983fd436173dNicolas Geoffray    // covered by `current`, split `current` before the position where
841a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // the register is not available anymore.
8428272688499c2232355db34d94057983fd436173dNicolas Geoffray    LiveInterval* split = SplitBetween(current, current->GetStart(), free_until[reg]);
843a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(split != nullptr);
8443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    AddSorted(unhandled_, split);
845a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
846a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
847a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
848a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
849a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::IsBlocked(int reg) const {
850102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return processing_core_registers_
851102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? blocked_core_registers_[reg]
852102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : blocked_fp_registers_[reg];
853a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
854a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
8556c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffrayint RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const {
8566c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
857840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Pick the register pair that is used the last.
858840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
859840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(i)) continue;
860840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (!IsLowRegister(i)) continue;
861840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int high_register = GetHighForLowRegister(i);
862840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(high_register)) continue;
863840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int existing_high_register = GetHighForLowRegister(reg);
8646c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if ((reg == kNoRegister) || (next_use[i] >= next_use[reg]
865840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                        && next_use[high_register] >= next_use[existing_high_register])) {
866840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
867840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition
868840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          && next_use[high_register] == kMaxLifetimePosition) {
869840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        break;
870840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
8716c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) {
8726c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If one of the current register is known to be unavailable, just unconditionally
8736c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // try a new one.
8746c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      reg = i;
875840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
876840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
877840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
878840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
879840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
8808826f67ad53099021f6442364348fa66729288d7Nicolas Geoffraybool RegisterAllocator::IsCallerSaveRegister(int reg) const {
8818826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  return processing_core_registers_
8828826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      ? !codegen_->IsCoreCalleeSaveRegister(reg)
8838826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      : !codegen_->IsFloatingPointCalleeSaveRegister(reg);
8848826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray}
8858826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
8868826f67ad53099021f6442364348fa66729288d7Nicolas Geoffrayint RegisterAllocator::FindAvailableRegister(size_t* next_use, LiveInterval* current) const {
8878826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  // We special case intervals that do not span a safepoint to try to find a caller-save
8888826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  // register if one is available. We iterate from 0 to the number of registers,
8898826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  // so if there are caller-save registers available at the end, we continue the iteration.
8908826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  bool prefers_caller_save = !current->HasWillCallSafepoint();
8916c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
892840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
8938826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (IsBlocked(i)) {
8948826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      // Register cannot be used. Continue.
8958826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      continue;
8968826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    }
8978826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
8988826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    // Best case: we found a register fully available.
8998826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (next_use[i] == kMaxLifetimePosition) {
9008826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      if (prefers_caller_save && !IsCallerSaveRegister(i)) {
9018826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // We can get shorter encodings on some platforms by using
9028826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // small register numbers. So only update the candidate if the previous
9038826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // one was not available for the whole method.
9048826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        if (reg == kNoRegister || next_use[reg] != kMaxLifetimePosition) {
9058826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray          reg = i;
9068826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        }
9078826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // Continue the iteration in the hope of finding a caller save register.
9088826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        continue;
9098826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      } else {
9108826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        reg = i;
9118826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // We know the register is good enough. Return it.
9128826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        break;
9138826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      }
9148826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    }
9158826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
9168826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    // If we had no register before, take this one as a reference.
9178826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (reg == kNoRegister) {
918840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
9198826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      continue;
9208826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    }
9218826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
9228826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    // Pick the register that is used the last.
9238826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (next_use[i] > next_use[reg]) {
9248826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      reg = i;
9258826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      continue;
926840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
927840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
928840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
929840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
930840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
9312aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko// Remove interval and its other half if any. Return iterator to the following element.
9322aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Markostatic ArenaVector<LiveInterval*>::iterator RemoveIntervalAndPotentialOtherHalf(
9332aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    ArenaVector<LiveInterval*>* intervals, ArenaVector<LiveInterval*>::iterator pos) {
9342aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  DCHECK(intervals->begin() <= pos && pos < intervals->end());
9352aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  LiveInterval* interval = *pos;
9362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  if (interval->IsLowInterval()) {
9372aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK(pos + 1 < intervals->end());
9382aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK_EQ(*(pos + 1), interval->GetHighInterval());
9392aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    return intervals->erase(pos, pos + 2);
9402aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  } else if (interval->IsHighInterval()) {
9412aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK(intervals->begin() < pos);
9422aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK_EQ(*(pos - 1), interval->GetLowInterval());
9432aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    return intervals->erase(pos - 1, pos + 1);
9442aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  } else {
9452aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    return intervals->erase(pos);
9462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  }
9472aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko}
9482aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko
949234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraybool RegisterAllocator::TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
950234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                                 size_t first_register_use,
951234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                                 size_t* next_use) {
9522aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (auto it = active_.begin(), end = active_.end(); it != end; ++it) {
9532aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    LiveInterval* active = *it;
9546c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    DCHECK(active->HasRegister());
955234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (active->IsFixed()) continue;
956234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (active->IsHighInterval()) continue;
957234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (first_register_use > next_use[active->GetRegister()]) continue;
958234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
9592e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // Split the first interval found that is either:
9602e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // 1) A non-pair interval.
9612e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // 2) A pair interval whose high is not low + 1.
9622e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // 3) A pair interval whose low is not even.
9632e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    if (!active->IsLowInterval() ||
9642e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray        IsLowOfUnalignedPairInterval(active) ||
9652e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray        !IsLowRegister(active->GetRegister())) {
9666c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* split = Split(active, position);
9676c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      if (split != active) {
9682aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        handled_.push_back(active);
9696c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      }
9702aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      RemoveIntervalAndPotentialOtherHalf(&active_, it);
9716c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
9726c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      return true;
9736c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
9746c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  }
9756c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  return false;
9766c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray}
9776c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
978a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval
979a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register
980a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead.
981a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
982a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t first_register_use = current->FirstRegisterUse();
983da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray  if (current->HasRegister()) {
984da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    DCHECK(current->IsHighInterval());
985da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // The low interval has allocated the register for the high interval. In
986da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // case the low interval had to split both intervals, we may end up in a
987da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // situation where the high interval does not have a register use anymore.
988da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // We must still proceed in order to split currently active and inactive
989da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // uses of the high interval's register, and put the high interval in the
990da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // active set.
991da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    DCHECK(first_register_use != kNoLifetime || (current->GetNextSibling() != nullptr));
992da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray  } else if (first_register_use == kNoLifetime) {
99331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
994a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
995a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
996a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9971ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray  // We use the first use to compare with other intervals. If this interval
9981ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray  // is used after any active intervals, we will spill this interval.
9991ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray  size_t first_use = current->FirstUseAfter(current->GetStart());
10001ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray
1001a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers as not being used.
1002a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* next_use = registers_array_;
1003a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
1004a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    next_use[i] = kMaxLifetimePosition;
1005a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1006a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1007a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each active interval, find the next use of its register after the
1008a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
10092aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* active : active_) {
1010a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(active->HasRegister());
101186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (active->IsFixed()) {
101286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      next_use[active->GetRegister()] = current->GetStart();
101386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
10141ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray      size_t use = active->FirstUseAfter(current->GetStart());
101586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (use != kNoLifetime) {
101686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[active->GetRegister()] = use;
101786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1018a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1019a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1020a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1021a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, find the next use of its register after the
1022a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
10232aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* inactive : inactive_) {
1024296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
1025296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
1026296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
1027296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
1028296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
1029296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
1030296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
1031296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
1032296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
1033296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
1034a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
103586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
103686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_intersection != kNoLifetime) {
103786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (inactive->IsFixed()) {
103886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[inactive->GetRegister()] =
103986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            std::min(next_intersection, next_use[inactive->GetRegister()]);
104086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else {
10411ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray        size_t use = inactive->FirstUseAfter(current->GetStart());
104286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (use != kNoLifetime) {
104386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
104486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
104586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1046a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1047a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1048a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10496c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
10506c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  bool should_spill = false;
1051840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (current->HasRegister()) {
1052840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(current->IsHighInterval());
1053840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = current->GetRegister();
10546c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // When allocating the low part, we made sure the high register was available.
10551ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray    DCHECK_LT(first_use, next_use[reg]);
1056840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (current->IsLowInterval()) {
1057da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    reg = FindAvailableRegisterPair(next_use, first_use);
10586c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // We should spill if both registers are not available.
10591ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray    should_spill = (first_use >= next_use[reg])
10601ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray      || (first_use >= next_use[GetHighForLowRegister(reg)]);
1061840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
1062840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
10638826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    reg = FindAvailableRegister(next_use, current);
10641ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray    should_spill = (first_use >= next_use[reg]);
1065a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1066a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10676c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
10686c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  if (should_spill) {
1069840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
1070234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    bool is_allocation_at_use_site = (current->GetStart() >= (first_register_use - 1));
1071da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    if (is_allocation_at_use_site) {
1072da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray      if (!current->IsLowInterval()) {
1073da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray        DumpInterval(std::cerr, current);
1074da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray        DumpAllIntervals(std::cerr);
1075da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray        // This situation has the potential to infinite loop, so we make it a non-debug CHECK.
1076da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray        HInstruction* at = liveness_.GetInstructionFromPosition(first_register_use / 2);
1077da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray        CHECK(false) << "There is not enough registers available for "
1078da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray          << current->GetParent()->GetDefinedBy()->DebugName() << " "
1079da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray          << current->GetParent()->GetDefinedBy()->GetId()
1080da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray          << " at " << first_register_use - 1 << " "
1081da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray          << (at == nullptr ? "" : at->DebugName());
1082da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray      }
1083da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray
10846c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If we're allocating a register for `current` because the instruction at
10856c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // that position requires it, but we think we should spill, then there are
1086234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // non-pair intervals or unaligned pair intervals blocking the allocation.
1087234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // We split the first interval found, and put ourselves first in the
1088234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // `unhandled_` list.
1089da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray      bool success = TrySplitNonPairOrUnalignedPairIntervalAt(current->GetStart(),
1090da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray                                                              first_register_use,
1091da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray                                                              next_use);
1092da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray      DCHECK(success);
10932aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      LiveInterval* existing = unhandled_->back();
10946c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK(existing->IsHighInterval());
10956c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK_EQ(existing->GetLowInterval(), current);
10962aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      unhandled_->push_back(current);
10976c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else {
10986c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If the first use of that instruction is after the last use of the found
10996c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // register, we split this interval just before its first register use.
11006c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AllocateSpillSlotFor(current);
11018cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray      LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1);
1102da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray      DCHECK(current != split);
11036c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
11046c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
1105a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
1106a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
1107a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Use this register and spill the active and inactives interval that
1108a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // have that register.
1109a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    current->SetRegister(reg);
1110a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11112aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    for (auto it = active_.begin(), end = active_.end(); it != end; ++it) {
11122aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      LiveInterval* active = *it;
1113a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (active->GetRegister() == reg) {
111486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(!active->IsFixed());
1115a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        LiveInterval* split = Split(active, current->GetStart());
1116dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        if (split != active) {
11172aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          handled_.push_back(active);
1118dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        }
11192aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        RemoveIntervalAndPotentialOtherHalf(&active_, it);
11203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        AddSorted(unhandled_, split);
1121a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        break;
1122a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
1123a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1124a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11252aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    // NOTE: Retrieve end() on each iteration because we're removing elements in the loop body.
11262aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    for (auto it = inactive_.begin(); it != inactive_.end(); ) {
11272aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      LiveInterval* inactive = *it;
11282aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      bool erased = false;
1129a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (inactive->GetRegister() == reg) {
1130296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        if (!current->IsSplit() && !inactive->IsFixed()) {
1131296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Neither current nor inactive are fixed.
1132296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Thanks to SSA, a non-split interval starting in a hole of an
1133296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // inactive interval should never intersect with that inactive interval.
1134296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Only if it's not fixed though, because fixed intervals don't come from SSA.
1135296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
11362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        } else {
11372aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          size_t next_intersection = inactive->FirstIntersectionWith(current);
11382aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          if (next_intersection != kNoLifetime) {
11392aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko            if (inactive->IsFixed()) {
11402aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              LiveInterval* split = Split(current, next_intersection);
11412aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              DCHECK_NE(split, current);
11422aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              AddSorted(unhandled_, split);
11432aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko            } else {
11442aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              // Split at the start of `current`, which will lead to splitting
11452aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              // at the end of the lifetime hole of `inactive`.
11462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              LiveInterval* split = Split(inactive, current->GetStart());
11472aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              // If it's inactive, it must start before the current interval.
11482aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              DCHECK_NE(split, inactive);
11492aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              it = RemoveIntervalAndPotentialOtherHalf(&inactive_, it);
11502aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              erased = true;
11512aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              handled_.push_back(inactive);
11522aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              AddSorted(unhandled_, split);
11535b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            }
115486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          }
115586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
1156a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
11572aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      // If we have erased the element, `it` already points to the next element.
11582aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      // Otherwise we need to move to the next element.
11592aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      if (!erased) {
11602aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        ++it;
11612aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      }
1162a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1163a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1164a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return true;
1165a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1166a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1167a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11682aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Markovoid RegisterAllocator::AddSorted(ArenaVector<LiveInterval*>* array, LiveInterval* interval) {
1169c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray  DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
117086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t insert_at = 0;
11712aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (size_t i = array->size(); i > 0; --i) {
11722aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    LiveInterval* current = (*array)[i - 1u];
11736c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // High intervals must be processed right after their low equivalent.
11746c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (current->StartsAfter(interval) && !current->IsHighInterval()) {
117586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      insert_at = i;
1176a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      break;
1177acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
1178acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Ensure the slow path interval is the last to be processed at its location: we want the
1179acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // interval to know all live registers at this location.
11802aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      DCHECK(i == 1 || (*array)[i - 2u]->StartsAfter(current));
1181acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      insert_at = i;
1182acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      break;
1183a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1184a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1185840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
1186840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Insert the high interval before the low, to ensure the low is processed before.
11872aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  auto insert_pos = array->begin() + insert_at;
1188840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->HasHighInterval()) {
11892aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    array->insert(insert_pos, { interval->GetHighInterval(), interval });
1190840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (interval->HasLowInterval()) {
11912aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    array->insert(insert_pos, { interval, interval->GetLowInterval() });
11922aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  } else {
11932aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    array->insert(insert_pos, interval);
1194840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1195a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1196a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11978cbab3c4de3328b576454ce702d7748f56c44346Nicolas GeoffrayLiveInterval* RegisterAllocator::SplitBetween(LiveInterval* interval, size_t from, size_t to) {
1198fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  HBasicBlock* block_from = liveness_.GetBlockFromPosition(from / 2);
1199fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  HBasicBlock* block_to = liveness_.GetBlockFromPosition(to / 2);
12008cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  DCHECK(block_from != nullptr);
12018cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  DCHECK(block_to != nullptr);
12028cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
12038cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // Both locations are in the same block. We split at the given location.
12048cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  if (block_from == block_to) {
12058cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    return Split(interval, to);
12068cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  }
12078cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
1208fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  /*
1209fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * Non-linear control flow will force moves at every branch instruction to the new location.
1210fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * To avoid having all branches doing the moves, we find the next non-linear position and
1211fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * split the interval at this position. Take the following example (block number is the linear
1212fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * order position):
1213fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *
1214fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *     B1
1215fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *    /  \
1216fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *   B2  B3
1217fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *    \  /
1218fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *     B4
1219fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *
1220fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * B2 needs to split an interval, whose next use is in B4. If we were to split at the
1221fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * beginning of B4, B3 would need to do a move between B3 and B4 to ensure the interval
1222fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * is now in the correct location. It makes performance worst if the interval is spilled
1223fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * and both B2 and B3 need to reload it before entering B4.
1224fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *
1225fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * By splitting at B3, we give a chance to the register allocator to allocate the
1226fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * interval to the same register as in B1, and therefore avoid doing any
1227fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * moves in B3.
1228fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   */
1229fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  if (block_from->GetDominator() != nullptr) {
12306058455d486219994921b63a2d774dc9908415a2Vladimir Marko    for (HBasicBlock* dominated : block_from->GetDominator()->GetDominatedBlocks()) {
12316058455d486219994921b63a2d774dc9908415a2Vladimir Marko      size_t position = dominated->GetLifetimeStart();
1232fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray      if ((position > from) && (block_to->GetLifetimeStart() > position)) {
1233fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        // Even if we found a better block, we continue iterating in case
1234fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        // a dominated block is closer.
1235fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        // Note that dominated blocks are not sorted in liveness order.
12366058455d486219994921b63a2d774dc9908415a2Vladimir Marko        block_to = dominated;
1237fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        DCHECK_NE(block_to, block_from);
1238fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray      }
1239fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray    }
1240fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  }
1241fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray
12428cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // If `to` is in a loop, find the outermost loop header which does not contain `from`.
12438cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  for (HLoopInformationOutwardIterator it(*block_to); !it.Done(); it.Advance()) {
12448cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    HBasicBlock* header = it.Current()->GetHeader();
12458cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    if (block_from->GetLifetimeStart() >= header->GetLifetimeStart()) {
12468cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray      break;
12478cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    }
12488cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    block_to = header;
12498cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  }
12508cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
12518cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // Split at the start of the found block, to piggy back on existing moves
12528cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // due to resolution if non-linear control flow (see `ConnectSplitSiblings`).
12538cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  return Split(interval, block_to->GetLifetimeStart());
12548cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray}
12558cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
1256a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
1257840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  DCHECK_GE(position, interval->GetStart());
1258a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(!interval->IsDeadAt(position));
1259a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (position == interval->GetStart()) {
1260a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Spill slot will be allocated when handling `interval` again.
1261a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    interval->ClearRegister();
1262840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1263840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetHighInterval()->ClearRegister();
1264840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1265840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetLowInterval()->ClearRegister();
1266840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1267a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return interval;
1268a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
1269a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* new_interval = interval->SplitAt(position);
1270840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1271840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
1272840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetHighInterval(high);
1273840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetLowInterval(new_interval);
1274840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1275840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
1276840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetLowInterval(low);
1277840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      low->SetHighInterval(new_interval);
1278840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1279a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return new_interval;
1280a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1281a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1282a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
128331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
1284840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->IsHighInterval()) {
1285da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // The low interval already took care of allocating the spill slot.
1286da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    DCHECK(!interval->GetLowInterval()->HasRegister());
1287da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    DCHECK(interval->GetLowInterval()->GetParent()->HasSpillSlot());
1288840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return;
1289840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1290840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
129131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* parent = interval->GetParent();
129231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
129331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // An instruction gets a spill slot for its entire lifetime. If the parent
129431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // of this interval already has a spill slot, there is nothing to do.
129531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  if (parent->HasSpillSlot()) {
129631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    return;
129731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
129831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
129986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* defined_by = parent->GetDefinedBy();
130077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  DCHECK(!defined_by->IsPhi() || !defined_by->AsPhi()->IsCatchPhi());
130177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
130286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (defined_by->IsParameterValue()) {
130386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Parameters have their own stack slot.
130486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
130586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
130686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
130786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
130876b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray  if (defined_by->IsCurrentMethod()) {
130976b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray    parent->SetSpillSlot(0);
131076b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray    return;
131176b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray  }
131276b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray
131396f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  if (defined_by->IsConstant()) {
131496f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    // Constants don't need a spill slot.
131596f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    return;
131696f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  }
131796f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray
13182aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  ArenaVector<size_t>* spill_slots = nullptr;
1319776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  switch (interval->GetType()) {
1320776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimDouble:
1321776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &double_spill_slots_;
1322776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1323776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimLong:
1324776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &long_spill_slots_;
1325776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1326776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimFloat:
1327776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &float_spill_slots_;
1328776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1329776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimNot:
1330776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimInt:
1331776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimChar:
1332776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimByte:
1333776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimBoolean:
1334776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimShort:
1335776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &int_spill_slots_;
1336776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1337776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimVoid:
1338776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      LOG(FATAL) << "Unexpected type for interval " << interval->GetType();
1339776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  }
1340776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray
1341412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  // Find an available spill slot.
1342412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  size_t slot = 0;
13432aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (size_t e = spill_slots->size(); slot < e; ++slot) {
13442aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    if ((*spill_slots)[slot] <= parent->GetStart()
13452aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        && (slot == (e - 1) || (*spill_slots)[slot + 1] <= parent->GetStart())) {
1346412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray      break;
1347412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    }
1348412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
1349412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
135077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  size_t end = interval->GetLastSibling()->GetEnd();
135101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  if (parent->NeedsTwoSpillSlots()) {
13522aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    if (slot + 2u > spill_slots->size()) {
13533c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
13542aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      spill_slots->resize(slot + 2u, end);
135531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
13562aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    (*spill_slots)[slot] = end;
13572aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    (*spill_slots)[slot + 1] = end;
135831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  } else {
13592aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    if (slot == spill_slots->size()) {
13603c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
13612aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      spill_slots->push_back(end);
13623c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
13632aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      (*spill_slots)[slot] = end;
13643c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    }
136531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
136631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1367776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // Note that the exact spill slot location will be computed when we resolve,
1368776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // that is when we know the number of spill slots for each type.
1369776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  parent->SetSpillSlot(slot);
137086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
137186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
13722a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffraystatic bool IsValidDestination(Location destination) {
1373102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return destination.IsRegister()
13746c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || destination.IsRegisterPair()
1375102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsFpuRegister()
1376840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      || destination.IsFpuRegisterPair()
1377102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsStackSlot()
1378102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsDoubleStackSlot();
13792a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray}
13802a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray
138177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilvoid RegisterAllocator::AllocateSpillSlotForCatchPhi(HPhi* phi) {
138277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  LiveInterval* interval = phi->GetLiveInterval();
138377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
138477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  HInstruction* previous_phi = phi->GetPrevious();
138577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  DCHECK(previous_phi == nullptr ||
138677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil         previous_phi->AsPhi()->GetRegNumber() <= phi->GetRegNumber())
138777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      << "Phis expected to be sorted by vreg number, so that equivalent phis are adjacent.";
138877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
138977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  if (phi->IsVRegEquivalentOf(previous_phi)) {
139077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    // This is an equivalent of the previous phi. We need to assign the same
139177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    // catch phi slot.
139277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    DCHECK(previous_phi->GetLiveInterval()->HasSpillSlot());
139377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    interval->SetSpillSlot(previous_phi->GetLiveInterval()->GetSpillSlot());
139477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  } else {
139577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    // Allocate a new spill slot for this catch phi.
139677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    // TODO: Reuse spill slots when intervals of phis from different catch
139777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    //       blocks do not overlap.
139877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    interval->SetSpillSlot(catch_phi_spill_slots_);
139977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    catch_phi_spill_slots_ += interval->NeedsTwoSpillSlots() ? 2 : 1;
140077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  }
140177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil}
140277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
1403234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddMove(HParallelMove* move,
1404234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location source,
1405234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location destination,
1406234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                HInstruction* instruction,
1407234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Primitive::Type type) const {
1408234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (type == Primitive::kPrimLong
1409234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && codegen_->ShouldSplitLongMoves()
1410234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // The parallel move resolver knows how to deal with long constants.
1411234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && !source.IsConstant()) {
14129021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source.ToLow(), destination.ToLow(), Primitive::kPrimInt, instruction);
14139021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source.ToHigh(), destination.ToHigh(), Primitive::kPrimInt, nullptr);
1414234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  } else {
14159021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source, destination, type, instruction);
1416234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  }
1417234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray}
1418234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1419234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* input,
1420234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                        HInstruction* user,
142186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
142286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
142386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
142486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1425476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  DCHECK(!user->IsPhi());
142686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1427740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  HInstruction* previous = user->GetPrevious();
142886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = nullptr;
142986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (previous == nullptr
1430476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain      || !previous->IsParallelMove()
14318e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray      || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
143286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
14338e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    move->SetLifetimePosition(user->GetLifetimePosition());
1434740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    user->GetBlock()->InsertInstructionBefore(move, user);
143586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
143686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
143786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
14388e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
1439234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, nullptr, input->GetType());
144086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
144186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
144246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionStart(size_t position) {
144346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 0;
144446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
144546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
144646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionEnd(size_t position) {
144746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 1;
144846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
144946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
145086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position,
1451740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                             HInstruction* instruction,
145286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location source,
145386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location destination) const {
14546c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
145586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
145686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
145786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
145886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
145946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  if (at == nullptr) {
146046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    if (IsInstructionStart(position)) {
146146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Block boundary, don't do anything the connection of split siblings will handle it.
146246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      return;
146346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    } else {
146446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Move must happen before the first instruction of the block.
146546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      at = liveness_.GetInstructionFromPosition((position + 1) / 2);
14665976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // Note that parallel moves may have already been inserted, so we explicitly
14675976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // ask for the first instruction of the block: `GetInstructionFromPosition` does
1468234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // not contain the `HParallelMove` instructions.
14695976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      at = at->GetBlock()->GetFirstInstruction();
1470234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1471234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      if (at->GetLifetimePosition() < position) {
1472234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // We may insert moves for split siblings and phi spills at the beginning of the block.
1473234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // Since this is a different lifetime position, we need to go to the next instruction.
1474234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DCHECK(at->IsParallelMove());
1475234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        at = at->GetNext();
1476234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      }
1477234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
14785976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      if (at->GetLifetimePosition() != position) {
14795976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_GT(at->GetLifetimePosition(), position);
148046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move = new (allocator_) HParallelMove(allocator_);
148146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move->SetLifetimePosition(position);
148246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        at->GetBlock()->InsertInstructionBefore(move, at);
14835976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      } else {
14845976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK(at->IsParallelMove());
14855976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        move = at->AsParallelMove();
148646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      }
148746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    }
148846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  } else if (IsInstructionEnd(position)) {
148986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen after the instruction.
149086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(!at->IsControlFlow());
149186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = at->GetNext()->AsParallelMove();
1492e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // This is a parallel move for connecting siblings in a same block. We need to
1493e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // differentiate it with moves for connecting blocks, and input moves.
14948e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    if (move == nullptr || move->GetLifetimePosition() > position) {
149586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
149686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
149786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
149886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
149986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
150086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen before the instruction.
150186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* previous = at->GetPrevious();
1502740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    if (previous == nullptr
1503740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || !previous->IsParallelMove()
1504740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || previous->GetLifetimePosition() != position) {
1505740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // If the previous is a parallel move, then its position must be lower
1506740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // than the given `position`: it was added just after the non-parallel
1507740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // move instruction that precedes `instruction`.
1508740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      DCHECK(previous == nullptr
1509740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || !previous->IsParallelMove()
1510740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || previous->GetLifetimePosition() < position);
151186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
151286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
151386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at);
151486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
151586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = previous->AsParallelMove();
151686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
151786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
151801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), position);
1519234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
152086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
152186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
152286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
1523740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                   HInstruction* instruction,
152486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location source,
152586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location destination) const {
15266c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
152786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
152886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1529d26a411adee1e71b3f09dd604ab9b23018037138David Brazdil  DCHECK_EQ(block->GetNormalSuccessors().size(), 1u);
153086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* last = block->GetLastInstruction();
1531360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // We insert moves at exit for phi predecessors and connecting blocks.
1532fe57faa2e0349418dda38e77ef1c0ac29db75f4dMark Mendell  // A block ending with an if or a packed switch cannot branch to a block
1533fe57faa2e0349418dda38e77ef1c0ac29db75f4dMark Mendell  // with phis because we do not allow critical edges. It can also not connect
1534360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // a split interval between two blocks: the move has to happen in the successor.
1535fe57faa2e0349418dda38e77ef1c0ac29db75f4dMark Mendell  DCHECK(!last->IsIf() && !last->IsPackedSwitch());
153686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* previous = last->GetPrevious();
153786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
1538e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1539e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and output moves.
15405976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray  size_t position = last->GetLifetimePosition();
1541740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  if (previous == nullptr || !previous->IsParallelMove()
15425976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      || previous->AsParallelMove()->GetLifetimePosition() != position) {
154386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
15445976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    move->SetLifetimePosition(position);
154586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, last);
154686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
154786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
154886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1549234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
155086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
155186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
155286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
1553740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                    HInstruction* instruction,
155486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location source,
155586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location destination) const {
15566c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
155786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
155886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
155986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* first = block->GetFirstInstruction();
156086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = first->AsParallelMove();
1561234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  size_t position = block->GetLifetimeStart();
1562e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1563e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and input moves.
1564234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
156586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1566234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->SetLifetimePosition(position);
156786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, first);
156886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1569234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
157086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
157186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
157286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
157386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
157486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
15756c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
157686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
157786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1578476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  if (instruction->IsPhi()) {
1579740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
158086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
158186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
158286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1583e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  size_t position = instruction->GetLifetimePosition() + 1;
158486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = instruction->GetNext()->AsParallelMove();
1585e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for moving the output of an instruction. We need
1586e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // to differentiate with input moves, moves for connecting siblings in a
1587e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // and moves for connecting blocks.
1588e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
158986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1590e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(position);
159186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
159286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1593234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
159486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
159586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
159686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
159786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
159876b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray  if (current->HasSpillSlot()
159976b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      && current->HasRegister()
160076b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      // Currently, we spill unconditionnally the current method in the code generators.
160176b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      && !interval->GetDefinedBy()->IsCurrentMethod()) {
160286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // We spill eagerly, so move must be at definition.
160386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertMoveAfter(interval->GetDefinedBy(),
1604840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                    interval->ToLocation(),
160501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                    interval->NeedsTwoSpillSlots()
1606412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1607412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
160886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
160986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  UsePosition* use = current->GetFirstUse();
16104ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray  UsePosition* env_use = current->GetFirstEnvironmentUse();
161186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
161286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Walk over all siblings, updating locations of use positions, and
161386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // connecting them when they are adjacent.
161486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  do {
161501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
161686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
161786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Walk over all uses covered by this interval, and update the location
161886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // information.
1619d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray
1620d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    LiveRange* range = current->GetFirstRange();
1621d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    while (range != nullptr) {
1622579026039080252878106118645ed70706f4838eNicolas Geoffray      while (use != nullptr && use->GetPosition() < range->GetStart()) {
1623579026039080252878106118645ed70706f4838eNicolas Geoffray        DCHECK(use->IsSynthesized());
1624579026039080252878106118645ed70706f4838eNicolas Geoffray        use = use->GetNext();
1625579026039080252878106118645ed70706f4838eNicolas Geoffray      }
1626d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      while (use != nullptr && use->GetPosition() <= range->GetEnd()) {
16274ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray        DCHECK(!use->GetIsEnvironment());
16283fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil        DCHECK(current->CoversSlow(use->GetPosition()) || (use->GetPosition() == range->GetEnd()));
1629579026039080252878106118645ed70706f4838eNicolas Geoffray        if (!use->IsSynthesized()) {
1630579026039080252878106118645ed70706f4838eNicolas Geoffray          LocationSummary* locations = use->GetUser()->GetLocations();
1631579026039080252878106118645ed70706f4838eNicolas Geoffray          Location expected_location = locations->InAt(use->GetInputIndex());
1632579026039080252878106118645ed70706f4838eNicolas Geoffray          // The expected (actual) location may be invalid in case the input is unused. Currently
1633579026039080252878106118645ed70706f4838eNicolas Geoffray          // this only happens for intrinsics.
1634579026039080252878106118645ed70706f4838eNicolas Geoffray          if (expected_location.IsValid()) {
1635579026039080252878106118645ed70706f4838eNicolas Geoffray            if (expected_location.IsUnallocated()) {
1636579026039080252878106118645ed70706f4838eNicolas Geoffray              locations->SetInAt(use->GetInputIndex(), source);
1637579026039080252878106118645ed70706f4838eNicolas Geoffray            } else if (!expected_location.IsConstant()) {
1638579026039080252878106118645ed70706f4838eNicolas Geoffray              AddInputMoveFor(interval->GetDefinedBy(), use->GetUser(), source, expected_location);
1639579026039080252878106118645ed70706f4838eNicolas Geoffray            }
1640579026039080252878106118645ed70706f4838eNicolas Geoffray          } else {
1641579026039080252878106118645ed70706f4838eNicolas Geoffray            DCHECK(use->GetUser()->IsInvoke());
1642579026039080252878106118645ed70706f4838eNicolas Geoffray            DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
1643d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray          }
164486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
1645d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray        use = use->GetNext();
164686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
16474ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray
16484ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      // Walk over the environment uses, and update their locations.
16494ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      while (env_use != nullptr && env_use->GetPosition() < range->GetStart()) {
16504ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray        env_use = env_use->GetNext();
16514ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      }
16524ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray
16534ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      while (env_use != nullptr && env_use->GetPosition() <= range->GetEnd()) {
16540a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray        DCHECK(current->CoversSlow(env_use->GetPosition())
16550a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray               || (env_use->GetPosition() == range->GetEnd()));
1656d23eeef3492b53102eb8093524cf37e2b4c296dbNicolas Geoffray        HEnvironment* environment = env_use->GetEnvironment();
16570a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray        environment->SetLocationAt(env_use->GetInputIndex(), source);
16584ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray        env_use = env_use->GetNext();
16594ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      }
16604ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray
1661d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      range = range->GetNext();
166286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
166386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
166486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // If the next interval starts just after this one, and has a register,
166586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // insert a move.
166686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* next_sibling = current->GetNextSibling();
166786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_sibling != nullptr
166886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && next_sibling->HasRegister()
166986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && current->GetEnd() == next_sibling->GetStart()) {
167001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      Location destination = next_sibling->ToLocation();
1671740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
167286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
16733946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
167443af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray    for (SafepointPosition* safepoint_position = current->GetFirstSafepoint();
167543af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray         safepoint_position != nullptr;
167643af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray         safepoint_position = safepoint_position->GetNext()) {
16773fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil      DCHECK(current->CoversSlow(safepoint_position->GetPosition()));
16783946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
16795588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      LocationSummary* locations = safepoint_position->GetLocations();
16803bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
16811af564e2d3b560fb9a076eb35ea20471aed0dc92Nicolas Geoffray        DCHECK(interval->GetDefinedBy()->IsActualObject())
16821af564e2d3b560fb9a076eb35ea20471aed0dc92Nicolas Geoffray            << interval->GetDefinedBy()->DebugName()
16831af564e2d3b560fb9a076eb35ea20471aed0dc92Nicolas Geoffray            << "@" << safepoint_position->GetInstruction()->DebugName();
16843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
16853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
16863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
16873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      switch (source.GetKind()) {
16883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kRegister: {
16893bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray          locations->AddLiveRegister(source);
1690988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) {
1691988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray            DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1692988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_core_registers_ +
1693988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_fp_registers_);
1694988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          }
16953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          if (current->GetType() == Primitive::kPrimNot) {
1696a3eca2d7300f35c66cf4b696d788a8b7ba74eb99Nicolas Geoffray            DCHECK(interval->GetDefinedBy()->IsActualObject())
16971af564e2d3b560fb9a076eb35ea20471aed0dc92Nicolas Geoffray                << interval->GetDefinedBy()->DebugName()
16981af564e2d3b560fb9a076eb35ea20471aed0dc92Nicolas Geoffray                << "@" << safepoint_position->GetInstruction()->DebugName();
169956b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray            locations->SetRegisterBit(source.reg());
17003946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          }
17013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
17023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
1703102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        case Location::kFpuRegister: {
1704102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          locations->AddLiveRegister(source);
1705102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          break;
1706102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        }
17076c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
17086c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        case Location::kRegisterPair:
1709840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        case Location::kFpuRegisterPair: {
1710840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToLow());
1711840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToHigh());
1712840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          break;
1713840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
17143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kStackSlot:  // Fall-through
17153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kDoubleStackSlot:  // Fall-through
17163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kConstant: {
17173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          // Nothing to do.
17183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
17193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
17203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        default: {
17213946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          LOG(FATAL) << "Unexpected location for object";
17223946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
17233946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
17243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
172586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = next_sibling;
172686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } while (current != nullptr);
1727d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray
1728579026039080252878106118645ed70706f4838eNicolas Geoffray  if (kIsDebugBuild) {
1729579026039080252878106118645ed70706f4838eNicolas Geoffray    // Following uses can only be synthesized uses.
1730579026039080252878106118645ed70706f4838eNicolas Geoffray    while (use != nullptr) {
1731579026039080252878106118645ed70706f4838eNicolas Geoffray      DCHECK(use->IsSynthesized());
1732579026039080252878106118645ed70706f4838eNicolas Geoffray      use = use->GetNext();
1733579026039080252878106118645ed70706f4838eNicolas Geoffray    }
1734579026039080252878106118645ed70706f4838eNicolas Geoffray  }
173586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
173686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
173704eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffraystatic bool IsMaterializableEntryBlockInstructionOfGraphWithIrreducibleLoop(
173804eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray    HInstruction* instruction) {
173904eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray  return instruction->GetBlock()->GetGraph()->HasIrreducibleLoops() &&
174004eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray         (instruction->IsConstant() || instruction->IsCurrentMethod());
174104eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray}
174204eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray
174386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
174486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* from,
174586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* to) const {
174686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval->GetNextSibling() == nullptr) {
174786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Nothing to connect. The whole range was allocated to the same location.
174886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
174986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
175086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1751241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  // Find the intervals that cover `from` and `to`.
1752241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  LiveInterval* destination = interval->GetSiblingAt(to->GetLifetimeStart());
1753241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  LiveInterval* source = interval->GetSiblingAt(from->GetLifetimeEnd() - 1);
175486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
175586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (destination == source) {
175686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Interval was not split.
175786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
175886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
175904eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray
176004eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray  LiveInterval* parent = interval->GetParent();
176104eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray  HInstruction* defined_by = parent->GetDefinedBy();
176204eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray  if (destination == nullptr) {
176304eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray    // Our live_in fixed point calculation has found that the instruction is live
176404eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray    // in the `to` block because it will eventually enter an irreducible loop. Our
176504eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray    // live interval computation however does not compute a fixed point, and
176604eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray    // therefore will not have a location for that instruction for `to`.
176704eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray    // Because the instruction is a constant or the ArtMethod, we don't need to
176804eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray    // do anything: it will be materialized in the irreducible loop.
176904eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray    DCHECK(IsMaterializableEntryBlockInstructionOfGraphWithIrreducibleLoop(defined_by));
177004eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray    return;
177104eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray  }
17728ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray
177386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!destination->HasRegister()) {
177486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Values are eagerly spilled. Spill slot already contains appropriate value.
177586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
177686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
177786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
177886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
177986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // we need to put the moves at the entry of `to`.
1780d26a411adee1e71b3f09dd604ab9b23018037138David Brazdil  if (from->GetNormalSuccessors().size() == 1) {
1781740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtExitOf(from,
178204eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray                               defined_by,
178301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               source->ToLocation(),
178401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               destination->ToLocation());
178586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
17866058455d486219994921b63a2d774dc9908415a2Vladimir Marko    DCHECK_EQ(to->GetPredecessors().size(), 1u);
1787740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(to,
178804eb70f0282703a150e3e2c7e5b3f678fec25397Nicolas Geoffray                                defined_by,
178901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                source->ToLocation(),
179001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                destination->ToLocation());
179186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
179286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
179386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
179486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() {
1795776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(),
17964c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_core_registers_,
17974c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_fp_registers_,
17984c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     reserved_out_slots_,
17990d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray                                     codegen_->GetGraph()->GetLinearOrder());
180086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
180186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Adjust the Out Location of instructions.
180286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
180386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
180486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
180586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* current = instruction->GetLiveInterval();
180686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LocationSummary* locations = instruction->GetLocations();
180786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location location = locations->Out();
1808476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain    if (instruction->IsParameterValue()) {
180986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Now that we know the frame size, adjust the parameter's location.
181086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.IsStackSlot()) {
181186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
181286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1813f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
181486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (location.IsDoubleStackSlot()) {
181586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
181686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1817f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
181886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (current->HasSpillSlot()) {
181986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
182086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
182176b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray    } else if (instruction->IsCurrentMethod()) {
182276b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      // The current method is always at offset 0.
182376b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      DCHECK(!current->HasSpillSlot() || (current->GetSpillSlot() == 0));
182477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    } else if (instruction->IsPhi() && instruction->AsPhi()->IsCatchPhi()) {
182577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      DCHECK(current->HasSpillSlot());
182677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      size_t slot = current->GetSpillSlot()
182777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                    + GetNumberOfSpillSlots()
182877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                    + reserved_out_slots_
182977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                    - catch_phi_spill_slots_;
183077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      current->SetSpillSlot(slot * kVRegSize);
1831776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    } else if (current->HasSpillSlot()) {
1832776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // Adjust the stack slot, now that we know the number of them for each type.
1833776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // The way this implementation lays out the stack is the following:
183477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [parameter slots       ]
183577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [catch phi spill slots ]
183677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [double spill slots    ]
183777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [long spill slots      ]
183877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [float spill slots     ]
183977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [int/ref values        ]
184077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [maximum out values    ] (number of arguments for calls)
184177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [art method            ].
184277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      size_t slot = current->GetSpillSlot();
1843776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      switch (current->GetType()) {
1844776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimDouble:
18452aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          slot += long_spill_slots_.size();
1846776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1847776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimLong:
18482aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          slot += float_spill_slots_.size();
1849776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1850776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimFloat:
18512aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          slot += int_spill_slots_.size();
1852776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1853776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimNot:
1854776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimInt:
1855776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimChar:
1856776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimByte:
1857776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimBoolean:
1858776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimShort:
1859776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += reserved_out_slots_;
1860776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          break;
1861776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimVoid:
1862776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          LOG(FATAL) << "Unexpected type for interval " << current->GetType();
1863776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      }
1864776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      current->SetSpillSlot(slot * kVRegSize);
186586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
186686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
186701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
186886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
186986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (location.IsUnallocated()) {
187086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.GetPolicy() == Location::kSameAsFirstInput) {
1871d0d4852847432368b090c184d6639e573538dccfCalin Juravle        if (locations->InAt(0).IsUnallocated()) {
1872d0d4852847432368b090c184d6639e573538dccfCalin Juravle          locations->SetInAt(0, source);
1873d0d4852847432368b090c184d6639e573538dccfCalin Juravle        } else {
1874d0d4852847432368b090c184d6639e573538dccfCalin Juravle          DCHECK(locations->InAt(0).Equals(source));
1875d0d4852847432368b090c184d6639e573538dccfCalin Juravle        }
187686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1877829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      locations->UpdateOut(source);
187886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
187986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source.Equals(location));
188086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
188186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
188286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
188386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Connect siblings.
188486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
188586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
188686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ConnectSiblings(instruction->GetLiveInterval());
188786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
188886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
188986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve non-linear control flow across branches. Order does not matter.
18900d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
189186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* block = it.Current();
189215bd22849ee6a1ffb3fb3630f686c2870bdf1bbcNicolas Geoffray    if (block->IsCatchBlock() ||
189315bd22849ee6a1ffb3fb3630f686c2870bdf1bbcNicolas Geoffray        (block->GetLoopInformation() != nullptr && block->GetLoopInformation()->IsIrreducible())) {
189415bd22849ee6a1ffb3fb3630f686c2870bdf1bbcNicolas Geoffray      // Instructions live at the top of catch blocks or irreducible loop header
189515bd22849ee6a1ffb3fb3630f686c2870bdf1bbcNicolas Geoffray      // were forced to spill.
189677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      if (kIsDebugBuild) {
189777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        BitVector* live = liveness_.GetLiveInSet(*block);
189877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        for (uint32_t idx : live->Indexes()) {
189977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          LiveInterval* interval = liveness_.GetInstructionFromSsaIndex(idx)->GetLiveInterval();
190077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          DCHECK(!interval->GetSiblingAt(block->GetLifetimeStart())->HasRegister());
190177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        }
190277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      }
190377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    } else {
190477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      BitVector* live = liveness_.GetLiveInSet(*block);
190577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      for (uint32_t idx : live->Indexes()) {
190677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        LiveInterval* interval = liveness_.GetInstructionFromSsaIndex(idx)->GetLiveInterval();
190777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        for (HBasicBlock* predecessor : block->GetPredecessors()) {
190877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          ConnectSplitSiblings(interval, predecessor, block);
190977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        }
191086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
191186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
191286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
191386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
191486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve phi inputs. Order does not matter.
19150d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
191686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* current = it.Current();
191777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    if (current->IsCatchBlock()) {
191877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // Catch phi values are set at runtime by the exception delivery mechanism.
191977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    } else {
192077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
192177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        HInstruction* phi = inst_it.Current();
192277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        for (size_t i = 0, e = current->GetPredecessors().size(); i < e; ++i) {
1923ec7802a102d49ab5c17495118d4fe0bcc7287bebVladimir Marko          HBasicBlock* predecessor = current->GetPredecessors()[i];
1924d26a411adee1e71b3f09dd604ab9b23018037138David Brazdil          DCHECK_EQ(predecessor->GetNormalSuccessors().size(), 1u);
192577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          HInstruction* input = phi->InputAt(i);
192677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          Location source = input->GetLiveInterval()->GetLocationAt(
192777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil              predecessor->GetLifetimeEnd() - 1);
192877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          Location destination = phi->GetLiveInterval()->ToLocation();
192977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          InsertParallelMoveAtExitOf(predecessor, phi, source, destination);
193077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        }
193186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
193286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
193386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
19343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
19353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Assign temp locations.
19362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* temp : temp_intervals_) {
1937840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (temp->IsHighInterval()) {
1938840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // High intervals can be skipped, they are already handled by the low interval.
1939840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
1940840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
194101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    HInstruction* at = liveness_.GetTempUser(temp);
1942f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray    size_t temp_index = liveness_.GetTempIndex(temp);
194301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    LocationSummary* locations = at->GetLocations();
19445368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    switch (temp->GetType()) {
19455368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimInt:
1946f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray        locations->SetTempAt(temp_index, Location::RegisterLocation(temp->GetRegister()));
19475368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
19485368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
19495368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimDouble:
1950840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1951840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          Location location = Location::FpuRegisterPairLocation(
1952840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray              temp->GetRegister(), temp->GetHighInterval()->GetRegister());
1953f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          locations->SetTempAt(temp_index, location);
1954840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        } else {
1955f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          locations->SetTempAt(temp_index, Location::FpuRegisterLocation(temp->GetRegister()));
1956840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
19575368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
19585368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
19595368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      default:
19605368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        LOG(FATAL) << "Unexpected type for temporary location "
19615368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                   << temp->GetType();
19625368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    }
19633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
196431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}
196531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1966a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}  // namespace art
1967