Add a small missed optimization: turn X == C ? X : Y into X == C ? C : Y. This
[llvm/stm8.git] / lib / CodeGen / RegAllocPBQP.cpp
blob1e1f1e0d347024ca0bafbd475d00783fe4da0f5a
1 //===------ RegAllocPBQP.cpp ---- PBQP Register Allocator -------*- C++ -*-===//
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 contains a Partitioned Boolean Quadratic Programming (PBQP) based
11 // register allocator for LLVM. This allocator works by constructing a PBQP
12 // problem representing the register allocation problem under consideration,
13 // solving this using a PBQP solver, and mapping the solution back to a
14 // register assignment. If any variables are selected for spilling then spill
15 // code is inserted and the process repeated.
17 // The PBQP solver (pbqp.c) provided for this allocator uses a heuristic tuned
18 // for register allocation. For more information on PBQP for register
19 // allocation, see the following papers:
21 // (1) Hames, L. and Scholz, B. 2006. Nearly optimal register allocation with
22 // PBQP. In Proceedings of the 7th Joint Modular Languages Conference
23 // (JMLC'06). LNCS, vol. 4228. Springer, New York, NY, USA. 346-361.
25 // (2) Scholz, B., Eckstein, E. 2002. Register allocation for irregular
26 // architectures. In Proceedings of the Joint Conference on Languages,
27 // Compilers and Tools for Embedded Systems (LCTES'02), ACM Press, New York,
28 // NY, USA, 139-148.
30 //===----------------------------------------------------------------------===//
32 #define DEBUG_TYPE "regalloc"
34 #include "RenderMachineFunction.h"
35 #include "Splitter.h"
36 #include "VirtRegMap.h"
37 #include "VirtRegRewriter.h"
38 #include "llvm/CodeGen/CalcSpillWeights.h"
39 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
40 #include "llvm/CodeGen/LiveStackAnalysis.h"
41 #include "llvm/CodeGen/RegAllocPBQP.h"
42 #include "llvm/CodeGen/MachineFunctionPass.h"
43 #include "llvm/CodeGen/MachineLoopInfo.h"
44 #include "llvm/CodeGen/MachineRegisterInfo.h"
45 #include "llvm/CodeGen/PBQP/HeuristicSolver.h"
46 #include "llvm/CodeGen/PBQP/Graph.h"
47 #include "llvm/CodeGen/PBQP/Heuristics/Briggs.h"
48 #include "llvm/CodeGen/RegAllocRegistry.h"
49 #include "llvm/CodeGen/RegisterCoalescer.h"
50 #include "llvm/Support/Debug.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include "llvm/Target/TargetInstrInfo.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include <limits>
55 #include <memory>
56 #include <set>
57 #include <vector>
59 using namespace llvm;
61 static RegisterRegAlloc
62 registerPBQPRepAlloc("pbqp", "PBQP register allocator",
63 createDefaultPBQPRegisterAllocator);
65 static cl::opt<bool>
66 pbqpCoalescing("pbqp-coalescing",
67 cl::desc("Attempt coalescing during PBQP register allocation."),
68 cl::init(false), cl::Hidden);
70 static cl::opt<bool>
71 pbqpPreSplitting("pbqp-pre-splitting",
72 cl::desc("Pre-split before PBQP register allocation."),
73 cl::init(false), cl::Hidden);
75 namespace {
77 ///
78 /// PBQP based allocators solve the register allocation problem by mapping
79 /// register allocation problems to Partitioned Boolean Quadratic
80 /// Programming problems.
81 class RegAllocPBQP : public MachineFunctionPass {
82 public:
84 static char ID;
86 /// Construct a PBQP register allocator.
87 RegAllocPBQP(std::auto_ptr<PBQPBuilder> b)
88 : MachineFunctionPass(ID), builder(b) {
89 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
90 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
91 initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
92 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
93 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
94 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
95 initializeLoopSplitterPass(*PassRegistry::getPassRegistry());
96 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
97 initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry());
100 /// Return the pass name.
101 virtual const char* getPassName() const {
102 return "PBQP Register Allocator";
105 /// PBQP analysis usage.
106 virtual void getAnalysisUsage(AnalysisUsage &au) const;
108 /// Perform register allocation
109 virtual bool runOnMachineFunction(MachineFunction &MF);
111 private:
113 typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
114 typedef std::vector<const LiveInterval*> Node2LIMap;
115 typedef std::vector<unsigned> AllowedSet;
116 typedef std::vector<AllowedSet> AllowedSetMap;
117 typedef std::pair<unsigned, unsigned> RegPair;
118 typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
119 typedef std::vector<PBQP::Graph::NodeItr> NodeVector;
120 typedef std::set<unsigned> RegSet;
123 std::auto_ptr<PBQPBuilder> builder;
125 MachineFunction *mf;
126 const TargetMachine *tm;
127 const TargetRegisterInfo *tri;
128 const TargetInstrInfo *tii;
129 const MachineLoopInfo *loopInfo;
130 MachineRegisterInfo *mri;
131 RenderMachineFunction *rmf;
133 LiveIntervals *lis;
134 LiveStacks *lss;
135 VirtRegMap *vrm;
137 RegSet vregsToAlloc, emptyIntervalVRegs;
139 /// \brief Finds the initial set of vreg intervals to allocate.
140 void findVRegIntervalsToAlloc();
142 /// \brief Adds a stack interval if the given live interval has been
143 /// spilled. Used to support stack slot coloring.
144 void addStackInterval(const LiveInterval *spilled,MachineRegisterInfo* mri);
146 /// \brief Given a solved PBQP problem maps this solution back to a register
147 /// assignment.
148 bool mapPBQPToRegAlloc(const PBQPRAProblem &problem,
149 const PBQP::Solution &solution);
151 /// \brief Postprocessing before final spilling. Sets basic block "live in"
152 /// variables.
153 void finalizeAlloc() const;
157 char RegAllocPBQP::ID = 0;
159 } // End anonymous namespace.
161 unsigned PBQPRAProblem::getVRegForNode(PBQP::Graph::ConstNodeItr node) const {
162 Node2VReg::const_iterator vregItr = node2VReg.find(node);
163 assert(vregItr != node2VReg.end() && "No vreg for node.");
164 return vregItr->second;
167 PBQP::Graph::NodeItr PBQPRAProblem::getNodeForVReg(unsigned vreg) const {
168 VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg);
169 assert(nodeItr != vreg2Node.end() && "No node for vreg.");
170 return nodeItr->second;
174 const PBQPRAProblem::AllowedSet&
175 PBQPRAProblem::getAllowedSet(unsigned vreg) const {
176 AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg);
177 assert(allowedSetItr != allowedSets.end() && "No pregs for vreg.");
178 const AllowedSet &allowedSet = allowedSetItr->second;
179 return allowedSet;
182 unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const {
183 assert(isPRegOption(vreg, option) && "Not a preg option.");
185 const AllowedSet& allowedSet = getAllowedSet(vreg);
186 assert(option <= allowedSet.size() && "Option outside allowed set.");
187 return allowedSet[option - 1];
190 std::auto_ptr<PBQPRAProblem> PBQPBuilder::build(MachineFunction *mf,
191 const LiveIntervals *lis,
192 const MachineLoopInfo *loopInfo,
193 const RegSet &vregs) {
195 typedef std::vector<const LiveInterval*> LIVector;
197 MachineRegisterInfo *mri = &mf->getRegInfo();
198 const TargetRegisterInfo *tri = mf->getTarget().getRegisterInfo();
200 std::auto_ptr<PBQPRAProblem> p(new PBQPRAProblem());
201 PBQP::Graph &g = p->getGraph();
202 RegSet pregs;
204 // Collect the set of preg intervals, record that they're used in the MF.
205 for (LiveIntervals::const_iterator itr = lis->begin(), end = lis->end();
206 itr != end; ++itr) {
207 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
208 pregs.insert(itr->first);
209 mri->setPhysRegUsed(itr->first);
213 BitVector reservedRegs = tri->getReservedRegs(*mf);
215 // Iterate over vregs.
216 for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end();
217 vregItr != vregEnd; ++vregItr) {
218 unsigned vreg = *vregItr;
219 const TargetRegisterClass *trc = mri->getRegClass(vreg);
220 const LiveInterval *vregLI = &lis->getInterval(vreg);
222 // Compute an initial allowed set for the current vreg.
223 typedef std::vector<unsigned> VRAllowed;
224 VRAllowed vrAllowed;
225 for (TargetRegisterClass::iterator aoItr = trc->allocation_order_begin(*mf),
226 aoEnd = trc->allocation_order_end(*mf);
227 aoItr != aoEnd; ++aoItr) {
228 unsigned preg = *aoItr;
229 if (!reservedRegs.test(preg)) {
230 vrAllowed.push_back(preg);
234 // Remove any physical registers which overlap.
235 for (RegSet::const_iterator pregItr = pregs.begin(),
236 pregEnd = pregs.end();
237 pregItr != pregEnd; ++pregItr) {
238 unsigned preg = *pregItr;
239 const LiveInterval *pregLI = &lis->getInterval(preg);
241 if (pregLI->empty()) {
242 continue;
245 if (!vregLI->overlaps(*pregLI)) {
246 continue;
249 // Remove the register from the allowed set.
250 VRAllowed::iterator eraseItr =
251 std::find(vrAllowed.begin(), vrAllowed.end(), preg);
253 if (eraseItr != vrAllowed.end()) {
254 vrAllowed.erase(eraseItr);
257 // Also remove any aliases.
258 const unsigned *aliasItr = tri->getAliasSet(preg);
259 if (aliasItr != 0) {
260 for (; *aliasItr != 0; ++aliasItr) {
261 VRAllowed::iterator eraseItr =
262 std::find(vrAllowed.begin(), vrAllowed.end(), *aliasItr);
264 if (eraseItr != vrAllowed.end()) {
265 vrAllowed.erase(eraseItr);
271 // Construct the node.
272 PBQP::Graph::NodeItr node =
273 g.addNode(PBQP::Vector(vrAllowed.size() + 1, 0));
275 // Record the mapping and allowed set in the problem.
276 p->recordVReg(vreg, node, vrAllowed.begin(), vrAllowed.end());
278 PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ?
279 vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min();
281 addSpillCosts(g.getNodeCosts(node), spillCost);
284 for (RegSet::const_iterator vr1Itr = vregs.begin(), vrEnd = vregs.end();
285 vr1Itr != vrEnd; ++vr1Itr) {
286 unsigned vr1 = *vr1Itr;
287 const LiveInterval &l1 = lis->getInterval(vr1);
288 const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1);
290 for (RegSet::const_iterator vr2Itr = llvm::next(vr1Itr);
291 vr2Itr != vrEnd; ++vr2Itr) {
292 unsigned vr2 = *vr2Itr;
293 const LiveInterval &l2 = lis->getInterval(vr2);
294 const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2);
296 assert(!l2.empty() && "Empty interval in vreg set?");
297 if (l1.overlaps(l2)) {
298 PBQP::Graph::EdgeItr edge =
299 g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2),
300 PBQP::Matrix(vr1Allowed.size()+1, vr2Allowed.size()+1, 0));
302 addInterferenceCosts(g.getEdgeCosts(edge), vr1Allowed, vr2Allowed, tri);
307 return p;
310 void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec,
311 PBQP::PBQPNum spillCost) {
312 costVec[0] = spillCost;
315 void PBQPBuilder::addInterferenceCosts(
316 PBQP::Matrix &costMat,
317 const PBQPRAProblem::AllowedSet &vr1Allowed,
318 const PBQPRAProblem::AllowedSet &vr2Allowed,
319 const TargetRegisterInfo *tri) {
320 assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch.");
321 assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch.");
323 for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
324 unsigned preg1 = vr1Allowed[i];
326 for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
327 unsigned preg2 = vr2Allowed[j];
329 if (tri->regsOverlap(preg1, preg2)) {
330 costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
336 std::auto_ptr<PBQPRAProblem> PBQPBuilderWithCoalescing::build(
337 MachineFunction *mf,
338 const LiveIntervals *lis,
339 const MachineLoopInfo *loopInfo,
340 const RegSet &vregs) {
342 std::auto_ptr<PBQPRAProblem> p = PBQPBuilder::build(mf, lis, loopInfo, vregs);
343 PBQP::Graph &g = p->getGraph();
345 const TargetMachine &tm = mf->getTarget();
346 CoalescerPair cp(*tm.getInstrInfo(), *tm.getRegisterInfo());
348 // Scan the machine function and add a coalescing cost whenever CoalescerPair
349 // gives the Ok.
350 for (MachineFunction::const_iterator mbbItr = mf->begin(),
351 mbbEnd = mf->end();
352 mbbItr != mbbEnd; ++mbbItr) {
353 const MachineBasicBlock *mbb = &*mbbItr;
355 for (MachineBasicBlock::const_iterator miItr = mbb->begin(),
356 miEnd = mbb->end();
357 miItr != miEnd; ++miItr) {
358 const MachineInstr *mi = &*miItr;
360 if (!cp.setRegisters(mi)) {
361 continue; // Not coalescable.
364 if (cp.getSrcReg() == cp.getDstReg()) {
365 continue; // Already coalesced.
368 unsigned dst = cp.getDstReg(),
369 src = cp.getSrcReg();
371 const float copyFactor = 0.5; // Cost of copy relative to load. Current
372 // value plucked randomly out of the air.
374 PBQP::PBQPNum cBenefit =
375 copyFactor * LiveIntervals::getSpillWeight(false, true,
376 loopInfo->getLoopDepth(mbb));
378 if (cp.isPhys()) {
379 if (!lis->isAllocatable(dst)) {
380 continue;
383 const PBQPRAProblem::AllowedSet &allowed = p->getAllowedSet(src);
384 unsigned pregOpt = 0;
385 while (pregOpt < allowed.size() && allowed[pregOpt] != dst) {
386 ++pregOpt;
388 if (pregOpt < allowed.size()) {
389 ++pregOpt; // +1 to account for spill option.
390 PBQP::Graph::NodeItr node = p->getNodeForVReg(src);
391 addPhysRegCoalesce(g.getNodeCosts(node), pregOpt, cBenefit);
393 } else {
394 const PBQPRAProblem::AllowedSet *allowed1 = &p->getAllowedSet(dst);
395 const PBQPRAProblem::AllowedSet *allowed2 = &p->getAllowedSet(src);
396 PBQP::Graph::NodeItr node1 = p->getNodeForVReg(dst);
397 PBQP::Graph::NodeItr node2 = p->getNodeForVReg(src);
398 PBQP::Graph::EdgeItr edge = g.findEdge(node1, node2);
399 if (edge == g.edgesEnd()) {
400 edge = g.addEdge(node1, node2, PBQP::Matrix(allowed1->size() + 1,
401 allowed2->size() + 1,
402 0));
403 } else {
404 if (g.getEdgeNode1(edge) == node2) {
405 std::swap(node1, node2);
406 std::swap(allowed1, allowed2);
410 addVirtRegCoalesce(g.getEdgeCosts(edge), *allowed1, *allowed2,
411 cBenefit);
416 return p;
419 void PBQPBuilderWithCoalescing::addPhysRegCoalesce(PBQP::Vector &costVec,
420 unsigned pregOption,
421 PBQP::PBQPNum benefit) {
422 costVec[pregOption] += -benefit;
425 void PBQPBuilderWithCoalescing::addVirtRegCoalesce(
426 PBQP::Matrix &costMat,
427 const PBQPRAProblem::AllowedSet &vr1Allowed,
428 const PBQPRAProblem::AllowedSet &vr2Allowed,
429 PBQP::PBQPNum benefit) {
431 assert(costMat.getRows() == vr1Allowed.size() + 1 && "Size mismatch.");
432 assert(costMat.getCols() == vr2Allowed.size() + 1 && "Size mismatch.");
434 for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
435 unsigned preg1 = vr1Allowed[i];
436 for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
437 unsigned preg2 = vr2Allowed[j];
439 if (preg1 == preg2) {
440 costMat[i + 1][j + 1] += -benefit;
447 void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
448 au.addRequired<SlotIndexes>();
449 au.addPreserved<SlotIndexes>();
450 au.addRequired<LiveIntervals>();
451 //au.addRequiredID(SplitCriticalEdgesID);
452 au.addRequired<RegisterCoalescer>();
453 au.addRequired<CalculateSpillWeights>();
454 au.addRequired<LiveStacks>();
455 au.addPreserved<LiveStacks>();
456 au.addRequired<MachineLoopInfo>();
457 au.addPreserved<MachineLoopInfo>();
458 if (pbqpPreSplitting)
459 au.addRequired<LoopSplitter>();
460 au.addRequired<VirtRegMap>();
461 au.addRequired<RenderMachineFunction>();
462 MachineFunctionPass::getAnalysisUsage(au);
465 void RegAllocPBQP::findVRegIntervalsToAlloc() {
467 // Iterate over all live ranges.
468 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
469 itr != end; ++itr) {
471 // Ignore physical ones.
472 if (TargetRegisterInfo::isPhysicalRegister(itr->first))
473 continue;
475 LiveInterval *li = itr->second;
477 // If this live interval is non-empty we will use pbqp to allocate it.
478 // Empty intervals we allocate in a simple post-processing stage in
479 // finalizeAlloc.
480 if (!li->empty()) {
481 vregsToAlloc.insert(li->reg);
482 } else {
483 emptyIntervalVRegs.insert(li->reg);
488 void RegAllocPBQP::addStackInterval(const LiveInterval *spilled,
489 MachineRegisterInfo* mri) {
490 int stackSlot = vrm->getStackSlot(spilled->reg);
492 if (stackSlot == VirtRegMap::NO_STACK_SLOT) {
493 return;
496 const TargetRegisterClass *RC = mri->getRegClass(spilled->reg);
497 LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC);
499 VNInfo *vni;
500 if (stackInterval.getNumValNums() != 0) {
501 vni = stackInterval.getValNumInfo(0);
502 } else {
503 vni = stackInterval.getNextValue(
504 SlotIndex(), 0, lss->getVNInfoAllocator());
507 LiveInterval &rhsInterval = lis->getInterval(spilled->reg);
508 stackInterval.MergeRangesInAsValue(rhsInterval, vni);
511 bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAProblem &problem,
512 const PBQP::Solution &solution) {
513 // Set to true if we have any spills
514 bool anotherRoundNeeded = false;
516 // Clear the existing allocation.
517 vrm->clearAllVirt();
519 const PBQP::Graph &g = problem.getGraph();
520 // Iterate over the nodes mapping the PBQP solution to a register
521 // assignment.
522 for (PBQP::Graph::ConstNodeItr node = g.nodesBegin(),
523 nodeEnd = g.nodesEnd();
524 node != nodeEnd; ++node) {
525 unsigned vreg = problem.getVRegForNode(node);
526 unsigned alloc = solution.getSelection(node);
528 if (problem.isPRegOption(vreg, alloc)) {
529 unsigned preg = problem.getPRegForOption(vreg, alloc);
530 DEBUG(dbgs() << "VREG " << vreg << " -> " << tri->getName(preg) << "\n");
531 assert(preg != 0 && "Invalid preg selected.");
532 vrm->assignVirt2Phys(vreg, preg);
533 } else if (problem.isSpillOption(vreg, alloc)) {
534 vregsToAlloc.erase(vreg);
535 const LiveInterval* spillInterval = &lis->getInterval(vreg);
536 double oldWeight = spillInterval->weight;
537 rmf->rememberUseDefs(spillInterval);
538 std::vector<LiveInterval*> newSpills =
539 lis->addIntervalsForSpills(*spillInterval, 0, loopInfo, *vrm);
540 addStackInterval(spillInterval, mri);
541 rmf->rememberSpills(spillInterval, newSpills);
543 (void) oldWeight;
544 DEBUG(dbgs() << "VREG " << vreg << " -> SPILLED (Cost: "
545 << oldWeight << ", New vregs: ");
547 // Copy any newly inserted live intervals into the list of regs to
548 // allocate.
549 for (std::vector<LiveInterval*>::const_iterator
550 itr = newSpills.begin(), end = newSpills.end();
551 itr != end; ++itr) {
552 assert(!(*itr)->empty() && "Empty spill range.");
553 DEBUG(dbgs() << (*itr)->reg << " ");
554 vregsToAlloc.insert((*itr)->reg);
557 DEBUG(dbgs() << ")\n");
559 // We need another round if spill intervals were added.
560 anotherRoundNeeded |= !newSpills.empty();
561 } else {
562 assert(false && "Unknown allocation option.");
566 return !anotherRoundNeeded;
570 void RegAllocPBQP::finalizeAlloc() const {
571 typedef LiveIntervals::iterator LIIterator;
572 typedef LiveInterval::Ranges::const_iterator LRIterator;
574 // First allocate registers for the empty intervals.
575 for (RegSet::const_iterator
576 itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end();
577 itr != end; ++itr) {
578 LiveInterval *li = &lis->getInterval(*itr);
580 unsigned physReg = vrm->getRegAllocPref(li->reg);
582 if (physReg == 0) {
583 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
584 physReg = *liRC->allocation_order_begin(*mf);
587 vrm->assignVirt2Phys(li->reg, physReg);
590 // Finally iterate over the basic blocks to compute and set the live-in sets.
591 SmallVector<MachineBasicBlock*, 8> liveInMBBs;
592 MachineBasicBlock *entryMBB = &*mf->begin();
594 for (LIIterator liItr = lis->begin(), liEnd = lis->end();
595 liItr != liEnd; ++liItr) {
597 const LiveInterval *li = liItr->second;
598 unsigned reg = 0;
600 // Get the physical register for this interval
601 if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
602 reg = li->reg;
603 } else if (vrm->isAssignedReg(li->reg)) {
604 reg = vrm->getPhys(li->reg);
605 } else {
606 // Ranges which are assigned a stack slot only are ignored.
607 continue;
610 if (reg == 0) {
611 // Filter out zero regs - they're for intervals that were spilled.
612 continue;
615 // Iterate over the ranges of the current interval...
616 for (LRIterator lrItr = li->begin(), lrEnd = li->end();
617 lrItr != lrEnd; ++lrItr) {
619 // Find the set of basic blocks which this range is live into...
620 if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
621 // And add the physreg for this interval to their live-in sets.
622 for (unsigned i = 0; i != liveInMBBs.size(); ++i) {
623 if (liveInMBBs[i] != entryMBB) {
624 if (!liveInMBBs[i]->isLiveIn(reg)) {
625 liveInMBBs[i]->addLiveIn(reg);
629 liveInMBBs.clear();
636 bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
638 mf = &MF;
639 tm = &mf->getTarget();
640 tri = tm->getRegisterInfo();
641 tii = tm->getInstrInfo();
642 mri = &mf->getRegInfo();
644 lis = &getAnalysis<LiveIntervals>();
645 lss = &getAnalysis<LiveStacks>();
646 loopInfo = &getAnalysis<MachineLoopInfo>();
647 rmf = &getAnalysis<RenderMachineFunction>();
649 vrm = &getAnalysis<VirtRegMap>();
652 DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n");
654 // Allocator main loop:
656 // * Map current regalloc problem to a PBQP problem
657 // * Solve the PBQP problem
658 // * Map the solution back to a register allocation
659 // * Spill if necessary
661 // This process is continued till no more spills are generated.
663 // Find the vreg intervals in need of allocation.
664 findVRegIntervalsToAlloc();
666 // If there are non-empty intervals allocate them using pbqp.
667 if (!vregsToAlloc.empty()) {
669 bool pbqpAllocComplete = false;
670 unsigned round = 0;
672 while (!pbqpAllocComplete) {
673 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
675 std::auto_ptr<PBQPRAProblem> problem =
676 builder->build(mf, lis, loopInfo, vregsToAlloc);
677 PBQP::Solution solution =
678 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(
679 problem->getGraph());
681 pbqpAllocComplete = mapPBQPToRegAlloc(*problem, solution);
683 ++round;
687 // Finalise allocation, allocate empty ranges.
688 finalizeAlloc();
690 rmf->renderMachineFunction("After PBQP register allocation.", vrm);
692 vregsToAlloc.clear();
693 emptyIntervalVRegs.clear();
695 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
697 // Run rewriter
698 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
700 rewriter->runOnMachineFunction(*mf, *vrm, lis);
702 return true;
705 FunctionPass* llvm::createPBQPRegisterAllocator(
706 std::auto_ptr<PBQPBuilder> builder) {
707 return new RegAllocPBQP(builder);
710 FunctionPass* llvm::createDefaultPBQPRegisterAllocator() {
711 if (pbqpCoalescing) {
712 return createPBQPRegisterAllocator(
713 std::auto_ptr<PBQPBuilder>(new PBQPBuilderWithCoalescing()));
714 } // else
715 return createPBQPRegisterAllocator(
716 std::auto_ptr<PBQPBuilder>(new PBQPBuilder()));
719 #undef DEBUG_TYPE