1 //===-- RegAllocBase.cpp - Register Allocator Base Class ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RegAllocBase class which provides comon functionality
11 // for LiveIntervalUnion-based register allocators.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "regalloc"
16 #include "RegAllocBase.h"
18 #include "VirtRegMap.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
21 #include "llvm/CodeGen/LiveRangeEdit.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetRegisterInfo.h"
27 #include "llvm/ADT/SparseBitVector.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Support/Timer.h"
37 STATISTIC(NumAssigned , "Number of registers assigned");
38 STATISTIC(NumUnassigned , "Number of registers unassigned");
39 STATISTIC(NumNewQueued , "Number of new live ranges queued");
41 // Temporary verification option until we can put verification inside
43 static cl::opt<bool, true>
44 VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
45 cl::desc("Verify during register allocation"));
47 const char *RegAllocBase::TimerGroupName = "Register Allocation";
48 bool RegAllocBase::VerifyEnabled = false;
51 // Verify each LiveIntervalUnion.
52 void RegAllocBase::verify() {
53 LiveVirtRegBitSet VisitedVRegs;
54 OwningArrayPtr<LiveVirtRegBitSet>
55 unionVRegs(new LiveVirtRegBitSet[TRI->getNumRegs()]);
57 // Verify disjoint unions.
58 for (unsigned PhysReg = 0, NumRegs = TRI->getNumRegs(); PhysReg != NumRegs;
60 DEBUG(PhysReg2LiveUnion[PhysReg].print(dbgs(), TRI));
61 LiveVirtRegBitSet &VRegs = unionVRegs[PhysReg];
62 PhysReg2LiveUnion[PhysReg].verify(VRegs);
63 // Union + intersection test could be done efficiently in one pass, but
64 // don't add a method to SparseBitVector unless we really need it.
65 assert(!VisitedVRegs.intersects(VRegs) && "vreg in multiple unions");
66 VisitedVRegs |= VRegs;
69 // Verify vreg coverage.
70 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
71 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
72 if (MRI->reg_nodbg_empty(Reg))
74 if (!VRM->hasPhys(Reg)) continue; // spilled?
75 LiveInterval &LI = LIS->getInterval(Reg);
76 if (LI.empty()) continue; // unionVRegs will only be filled if li is
78 unsigned PhysReg = VRM->getPhys(Reg);
79 if (!unionVRegs[PhysReg].test(Reg)) {
80 dbgs() << "LiveVirtReg " << PrintReg(Reg, TRI) << " not in union "
81 << TRI->getName(PhysReg) << "\n";
82 llvm_unreachable("unallocated live vreg");
85 // FIXME: I'm not sure how to verify spilled intervals.
89 //===----------------------------------------------------------------------===//
90 // RegAllocBase Implementation
91 //===----------------------------------------------------------------------===//
93 void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis) {
94 NamedRegionTimer T("Initialize", TimerGroupName, TimePassesIsEnabled);
95 TRI = &vrm.getTargetRegInfo();
96 MRI = &vrm.getRegInfo();
99 MRI->freezeReservedRegs(vrm.getMachineFunction());
100 RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
102 const unsigned NumRegs = TRI->getNumRegs();
103 if (NumRegs != PhysReg2LiveUnion.size()) {
104 PhysReg2LiveUnion.init(UnionAllocator, NumRegs);
105 // Cache an interferece query for each physical reg
106 Queries.reset(new LiveIntervalUnion::Query[NumRegs]);
110 void RegAllocBase::releaseMemory() {
111 for (unsigned r = 0, e = PhysReg2LiveUnion.size(); r != e; ++r)
112 PhysReg2LiveUnion[r].clear();
115 // Visit all the live registers. If they are already assigned to a physical
116 // register, unify them with the corresponding LiveIntervalUnion, otherwise push
117 // them on the priority queue for later assignment.
118 void RegAllocBase::seedLiveRegs() {
119 NamedRegionTimer T("Seed Live Regs", TimerGroupName, TimePassesIsEnabled);
121 for (unsigned Reg = 1, e = TRI->getNumRegs(); Reg != e; ++Reg) {
122 if (!LIS->hasInterval(Reg))
124 PhysReg2LiveUnion[Reg].unify(LIS->getInterval(Reg));
128 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
129 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
130 if (MRI->reg_nodbg_empty(Reg))
132 enqueue(&LIS->getInterval(Reg));
136 void RegAllocBase::assign(LiveInterval &VirtReg, unsigned PhysReg) {
137 DEBUG(dbgs() << "assigning " << PrintReg(VirtReg.reg, TRI)
138 << " to " << PrintReg(PhysReg, TRI) << '\n');
139 assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
140 VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
141 MRI->setPhysRegUsed(PhysReg);
142 PhysReg2LiveUnion[PhysReg].unify(VirtReg);
146 void RegAllocBase::unassign(LiveInterval &VirtReg, unsigned PhysReg) {
147 DEBUG(dbgs() << "unassigning " << PrintReg(VirtReg.reg, TRI)
148 << " from " << PrintReg(PhysReg, TRI) << '\n');
149 assert(VRM->getPhys(VirtReg.reg) == PhysReg && "Inconsistent unassign");
150 PhysReg2LiveUnion[PhysReg].extract(VirtReg);
151 VRM->clearVirt(VirtReg.reg);
155 // Top-level driver to manage the queue of unassigned VirtRegs and call the
156 // selectOrSplit implementation.
157 void RegAllocBase::allocatePhysRegs() {
160 // Continue assigning vregs one at a time to available physical registers.
161 while (LiveInterval *VirtReg = dequeue()) {
162 assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned");
164 // Unused registers can appear when the spiller coalesces snippets.
165 if (MRI->reg_nodbg_empty(VirtReg->reg)) {
166 DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
167 LIS->removeInterval(VirtReg->reg);
171 // Invalidate all interference queries, live ranges could have changed.
172 invalidateVirtRegs();
174 // selectOrSplit requests the allocator to return an available physical
175 // register if possible and populate a list of new live intervals that
176 // result from splitting.
177 DEBUG(dbgs() << "\nselectOrSplit "
178 << MRI->getRegClass(VirtReg->reg)->getName()
179 << ':' << PrintReg(VirtReg->reg) << ' ' << *VirtReg << '\n');
180 typedef SmallVector<LiveInterval*, 4> VirtRegVec;
181 VirtRegVec SplitVRegs;
182 unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
184 if (AvailablePhysReg == ~0u) {
185 // selectOrSplit failed to find a register!
186 const char *Msg = "ran out of registers during register allocation";
187 // Probably caused by an inline asm.
189 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(VirtReg->reg);
190 (MI = I.skipInstruction());)
191 if (MI->isInlineAsm())
196 report_fatal_error(Msg);
197 // Keep going after reporting the error.
198 VRM->assignVirt2Phys(VirtReg->reg,
199 RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
203 if (AvailablePhysReg)
204 assign(*VirtReg, AvailablePhysReg);
206 for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
208 LiveInterval *SplitVirtReg = *I;
209 assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
210 if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
211 DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
212 LIS->removeInterval(SplitVirtReg->reg);
215 DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
216 assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
217 "expect split value in virtual register");
218 enqueue(SplitVirtReg);
224 // Check if this live virtual register interferes with a physical register. If
225 // not, then check for interference on each register that aliases with the
226 // physical register. Return the interfering register.
227 unsigned RegAllocBase::checkPhysRegInterference(LiveInterval &VirtReg,
229 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
230 if (query(VirtReg, *AI).checkInterference())