12cab237bSDimitry Andric //===- RegAllocBase.cpp - Register Allocator Base Class -------------------===//
2dff0c46cSDimitry Andric //
3dff0c46cSDimitry Andric // The LLVM Compiler Infrastructure
4dff0c46cSDimitry Andric //
5dff0c46cSDimitry Andric // This file is distributed under the University of Illinois Open Source
6dff0c46cSDimitry Andric // License. See LICENSE.TXT for details.
7dff0c46cSDimitry Andric //
8dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
9dff0c46cSDimitry Andric //
1091bc56edSDimitry Andric // This file defines the RegAllocBase class which provides common functionality
11dff0c46cSDimitry Andric // for LiveIntervalUnion-based register allocators.
12dff0c46cSDimitry Andric //
13dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
14dff0c46cSDimitry Andric
15dff0c46cSDimitry Andric #include "RegAllocBase.h"
16dff0c46cSDimitry Andric #include "Spiller.h"
172cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h"
18dff0c46cSDimitry Andric #include "llvm/ADT/Statistic.h"
192cab237bSDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
202cab237bSDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
21139f7f9bSDimitry Andric #include "llvm/CodeGen/LiveRegMatrix.h"
22dff0c46cSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
23dff0c46cSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
242cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
25139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
262cab237bSDimitry Andric #include "llvm/Pass.h"
27dff0c46cSDimitry Andric #include "llvm/Support/CommandLine.h"
28dff0c46cSDimitry Andric #include "llvm/Support/Debug.h"
29dff0c46cSDimitry Andric #include "llvm/Support/ErrorHandling.h"
30dff0c46cSDimitry Andric #include "llvm/Support/Timer.h"
31db17bf38SDimitry Andric #include "llvm/Support/raw_ostream.h"
322cab237bSDimitry Andric #include <cassert>
33dff0c46cSDimitry Andric
34dff0c46cSDimitry Andric using namespace llvm;
35dff0c46cSDimitry Andric
3691bc56edSDimitry Andric #define DEBUG_TYPE "regalloc"
3791bc56edSDimitry Andric
38dff0c46cSDimitry Andric STATISTIC(NumNewQueued , "Number of new live ranges queued");
39dff0c46cSDimitry Andric
40dff0c46cSDimitry Andric // Temporary verification option until we can put verification inside
41dff0c46cSDimitry Andric // MachineVerifier.
42dff0c46cSDimitry Andric static cl::opt<bool, true>
43dff0c46cSDimitry Andric VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
442cab237bSDimitry Andric cl::Hidden, cl::desc("Verify during register allocation"));
45dff0c46cSDimitry Andric
46d88c1a5aSDimitry Andric const char RegAllocBase::TimerGroupName[] = "regalloc";
47d88c1a5aSDimitry Andric const char RegAllocBase::TimerGroupDescription[] = "Register Allocation";
48dff0c46cSDimitry Andric bool RegAllocBase::VerifyEnabled = false;
49dff0c46cSDimitry Andric
50dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
51dff0c46cSDimitry Andric // RegAllocBase Implementation
52dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
53dff0c46cSDimitry Andric
54f785676fSDimitry Andric // Pin the vtable to this file.
anchor()55f785676fSDimitry Andric void RegAllocBase::anchor() {}
56f785676fSDimitry Andric
init(VirtRegMap & vrm,LiveIntervals & lis,LiveRegMatrix & mat)577ae0e2c9SDimitry Andric void RegAllocBase::init(VirtRegMap &vrm,
587ae0e2c9SDimitry Andric LiveIntervals &lis,
597ae0e2c9SDimitry Andric LiveRegMatrix &mat) {
60dff0c46cSDimitry Andric TRI = &vrm.getTargetRegInfo();
61dff0c46cSDimitry Andric MRI = &vrm.getRegInfo();
62dff0c46cSDimitry Andric VRM = &vrm;
63dff0c46cSDimitry Andric LIS = &lis;
647ae0e2c9SDimitry Andric Matrix = &mat;
65dff0c46cSDimitry Andric MRI->freezeReservedRegs(vrm.getMachineFunction());
66dff0c46cSDimitry Andric RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
67dff0c46cSDimitry Andric }
68dff0c46cSDimitry Andric
69dff0c46cSDimitry Andric // Visit all the live registers. If they are already assigned to a physical
70dff0c46cSDimitry Andric // register, unify them with the corresponding LiveIntervalUnion, otherwise push
71dff0c46cSDimitry Andric // them on the priority queue for later assignment.
seedLiveRegs()72dff0c46cSDimitry Andric void RegAllocBase::seedLiveRegs() {
73d88c1a5aSDimitry Andric NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName,
74d88c1a5aSDimitry Andric TimerGroupDescription, TimePassesIsEnabled);
757ae0e2c9SDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
767ae0e2c9SDimitry Andric unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
777ae0e2c9SDimitry Andric if (MRI->reg_nodbg_empty(Reg))
787ae0e2c9SDimitry Andric continue;
797ae0e2c9SDimitry Andric enqueue(&LIS->getInterval(Reg));
80dff0c46cSDimitry Andric }
81dff0c46cSDimitry Andric }
82dff0c46cSDimitry Andric
83dff0c46cSDimitry Andric // Top-level driver to manage the queue of unassigned VirtRegs and call the
84dff0c46cSDimitry Andric // selectOrSplit implementation.
allocatePhysRegs()85dff0c46cSDimitry Andric void RegAllocBase::allocatePhysRegs() {
86dff0c46cSDimitry Andric seedLiveRegs();
87dff0c46cSDimitry Andric
88dff0c46cSDimitry Andric // Continue assigning vregs one at a time to available physical registers.
89dff0c46cSDimitry Andric while (LiveInterval *VirtReg = dequeue()) {
90dff0c46cSDimitry Andric assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned");
91dff0c46cSDimitry Andric
92dff0c46cSDimitry Andric // Unused registers can appear when the spiller coalesces snippets.
93dff0c46cSDimitry Andric if (MRI->reg_nodbg_empty(VirtReg->reg)) {
94*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
9539d628a0SDimitry Andric aboutToRemoveInterval(*VirtReg);
96dff0c46cSDimitry Andric LIS->removeInterval(VirtReg->reg);
97dff0c46cSDimitry Andric continue;
98dff0c46cSDimitry Andric }
99dff0c46cSDimitry Andric
100dff0c46cSDimitry Andric // Invalidate all interference queries, live ranges could have changed.
1017ae0e2c9SDimitry Andric Matrix->invalidateVirtRegs();
102dff0c46cSDimitry Andric
103dff0c46cSDimitry Andric // selectOrSplit requests the allocator to return an available physical
104dff0c46cSDimitry Andric // register if possible and populate a list of new live intervals that
105dff0c46cSDimitry Andric // result from splitting.
106*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "\nselectOrSplit "
10739d628a0SDimitry Andric << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg))
10891bc56edSDimitry Andric << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
1092cab237bSDimitry Andric
1102cab237bSDimitry Andric using VirtRegVec = SmallVector<unsigned, 4>;
1112cab237bSDimitry Andric
112dff0c46cSDimitry Andric VirtRegVec SplitVRegs;
113dff0c46cSDimitry Andric unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
114dff0c46cSDimitry Andric
115dff0c46cSDimitry Andric if (AvailablePhysReg == ~0u) {
116dff0c46cSDimitry Andric // selectOrSplit failed to find a register!
117dff0c46cSDimitry Andric // Probably caused by an inline asm.
11891bc56edSDimitry Andric MachineInstr *MI = nullptr;
11991bc56edSDimitry Andric for (MachineRegisterInfo::reg_instr_iterator
12091bc56edSDimitry Andric I = MRI->reg_instr_begin(VirtReg->reg), E = MRI->reg_instr_end();
12191bc56edSDimitry Andric I != E; ) {
12291bc56edSDimitry Andric MachineInstr *TmpMI = &*(I++);
12391bc56edSDimitry Andric if (TmpMI->isInlineAsm()) {
12491bc56edSDimitry Andric MI = TmpMI;
125dff0c46cSDimitry Andric break;
12691bc56edSDimitry Andric }
12791bc56edSDimitry Andric }
128dff0c46cSDimitry Andric if (MI)
129f785676fSDimitry Andric MI->emitError("inline assembly requires more registers than available");
130dff0c46cSDimitry Andric else
131f785676fSDimitry Andric report_fatal_error("ran out of registers during register allocation");
132dff0c46cSDimitry Andric // Keep going after reporting the error.
133dff0c46cSDimitry Andric VRM->assignVirt2Phys(VirtReg->reg,
134dff0c46cSDimitry Andric RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
135dff0c46cSDimitry Andric continue;
136dff0c46cSDimitry Andric }
137dff0c46cSDimitry Andric
138dff0c46cSDimitry Andric if (AvailablePhysReg)
1397ae0e2c9SDimitry Andric Matrix->assign(*VirtReg, AvailablePhysReg);
140dff0c46cSDimitry Andric
14137cd60a3SDimitry Andric for (unsigned Reg : SplitVRegs) {
14237cd60a3SDimitry Andric assert(LIS->hasInterval(Reg));
14337cd60a3SDimitry Andric
14437cd60a3SDimitry Andric LiveInterval *SplitVirtReg = &LIS->getInterval(Reg);
145dff0c46cSDimitry Andric assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
146dff0c46cSDimitry Andric if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
14737cd60a3SDimitry Andric assert(SplitVirtReg->empty() && "Non-empty but used interval");
148*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
14939d628a0SDimitry Andric aboutToRemoveInterval(*SplitVirtReg);
150dff0c46cSDimitry Andric LIS->removeInterval(SplitVirtReg->reg);
151dff0c46cSDimitry Andric continue;
152dff0c46cSDimitry Andric }
153*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
154dff0c46cSDimitry Andric assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
155dff0c46cSDimitry Andric "expect split value in virtual register");
156dff0c46cSDimitry Andric enqueue(SplitVirtReg);
157dff0c46cSDimitry Andric ++NumNewQueued;
158dff0c46cSDimitry Andric }
159dff0c46cSDimitry Andric }
160dff0c46cSDimitry Andric }
1613ca95b02SDimitry Andric
postOptimization()1623ca95b02SDimitry Andric void RegAllocBase::postOptimization() {
1633ca95b02SDimitry Andric spiller().postOptimization();
1643ca95b02SDimitry Andric for (auto DeadInst : DeadRemats) {
1653ca95b02SDimitry Andric LIS->RemoveMachineInstrFromMaps(*DeadInst);
1663ca95b02SDimitry Andric DeadInst->eraseFromParent();
1673ca95b02SDimitry Andric }
1683ca95b02SDimitry Andric DeadRemats.clear();
1693ca95b02SDimitry Andric }
170