10b57cec5SDimitry Andric //===- RegAllocBase.cpp - Register Allocator Base Class -------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file defines the RegAllocBase class which provides common functionality 100b57cec5SDimitry Andric // for LiveIntervalUnion-based register allocators. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "RegAllocBase.h" 150b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 160b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 170b57cec5SDimitry Andric #include "llvm/CodeGen/LiveInterval.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/LiveRegMatrix.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 235ffd83dbSDimitry Andric #include "llvm/CodeGen/Spiller.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h" 26*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h" 270b57cec5SDimitry Andric #include "llvm/Pass.h" 280b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 290b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 300b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 310b57cec5SDimitry Andric #include "llvm/Support/Timer.h" 320b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 330b57cec5SDimitry Andric #include <cassert> 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric using namespace llvm; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric #define DEBUG_TYPE "regalloc" 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric STATISTIC(NumNewQueued, "Number of new live ranges queued"); 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric // Temporary verification option until we can put verification inside 420b57cec5SDimitry Andric // MachineVerifier. 430b57cec5SDimitry Andric static cl::opt<bool, true> 440b57cec5SDimitry Andric VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled), 450b57cec5SDimitry Andric cl::Hidden, cl::desc("Verify during register allocation")); 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric const char RegAllocBase::TimerGroupName[] = "regalloc"; 480b57cec5SDimitry Andric const char RegAllocBase::TimerGroupDescription[] = "Register Allocation"; 490b57cec5SDimitry Andric bool RegAllocBase::VerifyEnabled = false; 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 520b57cec5SDimitry Andric // RegAllocBase Implementation 530b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric // Pin the vtable to this file. 560b57cec5SDimitry Andric void RegAllocBase::anchor() {} 570b57cec5SDimitry Andric 58fe6060f1SDimitry Andric void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis, 590b57cec5SDimitry Andric LiveRegMatrix &mat) { 600b57cec5SDimitry Andric TRI = &vrm.getTargetRegInfo(); 610b57cec5SDimitry Andric MRI = &vrm.getRegInfo(); 620b57cec5SDimitry Andric VRM = &vrm; 630b57cec5SDimitry Andric LIS = &lis; 640b57cec5SDimitry Andric Matrix = &mat; 65*0fca6ea1SDimitry Andric MRI->freezeReservedRegs(); 660b57cec5SDimitry Andric RegClassInfo.runOnMachineFunction(vrm.getMachineFunction()); 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric // Visit all the live registers. If they are already assigned to a physical 700b57cec5SDimitry Andric // register, unify them with the corresponding LiveIntervalUnion, otherwise push 710b57cec5SDimitry Andric // them on the priority queue for later assignment. 720b57cec5SDimitry Andric void RegAllocBase::seedLiveRegs() { 730b57cec5SDimitry Andric NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName, 740b57cec5SDimitry Andric TimerGroupDescription, TimePassesIsEnabled); 750b57cec5SDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 76e8d8bef9SDimitry Andric Register Reg = Register::index2VirtReg(i); 770b57cec5SDimitry Andric if (MRI->reg_nodbg_empty(Reg)) 780b57cec5SDimitry Andric continue; 790b57cec5SDimitry Andric enqueue(&LIS->getInterval(Reg)); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric // Top-level driver to manage the queue of unassigned VirtRegs and call the 840b57cec5SDimitry Andric // selectOrSplit implementation. 850b57cec5SDimitry Andric void RegAllocBase::allocatePhysRegs() { 860b57cec5SDimitry Andric seedLiveRegs(); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric // Continue assigning vregs one at a time to available physical registers. 8981ad6265SDimitry Andric while (const LiveInterval *VirtReg = dequeue()) { 90e8d8bef9SDimitry Andric assert(!VRM->hasPhys(VirtReg->reg()) && "Register already assigned"); 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric // Unused registers can appear when the spiller coalesces snippets. 93e8d8bef9SDimitry Andric if (MRI->reg_nodbg_empty(VirtReg->reg())) { 940b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n'); 950b57cec5SDimitry Andric aboutToRemoveInterval(*VirtReg); 96e8d8bef9SDimitry Andric LIS->removeInterval(VirtReg->reg()); 970b57cec5SDimitry Andric continue; 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric // Invalidate all interference queries, live ranges could have changed. 1010b57cec5SDimitry Andric Matrix->invalidateVirtRegs(); 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric // selectOrSplit requests the allocator to return an available physical 1040b57cec5SDimitry Andric // register if possible and populate a list of new live intervals that 1050b57cec5SDimitry Andric // result from splitting. 1060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\nselectOrSplit " 107e8d8bef9SDimitry Andric << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg())) 108e8d8bef9SDimitry Andric << ':' << *VirtReg << " w=" << VirtReg->weight() << '\n'); 1090b57cec5SDimitry Andric 1105ffd83dbSDimitry Andric using VirtRegVec = SmallVector<Register, 4>; 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric VirtRegVec SplitVRegs; 113e8d8bef9SDimitry Andric MCRegister AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs); 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric if (AvailablePhysReg == ~0u) { 1160b57cec5SDimitry Andric // selectOrSplit failed to find a register! 1170b57cec5SDimitry Andric // Probably caused by an inline asm. 1180b57cec5SDimitry Andric MachineInstr *MI = nullptr; 119*0fca6ea1SDimitry Andric for (MachineInstr &MIR : MRI->reg_instructions(VirtReg->reg())) { 120*0fca6ea1SDimitry Andric MI = &MIR; 1210b57cec5SDimitry Andric if (MI->isInlineAsm()) 1220b57cec5SDimitry Andric break; 1230b57cec5SDimitry Andric } 124fe6060f1SDimitry Andric 125fe6060f1SDimitry Andric const TargetRegisterClass *RC = MRI->getRegClass(VirtReg->reg()); 126fe6060f1SDimitry Andric ArrayRef<MCPhysReg> AllocOrder = RegClassInfo.getOrder(RC); 127fe6060f1SDimitry Andric if (AllocOrder.empty()) 128fe6060f1SDimitry Andric report_fatal_error("no registers from class available to allocate"); 129fe6060f1SDimitry Andric else if (MI && MI->isInlineAsm()) { 1300b57cec5SDimitry Andric MI->emitError("inline assembly requires more registers than available"); 1310b57cec5SDimitry Andric } else if (MI) { 1320b57cec5SDimitry Andric LLVMContext &Context = 133*0fca6ea1SDimitry Andric MI->getParent()->getParent()->getFunction().getContext(); 1340b57cec5SDimitry Andric Context.emitError("ran out of registers during register allocation"); 1350b57cec5SDimitry Andric } else { 1360b57cec5SDimitry Andric report_fatal_error("ran out of registers during register allocation"); 1370b57cec5SDimitry Andric } 138fe6060f1SDimitry Andric 1390b57cec5SDimitry Andric // Keep going after reporting the error. 140fe6060f1SDimitry Andric VRM->assignVirt2Phys(VirtReg->reg(), AllocOrder.front()); 14181ad6265SDimitry Andric } else if (AvailablePhysReg) 1420b57cec5SDimitry Andric Matrix->assign(*VirtReg, AvailablePhysReg); 1430b57cec5SDimitry Andric 144e8d8bef9SDimitry Andric for (Register Reg : SplitVRegs) { 1450b57cec5SDimitry Andric assert(LIS->hasInterval(Reg)); 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric LiveInterval *SplitVirtReg = &LIS->getInterval(Reg); 148e8d8bef9SDimitry Andric assert(!VRM->hasPhys(SplitVirtReg->reg()) && "Register already assigned"); 149e8d8bef9SDimitry Andric if (MRI->reg_nodbg_empty(SplitVirtReg->reg())) { 1500b57cec5SDimitry Andric assert(SplitVirtReg->empty() && "Non-empty but used interval"); 1510b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n'); 1520b57cec5SDimitry Andric aboutToRemoveInterval(*SplitVirtReg); 153e8d8bef9SDimitry Andric LIS->removeInterval(SplitVirtReg->reg()); 1540b57cec5SDimitry Andric continue; 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n"); 157bdd1243dSDimitry Andric assert(SplitVirtReg->reg().isVirtual() && 1580b57cec5SDimitry Andric "expect split value in virtual register"); 1590b57cec5SDimitry Andric enqueue(SplitVirtReg); 1600b57cec5SDimitry Andric ++NumNewQueued; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric void RegAllocBase::postOptimization() { 1660b57cec5SDimitry Andric spiller().postOptimization(); 167fcaf7f86SDimitry Andric for (auto *DeadInst : DeadRemats) { 1680b57cec5SDimitry Andric LIS->RemoveMachineInstrFromMaps(*DeadInst); 1690b57cec5SDimitry Andric DeadInst->eraseFromParent(); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric DeadRemats.clear(); 1720b57cec5SDimitry Andric } 173fe6060f1SDimitry Andric 17481ad6265SDimitry Andric void RegAllocBase::enqueue(const LiveInterval *LI) { 175fe6060f1SDimitry Andric const Register Reg = LI->reg(); 176fe6060f1SDimitry Andric 177fe6060f1SDimitry Andric assert(Reg.isVirtual() && "Can only enqueue virtual registers"); 178fe6060f1SDimitry Andric 179fe6060f1SDimitry Andric if (VRM->hasPhys(Reg)) 180fe6060f1SDimitry Andric return; 181fe6060f1SDimitry Andric 182*0fca6ea1SDimitry Andric if (shouldAllocateRegister(Reg)) { 183fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "Enqueuing " << printReg(Reg, TRI) << '\n'); 184fe6060f1SDimitry Andric enqueueImpl(LI); 185fe6060f1SDimitry Andric } else { 186fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "Not enqueueing " << printReg(Reg, TRI) 187fe6060f1SDimitry Andric << " in skipped register class\n"); 188fe6060f1SDimitry Andric } 189fe6060f1SDimitry Andric } 190