blob: f4774dcab090923b1418dcc6921a0ce8b73357ea [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 Olesenccc95812012-01-11 22:28:30 +000017#include "Spiller.h"
18#include "VirtRegMap.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000021#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000022#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetRegisterInfo.h"
26#ifndef NDEBUG
27#include "llvm/ADT/SparseBitVector.h"
28#endif
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"
34
35using namespace llvm;
36
37STATISTIC(NumAssigned , "Number of registers assigned");
38STATISTIC(NumUnassigned , "Number of registers unassigned");
39STATISTIC(NumNewQueued , "Number of new live ranges queued");
40
41// Temporary verification option until we can put verification inside
42// MachineVerifier.
43static cl::opt<bool, true>
44VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
45 cl::desc("Verify during register allocation"));
46
47const char *RegAllocBase::TimerGroupName = "Register Allocation";
48bool RegAllocBase::VerifyEnabled = false;
49
50#ifndef NDEBUG
51// Verify each LiveIntervalUnion.
52void RegAllocBase::verify() {
53 LiveVirtRegBitSet VisitedVRegs;
54 OwningArrayPtr<LiveVirtRegBitSet>
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +000055 unionVRegs(new LiveVirtRegBitSet[TRI->getNumRegs()]);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000056
57 // Verify disjoint unions.
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +000058 for (unsigned PhysReg = 0, NumRegs = TRI->getNumRegs(); PhysReg != NumRegs;
59 ++PhysReg) {
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000060 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;
67 }
68
69 // Verify vreg coverage.
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000070 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
71 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
72 if (MRI->reg_nodbg_empty(Reg))
73 continue;
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
77 // non-empty
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";
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000082 llvm_unreachable("unallocated live vreg");
83 }
84 }
85 // FIXME: I'm not sure how to verify spilled intervals.
86}
87#endif //!NDEBUG
88
89//===----------------------------------------------------------------------===//
90// RegAllocBase Implementation
91//===----------------------------------------------------------------------===//
92
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000093void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis) {
94 NamedRegionTimer T("Initialize", TimerGroupName, TimePassesIsEnabled);
95 TRI = &vrm.getTargetRegInfo();
96 MRI = &vrm.getRegInfo();
97 VRM = &vrm;
98 LIS = &lis;
99 MRI->freezeReservedRegs(vrm.getMachineFunction());
100 RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
101
102 const unsigned NumRegs = TRI->getNumRegs();
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000103 if (NumRegs != PhysReg2LiveUnion.size()) {
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000104 PhysReg2LiveUnion.init(UnionAllocator, NumRegs);
105 // Cache an interferece query for each physical reg
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000106 Queries.reset(new LiveIntervalUnion::Query[NumRegs]);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000107 }
108}
109
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000110void RegAllocBase::releaseMemory() {
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000111 for (unsigned r = 0, e = PhysReg2LiveUnion.size(); r != e; ++r)
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000112 PhysReg2LiveUnion[r].clear();
113}
114
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.
118void RegAllocBase::seedLiveRegs() {
119 NamedRegionTimer T("Seed Live Regs", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +0000120 // Physregs.
121 for (unsigned Reg = 1, e = TRI->getNumRegs(); Reg != e; ++Reg) {
122 if (!LIS->hasInterval(Reg))
123 continue;
124 PhysReg2LiveUnion[Reg].unify(LIS->getInterval(Reg));
125 }
126
127 // Virtregs.
128 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
129 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
130 if (MRI->reg_nodbg_empty(Reg))
131 continue;
132 enqueue(&LIS->getInterval(Reg));
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000133 }
134}
135
136void 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);
143 ++NumAssigned;
144}
145
146void 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);
152 ++NumUnassigned;
153}
154
155// Top-level driver to manage the queue of unassigned VirtRegs and call the
156// selectOrSplit implementation.
157void RegAllocBase::allocatePhysRegs() {
158 seedLiveRegs();
159
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");
163
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);
168 continue;
169 }
170
171 // Invalidate all interference queries, live ranges could have changed.
172 invalidateVirtRegs();
173
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()
Jakob Stoklund Olesenb77ec7d2012-06-05 22:51:54 +0000179 << ':' << PrintReg(VirtReg->reg) << ' ' << *VirtReg << '\n');
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000180 typedef SmallVector<LiveInterval*, 4> VirtRegVec;
181 VirtRegVec SplitVRegs;
182 unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
183
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.
188 MachineInstr *MI;
189 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(VirtReg->reg);
190 (MI = I.skipInstruction());)
191 if (MI->isInlineAsm())
192 break;
193 if (MI)
194 MI->emitError(Msg);
195 else
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());
200 continue;
201 }
202
203 if (AvailablePhysReg)
204 assign(*VirtReg, AvailablePhysReg);
205
206 for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
207 I != E; ++I) {
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);
213 continue;
214 }
215 DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
216 assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
217 "expect split value in virtual register");
218 enqueue(SplitVirtReg);
219 ++NumNewQueued;
220 }
221 }
222}
223
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.
227unsigned RegAllocBase::checkPhysRegInterference(LiveInterval &VirtReg,
228 unsigned PhysReg) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000229 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
230 if (query(VirtReg, *AI).checkInterference())
231 return *AI;
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000232 return 0;
233}