register_allocator.cc revision 46fbaab1bf2981f2768b046abf43e368663daacd
1a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray/* 2a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Copyright (C) 2014 The Android Open Source Project 3a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * 4a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License"); 5a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * you may not use this file except in compliance with the License. 6a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * You may obtain a copy of the License at 7a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * 8a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * http://www.apache.org/licenses/LICENSE-2.0 9a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * 10a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * Unless required by applicable law or agreed to in writing, software 11a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS, 12a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * See the License for the specific language governing permissions and 14a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray * limitations under the License. 15a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray */ 16a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 17a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h" 18a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 19c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#include <sstream> 20c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers 21e77493c7217efdd1a0ecef521a6845a13da0305bIan Rogers#include "base/bit_vector-inl.h" 22a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "code_generator.h" 23a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "ssa_liveness_analysis.h" 24a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 25a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraynamespace art { 26a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 27a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraystatic constexpr size_t kMaxLifetimePosition = -1; 2831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraystatic constexpr size_t kDefaultNumberOfSpillSlots = 4; 29a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 3086dbb9a12119273039ce272b41c809fa548b37b6Nicolas GeoffrayRegisterAllocator::RegisterAllocator(ArenaAllocator* allocator, 3186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray CodeGenerator* codegen, 3286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray const SsaLivenessAnalysis& liveness) 33a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray : allocator_(allocator), 34a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray codegen_(codegen), 3586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray liveness_(liveness), 363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray unhandled_core_intervals_(allocator, 0), 373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray unhandled_fp_intervals_(allocator, 0), 383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray unhandled_(nullptr), 39a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray handled_(allocator, 0), 40a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray active_(allocator, 0), 41a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray inactive_(allocator, 0), 42102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray physical_core_register_intervals_(allocator, codegen->GetNumberOfCoreRegisters()), 43102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray physical_fp_register_intervals_(allocator, codegen->GetNumberOfFloatingPointRegisters()), 443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray temp_intervals_(allocator, 4), 4531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray spill_slots_(allocator, kDefaultNumberOfSpillSlots), 463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray safepoints_(allocator, 0), 47a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray processing_core_registers_(false), 48a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray number_of_registers_(-1), 49a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray registers_array_(nullptr), 50102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray blocked_core_registers_(codegen->GetBlockedCoreRegisters()), 51102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray blocked_fp_registers_(codegen->GetBlockedFloatingPointRegisters()), 523bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray reserved_out_slots_(0), 533bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray maximum_number_of_live_registers_(0) { 5471175b7f19a4f6cf9cc264feafd820dbafa371fbNicolas Geoffray codegen->SetupBlockedRegisters(); 55102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray physical_core_register_intervals_.SetSize(codegen->GetNumberOfCoreRegisters()); 56102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray physical_fp_register_intervals_.SetSize(codegen->GetNumberOfFloatingPointRegisters()); 573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Always reserve for the current method and the graph's max out registers. 583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // TODO: compute it instead. 593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray reserved_out_slots_ = 1 + codegen->GetGraph()->GetMaximumNumberOfOutVRegs(); 60a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 61a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 6286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph, 6386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray InstructionSet instruction_set) { 6486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (!Supports(instruction_set)) { 6586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return false; 6686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 6786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (size_t i = 0, e = graph.GetBlocks().Size(); i < e; ++i) { 6886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (HInstructionIterator it(graph.GetBlocks().Get(i)->GetInstructions()); 6986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray !it.Done(); 7086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray it.Advance()) { 7186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* current = it.Current(); 72412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray if (current->GetType() == Primitive::kPrimLong && instruction_set != kX86_64) return false; 73102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if ((current->GetType() == Primitive::kPrimFloat || current->GetType() == Primitive::kPrimDouble) 74102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray && instruction_set != kX86_64) { 75102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray return false; 76102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 7786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 7886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 7986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return true; 8086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 8186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 8286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraystatic bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) { 833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (interval == nullptr) return false; 8486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray bool is_core_register = (interval->GetType() != Primitive::kPrimDouble) 8586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray && (interval->GetType() != Primitive::kPrimFloat); 86a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return processing_core_registers == is_core_register; 87a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 88a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 8986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegisters() { 9086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray AllocateRegistersInternal(); 9186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Resolve(); 9286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 9386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (kIsDebugBuild) { 9486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray processing_core_registers_ = true; 9586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray ValidateInternal(true); 9686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray processing_core_registers_ = false; 9786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray ValidateInternal(true); 9886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 9986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 10086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 10186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::BlockRegister(Location location, 10286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray size_t start, 103102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray size_t end) { 10456b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray int reg = location.reg(); 105102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray DCHECK(location.IsRegister() || location.IsFpuRegister()); 106102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray LiveInterval* interval = location.IsRegister() 107102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray ? physical_core_register_intervals_.Get(reg) 108102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray : physical_fp_register_intervals_.Get(reg); 109102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray Primitive::Type type = location.IsRegister() 110102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray ? Primitive::kPrimInt 111102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray : Primitive::kPrimDouble; 11286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (interval == nullptr) { 11386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray interval = LiveInterval::MakeFixedInterval(allocator_, reg, type); 114102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (location.IsRegister()) { 115102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray physical_core_register_intervals_.Put(reg, interval); 116102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } else { 117102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray physical_fp_register_intervals_.Put(reg, interval); 118102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 11986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 12086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(interval->GetRegister() == reg); 12186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray interval->AddRange(start, end); 12286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 12386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 12486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::AllocateRegistersInternal() { 1253946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Iterate post-order, to ensure the list is sorted, and the last added interval 1263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // is the one with the lowest start position. 1273946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (HLinearPostOrderIterator it(liveness_); !it.Done(); it.Advance()) { 1283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray HBasicBlock* block = it.Current(); 129277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done(); 130277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe back_it.Advance()) { 131277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe ProcessInstruction(back_it.Current()); 1323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 133277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { 134277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe ProcessInstruction(inst_it.Current()); 1353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 1363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 137a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 1383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray number_of_registers_ = codegen_->GetNumberOfCoreRegisters(); 139a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_); 1403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray processing_core_registers_ = true; 1413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray unhandled_ = &unhandled_core_intervals_; 142102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) { 143102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray LiveInterval* fixed = physical_core_register_intervals_.Get(i); 144102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (fixed != nullptr) { 145296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Fixed interval is added to inactive_ instead of unhandled_. 146296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // It's also the only type of inactive interval whose start position 147296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // can be after the current interval during linear scan. 148296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Fixed interval is never split and never moves to unhandled_. 149102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray inactive_.Add(fixed); 150102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 151102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 1523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LinearScan(); 153a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 154102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray size_t saved_maximum_number_of_live_registers = maximum_number_of_live_registers_; 155102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray maximum_number_of_live_registers_ = 0; 156102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray 1573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray inactive_.Reset(); 1583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray active_.Reset(); 1593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray handled_.Reset(); 160a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 1613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters(); 1623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_); 1633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray processing_core_registers_ = false; 1643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray unhandled_ = &unhandled_fp_intervals_; 165102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) { 166102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray LiveInterval* fixed = physical_fp_register_intervals_.Get(i); 167102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (fixed != nullptr) { 168296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Fixed interval is added to inactive_ instead of unhandled_. 169296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // It's also the only type of inactive interval whose start position 170296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // can be after the current interval during linear scan. 171296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Fixed interval is never split and never moves to unhandled_. 172102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray inactive_.Add(fixed); 173102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 174102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 1753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LinearScan(); 176102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray maximum_number_of_live_registers_ += saved_maximum_number_of_live_registers; 1773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray} 17886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 1793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::ProcessInstruction(HInstruction* instruction) { 1803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LocationSummary* locations = instruction->GetLocations(); 1813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray size_t position = instruction->GetLifetimePosition(); 1823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 1833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (locations == nullptr) return; 1843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 1853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Create synthesized intervals for temporaries. 1863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t i = 0; i < locations->GetTempCount(); ++i) { 1873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray Location temp = locations->GetTemp(i); 18852839d17c06175e19ca4a093fb878450d1c4310dNicolas Geoffray if (temp.IsRegister() || temp.IsFpuRegister()) { 189102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray BlockRegister(temp, position, position + 1); 1903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 191102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray DCHECK(temp.IsUnallocated()); 19252839d17c06175e19ca4a093fb878450d1c4310dNicolas Geoffray DCHECK(temp.GetPolicy() == Location::kRequiresRegister); 19301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray LiveInterval* interval = LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt); 1943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray temp_intervals_.Add(interval); 1953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray interval->AddRange(position, position + 1); 1963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray unhandled_core_intervals_.Add(interval); 197a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 198a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 19986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 2003bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray bool core_register = (instruction->GetType() != Primitive::kPrimDouble) 2013bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray && (instruction->GetType() != Primitive::kPrimFloat); 2023bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray 2033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (locations->CanCall()) { 2043bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray if (!instruction->IsSuspendCheck()) { 2053bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray codegen_->MarkNotLeaf(); 2063bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray } 2073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray safepoints_.Add(instruction); 2083bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray if (locations->OnlyCallsOnSlowPath()) { 2093bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // We add a synthesized range at this position to record the live registers 2103bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // at this position. Ideally, we could just update the safepoints when locations 2113bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // are updated, but we currently need to know the full stack size before updating 2123bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // locations (because of parameters and the fact that we don't have a frame pointer). 2133bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // And knowing the full stack size requires to know the maximum number of live 2143bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // registers at calls in slow paths. 2153bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // By adding the following interval in the algorithm, we can compute this 2163bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray // maximum before updating locations. 2173bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction); 21887d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // The start of the interval must be after the position of the safepoint, so that 21987d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // we can just check the number of active registers at that position. Note that this 22087d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // will include the current interval in the computation of 22187d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // `maximum_number_of_live_registers`, so we need a better strategy if this becomes 22287d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // a problem. 22387d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // TODO: We could put the logic in AddSorted, to ensure the safepoint range is 22487d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // after all other intervals starting at that same position. 22587d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray interval->AddRange(position + 1, position + 2); 22687d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray AddSorted(&unhandled_core_intervals_, interval); 22787d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray AddSorted(&unhandled_fp_intervals_, interval); 2283bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray } 2293bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray } 2303bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray 2313bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray if (locations->WillCall()) { 2323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Block all registers. 2333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) { 23456b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray BlockRegister(Location::RegisterLocation(i), 2353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray position, 236102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray position + 1); 237102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 238102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) { 239102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray BlockRegister(Location::FpuRegisterLocation(i), 240102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray position, 241102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray position + 1); 2423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 2433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 2443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 2453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t i = 0; i < instruction->InputCount(); ++i) { 2463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray Location input = locations->InAt(i); 247102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (input.IsRegister() || input.IsFpuRegister()) { 248102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray BlockRegister(input, position, position + 1); 2493946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 2503946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 2513946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 2523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LiveInterval* current = instruction->GetLiveInterval(); 2533946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (current == nullptr) return; 2543946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 255102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray GrowableArray<LiveInterval*>& unhandled = core_register 256102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray ? unhandled_core_intervals_ 257102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray : unhandled_fp_intervals_; 258102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray 2597690562d40878f44823d5fb03a2084cfc677ec4aNicolas Geoffray DCHECK(unhandled.IsEmpty() || current->StartsBeforeOrAt(unhandled.Peek())); 26087d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray 2613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Some instructions define their output in fixed register/stack slot. We need 2623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // to ensure we know these locations before doing register allocation. For a 2633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // given register, we create an interval that covers these locations. The register 2643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // will be unavailable at these locations when trying to allocate one for an 2653946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // interval. 2663946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // 2673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // The backwards walking ensures the ranges are ordered on increasing start positions. 2683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray Location output = locations->Out(); 269d0d4852847432368b090c184d6639e573538dccfCalin Juravle if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) { 270d0d4852847432368b090c184d6639e573538dccfCalin Juravle Location first = locations->InAt(0); 271d0d4852847432368b090c184d6639e573538dccfCalin Juravle if (first.IsRegister() || first.IsFpuRegister()) { 272d0d4852847432368b090c184d6639e573538dccfCalin Juravle current->SetFrom(position + 1); 273d0d4852847432368b090c184d6639e573538dccfCalin Juravle current->SetRegister(first.reg()); 274d0d4852847432368b090c184d6639e573538dccfCalin Juravle } 275d0d4852847432368b090c184d6639e573538dccfCalin Juravle } else if (output.IsRegister() || output.IsFpuRegister()) { 2763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Shift the interval's start by one to account for the blocked register. 2773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray current->SetFrom(position + 1); 27856b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray current->SetRegister(output.reg()); 279102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray BlockRegister(output, position, position + 1); 2803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) { 2813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray current->SetSpillSlot(output.GetStackIndex()); 2823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 2833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 2843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // If needed, add interval to the list of unhandled intervals. 2853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (current->HasSpillSlot() || instruction->IsConstant()) { 286c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray // Split just before first register use. 2873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray size_t first_register_use = current->FirstRegisterUse(); 2883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (first_register_use != kNoLifetime) { 289c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray LiveInterval* split = Split(current, first_register_use - 1); 290b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray // Don't add directly to `unhandled`, it needs to be sorted and the start 2913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // of this new interval might be after intervals already in the list. 2923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray AddSorted(&unhandled, split); 2933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 2943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Nothing to do, we won't allocate a register for this value. 2953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 2963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 297b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray // Don't add directly to `unhandled`, temp or safepoint intervals 298b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray // for this instruction may have been added, and those can be 299b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray // processed first. 300b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray AddSorted(&unhandled, current); 3013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 302a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 303a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 30431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayclass AllRangesIterator : public ValueObject { 30531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray public: 30631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray explicit AllRangesIterator(LiveInterval* interval) 30731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray : current_interval_(interval), 30831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray current_range_(interval->GetFirstRange()) {} 30931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 31031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray bool Done() const { return current_interval_ == nullptr; } 31131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveRange* CurrentRange() const { return current_range_; } 31231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveInterval* CurrentInterval() const { return current_interval_; } 31331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 31431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray void Advance() { 31531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray current_range_ = current_range_->GetNext(); 31631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (current_range_ == nullptr) { 31731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray current_interval_ = current_interval_->GetNextSibling(); 31831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (current_interval_ != nullptr) { 31931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray current_range_ = current_interval_->GetFirstRange(); 32031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 32131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 32231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 32331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 32431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray private: 32531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveInterval* current_interval_; 32631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveRange* current_range_; 32731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 32831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray DISALLOW_COPY_AND_ASSIGN(AllRangesIterator); 32931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray}; 33031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 33186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffraybool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const { 33286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // To simplify unit testing, we eagerly create the array of intervals, and 33386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // call the helper method. 33486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray GrowableArray<LiveInterval*> intervals(allocator_, 0); 33586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) { 33686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); 33786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) { 33886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray intervals.Add(instruction->GetLiveInterval()); 33986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 34086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 34186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 342102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (processing_core_registers_) { 343102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) { 344102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray LiveInterval* fixed = physical_core_register_intervals_.Get(i); 345102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (fixed != nullptr) { 346102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray intervals.Add(fixed); 347102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 348102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 349102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } else { 350102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) { 351102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray LiveInterval* fixed = physical_fp_register_intervals_.Get(i); 352102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (fixed != nullptr) { 353102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray intervals.Add(fixed); 354102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 35586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 35686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 35786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 3583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t i = 0, e = temp_intervals_.Size(); i < e; ++i) { 3593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LiveInterval* temp = temp_intervals_.Get(i); 3603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (ShouldProcess(processing_core_registers_, temp)) { 3613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray intervals.Add(temp); 3623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 3633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 3643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 3653946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray return ValidateIntervals(intervals, spill_slots_.Size(), reserved_out_slots_, *codegen_, 3663946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray allocator_, processing_core_registers_, log_fatal_on_failure); 36786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 36886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 36931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffraybool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals, 37031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray size_t number_of_spill_slots, 3713946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray size_t number_of_out_slots, 372a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray const CodeGenerator& codegen, 373a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray ArenaAllocator* allocator, 374a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray bool processing_core_registers, 375a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray bool log_fatal_on_failure) { 376a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t number_of_registers = processing_core_registers 377a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray ? codegen.GetNumberOfCoreRegisters() 378a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray : codegen.GetNumberOfFloatingPointRegisters(); 37931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray GrowableArray<ArenaBitVector*> liveness_of_values( 38031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray allocator, number_of_registers + number_of_spill_slots); 381a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 382a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Allocate a bit vector per register. A live interval that has a register 383a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // allocated will populate the associated bit vector based on its live ranges. 38431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) { 38531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true)); 386a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 387a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 38831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray for (size_t i = 0, e = intervals.Size(); i < e; ++i) { 38931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) { 39031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveInterval* current = it.CurrentInterval(); 39186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* defined_by = current->GetParent()->GetDefinedBy(); 39286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (current->GetParent()->HasSpillSlot() 39386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Parameters have their own stack slot. 39486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray && !(defined_by != nullptr && defined_by->IsParameterValue())) { 3953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers 3963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray + current->GetParent()->GetSpillSlot() / kVRegSize 3973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray - number_of_out_slots); 39831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) { 39931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (liveness_of_spill_slot->IsBitSet(j)) { 40031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (log_fatal_on_failure) { 40131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray std::ostringstream message; 40231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray message << "Spill slot conflict at " << j; 40331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LOG(FATAL) << message.str(); 40431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } else { 40531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray return false; 40631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 40731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } else { 40831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray liveness_of_spill_slot->SetBit(j); 40931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 41031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 411a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 41231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 41331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (current->HasRegister()) { 41431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister()); 41531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) { 41631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (liveness_of_register->IsBitSet(j)) { 417a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (log_fatal_on_failure) { 418a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray std::ostringstream message; 4193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray message << "Register conflict at " << j << " "; 4203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (defined_by != nullptr) { 4213946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray message << "(" << defined_by->DebugName() << ")"; 4223946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 4233946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray message << "for "; 424a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (processing_core_registers) { 425a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray codegen.DumpCoreRegister(message, current->GetRegister()); 426a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 427a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray codegen.DumpFloatingPointRegister(message, current->GetRegister()); 428a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 429a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LOG(FATAL) << message.str(); 430a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 431a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return false; 432a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 433a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 43431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray liveness_of_register->SetBit(j); 435a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 436a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 43731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 43831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 439a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 440a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return true; 441a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 442a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 44386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const { 444a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray interval->Dump(stream); 445a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray stream << ": "; 446a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (interval->HasRegister()) { 447102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray if (interval->IsFloatingPoint()) { 44886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray codegen_->DumpFloatingPointRegister(stream, interval->GetRegister()); 449102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } else { 450102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray codegen_->DumpCoreRegister(stream, interval->GetRegister()); 451a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 452a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 453a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray stream << "spilled"; 454a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 455a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray stream << std::endl; 456a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 457a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 458296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yangvoid RegisterAllocator::DumpAllIntervals(std::ostream& stream) const { 459296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang stream << "inactive: " << std::endl; 460296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang for (size_t i = 0; i < inactive_.Size(); i ++) { 461296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DumpInterval(stream, inactive_.Get(i)); 462296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 463296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang stream << "active: " << std::endl; 464296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang for (size_t i = 0; i < active_.Size(); i ++) { 465296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DumpInterval(stream, active_.Get(i)); 466296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 467296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang stream << "unhandled: " << std::endl; 468296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang auto unhandled = (unhandled_ != nullptr) ? 469296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang unhandled_ : &unhandled_core_intervals_; 470296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang for (size_t i = 0; i < unhandled->Size(); i ++) { 471296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DumpInterval(stream, unhandled->Get(i)); 472296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 473296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang stream << "handled: " << std::endl; 474296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang for (size_t i = 0; i < handled_.Size(); i ++) { 475296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DumpInterval(stream, handled_.Get(i)); 476296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 477296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang} 478296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang 479a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// By the book implementation of a linear scan register allocator. 480a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffrayvoid RegisterAllocator::LinearScan() { 4813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray while (!unhandled_->IsEmpty()) { 482a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (1) Remove interval with the lowest start position from unhandled. 4833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LiveInterval* current = unhandled_->Pop(); 4843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray DCHECK(!current->IsFixed() && !current->HasSpillSlot()); 485c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart()); 48687d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray 48787d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray if (current->IsSlowPathSafepoint()) { 48887d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // Synthesized interval to record the maximum number of live registers 48987d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // at safepoints. No need to allocate a register for it. 49087d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // We know that current actives are all live at the safepoint (modulo 49187d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray // the one created by the safepoint). 49287d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray maximum_number_of_live_registers_ = 49387d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray std::max(maximum_number_of_live_registers_, active_.Size()); 49487d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray continue; 49587d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray } 49687d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray 497a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t position = current->GetStart(); 498a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 499296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Remember the inactive_ size here since the ones moved to inactive_ from 500296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // active_ below shouldn't need to be re-checked. 501296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang size_t inactive_intervals_to_handle = inactive_.Size(); 502296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang 503a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (2) Remove currently active intervals that are dead at this position. 504a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Move active intervals that have a lifetime hole at this position 505a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // to inactive. 506a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray for (size_t i = 0; i < active_.Size(); ++i) { 507a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* interval = active_.Get(i); 508a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (interval->IsDeadAt(position)) { 509a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray active_.Delete(interval); 510a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray --i; 511a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray handled_.Add(interval); 512a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else if (!interval->Covers(position)) { 513a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray active_.Delete(interval); 514a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray --i; 515a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray inactive_.Add(interval); 516a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 517a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 518a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 519a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (3) Remove currently inactive intervals that are dead at this position. 520a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Move inactive intervals that cover this position to active. 521296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang for (size_t i = 0; i < inactive_intervals_to_handle; ++i) { 522a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* interval = inactive_.Get(i); 523296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK(interval->GetStart() < position || interval->IsFixed()); 524a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (interval->IsDeadAt(position)) { 525a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray inactive_.Delete(interval); 526a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray --i; 527296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang --inactive_intervals_to_handle; 528a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray handled_.Add(interval); 529a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else if (interval->Covers(position)) { 530a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray inactive_.Delete(interval); 531a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray --i; 532296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang --inactive_intervals_to_handle; 533a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray active_.Add(interval); 534a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 535a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 536a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 537a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (4) Try to find an available register. 538a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray bool success = TryAllocateFreeReg(current); 539a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 540a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (5) If no register could be found, we need to spill. 541a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (!success) { 542a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray success = AllocateBlockedReg(current); 543a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 544a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 545a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // (6) If the interval had a register allocated, add it to the list of active 546a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // intervals. 547a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (success) { 548a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray active_.Add(current); 549a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 550a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 551a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 552a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 553a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find a free register. If multiple are found, pick the register that 554a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// is free the longest. 555a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) { 556a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t* free_until = registers_array_; 557a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 558a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // First set all registers to be free. 559a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray for (size_t i = 0; i < number_of_registers_; ++i) { 560a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray free_until[i] = kMaxLifetimePosition; 561a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 562a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 563296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // For each active interval, set its register to not free. 564296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang for (size_t i = 0, e = active_.Size(); i < e; ++i) { 565296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang LiveInterval* interval = active_.Get(i); 566296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK(interval->HasRegister()); 567296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang free_until[interval->GetRegister()] = 0; 568296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 569296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang 570a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // For each inactive interval, set its register to be free until 571a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // the next intersection with `current`. 572a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray for (size_t i = 0, e = inactive_.Size(); i < e; ++i) { 573a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* inactive = inactive_.Get(i); 574296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Temp/Slow-path-safepoint interval has no holes. 575296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint()); 576296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang if (!current->IsSplit() && !inactive->IsFixed()) { 577296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Neither current nor inactive are fixed. 578296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Thanks to SSA, a non-split interval starting in a hole of an 579296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // inactive interval should never intersect with that inactive interval. 580296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Only if it's not fixed though, because fixed intervals don't come from SSA. 581296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime); 582296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang continue; 583296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 584296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang 585a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(inactive->HasRegister()); 586296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang if (free_until[inactive->GetRegister()] == 0) { 587296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Already used by some active interval. No need to intersect. 588296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang continue; 589296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 590a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t next_intersection = inactive->FirstIntersectionWith(current); 591a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (next_intersection != kNoLifetime) { 592aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray free_until[inactive->GetRegister()] = 593aac0f39a3501a7f7dd04b2342c2a16961969f139Nicolas Geoffray std::min(free_until[inactive->GetRegister()], next_intersection); 594a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 595a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 596a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 597a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray int reg = -1; 5983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (current->HasRegister()) { 5993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Some instructions have a fixed register output. 6003946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray reg = current->GetRegister(); 6013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray DCHECK_NE(free_until[reg], 0u); 6023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 60301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray int hint = current->FindFirstRegisterHint(free_until); 60401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray if (hint != kNoRegister) { 60501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray DCHECK(!IsBlocked(hint)); 60601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray reg = hint; 60701ef345767ea609417fc511e42007705c9667546Nicolas Geoffray } else { 60801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray // Pick the register that is free the longest. 60901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray for (size_t i = 0; i < number_of_registers_; ++i) { 61001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray if (IsBlocked(i)) continue; 61101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray if (reg == -1 || free_until[i] > free_until[reg]) { 61201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray reg = i; 61301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray if (free_until[i] == kMaxLifetimePosition) break; 61401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray } 6153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 616a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 617a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 618a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 619a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // If we could not find a register, we need to spill. 620a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (reg == -1 || free_until[reg] == 0) { 621a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return false; 622a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 623a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 624a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray current->SetRegister(reg); 625a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (!current->IsDeadAt(free_until[reg])) { 626a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // If the register is only available for a subset of live ranges 627a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // covered by `current`, split `current` at the position where 628a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // the register is not available anymore. 629a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* split = Split(current, free_until[reg]); 630a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(split != nullptr); 6313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray AddSorted(unhandled_, split); 632a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 633a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return true; 634a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 635a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 636a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::IsBlocked(int reg) const { 637102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray return processing_core_registers_ 638102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray ? blocked_core_registers_[reg] 639102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray : blocked_fp_registers_[reg]; 640a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 641a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 642a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// Find the register that is used the last, and spill the interval 643a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// that holds it. If the first use of `current` is after that register 644a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray// we spill `current` instead. 645a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffraybool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) { 646a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t first_register_use = current->FirstRegisterUse(); 647412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray if (first_register_use == kNoLifetime) { 64831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray AllocateSpillSlotFor(current); 649a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return false; 650a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 651a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 652a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // First set all registers as not being used. 653a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray size_t* next_use = registers_array_; 654a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray for (size_t i = 0; i < number_of_registers_; ++i) { 655a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray next_use[i] = kMaxLifetimePosition; 656a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 657a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 658a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // For each active interval, find the next use of its register after the 659a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // start of current. 660a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray for (size_t i = 0, e = active_.Size(); i < e; ++i) { 661a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* active = active_.Get(i); 662a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(active->HasRegister()); 66386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (active->IsFixed()) { 66486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray next_use[active->GetRegister()] = current->GetStart(); 66586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 66686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray size_t use = active->FirstRegisterUseAfter(current->GetStart()); 66786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (use != kNoLifetime) { 66886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray next_use[active->GetRegister()] = use; 66986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 670a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 671a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 672a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 673a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // For each inactive interval, find the next use of its register after the 674a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // start of current. 675a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray for (size_t i = 0, e = inactive_.Size(); i < e; ++i) { 676a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* inactive = inactive_.Get(i); 677296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Temp/Slow-path-safepoint interval has no holes. 678296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint()); 679296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang if (!current->IsSplit() && !inactive->IsFixed()) { 680296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Neither current nor inactive are fixed. 681296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Thanks to SSA, a non-split interval starting in a hole of an 682296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // inactive interval should never intersect with that inactive interval. 683296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Only if it's not fixed though, because fixed intervals don't come from SSA. 684296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime); 685296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang continue; 686296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 687a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(inactive->HasRegister()); 68886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray size_t next_intersection = inactive->FirstIntersectionWith(current); 68986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (next_intersection != kNoLifetime) { 69086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (inactive->IsFixed()) { 69186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray next_use[inactive->GetRegister()] = 69286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray std::min(next_intersection, next_use[inactive->GetRegister()]); 69386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 69486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray size_t use = inactive->FirstRegisterUseAfter(current->GetStart()); 69586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (use != kNoLifetime) { 69686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]); 69786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 69886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 699a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 700a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 701a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 702a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Pick the register that is used the last. 703a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray int reg = -1; 704a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray for (size_t i = 0; i < number_of_registers_; ++i) { 705a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (IsBlocked(i)) continue; 706a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (reg == -1 || next_use[i] > next_use[reg]) { 707a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray reg = i; 708a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (next_use[i] == kMaxLifetimePosition) break; 709a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 710a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 711a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 712a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (first_register_use >= next_use[reg]) { 713a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // If the first use of that instruction is after the last use of the found 714a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // register, we split this interval just before its first register use. 71531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray AllocateSpillSlotFor(current); 716c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray LiveInterval* split = Split(current, first_register_use - 1); 717c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray DCHECK_NE(current, split) << "There is not enough registers available for " 718c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray << split->GetParent()->GetDefinedBy()->DebugName(); 7193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray AddSorted(unhandled_, split); 720a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return false; 721a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 722a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Use this register and spill the active and inactives interval that 723a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // have that register. 724a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray current->SetRegister(reg); 725a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 726a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray for (size_t i = 0, e = active_.Size(); i < e; ++i) { 727a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* active = active_.Get(i); 728a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (active->GetRegister() == reg) { 72986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(!active->IsFixed()); 730a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* split = Split(active, current->GetStart()); 731a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray active_.DeleteAt(i); 732a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray handled_.Add(active); 7333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray AddSorted(unhandled_, split); 734a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray break; 735a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 736a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 737a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 738296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang for (size_t i = 0, e = inactive_.Size(); i < e; ++i) { 739a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* inactive = inactive_.Get(i); 740a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (inactive->GetRegister() == reg) { 741296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang if (!current->IsSplit() && !inactive->IsFixed()) { 742296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Neither current nor inactive are fixed. 743296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Thanks to SSA, a non-split interval starting in a hole of an 744296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // inactive interval should never intersect with that inactive interval. 745296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang // Only if it's not fixed though, because fixed intervals don't come from SSA. 746296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime); 747296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang continue; 748296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang } 74986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray size_t next_intersection = inactive->FirstIntersectionWith(current); 75086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (next_intersection != kNoLifetime) { 75186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (inactive->IsFixed()) { 75286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* split = Split(current, next_intersection); 7533946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray AddSorted(unhandled_, split); 75486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 755296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang LiveInterval* split = Split(inactive, next_intersection); 75686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray inactive_.DeleteAt(i); 757296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang --i; 758296bd60423e0630d8152b99fb7afb20fbff5a18aMingyao Yang --e; 75986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray handled_.Add(inactive); 7603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray AddSorted(unhandled_, split); 76186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 76286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 763a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 764a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 765a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 766a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return true; 767a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 768a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 769a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 7703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffrayvoid RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) { 771c8147a76ed2f440f38329dc08ff889d393b5c535Nicolas Geoffray DCHECK(!interval->IsFixed() && !interval->HasSpillSlot()); 77286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray size_t insert_at = 0; 7733946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t i = array->Size(); i > 0; --i) { 7743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LiveInterval* current = array->Get(i - 1); 775a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (current->StartsAfter(interval)) { 77686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray insert_at = i; 777a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray break; 778a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 779a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 7803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray array->InsertAt(insert_at, interval); 781a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 782a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 783a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas GeoffrayLiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) { 784a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(position >= interval->GetStart()); 785a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray DCHECK(!interval->IsDeadAt(position)); 786a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray if (position == interval->GetStart()) { 787a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray // Spill slot will be allocated when handling `interval` again. 788a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray interval->ClearRegister(); 789a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return interval; 790a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } else { 791a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray LiveInterval* new_interval = interval->SplitAt(position); 792a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray return new_interval; 793a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray } 794a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} 795a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 79631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffrayvoid RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) { 79731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveInterval* parent = interval->GetParent(); 79831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 79931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray // An instruction gets a spill slot for its entire lifetime. If the parent 80031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray // of this interval already has a spill slot, there is nothing to do. 80131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray if (parent->HasSpillSlot()) { 80231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray return; 80331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 80431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 80586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* defined_by = parent->GetDefinedBy(); 80686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (defined_by->IsParameterValue()) { 80786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Parameters have their own stack slot. 80886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue())); 80986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return; 81086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 81186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 81296f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray if (defined_by->IsConstant()) { 81396f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray // Constants don't need a spill slot. 81496f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray return; 81596f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray } 81696f89a290eb67d7bf4b1636798fa28df14309cc7Nicolas Geoffray 81731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray LiveInterval* last_sibling = interval; 81831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray while (last_sibling->GetNextSibling() != nullptr) { 81931d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray last_sibling = last_sibling->GetNextSibling(); 82031d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 82131d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray size_t end = last_sibling->GetEnd(); 82231d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 823412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray // Find an available spill slot. 824412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray size_t slot = 0; 825412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray for (size_t e = spill_slots_.Size(); slot < e; ++slot) { 826412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray // We check if it is less rather than less or equal because the parallel move 827412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray // resolver does not work when a single spill slot needs to be exchanged with 828412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray // a double spill slot. The strict comparison avoids needing to exchange these 829412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray // locations at the same lifetime position. 830412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray if (spill_slots_.Get(slot) < parent->GetStart() 831412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray && (slot == (e - 1) || spill_slots_.Get(slot + 1) < parent->GetStart())) { 832412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray break; 833412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray } 834412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray } 835412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray 83601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray if (parent->NeedsTwoSpillSlots()) { 8373c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray if (slot == spill_slots_.Size()) { 8383c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray // We need a new spill slot. 8393c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray spill_slots_.Add(end); 8403c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray spill_slots_.Add(end); 8413c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray } else if (slot == spill_slots_.Size() - 1) { 8423c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray spill_slots_.Put(slot, end); 8433c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray spill_slots_.Add(end); 8443c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray } else { 8453c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray spill_slots_.Put(slot, end); 8463c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray spill_slots_.Put(slot + 1, end); 84731d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 84831d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } else { 8493c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray if (slot == spill_slots_.Size()) { 8503c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray // We need a new spill slot. 8513c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray spill_slots_.Add(end); 8523c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray } else { 8533c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray spill_slots_.Put(slot, end); 8543c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray } 85531d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray } 85631d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 8573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray parent->SetSpillSlot((slot + reserved_out_slots_) * kVRegSize); 85886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 85986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 8602a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffraystatic bool IsValidDestination(Location destination) { 861102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray return destination.IsRegister() 862102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray || destination.IsFpuRegister() 863102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray || destination.IsStackSlot() 864102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray || destination.IsDoubleStackSlot(); 8652a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray} 8662a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray 867740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffrayvoid RegisterAllocator::AddInputMoveFor(HInstruction* user, 86886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location source, 86986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location destination) const { 8702a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray DCHECK(IsValidDestination(destination)); 87186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (source.Equals(destination)) return; 87286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 873476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain DCHECK(!user->IsPhi()); 87486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 875740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray HInstruction* previous = user->GetPrevious(); 87686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HParallelMove* move = nullptr; 87786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (previous == nullptr 878476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain || !previous->IsParallelMove() 8798e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray || previous->GetLifetimePosition() < user->GetLifetimePosition()) { 88086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 8818e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray move->SetLifetimePosition(user->GetLifetimePosition()); 882740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray user->GetBlock()->InsertInstructionBefore(move, user); 88386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 88486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = previous->AsParallelMove(); 88586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 8868e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition()); 887740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray move->AddMove(new (allocator_) MoveOperands(source, destination, nullptr)); 88886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 88986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 89046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionStart(size_t position) { 89146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray return (position & 1) == 0; 89246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray} 89346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray 89446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffraystatic bool IsInstructionEnd(size_t position) { 89546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray return (position & 1) == 1; 89646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray} 89746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray 89886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAt(size_t position, 899740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray HInstruction* instruction, 90086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location source, 90186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location destination) const { 9022a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray DCHECK(IsValidDestination(destination)); 90386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (source.Equals(destination)) return; 90486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 90586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* at = liveness_.GetInstructionFromPosition(position / 2); 90686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HParallelMove* move; 90746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray if (at == nullptr) { 90846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray if (IsInstructionStart(position)) { 90946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray // Block boundary, don't do anything the connection of split siblings will handle it. 91046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray return; 91146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray } else { 91246fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray // Move must happen before the first instruction of the block. 91346fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray at = liveness_.GetInstructionFromPosition((position + 1) / 2); 91446fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray move = at->AsParallelMove(); 91546fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray if (move == nullptr || move->GetLifetimePosition() < position) { 91646fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 91746fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray move->SetLifetimePosition(position); 91846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray at->GetBlock()->InsertInstructionBefore(move, at); 91946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray } 92046fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray } 92146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray } else if (IsInstructionEnd(position)) { 92286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Move must happen after the instruction. 92386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(!at->IsControlFlow()); 92486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = at->GetNext()->AsParallelMove(); 925e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // This is a parallel move for connecting siblings in a same block. We need to 926e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // differentiate it with moves for connecting blocks, and input moves. 9278e3964b766652a0478e8e0e303e8556c997675f1Nicolas Geoffray if (move == nullptr || move->GetLifetimePosition() > position) { 92886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 92986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move->SetLifetimePosition(position); 93086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray at->GetBlock()->InsertInstructionBefore(move, at->GetNext()); 93186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 93286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 93386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Move must happen before the instruction. 93486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* previous = at->GetPrevious(); 935740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray if (previous == nullptr 936740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray || !previous->IsParallelMove() 937740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray || previous->GetLifetimePosition() != position) { 938740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray // If the previous is a parallel move, then its position must be lower 939740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray // than the given `position`: it was added just after the non-parallel 940740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray // move instruction that precedes `instruction`. 941740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray DCHECK(previous == nullptr 942740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray || !previous->IsParallelMove() 943740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray || previous->GetLifetimePosition() < position); 94486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 94586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move->SetLifetimePosition(position); 94686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray at->GetBlock()->InsertInstructionBefore(move, at); 94786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 94886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = previous->AsParallelMove(); 94986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 95086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 95101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray DCHECK_EQ(move->GetLifetimePosition(), position); 952740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray move->AddMove(new (allocator_) MoveOperands(source, destination, instruction)); 95386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 95486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 95586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block, 956740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray HInstruction* instruction, 95786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location source, 95886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location destination) const { 9592a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray DCHECK(IsValidDestination(destination)); 96086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (source.Equals(destination)) return; 96186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 96286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK_EQ(block->GetSuccessors().Size(), 1u); 96386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* last = block->GetLastInstruction(); 964360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray // We insert moves at exit for phi predecessors and connecting blocks. 965360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray // A block ending with an if cannot branch to a block with phis because 966360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray // we do not allow critical edges. It can also not connect 967360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray // a split interval between two blocks: the move has to happen in the successor. 968360231a056e796c36ffe62348507e904dc9efb9bNicolas Geoffray DCHECK(!last->IsIf()); 96986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* previous = last->GetPrevious(); 97086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HParallelMove* move; 971e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // This is a parallel move for connecting blocks. We need to differentiate 972e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // it with moves for connecting siblings in a same block, and output moves. 973740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray if (previous == nullptr || !previous->IsParallelMove() 974e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray || previous->AsParallelMove()->GetLifetimePosition() != block->GetLifetimeEnd()) { 97586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 976e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray move->SetLifetimePosition(block->GetLifetimeEnd()); 97786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray block->InsertInstructionBefore(move, last); 97886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 97986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = previous->AsParallelMove(); 98086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 981740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray move->AddMove(new (allocator_) MoveOperands(source, destination, instruction)); 98286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 98386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 98486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block, 985740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray HInstruction* instruction, 98686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location source, 98786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location destination) const { 9882a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray DCHECK(IsValidDestination(destination)); 98986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (source.Equals(destination)) return; 99086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 99186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* first = block->GetFirstInstruction(); 99286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HParallelMove* move = first->AsParallelMove(); 993e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // This is a parallel move for connecting blocks. We need to differentiate 994e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // it with moves for connecting siblings in a same block, and input moves. 995e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray if (move == nullptr || move->GetLifetimePosition() != block->GetLifetimeStart()) { 99686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 99786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move->SetLifetimePosition(block->GetLifetimeStart()); 99886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray block->InsertInstructionBefore(move, first); 99986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 1000740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray move->AddMove(new (allocator_) MoveOperands(source, destination, instruction)); 100186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 100286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 100386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::InsertMoveAfter(HInstruction* instruction, 100486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location source, 100586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location destination) const { 10062a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray DCHECK(IsValidDestination(destination)); 100786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (source.Equals(destination)) return; 100886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 1009476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain if (instruction->IsPhi()) { 1010740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination); 101186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return; 101286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 101386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 1014e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray size_t position = instruction->GetLifetimePosition() + 1; 101586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HParallelMove* move = instruction->GetNext()->AsParallelMove(); 1016e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // This is a parallel move for moving the output of an instruction. We need 1017e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // to differentiate with input moves, moves for connecting siblings in a 1018e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray // and moves for connecting blocks. 1019e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray if (move == nullptr || move->GetLifetimePosition() != position) { 102086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray move = new (allocator_) HParallelMove(allocator_); 1021e27f31a81636ad74bd3376ee39cf215941b85c0eNicolas Geoffray move->SetLifetimePosition(position); 102286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext()); 102386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 1024740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray move->AddMove(new (allocator_) MoveOperands(source, destination, instruction)); 102586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 102686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 102786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSiblings(LiveInterval* interval) { 102886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* current = interval; 102986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (current->HasSpillSlot() && current->HasRegister()) { 103086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // We spill eagerly, so move must be at definition. 103186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray InsertMoveAfter(interval->GetDefinedBy(), 1032102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray interval->IsFloatingPoint() 1033102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray ? Location::FpuRegisterLocation(interval->GetRegister()) 1034102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray : Location::RegisterLocation(interval->GetRegister()), 103501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray interval->NeedsTwoSpillSlots() 1036412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot()) 1037412f10cfed002ab617c78f2621d68446ca4dd8bdNicolas Geoffray : Location::StackSlot(interval->GetParent()->GetSpillSlot())); 103886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 103986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray UsePosition* use = current->GetFirstUse(); 104086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 104186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Walk over all siblings, updating locations of use positions, and 104286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // connecting them when they are adjacent. 104386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray do { 104401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray Location source = current->ToLocation(); 104586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 104686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Walk over all uses covered by this interval, and update the location 104786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // information. 104886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray while (use != nullptr && use->GetPosition() <= current->GetEnd()) { 10493946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LocationSummary* locations = use->GetUser()->GetLocations(); 10503946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (use->GetIsEnvironment()) { 10513946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray locations->SetEnvironmentAt(use->GetInputIndex(), source); 10523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 105386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location expected_location = locations->InAt(use->GetInputIndex()); 105486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (expected_location.IsUnallocated()) { 105586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray locations->SetInAt(use->GetInputIndex(), source); 10562a877f32fe145ad50250389df958a559e7d4ad92Nicolas Geoffray } else if (!expected_location.IsConstant()) { 105786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray AddInputMoveFor(use->GetUser(), source, expected_location); 105886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 105986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 106086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray use = use->GetNext(); 106186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 106286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 106386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // If the next interval starts just after this one, and has a register, 106486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // insert a move. 106586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* next_sibling = current->GetNextSibling(); 106686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (next_sibling != nullptr 106786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray && next_sibling->HasRegister() 106886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray && current->GetEnd() == next_sibling->GetStart()) { 106901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray Location destination = next_sibling->ToLocation(); 1070740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination); 107186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 10723946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 10733946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // At each safepoint, we record stack and register information. 10743946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t i = 0, e = safepoints_.Size(); i < e; ++i) { 10753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray HInstruction* safepoint = safepoints_.Get(i); 10763946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray size_t position = safepoint->GetLifetimePosition(); 10773946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LocationSummary* locations = safepoint->GetLocations(); 1078b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray if (!current->Covers(position)) { 1079b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray continue; 1080b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray } 1081b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray if (interval->GetStart() == position) { 1082b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray // The safepoint is for this instruction, so the location of the instruction 1083b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray // does not need to be saved. 1084b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray continue; 1085b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray } 10863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 10873bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) { 10883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize); 10893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 10903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 10913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray switch (source.GetKind()) { 10923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray case Location::kRegister: { 10933bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray locations->AddLiveRegister(source); 109487d03761f35ad6cbe0bffbf1ec739875a471da6dNicolas Geoffray DCHECK_LE(locations->GetNumberOfLiveRegisters(), maximum_number_of_live_registers_); 10953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (current->GetType() == Primitive::kPrimNot) { 109656b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray locations->SetRegisterBit(source.reg()); 10973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 10983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray break; 10993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 1100102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray case Location::kFpuRegister: { 1101102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray locations->AddLiveRegister(source); 1102102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray break; 1103102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray } 11043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray case Location::kStackSlot: // Fall-through 11053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray case Location::kDoubleStackSlot: // Fall-through 11063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray case Location::kConstant: { 11073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Nothing to do. 11083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray break; 11093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 11103946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray default: { 11113946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LOG(FATAL) << "Unexpected location for object"; 11123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 11133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 11143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 111586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray current = next_sibling; 111686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } while (current != nullptr); 111786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(use == nullptr); 111886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 111986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 112086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval, 112186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HBasicBlock* from, 112286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HBasicBlock* to) const { 112386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (interval->GetNextSibling() == nullptr) { 112486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Nothing to connect. The whole range was allocated to the same location. 112586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return; 112686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 112786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 112846fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray // Intervals end at the lifetime end of a block. The decrement by one 112946fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray // ensures the `Cover` call will return true. 113086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray size_t from_position = from->GetLifetimeEnd() - 1; 113146fbaab1bf2981f2768b046abf43e368663daacdNicolas Geoffray size_t to_position = to->GetLifetimeStart(); 113286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 113386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* destination = nullptr; 113486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* source = nullptr; 113586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 113686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* current = interval; 113786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 113886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Check the intervals that cover `from` and `to`. 113986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray while ((current != nullptr) && (source == nullptr || destination == nullptr)) { 114086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (current->Covers(from_position)) { 114186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(source == nullptr); 114286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray source = current; 114386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 114486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (current->Covers(to_position)) { 114586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(destination == nullptr); 114686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray destination = current; 114786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 114886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 114986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray current = current->GetNextSibling(); 115086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 115186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 115286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (destination == source) { 115386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Interval was not split. 115486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return; 115586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 115686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 11578ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray DCHECK(destination != nullptr && source != nullptr); 11588ddb00ca935733f5d3b07816e5bb33d6cabe6ec4Nicolas Geoffray 115986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (!destination->HasRegister()) { 116086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Values are eagerly spilled. Spill slot already contains appropriate value. 116186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray return; 116286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 116386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 116486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // If `from` has only one successor, we can put the moves at the exit of it. Otherwise 116586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // we need to put the moves at the entry of `to`. 116686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (from->GetSuccessors().Size() == 1) { 1167740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray InsertParallelMoveAtExitOf(from, 1168740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray interval->GetParent()->GetDefinedBy(), 116901ef345767ea609417fc511e42007705c9667546Nicolas Geoffray source->ToLocation(), 117001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray destination->ToLocation()); 117186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 117286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK_EQ(to->GetPredecessors().Size(), 1u); 1173740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray InsertParallelMoveAtEntryOf(to, 1174740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray interval->GetParent()->GetDefinedBy(), 117501ef345767ea609417fc511e42007705c9667546Nicolas Geoffray source->ToLocation(), 117601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray destination->ToLocation()); 117786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 117886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray} 117986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 118086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffrayvoid RegisterAllocator::Resolve() { 11813bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray codegen_->ComputeFrameSize( 11823bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray spill_slots_.Size(), maximum_number_of_live_registers_, reserved_out_slots_); 118386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 118486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Adjust the Out Location of instructions. 118586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration. 118686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) { 118786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); 118886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* current = instruction->GetLiveInterval(); 118986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LocationSummary* locations = instruction->GetLocations(); 119086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray Location location = locations->Out(); 1191476df557fed5f0b3f32f8d11a654674bb403a8f8Roland Levillain if (instruction->IsParameterValue()) { 119286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Now that we know the frame size, adjust the parameter's location. 119386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (location.IsStackSlot()) { 119486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); 119586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray current->SetSpillSlot(location.GetStackIndex()); 1196f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray locations->UpdateOut(location); 119786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else if (location.IsDoubleStackSlot()) { 119886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); 119986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray current->SetSpillSlot(location.GetStackIndex()); 1200f43083d560565aea46c602adb86423daeefe589dNicolas Geoffray locations->UpdateOut(location); 120186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else if (current->HasSpillSlot()) { 120286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize()); 120386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 120486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 120586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 120601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray Location source = current->ToLocation(); 120786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 120886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (location.IsUnallocated()) { 120986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray if (location.GetPolicy() == Location::kSameAsFirstInput) { 1210d0d4852847432368b090c184d6639e573538dccfCalin Juravle if (locations->InAt(0).IsUnallocated()) { 1211d0d4852847432368b090c184d6639e573538dccfCalin Juravle locations->SetInAt(0, source); 1212d0d4852847432368b090c184d6639e573538dccfCalin Juravle } else { 1213d0d4852847432368b090c184d6639e573538dccfCalin Juravle DCHECK(locations->InAt(0).Equals(source)); 1214d0d4852847432368b090c184d6639e573538dccfCalin Juravle } 121586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 121686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray locations->SetOut(source); 121786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 121886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK(source.Equals(location)); 121986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 122086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 122186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 122286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Connect siblings. 122386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) { 122486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); 122586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray ConnectSiblings(instruction->GetLiveInterval()); 122686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 122786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 122886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Resolve non-linear control flow across branches. Order does not matter. 122986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) { 123086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HBasicBlock* block = it.Current(); 123186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray BitVector* live = liveness_.GetLiveInSet(*block); 123286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (uint32_t idx : live->Indexes()) { 123386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx); 123486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LiveInterval* interval = current->GetLiveInterval(); 123586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) { 123686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block); 123786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 123886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 123986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 124086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 124186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Resolve phi inputs. Order does not matter. 124286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) { 124386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HBasicBlock* current = it.Current(); 1244277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) { 1245277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe HInstruction* phi = inst_it.Current(); 124686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) { 124786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HBasicBlock* predecessor = current->GetPredecessors().Get(i); 124886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u); 124986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray HInstruction* input = phi->InputAt(i); 125001ef345767ea609417fc511e42007705c9667546Nicolas Geoffray Location source = input->GetLiveInterval()->GetLocationAt( 125101ef345767ea609417fc511e42007705c9667546Nicolas Geoffray predecessor->GetLifetimeEnd() - 1); 125201ef345767ea609417fc511e42007705c9667546Nicolas Geoffray Location destination = phi->GetLiveInterval()->ToLocation(); 1253740475d5f45b8caa2c3c6fc51e657ecf4f3547e5Nicolas Geoffray InsertParallelMoveAtExitOf(predecessor, nullptr, source, destination); 125486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 125586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 125686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } 12573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 12583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Assign temp locations. 12593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray HInstruction* current = nullptr; 12603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray size_t temp_index = 0; 12613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t i = 0; i < temp_intervals_.Size(); ++i) { 12623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray LiveInterval* temp = temp_intervals_.Get(i); 126301ef345767ea609417fc511e42007705c9667546Nicolas Geoffray HInstruction* at = liveness_.GetTempUser(temp); 126401ef345767ea609417fc511e42007705c9667546Nicolas Geoffray if (at != current) { 12653946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray temp_index = 0; 126601ef345767ea609417fc511e42007705c9667546Nicolas Geoffray current = at; 12673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 126801ef345767ea609417fc511e42007705c9667546Nicolas Geoffray LocationSummary* locations = at->GetLocations(); 1269102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray DCHECK(temp->GetType() == Primitive::kPrimInt); 12703946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray locations->SetTempAt( 127156b9ee6fe1d6880c5fca0e7feb28b25a1ded2e2fNicolas Geoffray temp_index++, Location::RegisterLocation(temp->GetRegister())); 12723946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 127331d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray} 127431d76b42ef5165351499da3f8ee0ac147428c5edNicolas Geoffray 1275a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray} // namespace art 1276