blob: 1f824a8fdbf12f6a79c7f608f76688ec5de270cf [file] [log] [blame]
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +00001//===-- RegAllocBase.cpp - Register Allocator Base Class ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the RegAllocBase class which provides comon functionality
11// for LiveIntervalUnion-based register allocators.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "regalloc"
16#include "RegAllocBase.h"
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +000017#include "LiveRegMatrix.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000018#include "Spiller.h"
19#include "VirtRegMap.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000022#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000023#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetRegisterInfo.h"
27#ifndef NDEBUG
28#include "llvm/ADT/SparseBitVector.h"
29#endif
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"
35
36using namespace llvm;
37
38STATISTIC(NumAssigned , "Number of registers assigned");
39STATISTIC(NumUnassigned , "Number of registers unassigned");
40STATISTIC(NumNewQueued , "Number of new live ranges queued");
41
42// Temporary verification option until we can put verification inside
43// MachineVerifier.
44static cl::opt<bool, true>
45VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
46 cl::desc("Verify during register allocation"));
47
48const char *RegAllocBase::TimerGroupName = "Register Allocation";
49bool RegAllocBase::VerifyEnabled = false;
50
51#ifndef NDEBUG
52// Verify each LiveIntervalUnion.
53void RegAllocBase::verify() {
54 LiveVirtRegBitSet VisitedVRegs;
55 OwningArrayPtr<LiveVirtRegBitSet>
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +000056 unionVRegs(new LiveVirtRegBitSet[TRI->getNumRegs()]);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000057
58 // Verify disjoint unions.
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +000059 for (unsigned PhysReg = 0, NumRegs = TRI->getNumRegs(); PhysReg != NumRegs;
60 ++PhysReg) {
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000061 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;
68 }
69
70 // Verify vreg coverage.
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000071 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
72 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
73 if (MRI->reg_nodbg_empty(Reg))
74 continue;
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
78 // non-empty
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";
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000083 llvm_unreachable("unallocated live vreg");
84 }
85 }
86 // FIXME: I'm not sure how to verify spilled intervals.
87}
88#endif //!NDEBUG
89
90//===----------------------------------------------------------------------===//
91// RegAllocBase Implementation
92//===----------------------------------------------------------------------===//
93
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000094void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis) {
95 NamedRegionTimer T("Initialize", TimerGroupName, TimePassesIsEnabled);
96 TRI = &vrm.getTargetRegInfo();
97 MRI = &vrm.getRegInfo();
98 VRM = &vrm;
99 LIS = &lis;
100 MRI->freezeReservedRegs(vrm.getMachineFunction());
101 RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
102
103 const unsigned NumRegs = TRI->getNumRegs();
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000104 if (NumRegs != PhysReg2LiveUnion.size()) {
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000105 PhysReg2LiveUnion.init(UnionAllocator, NumRegs);
106 // Cache an interferece query for each physical reg
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000107 Queries.reset(new LiveIntervalUnion::Query[NumRegs]);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000108 }
109}
110
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000111void RegAllocBase::releaseMemory() {
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000112 for (unsigned r = 0, e = PhysReg2LiveUnion.size(); r != e; ++r)
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000113 PhysReg2LiveUnion[r].clear();
114}
115
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.
119void RegAllocBase::seedLiveRegs() {
120 NamedRegionTimer T("Seed Live Regs", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +0000121 // Physregs.
122 for (unsigned Reg = 1, e = TRI->getNumRegs(); Reg != e; ++Reg) {
123 if (!LIS->hasInterval(Reg))
124 continue;
125 PhysReg2LiveUnion[Reg].unify(LIS->getInterval(Reg));
126 }
127
128 // Virtregs.
129 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
130 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
131 if (MRI->reg_nodbg_empty(Reg))
132 continue;
133 enqueue(&LIS->getInterval(Reg));
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000134 }
135}
136
137void RegAllocBase::assign(LiveInterval &VirtReg, unsigned PhysReg) {
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000138 // FIXME: This diversion is temporary.
139 if (Matrix) {
140 Matrix->assign(VirtReg, PhysReg);
141 return;
142 }
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000143 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);
149 ++NumAssigned;
150}
151
152void RegAllocBase::unassign(LiveInterval &VirtReg, unsigned PhysReg) {
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000153 // FIXME: This diversion is temporary.
154 if (Matrix) {
155 Matrix->unassign(VirtReg);
156 return;
157 }
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000158 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);
163 ++NumUnassigned;
164}
165
166// Top-level driver to manage the queue of unassigned VirtRegs and call the
167// selectOrSplit implementation.
168void RegAllocBase::allocatePhysRegs() {
169 seedLiveRegs();
170
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");
174
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);
179 continue;
180 }
181
182 // Invalidate all interference queries, live ranges could have changed.
183 invalidateVirtRegs();
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000184 if (Matrix)
185 Matrix->invalidateVirtRegs();
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000186
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()
Jakob Stoklund Olesenb77ec7d2012-06-05 22:51:54 +0000192 << ':' << PrintReg(VirtReg->reg) << ' ' << *VirtReg << '\n');
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000193 typedef SmallVector<LiveInterval*, 4> VirtRegVec;
194 VirtRegVec SplitVRegs;
195 unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
196
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.
201 MachineInstr *MI;
202 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(VirtReg->reg);
203 (MI = I.skipInstruction());)
204 if (MI->isInlineAsm())
205 break;
206 if (MI)
207 MI->emitError(Msg);
208 else
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());
213 continue;
214 }
215
216 if (AvailablePhysReg)
217 assign(*VirtReg, AvailablePhysReg);
218
219 for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
220 I != E; ++I) {
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);
226 continue;
227 }
228 DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
229 assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
230 "expect split value in virtual register");
231 enqueue(SplitVirtReg);
232 ++NumNewQueued;
233 }
234 }
235}
236
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.
240unsigned RegAllocBase::checkPhysRegInterference(LiveInterval &VirtReg,
241 unsigned PhysReg) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000242 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
243 if (query(VirtReg, *AI).checkInterference())
244 return *AI;
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000245 return 0;
246}