register_allocator.cc revision d26a411adee1e71b3f09dd604ab9b23018037138
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
75988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray  static constexpr bool kIsBaseline = false;
76988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray  codegen->SetupBlockedRegisters(kIsBaseline);
772aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  physical_core_register_intervals_.resize(codegen->GetNumberOfCoreRegisters(), nullptr);
782aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  physical_fp_register_intervals_.resize(codegen->GetNumberOfFloatingPointRegisters(), nullptr);
793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Always reserve for the current method and the graph's max out registers.
803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // TODO: compute it instead.
81e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  // ArtMethod* takes 2 vregs for 64 bits.
82e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  reserved_out_slots_ = InstructionSetPointerSize(codegen->GetInstructionSet()) / kVRegSize +
83e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      codegen->GetGraph()->GetMaximumNumberOfOutVRegs();
84a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
85a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
86234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraybool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph ATTRIBUTE_UNUSED,
8786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                InstructionSet instruction_set) {
88f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic  return instruction_set == kArm
89f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kArm64
90f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kMips
914dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze      || instruction_set == kMips64
92f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kThumb2
93234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      || instruction_set == kX86
94f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kX86_64;
9586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
9686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
9786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) {
983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (interval == nullptr) return false;
9986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  bool is_core_register = (interval->GetType() != Primitive::kPrimDouble)
10086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      && (interval->GetType() != Primitive::kPrimFloat);
101a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return processing_core_registers == is_core_register;
102a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
103a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegisters() {
10586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  AllocateRegistersInternal();
10686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  Resolve();
10786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
10886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (kIsDebugBuild) {
10986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = true;
11086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
11186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    processing_core_registers_ = false;
11286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ValidateInternal(true);
1135976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // Check that the linear order is still correct with regards to lifetime positions.
1145976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // Since only parallel moves have been inserted during the register allocation,
1155976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    // these checks are mostly for making sure these moves have been added correctly.
1165976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    size_t current_liveness = 0;
1170d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray    for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
1185976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      HBasicBlock* block = it.Current();
1195976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
1205976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        HInstruction* instruction = inst_it.Current();
1215976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_LE(current_liveness, instruction->GetLifetimePosition());
1225976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        current_liveness = instruction->GetLifetimePosition();
1235976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      }
1245976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      for (HInstructionIterator inst_it(block->GetInstructions());
1255976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray           !inst_it.Done();
1265976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray           inst_it.Advance()) {
1275976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        HInstruction* instruction = inst_it.Current();
1285976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_LE(current_liveness, instruction->GetLifetimePosition()) << instruction->DebugName();
1295976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        current_liveness = instruction->GetLifetimePosition();
1305976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      }
1315976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    }
13286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
13386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
13486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
13577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilvoid RegisterAllocator::BlockRegister(Location location, size_t start, size_t end) {
13656b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray  int reg = location.reg();
137102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  DCHECK(location.IsRegister() || location.IsFpuRegister());
138102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  LiveInterval* interval = location.IsRegister()
1392aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      ? physical_core_register_intervals_[reg]
1402aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      : physical_fp_register_intervals_[reg];
141102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  Primitive::Type type = location.IsRegister()
142102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? Primitive::kPrimInt
143840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      : Primitive::kPrimFloat;
14486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval == nullptr) {
14586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    interval = LiveInterval::MakeFixedInterval(allocator_, reg, type);
146102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (location.IsRegister()) {
1472aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      physical_core_register_intervals_[reg] = interval;
148102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
1492aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      physical_fp_register_intervals_[reg] = interval;
150102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
15186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
15286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  DCHECK(interval->GetRegister() == reg);
15386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  interval->AddRange(start, end);
15486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
15586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
15677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilvoid RegisterAllocator::BlockRegisters(size_t start, size_t end, bool caller_save_only) {
15777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) {
15877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    if (!caller_save_only || !codegen_->IsCoreCalleeSaveRegister(i)) {
15977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      BlockRegister(Location::RegisterLocation(i), start, end);
16077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    }
16177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  }
16277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) {
16377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    if (!caller_save_only || !codegen_->IsFloatingPointCalleeSaveRegister(i)) {
16477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      BlockRegister(Location::FpuRegisterLocation(i), start, end);
16577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    }
16677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  }
16777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil}
16877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
16986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegistersInternal() {
1703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Iterate post-order, to ensure the list is sorted, and the last added interval
1713946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // is the one with the lowest start position.
1720d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearPostOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
1733946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    HBasicBlock* block = it.Current();
174277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
175277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe         back_it.Advance()) {
176277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      ProcessInstruction(back_it.Current());
1773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
178277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
179277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      ProcessInstruction(inst_it.Current());
1803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
18177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
18277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    if (block->IsCatchBlock()) {
18377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // By blocking all registers at the top of each catch block, we force
18477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // intervals used after catch to spill.
18577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      size_t position = block->GetLifetimeStart();
18677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      BlockRegisters(position, position + 1);
18777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    }
1883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
189a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  number_of_registers_ = codegen_->GetNumberOfCoreRegisters();
1915233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_,
1925233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko                                                    kArenaAllocRegisterAllocator);
1933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  processing_core_registers_ = true;
1943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  unhandled_ = &unhandled_core_intervals_;
1952aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* fixed : physical_core_register_intervals_) {
196102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (fixed != nullptr) {
197296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is added to inactive_ instead of unhandled_.
198296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // It's also the only type of inactive interval whose start position
199296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // can be after the current interval during linear scan.
200296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is never split and never moves to unhandled_.
2012aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      inactive_.push_back(fixed);
202102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
203102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  }
2043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LinearScan();
205a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
2062aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  inactive_.clear();
2072aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  active_.clear();
2082aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  handled_.clear();
209a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
2103946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters();
2115233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko  registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_,
2125233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko                                                    kArenaAllocRegisterAllocator);
2133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  processing_core_registers_ = false;
2143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  unhandled_ = &unhandled_fp_intervals_;
2152aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* fixed : physical_fp_register_intervals_) {
216102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (fixed != nullptr) {
217296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is added to inactive_ instead of unhandled_.
218296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // It's also the only type of inactive interval whose start position
219296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // can be after the current interval during linear scan.
220296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Fixed interval is never split and never moves to unhandled_.
2212aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      inactive_.push_back(fixed);
222102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    }
223102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  }
2243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LinearScan();
2253946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray}
22686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
2273946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::ProcessInstruction(HInstruction* instruction) {
2283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LocationSummary* locations = instruction->GetLocations();
2293946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  size_t position = instruction->GetLifetimePosition();
2303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (locations == nullptr) return;
2323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Create synthesized intervals for temporaries.
2343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < locations->GetTempCount(); ++i) {
2353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    Location temp = locations->GetTemp(i);
23652839d17c06175e19ca4a093fb878450d1c4310dNicolas Geoffray    if (temp.IsRegister() || temp.IsFpuRegister()) {
237102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(temp, position, position + 1);
23845b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray      // Ensure that an explicit temporary register is marked as being allocated.
23945b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray      codegen_->AddAllocatedRegister(temp);
2403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
241102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      DCHECK(temp.IsUnallocated());
2425368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      switch (temp.GetPolicy()) {
2435368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        case Location::kRequiresRegister: {
2445368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LiveInterval* interval =
2455368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain              LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt);
2462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          temp_intervals_.push_back(interval);
247f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          interval->AddTempUse(instruction, i);
2482aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          unhandled_core_intervals_.push_back(interval);
2495368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          break;
2505368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        }
2515368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
2525368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        case Location::kRequiresFpuRegister: {
2535368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LiveInterval* interval =
2545368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain              LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimDouble);
2552aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          temp_intervals_.push_back(interval);
256f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          interval->AddTempUse(instruction, i);
257840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
2585588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray            interval->AddHighInterval(/* is_temp */ true);
259840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray            LiveInterval* high = interval->GetHighInterval();
2602aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko            temp_intervals_.push_back(high);
2612aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko            unhandled_fp_intervals_.push_back(high);
262840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          }
2632aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          unhandled_fp_intervals_.push_back(interval);
2645368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          break;
2655368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        }
2665368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
2675368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        default:
2685368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain          LOG(FATAL) << "Unexpected policy for temporary location "
2695368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                     << temp.GetPolicy();
2705368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      }
271a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
272a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
27386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
2743bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  bool core_register = (instruction->GetType() != Primitive::kPrimDouble)
2753bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      && (instruction->GetType() != Primitive::kPrimFloat);
2763bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
2778158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames  if (locations->NeedsSafepoint()) {
278c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray    if (codegen_->IsLeafMethod()) {
279c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      // TODO: We do this here because we do not want the suspend check to artificially
280c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      // create live registers. We should find another place, but this is currently the
281c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      // simplest.
282c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      DCHECK(instruction->IsSuspendCheckEntry());
283c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      instruction->GetBlock()->RemoveInstruction(instruction);
284c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      return;
2853bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
2862aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    safepoints_.push_back(instruction);
2873bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    if (locations->OnlyCallsOnSlowPath()) {
2883bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // We add a synthesized range at this position to record the live registers
2893bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // at this position. Ideally, we could just update the safepoints when locations
2903bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // are updated, but we currently need to know the full stack size before updating
2913bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // locations (because of parameters and the fact that we don't have a frame pointer).
2923bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // And knowing the full stack size requires to know the maximum number of live
2933bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // registers at calls in slow paths.
2943bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // By adding the following interval in the algorithm, we can compute this
2953bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      // maximum before updating locations.
2963bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction);
297acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      interval->AddRange(position, position + 1);
29887d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray      AddSorted(&unhandled_core_intervals_, interval);
29987d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray      AddSorted(&unhandled_fp_intervals_, interval);
3003bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
3013bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  }
3023bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
3033bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  if (locations->WillCall()) {
30477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    BlockRegisters(position, position + 1, /* caller_save_only */ true);
3053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  for (size_t i = 0; i < instruction->InputCount(); ++i) {
3083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    Location input = locations->InAt(i);
309102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (input.IsRegister() || input.IsFpuRegister()) {
310102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      BlockRegister(input, position, position + 1);
311840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (input.IsPair()) {
312840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      BlockRegister(input.ToLow(), position, position + 1);
313840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      BlockRegister(input.ToHigh(), position, position + 1);
3143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
3153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  LiveInterval* current = instruction->GetLiveInterval();
3183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current == nullptr) return;
3193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3202aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  ArenaVector<LiveInterval*>& unhandled = core_register
321102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? unhandled_core_intervals_
322102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : unhandled_fp_intervals_;
323102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray
3242aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  DCHECK(unhandled.empty() || current->StartsBeforeOrAt(unhandled.back()));
32587d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray
326840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (codegen_->NeedsTwoRegisters(current->GetType())) {
327840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->AddHighInterval();
328840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
329840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
3302aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (size_t safepoint_index = safepoints_.size(); safepoint_index > 0; --safepoint_index) {
3312aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    HInstruction* safepoint = safepoints_[safepoint_index - 1u];
3325588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    size_t safepoint_position = safepoint->GetLifetimePosition();
3335588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray
3345588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    // Test that safepoints are ordered in the optimal way.
3352aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK(safepoint_index == safepoints_.size() ||
3362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko           safepoints_[safepoint_index]->GetLifetimePosition() < safepoint_position);
3375588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray
3385588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    if (safepoint_position == current->GetStart()) {
3395588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      // The safepoint is for this instruction, so the location of the instruction
3405588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      // does not need to be saved.
3412aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      DCHECK_EQ(safepoint_index, safepoints_.size());
3425588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      DCHECK_EQ(safepoint, instruction);
3435588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      continue;
3445588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    } else if (current->IsDeadAt(safepoint_position)) {
3455588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      break;
3465588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    } else if (!current->Covers(safepoint_position)) {
3475588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      // Hole in the interval.
3485588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      continue;
3495588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    }
3505588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray    current->AddSafepoint(safepoint);
3515588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray  }
3523fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  current->ResetSearchCache();
3535588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray
3543946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Some instructions define their output in fixed register/stack slot. We need
3553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // to ensure we know these locations before doing register allocation. For a
3563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // given register, we create an interval that covers these locations. The register
3573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // will be unavailable at these locations when trying to allocate one for an
3583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // interval.
3593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  //
3603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // The backwards walking ensures the ranges are ordered on increasing start positions.
3613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  Location output = locations->Out();
362d0d4852847432368b090c184d6639e573538dccfCalin Juravle  if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) {
363d0d4852847432368b090c184d6639e573538dccfCalin Juravle    Location first = locations->InAt(0);
364d0d4852847432368b090c184d6639e573538dccfCalin Juravle    if (first.IsRegister() || first.IsFpuRegister()) {
365d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetFrom(position + 1);
366d0d4852847432368b090c184d6639e573538dccfCalin Juravle      current->SetRegister(first.reg());
367840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (first.IsPair()) {
368840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      current->SetFrom(position + 1);
369840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      current->SetRegister(first.low());
370840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = current->GetHighInterval();
371840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetRegister(first.high());
372840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetFrom(position + 1);
373d0d4852847432368b090c184d6639e573538dccfCalin Juravle    }
374d0d4852847432368b090c184d6639e573538dccfCalin Juravle  } else if (output.IsRegister() || output.IsFpuRegister()) {
3753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Shift the interval's start by one to account for the blocked register.
3763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetFrom(position + 1);
37756b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray    current->SetRegister(output.reg());
378102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    BlockRegister(output, position, position + 1);
379840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (output.IsPair()) {
380840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->SetFrom(position + 1);
381840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    current->SetRegister(output.low());
382840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    LiveInterval* high = current->GetHighInterval();
383840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    high->SetRegister(output.high());
384840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    high->SetFrom(position + 1);
385840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    BlockRegister(output.ToLow(), position, position + 1);
386840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    BlockRegister(output.ToHigh(), position, position + 1);
3873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) {
3883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    current->SetSpillSlot(output.GetStackIndex());
389840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
390840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(output.IsUnallocated() || output.IsConstant());
3913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
3923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
39377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  if (instruction->IsPhi() && instruction->AsPhi()->IsCatchPhi()) {
39477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    AllocateSpillSlotForCatchPhi(instruction->AsPhi());
39577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  }
39677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
3973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // If needed, add interval to the list of unhandled intervals.
3983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasSpillSlot() || instruction->IsConstant()) {
399c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray    // Split just before first register use.
4003946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    size_t first_register_use = current->FirstRegisterUse();
4013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (first_register_use != kNoLifetime) {
4028cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray      LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1);
403b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray      // Don't add directly to `unhandled`, it needs to be sorted and the start
4043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // of this new interval might be after intervals already in the list.
4053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      AddSorted(&unhandled, split);
4063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    } else {
4073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      // Nothing to do, we won't allocate a register for this value.
4083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
4093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
410b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // Don't add directly to `unhandled`, temp or safepoint intervals
411b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // for this instruction may have been added, and those can be
412b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    // processed first.
413b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    AddSorted(&unhandled, current);
4143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
415a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
416a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
41731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayclass AllRangesIterator : public ValueObject {
41831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray public:
41931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  explicit AllRangesIterator(LiveInterval* interval)
42031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      : current_interval_(interval),
42131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_(interval->GetFirstRange()) {}
42231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
42331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  bool Done() const { return current_interval_ == nullptr; }
42431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* CurrentRange() const { return current_range_; }
42531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* CurrentInterval() const { return current_interval_; }
42631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
42731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  void Advance() {
42831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    current_range_ = current_range_->GetNext();
42931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    if (current_range_ == nullptr) {
43031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      current_interval_ = current_interval_->GetNextSibling();
43131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current_interval_ != nullptr) {
43231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        current_range_ = current_interval_->GetFirstRange();
43331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
43431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
43531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
43631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
43731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray private:
43831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* current_interval_;
43931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveRange* current_range_;
44031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
44131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(AllRangesIterator);
44231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray};
44331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
44486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const {
44586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // To simplify unit testing, we eagerly create the array of intervals, and
44686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // call the helper method.
4472aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  ArenaVector<LiveInterval*> intervals(allocator_->Adapter(kArenaAllocRegisterAllocator));
44886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) {
44986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
45086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) {
4512aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      intervals.push_back(instruction->GetLiveInterval());
45286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
45386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
45486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
4552aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  const ArenaVector<LiveInterval*>* physical_register_intervals = processing_core_registers_
4562aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      ? &physical_core_register_intervals_
4572aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      : &physical_fp_register_intervals_;
4582aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* fixed : *physical_register_intervals) {
4592aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    if (fixed != nullptr) {
4602aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      intervals.push_back(fixed);
46186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
46286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
46386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
4642aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* temp : temp_intervals_) {
4653946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    if (ShouldProcess(processing_core_registers_, temp)) {
4662aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      intervals.push_back(temp);
4673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
4683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
4693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
470776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  return ValidateIntervals(intervals, GetNumberOfSpillSlots(), reserved_out_slots_, *codegen_,
4713946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                           allocator_, processing_core_registers_, log_fatal_on_failure);
47286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
47386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
4742aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Markobool RegisterAllocator::ValidateIntervals(const ArenaVector<LiveInterval*>& intervals,
47531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray                                          size_t number_of_spill_slots,
4763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                                          size_t number_of_out_slots,
477a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          const CodeGenerator& codegen,
478a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          ArenaAllocator* allocator,
479a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool processing_core_registers,
480a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                                          bool log_fatal_on_failure) {
481a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t number_of_registers = processing_core_registers
482a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      ? codegen.GetNumberOfCoreRegisters()
483a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      : codegen.GetNumberOfFloatingPointRegisters();
4842aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  ArenaVector<ArenaBitVector*> liveness_of_values(
4852aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      allocator->Adapter(kArenaAllocRegisterAllocator));
4862aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  liveness_of_values.reserve(number_of_registers + number_of_spill_slots);
487a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
488a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // Allocate a bit vector per register. A live interval that has a register
489a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // allocated will populate the associated bit vector based on its live ranges.
49031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) {
4912aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    liveness_of_values.push_back(new (allocator) ArenaBitVector(allocator, 0, true));
492a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
493a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
4942aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* start_interval : intervals) {
4952aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    for (AllRangesIterator it(start_interval); !it.Done(); it.Advance()) {
49631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      LiveInterval* current = it.CurrentInterval();
49786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* defined_by = current->GetParent()->GetDefinedBy();
49886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (current->GetParent()->HasSpillSlot()
49976b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray           // Parameters and current method have their own stack slot.
50076b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray           && !(defined_by != nullptr && (defined_by->IsParameterValue()
50176b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray                                          || defined_by->IsCurrentMethod()))) {
5022aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        BitVector* liveness_of_spill_slot = liveness_of_values[number_of_registers
5033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            + current->GetParent()->GetSpillSlot() / kVRegSize
5042aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko            - number_of_out_slots];
50531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
50631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_spill_slot->IsBitSet(j)) {
50731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            if (log_fatal_on_failure) {
50831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              std::ostringstream message;
50931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              message << "Spill slot conflict at " << j;
51031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              LOG(FATAL) << message.str();
51131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            } else {
51231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray              return false;
51331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            }
51431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          } else {
51531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_spill_slot->SetBit(j);
51631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          }
51731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        }
518a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
51931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
52031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      if (current->HasRegister()) {
52145b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray        if (kIsDebugBuild && log_fatal_on_failure && !current->IsFixed()) {
52245b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray          // Only check when an error is fatal. Only tests code ask for non-fatal failures
52345b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray          // and test code may not properly fill the right information to the code generator.
52445b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray          CHECK(codegen.HasAllocatedRegister(processing_core_registers, current->GetRegister()));
52545b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray        }
5262aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        BitVector* liveness_of_register = liveness_of_values[current->GetRegister()];
52731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray        for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) {
52831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray          if (liveness_of_register->IsBitSet(j)) {
529829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray            if (current->IsUsingInputRegister() && current->CanUseInputRegister()) {
530829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray              continue;
531829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray            }
532a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            if (log_fatal_on_failure) {
533a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              std::ostringstream message;
5343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "Register conflict at " << j << " ";
5353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              if (defined_by != nullptr) {
5363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                message << "(" << defined_by->DebugName() << ")";
5373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              }
5383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray              message << "for ";
539a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              if (processing_core_registers) {
540a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpCoreRegister(message, current->GetRegister());
541a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              } else {
542a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray                codegen.DumpFloatingPointRegister(message, current->GetRegister());
543a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              }
544a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              LOG(FATAL) << message.str();
545a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            } else {
546a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray              return false;
547a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray            }
548a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          } else {
54931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray            liveness_of_register->SetBit(j);
550a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray          }
551a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        }
55231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray      }
55331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
554a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
555a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
556a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
557a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
55886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const {
559a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  interval->Dump(stream);
560a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << ": ";
561a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (interval->HasRegister()) {
562102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    if (interval->IsFloatingPoint()) {
56386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      codegen_->DumpFloatingPointRegister(stream, interval->GetRegister());
564102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray    } else {
565102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      codegen_->DumpCoreRegister(stream, interval->GetRegister());
566a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
567a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
568a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    stream << "spilled";
569a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
570a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  stream << std::endl;
571a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
572a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
573296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yangvoid RegisterAllocator::DumpAllIntervals(std::ostream& stream) const {
574296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "inactive: " << std::endl;
5752aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* inactive_interval : inactive_) {
5762aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DumpInterval(stream, inactive_interval);
577296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
578296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "active: " << std::endl;
5792aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* active_interval : active_) {
5802aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DumpInterval(stream, active_interval);
581296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
582296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "unhandled: " << std::endl;
583296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  auto unhandled = (unhandled_ != nullptr) ?
584296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      unhandled_ : &unhandled_core_intervals_;
5852aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* unhandled_interval : *unhandled) {
5862aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DumpInterval(stream, unhandled_interval);
587296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
588296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  stream << "handled: " << std::endl;
5892aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* handled_interval : handled_) {
5902aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DumpInterval(stream, handled_interval);
591296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
592296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang}
593296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
594a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// By the book implementation of a linear scan register allocator.
595a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::LinearScan() {
5962aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  while (!unhandled_->empty()) {
597a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (1) Remove interval with the lowest start position from unhandled.
5982aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    LiveInterval* current = unhandled_->back();
5992aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    unhandled_->pop_back();
6002e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray
6012e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // Make sure the interval is an expected state.
6023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    DCHECK(!current->IsFixed() && !current->HasSpillSlot());
6032e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // Make sure we are going in the right order.
6042aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK(unhandled_->empty() || unhandled_->back()->GetStart() >= current->GetStart());
6052e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // Make sure a low interval is always with a high.
6062aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK(!current->IsLowInterval() || unhandled_->back()->IsHighInterval());
6072e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // Make sure a high interval is always with a low.
6082e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    DCHECK(current->IsLowInterval() ||
6092aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko           unhandled_->empty() ||
6102aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko           !unhandled_->back()->IsHighInterval());
61187d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray
612a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t position = current->GetStart();
613a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
614296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Remember the inactive_ size here since the ones moved to inactive_ from
615296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // active_ below shouldn't need to be re-checked.
6162aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    size_t inactive_intervals_to_handle = inactive_.size();
617296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
618a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (2) Remove currently active intervals that are dead at this position.
619a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move active intervals that have a lifetime hole at this position
620a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     to inactive.
621b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko    auto active_kept_end = std::remove_if(
622b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        active_.begin(),
623b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        active_.end(),
624b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        [this, position](LiveInterval* interval) {
625b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          if (interval->IsDeadAt(position)) {
626b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            handled_.push_back(interval);
627b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return true;
628b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          } else if (!interval->Covers(position)) {
629b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            inactive_.push_back(interval);
630b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return true;
631b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          } else {
632b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return false;  // Keep this interval.
633b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          }
634b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        });
6352aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    active_.erase(active_kept_end, active_.end());
636a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
637a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (3) Remove currently inactive intervals that are dead at this position.
638a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     Move inactive intervals that cover this position to active.
6392aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    auto inactive_to_handle_end = inactive_.begin() + inactive_intervals_to_handle;
640b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko    auto inactive_kept_end = std::remove_if(
641b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        inactive_.begin(),
642b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        inactive_to_handle_end,
643b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        [this, position](LiveInterval* interval) {
644b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          DCHECK(interval->GetStart() < position || interval->IsFixed());
645b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          if (interval->IsDeadAt(position)) {
646b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            handled_.push_back(interval);
647b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return true;
648b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          } else if (interval->Covers(position)) {
649b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            active_.push_back(interval);
650b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return true;
651b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          } else {
652b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko            return false;  // Keep this interval.
653b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko          }
654b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko        });
6552aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    inactive_.erase(inactive_kept_end, inactive_to_handle_end);
656a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
657acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    if (current->IsSlowPathSafepoint()) {
658acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Synthesized interval to record the maximum number of live registers
659acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // at safepoints. No need to allocate a register for it.
660f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      if (processing_core_registers_) {
661f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_core_registers_ =
6622aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          std::max(maximum_number_of_live_core_registers_, active_.size());
663f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      } else {
664f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell        maximum_number_of_live_fp_registers_ =
6652aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          std::max(maximum_number_of_live_fp_registers_, active_.size());
666f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell      }
6672aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      DCHECK(unhandled_->empty() || unhandled_->back()->GetStart() > current->GetStart());
668acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      continue;
669acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    }
670acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray
671840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) {
672840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(!current->HasRegister());
673840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // Allocating the low part was unsucessful. The splitted interval for the high part
674840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // will be handled next (it is in the `unhandled_` list).
675840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
676840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
677840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
678a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (4) Try to find an available register.
679a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    bool success = TryAllocateFreeReg(current);
680a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
681a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (5) If no register could be found, we need to spill.
682a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (!success) {
683a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      success = AllocateBlockedReg(current);
684a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
685a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
686a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // (6) If the interval had a register allocated, add it to the list of active
687a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    //     intervals.
688a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (success) {
689988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      codegen_->AddAllocatedRegister(processing_core_registers_
690988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          ? Location::RegisterLocation(current->GetRegister())
691988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          : Location::FpuRegisterLocation(current->GetRegister()));
6922aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      active_.push_back(current);
693840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) {
694840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister()));
695840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
696a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
697a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
698a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
699a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
700829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffraystatic void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) {
701829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  DCHECK(!interval->IsHighInterval());
702829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // Note that the same instruction may occur multiple times in the input list,
703829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // so `free_until` may have changed already.
7043fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  // Since `position` is not the current scan position, we need to use CoversSlow.
705829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  if (interval->IsDeadAt(position)) {
706829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // Set the register to be free. Note that inactive intervals might later
707829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // update this.
708829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    free_until[interval->GetRegister()] = kMaxLifetimePosition;
709829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (interval->HasHighInterval()) {
710829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      DCHECK(interval->GetHighInterval()->IsDeadAt(position));
711829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition;
712829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
7133fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil  } else if (!interval->CoversSlow(position)) {
714829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // The interval becomes inactive at `defined_by`. We make its register
715829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    // available only until the next use strictly after `defined_by`.
716829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    free_until[interval->GetRegister()] = interval->FirstUseAfter(position);
717829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (interval->HasHighInterval()) {
7183fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil      DCHECK(!interval->GetHighInterval()->CoversSlow(position));
719829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()];
720829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
721829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  }
722829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray}
723829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray
724a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find a free register. If multiple are found, pick the register that
725a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// is free the longest.
726a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) {
727a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* free_until = registers_array_;
728a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
729a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers to be free.
730a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
731a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    free_until[i] = kMaxLifetimePosition;
732a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
733a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
734296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  // For each active interval, set its register to not free.
7352aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* interval : active_) {
736296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(interval->HasRegister());
737296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    free_until[interval->GetRegister()] = 0;
738296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang  }
739296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
740829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // An interval that starts an instruction (that is, it is not split), may
741829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // re-use the registers used by the inputs of that instruciton, based on the
742829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  // location summary.
743829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  HInstruction* defined_by = current->GetDefinedBy();
744829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  if (defined_by != nullptr && !current->IsSplit()) {
745829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    LocationSummary* locations = defined_by->GetLocations();
746829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) {
74794015b939060f5041d408d48717f22443e55b6adNicolas Geoffray      for (size_t i = 0, e = defined_by->InputCount(); i < e; ++i) {
748829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // Take the last interval of the input. It is the location of that interval
749829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // that will be used at `defined_by`.
75094015b939060f5041d408d48717f22443e55b6adNicolas Geoffray        LiveInterval* interval = defined_by->InputAt(i)->GetLiveInterval()->GetLastSibling();
751829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // Note that interval may have not been processed yet.
752829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        // TODO: Handle non-split intervals last in the work list.
75394015b939060f5041d408d48717f22443e55b6adNicolas Geoffray        if (locations->InAt(i).IsValid()
75494015b939060f5041d408d48717f22443e55b6adNicolas Geoffray            && interval->HasRegister()
75594015b939060f5041d408d48717f22443e55b6adNicolas Geoffray            && interval->SameRegisterKind(*current)) {
756829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // The input must be live until the end of `defined_by`, to comply to
757829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // the linear scan algorithm. So we use `defined_by`'s end lifetime
758829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // position to check whether the input is dead or is inactive after
759829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          // `defined_by`.
7603fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil          DCHECK(interval->CoversSlow(defined_by->GetLifetimePosition()));
761829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          size_t position = defined_by->GetLifetimePosition() + 1;
762829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray          FreeIfNotCoverAt(interval, position, free_until);
763829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray        }
764829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      }
765829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray    }
766829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray  }
767829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray
768a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, set its register to be free until
769a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // the next intersection with `current`.
7702aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* inactive : inactive_) {
771296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
772296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
773296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
774296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
775296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
776296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
777296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
778296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
779296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
780296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
781296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang
782a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
783296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (free_until[inactive->GetRegister()] == 0) {
784296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Already used by some active interval. No need to intersect.
785296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
786296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
787a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
788a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    if (next_intersection != kNoLifetime) {
789aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray      free_until[inactive->GetRegister()] =
790aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray          std::min(free_until[inactive->GetRegister()], next_intersection);
791a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
792a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
793a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7946c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
7953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (current->HasRegister()) {
7963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    // Some instructions have a fixed register output.
7973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    reg = current->GetRegister();
798840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (free_until[reg] == 0) {
799840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      DCHECK(current->IsHighInterval());
800840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // AllocateBlockedReg will spill the holder of the register.
801840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      return false;
802840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
8033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  } else {
804840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
805fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray    int hint = current->FindFirstRegisterHint(free_until, liveness_);
806f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray    if ((hint != kNoRegister)
807f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray        // For simplicity, if the hint we are getting for a pair cannot be used,
808f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray        // we are just going to allocate a new pair.
809f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray        && !(current->IsLowInterval() && IsBlocked(GetHighForLowRegister(hint)))) {
81001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      DCHECK(!IsBlocked(hint));
81101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      reg = hint;
812840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (current->IsLowInterval()) {
8136c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      reg = FindAvailableRegisterPair(free_until, current->GetStart());
81401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    } else {
8158826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      reg = FindAvailableRegister(free_until, current);
816a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
817a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
818a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
8196c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
820a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // If we could not find a register, we need to spill.
821840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (free_until[reg] == 0) {
822840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return false;
823840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
824840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
825234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (current->IsLowInterval()) {
826234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    // If the high register of this interval is not available, we need to spill.
827234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    int high_reg = current->GetHighInterval()->GetRegister();
828234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (high_reg == kNoRegister) {
829234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      high_reg = GetHighForLowRegister(reg);
830234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    }
831234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (free_until[high_reg] == 0) {
832234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      return false;
833234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    }
834a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
835a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
836a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  current->SetRegister(reg);
837a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (!current->IsDeadAt(free_until[reg])) {
838a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // If the register is only available for a subset of live ranges
8398272688499c2232355db34d94057983fd436173dNicolas Geoffray    // covered by `current`, split `current` before the position where
840a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // the register is not available anymore.
8418272688499c2232355db34d94057983fd436173dNicolas Geoffray    LiveInterval* split = SplitBetween(current, current->GetStart(), free_until[reg]);
842a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(split != nullptr);
8433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    AddSorted(unhandled_, split);
844a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
845a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  return true;
846a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
847a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
848a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::IsBlocked(int reg) const {
849102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return processing_core_registers_
850102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      ? blocked_core_registers_[reg]
851102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      : blocked_fp_registers_[reg];
852a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
853a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
8546c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffrayint RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const {
8556c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
856840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Pick the register pair that is used the last.
857840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
858840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(i)) continue;
859840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (!IsLowRegister(i)) continue;
860840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int high_register = GetHighForLowRegister(i);
861840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (IsBlocked(high_register)) continue;
862840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    int existing_high_register = GetHighForLowRegister(reg);
8636c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if ((reg == kNoRegister) || (next_use[i] >= next_use[reg]
864840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                        && next_use[high_register] >= next_use[existing_high_register])) {
865840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
866840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      if (next_use[i] == kMaxLifetimePosition
867840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          && next_use[high_register] == kMaxLifetimePosition) {
868840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        break;
869840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      }
8706c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) {
8716c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If one of the current register is known to be unavailable, just unconditionally
8726c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // try a new one.
8736c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      reg = i;
874840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
875840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
876840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
877840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
878840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
8798826f67ad53099021f6442364348fa66729288d7Nicolas Geoffraybool RegisterAllocator::IsCallerSaveRegister(int reg) const {
8808826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  return processing_core_registers_
8818826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      ? !codegen_->IsCoreCalleeSaveRegister(reg)
8828826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      : !codegen_->IsFloatingPointCalleeSaveRegister(reg);
8838826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray}
8848826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
8858826f67ad53099021f6442364348fa66729288d7Nicolas Geoffrayint RegisterAllocator::FindAvailableRegister(size_t* next_use, LiveInterval* current) const {
8868826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  // We special case intervals that do not span a safepoint to try to find a caller-save
8878826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  // register if one is available. We iterate from 0 to the number of registers,
8888826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  // so if there are caller-save registers available at the end, we continue the iteration.
8898826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray  bool prefers_caller_save = !current->HasWillCallSafepoint();
8906c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
891840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
8928826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (IsBlocked(i)) {
8938826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      // Register cannot be used. Continue.
8948826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      continue;
8958826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    }
8968826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
8978826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    // Best case: we found a register fully available.
8988826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (next_use[i] == kMaxLifetimePosition) {
8998826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      if (prefers_caller_save && !IsCallerSaveRegister(i)) {
9008826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // We can get shorter encodings on some platforms by using
9018826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // small register numbers. So only update the candidate if the previous
9028826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // one was not available for the whole method.
9038826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        if (reg == kNoRegister || next_use[reg] != kMaxLifetimePosition) {
9048826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray          reg = i;
9058826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        }
9068826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // Continue the iteration in the hope of finding a caller save register.
9078826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        continue;
9088826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      } else {
9098826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        reg = i;
9108826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        // We know the register is good enough. Return it.
9118826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray        break;
9128826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      }
9138826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    }
9148826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
9158826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    // If we had no register before, take this one as a reference.
9168826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (reg == kNoRegister) {
917840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      reg = i;
9188826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      continue;
9198826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    }
9208826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray
9218826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    // Pick the register that is used the last.
9228826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    if (next_use[i] > next_use[reg]) {
9238826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      reg = i;
9248826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray      continue;
925840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
926840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
927840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  return reg;
928840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray}
929840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
9302aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko// Remove interval and its other half if any. Return iterator to the following element.
9312aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Markostatic ArenaVector<LiveInterval*>::iterator RemoveIntervalAndPotentialOtherHalf(
9322aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    ArenaVector<LiveInterval*>* intervals, ArenaVector<LiveInterval*>::iterator pos) {
9332aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  DCHECK(intervals->begin() <= pos && pos < intervals->end());
9342aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  LiveInterval* interval = *pos;
9352aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  if (interval->IsLowInterval()) {
9362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK(pos + 1 < intervals->end());
9372aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK_EQ(*(pos + 1), interval->GetHighInterval());
9382aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    return intervals->erase(pos, pos + 2);
9392aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  } else if (interval->IsHighInterval()) {
9402aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK(intervals->begin() < pos);
9412aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    DCHECK_EQ(*(pos - 1), interval->GetLowInterval());
9422aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    return intervals->erase(pos - 1, pos + 1);
9432aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  } else {
9442aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    return intervals->erase(pos);
9452aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  }
9462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko}
9472aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko
948234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraybool RegisterAllocator::TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
949234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                                 size_t first_register_use,
950234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                                                 size_t* next_use) {
9512aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (auto it = active_.begin(), end = active_.end(); it != end; ++it) {
9522aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    LiveInterval* active = *it;
9536c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    DCHECK(active->HasRegister());
954234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (active->IsFixed()) continue;
955234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (active->IsHighInterval()) continue;
956234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    if (first_register_use > next_use[active->GetRegister()]) continue;
957234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
9582e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // Split the first interval found that is either:
9592e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // 1) A non-pair interval.
9602e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // 2) A pair interval whose high is not low + 1.
9612e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    // 3) A pair interval whose low is not even.
9622e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray    if (!active->IsLowInterval() ||
9632e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray        IsLowOfUnalignedPairInterval(active) ||
9642e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray        !IsLowRegister(active->GetRegister())) {
9656c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      LiveInterval* split = Split(active, position);
9666c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      if (split != active) {
9672aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        handled_.push_back(active);
9686c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      }
9692aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      RemoveIntervalAndPotentialOtherHalf(&active_, it);
9706c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
9716c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      return true;
9726c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
9736c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  }
9746c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  return false;
9756c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray}
9766c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
977a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval
978a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register
979a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead.
980a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) {
981a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t first_register_use = current->FirstRegisterUse();
982da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray  if (current->HasRegister()) {
983da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    DCHECK(current->IsHighInterval());
984da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // The low interval has allocated the register for the high interval. In
985da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // case the low interval had to split both intervals, we may end up in a
986da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // situation where the high interval does not have a register use anymore.
987da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // We must still proceed in order to split currently active and inactive
988da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // uses of the high interval's register, and put the high interval in the
989da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // active set.
990da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    DCHECK(first_register_use != kNoLifetime || (current->GetNextSibling() != nullptr));
991da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray  } else if (first_register_use == kNoLifetime) {
99231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    AllocateSpillSlotFor(current);
993a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
994a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
995a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
9961ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray  // We use the first use to compare with other intervals. If this interval
9971ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray  // is used after any active intervals, we will spill this interval.
9981ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray  size_t first_use = current->FirstUseAfter(current->GetStart());
9991ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray
1000a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // First set all registers as not being used.
1001a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  size_t* next_use = registers_array_;
1002a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  for (size_t i = 0; i < number_of_registers_; ++i) {
1003a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    next_use[i] = kMaxLifetimePosition;
1004a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1005a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1006a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each active interval, find the next use of its register after the
1007a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
10082aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* active : active_) {
1009a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(active->HasRegister());
101086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (active->IsFixed()) {
101186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      next_use[active->GetRegister()] = current->GetStart();
101286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
10131ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray      size_t use = active->FirstUseAfter(current->GetStart());
101486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (use != kNoLifetime) {
101586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[active->GetRegister()] = use;
101686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1017a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1018a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1019a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1020a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // For each inactive interval, find the next use of its register after the
1021a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  // start of current.
10222aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* inactive : inactive_) {
1023296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    // Temp/Slow-path-safepoint interval has no holes.
1024296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint());
1025296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    if (!current->IsSplit() && !inactive->IsFixed()) {
1026296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Neither current nor inactive are fixed.
1027296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Thanks to SSA, a non-split interval starting in a hole of an
1028296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // inactive interval should never intersect with that inactive interval.
1029296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      // Only if it's not fixed though, because fixed intervals don't come from SSA.
1030296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
1031296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang      continue;
1032296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang    }
1033a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    DCHECK(inactive->HasRegister());
103486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    size_t next_intersection = inactive->FirstIntersectionWith(current);
103586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_intersection != kNoLifetime) {
103686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (inactive->IsFixed()) {
103786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        next_use[inactive->GetRegister()] =
103886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray            std::min(next_intersection, next_use[inactive->GetRegister()]);
103986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else {
10401ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray        size_t use = inactive->FirstUseAfter(current->GetStart());
104186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        if (use != kNoLifetime) {
104286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]);
104386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
104486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1045a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1046a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1047a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10486c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  int reg = kNoRegister;
10496c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  bool should_spill = false;
1050840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (current->HasRegister()) {
1051840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(current->IsHighInterval());
1052840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    reg = current->GetRegister();
10536c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // When allocating the low part, we made sure the high register was available.
10541ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray    DCHECK_LT(first_use, next_use[reg]);
1055840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (current->IsLowInterval()) {
1056da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    reg = FindAvailableRegisterPair(next_use, first_use);
10576c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // We should spill if both registers are not available.
10581ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray    should_spill = (first_use >= next_use[reg])
10591ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray      || (first_use >= next_use[GetHighForLowRegister(reg)]);
1060840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else {
1061840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
10628826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray    reg = FindAvailableRegister(next_use, current);
10631ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray    should_spill = (first_use >= next_use[reg]);
1064a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1065a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
10666c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK_NE(reg, kNoRegister);
10676c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  if (should_spill) {
1068840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    DCHECK(!current->IsHighInterval());
1069234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    bool is_allocation_at_use_site = (current->GetStart() >= (first_register_use - 1));
1070da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    if (is_allocation_at_use_site) {
1071da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray      if (!current->IsLowInterval()) {
1072da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray        DumpInterval(std::cerr, current);
1073da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray        DumpAllIntervals(std::cerr);
1074da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray        // This situation has the potential to infinite loop, so we make it a non-debug CHECK.
1075da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray        HInstruction* at = liveness_.GetInstructionFromPosition(first_register_use / 2);
1076da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray        CHECK(false) << "There is not enough registers available for "
1077da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray          << current->GetParent()->GetDefinedBy()->DebugName() << " "
1078da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray          << current->GetParent()->GetDefinedBy()->GetId()
1079da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray          << " at " << first_register_use - 1 << " "
1080da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray          << (at == nullptr ? "" : at->DebugName());
1081da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray      }
1082da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray
10836c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If we're allocating a register for `current` because the instruction at
10846c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // that position requires it, but we think we should spill, then there are
1085234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // non-pair intervals or unaligned pair intervals blocking the allocation.
1086234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // We split the first interval found, and put ourselves first in the
1087234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // `unhandled_` list.
1088da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray      bool success = TrySplitNonPairOrUnalignedPairIntervalAt(current->GetStart(),
1089da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray                                                              first_register_use,
1090da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray                                                              next_use);
1091da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray      DCHECK(success);
10922aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      LiveInterval* existing = unhandled_->back();
10936c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK(existing->IsHighInterval());
10946c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      DCHECK_EQ(existing->GetLowInterval(), current);
10952aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      unhandled_->push_back(current);
10966c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    } else {
10976c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // If the first use of that instruction is after the last use of the found
10986c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      // register, we split this interval just before its first register use.
10996c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AllocateSpillSlotFor(current);
11008cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray      LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1);
1101da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray      DCHECK(current != split);
11026c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      AddSorted(unhandled_, split);
11036c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    }
1104a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return false;
1105a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
1106a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Use this register and spill the active and inactives interval that
1107a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // have that register.
1108a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    current->SetRegister(reg);
1109a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11102aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    for (auto it = active_.begin(), end = active_.end(); it != end; ++it) {
11112aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      LiveInterval* active = *it;
1112a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (active->GetRegister() == reg) {
111386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        DCHECK(!active->IsFixed());
1114a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        LiveInterval* split = Split(active, current->GetStart());
1115dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        if (split != active) {
11162aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          handled_.push_back(active);
1117dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray        }
11182aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        RemoveIntervalAndPotentialOtherHalf(&active_, it);
11193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        AddSorted(unhandled_, split);
1120a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray        break;
1121a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
1122a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1123a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11242aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    // NOTE: Retrieve end() on each iteration because we're removing elements in the loop body.
11252aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    for (auto it = inactive_.begin(); it != inactive_.end(); ) {
11262aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      LiveInterval* inactive = *it;
11272aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      bool erased = false;
1128a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      if (inactive->GetRegister() == reg) {
1129296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang        if (!current->IsSplit() && !inactive->IsFixed()) {
1130296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Neither current nor inactive are fixed.
1131296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Thanks to SSA, a non-split interval starting in a hole of an
1132296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // inactive interval should never intersect with that inactive interval.
1133296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          // Only if it's not fixed though, because fixed intervals don't come from SSA.
1134296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang          DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime);
11352aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        } else {
11362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          size_t next_intersection = inactive->FirstIntersectionWith(current);
11372aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          if (next_intersection != kNoLifetime) {
11382aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko            if (inactive->IsFixed()) {
11392aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              LiveInterval* split = Split(current, next_intersection);
11402aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              DCHECK_NE(split, current);
11412aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              AddSorted(unhandled_, split);
11422aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko            } else {
11432aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              // Split at the start of `current`, which will lead to splitting
11442aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              // at the end of the lifetime hole of `inactive`.
11452aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              LiveInterval* split = Split(inactive, current->GetStart());
11462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              // If it's inactive, it must start before the current interval.
11472aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              DCHECK_NE(split, inactive);
11482aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              it = RemoveIntervalAndPotentialOtherHalf(&inactive_, it);
11492aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              erased = true;
11502aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              handled_.push_back(inactive);
11512aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko              AddSorted(unhandled_, split);
11525b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray            }
115386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray          }
115486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
1155a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      }
11562aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      // If we have erased the element, `it` already points to the next element.
11572aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      // Otherwise we need to move to the next element.
11582aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      if (!erased) {
11592aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        ++it;
11602aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      }
1161a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1162a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
1163a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return true;
1164a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1165a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1166a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11672aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Markovoid RegisterAllocator::AddSorted(ArenaVector<LiveInterval*>* array, LiveInterval* interval) {
1168c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray  DCHECK(!interval->IsFixed() && !interval->HasSpillSlot());
116986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t insert_at = 0;
11702aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (size_t i = array->size(); i > 0; --i) {
11712aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    LiveInterval* current = (*array)[i - 1u];
11726c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    // High intervals must be processed right after their low equivalent.
11736c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray    if (current->StartsAfter(interval) && !current->IsHighInterval()) {
117486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      insert_at = i;
1175a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      break;
1176acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray    } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) {
1177acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // Ensure the slow path interval is the last to be processed at its location: we want the
1178acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      // interval to know all live registers at this location.
11792aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      DCHECK(i == 1 || (*array)[i - 2u]->StartsAfter(current));
1180acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      insert_at = i;
1181acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray      break;
1182a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    }
1183a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1184840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
1185840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  // Insert the high interval before the low, to ensure the low is processed before.
11862aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  auto insert_pos = array->begin() + insert_at;
1187840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->HasHighInterval()) {
11882aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    array->insert(insert_pos, { interval->GetHighInterval(), interval });
1189840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  } else if (interval->HasLowInterval()) {
11902aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    array->insert(insert_pos, { interval, interval->GetLowInterval() });
11912aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  } else {
11922aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    array->insert(insert_pos, interval);
1193840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1194a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1195a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
11968cbab3c4de3328b576454ce702d7748f56c44346Nicolas GeoffrayLiveInterval* RegisterAllocator::SplitBetween(LiveInterval* interval, size_t from, size_t to) {
1197fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  HBasicBlock* block_from = liveness_.GetBlockFromPosition(from / 2);
1198fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  HBasicBlock* block_to = liveness_.GetBlockFromPosition(to / 2);
11998cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  DCHECK(block_from != nullptr);
12008cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  DCHECK(block_to != nullptr);
12018cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
12028cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // Both locations are in the same block. We split at the given location.
12038cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  if (block_from == block_to) {
12048cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    return Split(interval, to);
12058cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  }
12068cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
1207fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  /*
1208fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * Non-linear control flow will force moves at every branch instruction to the new location.
1209fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * To avoid having all branches doing the moves, we find the next non-linear position and
1210fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * split the interval at this position. Take the following example (block number is the linear
1211fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * order position):
1212fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *
1213fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *     B1
1214fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *    /  \
1215fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *   B2  B3
1216fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *    \  /
1217fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *     B4
1218fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *
1219fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * B2 needs to split an interval, whose next use is in B4. If we were to split at the
1220fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * beginning of B4, B3 would need to do a move between B3 and B4 to ensure the interval
1221fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * is now in the correct location. It makes performance worst if the interval is spilled
1222fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * and both B2 and B3 need to reload it before entering B4.
1223fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   *
1224fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * By splitting at B3, we give a chance to the register allocator to allocate the
1225fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * interval to the same register as in B1, and therefore avoid doing any
1226fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   * moves in B3.
1227fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray   */
1228fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  if (block_from->GetDominator() != nullptr) {
12296058455d486219994921b63a2d774dc9908415a2Vladimir Marko    for (HBasicBlock* dominated : block_from->GetDominator()->GetDominatedBlocks()) {
12306058455d486219994921b63a2d774dc9908415a2Vladimir Marko      size_t position = dominated->GetLifetimeStart();
1231fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray      if ((position > from) && (block_to->GetLifetimeStart() > position)) {
1232fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        // Even if we found a better block, we continue iterating in case
1233fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        // a dominated block is closer.
1234fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        // Note that dominated blocks are not sorted in liveness order.
12356058455d486219994921b63a2d774dc9908415a2Vladimir Marko        block_to = dominated;
1236fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray        DCHECK_NE(block_to, block_from);
1237fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray      }
1238fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray    }
1239fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray  }
1240fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray
12418cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // If `to` is in a loop, find the outermost loop header which does not contain `from`.
12428cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  for (HLoopInformationOutwardIterator it(*block_to); !it.Done(); it.Advance()) {
12438cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    HBasicBlock* header = it.Current()->GetHeader();
12448cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    if (block_from->GetLifetimeStart() >= header->GetLifetimeStart()) {
12458cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray      break;
12468cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    }
12478cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray    block_to = header;
12488cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  }
12498cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
12508cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // Split at the start of the found block, to piggy back on existing moves
12518cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  // due to resolution if non-linear control flow (see `ConnectSplitSiblings`).
12528cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray  return Split(interval, block_to->GetLifetimeStart());
12538cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray}
12548cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray
1255a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) {
1256840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  DCHECK_GE(position, interval->GetStart());
1257a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  DCHECK(!interval->IsDeadAt(position));
1258a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  if (position == interval->GetStart()) {
1259a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    // Spill slot will be allocated when handling `interval` again.
1260a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    interval->ClearRegister();
1261840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1262840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetHighInterval()->ClearRegister();
1263840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1264840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      interval->GetLowInterval()->ClearRegister();
1265840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1266a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return interval;
1267a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  } else {
1268a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    LiveInterval* new_interval = interval->SplitAt(position);
1269840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (interval->HasHighInterval()) {
1270840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* high = interval->GetHighInterval()->SplitAt(position);
1271840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetHighInterval(high);
1272840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      high->SetLowInterval(new_interval);
1273840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    } else if (interval->HasLowInterval()) {
1274840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      LiveInterval* low = interval->GetLowInterval()->SplitAt(position);
1275840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      new_interval->SetLowInterval(low);
1276840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      low->SetHighInterval(new_interval);
1277840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
1278a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray    return new_interval;
1279a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  }
1280a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}
1281a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
128231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) {
1283840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  if (interval->IsHighInterval()) {
1284da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    // The low interval already took care of allocating the spill slot.
1285da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    DCHECK(!interval->GetLowInterval()->HasRegister());
1286da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray    DCHECK(interval->GetLowInterval()->GetParent()->HasSpillSlot());
1287840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    return;
1288840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray  }
1289840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray
129031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  LiveInterval* parent = interval->GetParent();
129131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
129231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // An instruction gets a spill slot for its entire lifetime. If the parent
129331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  // of this interval already has a spill slot, there is nothing to do.
129431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  if (parent->HasSpillSlot()) {
129531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    return;
129631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
129731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
129886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* defined_by = parent->GetDefinedBy();
129977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  DCHECK(!defined_by->IsPhi() || !defined_by->AsPhi()->IsCatchPhi());
130077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
130186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (defined_by->IsParameterValue()) {
130286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Parameters have their own stack slot.
130386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue()));
130486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
130586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
130686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
130776b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray  if (defined_by->IsCurrentMethod()) {
130876b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray    parent->SetSpillSlot(0);
130976b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray    return;
131076b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray  }
131176b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray
131296f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  if (defined_by->IsConstant()) {
131396f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    // Constants don't need a spill slot.
131496f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray    return;
131596f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray  }
131696f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray
13172aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  ArenaVector<size_t>* spill_slots = nullptr;
1318776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  switch (interval->GetType()) {
1319776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimDouble:
1320776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &double_spill_slots_;
1321776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1322776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimLong:
1323776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &long_spill_slots_;
1324776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1325776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimFloat:
1326776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &float_spill_slots_;
1327776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1328776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimNot:
1329776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimInt:
1330776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimChar:
1331776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimByte:
1332776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimBoolean:
1333776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimShort:
1334776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      spill_slots = &int_spill_slots_;
1335776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      break;
1336776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    case Primitive::kPrimVoid:
1337776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      LOG(FATAL) << "Unexpected type for interval " << interval->GetType();
1338776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  }
1339776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray
1340412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  // Find an available spill slot.
1341412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  size_t slot = 0;
13422aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (size_t e = spill_slots->size(); slot < e; ++slot) {
13432aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    if ((*spill_slots)[slot] <= parent->GetStart()
13442aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko        && (slot == (e - 1) || (*spill_slots)[slot + 1] <= parent->GetStart())) {
1345412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray      break;
1346412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray    }
1347412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray  }
1348412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray
134977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  size_t end = interval->GetLastSibling()->GetEnd();
135001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  if (parent->NeedsTwoSpillSlots()) {
13512aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    if (slot + 2u > spill_slots->size()) {
13523c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
13532aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      spill_slots->resize(slot + 2u, end);
135431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray    }
13552aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    (*spill_slots)[slot] = end;
13562aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    (*spill_slots)[slot + 1] = end;
135731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  } else {
13582aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko    if (slot == spill_slots->size()) {
13593c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray      // We need a new spill slot.
13602aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      spill_slots->push_back(end);
13613c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    } else {
13622aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko      (*spill_slots)[slot] = end;
13633c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    }
136431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray  }
136531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1366776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // Note that the exact spill slot location will be computed when we resolve,
1367776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  // that is when we know the number of spill slots for each type.
1368776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  parent->SetSpillSlot(slot);
136986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
137086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
13712a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffraystatic bool IsValidDestination(Location destination) {
1372102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray  return destination.IsRegister()
13736c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray      || destination.IsRegisterPair()
1374102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsFpuRegister()
1375840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      || destination.IsFpuRegisterPair()
1376102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsStackSlot()
1377102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray      || destination.IsDoubleStackSlot();
13782a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray}
13792a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray
138077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilvoid RegisterAllocator::AllocateSpillSlotForCatchPhi(HPhi* phi) {
138177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  LiveInterval* interval = phi->GetLiveInterval();
138277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
138377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  HInstruction* previous_phi = phi->GetPrevious();
138477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  DCHECK(previous_phi == nullptr ||
138577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil         previous_phi->AsPhi()->GetRegNumber() <= phi->GetRegNumber())
138677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      << "Phis expected to be sorted by vreg number, so that equivalent phis are adjacent.";
138777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
138877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  if (phi->IsVRegEquivalentOf(previous_phi)) {
138977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    // This is an equivalent of the previous phi. We need to assign the same
139077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    // catch phi slot.
139177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    DCHECK(previous_phi->GetLiveInterval()->HasSpillSlot());
139277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    interval->SetSpillSlot(previous_phi->GetLiveInterval()->GetSpillSlot());
139377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  } else {
139477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    // Allocate a new spill slot for this catch phi.
139577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    // TODO: Reuse spill slots when intervals of phis from different catch
139677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    //       blocks do not overlap.
139777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    interval->SetSpillSlot(catch_phi_spill_slots_);
139877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    catch_phi_spill_slots_ += interval->NeedsTwoSpillSlots() ? 2 : 1;
139977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  }
140077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil}
140177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
1402234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddMove(HParallelMove* move,
1403234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location source,
1404234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Location destination,
1405234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                HInstruction* instruction,
1406234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                Primitive::Type type) const {
1407234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (type == Primitive::kPrimLong
1408234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && codegen_->ShouldSplitLongMoves()
1409234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // The parallel move resolver knows how to deal with long constants.
1410234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      && !source.IsConstant()) {
14119021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source.ToLow(), destination.ToLow(), Primitive::kPrimInt, instruction);
14129021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source.ToHigh(), destination.ToHigh(), Primitive::kPrimInt, nullptr);
1413234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  } else {
14149021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray    move->AddMove(source, destination, type, instruction);
1415234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  }
1416234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray}
1417234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1418234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* input,
1419234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray                                        HInstruction* user,
142086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
142186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
142286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
142386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1424476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  DCHECK(!user->IsPhi());
142586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1426740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  HInstruction* previous = user->GetPrevious();
142786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = nullptr;
142886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (previous == nullptr
1429476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain      || !previous->IsParallelMove()
14308e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray      || previous->GetLifetimePosition() < user->GetLifetimePosition()) {
143186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
14328e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    move->SetLifetimePosition(user->GetLifetimePosition());
1433740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    user->GetBlock()->InsertInstructionBefore(move, user);
143486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
143586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
143686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
14378e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition());
1438234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, nullptr, input->GetType());
143986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
144086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
144146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionStart(size_t position) {
144246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 0;
144346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
144446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
144546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionEnd(size_t position) {
144646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  return (position & 1) == 1;
144746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray}
144846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray
144986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position,
1450740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                             HInstruction* instruction,
145186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location source,
145286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             Location destination) const {
14536c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
145486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
145586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
145686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* at = liveness_.GetInstructionFromPosition(position / 2);
145786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
145846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  if (at == nullptr) {
145946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    if (IsInstructionStart(position)) {
146046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Block boundary, don't do anything the connection of split siblings will handle it.
146146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      return;
146246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    } else {
146346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      // Move must happen before the first instruction of the block.
146446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      at = liveness_.GetInstructionFromPosition((position + 1) / 2);
14655976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // Note that parallel moves may have already been inserted, so we explicitly
14665976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      // ask for the first instruction of the block: `GetInstructionFromPosition` does
1467234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      // not contain the `HParallelMove` instructions.
14685976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      at = at->GetBlock()->GetFirstInstruction();
1469234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
1470234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      if (at->GetLifetimePosition() < position) {
1471234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // We may insert moves for split siblings and phi spills at the beginning of the block.
1472234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        // Since this is a different lifetime position, we need to go to the next instruction.
1473234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        DCHECK(at->IsParallelMove());
1474234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray        at = at->GetNext();
1475234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray      }
1476234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray
14775976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      if (at->GetLifetimePosition() != position) {
14785976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK_GT(at->GetLifetimePosition(), position);
147946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move = new (allocator_) HParallelMove(allocator_);
148046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        move->SetLifetimePosition(position);
148146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray        at->GetBlock()->InsertInstructionBefore(move, at);
14825976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      } else {
14835976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        DCHECK(at->IsParallelMove());
14845976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray        move = at->AsParallelMove();
148546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray      }
148646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray    }
148746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray  } else if (IsInstructionEnd(position)) {
148886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen after the instruction.
148986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    DCHECK(!at->IsControlFlow());
149086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = at->GetNext()->AsParallelMove();
1491e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // This is a parallel move for connecting siblings in a same block. We need to
1492e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    // differentiate it with moves for connecting blocks, and input moves.
14938e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray    if (move == nullptr || move->GetLifetimePosition() > position) {
149486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
149586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
149686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at->GetNext());
149786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
149886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
149986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Move must happen before the instruction.
150086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* previous = at->GetPrevious();
1501740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    if (previous == nullptr
1502740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || !previous->IsParallelMove()
1503740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray        || previous->GetLifetimePosition() != position) {
1504740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // If the previous is a parallel move, then its position must be lower
1505740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // than the given `position`: it was added just after the non-parallel
1506740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      // move instruction that precedes `instruction`.
1507740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      DCHECK(previous == nullptr
1508740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || !previous->IsParallelMove()
1509740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray             || previous->GetLifetimePosition() < position);
151086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = new (allocator_) HParallelMove(allocator_);
151186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move->SetLifetimePosition(position);
151286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      at->GetBlock()->InsertInstructionBefore(move, at);
151386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
151486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      move = previous->AsParallelMove();
151586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
151686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
151701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray  DCHECK_EQ(move->GetLifetimePosition(), position);
1518234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
151986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
152086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
152186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block,
1522740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                   HInstruction* instruction,
152386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location source,
152486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                   Location destination) const {
15256c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
152686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
152786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1528d26a411adee1e71b3f09dd604ab9b23018037138David Brazdil  DCHECK_EQ(block->GetNormalSuccessors().size(), 1u);
152986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* last = block->GetLastInstruction();
1530360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // We insert moves at exit for phi predecessors and connecting blocks.
1531fe57faa2e0349418dda38e77ef1c0ac29db75f4dMark Mendell  // A block ending with an if or a packed switch cannot branch to a block
1532fe57faa2e0349418dda38e77ef1c0ac29db75f4dMark Mendell  // with phis because we do not allow critical edges. It can also not connect
1533360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray  // a split interval between two blocks: the move has to happen in the successor.
1534fe57faa2e0349418dda38e77ef1c0ac29db75f4dMark Mendell  DCHECK(!last->IsIf() && !last->IsPackedSwitch());
153586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* previous = last->GetPrevious();
153686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move;
1537e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1538e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and output moves.
15395976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray  size_t position = last->GetLifetimePosition();
1540740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray  if (previous == nullptr || !previous->IsParallelMove()
15415976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray      || previous->AsParallelMove()->GetLifetimePosition() != position) {
154286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
15435976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray    move->SetLifetimePosition(position);
154486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, last);
154586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
154686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = previous->AsParallelMove();
154786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1548234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
154986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
155086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
155186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block,
1552740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                                    HInstruction* instruction,
155386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location source,
155486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                                    Location destination) const {
15556c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
155686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
155786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
155886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HInstruction* first = block->GetFirstInstruction();
155986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = first->AsParallelMove();
1560234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  size_t position = block->GetLifetimeStart();
1561e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for connecting blocks. We need to differentiate
1562e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // it with moves for connecting siblings in a same block, and input moves.
1563234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
156486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1565234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray    move->SetLifetimePosition(position);
156686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    block->InsertInstructionBefore(move, first);
156786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1568234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
156986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
157086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
157186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction,
157286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location source,
157386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                        Location destination) const {
15746c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray  DCHECK(IsValidDestination(destination)) << destination;
157586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (source.Equals(destination)) return;
157686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1577476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain  if (instruction->IsPhi()) {
1578740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination);
157986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
158086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
158186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1582e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  size_t position = instruction->GetLifetimePosition() + 1;
158386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  HParallelMove* move = instruction->GetNext()->AsParallelMove();
1584e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // This is a parallel move for moving the output of an instruction. We need
1585e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // to differentiate with input moves, moves for connecting siblings in a
1586e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  // and moves for connecting blocks.
1587e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray  if (move == nullptr || move->GetLifetimePosition() != position) {
158886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    move = new (allocator_) HParallelMove(allocator_);
1589e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray    move->SetLifetimePosition(position);
159086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext());
159186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
1592234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  AddMove(move, source, destination, instruction, instruction->GetType());
159386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
159486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
159586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) {
159686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  LiveInterval* current = interval;
159776b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray  if (current->HasSpillSlot()
159876b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      && current->HasRegister()
159976b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      // Currently, we spill unconditionnally the current method in the code generators.
160076b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      && !interval->GetDefinedBy()->IsCurrentMethod()) {
160186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // We spill eagerly, so move must be at definition.
160286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    InsertMoveAfter(interval->GetDefinedBy(),
1603840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray                    interval->ToLocation(),
160401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                    interval->NeedsTwoSpillSlots()
1605412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot())
1606412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray                        : Location::StackSlot(interval->GetParent()->GetSpillSlot()));
160786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
160886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  UsePosition* use = current->GetFirstUse();
16094ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray  UsePosition* env_use = current->GetFirstEnvironmentUse();
161086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
161186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Walk over all siblings, updating locations of use positions, and
161286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // connecting them when they are adjacent.
161386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  do {
161401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
161586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
161686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Walk over all uses covered by this interval, and update the location
161786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // information.
1618d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray
1619d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    LiveRange* range = current->GetFirstRange();
1620d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray    while (range != nullptr) {
1621579026039080252878106118645ed70706f4838eNicolas Geoffray      while (use != nullptr && use->GetPosition() < range->GetStart()) {
1622579026039080252878106118645ed70706f4838eNicolas Geoffray        DCHECK(use->IsSynthesized());
1623579026039080252878106118645ed70706f4838eNicolas Geoffray        use = use->GetNext();
1624579026039080252878106118645ed70706f4838eNicolas Geoffray      }
1625d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      while (use != nullptr && use->GetPosition() <= range->GetEnd()) {
16264ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray        DCHECK(!use->GetIsEnvironment());
16273fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil        DCHECK(current->CoversSlow(use->GetPosition()) || (use->GetPosition() == range->GetEnd()));
1628579026039080252878106118645ed70706f4838eNicolas Geoffray        if (!use->IsSynthesized()) {
1629579026039080252878106118645ed70706f4838eNicolas Geoffray          LocationSummary* locations = use->GetUser()->GetLocations();
1630579026039080252878106118645ed70706f4838eNicolas Geoffray          Location expected_location = locations->InAt(use->GetInputIndex());
1631579026039080252878106118645ed70706f4838eNicolas Geoffray          // The expected (actual) location may be invalid in case the input is unused. Currently
1632579026039080252878106118645ed70706f4838eNicolas Geoffray          // this only happens for intrinsics.
1633579026039080252878106118645ed70706f4838eNicolas Geoffray          if (expected_location.IsValid()) {
1634579026039080252878106118645ed70706f4838eNicolas Geoffray            if (expected_location.IsUnallocated()) {
1635579026039080252878106118645ed70706f4838eNicolas Geoffray              locations->SetInAt(use->GetInputIndex(), source);
1636579026039080252878106118645ed70706f4838eNicolas Geoffray            } else if (!expected_location.IsConstant()) {
1637579026039080252878106118645ed70706f4838eNicolas Geoffray              AddInputMoveFor(interval->GetDefinedBy(), use->GetUser(), source, expected_location);
1638579026039080252878106118645ed70706f4838eNicolas Geoffray            }
1639579026039080252878106118645ed70706f4838eNicolas Geoffray          } else {
1640579026039080252878106118645ed70706f4838eNicolas Geoffray            DCHECK(use->GetUser()->IsInvoke());
1641579026039080252878106118645ed70706f4838eNicolas Geoffray            DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone);
1642d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray          }
164386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        }
1644d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray        use = use->GetNext();
164586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
16464ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray
16474ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      // Walk over the environment uses, and update their locations.
16484ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      while (env_use != nullptr && env_use->GetPosition() < range->GetStart()) {
16494ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray        env_use = env_use->GetNext();
16504ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      }
16514ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray
16524ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      while (env_use != nullptr && env_use->GetPosition() <= range->GetEnd()) {
16530a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray        DCHECK(current->CoversSlow(env_use->GetPosition())
16540a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray               || (env_use->GetPosition() == range->GetEnd()));
1655d23eeef3492b53102eb8093524cf37e2b4c296dbNicolas Geoffray        HEnvironment* environment = env_use->GetEnvironment();
16560a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray        environment->SetLocationAt(env_use->GetInputIndex(), source);
16574ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray        env_use = env_use->GetNext();
16584ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray      }
16594ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray
1660d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray      range = range->GetNext();
166186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
166286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
166386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // If the next interval starts just after this one, and has a register,
166486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // insert a move.
166586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* next_sibling = current->GetNextSibling();
166686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (next_sibling != nullptr
166786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && next_sibling->HasRegister()
166886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        && current->GetEnd() == next_sibling->GetStart()) {
166901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray      Location destination = next_sibling->ToLocation();
1670740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray      InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination);
167186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
16723946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
167343af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray    for (SafepointPosition* safepoint_position = current->GetFirstSafepoint();
167443af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray         safepoint_position != nullptr;
167543af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray         safepoint_position = safepoint_position->GetNext()) {
16763fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil      DCHECK(current->CoversSlow(safepoint_position->GetPosition()));
16773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
16785588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray      LocationSummary* locations = safepoint_position->GetLocations();
16793bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) {
16803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize);
16813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
16823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
16833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      switch (source.GetKind()) {
16843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kRegister: {
16853bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray          locations->AddLiveRegister(source);
1686988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) {
1687988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray            DCHECK_LE(locations->GetNumberOfLiveRegisters(),
1688988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_core_registers_ +
1689988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray                      maximum_number_of_live_fp_registers_);
1690988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          }
16913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          if (current->GetType() == Primitive::kPrimNot) {
169256b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray            locations->SetRegisterBit(source.reg());
16933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          }
16943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
16953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
1696102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        case Location::kFpuRegister: {
1697102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          locations->AddLiveRegister(source);
1698102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray          break;
1699102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray        }
17006c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray
17016c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray        case Location::kRegisterPair:
1702840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        case Location::kFpuRegisterPair: {
1703840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToLow());
1704840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          locations->AddLiveRegister(source.ToHigh());
1705840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          break;
1706840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
17073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kStackSlot:  // Fall-through
17083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kDoubleStackSlot:  // Fall-through
17093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        case Location::kConstant: {
17103946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          // Nothing to do.
17113946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          break;
17123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
17133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        default: {
17143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray          LOG(FATAL) << "Unexpected location for object";
17153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray        }
17163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
17173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
171886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    current = next_sibling;
171986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } while (current != nullptr);
1720d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray
1721579026039080252878106118645ed70706f4838eNicolas Geoffray  if (kIsDebugBuild) {
1722579026039080252878106118645ed70706f4838eNicolas Geoffray    // Following uses can only be synthesized uses.
1723579026039080252878106118645ed70706f4838eNicolas Geoffray    while (use != nullptr) {
1724579026039080252878106118645ed70706f4838eNicolas Geoffray      DCHECK(use->IsSynthesized());
1725579026039080252878106118645ed70706f4838eNicolas Geoffray      use = use->GetNext();
1726579026039080252878106118645ed70706f4838eNicolas Geoffray    }
1727579026039080252878106118645ed70706f4838eNicolas Geoffray  }
172886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
172986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
173086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval,
173186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* from,
173286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray                                             HBasicBlock* to) const {
173386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (interval->GetNextSibling() == nullptr) {
173486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Nothing to connect. The whole range was allocated to the same location.
173586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
173686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
173786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
1738241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  // Find the intervals that cover `from` and `to`.
1739241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  LiveInterval* destination = interval->GetSiblingAt(to->GetLifetimeStart());
1740241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil  LiveInterval* source = interval->GetSiblingAt(from->GetLifetimeEnd() - 1);
174186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
174286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (destination == source) {
174386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Interval was not split.
174486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
174586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
17468ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray  DCHECK(destination != nullptr && source != nullptr);
17478ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray
174886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  if (!destination->HasRegister()) {
174986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Values are eagerly spilled. Spill slot already contains appropriate value.
175086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    return;
175186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
175286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
175386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // If `from` has only one successor, we can put the moves at the exit of it. Otherwise
175486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // we need to put the moves at the entry of `to`.
1755d26a411adee1e71b3f09dd604ab9b23018037138David Brazdil  if (from->GetNormalSuccessors().size() == 1) {
1756740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtExitOf(from,
1757740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                               interval->GetParent()->GetDefinedBy(),
175801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               source->ToLocation(),
175901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                               destination->ToLocation());
176086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
17616058455d486219994921b63a2d774dc9908415a2Vladimir Marko    DCHECK_EQ(to->GetPredecessors().size(), 1u);
1762740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray    InsertParallelMoveAtEntryOf(to,
1763740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray                                interval->GetParent()->GetDefinedBy(),
176401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                source->ToLocation(),
176501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray                                destination->ToLocation());
176686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
176786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray}
176886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
176986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() {
1770776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray  codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(),
17714c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_core_registers_,
17724c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     maximum_number_of_live_fp_registers_,
17734c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                     reserved_out_slots_,
17740d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray                                     codegen_->GetGraph()->GetLinearOrder());
177586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
177686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Adjust the Out Location of instructions.
177786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration.
177886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
177986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
178086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LiveInterval* current = instruction->GetLiveInterval();
178186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LocationSummary* locations = instruction->GetLocations();
178286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    Location location = locations->Out();
1783476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain    if (instruction->IsParameterValue()) {
178486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      // Now that we know the frame size, adjust the parameter's location.
178586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.IsStackSlot()) {
178686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
178786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1788f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
178986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (location.IsDoubleStackSlot()) {
179086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
179186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(location.GetStackIndex());
1792f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray        locations->UpdateOut(location);
179386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      } else if (current->HasSpillSlot()) {
179486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray        current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize());
179586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
179676b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray    } else if (instruction->IsCurrentMethod()) {
179776b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      // The current method is always at offset 0.
179876b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray      DCHECK(!current->HasSpillSlot() || (current->GetSpillSlot() == 0));
179977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    } else if (instruction->IsPhi() && instruction->AsPhi()->IsCatchPhi()) {
180077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      DCHECK(current->HasSpillSlot());
180177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      size_t slot = current->GetSpillSlot()
180277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                    + GetNumberOfSpillSlots()
180377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                    + reserved_out_slots_
180477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                    - catch_phi_spill_slots_;
180577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      current->SetSpillSlot(slot * kVRegSize);
1806776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray    } else if (current->HasSpillSlot()) {
1807776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // Adjust the stack slot, now that we know the number of them for each type.
1808776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      // The way this implementation lays out the stack is the following:
180977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [parameter slots       ]
181077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [catch phi spill slots ]
181177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [double spill slots    ]
181277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [long spill slots      ]
181377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [float spill slots     ]
181477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [int/ref values        ]
181577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [maximum out values    ] (number of arguments for calls)
181677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // [art method            ].
181777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      size_t slot = current->GetSpillSlot();
1818776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      switch (current->GetType()) {
1819776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimDouble:
18202aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          slot += long_spill_slots_.size();
1821776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1822776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimLong:
18232aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          slot += float_spill_slots_.size();
1824776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1825776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimFloat:
18262aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko          slot += int_spill_slots_.size();
1827776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          FALLTHROUGH_INTENDED;
1828776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimNot:
1829776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimInt:
1830776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimChar:
1831776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimByte:
1832776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimBoolean:
1833776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimShort:
1834776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          slot += reserved_out_slots_;
1835776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          break;
1836776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray        case Primitive::kPrimVoid:
1837776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray          LOG(FATAL) << "Unexpected type for interval " << current->GetType();
1838776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      }
1839776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray      current->SetSpillSlot(slot * kVRegSize);
184086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
184186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
184201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    Location source = current->ToLocation();
184386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
184486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    if (location.IsUnallocated()) {
184586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      if (location.GetPolicy() == Location::kSameAsFirstInput) {
1846d0d4852847432368b090c184d6639e573538dccfCalin Juravle        if (locations->InAt(0).IsUnallocated()) {
1847d0d4852847432368b090c184d6639e573538dccfCalin Juravle          locations->SetInAt(0, source);
1848d0d4852847432368b090c184d6639e573538dccfCalin Juravle        } else {
1849d0d4852847432368b090c184d6639e573538dccfCalin Juravle          DCHECK(locations->InAt(0).Equals(source));
1850d0d4852847432368b090c184d6639e573538dccfCalin Juravle        }
185186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
1852829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray      locations->UpdateOut(source);
185386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    } else {
185486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      DCHECK(source.Equals(location));
185586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
185686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
185786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
185886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Connect siblings.
185986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) {
186086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i);
186186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    ConnectSiblings(instruction->GetLiveInterval());
186286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
186386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
186486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve non-linear control flow across branches. Order does not matter.
18650d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
186686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* block = it.Current();
186777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    if (block->IsCatchBlock()) {
186877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // Instructions live at the top of catch blocks were forced to spill.
186977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      if (kIsDebugBuild) {
187077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        BitVector* live = liveness_.GetLiveInSet(*block);
187177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        for (uint32_t idx : live->Indexes()) {
187277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          LiveInterval* interval = liveness_.GetInstructionFromSsaIndex(idx)->GetLiveInterval();
187377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          DCHECK(!interval->GetSiblingAt(block->GetLifetimeStart())->HasRegister());
187477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        }
187577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      }
187677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    } else {
187777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      BitVector* live = liveness_.GetLiveInSet(*block);
187877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      for (uint32_t idx : live->Indexes()) {
187977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        LiveInterval* interval = liveness_.GetInstructionFromSsaIndex(idx)->GetLiveInterval();
188077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        for (HBasicBlock* predecessor : block->GetPredecessors()) {
188177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          ConnectSplitSiblings(interval, predecessor, block);
188277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        }
188386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
188486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
188586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
188686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
188786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  // Resolve phi inputs. Order does not matter.
18880d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) {
188986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    HBasicBlock* current = it.Current();
189077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    if (current->IsCatchBlock()) {
189177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // Catch phi values are set at runtime by the exception delivery mechanism.
189277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    } else {
189377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
189477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        HInstruction* phi = inst_it.Current();
189577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        for (size_t i = 0, e = current->GetPredecessors().size(); i < e; ++i) {
1896ec7802a102d49ab5c17495118d4fe0bcc7287bebVladimir Marko          HBasicBlock* predecessor = current->GetPredecessors()[i];
1897d26a411adee1e71b3f09dd604ab9b23018037138David Brazdil          DCHECK_EQ(predecessor->GetNormalSuccessors().size(), 1u);
189877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          HInstruction* input = phi->InputAt(i);
189977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          Location source = input->GetLiveInterval()->GetLocationAt(
190077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil              predecessor->GetLifetimeEnd() - 1);
190177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          Location destination = phi->GetLiveInterval()->ToLocation();
190277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          InsertParallelMoveAtExitOf(predecessor, phi, source, destination);
190377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        }
190486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      }
190586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
190686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  }
19073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
19083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Assign temp locations.
19092aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko  for (LiveInterval* temp : temp_intervals_) {
1910840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    if (temp->IsHighInterval()) {
1911840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      // High intervals can be skipped, they are already handled by the low interval.
1912840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray      continue;
1913840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray    }
191401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    HInstruction* at = liveness_.GetTempUser(temp);
1915f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray    size_t temp_index = liveness_.GetTempIndex(temp);
191601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray    LocationSummary* locations = at->GetLocations();
19175368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    switch (temp->GetType()) {
19185368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimInt:
1919f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray        locations->SetTempAt(temp_index, Location::RegisterLocation(temp->GetRegister()));
19205368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
19215368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
19225368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      case Primitive::kPrimDouble:
1923840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) {
1924840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray          Location location = Location::FpuRegisterPairLocation(
1925840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray              temp->GetRegister(), temp->GetHighInterval()->GetRegister());
1926f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          locations->SetTempAt(temp_index, location);
1927840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        } else {
1928f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray          locations->SetTempAt(temp_index, Location::FpuRegisterLocation(temp->GetRegister()));
1929840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray        }
19305368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        break;
19315368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain
19325368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain      default:
19335368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain        LOG(FATAL) << "Unexpected type for temporary location "
19345368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain                   << temp->GetType();
19355368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain    }
19363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
193731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}
193831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray
1939a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray}  // namespace art
1940