register_allocator.cc revision b95fb775cc4c08349d0d905adbc96ad85e50601d
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) { 88234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray return instruction_set == kArm64 896c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray || instruction_set == kX86_64 904dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze || instruction_set == kMips64 916c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray || instruction_set == kArm 92234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray || instruction_set == kX86 93234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray || instruction_set == kThumb2; 9486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 9586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 9686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) { 973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (interval == nullptr) return false; 9886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray bool is_core_register = (interval->GetType() != Primitive::kPrimDouble) 9986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray && (interval->GetType() != Primitive::kPrimFloat); 100a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return processing_core_registers == is_core_register; 101a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 102a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 10386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegisters() { 10486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray AllocateRegistersInternal(); 10586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Resolve(); 10686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 10786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (kIsDebugBuild) { 10886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray processing_core_registers_ = true; 10986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray ValidateInternal(true); 11086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray processing_core_registers_ = false; 11186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray ValidateInternal(true); 1125976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray // Check that the linear order is still correct with regards to lifetime positions. 1135976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray // Since only parallel moves have been inserted during the register allocation, 1145976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray // these checks are mostly for making sure these moves have been added correctly. 1155976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray size_t current_liveness = 0; 1160d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) { 1175976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray HBasicBlock* block = it.Current(); 1185976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { 1195976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray HInstruction* instruction = inst_it.Current(); 1205976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray DCHECK_LE(current_liveness, instruction->GetLifetimePosition()); 1215976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray current_liveness = instruction->GetLifetimePosition(); 1225976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray } 1235976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray for (HInstructionIterator inst_it(block->GetInstructions()); 1245976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray !inst_it.Done(); 1255976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray inst_it.Advance()) { 1265976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray HInstruction* instruction = inst_it.Current(); 1275976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray DCHECK_LE(current_liveness, instruction->GetLifetimePosition()) << instruction->DebugName(); 1285976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray current_liveness = instruction->GetLifetimePosition(); 1295976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray } 1305976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray } 13186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 13286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 13386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 13477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilvoid RegisterAllocator::BlockRegister(Location location, size_t start, size_t end) { 13556b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray int reg = location.reg(); 136102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray DCHECK(location.IsRegister() || location.IsFpuRegister()); 137102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray LiveInterval* interval = location.IsRegister() 1382aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko ? physical_core_register_intervals_[reg] 1392aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko : physical_fp_register_intervals_[reg]; 140102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray Primitive::Type type = location.IsRegister() 141102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray ? Primitive::kPrimInt 142840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray : Primitive::kPrimFloat; 14386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (interval == nullptr) { 14486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray interval = LiveInterval::MakeFixedInterval(allocator_, reg, type); 145102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (location.IsRegister()) { 1462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko physical_core_register_intervals_[reg] = interval; 147102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } else { 1482aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko physical_fp_register_intervals_[reg] = interval; 149102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 15086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 15186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(interval->GetRegister() == reg); 15286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray interval->AddRange(start, end); 15386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 15486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 15577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilvoid RegisterAllocator::BlockRegisters(size_t start, size_t end, bool caller_save_only) { 15677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) { 15777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil if (!caller_save_only || !codegen_->IsCoreCalleeSaveRegister(i)) { 15877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil BlockRegister(Location::RegisterLocation(i), start, end); 15977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 16077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 16177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) { 16277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil if (!caller_save_only || !codegen_->IsFloatingPointCalleeSaveRegister(i)) { 16377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil BlockRegister(Location::FpuRegisterLocation(i), start, end); 16477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 16577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 16677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil} 16777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil 16886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegistersInternal() { 1693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Iterate post-order, to ensure the list is sorted, and the last added interval 1703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // is the one with the lowest start position. 1710d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray for (HLinearPostOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) { 1723946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray HBasicBlock* block = it.Current(); 173277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done(); 174277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe back_it.Advance()) { 175277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe ProcessInstruction(back_it.Current()); 1763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 177277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { 178277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe ProcessInstruction(inst_it.Current()); 1793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 18077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil 18177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil if (block->IsCatchBlock()) { 18277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // By blocking all registers at the top of each catch block, we force 18377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // intervals used after catch to spill. 18477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil size_t position = block->GetLifetimeStart(); 18577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil BlockRegisters(position, position + 1); 18677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 1873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 188a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 1893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray number_of_registers_ = codegen_->GetNumberOfCoreRegisters(); 1905233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_, 1915233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko kArenaAllocRegisterAllocator); 1923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray processing_core_registers_ = true; 1933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray unhandled_ = &unhandled_core_intervals_; 1942aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* fixed : physical_core_register_intervals_) { 195102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (fixed != nullptr) { 196296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Fixed interval is added to inactive_ instead of unhandled_. 197296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // It's also the only type of inactive interval whose start position 198296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // can be after the current interval during linear scan. 199296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Fixed interval is never split and never moves to unhandled_. 2002aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko inactive_.push_back(fixed); 201102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 202102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 2033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LinearScan(); 204a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 2052aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko inactive_.clear(); 2062aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko active_.clear(); 2072aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko handled_.clear(); 208a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 2093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters(); 2105233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_, 2115233f93ee336b3581ccdb993ff6342c52fec34b0Vladimir Marko kArenaAllocRegisterAllocator); 2123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray processing_core_registers_ = false; 2133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray unhandled_ = &unhandled_fp_intervals_; 2142aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* fixed : physical_fp_register_intervals_) { 215102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (fixed != nullptr) { 216296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Fixed interval is added to inactive_ instead of unhandled_. 217296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // It's also the only type of inactive interval whose start position 218296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // can be after the current interval during linear scan. 219296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Fixed interval is never split and never moves to unhandled_. 2202aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko inactive_.push_back(fixed); 221102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 222102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 2233946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LinearScan(); 2243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray} 22586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 2263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::ProcessInstruction(HInstruction* instruction) { 2273946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LocationSummary* locations = instruction->GetLocations(); 2283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray size_t position = instruction->GetLifetimePosition(); 2293946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 2303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (locations == nullptr) return; 2313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 2323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Create synthesized intervals for temporaries. 2333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t i = 0; i < locations->GetTempCount(); ++i) { 2343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray Location temp = locations->GetTemp(i); 23552839d17c06175e19ca4a093fb878450d1c4310dNicolas Geoffray if (temp.IsRegister() || temp.IsFpuRegister()) { 236102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray BlockRegister(temp, position, position + 1); 23745b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray // Ensure that an explicit temporary register is marked as being allocated. 23845b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray codegen_->AddAllocatedRegister(temp); 2393946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 240102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray DCHECK(temp.IsUnallocated()); 2415368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain switch (temp.GetPolicy()) { 2425368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain case Location::kRequiresRegister: { 2435368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain LiveInterval* interval = 2445368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt); 2452aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko temp_intervals_.push_back(interval); 246f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray interval->AddTempUse(instruction, i); 2472aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko unhandled_core_intervals_.push_back(interval); 2485368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain break; 2495368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain } 2505368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain 2515368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain case Location::kRequiresFpuRegister: { 2525368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain LiveInterval* interval = 2535368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimDouble); 2542aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko temp_intervals_.push_back(interval); 255f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray interval->AddTempUse(instruction, i); 256840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) { 2575588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray interval->AddHighInterval(/* is_temp */ true); 258840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray LiveInterval* high = interval->GetHighInterval(); 2592aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko temp_intervals_.push_back(high); 2602aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko unhandled_fp_intervals_.push_back(high); 261840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 2622aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko unhandled_fp_intervals_.push_back(interval); 2635368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain break; 2645368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain } 2655368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain 2665368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain default: 2675368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain LOG(FATAL) << "Unexpected policy for temporary location " 2685368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain << temp.GetPolicy(); 2695368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain } 270a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 271a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 27286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 2733bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray bool core_register = (instruction->GetType() != Primitive::kPrimDouble) 2743bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray && (instruction->GetType() != Primitive::kPrimFloat); 2753bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray 2768158f28b6689314213eb4dbbe14166073be71f7eAlexandre Rames if (locations->NeedsSafepoint()) { 277c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray if (codegen_->IsLeafMethod()) { 278c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray // TODO: We do this here because we do not want the suspend check to artificially 279c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray // create live registers. We should find another place, but this is currently the 280c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray // simplest. 281c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray DCHECK(instruction->IsSuspendCheckEntry()); 282c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray instruction->GetBlock()->RemoveInstruction(instruction); 283c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray return; 2843bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray } 2852aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko safepoints_.push_back(instruction); 2863bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray if (locations->OnlyCallsOnSlowPath()) { 2873bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // We add a synthesized range at this position to record the live registers 2883bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // at this position. Ideally, we could just update the safepoints when locations 2893bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // are updated, but we currently need to know the full stack size before updating 2903bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // locations (because of parameters and the fact that we don't have a frame pointer). 2913bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // And knowing the full stack size requires to know the maximum number of live 2923bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // registers at calls in slow paths. 2933bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // By adding the following interval in the algorithm, we can compute this 2943bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // maximum before updating locations. 2953bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction); 296acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray interval->AddRange(position, position + 1); 29787d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray AddSorted(&unhandled_core_intervals_, interval); 29887d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray AddSorted(&unhandled_fp_intervals_, interval); 2993bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray } 3003bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray } 3013bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray 3023bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray if (locations->WillCall()) { 30377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil BlockRegisters(position, position + 1, /* caller_save_only */ true); 3043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 3053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 3063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t i = 0; i < instruction->InputCount(); ++i) { 3073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray Location input = locations->InAt(i); 308102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (input.IsRegister() || input.IsFpuRegister()) { 309102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray BlockRegister(input, position, position + 1); 310840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else if (input.IsPair()) { 311840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray BlockRegister(input.ToLow(), position, position + 1); 312840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray BlockRegister(input.ToHigh(), position, position + 1); 3133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 3143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 3153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 3163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LiveInterval* current = instruction->GetLiveInterval(); 3173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (current == nullptr) return; 3183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 3192aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko ArenaVector<LiveInterval*>& unhandled = core_register 320102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray ? unhandled_core_intervals_ 321102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray : unhandled_fp_intervals_; 322102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray 3232aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK(unhandled.empty() || current->StartsBeforeOrAt(unhandled.back())); 32487d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray 325840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (codegen_->NeedsTwoRegisters(current->GetType())) { 326840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray current->AddHighInterval(); 327840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 328840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray 3292aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (size_t safepoint_index = safepoints_.size(); safepoint_index > 0; --safepoint_index) { 3302aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko HInstruction* safepoint = safepoints_[safepoint_index - 1u]; 3315588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray size_t safepoint_position = safepoint->GetLifetimePosition(); 3325588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray 3335588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray // Test that safepoints are ordered in the optimal way. 3342aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK(safepoint_index == safepoints_.size() || 3352aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko safepoints_[safepoint_index]->GetLifetimePosition() < safepoint_position); 3365588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray 3375588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray if (safepoint_position == current->GetStart()) { 3385588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray // The safepoint is for this instruction, so the location of the instruction 3395588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray // does not need to be saved. 3402aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK_EQ(safepoint_index, safepoints_.size()); 3415588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray DCHECK_EQ(safepoint, instruction); 3425588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray continue; 3435588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray } else if (current->IsDeadAt(safepoint_position)) { 3445588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray break; 3455588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray } else if (!current->Covers(safepoint_position)) { 3465588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray // Hole in the interval. 3475588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray continue; 3485588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray } 3495588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray current->AddSafepoint(safepoint); 3505588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray } 3513fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil current->ResetSearchCache(); 3525588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray 3533946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Some instructions define their output in fixed register/stack slot. We need 3543946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // to ensure we know these locations before doing register allocation. For a 3553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // given register, we create an interval that covers these locations. The register 3563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // will be unavailable at these locations when trying to allocate one for an 3573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // interval. 3583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // 3593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // The backwards walking ensures the ranges are ordered on increasing start positions. 3603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray Location output = locations->Out(); 361d0d4852847432368b090c184d6639e573538dccfCalin Juravle if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) { 362d0d4852847432368b090c184d6639e573538dccfCalin Juravle Location first = locations->InAt(0); 363d0d4852847432368b090c184d6639e573538dccfCalin Juravle if (first.IsRegister() || first.IsFpuRegister()) { 364d0d4852847432368b090c184d6639e573538dccfCalin Juravle current->SetFrom(position + 1); 365d0d4852847432368b090c184d6639e573538dccfCalin Juravle current->SetRegister(first.reg()); 366840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else if (first.IsPair()) { 367840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray current->SetFrom(position + 1); 368840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray current->SetRegister(first.low()); 369840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray LiveInterval* high = current->GetHighInterval(); 370840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray high->SetRegister(first.high()); 371840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray high->SetFrom(position + 1); 372d0d4852847432368b090c184d6639e573538dccfCalin Juravle } 373d0d4852847432368b090c184d6639e573538dccfCalin Juravle } else if (output.IsRegister() || output.IsFpuRegister()) { 3743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Shift the interval's start by one to account for the blocked register. 3753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray current->SetFrom(position + 1); 37656b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray current->SetRegister(output.reg()); 377102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray BlockRegister(output, position, position + 1); 378840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else if (output.IsPair()) { 379840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray current->SetFrom(position + 1); 380840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray current->SetRegister(output.low()); 381840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray LiveInterval* high = current->GetHighInterval(); 382840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray high->SetRegister(output.high()); 383840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray high->SetFrom(position + 1); 384840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray BlockRegister(output.ToLow(), position, position + 1); 385840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray BlockRegister(output.ToHigh(), position, position + 1); 3863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) { 3873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray current->SetSpillSlot(output.GetStackIndex()); 388840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else { 389840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray DCHECK(output.IsUnallocated() || output.IsConstant()); 3903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 3913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 39277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil if (instruction->IsPhi() && instruction->AsPhi()->IsCatchPhi()) { 39377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil AllocateSpillSlotForCatchPhi(instruction->AsPhi()); 39477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 39577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil 3963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // If needed, add interval to the list of unhandled intervals. 3973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (current->HasSpillSlot() || instruction->IsConstant()) { 398c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray // Split just before first register use. 3993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray size_t first_register_use = current->FirstRegisterUse(); 4003946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (first_register_use != kNoLifetime) { 4018cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1); 402b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray // Don't add directly to `unhandled`, it needs to be sorted and the start 4033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // of this new interval might be after intervals already in the list. 4043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray AddSorted(&unhandled, split); 4053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 4063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Nothing to do, we won't allocate a register for this value. 4073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 4083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 409b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray // Don't add directly to `unhandled`, temp or safepoint intervals 410b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray // for this instruction may have been added, and those can be 411b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray // processed first. 412b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray AddSorted(&unhandled, current); 4133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 414a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 415a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 41631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayclass AllRangesIterator : public ValueObject { 41731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray public: 41831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray explicit AllRangesIterator(LiveInterval* interval) 41931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray : current_interval_(interval), 42031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray current_range_(interval->GetFirstRange()) {} 42131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 42231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray bool Done() const { return current_interval_ == nullptr; } 42331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveRange* CurrentRange() const { return current_range_; } 42431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveInterval* CurrentInterval() const { return current_interval_; } 42531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 42631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray void Advance() { 42731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray current_range_ = current_range_->GetNext(); 42831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (current_range_ == nullptr) { 42931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray current_interval_ = current_interval_->GetNextSibling(); 43031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (current_interval_ != nullptr) { 43131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray current_range_ = current_interval_->GetFirstRange(); 43231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 43331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 43431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 43531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 43631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray private: 43731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveInterval* current_interval_; 43831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveRange* current_range_; 43931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 44031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray DISALLOW_COPY_AND_ASSIGN(AllRangesIterator); 44131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}; 44231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 44386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const { 44486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // To simplify unit testing, we eagerly create the array of intervals, and 44586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // call the helper method. 4462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko ArenaVector<LiveInterval*> intervals(allocator_->Adapter(kArenaAllocRegisterAllocator)); 44786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) { 44886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); 44986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) { 4502aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko intervals.push_back(instruction->GetLiveInterval()); 45186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 45286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 45386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 4542aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko const ArenaVector<LiveInterval*>* physical_register_intervals = processing_core_registers_ 4552aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko ? &physical_core_register_intervals_ 4562aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko : &physical_fp_register_intervals_; 4572aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* fixed : *physical_register_intervals) { 4582aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko if (fixed != nullptr) { 4592aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko intervals.push_back(fixed); 46086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 46186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 46286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 4632aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* temp : temp_intervals_) { 4643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (ShouldProcess(processing_core_registers_, temp)) { 4652aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko intervals.push_back(temp); 4663946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 4673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 4683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 469776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray return ValidateIntervals(intervals, GetNumberOfSpillSlots(), reserved_out_slots_, *codegen_, 4703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray allocator_, processing_core_registers_, log_fatal_on_failure); 47186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 47286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 4732aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Markobool RegisterAllocator::ValidateIntervals(const ArenaVector<LiveInterval*>& intervals, 47431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray size_t number_of_spill_slots, 4753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray size_t number_of_out_slots, 476a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray const CodeGenerator& codegen, 477a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray ArenaAllocator* allocator, 478a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray bool processing_core_registers, 479a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray bool log_fatal_on_failure) { 480a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t number_of_registers = processing_core_registers 481a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray ? codegen.GetNumberOfCoreRegisters() 482a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray : codegen.GetNumberOfFloatingPointRegisters(); 4832aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko ArenaVector<ArenaBitVector*> liveness_of_values( 4842aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko allocator->Adapter(kArenaAllocRegisterAllocator)); 4852aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko liveness_of_values.reserve(number_of_registers + number_of_spill_slots); 486a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 487a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Allocate a bit vector per register. A live interval that has a register 488a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // allocated will populate the associated bit vector based on its live ranges. 48931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) { 4902aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko liveness_of_values.push_back(new (allocator) ArenaBitVector(allocator, 0, true)); 491a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 492a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 4932aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* start_interval : intervals) { 4942aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (AllRangesIterator it(start_interval); !it.Done(); it.Advance()) { 49531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveInterval* current = it.CurrentInterval(); 49686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* defined_by = current->GetParent()->GetDefinedBy(); 49786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (current->GetParent()->HasSpillSlot() 49876b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray // Parameters and current method have their own stack slot. 49976b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray && !(defined_by != nullptr && (defined_by->IsParameterValue() 50076b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray || defined_by->IsCurrentMethod()))) { 5012aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko BitVector* liveness_of_spill_slot = liveness_of_values[number_of_registers 5023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray + current->GetParent()->GetSpillSlot() / kVRegSize 5032aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko - number_of_out_slots]; 50431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) { 50531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (liveness_of_spill_slot->IsBitSet(j)) { 50631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (log_fatal_on_failure) { 50731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray std::ostringstream message; 50831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray message << "Spill slot conflict at " << j; 50931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LOG(FATAL) << message.str(); 51031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } else { 51131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray return false; 51231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 51331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } else { 51431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray liveness_of_spill_slot->SetBit(j); 51531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 51631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 517a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 51831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 51931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (current->HasRegister()) { 52045b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray if (kIsDebugBuild && log_fatal_on_failure && !current->IsFixed()) { 52145b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray // Only check when an error is fatal. Only tests code ask for non-fatal failures 52245b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray // and test code may not properly fill the right information to the code generator. 52345b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray CHECK(codegen.HasAllocatedRegister(processing_core_registers, current->GetRegister())); 52445b83aff85a8a8dfcae0da90d010fa2d7eb299a7Nicolas Geoffray } 5252aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko BitVector* liveness_of_register = liveness_of_values[current->GetRegister()]; 52631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) { 52731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (liveness_of_register->IsBitSet(j)) { 528829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray if (current->IsUsingInputRegister() && current->CanUseInputRegister()) { 529829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray continue; 530829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray } 531a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (log_fatal_on_failure) { 532a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray std::ostringstream message; 5333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray message << "Register conflict at " << j << " "; 5343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (defined_by != nullptr) { 5353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray message << "(" << defined_by->DebugName() << ")"; 5363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 5373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray message << "for "; 538a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (processing_core_registers) { 539a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray codegen.DumpCoreRegister(message, current->GetRegister()); 540a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 541a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray codegen.DumpFloatingPointRegister(message, current->GetRegister()); 542a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 543a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LOG(FATAL) << message.str(); 544a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 545a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return false; 546a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 547a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 54831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray liveness_of_register->SetBit(j); 549a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 550a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 55131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 55231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 553a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 554a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return true; 555a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 556a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 55786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const { 558a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray interval->Dump(stream); 559a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray stream << ": "; 560a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (interval->HasRegister()) { 561102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (interval->IsFloatingPoint()) { 56286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray codegen_->DumpFloatingPointRegister(stream, interval->GetRegister()); 563102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } else { 564102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray codegen_->DumpCoreRegister(stream, interval->GetRegister()); 565a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 566a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 567a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray stream << "spilled"; 568a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 569a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray stream << std::endl; 570a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 571a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 572296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yangvoid RegisterAllocator::DumpAllIntervals(std::ostream& stream) const { 573296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang stream << "inactive: " << std::endl; 5742aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* inactive_interval : inactive_) { 5752aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DumpInterval(stream, inactive_interval); 576296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 577296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang stream << "active: " << std::endl; 5782aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* active_interval : active_) { 5792aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DumpInterval(stream, active_interval); 580296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 581296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang stream << "unhandled: " << std::endl; 582296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang auto unhandled = (unhandled_ != nullptr) ? 583296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang unhandled_ : &unhandled_core_intervals_; 5842aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* unhandled_interval : *unhandled) { 5852aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DumpInterval(stream, unhandled_interval); 586296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 587296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang stream << "handled: " << std::endl; 5882aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* handled_interval : handled_) { 5892aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DumpInterval(stream, handled_interval); 590296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 591296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang} 592296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang 593a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// By the book implementation of a linear scan register allocator. 594a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::LinearScan() { 5952aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko while (!unhandled_->empty()) { 596a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (1) Remove interval with the lowest start position from unhandled. 5972aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko LiveInterval* current = unhandled_->back(); 5982aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko unhandled_->pop_back(); 5992e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray 6002e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray // Make sure the interval is an expected state. 6013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray DCHECK(!current->IsFixed() && !current->HasSpillSlot()); 6022e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray // Make sure we are going in the right order. 6032aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK(unhandled_->empty() || unhandled_->back()->GetStart() >= current->GetStart()); 6042e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray // Make sure a low interval is always with a high. 6052aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK(!current->IsLowInterval() || unhandled_->back()->IsHighInterval()); 6062e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray // Make sure a high interval is always with a low. 6072e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray DCHECK(current->IsLowInterval() || 6082aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko unhandled_->empty() || 6092aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko !unhandled_->back()->IsHighInterval()); 61087d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray 611a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t position = current->GetStart(); 612a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 613296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Remember the inactive_ size here since the ones moved to inactive_ from 614296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // active_ below shouldn't need to be re-checked. 6152aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko size_t inactive_intervals_to_handle = inactive_.size(); 616296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang 617a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (2) Remove currently active intervals that are dead at this position. 618a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Move active intervals that have a lifetime hole at this position 619a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // to inactive. 620b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko auto active_kept_end = std::remove_if( 621b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko active_.begin(), 622b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko active_.end(), 623b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko [this, position](LiveInterval* interval) { 624b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko if (interval->IsDeadAt(position)) { 625b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko handled_.push_back(interval); 626b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko return true; 627b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko } else if (!interval->Covers(position)) { 628b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko inactive_.push_back(interval); 629b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko return true; 630b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko } else { 631b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko return false; // Keep this interval. 632b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko } 633b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko }); 6342aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko active_.erase(active_kept_end, active_.end()); 635a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 636a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (3) Remove currently inactive intervals that are dead at this position. 637a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Move inactive intervals that cover this position to active. 6382aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko auto inactive_to_handle_end = inactive_.begin() + inactive_intervals_to_handle; 639b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko auto inactive_kept_end = std::remove_if( 640b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko inactive_.begin(), 641b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko inactive_to_handle_end, 642b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko [this, position](LiveInterval* interval) { 643b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko DCHECK(interval->GetStart() < position || interval->IsFixed()); 644b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko if (interval->IsDeadAt(position)) { 645b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko handled_.push_back(interval); 646b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko return true; 647b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko } else if (interval->Covers(position)) { 648b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko active_.push_back(interval); 649b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko return true; 650b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko } else { 651b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko return false; // Keep this interval. 652b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko } 653b95fb775cc4c08349d0d905adbc96ad85e50601dVladimir Marko }); 6542aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko inactive_.erase(inactive_kept_end, inactive_to_handle_end); 655a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 656acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray if (current->IsSlowPathSafepoint()) { 657acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray // Synthesized interval to record the maximum number of live registers 658acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray // at safepoints. No need to allocate a register for it. 659f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell if (processing_core_registers_) { 660f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell maximum_number_of_live_core_registers_ = 6612aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko std::max(maximum_number_of_live_core_registers_, active_.size()); 662f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell } else { 663f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell maximum_number_of_live_fp_registers_ = 6642aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko std::max(maximum_number_of_live_fp_registers_, active_.size()); 665f85a9ca9859ad843dc03d3a2b600afbaf2e9bbddMark Mendell } 6662aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK(unhandled_->empty() || unhandled_->back()->GetStart() > current->GetStart()); 667acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray continue; 668acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray } 669acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray 670840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) { 671840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray DCHECK(!current->HasRegister()); 672840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray // Allocating the low part was unsucessful. The splitted interval for the high part 673840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray // will be handled next (it is in the `unhandled_` list). 674840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray continue; 675840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 676840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray 677a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (4) Try to find an available register. 678a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray bool success = TryAllocateFreeReg(current); 679a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 680a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (5) If no register could be found, we need to spill. 681a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (!success) { 682a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray success = AllocateBlockedReg(current); 683a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 684a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 685a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (6) If the interval had a register allocated, add it to the list of active 686a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // intervals. 687a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (success) { 688988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray codegen_->AddAllocatedRegister(processing_core_registers_ 689988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray ? Location::RegisterLocation(current->GetRegister()) 690988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray : Location::FpuRegisterLocation(current->GetRegister())); 6912aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko active_.push_back(current); 692840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) { 693840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister())); 694840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 695a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 696a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 697a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 698a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 699829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffraystatic void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) { 700829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray DCHECK(!interval->IsHighInterval()); 701829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // Note that the same instruction may occur multiple times in the input list, 702829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // so `free_until` may have changed already. 7033fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil // Since `position` is not the current scan position, we need to use CoversSlow. 704829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray if (interval->IsDeadAt(position)) { 705829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // Set the register to be free. Note that inactive intervals might later 706829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // update this. 707829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray free_until[interval->GetRegister()] = kMaxLifetimePosition; 708829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray if (interval->HasHighInterval()) { 709829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray DCHECK(interval->GetHighInterval()->IsDeadAt(position)); 710829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition; 711829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray } 7123fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil } else if (!interval->CoversSlow(position)) { 713829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // The interval becomes inactive at `defined_by`. We make its register 714829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // available only until the next use strictly after `defined_by`. 715829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray free_until[interval->GetRegister()] = interval->FirstUseAfter(position); 716829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray if (interval->HasHighInterval()) { 7173fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil DCHECK(!interval->GetHighInterval()->CoversSlow(position)); 718829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()]; 719829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray } 720829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray } 721829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray} 722829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray 723a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find a free register. If multiple are found, pick the register that 724a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// is free the longest. 725a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) { 726a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t* free_until = registers_array_; 727a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 728a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // First set all registers to be free. 729a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray for (size_t i = 0; i < number_of_registers_; ++i) { 730a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray free_until[i] = kMaxLifetimePosition; 731a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 732a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 733296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // For each active interval, set its register to not free. 7342aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* interval : active_) { 735296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK(interval->HasRegister()); 736296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang free_until[interval->GetRegister()] = 0; 737296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 738296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang 739829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // An interval that starts an instruction (that is, it is not split), may 740829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // re-use the registers used by the inputs of that instruciton, based on the 741829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // location summary. 742829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray HInstruction* defined_by = current->GetDefinedBy(); 743829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray if (defined_by != nullptr && !current->IsSplit()) { 744829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray LocationSummary* locations = defined_by->GetLocations(); 745829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) { 74694015b939060f5041d408d48717f22443e55b6adNicolas Geoffray for (size_t i = 0, e = defined_by->InputCount(); i < e; ++i) { 747829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // Take the last interval of the input. It is the location of that interval 748829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // that will be used at `defined_by`. 74994015b939060f5041d408d48717f22443e55b6adNicolas Geoffray LiveInterval* interval = defined_by->InputAt(i)->GetLiveInterval()->GetLastSibling(); 750829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // Note that interval may have not been processed yet. 751829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // TODO: Handle non-split intervals last in the work list. 75294015b939060f5041d408d48717f22443e55b6adNicolas Geoffray if (locations->InAt(i).IsValid() 75394015b939060f5041d408d48717f22443e55b6adNicolas Geoffray && interval->HasRegister() 75494015b939060f5041d408d48717f22443e55b6adNicolas Geoffray && interval->SameRegisterKind(*current)) { 755829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // The input must be live until the end of `defined_by`, to comply to 756829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // the linear scan algorithm. So we use `defined_by`'s end lifetime 757829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // position to check whether the input is dead or is inactive after 758829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray // `defined_by`. 7593fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil DCHECK(interval->CoversSlow(defined_by->GetLifetimePosition())); 760829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray size_t position = defined_by->GetLifetimePosition() + 1; 761829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray FreeIfNotCoverAt(interval, position, free_until); 762829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray } 763829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray } 764829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray } 765829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray } 766829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray 767a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // For each inactive interval, set its register to be free until 768a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // the next intersection with `current`. 7692aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* inactive : inactive_) { 770296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Temp/Slow-path-safepoint interval has no holes. 771296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint()); 772296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang if (!current->IsSplit() && !inactive->IsFixed()) { 773296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Neither current nor inactive are fixed. 774296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Thanks to SSA, a non-split interval starting in a hole of an 775296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // inactive interval should never intersect with that inactive interval. 776296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Only if it's not fixed though, because fixed intervals don't come from SSA. 777296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime); 778296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang continue; 779296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 780296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang 781a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(inactive->HasRegister()); 782296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang if (free_until[inactive->GetRegister()] == 0) { 783296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Already used by some active interval. No need to intersect. 784296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang continue; 785296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 786a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t next_intersection = inactive->FirstIntersectionWith(current); 787a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (next_intersection != kNoLifetime) { 788aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray free_until[inactive->GetRegister()] = 789aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray std::min(free_until[inactive->GetRegister()], next_intersection); 790a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 791a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 792a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 7936c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray int reg = kNoRegister; 7943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (current->HasRegister()) { 7953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Some instructions have a fixed register output. 7963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray reg = current->GetRegister(); 797840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (free_until[reg] == 0) { 798840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray DCHECK(current->IsHighInterval()); 799840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray // AllocateBlockedReg will spill the holder of the register. 800840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray return false; 801840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 8023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 803840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray DCHECK(!current->IsHighInterval()); 804fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray int hint = current->FindFirstRegisterHint(free_until, liveness_); 805f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray if ((hint != kNoRegister) 806f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray // For simplicity, if the hint we are getting for a pair cannot be used, 807f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray // we are just going to allocate a new pair. 808f29758111e71a7d14f3e52d78773561a5d59961fNicolas Geoffray && !(current->IsLowInterval() && IsBlocked(GetHighForLowRegister(hint)))) { 80901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray DCHECK(!IsBlocked(hint)); 81001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray reg = hint; 811840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else if (current->IsLowInterval()) { 8126c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray reg = FindAvailableRegisterPair(free_until, current->GetStart()); 81301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray } else { 8148826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray reg = FindAvailableRegister(free_until, current); 815a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 816a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 817a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 8186c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray DCHECK_NE(reg, kNoRegister); 819a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // If we could not find a register, we need to spill. 820840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (free_until[reg] == 0) { 821840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray return false; 822840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 823840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray 824234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray if (current->IsLowInterval()) { 825234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray // If the high register of this interval is not available, we need to spill. 826234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray int high_reg = current->GetHighInterval()->GetRegister(); 827234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray if (high_reg == kNoRegister) { 828234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray high_reg = GetHighForLowRegister(reg); 829234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray } 830234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray if (free_until[high_reg] == 0) { 831234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray return false; 832234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray } 833a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 834a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 835a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray current->SetRegister(reg); 836a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (!current->IsDeadAt(free_until[reg])) { 837a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // If the register is only available for a subset of live ranges 8388272688499c2232355db34d94057983fd436173dNicolas Geoffray // covered by `current`, split `current` before the position where 839a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // the register is not available anymore. 8408272688499c2232355db34d94057983fd436173dNicolas Geoffray LiveInterval* split = SplitBetween(current, current->GetStart(), free_until[reg]); 841a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(split != nullptr); 8423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray AddSorted(unhandled_, split); 843a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 844a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return true; 845a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 846a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 847a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::IsBlocked(int reg) const { 848102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray return processing_core_registers_ 849102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray ? blocked_core_registers_[reg] 850102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray : blocked_fp_registers_[reg]; 851a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 852a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 8536c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffrayint RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const { 8546c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray int reg = kNoRegister; 855840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray // Pick the register pair that is used the last. 856840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray for (size_t i = 0; i < number_of_registers_; ++i) { 857840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (IsBlocked(i)) continue; 858840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (!IsLowRegister(i)) continue; 859840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray int high_register = GetHighForLowRegister(i); 860840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (IsBlocked(high_register)) continue; 861840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray int existing_high_register = GetHighForLowRegister(reg); 8626c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray if ((reg == kNoRegister) || (next_use[i] >= next_use[reg] 863840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray && next_use[high_register] >= next_use[existing_high_register])) { 864840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray reg = i; 865840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (next_use[i] == kMaxLifetimePosition 866840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray && next_use[high_register] == kMaxLifetimePosition) { 867840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray break; 868840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 8696c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) { 8706c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray // If one of the current register is known to be unavailable, just unconditionally 8716c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray // try a new one. 8726c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray reg = i; 873840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 874840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 875840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray return reg; 876840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray} 877840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray 8788826f67ad53099021f6442364348fa66729288d7Nicolas Geoffraybool RegisterAllocator::IsCallerSaveRegister(int reg) const { 8798826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray return processing_core_registers_ 8808826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray ? !codegen_->IsCoreCalleeSaveRegister(reg) 8818826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray : !codegen_->IsFloatingPointCalleeSaveRegister(reg); 8828826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray} 8838826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray 8848826f67ad53099021f6442364348fa66729288d7Nicolas Geoffrayint RegisterAllocator::FindAvailableRegister(size_t* next_use, LiveInterval* current) const { 8858826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // We special case intervals that do not span a safepoint to try to find a caller-save 8868826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // register if one is available. We iterate from 0 to the number of registers, 8878826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // so if there are caller-save registers available at the end, we continue the iteration. 8888826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray bool prefers_caller_save = !current->HasWillCallSafepoint(); 8896c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray int reg = kNoRegister; 890840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray for (size_t i = 0; i < number_of_registers_; ++i) { 8918826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray if (IsBlocked(i)) { 8928826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // Register cannot be used. Continue. 8938826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray continue; 8948826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray } 8958826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray 8968826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // Best case: we found a register fully available. 8978826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray if (next_use[i] == kMaxLifetimePosition) { 8988826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray if (prefers_caller_save && !IsCallerSaveRegister(i)) { 8998826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // We can get shorter encodings on some platforms by using 9008826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // small register numbers. So only update the candidate if the previous 9018826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // one was not available for the whole method. 9028826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray if (reg == kNoRegister || next_use[reg] != kMaxLifetimePosition) { 9038826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray reg = i; 9048826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray } 9058826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // Continue the iteration in the hope of finding a caller save register. 9068826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray continue; 9078826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray } else { 9088826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray reg = i; 9098826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // We know the register is good enough. Return it. 9108826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray break; 9118826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray } 9128826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray } 9138826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray 9148826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // If we had no register before, take this one as a reference. 9158826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray if (reg == kNoRegister) { 916840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray reg = i; 9178826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray continue; 9188826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray } 9198826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray 9208826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray // Pick the register that is used the last. 9218826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray if (next_use[i] > next_use[reg]) { 9228826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray reg = i; 9238826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray continue; 924840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 925840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 926840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray return reg; 927840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray} 928840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray 9292aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko// Remove interval and its other half if any. Return iterator to the following element. 9302aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Markostatic ArenaVector<LiveInterval*>::iterator RemoveIntervalAndPotentialOtherHalf( 9312aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko ArenaVector<LiveInterval*>* intervals, ArenaVector<LiveInterval*>::iterator pos) { 9322aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK(intervals->begin() <= pos && pos < intervals->end()); 9332aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko LiveInterval* interval = *pos; 9342aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko if (interval->IsLowInterval()) { 9352aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK(pos + 1 < intervals->end()); 9362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK_EQ(*(pos + 1), interval->GetHighInterval()); 9372aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko return intervals->erase(pos, pos + 2); 9382aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko } else if (interval->IsHighInterval()) { 9392aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK(intervals->begin() < pos); 9402aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK_EQ(*(pos - 1), interval->GetLowInterval()); 9412aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko return intervals->erase(pos - 1, pos + 1); 9422aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko } else { 9432aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko return intervals->erase(pos); 9442aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko } 9452aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko} 9462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko 947234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffraybool RegisterAllocator::TrySplitNonPairOrUnalignedPairIntervalAt(size_t position, 948234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray size_t first_register_use, 949234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray size_t* next_use) { 9502aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (auto it = active_.begin(), end = active_.end(); it != end; ++it) { 9512aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko LiveInterval* active = *it; 9526c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray DCHECK(active->HasRegister()); 953234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray if (active->IsFixed()) continue; 954234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray if (active->IsHighInterval()) continue; 955234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray if (first_register_use > next_use[active->GetRegister()]) continue; 956234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray 9572e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray // Split the first interval found that is either: 9582e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray // 1) A non-pair interval. 9592e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray // 2) A pair interval whose high is not low + 1. 9602e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray // 3) A pair interval whose low is not even. 9612e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray if (!active->IsLowInterval() || 9622e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray IsLowOfUnalignedPairInterval(active) || 9632e92bc2ba2446525a07f5172d1cd30ab49d26cd6Nicolas Geoffray !IsLowRegister(active->GetRegister())) { 9646c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray LiveInterval* split = Split(active, position); 9656c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray if (split != active) { 9662aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko handled_.push_back(active); 9676c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray } 9682aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko RemoveIntervalAndPotentialOtherHalf(&active_, it); 9696c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray AddSorted(unhandled_, split); 9706c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray return true; 9716c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray } 9726c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray } 9736c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray return false; 9746c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray} 9756c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray 976a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval 977a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register 978a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead. 979a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) { 980a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t first_register_use = current->FirstRegisterUse(); 981da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray if (current->HasRegister()) { 982da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray DCHECK(current->IsHighInterval()); 983da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray // The low interval has allocated the register for the high interval. In 984da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray // case the low interval had to split both intervals, we may end up in a 985da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray // situation where the high interval does not have a register use anymore. 986da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray // We must still proceed in order to split currently active and inactive 987da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray // uses of the high interval's register, and put the high interval in the 988da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray // active set. 989da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray DCHECK(first_register_use != kNoLifetime || (current->GetNextSibling() != nullptr)); 990da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray } else if (first_register_use == kNoLifetime) { 99131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray AllocateSpillSlotFor(current); 992a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return false; 993a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 994a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 9951ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray // We use the first use to compare with other intervals. If this interval 9961ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray // is used after any active intervals, we will spill this interval. 9971ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray size_t first_use = current->FirstUseAfter(current->GetStart()); 9981ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray 999a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // First set all registers as not being used. 1000a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t* next_use = registers_array_; 1001a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray for (size_t i = 0; i < number_of_registers_; ++i) { 1002a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray next_use[i] = kMaxLifetimePosition; 1003a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1004a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 1005a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // For each active interval, find the next use of its register after the 1006a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // start of current. 10072aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* active : active_) { 1008a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(active->HasRegister()); 100986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (active->IsFixed()) { 101086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray next_use[active->GetRegister()] = current->GetStart(); 101186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 10121ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray size_t use = active->FirstUseAfter(current->GetStart()); 101386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (use != kNoLifetime) { 101486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray next_use[active->GetRegister()] = use; 101586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 1016a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1017a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1018a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 1019a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // For each inactive interval, find the next use of its register after the 1020a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // start of current. 10212aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* inactive : inactive_) { 1022296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Temp/Slow-path-safepoint interval has no holes. 1023296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint()); 1024296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang if (!current->IsSplit() && !inactive->IsFixed()) { 1025296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Neither current nor inactive are fixed. 1026296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Thanks to SSA, a non-split interval starting in a hole of an 1027296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // inactive interval should never intersect with that inactive interval. 1028296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Only if it's not fixed though, because fixed intervals don't come from SSA. 1029296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime); 1030296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang continue; 1031296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 1032a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(inactive->HasRegister()); 103386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray size_t next_intersection = inactive->FirstIntersectionWith(current); 103486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (next_intersection != kNoLifetime) { 103586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (inactive->IsFixed()) { 103686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray next_use[inactive->GetRegister()] = 103786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray std::min(next_intersection, next_use[inactive->GetRegister()]); 103886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 10391ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray size_t use = inactive->FirstUseAfter(current->GetStart()); 104086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (use != kNoLifetime) { 104186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]); 104286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 104386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 1044a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1045a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1046a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 10476c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray int reg = kNoRegister; 10486c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray bool should_spill = false; 1049840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (current->HasRegister()) { 1050840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray DCHECK(current->IsHighInterval()); 1051840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray reg = current->GetRegister(); 10526c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray // When allocating the low part, we made sure the high register was available. 10531ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray DCHECK_LT(first_use, next_use[reg]); 1054840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else if (current->IsLowInterval()) { 1055da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray reg = FindAvailableRegisterPair(next_use, first_use); 10566c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray // We should spill if both registers are not available. 10571ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray should_spill = (first_use >= next_use[reg]) 10581ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray || (first_use >= next_use[GetHighForLowRegister(reg)]); 1059840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else { 1060840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray DCHECK(!current->IsHighInterval()); 10618826f67ad53099021f6442364348fa66729288d7Nicolas Geoffray reg = FindAvailableRegister(next_use, current); 10621ba1981ee9d28f87f594b157566d09e973fa5bceNicolas Geoffray should_spill = (first_use >= next_use[reg]); 1063a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1064a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 10656c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray DCHECK_NE(reg, kNoRegister); 10666c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray if (should_spill) { 1067840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray DCHECK(!current->IsHighInterval()); 1068234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray bool is_allocation_at_use_site = (current->GetStart() >= (first_register_use - 1)); 1069da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray if (is_allocation_at_use_site) { 1070da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray if (!current->IsLowInterval()) { 1071da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray DumpInterval(std::cerr, current); 1072da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray DumpAllIntervals(std::cerr); 1073da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray // This situation has the potential to infinite loop, so we make it a non-debug CHECK. 1074da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray HInstruction* at = liveness_.GetInstructionFromPosition(first_register_use / 2); 1075da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray CHECK(false) << "There is not enough registers available for " 1076da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray << current->GetParent()->GetDefinedBy()->DebugName() << " " 1077da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray << current->GetParent()->GetDefinedBy()->GetId() 1078da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray << " at " << first_register_use - 1 << " " 1079da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray << (at == nullptr ? "" : at->DebugName()); 1080da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray } 1081da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray 10826c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray // If we're allocating a register for `current` because the instruction at 10836c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray // that position requires it, but we think we should spill, then there are 1084234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray // non-pair intervals or unaligned pair intervals blocking the allocation. 1085234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray // We split the first interval found, and put ourselves first in the 1086234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray // `unhandled_` list. 1087da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray bool success = TrySplitNonPairOrUnalignedPairIntervalAt(current->GetStart(), 1088da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray first_register_use, 1089da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray next_use); 1090da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray DCHECK(success); 10912aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko LiveInterval* existing = unhandled_->back(); 10926c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray DCHECK(existing->IsHighInterval()); 10936c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray DCHECK_EQ(existing->GetLowInterval(), current); 10942aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko unhandled_->push_back(current); 10956c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray } else { 10966c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray // If the first use of that instruction is after the last use of the found 10976c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray // register, we split this interval just before its first register use. 10986c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray AllocateSpillSlotFor(current); 10998cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray LiveInterval* split = SplitBetween(current, current->GetStart(), first_register_use - 1); 1100da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray DCHECK(current != split); 11016c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray AddSorted(unhandled_, split); 11026c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray } 1103a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return false; 1104a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 1105a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Use this register and spill the active and inactives interval that 1106a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // have that register. 1107a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray current->SetRegister(reg); 1108a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 11092aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (auto it = active_.begin(), end = active_.end(); it != end; ++it) { 11102aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko LiveInterval* active = *it; 1111a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (active->GetRegister() == reg) { 111286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(!active->IsFixed()); 1113a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* split = Split(active, current->GetStart()); 1114dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray if (split != active) { 11152aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko handled_.push_back(active); 1116dd8f887e81b894bc8075d8bacdb223747b6a8018Nicolas Geoffray } 11172aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko RemoveIntervalAndPotentialOtherHalf(&active_, it); 11183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray AddSorted(unhandled_, split); 1119a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray break; 1120a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1121a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1122a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 11232aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko // NOTE: Retrieve end() on each iteration because we're removing elements in the loop body. 11242aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (auto it = inactive_.begin(); it != inactive_.end(); ) { 11252aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko LiveInterval* inactive = *it; 11262aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko bool erased = false; 1127a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (inactive->GetRegister() == reg) { 1128296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang if (!current->IsSplit() && !inactive->IsFixed()) { 1129296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Neither current nor inactive are fixed. 1130296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Thanks to SSA, a non-split interval starting in a hole of an 1131296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // inactive interval should never intersect with that inactive interval. 1132296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Only if it's not fixed though, because fixed intervals don't come from SSA. 1133296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime); 11342aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko } else { 11352aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko size_t next_intersection = inactive->FirstIntersectionWith(current); 11362aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko if (next_intersection != kNoLifetime) { 11372aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko if (inactive->IsFixed()) { 11382aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko LiveInterval* split = Split(current, next_intersection); 11392aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK_NE(split, current); 11402aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko AddSorted(unhandled_, split); 11412aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko } else { 11422aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko // Split at the start of `current`, which will lead to splitting 11432aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko // at the end of the lifetime hole of `inactive`. 11442aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko LiveInterval* split = Split(inactive, current->GetStart()); 11452aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko // If it's inactive, it must start before the current interval. 11462aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK_NE(split, inactive); 11472aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko it = RemoveIntervalAndPotentialOtherHalf(&inactive_, it); 11482aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko erased = true; 11492aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko handled_.push_back(inactive); 11502aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko AddSorted(unhandled_, split); 11515b168deeae2c5a8a566ce5c140741f0e2227af21Nicolas Geoffray } 115286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 115386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 1154a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 11552aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko // If we have erased the element, `it` already points to the next element. 11562aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko // Otherwise we need to move to the next element. 11572aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko if (!erased) { 11582aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko ++it; 11592aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko } 1160a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1161a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 1162a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return true; 1163a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1164a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 1165a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 11662aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Markovoid RegisterAllocator::AddSorted(ArenaVector<LiveInterval*>* array, LiveInterval* interval) { 1167c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray DCHECK(!interval->IsFixed() && !interval->HasSpillSlot()); 116886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray size_t insert_at = 0; 11692aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (size_t i = array->size(); i > 0; --i) { 11702aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko LiveInterval* current = (*array)[i - 1u]; 11716c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray // High intervals must be processed right after their low equivalent. 11726c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray if (current->StartsAfter(interval) && !current->IsHighInterval()) { 117386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray insert_at = i; 1174a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray break; 1175acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) { 1176acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray // Ensure the slow path interval is the last to be processed at its location: we want the 1177acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray // interval to know all live registers at this location. 11782aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko DCHECK(i == 1 || (*array)[i - 2u]->StartsAfter(current)); 1179acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray insert_at = i; 1180acd033994aced8246c2fd8e931340dbf82d06d1aNicolas Geoffray break; 1181a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1182a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1183840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray 1184840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray // Insert the high interval before the low, to ensure the low is processed before. 11852aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko auto insert_pos = array->begin() + insert_at; 1186840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (interval->HasHighInterval()) { 11872aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko array->insert(insert_pos, { interval->GetHighInterval(), interval }); 1188840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else if (interval->HasLowInterval()) { 11892aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko array->insert(insert_pos, { interval, interval->GetLowInterval() }); 11902aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko } else { 11912aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko array->insert(insert_pos, interval); 1192840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 1193a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 1194a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 11958cbab3c4de3328b576454ce702d7748f56c44346Nicolas GeoffrayLiveInterval* RegisterAllocator::SplitBetween(LiveInterval* interval, size_t from, size_t to) { 1196fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray HBasicBlock* block_from = liveness_.GetBlockFromPosition(from / 2); 1197fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray HBasicBlock* block_to = liveness_.GetBlockFromPosition(to / 2); 11988cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray DCHECK(block_from != nullptr); 11998cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray DCHECK(block_to != nullptr); 12008cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray 12018cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray // Both locations are in the same block. We split at the given location. 12028cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray if (block_from == block_to) { 12038cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray return Split(interval, to); 12048cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray } 12058cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray 1206fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray /* 1207fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * Non-linear control flow will force moves at every branch instruction to the new location. 1208fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * To avoid having all branches doing the moves, we find the next non-linear position and 1209fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * split the interval at this position. Take the following example (block number is the linear 1210fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * order position): 1211fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * 1212fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * B1 1213fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * / \ 1214fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * B2 B3 1215fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * \ / 1216fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * B4 1217fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * 1218fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * B2 needs to split an interval, whose next use is in B4. If we were to split at the 1219fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * beginning of B4, B3 would need to do a move between B3 and B4 to ensure the interval 1220fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * is now in the correct location. It makes performance worst if the interval is spilled 1221fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * and both B2 and B3 need to reload it before entering B4. 1222fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * 1223fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * By splitting at B3, we give a chance to the register allocator to allocate the 1224fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * interval to the same register as in B1, and therefore avoid doing any 1225fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray * moves in B3. 1226fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray */ 1227fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray if (block_from->GetDominator() != nullptr) { 12286058455d486219994921b63a2d774dc9908415a2Vladimir Marko for (HBasicBlock* dominated : block_from->GetDominator()->GetDominatedBlocks()) { 12296058455d486219994921b63a2d774dc9908415a2Vladimir Marko size_t position = dominated->GetLifetimeStart(); 1230fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray if ((position > from) && (block_to->GetLifetimeStart() > position)) { 1231fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray // Even if we found a better block, we continue iterating in case 1232fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray // a dominated block is closer. 1233fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray // Note that dominated blocks are not sorted in liveness order. 12346058455d486219994921b63a2d774dc9908415a2Vladimir Marko block_to = dominated; 1235fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray DCHECK_NE(block_to, block_from); 1236fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray } 1237fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray } 1238fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray } 1239fbda5f3e1378f07ae202f62da625ee43a063a052Nicolas Geoffray 12408cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray // If `to` is in a loop, find the outermost loop header which does not contain `from`. 12418cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray for (HLoopInformationOutwardIterator it(*block_to); !it.Done(); it.Advance()) { 12428cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray HBasicBlock* header = it.Current()->GetHeader(); 12438cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray if (block_from->GetLifetimeStart() >= header->GetLifetimeStart()) { 12448cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray break; 12458cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray } 12468cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray block_to = header; 12478cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray } 12488cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray 12498cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray // Split at the start of the found block, to piggy back on existing moves 12508cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray // due to resolution if non-linear control flow (see `ConnectSplitSiblings`). 12518cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray return Split(interval, block_to->GetLifetimeStart()); 12528cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray} 12538cbab3c4de3328b576454ce702d7748f56c44346Nicolas Geoffray 1254a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) { 1255840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray DCHECK_GE(position, interval->GetStart()); 1256a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(!interval->IsDeadAt(position)); 1257a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (position == interval->GetStart()) { 1258a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Spill slot will be allocated when handling `interval` again. 1259a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray interval->ClearRegister(); 1260840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (interval->HasHighInterval()) { 1261840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray interval->GetHighInterval()->ClearRegister(); 1262840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else if (interval->HasLowInterval()) { 1263840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray interval->GetLowInterval()->ClearRegister(); 1264840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 1265a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return interval; 1266a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 1267a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* new_interval = interval->SplitAt(position); 1268840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (interval->HasHighInterval()) { 1269840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray LiveInterval* high = interval->GetHighInterval()->SplitAt(position); 1270840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray new_interval->SetHighInterval(high); 1271840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray high->SetLowInterval(new_interval); 1272840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else if (interval->HasLowInterval()) { 1273840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray LiveInterval* low = interval->GetLowInterval()->SplitAt(position); 1274840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray new_interval->SetLowInterval(low); 1275840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray low->SetHighInterval(new_interval); 1276840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 1277a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return new_interval; 1278a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 1279a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 1280a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 128131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) { 1282840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (interval->IsHighInterval()) { 1283da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray // The low interval already took care of allocating the spill slot. 1284da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray DCHECK(!interval->GetLowInterval()->HasRegister()); 1285da2b254fe4c35986d85876c5819b1114e25140cbNicolas Geoffray DCHECK(interval->GetLowInterval()->GetParent()->HasSpillSlot()); 1286840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray return; 1287840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 1288840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray 128931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveInterval* parent = interval->GetParent(); 129031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 129131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray // An instruction gets a spill slot for its entire lifetime. If the parent 129231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray // of this interval already has a spill slot, there is nothing to do. 129331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (parent->HasSpillSlot()) { 129431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray return; 129531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 129631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 129786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* defined_by = parent->GetDefinedBy(); 129877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil DCHECK(!defined_by->IsPhi() || !defined_by->AsPhi()->IsCatchPhi()); 129977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil 130086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (defined_by->IsParameterValue()) { 130186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Parameters have their own stack slot. 130286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue())); 130386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return; 130486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 130586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 130676b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray if (defined_by->IsCurrentMethod()) { 130776b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray parent->SetSpillSlot(0); 130876b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray return; 130976b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray } 131076b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray 131196f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray if (defined_by->IsConstant()) { 131296f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray // Constants don't need a spill slot. 131396f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray return; 131496f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray } 131596f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray 13162aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko ArenaVector<size_t>* spill_slots = nullptr; 1317776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray switch (interval->GetType()) { 1318776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimDouble: 1319776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray spill_slots = &double_spill_slots_; 1320776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray break; 1321776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimLong: 1322776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray spill_slots = &long_spill_slots_; 1323776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray break; 1324776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimFloat: 1325776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray spill_slots = &float_spill_slots_; 1326776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray break; 1327776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimNot: 1328776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimInt: 1329776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimChar: 1330776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimByte: 1331776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimBoolean: 1332776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimShort: 1333776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray spill_slots = &int_spill_slots_; 1334776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray break; 1335776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimVoid: 1336776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray LOG(FATAL) << "Unexpected type for interval " << interval->GetType(); 1337776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray } 1338776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray 1339412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray // Find an available spill slot. 1340412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray size_t slot = 0; 13412aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (size_t e = spill_slots->size(); slot < e; ++slot) { 13422aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko if ((*spill_slots)[slot] <= parent->GetStart() 13432aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko && (slot == (e - 1) || (*spill_slots)[slot + 1] <= parent->GetStart())) { 1344412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray break; 1345412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray } 1346412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray } 1347412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray 134877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil size_t end = interval->GetLastSibling()->GetEnd(); 134901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray if (parent->NeedsTwoSpillSlots()) { 13502aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko if (slot + 2u > spill_slots->size()) { 13513c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray // We need a new spill slot. 13522aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko spill_slots->resize(slot + 2u, end); 135331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 13542aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko (*spill_slots)[slot] = end; 13552aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko (*spill_slots)[slot + 1] = end; 135631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } else { 13572aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko if (slot == spill_slots->size()) { 13583c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray // We need a new spill slot. 13592aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko spill_slots->push_back(end); 13603c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray } else { 13612aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko (*spill_slots)[slot] = end; 13623c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray } 136331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 136431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 1365776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray // Note that the exact spill slot location will be computed when we resolve, 1366776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray // that is when we know the number of spill slots for each type. 1367776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray parent->SetSpillSlot(slot); 136886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 136986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 13702a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffraystatic bool IsValidDestination(Location destination) { 1371102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray return destination.IsRegister() 13726c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray || destination.IsRegisterPair() 1373102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray || destination.IsFpuRegister() 1374840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray || destination.IsFpuRegisterPair() 1375102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray || destination.IsStackSlot() 1376102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray || destination.IsDoubleStackSlot(); 13772a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray} 13782a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray 137977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilvoid RegisterAllocator::AllocateSpillSlotForCatchPhi(HPhi* phi) { 138077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil LiveInterval* interval = phi->GetLiveInterval(); 138177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil 138277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil HInstruction* previous_phi = phi->GetPrevious(); 138377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil DCHECK(previous_phi == nullptr || 138477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil previous_phi->AsPhi()->GetRegNumber() <= phi->GetRegNumber()) 138577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil << "Phis expected to be sorted by vreg number, so that equivalent phis are adjacent."; 138677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil 138777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil if (phi->IsVRegEquivalentOf(previous_phi)) { 138877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // This is an equivalent of the previous phi. We need to assign the same 138977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // catch phi slot. 139077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil DCHECK(previous_phi->GetLiveInterval()->HasSpillSlot()); 139177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil interval->SetSpillSlot(previous_phi->GetLiveInterval()->GetSpillSlot()); 139277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } else { 139377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // Allocate a new spill slot for this catch phi. 139477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // TODO: Reuse spill slots when intervals of phis from different catch 139577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // blocks do not overlap. 139677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil interval->SetSpillSlot(catch_phi_spill_slots_); 139777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil catch_phi_spill_slots_ += interval->NeedsTwoSpillSlots() ? 2 : 1; 139877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 139977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil} 140077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil 1401234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddMove(HParallelMove* move, 1402234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray Location source, 1403234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray Location destination, 1404234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray HInstruction* instruction, 1405234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray Primitive::Type type) const { 1406234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray if (type == Primitive::kPrimLong 1407234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray && codegen_->ShouldSplitLongMoves() 1408234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray // The parallel move resolver knows how to deal with long constants. 1409234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray && !source.IsConstant()) { 14109021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray move->AddMove(source.ToLow(), destination.ToLow(), Primitive::kPrimInt, instruction); 14119021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray move->AddMove(source.ToHigh(), destination.ToHigh(), Primitive::kPrimInt, nullptr); 1412234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray } else { 14139021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray move->AddMove(source, destination, type, instruction); 1414234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray } 1415234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray} 1416234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray 1417234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* input, 1418234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray HInstruction* user, 141986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location source, 142086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location destination) const { 142186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (source.Equals(destination)) return; 142286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 1423476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain DCHECK(!user->IsPhi()); 142486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 1425740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray HInstruction* previous = user->GetPrevious(); 142686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HParallelMove* move = nullptr; 142786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (previous == nullptr 1428476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain || !previous->IsParallelMove() 14298e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray || previous->GetLifetimePosition() < user->GetLifetimePosition()) { 143086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 14318e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray move->SetLifetimePosition(user->GetLifetimePosition()); 1432740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray user->GetBlock()->InsertInstructionBefore(move, user); 143386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 143486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = previous->AsParallelMove(); 143586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 14368e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition()); 1437234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray AddMove(move, source, destination, nullptr, input->GetType()); 143886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 143986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 144046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionStart(size_t position) { 144146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray return (position & 1) == 0; 144246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray} 144346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray 144446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionEnd(size_t position) { 144546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray return (position & 1) == 1; 144646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray} 144746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray 144886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position, 1449740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray HInstruction* instruction, 145086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location source, 145186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location destination) const { 14526c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray DCHECK(IsValidDestination(destination)) << destination; 145386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (source.Equals(destination)) return; 145486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 145586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* at = liveness_.GetInstructionFromPosition(position / 2); 145686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HParallelMove* move; 145746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray if (at == nullptr) { 145846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray if (IsInstructionStart(position)) { 145946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray // Block boundary, don't do anything the connection of split siblings will handle it. 146046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray return; 146146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray } else { 146246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray // Move must happen before the first instruction of the block. 146346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray at = liveness_.GetInstructionFromPosition((position + 1) / 2); 14645976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray // Note that parallel moves may have already been inserted, so we explicitly 14655976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray // ask for the first instruction of the block: `GetInstructionFromPosition` does 1466234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray // not contain the `HParallelMove` instructions. 14675976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray at = at->GetBlock()->GetFirstInstruction(); 1468234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray 1469234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray if (at->GetLifetimePosition() < position) { 1470234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray // We may insert moves for split siblings and phi spills at the beginning of the block. 1471234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray // Since this is a different lifetime position, we need to go to the next instruction. 1472234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray DCHECK(at->IsParallelMove()); 1473234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray at = at->GetNext(); 1474234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray } 1475234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray 14765976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray if (at->GetLifetimePosition() != position) { 14775976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray DCHECK_GT(at->GetLifetimePosition(), position); 147846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 147946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray move->SetLifetimePosition(position); 148046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray at->GetBlock()->InsertInstructionBefore(move, at); 14815976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray } else { 14825976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray DCHECK(at->IsParallelMove()); 14835976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray move = at->AsParallelMove(); 148446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray } 148546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray } 148646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray } else if (IsInstructionEnd(position)) { 148786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Move must happen after the instruction. 148886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(!at->IsControlFlow()); 148986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = at->GetNext()->AsParallelMove(); 1490e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // This is a parallel move for connecting siblings in a same block. We need to 1491e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // differentiate it with moves for connecting blocks, and input moves. 14928e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray if (move == nullptr || move->GetLifetimePosition() > position) { 149386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 149486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move->SetLifetimePosition(position); 149586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray at->GetBlock()->InsertInstructionBefore(move, at->GetNext()); 149686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 149786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 149886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Move must happen before the instruction. 149986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* previous = at->GetPrevious(); 1500740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray if (previous == nullptr 1501740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray || !previous->IsParallelMove() 1502740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray || previous->GetLifetimePosition() != position) { 1503740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray // If the previous is a parallel move, then its position must be lower 1504740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray // than the given `position`: it was added just after the non-parallel 1505740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray // move instruction that precedes `instruction`. 1506740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray DCHECK(previous == nullptr 1507740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray || !previous->IsParallelMove() 1508740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray || previous->GetLifetimePosition() < position); 150986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 151086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move->SetLifetimePosition(position); 151186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray at->GetBlock()->InsertInstructionBefore(move, at); 151286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 151386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = previous->AsParallelMove(); 151486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 151586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 151601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray DCHECK_EQ(move->GetLifetimePosition(), position); 1517234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray AddMove(move, source, destination, instruction, instruction->GetType()); 151886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 151986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 152086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block, 1521740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray HInstruction* instruction, 152286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location source, 152386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location destination) const { 15246c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray DCHECK(IsValidDestination(destination)) << destination; 152586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (source.Equals(destination)) return; 152686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 152777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil DCHECK_EQ(block->NumberOfNormalSuccessors(), 1u); 152886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* last = block->GetLastInstruction(); 1529360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray // We insert moves at exit for phi predecessors and connecting blocks. 1530fe57faa2e0349418dda38e77ef1c0ac29db75f4dMark Mendell // A block ending with an if or a packed switch cannot branch to a block 1531fe57faa2e0349418dda38e77ef1c0ac29db75f4dMark Mendell // with phis because we do not allow critical edges. It can also not connect 1532360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray // a split interval between two blocks: the move has to happen in the successor. 1533fe57faa2e0349418dda38e77ef1c0ac29db75f4dMark Mendell DCHECK(!last->IsIf() && !last->IsPackedSwitch()); 153486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* previous = last->GetPrevious(); 153586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HParallelMove* move; 1536e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // This is a parallel move for connecting blocks. We need to differentiate 1537e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // it with moves for connecting siblings in a same block, and output moves. 15385976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray size_t position = last->GetLifetimePosition(); 1539740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray if (previous == nullptr || !previous->IsParallelMove() 15405976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray || previous->AsParallelMove()->GetLifetimePosition() != position) { 154186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 15425976857e65d3d0e7be0c4e3183e9483c85a76bb8Nicolas Geoffray move->SetLifetimePosition(position); 154386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray block->InsertInstructionBefore(move, last); 154486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 154586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = previous->AsParallelMove(); 154686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 1547234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray AddMove(move, source, destination, instruction, instruction->GetType()); 154886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 154986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 155086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block, 1551740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray HInstruction* instruction, 155286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location source, 155386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location destination) const { 15546c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray DCHECK(IsValidDestination(destination)) << destination; 155586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (source.Equals(destination)) return; 155686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 155786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* first = block->GetFirstInstruction(); 155886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HParallelMove* move = first->AsParallelMove(); 1559234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray size_t position = block->GetLifetimeStart(); 1560e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // This is a parallel move for connecting blocks. We need to differentiate 1561e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // it with moves for connecting siblings in a same block, and input moves. 1562234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray if (move == nullptr || move->GetLifetimePosition() != position) { 156386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 1564234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray move->SetLifetimePosition(position); 156586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray block->InsertInstructionBefore(move, first); 156686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 1567234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray AddMove(move, source, destination, instruction, instruction->GetType()); 156886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 156986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 157086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction, 157186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location source, 157286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location destination) const { 15736c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray DCHECK(IsValidDestination(destination)) << destination; 157486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (source.Equals(destination)) return; 157586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 1576476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain if (instruction->IsPhi()) { 1577740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination); 157886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return; 157986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 158086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 1581e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray size_t position = instruction->GetLifetimePosition() + 1; 158286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HParallelMove* move = instruction->GetNext()->AsParallelMove(); 1583e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // This is a parallel move for moving the output of an instruction. We need 1584e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // to differentiate with input moves, moves for connecting siblings in a 1585e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // and moves for connecting blocks. 1586e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray if (move == nullptr || move->GetLifetimePosition() != position) { 158786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 1588e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray move->SetLifetimePosition(position); 158986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext()); 159086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 1591234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray AddMove(move, source, destination, instruction, instruction->GetType()); 159286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 159386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 159486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) { 159586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* current = interval; 159676b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray if (current->HasSpillSlot() 159776b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray && current->HasRegister() 159876b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray // Currently, we spill unconditionnally the current method in the code generators. 159976b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray && !interval->GetDefinedBy()->IsCurrentMethod()) { 160086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // We spill eagerly, so move must be at definition. 160186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray InsertMoveAfter(interval->GetDefinedBy(), 1602840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray interval->ToLocation(), 160301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray interval->NeedsTwoSpillSlots() 1604412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot()) 1605412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray : Location::StackSlot(interval->GetParent()->GetSpillSlot())); 160686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 160786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray UsePosition* use = current->GetFirstUse(); 16084ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray UsePosition* env_use = current->GetFirstEnvironmentUse(); 160986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 161086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Walk over all siblings, updating locations of use positions, and 161186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // connecting them when they are adjacent. 161286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray do { 161301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray Location source = current->ToLocation(); 161486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 161586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Walk over all uses covered by this interval, and update the location 161686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // information. 1617d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray 1618d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray LiveRange* range = current->GetFirstRange(); 1619d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray while (range != nullptr) { 1620579026039080252878106118645ed70706f4838eNicolas Geoffray while (use != nullptr && use->GetPosition() < range->GetStart()) { 1621579026039080252878106118645ed70706f4838eNicolas Geoffray DCHECK(use->IsSynthesized()); 1622579026039080252878106118645ed70706f4838eNicolas Geoffray use = use->GetNext(); 1623579026039080252878106118645ed70706f4838eNicolas Geoffray } 1624d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray while (use != nullptr && use->GetPosition() <= range->GetEnd()) { 16254ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray DCHECK(!use->GetIsEnvironment()); 16263fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil DCHECK(current->CoversSlow(use->GetPosition()) || (use->GetPosition() == range->GetEnd())); 1627579026039080252878106118645ed70706f4838eNicolas Geoffray if (!use->IsSynthesized()) { 1628579026039080252878106118645ed70706f4838eNicolas Geoffray LocationSummary* locations = use->GetUser()->GetLocations(); 1629579026039080252878106118645ed70706f4838eNicolas Geoffray Location expected_location = locations->InAt(use->GetInputIndex()); 1630579026039080252878106118645ed70706f4838eNicolas Geoffray // The expected (actual) location may be invalid in case the input is unused. Currently 1631579026039080252878106118645ed70706f4838eNicolas Geoffray // this only happens for intrinsics. 1632579026039080252878106118645ed70706f4838eNicolas Geoffray if (expected_location.IsValid()) { 1633579026039080252878106118645ed70706f4838eNicolas Geoffray if (expected_location.IsUnallocated()) { 1634579026039080252878106118645ed70706f4838eNicolas Geoffray locations->SetInAt(use->GetInputIndex(), source); 1635579026039080252878106118645ed70706f4838eNicolas Geoffray } else if (!expected_location.IsConstant()) { 1636579026039080252878106118645ed70706f4838eNicolas Geoffray AddInputMoveFor(interval->GetDefinedBy(), use->GetUser(), source, expected_location); 1637579026039080252878106118645ed70706f4838eNicolas Geoffray } 1638579026039080252878106118645ed70706f4838eNicolas Geoffray } else { 1639579026039080252878106118645ed70706f4838eNicolas Geoffray DCHECK(use->GetUser()->IsInvoke()); 1640579026039080252878106118645ed70706f4838eNicolas Geoffray DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone); 1641d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray } 164286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 1643d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray use = use->GetNext(); 164486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 16454ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray 16464ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray // Walk over the environment uses, and update their locations. 16474ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray while (env_use != nullptr && env_use->GetPosition() < range->GetStart()) { 16484ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray env_use = env_use->GetNext(); 16494ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray } 16504ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray 16514ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray while (env_use != nullptr && env_use->GetPosition() <= range->GetEnd()) { 16520a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray DCHECK(current->CoversSlow(env_use->GetPosition()) 16530a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray || (env_use->GetPosition() == range->GetEnd())); 1654d23eeef3492b53102eb8093524cf37e2b4c296dbNicolas Geoffray HEnvironment* environment = env_use->GetEnvironment(); 16550a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray environment->SetLocationAt(env_use->GetInputIndex(), source); 16564ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray env_use = env_use->GetNext(); 16574ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray } 16584ed947a58de87d19d0609be773207c905ccb0f7fNicolas Geoffray 1659d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray range = range->GetNext(); 166086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 166186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 166286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // If the next interval starts just after this one, and has a register, 166386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // insert a move. 166486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* next_sibling = current->GetNextSibling(); 166586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (next_sibling != nullptr 166686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray && next_sibling->HasRegister() 166786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray && current->GetEnd() == next_sibling->GetStart()) { 166801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray Location destination = next_sibling->ToLocation(); 1669740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination); 167086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 16713946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 167243af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray for (SafepointPosition* safepoint_position = current->GetFirstSafepoint(); 167343af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray safepoint_position != nullptr; 167443af728a3ccecb5f0eacef85f44d70df3d4c40f9Nicolas Geoffray safepoint_position = safepoint_position->GetNext()) { 16753fc992f9dfe8f49ff350132323cc635f102b7b62David Brazdil DCHECK(current->CoversSlow(safepoint_position->GetPosition())); 16763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 16775588e588144fffc978845a2c9c915a0044565a03Nicolas Geoffray LocationSummary* locations = safepoint_position->GetLocations(); 16783bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) { 16793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize); 16803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 16813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 16823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray switch (source.GetKind()) { 16833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray case Location::kRegister: { 16843bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray locations->AddLiveRegister(source); 1685988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) { 1686988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray DCHECK_LE(locations->GetNumberOfLiveRegisters(), 1687988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray maximum_number_of_live_core_registers_ + 1688988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray maximum_number_of_live_fp_registers_); 1689988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray } 16903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (current->GetType() == Primitive::kPrimNot) { 169156b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray locations->SetRegisterBit(source.reg()); 16923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 16933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray break; 16943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 1695102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray case Location::kFpuRegister: { 1696102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray locations->AddLiveRegister(source); 1697102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray break; 1698102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 16996c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray 17006c2dff8ff8e1440fa4d9e1b2ba2a44d036882801Nicolas Geoffray case Location::kRegisterPair: 1701840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray case Location::kFpuRegisterPair: { 1702840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray locations->AddLiveRegister(source.ToLow()); 1703840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray locations->AddLiveRegister(source.ToHigh()); 1704840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray break; 1705840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 17063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray case Location::kStackSlot: // Fall-through 17073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray case Location::kDoubleStackSlot: // Fall-through 17083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray case Location::kConstant: { 17093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Nothing to do. 17103946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray break; 17113946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 17123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray default: { 17133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LOG(FATAL) << "Unexpected location for object"; 17143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 17153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 17163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 171786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray current = next_sibling; 171886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } while (current != nullptr); 1719d8126bef62df7f40f2e6abc74004f52e664daf45Nicolas Geoffray 1720579026039080252878106118645ed70706f4838eNicolas Geoffray if (kIsDebugBuild) { 1721579026039080252878106118645ed70706f4838eNicolas Geoffray // Following uses can only be synthesized uses. 1722579026039080252878106118645ed70706f4838eNicolas Geoffray while (use != nullptr) { 1723579026039080252878106118645ed70706f4838eNicolas Geoffray DCHECK(use->IsSynthesized()); 1724579026039080252878106118645ed70706f4838eNicolas Geoffray use = use->GetNext(); 1725579026039080252878106118645ed70706f4838eNicolas Geoffray } 1726579026039080252878106118645ed70706f4838eNicolas Geoffray } 172786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 172886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 172986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval, 173086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HBasicBlock* from, 173186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HBasicBlock* to) const { 173286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (interval->GetNextSibling() == nullptr) { 173386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Nothing to connect. The whole range was allocated to the same location. 173486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return; 173586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 173686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 1737241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil // Find the intervals that cover `from` and `to`. 1738241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil LiveInterval* destination = interval->GetSiblingAt(to->GetLifetimeStart()); 1739241a486267bdb59b32fe4c8db370eb936068fb39David Brazdil LiveInterval* source = interval->GetSiblingAt(from->GetLifetimeEnd() - 1); 174086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 174186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (destination == source) { 174286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Interval was not split. 174386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return; 174486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 17458ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray DCHECK(destination != nullptr && source != nullptr); 17468ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray 174786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (!destination->HasRegister()) { 174886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Values are eagerly spilled. Spill slot already contains appropriate value. 174986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return; 175086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 175186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 175286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // If `from` has only one successor, we can put the moves at the exit of it. Otherwise 175386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // we need to put the moves at the entry of `to`. 175477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil if (from->NumberOfNormalSuccessors() == 1) { 1755740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray InsertParallelMoveAtExitOf(from, 1756740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray interval->GetParent()->GetDefinedBy(), 175701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray source->ToLocation(), 175801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray destination->ToLocation()); 175986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 17606058455d486219994921b63a2d774dc9908415a2Vladimir Marko DCHECK_EQ(to->GetPredecessors().size(), 1u); 1761740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray InsertParallelMoveAtEntryOf(to, 1762740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray interval->GetParent()->GetDefinedBy(), 176301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray source->ToLocation(), 176401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray destination->ToLocation()); 176586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 176686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 176786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 176886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() { 1769776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(), 17704c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray maximum_number_of_live_core_registers_, 17714c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray maximum_number_of_live_fp_registers_, 17724c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray reserved_out_slots_, 17730d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray codegen_->GetGraph()->GetLinearOrder()); 177486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 177586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Adjust the Out Location of instructions. 177686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration. 177786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) { 177886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); 177986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* current = instruction->GetLiveInterval(); 178086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LocationSummary* locations = instruction->GetLocations(); 178186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location location = locations->Out(); 1782476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain if (instruction->IsParameterValue()) { 178386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Now that we know the frame size, adjust the parameter's location. 178486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (location.IsStackSlot()) { 178586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); 178686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray current->SetSpillSlot(location.GetStackIndex()); 1787f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray locations->UpdateOut(location); 178886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else if (location.IsDoubleStackSlot()) { 178986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); 179086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray current->SetSpillSlot(location.GetStackIndex()); 1791f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray locations->UpdateOut(location); 179286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else if (current->HasSpillSlot()) { 179386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize()); 179486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 179576b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray } else if (instruction->IsCurrentMethod()) { 179676b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray // The current method is always at offset 0. 179776b1e1799a713a19218de26b171b0aef48a59e98Nicolas Geoffray DCHECK(!current->HasSpillSlot() || (current->GetSpillSlot() == 0)); 179877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } else if (instruction->IsPhi() && instruction->AsPhi()->IsCatchPhi()) { 179977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil DCHECK(current->HasSpillSlot()); 180077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil size_t slot = current->GetSpillSlot() 180177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil + GetNumberOfSpillSlots() 180277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil + reserved_out_slots_ 180377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil - catch_phi_spill_slots_; 180477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil current->SetSpillSlot(slot * kVRegSize); 1805776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray } else if (current->HasSpillSlot()) { 1806776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray // Adjust the stack slot, now that we know the number of them for each type. 1807776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray // The way this implementation lays out the stack is the following: 180877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // [parameter slots ] 180977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // [catch phi spill slots ] 181077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // [double spill slots ] 181177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // [long spill slots ] 181277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // [float spill slots ] 181377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // [int/ref values ] 181477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // [maximum out values ] (number of arguments for calls) 181577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // [art method ]. 181677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil size_t slot = current->GetSpillSlot(); 1817776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray switch (current->GetType()) { 1818776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimDouble: 18192aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko slot += long_spill_slots_.size(); 1820776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray FALLTHROUGH_INTENDED; 1821776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimLong: 18222aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko slot += float_spill_slots_.size(); 1823776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray FALLTHROUGH_INTENDED; 1824776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimFloat: 18252aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko slot += int_spill_slots_.size(); 1826776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray FALLTHROUGH_INTENDED; 1827776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimNot: 1828776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimInt: 1829776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimChar: 1830776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimByte: 1831776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimBoolean: 1832776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimShort: 1833776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray slot += reserved_out_slots_; 1834776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray break; 1835776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray case Primitive::kPrimVoid: 1836776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray LOG(FATAL) << "Unexpected type for interval " << current->GetType(); 1837776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray } 1838776b3184ee04092b11edc781cdb81e8ed60601e3Nicolas Geoffray current->SetSpillSlot(slot * kVRegSize); 183986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 184086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 184101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray Location source = current->ToLocation(); 184286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 184386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (location.IsUnallocated()) { 184486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (location.GetPolicy() == Location::kSameAsFirstInput) { 1845d0d4852847432368b090c184d6639e573538dccfCalin Juravle if (locations->InAt(0).IsUnallocated()) { 1846d0d4852847432368b090c184d6639e573538dccfCalin Juravle locations->SetInAt(0, source); 1847d0d4852847432368b090c184d6639e573538dccfCalin Juravle } else { 1848d0d4852847432368b090c184d6639e573538dccfCalin Juravle DCHECK(locations->InAt(0).Equals(source)); 1849d0d4852847432368b090c184d6639e573538dccfCalin Juravle } 185086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 1851829280cc90b7a84db42864589b4bafb4c94a79d9Nicolas Geoffray locations->UpdateOut(source); 185286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 185386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(source.Equals(location)); 185486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 185586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 185686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 185786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Connect siblings. 185886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) { 185986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); 186086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray ConnectSiblings(instruction->GetLiveInterval()); 186186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 186286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 186386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Resolve non-linear control flow across branches. Order does not matter. 18640d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) { 186586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HBasicBlock* block = it.Current(); 186677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil if (block->IsCatchBlock()) { 186777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // Instructions live at the top of catch blocks were forced to spill. 186877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil if (kIsDebugBuild) { 186977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil BitVector* live = liveness_.GetLiveInSet(*block); 187077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil for (uint32_t idx : live->Indexes()) { 187177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil LiveInterval* interval = liveness_.GetInstructionFromSsaIndex(idx)->GetLiveInterval(); 187277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil DCHECK(!interval->GetSiblingAt(block->GetLifetimeStart())->HasRegister()); 187377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 187477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 187577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } else { 187677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil BitVector* live = liveness_.GetLiveInSet(*block); 187777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil for (uint32_t idx : live->Indexes()) { 187877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil LiveInterval* interval = liveness_.GetInstructionFromSsaIndex(idx)->GetLiveInterval(); 187977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil for (HBasicBlock* predecessor : block->GetPredecessors()) { 188077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil ConnectSplitSiblings(interval, predecessor, block); 188177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 188286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 188386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 188486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 188586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 188686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Resolve phi inputs. Order does not matter. 18870d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray for (HLinearOrderIterator it(*codegen_->GetGraph()); !it.Done(); it.Advance()) { 188886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HBasicBlock* current = it.Current(); 188977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil if (current->IsCatchBlock()) { 189077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // Catch phi values are set at runtime by the exception delivery mechanism. 189177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } else { 189277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) { 189377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil HInstruction* phi = inst_it.Current(); 189477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil for (size_t i = 0, e = current->GetPredecessors().size(); i < e; ++i) { 189577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil HBasicBlock* predecessor = current->GetPredecessor(i); 189677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil DCHECK_EQ(predecessor->NumberOfNormalSuccessors(), 1u); 189777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil HInstruction* input = phi->InputAt(i); 189877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil Location source = input->GetLiveInterval()->GetLocationAt( 189977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil predecessor->GetLifetimeEnd() - 1); 190077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil Location destination = phi->GetLiveInterval()->ToLocation(); 190177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil InsertParallelMoveAtExitOf(predecessor, phi, source, destination); 190277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil } 190386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 190486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 190586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 19063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 19073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Assign temp locations. 19082aaa4b5532d30c4e65d8892b556400bb61f9dc8cVladimir Marko for (LiveInterval* temp : temp_intervals_) { 1909840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (temp->IsHighInterval()) { 1910840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray // High intervals can be skipped, they are already handled by the low interval. 1911840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray continue; 1912840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 191301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray HInstruction* at = liveness_.GetTempUser(temp); 1914f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray size_t temp_index = liveness_.GetTempIndex(temp); 191501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray LocationSummary* locations = at->GetLocations(); 19165368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain switch (temp->GetType()) { 19175368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain case Primitive::kPrimInt: 1918f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray locations->SetTempAt(temp_index, Location::RegisterLocation(temp->GetRegister())); 19195368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain break; 19205368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain 19215368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain case Primitive::kPrimDouble: 1922840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) { 1923840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray Location location = Location::FpuRegisterPairLocation( 1924840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray temp->GetRegister(), temp->GetHighInterval()->GetRegister()); 1925f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray locations->SetTempAt(temp_index, location); 1926840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } else { 1927f01d34445953e6b9c9b13de1dd32a5c0ee5abab5Nicolas Geoffray locations->SetTempAt(temp_index, Location::FpuRegisterLocation(temp->GetRegister())); 1928840e5461a85f8908f51e7f6cd562a9129ff0e7ceNicolas Geoffray } 19295368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain break; 19305368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain 19315368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain default: 19325368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain LOG(FATAL) << "Unexpected type for temporary location " 19335368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain << temp->GetType(); 19345368c219a462defc90c4b896b34eb7506ba5c142Roland Levillain } 19353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 193631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray} 193731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 1938a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} // namespace art 1939