c9588db738ce21c9edd4ac351f62d437994f8ba5
[oota-llvm.git] / lib / CodeGen / RegAllocPBQP.cpp
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.
16 //
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:
20 //
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.
24 //
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.
29 //
30 //===----------------------------------------------------------------------===//
31
32 #define DEBUG_TYPE "regalloc"
33
34 #include "llvm/CodeGen/RegAllocPBQP.h"
35 #include "RegisterCoalescer.h"
36 #include "Spiller.h"
37 #include "llvm/ADT/OwningPtr.h"
38 #include "llvm/Analysis/AliasAnalysis.h"
39 #include "llvm/CodeGen/CalcSpillWeights.h"
40 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
41 #include "llvm/CodeGen/LiveRangeEdit.h"
42 #include "llvm/CodeGen/LiveStackAnalysis.h"
43 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
44 #include "llvm/CodeGen/MachineDominators.h"
45 #include "llvm/CodeGen/MachineFunctionPass.h"
46 #include "llvm/CodeGen/MachineLoopInfo.h"
47 #include "llvm/CodeGen/MachineRegisterInfo.h"
48 #include "llvm/CodeGen/RegAllocRegistry.h"
49 #include "llvm/CodeGen/VirtRegMap.h"
50 #include "llvm/IR/Module.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Target/TargetInstrInfo.h"
54 #include "llvm/Target/TargetMachine.h"
55 #include <limits>
56 #include <memory>
57 #include <set>
58 #include <sstream>
59 #include <vector>
60
61 using namespace llvm;
62
63 static RegisterRegAlloc
64 registerPBQPRepAlloc("pbqp", "PBQP register allocator",
65                        createDefaultPBQPRegisterAllocator);
66
67 static cl::opt<bool>
68 pbqpCoalescing("pbqp-coalescing",
69                 cl::desc("Attempt coalescing during PBQP register allocation."),
70                 cl::init(false), cl::Hidden);
71
72 #ifndef NDEBUG
73 static cl::opt<bool>
74 pbqpDumpGraphs("pbqp-dump-graphs",
75                cl::desc("Dump graphs for each function/round in the compilation unit."),
76                cl::init(false), cl::Hidden);
77 #endif
78
79 namespace {
80
81 ///
82 /// PBQP based allocators solve the register allocation problem by mapping
83 /// register allocation problems to Partitioned Boolean Quadratic
84 /// Programming problems.
85 class RegAllocPBQP : public MachineFunctionPass {
86 public:
87
88   static char ID;
89
90   /// Construct a PBQP register allocator.
91   RegAllocPBQP(OwningPtr<PBQPBuilder> &b, char *cPassID=0)
92       : MachineFunctionPass(ID), builder(b.release()), customPassID(cPassID) {
93     initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
94     initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
95     initializeLiveStacksPass(*PassRegistry::getPassRegistry());
96     initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
97   }
98
99   /// Return the pass name.
100   virtual const char* getPassName() const {
101     return "PBQP Register Allocator";
102   }
103
104   /// PBQP analysis usage.
105   virtual void getAnalysisUsage(AnalysisUsage &au) const;
106
107   /// Perform register allocation
108   virtual bool runOnMachineFunction(MachineFunction &MF);
109
110 private:
111
112   typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
113   typedef std::vector<const LiveInterval*> Node2LIMap;
114   typedef std::vector<unsigned> AllowedSet;
115   typedef std::vector<AllowedSet> AllowedSetMap;
116   typedef std::pair<unsigned, unsigned> RegPair;
117   typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
118   typedef std::set<unsigned> RegSet;
119
120
121   OwningPtr<PBQPBuilder> builder;
122
123   char *customPassID;
124
125   MachineFunction *mf;
126   const TargetMachine *tm;
127   const TargetRegisterInfo *tri;
128   const TargetInstrInfo *tii;
129   MachineRegisterInfo *mri;
130   const MachineBlockFrequencyInfo *mbfi;
131
132   OwningPtr<Spiller> spiller;
133   LiveIntervals *lis;
134   LiveStacks *lss;
135   VirtRegMap *vrm;
136
137   RegSet vregsToAlloc, emptyIntervalVRegs;
138
139   /// \brief Finds the initial set of vreg intervals to allocate.
140   void findVRegIntervalsToAlloc();
141
142   /// \brief Given a solved PBQP problem maps this solution back to a register
143   /// assignment.
144   bool mapPBQPToRegAlloc(const PBQPRAProblem &problem,
145                          const PBQP::Solution &solution);
146
147   /// \brief Postprocessing before final spilling. Sets basic block "live in"
148   /// variables.
149   void finalizeAlloc() const;
150
151 };
152
153 char RegAllocPBQP::ID = 0;
154
155 } // End anonymous namespace.
156
157 unsigned PBQPRAProblem::getVRegForNode(PBQPRAGraph::NodeId node) const {
158   Node2VReg::const_iterator vregItr = node2VReg.find(node);
159   assert(vregItr != node2VReg.end() && "No vreg for node.");
160   return vregItr->second;
161 }
162
163 PBQPRAGraph::NodeId PBQPRAProblem::getNodeForVReg(unsigned vreg) const {
164   VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg);
165   assert(nodeItr != vreg2Node.end() && "No node for vreg.");
166   return nodeItr->second;
167
168 }
169
170 const PBQPRAProblem::AllowedSet&
171   PBQPRAProblem::getAllowedSet(unsigned vreg) const {
172   AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg);
173   assert(allowedSetItr != allowedSets.end() && "No pregs for vreg.");
174   const AllowedSet &allowedSet = allowedSetItr->second;
175   return allowedSet;
176 }
177
178 unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const {
179   assert(isPRegOption(vreg, option) && "Not a preg option.");
180
181   const AllowedSet& allowedSet = getAllowedSet(vreg);
182   assert(option <= allowedSet.size() && "Option outside allowed set.");
183   return allowedSet[option - 1];
184 }
185
186 PBQPRAProblem *PBQPBuilder::build(MachineFunction *mf, const LiveIntervals *lis,
187                                   const MachineBlockFrequencyInfo *mbfi,
188                                   const RegSet &vregs) {
189
190   LiveIntervals *LIS = const_cast<LiveIntervals*>(lis);
191   MachineRegisterInfo *mri = &mf->getRegInfo();
192   const TargetRegisterInfo *tri = mf->getTarget().getRegisterInfo();
193
194   OwningPtr<PBQPRAProblem> p(new PBQPRAProblem());
195   PBQPRAGraph &g = p->getGraph();
196   RegSet pregs;
197
198   // Collect the set of preg intervals, record that they're used in the MF.
199   for (unsigned Reg = 1, e = tri->getNumRegs(); Reg != e; ++Reg) {
200     if (mri->def_empty(Reg))
201       continue;
202     pregs.insert(Reg);
203     mri->setPhysRegUsed(Reg);
204   }
205
206   // Iterate over vregs.
207   for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end();
208        vregItr != vregEnd; ++vregItr) {
209     unsigned vreg = *vregItr;
210     const TargetRegisterClass *trc = mri->getRegClass(vreg);
211     LiveInterval *vregLI = &LIS->getInterval(vreg);
212
213     // Record any overlaps with regmask operands.
214     BitVector regMaskOverlaps;
215     LIS->checkRegMaskInterference(*vregLI, regMaskOverlaps);
216
217     // Compute an initial allowed set for the current vreg.
218     typedef std::vector<unsigned> VRAllowed;
219     VRAllowed vrAllowed;
220     ArrayRef<uint16_t> rawOrder = trc->getRawAllocationOrder(*mf);
221     for (unsigned i = 0; i != rawOrder.size(); ++i) {
222       unsigned preg = rawOrder[i];
223       if (mri->isReserved(preg))
224         continue;
225
226       // vregLI crosses a regmask operand that clobbers preg.
227       if (!regMaskOverlaps.empty() && !regMaskOverlaps.test(preg))
228         continue;
229
230       // vregLI overlaps fixed regunit interference.
231       bool Interference = false;
232       for (MCRegUnitIterator Units(preg, tri); Units.isValid(); ++Units) {
233         if (vregLI->overlaps(LIS->getRegUnit(*Units))) {
234           Interference = true;
235           break;
236         }
237       }
238       if (Interference)
239         continue;
240
241       // preg is usable for this virtual register.
242       vrAllowed.push_back(preg);
243     }
244
245     PBQP::Vector nodeCosts(vrAllowed.size() + 1, 0);
246
247     PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ?
248         vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min();
249
250     addSpillCosts(nodeCosts, spillCost);
251
252     // Construct the node.
253     PBQPRAGraph::NodeId nId = g.addNode(std::move(nodeCosts));
254
255     // Record the mapping and allowed set in the problem.
256     p->recordVReg(vreg, nId, vrAllowed.begin(), vrAllowed.end());
257
258   }
259
260   for (RegSet::const_iterator vr1Itr = vregs.begin(), vrEnd = vregs.end();
261          vr1Itr != vrEnd; ++vr1Itr) {
262     unsigned vr1 = *vr1Itr;
263     const LiveInterval &l1 = lis->getInterval(vr1);
264     const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1);
265
266     for (RegSet::const_iterator vr2Itr = std::next(vr1Itr); vr2Itr != vrEnd;
267          ++vr2Itr) {
268       unsigned vr2 = *vr2Itr;
269       const LiveInterval &l2 = lis->getInterval(vr2);
270       const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2);
271
272       assert(!l2.empty() && "Empty interval in vreg set?");
273       if (l1.overlaps(l2)) {
274         PBQP::Matrix edgeCosts(vr1Allowed.size()+1, vr2Allowed.size()+1, 0);
275         addInterferenceCosts(edgeCosts, vr1Allowed, vr2Allowed, tri);
276
277         g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2),
278                   std::move(edgeCosts));
279       }
280     }
281   }
282
283   return p.release();
284 }
285
286 void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec,
287                                 PBQP::PBQPNum spillCost) {
288   costVec[0] = spillCost;
289 }
290
291 void PBQPBuilder::addInterferenceCosts(
292                                     PBQP::Matrix &costMat,
293                                     const PBQPRAProblem::AllowedSet &vr1Allowed,
294                                     const PBQPRAProblem::AllowedSet &vr2Allowed,
295                                     const TargetRegisterInfo *tri) {
296   assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch.");
297   assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch.");
298
299   for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
300     unsigned preg1 = vr1Allowed[i];
301
302     for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
303       unsigned preg2 = vr2Allowed[j];
304
305       if (tri->regsOverlap(preg1, preg2)) {
306         costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
307       }
308     }
309   }
310 }
311
312 PBQPRAProblem *PBQPBuilderWithCoalescing::build(MachineFunction *mf,
313                                                 const LiveIntervals *lis,
314                                                 const MachineBlockFrequencyInfo *mbfi,
315                                                 const RegSet &vregs) {
316
317   OwningPtr<PBQPRAProblem> p(PBQPBuilder::build(mf, lis, mbfi, vregs));
318   PBQPRAGraph &g = p->getGraph();
319
320   const TargetMachine &tm = mf->getTarget();
321   CoalescerPair cp(*tm.getRegisterInfo());
322
323   // Scan the machine function and add a coalescing cost whenever CoalescerPair
324   // gives the Ok.
325   for (MachineFunction::const_iterator mbbItr = mf->begin(),
326                                        mbbEnd = mf->end();
327        mbbItr != mbbEnd; ++mbbItr) {
328     const MachineBasicBlock *mbb = &*mbbItr;
329
330     for (MachineBasicBlock::const_iterator miItr = mbb->begin(),
331                                            miEnd = mbb->end();
332          miItr != miEnd; ++miItr) {
333       const MachineInstr *mi = &*miItr;
334
335       if (!cp.setRegisters(mi)) {
336         continue; // Not coalescable.
337       }
338
339       if (cp.getSrcReg() == cp.getDstReg()) {
340         continue; // Already coalesced.
341       }
342
343       unsigned dst = cp.getDstReg(),
344                src = cp.getSrcReg();
345
346       const float copyFactor = 0.5; // Cost of copy relative to load. Current
347       // value plucked randomly out of the air.
348
349       PBQP::PBQPNum cBenefit =
350         copyFactor * LiveIntervals::getSpillWeight(false, true, mbfi, mi);
351
352       if (cp.isPhys()) {
353         if (!mf->getRegInfo().isAllocatable(dst)) {
354           continue;
355         }
356
357         const PBQPRAProblem::AllowedSet &allowed = p->getAllowedSet(src);
358         unsigned pregOpt = 0;
359         while (pregOpt < allowed.size() && allowed[pregOpt] != dst) {
360           ++pregOpt;
361         }
362         if (pregOpt < allowed.size()) {
363           ++pregOpt; // +1 to account for spill option.
364           PBQPRAGraph::NodeId node = p->getNodeForVReg(src);
365           llvm::dbgs() << "Reading node costs for node " << node << "\n";
366           llvm::dbgs() << "Source node: " << &g.getNodeCosts(node) << "\n";
367           PBQP::Vector newCosts(g.getNodeCosts(node));
368           addPhysRegCoalesce(newCosts, pregOpt, cBenefit);
369           g.setNodeCosts(node, newCosts);
370         }
371       } else {
372         const PBQPRAProblem::AllowedSet *allowed1 = &p->getAllowedSet(dst);
373         const PBQPRAProblem::AllowedSet *allowed2 = &p->getAllowedSet(src);
374         PBQPRAGraph::NodeId node1 = p->getNodeForVReg(dst);
375         PBQPRAGraph::NodeId node2 = p->getNodeForVReg(src);
376         PBQPRAGraph::EdgeId edge = g.findEdge(node1, node2);
377         if (edge == g.invalidEdgeId()) {
378           PBQP::Matrix costs(allowed1->size() + 1, allowed2->size() + 1, 0);
379           addVirtRegCoalesce(costs, *allowed1, *allowed2, cBenefit);
380           g.addEdge(node1, node2, costs);
381         } else {
382           if (g.getEdgeNode1Id(edge) == node2) {
383             std::swap(node1, node2);
384             std::swap(allowed1, allowed2);
385           }
386           PBQP::Matrix costs(g.getEdgeCosts(edge));
387           addVirtRegCoalesce(costs, *allowed1, *allowed2, cBenefit);
388           g.setEdgeCosts(edge, costs);
389         }
390       }
391     }
392   }
393
394   return p.release();
395 }
396
397 void PBQPBuilderWithCoalescing::addPhysRegCoalesce(PBQP::Vector &costVec,
398                                                    unsigned pregOption,
399                                                    PBQP::PBQPNum benefit) {
400   costVec[pregOption] += -benefit;
401 }
402
403 void PBQPBuilderWithCoalescing::addVirtRegCoalesce(
404                                     PBQP::Matrix &costMat,
405                                     const PBQPRAProblem::AllowedSet &vr1Allowed,
406                                     const PBQPRAProblem::AllowedSet &vr2Allowed,
407                                     PBQP::PBQPNum benefit) {
408
409   assert(costMat.getRows() == vr1Allowed.size() + 1 && "Size mismatch.");
410   assert(costMat.getCols() == vr2Allowed.size() + 1 && "Size mismatch.");
411
412   for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
413     unsigned preg1 = vr1Allowed[i];
414     for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
415       unsigned preg2 = vr2Allowed[j];
416
417       if (preg1 == preg2) {
418         costMat[i + 1][j + 1] += -benefit;
419       }
420     }
421   }
422 }
423
424
425 void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
426   au.setPreservesCFG();
427   au.addRequired<AliasAnalysis>();
428   au.addPreserved<AliasAnalysis>();
429   au.addRequired<SlotIndexes>();
430   au.addPreserved<SlotIndexes>();
431   au.addRequired<LiveIntervals>();
432   au.addPreserved<LiveIntervals>();
433   //au.addRequiredID(SplitCriticalEdgesID);
434   if (customPassID)
435     au.addRequiredID(*customPassID);
436   au.addRequired<LiveStacks>();
437   au.addPreserved<LiveStacks>();
438   au.addRequired<MachineBlockFrequencyInfo>();
439   au.addPreserved<MachineBlockFrequencyInfo>();
440   au.addRequired<MachineLoopInfo>();
441   au.addPreserved<MachineLoopInfo>();
442   au.addRequired<MachineDominatorTree>();
443   au.addPreserved<MachineDominatorTree>();
444   au.addRequired<VirtRegMap>();
445   au.addPreserved<VirtRegMap>();
446   MachineFunctionPass::getAnalysisUsage(au);
447 }
448
449 void RegAllocPBQP::findVRegIntervalsToAlloc() {
450
451   // Iterate over all live ranges.
452   for (unsigned i = 0, e = mri->getNumVirtRegs(); i != e; ++i) {
453     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
454     if (mri->reg_nodbg_empty(Reg))
455       continue;
456     LiveInterval *li = &lis->getInterval(Reg);
457
458     // If this live interval is non-empty we will use pbqp to allocate it.
459     // Empty intervals we allocate in a simple post-processing stage in
460     // finalizeAlloc.
461     if (!li->empty()) {
462       vregsToAlloc.insert(li->reg);
463     } else {
464       emptyIntervalVRegs.insert(li->reg);
465     }
466   }
467 }
468
469 bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAProblem &problem,
470                                      const PBQP::Solution &solution) {
471   // Set to true if we have any spills
472   bool anotherRoundNeeded = false;
473
474   // Clear the existing allocation.
475   vrm->clearAllVirt();
476
477   const PBQPRAGraph &g = problem.getGraph();
478   // Iterate over the nodes mapping the PBQP solution to a register
479   // assignment.
480   for (auto NId : g.nodeIds()) {
481     unsigned vreg = problem.getVRegForNode(NId);
482     unsigned alloc = solution.getSelection(NId);
483
484     if (problem.isPRegOption(vreg, alloc)) {
485       unsigned preg = problem.getPRegForOption(vreg, alloc);
486       DEBUG(dbgs() << "VREG " << PrintReg(vreg, tri) << " -> "
487             << tri->getName(preg) << "\n");
488       assert(preg != 0 && "Invalid preg selected.");
489       vrm->assignVirt2Phys(vreg, preg);
490     } else if (problem.isSpillOption(vreg, alloc)) {
491       vregsToAlloc.erase(vreg);
492       SmallVector<unsigned, 8> newSpills;
493       LiveRangeEdit LRE(&lis->getInterval(vreg), newSpills, *mf, *lis, vrm);
494       spiller->spill(LRE);
495
496       DEBUG(dbgs() << "VREG " << PrintReg(vreg, tri) << " -> SPILLED (Cost: "
497                    << LRE.getParent().weight << ", New vregs: ");
498
499       // Copy any newly inserted live intervals into the list of regs to
500       // allocate.
501       for (LiveRangeEdit::iterator itr = LRE.begin(), end = LRE.end();
502            itr != end; ++itr) {
503         LiveInterval &li = lis->getInterval(*itr);
504         assert(!li.empty() && "Empty spill range.");
505         DEBUG(dbgs() << PrintReg(li.reg, tri) << " ");
506         vregsToAlloc.insert(li.reg);
507       }
508
509       DEBUG(dbgs() << ")\n");
510
511       // We need another round if spill intervals were added.
512       anotherRoundNeeded |= !LRE.empty();
513     } else {
514       llvm_unreachable("Unknown allocation option.");
515     }
516   }
517
518   return !anotherRoundNeeded;
519 }
520
521
522 void RegAllocPBQP::finalizeAlloc() const {
523   // First allocate registers for the empty intervals.
524   for (RegSet::const_iterator
525          itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end();
526          itr != end; ++itr) {
527     LiveInterval *li = &lis->getInterval(*itr);
528
529     unsigned physReg = mri->getSimpleHint(li->reg);
530
531     if (physReg == 0) {
532       const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
533       physReg = liRC->getRawAllocationOrder(*mf).front();
534     }
535
536     vrm->assignVirt2Phys(li->reg, physReg);
537   }
538 }
539
540 bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
541
542   mf = &MF;
543   tm = &mf->getTarget();
544   tri = tm->getRegisterInfo();
545   tii = tm->getInstrInfo();
546   mri = &mf->getRegInfo();
547
548   lis = &getAnalysis<LiveIntervals>();
549   lss = &getAnalysis<LiveStacks>();
550   mbfi = &getAnalysis<MachineBlockFrequencyInfo>();
551
552   calculateSpillWeightsAndHints(*lis, MF, getAnalysis<MachineLoopInfo>(),
553                                 *mbfi);
554
555   vrm = &getAnalysis<VirtRegMap>();
556   spiller.reset(createInlineSpiller(*this, MF, *vrm));
557
558   mri->freezeReservedRegs(MF);
559
560   DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getName() << "\n");
561
562   // Allocator main loop:
563   //
564   // * Map current regalloc problem to a PBQP problem
565   // * Solve the PBQP problem
566   // * Map the solution back to a register allocation
567   // * Spill if necessary
568   //
569   // This process is continued till no more spills are generated.
570
571   // Find the vreg intervals in need of allocation.
572   findVRegIntervalsToAlloc();
573
574 #ifndef NDEBUG
575   const Function* func = mf->getFunction();
576   std::string fqn =
577     func->getParent()->getModuleIdentifier() + "." +
578     func->getName().str();
579 #endif
580
581   // If there are non-empty intervals allocate them using pbqp.
582   if (!vregsToAlloc.empty()) {
583
584     bool pbqpAllocComplete = false;
585     unsigned round = 0;
586
587     while (!pbqpAllocComplete) {
588       DEBUG(dbgs() << "  PBQP Regalloc round " << round << ":\n");
589
590       OwningPtr<PBQPRAProblem> problem(
591         builder->build(mf, lis, mbfi, vregsToAlloc));
592
593 #ifndef NDEBUG
594       if (pbqpDumpGraphs) {
595         std::ostringstream rs;
596         rs << round;
597         std::string graphFileName(fqn + "." + rs.str() + ".pbqpgraph");
598         std::string tmp;
599         raw_fd_ostream os(graphFileName.c_str(), tmp, sys::fs::F_Text);
600         DEBUG(dbgs() << "Dumping graph for round " << round << " to \""
601               << graphFileName << "\"\n");
602         problem->getGraph().dump(os);
603       }
604 #endif
605
606       PBQP::Solution solution =
607         PBQP::RegAlloc::solve(problem->getGraph());
608
609       pbqpAllocComplete = mapPBQPToRegAlloc(*problem, solution);
610
611       ++round;
612     }
613   }
614
615   // Finalise allocation, allocate empty ranges.
616   finalizeAlloc();
617   vregsToAlloc.clear();
618   emptyIntervalVRegs.clear();
619
620   DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
621
622   return true;
623 }
624
625 FunctionPass* llvm::createPBQPRegisterAllocator(
626                                            OwningPtr<PBQPBuilder> &builder,
627                                            char *customPassID) {
628   return new RegAllocPBQP(builder, customPassID);
629 }
630
631 FunctionPass* llvm::createDefaultPBQPRegisterAllocator() {
632   OwningPtr<PBQPBuilder> Builder;
633   if (pbqpCoalescing)
634     Builder.reset(new PBQPBuilderWithCoalescing());
635   else
636     Builder.reset(new PBQPBuilder());
637   return createPBQPRegisterAllocator(Builder);
638 }
639
640 #undef DEBUG_TYPE