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"
17 #include "LiveRegMatrix.h"
19 #include "VirtRegMap.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
22 #include "llvm/CodeGen/LiveRangeEdit.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetRegisterInfo.h"
28 #include "llvm/ADT/SparseBitVector.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Support/Timer.h"
38 STATISTIC(NumAssigned , "Number of registers assigned");
39 STATISTIC(NumUnassigned , "Number of registers unassigned");
40 STATISTIC(NumNewQueued , "Number of new live ranges queued");
42 // Temporary verification option until we can put verification inside
44 static cl::opt<bool, true>
45 VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
46 cl::desc("Verify during register allocation"));
48 const char *RegAllocBase::TimerGroupName = "Register Allocation";
49 bool RegAllocBase::VerifyEnabled = false;
52 // Verify each LiveIntervalUnion.
53 void RegAllocBase::verify() {
54 LiveVirtRegBitSet VisitedVRegs;
55 OwningArrayPtr<LiveVirtRegBitSet>
56 unionVRegs(new LiveVirtRegBitSet[TRI->getNumRegs()]);
58 // Verify disjoint unions.
59 for (unsigned PhysReg = 0, NumRegs = TRI->getNumRegs(); PhysReg != NumRegs;
61 DEBUG(PhysReg2LiveUnion[PhysReg].print(dbgs(), TRI));
62 LiveVirtRegBitSet &VRegs = unionVRegs[PhysReg];
63 PhysReg2LiveUnion[PhysReg].verify(VRegs);
64 // Union + intersection test could be done efficiently in one pass, but
65 // don't add a method to SparseBitVector unless we really need it.
66 assert(!VisitedVRegs.intersects(VRegs) && "vreg in multiple unions");
67 VisitedVRegs |= VRegs;
70 // Verify vreg coverage.
71 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
72 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
73 if (MRI->reg_nodbg_empty(Reg))
75 if (!VRM->hasPhys(Reg)) continue; // spilled?
76 LiveInterval &LI = LIS->getInterval(Reg);
77 if (LI.empty()) continue; // unionVRegs will only be filled if li is
79 unsigned PhysReg = VRM->getPhys(Reg);
80 if (!unionVRegs[PhysReg].test(Reg)) {
81 dbgs() << "LiveVirtReg " << PrintReg(Reg, TRI) << " not in union "
82 << TRI->getName(PhysReg) << "\n";
83 llvm_unreachable("unallocated live vreg");
86 // FIXME: I'm not sure how to verify spilled intervals.
90 //===----------------------------------------------------------------------===//
91 // RegAllocBase Implementation
92 //===----------------------------------------------------------------------===//
94 void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis) {
95 NamedRegionTimer T("Initialize", TimerGroupName, TimePassesIsEnabled);
96 TRI = &vrm.getTargetRegInfo();
97 MRI = &vrm.getRegInfo();
100 MRI->freezeReservedRegs(vrm.getMachineFunction());
101 RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
103 const unsigned NumRegs = TRI->getNumRegs();
104 if (NumRegs != PhysReg2LiveUnion.size()) {
105 PhysReg2LiveUnion.init(UnionAllocator, NumRegs);
106 // Cache an interferece query for each physical reg
107 Queries.reset(new LiveIntervalUnion::Query[NumRegs]);
111 void RegAllocBase::releaseMemory() {
112 for (unsigned r = 0, e = PhysReg2LiveUnion.size(); r != e; ++r)
113 PhysReg2LiveUnion[r].clear();
116 // Visit all the live registers. If they are already assigned to a physical
117 // register, unify them with the corresponding LiveIntervalUnion, otherwise push
118 // them on the priority queue for later assignment.
119 void RegAllocBase::seedLiveRegs() {
120 NamedRegionTimer T("Seed Live Regs", TimerGroupName, TimePassesIsEnabled);
122 for (unsigned Reg = 1, e = TRI->getNumRegs(); Reg != e; ++Reg) {
123 if (!LIS->hasInterval(Reg))
125 PhysReg2LiveUnion[Reg].unify(LIS->getInterval(Reg));
129 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
130 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
131 if (MRI->reg_nodbg_empty(Reg))
133 enqueue(&LIS->getInterval(Reg));
137 void RegAllocBase::assign(LiveInterval &VirtReg, unsigned PhysReg) {
138 // FIXME: This diversion is temporary.
140 Matrix->assign(VirtReg, PhysReg);
143 DEBUG(dbgs() << "assigning " << PrintReg(VirtReg.reg, TRI)
144 << " to " << PrintReg(PhysReg, TRI) << '\n');
145 assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
146 VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
147 MRI->setPhysRegUsed(PhysReg);
148 PhysReg2LiveUnion[PhysReg].unify(VirtReg);
152 void RegAllocBase::unassign(LiveInterval &VirtReg, unsigned PhysReg) {
153 // FIXME: This diversion is temporary.
155 Matrix->unassign(VirtReg);
158 DEBUG(dbgs() << "unassigning " << PrintReg(VirtReg.reg, TRI)
159 << " from " << PrintReg(PhysReg, TRI) << '\n');
160 assert(VRM->getPhys(VirtReg.reg) == PhysReg && "Inconsistent unassign");
161 PhysReg2LiveUnion[PhysReg].extract(VirtReg);
162 VRM->clearVirt(VirtReg.reg);
166 // Top-level driver to manage the queue of unassigned VirtRegs and call the
167 // selectOrSplit implementation.
168 void RegAllocBase::allocatePhysRegs() {
171 // Continue assigning vregs one at a time to available physical registers.
172 while (LiveInterval *VirtReg = dequeue()) {
173 assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned");
175 // Unused registers can appear when the spiller coalesces snippets.
176 if (MRI->reg_nodbg_empty(VirtReg->reg)) {
177 DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
178 LIS->removeInterval(VirtReg->reg);
182 // Invalidate all interference queries, live ranges could have changed.
183 invalidateVirtRegs();
185 Matrix->invalidateVirtRegs();
187 // selectOrSplit requests the allocator to return an available physical
188 // register if possible and populate a list of new live intervals that
189 // result from splitting.
190 DEBUG(dbgs() << "\nselectOrSplit "
191 << MRI->getRegClass(VirtReg->reg)->getName()
192 << ':' << PrintReg(VirtReg->reg) << ' ' << *VirtReg << '\n');
193 typedef SmallVector<LiveInterval*, 4> VirtRegVec;
194 VirtRegVec SplitVRegs;
195 unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
197 if (AvailablePhysReg == ~0u) {
198 // selectOrSplit failed to find a register!
199 const char *Msg = "ran out of registers during register allocation";
200 // Probably caused by an inline asm.
202 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(VirtReg->reg);
203 (MI = I.skipInstruction());)
204 if (MI->isInlineAsm())
209 report_fatal_error(Msg);
210 // Keep going after reporting the error.
211 VRM->assignVirt2Phys(VirtReg->reg,
212 RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
216 if (AvailablePhysReg)
217 assign(*VirtReg, AvailablePhysReg);
219 for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
221 LiveInterval *SplitVirtReg = *I;
222 assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
223 if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
224 DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
225 LIS->removeInterval(SplitVirtReg->reg);
228 DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
229 assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
230 "expect split value in virtual register");
231 enqueue(SplitVirtReg);
237 // Check if this live virtual register interferes with a physical register. If
238 // not, then check for interference on each register that aliases with the
239 // physical register. Return the interfering register.
240 unsigned RegAllocBase::checkPhysRegInterference(LiveInterval &VirtReg,
242 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
243 if (query(VirtReg, *AI).checkInterference())