blob: 6aa4c0e5a32b935541e1037ad0ece1d463580967 [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"
22#include "llvm/Function.h"
23#include "llvm/PassAnalysisSupport.h"
24#include "llvm/CodeGen/CalcSpillWeights.h"
Andrew Tricke141a492010-11-08 18:02:08 +000025#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000026#include "llvm/CodeGen/LiveStackAnalysis.h"
27#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/CodeGen/MachineLoopInfo.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/CodeGen/Passes.h"
32#include "llvm/CodeGen/RegAllocRegistry.h"
33#include "llvm/CodeGen/RegisterCoalescer.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetOptions.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000036#include "llvm/Target/TargetRegisterInfo.h"
Andrew Trick071d1c02010-11-09 21:04:34 +000037#ifndef NDEBUG
38#include "llvm/ADT/SparseBitVector.h"
39#endif
Andrew Tricke141a492010-11-08 18:02:08 +000040#include "llvm/Support/Debug.h"
41#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/raw_ostream.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000043
44#include <vector>
45#include <queue>
46
Andrew Trick14e8d712010-10-22 23:09:15 +000047using namespace llvm;
48
49static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
50 createBasicRegisterAllocator);
51
Andrew Trick071d1c02010-11-09 21:04:34 +000052// Temporary verification option until we can put verification inside
53// MachineVerifier.
54static cl::opt<bool>
55VerifyRegAlloc("verify-regalloc",
56 cl::desc("Verify live intervals before renaming"));
57
58class PhysicalRegisterDescription : public AbstractRegisterDescription {
59 const TargetRegisterInfo *tri_;
60public:
61 PhysicalRegisterDescription(const TargetRegisterInfo *tri): tri_(tri) {}
62 virtual const char *getName(unsigned reg) const { return tri_->getName(reg); }
63};
64
Andrew Trick14e8d712010-10-22 23:09:15 +000065namespace {
66
67/// RABasic provides a minimal implementation of the basic register allocation
68/// algorithm. It prioritizes live virtual registers by spill weight and spills
69/// whenever a register is unavailable. This is not practical in production but
70/// provides a useful baseline both for measuring other allocators and comparing
71/// the speed of the basic algorithm against other styles of allocators.
72class RABasic : public MachineFunctionPass, public RegAllocBase
73{
74 // context
75 MachineFunction *mf_;
76 const TargetMachine *tm_;
77 MachineRegisterInfo *mri_;
78
79 // analyses
80 LiveStacks *ls_;
81 RenderMachineFunction *rmf_;
82
83 // state
84 std::auto_ptr<Spiller> spiller_;
85
86public:
87 RABasic();
88
89 /// Return the pass name.
90 virtual const char* getPassName() const {
91 return "Basic Register Allocator";
92 }
93
94 /// RABasic analysis usage.
95 virtual void getAnalysisUsage(AnalysisUsage &au) const;
96
97 virtual void releaseMemory();
98
Andrew Trickf4baeaf2010-11-10 19:18:47 +000099 virtual Spiller &spiller() { return *spiller_; }
100
Andrew Tricke16eecc2010-10-26 18:34:01 +0000101 virtual unsigned selectOrSplit(LiveInterval &lvr,
102 SmallVectorImpl<LiveInterval*> &splitLVRs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000103
104 /// Perform register allocation.
105 virtual bool runOnMachineFunction(MachineFunction &mf);
106
107 static char ID;
108};
109
110char RABasic::ID = 0;
111
112} // end anonymous namespace
113
114// We should not need to publish the initializer as long as no other passes
115// require RABasic.
116#if 0 // disable INITIALIZE_PASS
117INITIALIZE_PASS_BEGIN(RABasic, "basic-regalloc",
118 "Basic Register Allocator", false, false)
119INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
120INITIALIZE_PASS_DEPENDENCY(StrongPHIElimination)
121INITIALIZE_AG_DEPENDENCY(RegisterCoalescer)
122INITIALIZE_PASS_DEPENDENCY(CalculateSpillWeights)
123INITIALIZE_PASS_DEPENDENCY(LiveStacks)
124INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
125INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
126#ifndef NDEBUG
127INITIALIZE_PASS_DEPENDENCY(RenderMachineFunction)
128#endif
129INITIALIZE_PASS_END(RABasic, "basic-regalloc",
130 "Basic Register Allocator", false, false)
Andrew Tricke16eecc2010-10-26 18:34:01 +0000131#endif // disable INITIALIZE_PASS
Andrew Trick14e8d712010-10-22 23:09:15 +0000132
133RABasic::RABasic(): MachineFunctionPass(ID) {
134 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
135 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
136 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
137 initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
138 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
139 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen964bc252010-11-03 20:39:26 +0000140 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000141 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
142 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
143 initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry());
144}
145
146void RABasic::getAnalysisUsage(AnalysisUsage &au) const {
147 au.setPreservesCFG();
148 au.addRequired<LiveIntervals>();
149 au.addPreserved<SlotIndexes>();
150 if (StrongPHIElim)
151 au.addRequiredID(StrongPHIEliminationID);
152 au.addRequiredTransitive<RegisterCoalescer>();
153 au.addRequired<CalculateSpillWeights>();
154 au.addRequired<LiveStacks>();
155 au.addPreserved<LiveStacks>();
Jakob Stoklund Olesen964bc252010-11-03 20:39:26 +0000156 au.addRequiredID(MachineDominatorsID);
157 au.addPreservedID(MachineDominatorsID);
Andrew Trick14e8d712010-10-22 23:09:15 +0000158 au.addRequired<MachineLoopInfo>();
159 au.addPreserved<MachineLoopInfo>();
160 au.addRequired<VirtRegMap>();
161 au.addPreserved<VirtRegMap>();
162 DEBUG(au.addRequired<RenderMachineFunction>());
163 MachineFunctionPass::getAnalysisUsage(au);
164}
165
166void RABasic::releaseMemory() {
167 spiller_.reset(0);
168 RegAllocBase::releaseMemory();
169}
170
Andrew Trick071d1c02010-11-09 21:04:34 +0000171#ifndef NDEBUG
172// Verify each LiveIntervalUnion.
173void RegAllocBase::verify() {
174 LvrBitSet visitedVRegs;
175 OwningArrayPtr<LvrBitSet> unionVRegs(new LvrBitSet[physReg2liu_.numRegs()]);
176 // Verify disjoint unions.
177 for (unsigned preg = 0; preg < physReg2liu_.numRegs(); ++preg) {
178 DEBUG(PhysicalRegisterDescription prd(tri_); physReg2liu_[preg].dump(&prd));
179 LvrBitSet &vregs = unionVRegs[preg];
180 physReg2liu_[preg].verify(vregs);
181 // Union + intersection test could be done efficiently in one pass, but
182 // don't add a method to SparseBitVector unless we really need it.
183 assert(!visitedVRegs.intersects(vregs) && "vreg in multiple unions");
184 visitedVRegs |= vregs;
185 }
186 // Verify vreg coverage.
187 for (LiveIntervals::iterator liItr = lis_->begin(), liEnd = lis_->end();
188 liItr != liEnd; ++liItr) {
189 unsigned reg = liItr->first;
190 LiveInterval &li = *liItr->second;
191 if (li.empty() ) continue;
192 if (TargetRegisterInfo::isPhysicalRegister(reg)) continue;
193 if (!vrm_->hasPhys(reg)) continue; // spilled?
194 unsigned preg = vrm_->getPhys(reg);
195 if (!unionVRegs[preg].test(reg)) {
196 dbgs() << "LiveVirtReg " << reg << " not in union " <<
197 tri_->getName(preg) << "\n";
198 llvm_unreachable("unallocated live vreg");
199 }
200 }
201 // FIXME: I'm not sure how to verify spilled intervals.
202}
203#endif //!NDEBUG
204
Andrew Trick14e8d712010-10-22 23:09:15 +0000205//===----------------------------------------------------------------------===//
206// RegAllocBase Implementation
207//===----------------------------------------------------------------------===//
208
209// Instantiate a LiveIntervalUnion for each physical register.
210void RegAllocBase::LIUArray::init(unsigned nRegs) {
211 array_.reset(new LiveIntervalUnion[nRegs]);
212 nRegs_ = nRegs;
213 for (unsigned pr = 0; pr < nRegs; ++pr) {
214 array_[pr].init(pr);
215 }
216}
217
218void RegAllocBase::init(const TargetRegisterInfo &tri, VirtRegMap &vrm,
219 LiveIntervals &lis) {
220 tri_ = &tri;
221 vrm_ = &vrm;
222 lis_ = &lis;
223 physReg2liu_.init(tri_->getNumRegs());
Andrew Tricke141a492010-11-08 18:02:08 +0000224 // Cache an interferece query for each physical reg
225 queries_.reset(new LiveIntervalUnion::Query[physReg2liu_.numRegs()]);
Andrew Trick14e8d712010-10-22 23:09:15 +0000226}
227
228void RegAllocBase::LIUArray::clear() {
229 nRegs_ = 0;
230 array_.reset(0);
231}
232
233void RegAllocBase::releaseMemory() {
234 physReg2liu_.clear();
235}
236
Andrew Tricke16eecc2010-10-26 18:34:01 +0000237namespace llvm {
238/// This class defines a queue of live virtual registers prioritized by spill
239/// weight. The heaviest vreg is popped first.
240///
241/// Currently, this is trivial wrapper that gives us an opaque type in the
242/// header, but we may later give it a virtual interface for register allocators
243/// to override the priority queue comparator.
244class LiveVirtRegQueue {
245 typedef std::priority_queue
246 <LiveInterval*, std::vector<LiveInterval*>, LessSpillWeightPriority> PQ;
247 PQ pq_;
248
249public:
250 // Is the queue empty?
251 bool empty() { return pq_.empty(); }
252
253 // Get the highest priority lvr (top + pop)
254 LiveInterval *get() {
255 LiveInterval *lvr = pq_.top();
256 pq_.pop();
257 return lvr;
258 }
259 // Add this lvr to the queue
260 void push(LiveInterval *lvr) {
261 pq_.push(lvr);
262 }
263};
264} // end namespace llvm
265
266// Visit all the live virtual registers. If they are already assigned to a
267// physical register, unify them with the corresponding LiveIntervalUnion,
268// otherwise push them on the priority queue for later assignment.
269void RegAllocBase::seedLiveVirtRegs(LiveVirtRegQueue &lvrQ) {
270 for (LiveIntervals::iterator liItr = lis_->begin(), liEnd = lis_->end();
271 liItr != liEnd; ++liItr) {
272 unsigned reg = liItr->first;
273 LiveInterval &li = *liItr->second;
Andrew Trick071d1c02010-11-09 21:04:34 +0000274 if (li.empty()) continue;
Andrew Tricke16eecc2010-10-26 18:34:01 +0000275 if (TargetRegisterInfo::isPhysicalRegister(reg)) {
276 physReg2liu_[reg].unify(li);
277 }
278 else {
279 lvrQ.push(&li);
280 }
281 }
282}
283
284// Top-level driver to manage the queue of unassigned LiveVirtRegs and call the
285// selectOrSplit implementation.
286void RegAllocBase::allocatePhysRegs() {
287 LiveVirtRegQueue lvrQ;
288 seedLiveVirtRegs(lvrQ);
289 while (!lvrQ.empty()) {
290 LiveInterval *lvr = lvrQ.get();
291 typedef SmallVector<LiveInterval*, 4> LVRVec;
292 LVRVec splitLVRs;
293 unsigned availablePhysReg = selectOrSplit(*lvr, splitLVRs);
294 if (availablePhysReg) {
Andrew Tricke141a492010-11-08 18:02:08 +0000295 DEBUG(dbgs() << "allocating: " << tri_->getName(availablePhysReg) <<
Andrew Trick071d1c02010-11-09 21:04:34 +0000296 " " << *lvr << '\n');
Andrew Tricke16eecc2010-10-26 18:34:01 +0000297 assert(!vrm_->hasPhys(lvr->reg) && "duplicate vreg in interval unions");
298 vrm_->assignVirt2Phys(lvr->reg, availablePhysReg);
299 physReg2liu_[availablePhysReg].unify(*lvr);
300 }
Andrew Tricke141a492010-11-08 18:02:08 +0000301 for (LVRVec::iterator lvrI = splitLVRs.begin(), lvrEnd = splitLVRs.end();
302 lvrI != lvrEnd; ++lvrI) {
Andrew Trick071d1c02010-11-09 21:04:34 +0000303 if ((*lvrI)->empty()) continue;
Andrew Tricke141a492010-11-08 18:02:08 +0000304 DEBUG(dbgs() << "queuing new interval: " << **lvrI << "\n");
305 assert(TargetRegisterInfo::isVirtualRegister((*lvrI)->reg) &&
306 "expect split value in virtual register");
307 lvrQ.push(*lvrI);
Andrew Tricke16eecc2010-10-26 18:34:01 +0000308 }
309 }
310}
311
Andrew Trick14e8d712010-10-22 23:09:15 +0000312// Check if this live virtual reg interferes with a physical register. If not,
313// then check for interference on each register that aliases with the physical
Andrew Tricke141a492010-11-08 18:02:08 +0000314// register. Return the interfering register.
315unsigned RegAllocBase::checkPhysRegInterference(LiveInterval &lvr,
316 unsigned preg) {
317 queries_[preg].init(&lvr, &physReg2liu_[preg]);
318 if (queries_[preg].checkInterference())
319 return preg;
Andrew Trick14e8d712010-10-22 23:09:15 +0000320 for (const unsigned *asI = tri_->getAliasSet(preg); *asI; ++asI) {
Andrew Tricke141a492010-11-08 18:02:08 +0000321 queries_[*asI].init(&lvr, &physReg2liu_[*asI]);
322 if (queries_[*asI].checkInterference())
323 return *asI;
Andrew Trick14e8d712010-10-22 23:09:15 +0000324 }
Andrew Tricke141a492010-11-08 18:02:08 +0000325 return 0;
326}
327
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000328// Sort live virtual registers by their register number.
329struct LessLiveVirtualReg
330 : public std::binary_function<LiveInterval, LiveInterval, bool> {
331 bool operator()(const LiveInterval *left, const LiveInterval *right) const {
332 return left->reg < right->reg;
333 }
334};
335
336// Spill all interferences currently assigned to this physical register.
337void RegAllocBase::spillReg(unsigned reg,
338 SmallVectorImpl<LiveInterval*> &splitLVRs) {
339 LiveIntervalUnion::Query &query = queries_[reg];
340 const SmallVectorImpl<LiveInterval*> &pendingSpills =
341 query.interferingVRegs();
342 for (SmallVectorImpl<LiveInterval*>::const_iterator I = pendingSpills.begin(),
343 E = pendingSpills.end(); I != E; ++I) {
344 LiveInterval &lvr = **I;
345 DEBUG(dbgs() <<
346 "extracting from " << tri_->getName(reg) << " " << lvr << '\n');
347
348 // Deallocate the interfering vreg by removing it from the union.
349 // A LiveInterval instance may not be in a union during modification!
350 physReg2liu_[reg].extract(lvr);
351
352 // After extracting segments, the query's results are invalid.
353 query.clear();
354
355 // Clear the vreg assignment.
356 vrm_->clearVirt(lvr.reg);
357
358 // Spill the extracted interval.
359 spiller().spill(&lvr, splitLVRs, pendingSpills);
360 }
361}
362
Andrew Trick071d1c02010-11-09 21:04:34 +0000363// Spill or split all live virtual registers currently unified under preg that
364// interfere with lvr. The newly spilled or split live intervals are returned by
365// appending them to splitLVRs.
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000366bool
367RegAllocBase::spillInterferences(unsigned preg,
Andrew Tricke141a492010-11-08 18:02:08 +0000368 SmallVectorImpl<LiveInterval*> &splitLVRs) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000369 // Record each interference and determine if all are spillable before mutating
370 // either the union or live intervals.
371 std::vector<LiveInterval*> spilledLVRs;
372
373 unsigned numInterferences = queries_[preg].collectInterferingVRegs();
374 if (queries_[preg].seenUnspillableVReg()) {
375 return false;
Andrew Tricke141a492010-11-08 18:02:08 +0000376 }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000377 for (const unsigned *asI = tri_->getAliasSet(preg); *asI; ++asI) {
378 numInterferences += queries_[*asI].collectInterferingVRegs();
379 if (queries_[*asI].seenUnspillableVReg()) {
380 return false;
381 }
382 }
383 DEBUG(dbgs() << "spilling " << tri_->getName(preg) <<
384 " interferences with " << queries_[preg].lvr() << "\n");
385 assert(numInterferences > 0 && "expect interference");
386
387 // Spill each interfering vreg allocated to preg or an alias.
388 spillReg(preg, splitLVRs);
389 for (const unsigned *asI = tri_->getAliasSet(preg); *asI; ++asI)
390 spillReg(*asI, splitLVRs);
391 return true;
Andrew Trick14e8d712010-10-22 23:09:15 +0000392}
393
394//===----------------------------------------------------------------------===//
395// RABasic Implementation
396//===----------------------------------------------------------------------===//
397
398// Driver for the register assignment and splitting heuristics.
399// Manages iteration over the LiveIntervalUnions.
400//
401// Minimal implementation of register assignment and splitting--spills whenever
402// we run out of registers.
403//
404// selectOrSplit can only be called once per live virtual register. We then do a
405// single interference test for each register the correct class until we find an
406// available register. So, the number of interference tests in the worst case is
407// |vregs| * |machineregs|. And since the number of interference tests is
408// minimal, there is no value in caching them.
Andrew Tricke16eecc2010-10-26 18:34:01 +0000409unsigned RABasic::selectOrSplit(LiveInterval &lvr,
410 SmallVectorImpl<LiveInterval*> &splitLVRs) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000411 // Populate a list of physical register spill candidates.
412 std::vector<unsigned> pregSpillCands;
Andrew Tricke141a492010-11-08 18:02:08 +0000413
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000414 // Check for an available register in this class.
Andrew Trick14e8d712010-10-22 23:09:15 +0000415 const TargetRegisterClass *trc = mri_->getRegClass(lvr.reg);
416 for (TargetRegisterClass::iterator trcI = trc->allocation_order_begin(*mf_),
417 trcEnd = trc->allocation_order_end(*mf_);
418 trcI != trcEnd; ++trcI) {
419 unsigned preg = *trcI;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000420 // Check interference and intialize queries for this lvr as a side effect.
Andrew Tricke141a492010-11-08 18:02:08 +0000421 unsigned interfReg = checkPhysRegInterference(lvr, preg);
422 if (interfReg == 0) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000423 // Found an available register.
Andrew Trick14e8d712010-10-22 23:09:15 +0000424 return preg;
425 }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000426 LiveInterval *interferingVirtReg =
427 queries_[interfReg].firstInterference().liuSegPos()->liveVirtReg;
428
429 // The current lvr must either spillable, or one of its interferences must
430 // have less spill weight.
431 if (interferingVirtReg->weight < lvr.weight ) {
432 pregSpillCands.push_back(preg);
Andrew Tricke141a492010-11-08 18:02:08 +0000433 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000434 }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000435 // Try to spill another interfering reg with less spill weight.
436 //
437 // FIXME: RAGreedy will sort this list by spill weight.
438 for (std::vector<unsigned>::iterator pregI = pregSpillCands.begin(),
439 pregE = pregSpillCands.end(); pregI != pregE; ++pregI) {
440
441 if (!spillInterferences(*pregI, splitLVRs)) continue;
442
443 unsigned interfReg = checkPhysRegInterference(lvr, *pregI);
444 if (interfReg != 0) {
445 const LiveSegment &seg =
446 *queries_[interfReg].firstInterference().liuSegPos();
447 dbgs() << "spilling cannot free " << tri_->getName(*pregI) <<
448 " for " << lvr.reg << " with interference " << seg.liveVirtReg << "\n";
449 llvm_unreachable("Interference after spill.");
450 }
451 // Tell the caller to allocate to this newly freed physical register.
452 return *pregI;
Andrew Tricke141a492010-11-08 18:02:08 +0000453 }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000454 // No other spill candidates were found, so spill the current lvr.
455 DEBUG(dbgs() << "spilling: " << lvr << '\n');
456 SmallVector<LiveInterval*, 1> pendingSpills;
457 spiller().spill(&lvr, splitLVRs, pendingSpills);
458
459 // The live virtual register requesting allocation was spilled, so tell
460 // the caller not to allocate anything during this round.
461 return 0;
Andrew Tricke141a492010-11-08 18:02:08 +0000462}
Andrew Trick14e8d712010-10-22 23:09:15 +0000463
Andrew Tricke141a492010-11-08 18:02:08 +0000464namespace llvm {
465Spiller *createInlineSpiller(MachineFunctionPass &pass,
466 MachineFunction &mf,
467 VirtRegMap &vrm);
Andrew Trick14e8d712010-10-22 23:09:15 +0000468}
469
470bool RABasic::runOnMachineFunction(MachineFunction &mf) {
471 DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
472 << "********** Function: "
473 << ((Value*)mf.getFunction())->getName() << '\n');
474
475 mf_ = &mf;
476 tm_ = &mf.getTarget();
477 mri_ = &mf.getRegInfo();
478
479 DEBUG(rmf_ = &getAnalysis<RenderMachineFunction>());
480
481 RegAllocBase::init(*tm_->getRegisterInfo(), getAnalysis<VirtRegMap>(),
482 getAnalysis<LiveIntervals>());
483
Andrew Tricke141a492010-11-08 18:02:08 +0000484 // We may want to force InlineSpiller for this register allocator. For
485 // now we're also experimenting with the standard spiller.
486 //
487 //spiller_.reset(createInlineSpiller(*this, *mf_, *vrm_));
Andrew Trick14e8d712010-10-22 23:09:15 +0000488 spiller_.reset(createSpiller(*this, *mf_, *vrm_));
489
Andrew Tricke16eecc2010-10-26 18:34:01 +0000490 allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000491
492 // Diagnostic output before rewriting
493 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm_ << "\n");
494
495 // optional HTML output
496 DEBUG(rmf_->renderMachineFunction("After basic register allocation.", vrm_));
497
Andrew Trick071d1c02010-11-09 21:04:34 +0000498 // FIXME: Verification currently must run before VirtRegRewriter. We should
499 // make the rewriter a separate pass and override verifyAnalysis instead. When
500 // that happens, verification naturally falls under VerifyMachineCode.
501#ifndef NDEBUG
502 if (VerifyRegAlloc) {
503 // Verify accuracy of LiveIntervals. The standard machine code verifier
504 // ensures that each LiveIntervals covers all uses of the virtual reg.
505
506 // FIXME: MachineVerifier is currently broken when using the standard
507 // spiller. Enable it for InlineSpiller only.
508 // mf_->verify(this);
509
510 // Verify that LiveIntervals are partitioned into unions and disjoint within
511 // the unions.
512 verify();
513 }
514#endif // !NDEBUG
515
Andrew Trick14e8d712010-10-22 23:09:15 +0000516 // Run rewriter
517 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
518 rewriter->runOnMachineFunction(*mf_, *vrm_, lis_);
Andrew Tricke16eecc2010-10-26 18:34:01 +0000519
520 // The pass output is in VirtRegMap. Release all the transient data.
521 releaseMemory();
Andrew Trick14e8d712010-10-22 23:09:15 +0000522
523 return true;
524}
525
526FunctionPass* llvm::createBasicRegisterAllocator()
527{
528 return new RABasic();
529}