blob: c0d4d8146a8189e1deb1de88ade9338e19095259 [file] [log] [blame]
Andrew Trick14e8d712010-10-22 23:09:15 +00001//===-- RegAllocBasic.cpp - basic register allocator ----------------------===//
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 RABasic function pass, which provides a minimal
11// implementation of the basic register allocator.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "regalloc"
Andrew Tricke16eecc2010-10-26 18:34:01 +000016#include "LiveIntervalUnion.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000017#include "RegAllocBase.h"
18#include "RenderMachineFunction.h"
19#include "Spiller.h"
Andrew Tricke141a492010-11-08 18:02:08 +000020#include "VirtRegMap.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000021#include "VirtRegRewriter.h"
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000022#include "llvm/ADT/OwningPtr.h"
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000023#include "llvm/ADT/Statistic.h"
Andrew Trick8a83d542010-11-11 17:46:29 +000024#include "llvm/Analysis/AliasAnalysis.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000025#include "llvm/Function.h"
26#include "llvm/PassAnalysisSupport.h"
27#include "llvm/CodeGen/CalcSpillWeights.h"
Andrew Tricke141a492010-11-08 18:02:08 +000028#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000029#include "llvm/CodeGen/LiveStackAnalysis.h"
30#include "llvm/CodeGen/MachineFunctionPass.h"
31#include "llvm/CodeGen/MachineInstr.h"
32#include "llvm/CodeGen/MachineLoopInfo.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
34#include "llvm/CodeGen/Passes.h"
35#include "llvm/CodeGen/RegAllocRegistry.h"
36#include "llvm/CodeGen/RegisterCoalescer.h"
37#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetOptions.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000039#include "llvm/Target/TargetRegisterInfo.h"
Andrew Trick071d1c02010-11-09 21:04:34 +000040#ifndef NDEBUG
41#include "llvm/ADT/SparseBitVector.h"
42#endif
Andrew Tricke141a492010-11-08 18:02:08 +000043#include "llvm/Support/Debug.h"
44#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +000046#include "llvm/Support/Timer.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000047
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000048#include <cstdlib>
Andrew Tricke16eecc2010-10-26 18:34:01 +000049
Andrew Trick14e8d712010-10-22 23:09:15 +000050using namespace llvm;
51
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000052STATISTIC(NumAssigned , "Number of registers assigned");
53STATISTIC(NumUnassigned , "Number of registers unassigned");
54STATISTIC(NumNewQueued , "Number of new live ranges queued");
55
Andrew Trick14e8d712010-10-22 23:09:15 +000056static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
57 createBasicRegisterAllocator);
58
Andrew Trick071d1c02010-11-09 21:04:34 +000059// Temporary verification option until we can put verification inside
60// MachineVerifier.
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +000061static cl::opt<bool, true>
62VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
63 cl::desc("Verify during register allocation"));
Andrew Trick071d1c02010-11-09 21:04:34 +000064
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +000065const char *RegAllocBase::TimerGroupName = "Register Allocation";
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +000066bool RegAllocBase::VerifyEnabled = false;
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +000067
Benjamin Kramerc62feda2010-11-25 16:42:51 +000068namespace {
Andrew Trick14e8d712010-10-22 23:09:15 +000069/// RABasic provides a minimal implementation of the basic register allocation
70/// algorithm. It prioritizes live virtual registers by spill weight and spills
71/// whenever a register is unavailable. This is not practical in production but
72/// provides a useful baseline both for measuring other allocators and comparing
73/// the speed of the basic algorithm against other styles of allocators.
74class RABasic : public MachineFunctionPass, public RegAllocBase
75{
76 // context
Andrew Trick18c57a82010-11-30 23:18:47 +000077 MachineFunction *MF;
Andrew Trick18c57a82010-11-30 23:18:47 +000078 BitVector ReservedRegs;
Andrew Trick14e8d712010-10-22 23:09:15 +000079
80 // analyses
Andrew Trick18c57a82010-11-30 23:18:47 +000081 LiveStacks *LS;
82 RenderMachineFunction *RMF;
Andrew Trick14e8d712010-10-22 23:09:15 +000083
84 // state
Andrew Trick18c57a82010-11-30 23:18:47 +000085 std::auto_ptr<Spiller> SpillerInstance;
Andrew Trick14e8d712010-10-22 23:09:15 +000086
87public:
88 RABasic();
89
90 /// Return the pass name.
91 virtual const char* getPassName() const {
92 return "Basic Register Allocator";
93 }
94
95 /// RABasic analysis usage.
Andrew Trick18c57a82010-11-30 23:18:47 +000096 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Andrew Trick14e8d712010-10-22 23:09:15 +000097
98 virtual void releaseMemory();
99
Andrew Trick18c57a82010-11-30 23:18:47 +0000100 virtual Spiller &spiller() { return *SpillerInstance; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000101
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000102 virtual float getPriority(LiveInterval *LI) { return LI->weight; }
103
Andrew Trick18c57a82010-11-30 23:18:47 +0000104 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
105 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000106
107 /// Perform register allocation.
108 virtual bool runOnMachineFunction(MachineFunction &mf);
109
110 static char ID;
111};
112
113char RABasic::ID = 0;
114
115} // end anonymous namespace
116
Andrew Trick14e8d712010-10-22 23:09:15 +0000117RABasic::RABasic(): MachineFunctionPass(ID) {
118 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
119 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
120 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
121 initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
122 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
123 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen964bc252010-11-03 20:39:26 +0000124 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000125 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
126 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
127 initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry());
128}
129
Andrew Trick18c57a82010-11-30 23:18:47 +0000130void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
131 AU.setPreservesCFG();
132 AU.addRequired<AliasAnalysis>();
133 AU.addPreserved<AliasAnalysis>();
134 AU.addRequired<LiveIntervals>();
135 AU.addPreserved<SlotIndexes>();
Andrew Trick14e8d712010-10-22 23:09:15 +0000136 if (StrongPHIElim)
Andrew Trick18c57a82010-11-30 23:18:47 +0000137 AU.addRequiredID(StrongPHIEliminationID);
138 AU.addRequiredTransitive<RegisterCoalescer>();
139 AU.addRequired<CalculateSpillWeights>();
140 AU.addRequired<LiveStacks>();
141 AU.addPreserved<LiveStacks>();
142 AU.addRequiredID(MachineDominatorsID);
143 AU.addPreservedID(MachineDominatorsID);
144 AU.addRequired<MachineLoopInfo>();
145 AU.addPreserved<MachineLoopInfo>();
146 AU.addRequired<VirtRegMap>();
147 AU.addPreserved<VirtRegMap>();
148 DEBUG(AU.addRequired<RenderMachineFunction>());
149 MachineFunctionPass::getAnalysisUsage(AU);
Andrew Trick14e8d712010-10-22 23:09:15 +0000150}
151
152void RABasic::releaseMemory() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000153 SpillerInstance.reset(0);
Andrew Trick14e8d712010-10-22 23:09:15 +0000154 RegAllocBase::releaseMemory();
155}
156
Andrew Trick071d1c02010-11-09 21:04:34 +0000157#ifndef NDEBUG
158// Verify each LiveIntervalUnion.
159void RegAllocBase::verify() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000160 LiveVirtRegBitSet VisitedVRegs;
161 OwningArrayPtr<LiveVirtRegBitSet>
162 unionVRegs(new LiveVirtRegBitSet[PhysReg2LiveUnion.numRegs()]);
163
Andrew Trick071d1c02010-11-09 21:04:34 +0000164 // Verify disjoint unions.
Andrew Trick18c57a82010-11-30 23:18:47 +0000165 for (unsigned PhysReg = 0; PhysReg < PhysReg2LiveUnion.numRegs(); ++PhysReg) {
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +0000166 DEBUG(PhysReg2LiveUnion[PhysReg].print(dbgs(), TRI));
Andrew Trick18c57a82010-11-30 23:18:47 +0000167 LiveVirtRegBitSet &VRegs = unionVRegs[PhysReg];
168 PhysReg2LiveUnion[PhysReg].verify(VRegs);
Andrew Trick071d1c02010-11-09 21:04:34 +0000169 // Union + intersection test could be done efficiently in one pass, but
170 // don't add a method to SparseBitVector unless we really need it.
Andrew Trick18c57a82010-11-30 23:18:47 +0000171 assert(!VisitedVRegs.intersects(VRegs) && "vreg in multiple unions");
172 VisitedVRegs |= VRegs;
Andrew Trick071d1c02010-11-09 21:04:34 +0000173 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000174
Andrew Trick071d1c02010-11-09 21:04:34 +0000175 // Verify vreg coverage.
Andrew Trick18c57a82010-11-30 23:18:47 +0000176 for (LiveIntervals::iterator liItr = LIS->begin(), liEnd = LIS->end();
Andrew Trick071d1c02010-11-09 21:04:34 +0000177 liItr != liEnd; ++liItr) {
178 unsigned reg = liItr->first;
Andrew Trick071d1c02010-11-09 21:04:34 +0000179 if (TargetRegisterInfo::isPhysicalRegister(reg)) continue;
Andrew Trick18c57a82010-11-30 23:18:47 +0000180 if (!VRM->hasPhys(reg)) continue; // spilled?
181 unsigned PhysReg = VRM->getPhys(reg);
182 if (!unionVRegs[PhysReg].test(reg)) {
Andrew Trick071d1c02010-11-09 21:04:34 +0000183 dbgs() << "LiveVirtReg " << reg << " not in union " <<
Andrew Trick18c57a82010-11-30 23:18:47 +0000184 TRI->getName(PhysReg) << "\n";
Andrew Trick071d1c02010-11-09 21:04:34 +0000185 llvm_unreachable("unallocated live vreg");
186 }
187 }
188 // FIXME: I'm not sure how to verify spilled intervals.
189}
190#endif //!NDEBUG
191
Andrew Trick14e8d712010-10-22 23:09:15 +0000192//===----------------------------------------------------------------------===//
193// RegAllocBase Implementation
194//===----------------------------------------------------------------------===//
195
196// Instantiate a LiveIntervalUnion for each physical register.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000197void RegAllocBase::LiveUnionArray::init(LiveIntervalUnion::Allocator &allocator,
198 unsigned NRegs) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000199 NumRegs = NRegs;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000200 Array =
201 static_cast<LiveIntervalUnion*>(malloc(sizeof(LiveIntervalUnion)*NRegs));
202 for (unsigned r = 0; r != NRegs; ++r)
203 new(Array + r) LiveIntervalUnion(r, allocator);
Andrew Trick14e8d712010-10-22 23:09:15 +0000204}
205
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +0000206void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis) {
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000207 NamedRegionTimer T("Initialize", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +0000208 TRI = &vrm.getTargetRegInfo();
209 MRI = &vrm.getRegInfo();
Andrew Trick18c57a82010-11-30 23:18:47 +0000210 VRM = &vrm;
211 LIS = &lis;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000212 PhysReg2LiveUnion.init(UnionAllocator, TRI->getNumRegs());
Andrew Tricke141a492010-11-08 18:02:08 +0000213 // Cache an interferece query for each physical reg
Andrew Trick18c57a82010-11-30 23:18:47 +0000214 Queries.reset(new LiveIntervalUnion::Query[PhysReg2LiveUnion.numRegs()]);
Andrew Trick14e8d712010-10-22 23:09:15 +0000215}
216
Andrew Trick18c57a82010-11-30 23:18:47 +0000217void RegAllocBase::LiveUnionArray::clear() {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000218 if (!Array)
219 return;
220 for (unsigned r = 0; r != NumRegs; ++r)
221 Array[r].~LiveIntervalUnion();
222 free(Array);
Andrew Trick18c57a82010-11-30 23:18:47 +0000223 NumRegs = 0;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000224 Array = 0;
Andrew Trick14e8d712010-10-22 23:09:15 +0000225}
226
227void RegAllocBase::releaseMemory() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000228 PhysReg2LiveUnion.clear();
Andrew Trick14e8d712010-10-22 23:09:15 +0000229}
230
Andrew Tricke16eecc2010-10-26 18:34:01 +0000231// Visit all the live virtual registers. If they are already assigned to a
232// physical register, unify them with the corresponding LiveIntervalUnion,
233// otherwise push them on the priority queue for later assignment.
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000234void RegAllocBase::
235seedLiveVirtRegs(std::priority_queue<std::pair<float, unsigned> > &VirtRegQ) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000236 for (LiveIntervals::iterator I = LIS->begin(), E = LIS->end(); I != E; ++I) {
237 unsigned RegNum = I->first;
238 LiveInterval &VirtReg = *I->second;
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000239 if (TargetRegisterInfo::isPhysicalRegister(RegNum))
Andrew Trick18c57a82010-11-30 23:18:47 +0000240 PhysReg2LiveUnion[RegNum].unify(VirtReg);
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000241 else
242 VirtRegQ.push(std::make_pair(getPriority(&VirtReg), RegNum));
Andrew Tricke16eecc2010-10-26 18:34:01 +0000243 }
244}
245
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000246void RegAllocBase::assign(LiveInterval &VirtReg, unsigned PhysReg) {
247 assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
248 VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
249 PhysReg2LiveUnion[PhysReg].unify(VirtReg);
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000250 ++NumAssigned;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000251}
252
253void RegAllocBase::unassign(LiveInterval &VirtReg, unsigned PhysReg) {
254 assert(VRM->getPhys(VirtReg.reg) == PhysReg && "Inconsistent unassign");
255 PhysReg2LiveUnion[PhysReg].extract(VirtReg);
256 VRM->clearVirt(VirtReg.reg);
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000257 ++NumUnassigned;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000258}
259
Andrew Trick18c57a82010-11-30 23:18:47 +0000260// Top-level driver to manage the queue of unassigned VirtRegs and call the
Andrew Tricke16eecc2010-10-26 18:34:01 +0000261// selectOrSplit implementation.
262void RegAllocBase::allocatePhysRegs() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000263
264 // Push each vreg onto a queue or "precolor" by adding it to a physreg union.
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000265 std::priority_queue<std::pair<float, unsigned> > VirtRegQ;
Andrew Trick18c57a82010-11-30 23:18:47 +0000266 seedLiveVirtRegs(VirtRegQ);
267
268 // Continue assigning vregs one at a time to available physical registers.
269 while (!VirtRegQ.empty()) {
270 // Pop the highest priority vreg.
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000271 LiveInterval &VirtReg = LIS->getInterval(VirtRegQ.top().second);
272 VirtRegQ.pop();
Andrew Trick18c57a82010-11-30 23:18:47 +0000273
274 // selectOrSplit requests the allocator to return an available physical
275 // register if possible and populate a list of new live intervals that
276 // result from splitting.
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +0000277 DEBUG(dbgs() << "\nselectOrSplit " << MRI->getRegClass(VirtReg.reg)->getName()
278 << ':' << VirtReg << '\n');
Andrew Trick18c57a82010-11-30 23:18:47 +0000279 typedef SmallVector<LiveInterval*, 4> VirtRegVec;
280 VirtRegVec SplitVRegs;
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000281 unsigned AvailablePhysReg = selectOrSplit(VirtReg, SplitVRegs);
Andrew Trick18c57a82010-11-30 23:18:47 +0000282
283 if (AvailablePhysReg) {
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +0000284 DEBUG(dbgs() << "allocating: " << TRI->getName(AvailablePhysReg)
285 << " for " << VirtReg << '\n');
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000286 assign(VirtReg, AvailablePhysReg);
Andrew Tricke16eecc2010-10-26 18:34:01 +0000287 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000288 for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
289 I != E; ++I) {
290 LiveInterval* SplitVirtReg = *I;
291 if (SplitVirtReg->empty()) continue;
292 DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
293 assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
Andrew Tricke141a492010-11-08 18:02:08 +0000294 "expect split value in virtual register");
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000295 VirtRegQ.push(std::make_pair(getPriority(SplitVirtReg),
296 SplitVirtReg->reg));
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000297 ++NumNewQueued;
Andrew Tricke16eecc2010-10-26 18:34:01 +0000298 }
299 }
300}
301
Andrew Trick18c57a82010-11-30 23:18:47 +0000302// Check if this live virtual register interferes with a physical register. If
303// not, then check for interference on each register that aliases with the
304// physical register. Return the interfering register.
305unsigned RegAllocBase::checkPhysRegInterference(LiveInterval &VirtReg,
306 unsigned PhysReg) {
Jakob Stoklund Olesen16999da2010-12-14 23:10:48 +0000307 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI)
Andrew Trick18c57a82010-11-30 23:18:47 +0000308 if (query(VirtReg, *AliasI).checkInterference())
309 return *AliasI;
Andrew Tricke141a492010-11-08 18:02:08 +0000310 return 0;
311}
312
Andrew Trick18c57a82010-11-30 23:18:47 +0000313// Helper for spillInteferences() that spills all interfering vregs currently
314// assigned to this physical register.
315void RegAllocBase::spillReg(LiveInterval& VirtReg, unsigned PhysReg,
316 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
317 LiveIntervalUnion::Query &Q = query(VirtReg, PhysReg);
318 assert(Q.seenAllInterferences() && "need collectInterferences()");
319 const SmallVectorImpl<LiveInterval*> &PendingSpills = Q.interferingVRegs();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000320
Andrew Trick18c57a82010-11-30 23:18:47 +0000321 for (SmallVectorImpl<LiveInterval*>::const_iterator I = PendingSpills.begin(),
322 E = PendingSpills.end(); I != E; ++I) {
323 LiveInterval &SpilledVReg = **I;
Andrew Trick8a83d542010-11-11 17:46:29 +0000324 DEBUG(dbgs() << "extracting from " <<
Andrew Trick18c57a82010-11-30 23:18:47 +0000325 TRI->getName(PhysReg) << " " << SpilledVReg << '\n');
Andrew Trick13bdbb02010-11-20 02:43:55 +0000326
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000327 // Deallocate the interfering vreg by removing it from the union.
328 // A LiveInterval instance may not be in a union during modification!
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000329 unassign(SpilledVReg, PhysReg);
Andrew Trick13bdbb02010-11-20 02:43:55 +0000330
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000331 // Spill the extracted interval.
Andrew Trick18c57a82010-11-30 23:18:47 +0000332 spiller().spill(&SpilledVReg, SplitVRegs, PendingSpills);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000333 }
Andrew Trick8a83d542010-11-11 17:46:29 +0000334 // After extracting segments, the query's results are invalid. But keep the
335 // contents valid until we're done accessing pendingSpills.
336 Q.clear();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000337}
338
Andrew Trick18c57a82010-11-30 23:18:47 +0000339// Spill or split all live virtual registers currently unified under PhysReg
340// that interfere with VirtReg. The newly spilled or split live intervals are
341// returned by appending them to SplitVRegs.
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000342bool
Andrew Trick18c57a82010-11-30 23:18:47 +0000343RegAllocBase::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
344 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000345 // Record each interference and determine if all are spillable before mutating
346 // either the union or live intervals.
Jakob Stoklund Olesen16999da2010-12-14 23:10:48 +0000347 unsigned NumInterferences = 0;
Andrew Trick8a83d542010-11-11 17:46:29 +0000348 // Collect interferences assigned to any alias of the physical register.
Jakob Stoklund Olesen16999da2010-12-14 23:10:48 +0000349 for (const unsigned *asI = TRI->getOverlaps(PhysReg); *asI; ++asI) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000350 LiveIntervalUnion::Query &QAlias = query(VirtReg, *asI);
351 NumInterferences += QAlias.collectInterferingVRegs();
Andrew Trick8a83d542010-11-11 17:46:29 +0000352 if (QAlias.seenUnspillableVReg()) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000353 return false;
354 }
355 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000356 DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) <<
357 " interferences with " << VirtReg << "\n");
358 assert(NumInterferences > 0 && "expect interference");
Andrew Trick13bdbb02010-11-20 02:43:55 +0000359
Andrew Trick18c57a82010-11-30 23:18:47 +0000360 // Spill each interfering vreg allocated to PhysReg or an alias.
Jakob Stoklund Olesen16999da2010-12-14 23:10:48 +0000361 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI)
Andrew Trick18c57a82010-11-30 23:18:47 +0000362 spillReg(VirtReg, *AliasI, SplitVRegs);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000363 return true;
Andrew Trick14e8d712010-10-22 23:09:15 +0000364}
365
Jakob Stoklund Olesen1b19dc12010-12-08 01:06:06 +0000366// Add newly allocated physical registers to the MBB live in sets.
367void RegAllocBase::addMBBLiveIns(MachineFunction *MF) {
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000368 NamedRegionTimer T("MBB Live Ins", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen1b19dc12010-12-08 01:06:06 +0000369 typedef SmallVector<MachineBasicBlock*, 8> MBBVec;
370 MBBVec liveInMBBs;
371 MachineBasicBlock &entryMBB = *MF->begin();
372
373 for (unsigned PhysReg = 0; PhysReg < PhysReg2LiveUnion.numRegs(); ++PhysReg) {
374 LiveIntervalUnion &LiveUnion = PhysReg2LiveUnion[PhysReg];
375 if (LiveUnion.empty())
376 continue;
377 for (LiveIntervalUnion::SegmentIter SI = LiveUnion.begin(); SI.valid();
378 ++SI) {
379
380 // Find the set of basic blocks which this range is live into...
381 liveInMBBs.clear();
382 if (!LIS->findLiveInMBBs(SI.start(), SI.stop(), liveInMBBs)) continue;
383
384 // And add the physreg for this interval to their live-in sets.
385 for (MBBVec::iterator I = liveInMBBs.begin(), E = liveInMBBs.end();
386 I != E; ++I) {
387 MachineBasicBlock *MBB = *I;
388 if (MBB == &entryMBB) continue;
389 if (MBB->isLiveIn(PhysReg)) continue;
390 MBB->addLiveIn(PhysReg);
391 }
392 }
393 }
394}
395
396
Andrew Trick14e8d712010-10-22 23:09:15 +0000397//===----------------------------------------------------------------------===//
398// RABasic Implementation
399//===----------------------------------------------------------------------===//
400
401// Driver for the register assignment and splitting heuristics.
402// Manages iteration over the LiveIntervalUnions.
Andrew Trick13bdbb02010-11-20 02:43:55 +0000403//
Andrew Trick18c57a82010-11-30 23:18:47 +0000404// This is a minimal implementation of register assignment and splitting that
405// spills whenever we run out of registers.
Andrew Trick14e8d712010-10-22 23:09:15 +0000406//
407// selectOrSplit can only be called once per live virtual register. We then do a
408// single interference test for each register the correct class until we find an
409// available register. So, the number of interference tests in the worst case is
410// |vregs| * |machineregs|. And since the number of interference tests is
Andrew Trick18c57a82010-11-30 23:18:47 +0000411// minimal, there is no value in caching them outside the scope of
412// selectOrSplit().
413unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
414 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000415 // Populate a list of physical register spill candidates.
Andrew Trick18c57a82010-11-30 23:18:47 +0000416 SmallVector<unsigned, 8> PhysRegSpillCands;
Andrew Tricke141a492010-11-08 18:02:08 +0000417
Andrew Trick13bdbb02010-11-20 02:43:55 +0000418 // Check for an available register in this class.
Andrew Trick18c57a82010-11-30 23:18:47 +0000419 const TargetRegisterClass *TRC = MRI->getRegClass(VirtReg.reg);
Andrew Trick13bdbb02010-11-20 02:43:55 +0000420
Andrew Trick18c57a82010-11-30 23:18:47 +0000421 for (TargetRegisterClass::iterator I = TRC->allocation_order_begin(*MF),
422 E = TRC->allocation_order_end(*MF);
423 I != E; ++I) {
424
425 unsigned PhysReg = *I;
426 if (ReservedRegs.test(PhysReg)) continue;
427
428 // Check interference and as a side effect, intialize queries for this
429 // VirtReg and its aliases.
430 unsigned interfReg = checkPhysRegInterference(VirtReg, PhysReg);
Andrew Tricke141a492010-11-08 18:02:08 +0000431 if (interfReg == 0) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000432 // Found an available register.
Andrew Trick18c57a82010-11-30 23:18:47 +0000433 return PhysReg;
Andrew Trick14e8d712010-10-22 23:09:15 +0000434 }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000435 LiveInterval *interferingVirtReg =
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000436 Queries[interfReg].firstInterference().liveUnionPos().value();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000437
Andrew Trickb853e6c2010-12-09 18:15:21 +0000438 // The current VirtReg must either be spillable, or one of its interferences
Andrew Trick18c57a82010-11-30 23:18:47 +0000439 // must have less spill weight.
440 if (interferingVirtReg->weight < VirtReg.weight ) {
441 PhysRegSpillCands.push_back(PhysReg);
Andrew Tricke141a492010-11-08 18:02:08 +0000442 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000443 }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000444 // Try to spill another interfering reg with less spill weight.
Andrew Trick18c57a82010-11-30 23:18:47 +0000445 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
446 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000447
Andrew Trick18c57a82010-11-30 23:18:47 +0000448 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) continue;
Andrew Trick13bdbb02010-11-20 02:43:55 +0000449
Jakob Stoklund Olesen2b38c512010-12-07 18:51:27 +0000450 assert(checkPhysRegInterference(VirtReg, *PhysRegI) == 0 &&
451 "Interference after spill.");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000452 // Tell the caller to allocate to this newly freed physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +0000453 return *PhysRegI;
Andrew Tricke141a492010-11-08 18:02:08 +0000454 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000455 // No other spill candidates were found, so spill the current VirtReg.
456 DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000457 SmallVector<LiveInterval*, 1> pendingSpills;
Andrew Trick18c57a82010-11-30 23:18:47 +0000458
459 spiller().spill(&VirtReg, SplitVRegs, pendingSpills);
Andrew Trick13bdbb02010-11-20 02:43:55 +0000460
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000461 // The live virtual register requesting allocation was spilled, so tell
462 // the caller not to allocate anything during this round.
463 return 0;
Andrew Tricke141a492010-11-08 18:02:08 +0000464}
Andrew Trick14e8d712010-10-22 23:09:15 +0000465
Andrew Trick14e8d712010-10-22 23:09:15 +0000466bool RABasic::runOnMachineFunction(MachineFunction &mf) {
467 DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
468 << "********** Function: "
469 << ((Value*)mf.getFunction())->getName() << '\n');
470
Andrew Trick18c57a82010-11-30 23:18:47 +0000471 MF = &mf;
Andrew Trick18c57a82010-11-30 23:18:47 +0000472 DEBUG(RMF = &getAnalysis<RenderMachineFunction>());
Andrew Trick8a83d542010-11-11 17:46:29 +0000473
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +0000474 RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
Andrew Trick14e8d712010-10-22 23:09:15 +0000475
Andrew Trick18c57a82010-11-30 23:18:47 +0000476 ReservedRegs = TRI->getReservedRegs(*MF);
Andrew Trick8a83d542010-11-11 17:46:29 +0000477
Andrew Trick18c57a82010-11-30 23:18:47 +0000478 SpillerInstance.reset(createSpiller(*this, *MF, *VRM));
Andrew Trick13bdbb02010-11-20 02:43:55 +0000479
Andrew Tricke16eecc2010-10-26 18:34:01 +0000480 allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000481
Jakob Stoklund Olesen1b19dc12010-12-08 01:06:06 +0000482 addMBBLiveIns(MF);
Andrew Trick316df4b2010-11-20 02:57:05 +0000483
Andrew Trick14e8d712010-10-22 23:09:15 +0000484 // Diagnostic output before rewriting
Andrew Trick18c57a82010-11-30 23:18:47 +0000485 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
Andrew Trick14e8d712010-10-22 23:09:15 +0000486
487 // optional HTML output
Andrew Trick18c57a82010-11-30 23:18:47 +0000488 DEBUG(RMF->renderMachineFunction("After basic register allocation.", VRM));
Andrew Trick14e8d712010-10-22 23:09:15 +0000489
Andrew Trick071d1c02010-11-09 21:04:34 +0000490 // FIXME: Verification currently must run before VirtRegRewriter. We should
491 // make the rewriter a separate pass and override verifyAnalysis instead. When
492 // that happens, verification naturally falls under VerifyMachineCode.
493#ifndef NDEBUG
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +0000494 if (VerifyEnabled) {
Andrew Trick071d1c02010-11-09 21:04:34 +0000495 // Verify accuracy of LiveIntervals. The standard machine code verifier
496 // ensures that each LiveIntervals covers all uses of the virtual reg.
497
Andrew Trick18c57a82010-11-30 23:18:47 +0000498 // FIXME: MachineVerifier is badly broken when using the standard
499 // spiller. Always use -spiller=inline with -verify-regalloc. Even with the
500 // inline spiller, some tests fail to verify because the coalescer does not
501 // always generate verifiable code.
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000502 MF->verify(this, "In RABasic::verify");
Andrew Trick13bdbb02010-11-20 02:43:55 +0000503
Andrew Trick071d1c02010-11-09 21:04:34 +0000504 // Verify that LiveIntervals are partitioned into unions and disjoint within
505 // the unions.
506 verify();
507 }
508#endif // !NDEBUG
Andrew Trick13bdbb02010-11-20 02:43:55 +0000509
Andrew Trick14e8d712010-10-22 23:09:15 +0000510 // Run rewriter
511 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
Andrew Trick18c57a82010-11-30 23:18:47 +0000512 rewriter->runOnMachineFunction(*MF, *VRM, LIS);
Andrew Tricke16eecc2010-10-26 18:34:01 +0000513
514 // The pass output is in VirtRegMap. Release all the transient data.
515 releaseMemory();
Andrew Trick13bdbb02010-11-20 02:43:55 +0000516
Andrew Trick14e8d712010-10-22 23:09:15 +0000517 return true;
518}
519
Andrew Trick13bdbb02010-11-20 02:43:55 +0000520FunctionPass* llvm::createBasicRegisterAllocator()
Andrew Trick14e8d712010-10-22 23:09:15 +0000521{
522 return new RABasic();
523}