1044eb2f6SDimitry Andric //===- RegAllocBase.cpp - Register Allocator Base Class -------------------===//
263faed5bSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
663faed5bSDimitry Andric //
763faed5bSDimitry Andric //===----------------------------------------------------------------------===//
863faed5bSDimitry Andric //
95ca98fd9SDimitry Andric // This file defines the RegAllocBase class which provides common functionality
1063faed5bSDimitry Andric // for LiveIntervalUnion-based register allocators.
1163faed5bSDimitry Andric //
1263faed5bSDimitry Andric //===----------------------------------------------------------------------===//
1363faed5bSDimitry Andric
1463faed5bSDimitry Andric #include "RegAllocBase.h"
15044eb2f6SDimitry Andric #include "llvm/ADT/SmallVector.h"
1663faed5bSDimitry Andric #include "llvm/ADT/Statistic.h"
17044eb2f6SDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
18044eb2f6SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
194a16efa3SDimitry Andric #include "llvm/CodeGen/LiveRegMatrix.h"
2063faed5bSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
21e6d15924SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
2263faed5bSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
23cfca06d7SDimitry Andric #include "llvm/CodeGen/Spiller.h"
24044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
254a16efa3SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
26ac9a064cSDimitry Andric #include "llvm/IR/Module.h"
27044eb2f6SDimitry Andric #include "llvm/Pass.h"
2863faed5bSDimitry Andric #include "llvm/Support/CommandLine.h"
2963faed5bSDimitry Andric #include "llvm/Support/Debug.h"
3063faed5bSDimitry Andric #include "llvm/Support/ErrorHandling.h"
3163faed5bSDimitry Andric #include "llvm/Support/Timer.h"
327ab83427SDimitry Andric #include "llvm/Support/raw_ostream.h"
33044eb2f6SDimitry Andric #include <cassert>
3463faed5bSDimitry Andric
3563faed5bSDimitry Andric using namespace llvm;
3663faed5bSDimitry Andric
375ca98fd9SDimitry Andric #define DEBUG_TYPE "regalloc"
385ca98fd9SDimitry Andric
3963faed5bSDimitry Andric STATISTIC(NumNewQueued, "Number of new live ranges queued");
4063faed5bSDimitry Andric
4163faed5bSDimitry Andric // Temporary verification option until we can put verification inside
4263faed5bSDimitry Andric // MachineVerifier.
4363faed5bSDimitry Andric static cl::opt<bool, true>
4463faed5bSDimitry Andric VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
45044eb2f6SDimitry Andric cl::Hidden, cl::desc("Verify during register allocation"));
4663faed5bSDimitry Andric
47b915e9e0SDimitry Andric const char RegAllocBase::TimerGroupName[] = "regalloc";
48b915e9e0SDimitry Andric const char RegAllocBase::TimerGroupDescription[] = "Register Allocation";
4963faed5bSDimitry Andric bool RegAllocBase::VerifyEnabled = false;
5063faed5bSDimitry Andric
5163faed5bSDimitry Andric //===----------------------------------------------------------------------===//
5263faed5bSDimitry Andric // RegAllocBase Implementation
5363faed5bSDimitry Andric //===----------------------------------------------------------------------===//
5463faed5bSDimitry Andric
55f8af5cf6SDimitry Andric // Pin the vtable to this file.
anchor()56f8af5cf6SDimitry Andric void RegAllocBase::anchor() {}
57f8af5cf6SDimitry Andric
init(VirtRegMap & vrm,LiveIntervals & lis,LiveRegMatrix & mat)58344a3780SDimitry Andric void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis,
5958b69754SDimitry Andric LiveRegMatrix &mat) {
6063faed5bSDimitry Andric TRI = &vrm.getTargetRegInfo();
6163faed5bSDimitry Andric MRI = &vrm.getRegInfo();
6263faed5bSDimitry Andric VRM = &vrm;
6363faed5bSDimitry Andric LIS = &lis;
6458b69754SDimitry Andric Matrix = &mat;
65ac9a064cSDimitry Andric MRI->freezeReservedRegs();
6663faed5bSDimitry Andric RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
6763faed5bSDimitry Andric }
6863faed5bSDimitry Andric
6963faed5bSDimitry Andric // Visit all the live registers. If they are already assigned to a physical
7063faed5bSDimitry Andric // register, unify them with the corresponding LiveIntervalUnion, otherwise push
7163faed5bSDimitry Andric // them on the priority queue for later assignment.
seedLiveRegs()7263faed5bSDimitry Andric void RegAllocBase::seedLiveRegs() {
73b915e9e0SDimitry Andric NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName,
74b915e9e0SDimitry Andric TimerGroupDescription, TimePassesIsEnabled);
7558b69754SDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
76b60736ecSDimitry Andric Register Reg = Register::index2VirtReg(i);
7758b69754SDimitry Andric if (MRI->reg_nodbg_empty(Reg))
7858b69754SDimitry Andric continue;
7958b69754SDimitry Andric enqueue(&LIS->getInterval(Reg));
8063faed5bSDimitry Andric }
8163faed5bSDimitry Andric }
8263faed5bSDimitry Andric
8363faed5bSDimitry Andric // Top-level driver to manage the queue of unassigned VirtRegs and call the
8463faed5bSDimitry Andric // selectOrSplit implementation.
allocatePhysRegs()8563faed5bSDimitry Andric void RegAllocBase::allocatePhysRegs() {
8663faed5bSDimitry Andric seedLiveRegs();
8763faed5bSDimitry Andric
8863faed5bSDimitry Andric // Continue assigning vregs one at a time to available physical registers.
89145449b1SDimitry Andric while (const LiveInterval *VirtReg = dequeue()) {
90b60736ecSDimitry Andric assert(!VRM->hasPhys(VirtReg->reg()) && "Register already assigned");
9163faed5bSDimitry Andric
9263faed5bSDimitry Andric // Unused registers can appear when the spiller coalesces snippets.
93b60736ecSDimitry Andric if (MRI->reg_nodbg_empty(VirtReg->reg())) {
94eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
9567c32a98SDimitry Andric aboutToRemoveInterval(*VirtReg);
96b60736ecSDimitry Andric LIS->removeInterval(VirtReg->reg());
9763faed5bSDimitry Andric continue;
9863faed5bSDimitry Andric }
9963faed5bSDimitry Andric
10063faed5bSDimitry Andric // Invalidate all interference queries, live ranges could have changed.
10158b69754SDimitry Andric Matrix->invalidateVirtRegs();
10263faed5bSDimitry Andric
10363faed5bSDimitry Andric // selectOrSplit requests the allocator to return an available physical
10463faed5bSDimitry Andric // register if possible and populate a list of new live intervals that
10563faed5bSDimitry Andric // result from splitting.
106eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "\nselectOrSplit "
107b60736ecSDimitry Andric << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg()))
108b60736ecSDimitry Andric << ':' << *VirtReg << " w=" << VirtReg->weight() << '\n');
109044eb2f6SDimitry Andric
110cfca06d7SDimitry Andric using VirtRegVec = SmallVector<Register, 4>;
111044eb2f6SDimitry Andric
11263faed5bSDimitry Andric VirtRegVec SplitVRegs;
113b60736ecSDimitry Andric MCRegister AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
11463faed5bSDimitry Andric
11563faed5bSDimitry Andric if (AvailablePhysReg == ~0u) {
11663faed5bSDimitry Andric // selectOrSplit failed to find a register!
11763faed5bSDimitry Andric // Probably caused by an inline asm.
1185ca98fd9SDimitry Andric MachineInstr *MI = nullptr;
119ac9a064cSDimitry Andric for (MachineInstr &MIR : MRI->reg_instructions(VirtReg->reg())) {
120ac9a064cSDimitry Andric MI = &MIR;
121e6d15924SDimitry Andric if (MI->isInlineAsm())
12263faed5bSDimitry Andric break;
1235ca98fd9SDimitry Andric }
124344a3780SDimitry Andric
125344a3780SDimitry Andric const TargetRegisterClass *RC = MRI->getRegClass(VirtReg->reg());
126344a3780SDimitry Andric ArrayRef<MCPhysReg> AllocOrder = RegClassInfo.getOrder(RC);
127344a3780SDimitry Andric if (AllocOrder.empty())
128344a3780SDimitry Andric report_fatal_error("no registers from class available to allocate");
129344a3780SDimitry Andric else if (MI && MI->isInlineAsm()) {
130f8af5cf6SDimitry Andric MI->emitError("inline assembly requires more registers than available");
131e6d15924SDimitry Andric } else if (MI) {
132e6d15924SDimitry Andric LLVMContext &Context =
133ac9a064cSDimitry Andric MI->getParent()->getParent()->getFunction().getContext();
134e6d15924SDimitry Andric Context.emitError("ran out of registers during register allocation");
135e6d15924SDimitry Andric } else {
136f8af5cf6SDimitry Andric report_fatal_error("ran out of registers during register allocation");
137e6d15924SDimitry Andric }
138344a3780SDimitry Andric
13963faed5bSDimitry Andric // Keep going after reporting the error.
140344a3780SDimitry Andric VRM->assignVirt2Phys(VirtReg->reg(), AllocOrder.front());
141145449b1SDimitry Andric } else if (AvailablePhysReg)
14258b69754SDimitry Andric Matrix->assign(*VirtReg, AvailablePhysReg);
14363faed5bSDimitry Andric
144b60736ecSDimitry Andric for (Register Reg : SplitVRegs) {
1453ad6a4b4SDimitry Andric assert(LIS->hasInterval(Reg));
1463ad6a4b4SDimitry Andric
1473ad6a4b4SDimitry Andric LiveInterval *SplitVirtReg = &LIS->getInterval(Reg);
148b60736ecSDimitry Andric assert(!VRM->hasPhys(SplitVirtReg->reg()) && "Register already assigned");
149b60736ecSDimitry Andric if (MRI->reg_nodbg_empty(SplitVirtReg->reg())) {
1503ad6a4b4SDimitry Andric assert(SplitVirtReg->empty() && "Non-empty but used interval");
151eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
15267c32a98SDimitry Andric aboutToRemoveInterval(*SplitVirtReg);
153b60736ecSDimitry Andric LIS->removeInterval(SplitVirtReg->reg());
15463faed5bSDimitry Andric continue;
15563faed5bSDimitry Andric }
156eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
157e3b55780SDimitry Andric assert(SplitVirtReg->reg().isVirtual() &&
15863faed5bSDimitry Andric "expect split value in virtual register");
15963faed5bSDimitry Andric enqueue(SplitVirtReg);
16063faed5bSDimitry Andric ++NumNewQueued;
16163faed5bSDimitry Andric }
16263faed5bSDimitry Andric }
16363faed5bSDimitry Andric }
16401095a5dSDimitry Andric
postOptimization()16501095a5dSDimitry Andric void RegAllocBase::postOptimization() {
16601095a5dSDimitry Andric spiller().postOptimization();
1674b4fe385SDimitry Andric for (auto *DeadInst : DeadRemats) {
16801095a5dSDimitry Andric LIS->RemoveMachineInstrFromMaps(*DeadInst);
16901095a5dSDimitry Andric DeadInst->eraseFromParent();
17001095a5dSDimitry Andric }
17101095a5dSDimitry Andric DeadRemats.clear();
17201095a5dSDimitry Andric }
173344a3780SDimitry Andric
enqueue(const LiveInterval * LI)174145449b1SDimitry Andric void RegAllocBase::enqueue(const LiveInterval *LI) {
175344a3780SDimitry Andric const Register Reg = LI->reg();
176344a3780SDimitry Andric
177344a3780SDimitry Andric assert(Reg.isVirtual() && "Can only enqueue virtual registers");
178344a3780SDimitry Andric
179344a3780SDimitry Andric if (VRM->hasPhys(Reg))
180344a3780SDimitry Andric return;
181344a3780SDimitry Andric
182ac9a064cSDimitry Andric if (shouldAllocateRegister(Reg)) {
183344a3780SDimitry Andric LLVM_DEBUG(dbgs() << "Enqueuing " << printReg(Reg, TRI) << '\n');
184344a3780SDimitry Andric enqueueImpl(LI);
185344a3780SDimitry Andric } else {
186344a3780SDimitry Andric LLVM_DEBUG(dbgs() << "Not enqueueing " << printReg(Reg, TRI)
187344a3780SDimitry Andric << " in skipped register class\n");
188344a3780SDimitry Andric }
189344a3780SDimitry Andric }
190