LLVM API Documentation

RegAllocPBQP.cpp
Go to the documentation of this file.
00001 //===------ RegAllocPBQP.cpp ---- PBQP Register Allocator -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains a Partitioned Boolean Quadratic Programming (PBQP) based
00011 // register allocator for LLVM. This allocator works by constructing a PBQP
00012 // problem representing the register allocation problem under consideration,
00013 // solving this using a PBQP solver, and mapping the solution back to a
00014 // register assignment. If any variables are selected for spilling then spill
00015 // code is inserted and the process repeated.
00016 //
00017 // The PBQP solver (pbqp.c) provided for this allocator uses a heuristic tuned
00018 // for register allocation. For more information on PBQP for register
00019 // allocation, see the following papers:
00020 //
00021 //   (1) Hames, L. and Scholz, B. 2006. Nearly optimal register allocation with
00022 //   PBQP. In Proceedings of the 7th Joint Modular Languages Conference
00023 //   (JMLC'06). LNCS, vol. 4228. Springer, New York, NY, USA. 346-361.
00024 //
00025 //   (2) Scholz, B., Eckstein, E. 2002. Register allocation for irregular
00026 //   architectures. In Proceedings of the Joint Conference on Languages,
00027 //   Compilers and Tools for Embedded Systems (LCTES'02), ACM Press, New York,
00028 //   NY, USA, 139-148.
00029 //
00030 //===----------------------------------------------------------------------===//
00031 
00032 #include "llvm/CodeGen/RegAllocPBQP.h"
00033 #include "RegisterCoalescer.h"
00034 #include "Spiller.h"
00035 #include "llvm/Analysis/AliasAnalysis.h"
00036 #include "llvm/CodeGen/CalcSpillWeights.h"
00037 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
00038 #include "llvm/CodeGen/LiveRangeEdit.h"
00039 #include "llvm/CodeGen/LiveStackAnalysis.h"
00040 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
00041 #include "llvm/CodeGen/MachineDominators.h"
00042 #include "llvm/CodeGen/MachineFunctionPass.h"
00043 #include "llvm/CodeGen/MachineLoopInfo.h"
00044 #include "llvm/CodeGen/MachineRegisterInfo.h"
00045 #include "llvm/CodeGen/RegAllocRegistry.h"
00046 #include "llvm/CodeGen/VirtRegMap.h"
00047 #include "llvm/IR/Module.h"
00048 #include "llvm/Support/Debug.h"
00049 #include "llvm/Support/FileSystem.h"
00050 #include "llvm/Support/raw_ostream.h"
00051 #include "llvm/Target/TargetInstrInfo.h"
00052 #include "llvm/Target/TargetMachine.h"
00053 #include "llvm/Target/TargetSubtargetInfo.h"
00054 #include <limits>
00055 #include <memory>
00056 #include <set>
00057 #include <sstream>
00058 #include <vector>
00059 
00060 using namespace llvm;
00061 
00062 #define DEBUG_TYPE "regalloc"
00063 
00064 static RegisterRegAlloc
00065 registerPBQPRepAlloc("pbqp", "PBQP register allocator",
00066                        createDefaultPBQPRegisterAllocator);
00067 
00068 static cl::opt<bool>
00069 pbqpCoalescing("pbqp-coalescing",
00070                 cl::desc("Attempt coalescing during PBQP register allocation."),
00071                 cl::init(false), cl::Hidden);
00072 
00073 #ifndef NDEBUG
00074 static cl::opt<bool>
00075 pbqpDumpGraphs("pbqp-dump-graphs",
00076                cl::desc("Dump graphs for each function/round in the compilation unit."),
00077                cl::init(false), cl::Hidden);
00078 #endif
00079 
00080 namespace {
00081 
00082 ///
00083 /// PBQP based allocators solve the register allocation problem by mapping
00084 /// register allocation problems to Partitioned Boolean Quadratic
00085 /// Programming problems.
00086 class RegAllocPBQP : public MachineFunctionPass {
00087 public:
00088 
00089   static char ID;
00090 
00091   /// Construct a PBQP register allocator.
00092   RegAllocPBQP(std::unique_ptr<PBQPBuilder> b, char *cPassID = nullptr)
00093       : MachineFunctionPass(ID), builder(std::move(b)), customPassID(cPassID) {
00094     initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
00095     initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
00096     initializeLiveStacksPass(*PassRegistry::getPassRegistry());
00097     initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
00098   }
00099 
00100   /// Return the pass name.
00101   const char* getPassName() const override {
00102     return "PBQP Register Allocator";
00103   }
00104 
00105   /// PBQP analysis usage.
00106   void getAnalysisUsage(AnalysisUsage &au) const override;
00107 
00108   /// Perform register allocation
00109   bool runOnMachineFunction(MachineFunction &MF) override;
00110 
00111 private:
00112 
00113   typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
00114   typedef std::vector<const LiveInterval*> Node2LIMap;
00115   typedef std::vector<unsigned> AllowedSet;
00116   typedef std::vector<AllowedSet> AllowedSetMap;
00117   typedef std::pair<unsigned, unsigned> RegPair;
00118   typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
00119   typedef std::set<unsigned> RegSet;
00120 
00121   std::unique_ptr<PBQPBuilder> builder;
00122 
00123   char *customPassID;
00124 
00125   MachineFunction *mf;
00126   const TargetMachine *tm;
00127   const TargetRegisterInfo *tri;
00128   const TargetInstrInfo *tii;
00129   MachineRegisterInfo *mri;
00130   const MachineBlockFrequencyInfo *mbfi;
00131 
00132   std::unique_ptr<Spiller> spiller;
00133   LiveIntervals *lis;
00134   LiveStacks *lss;
00135   VirtRegMap *vrm;
00136 
00137   RegSet vregsToAlloc, emptyIntervalVRegs;
00138 
00139   /// \brief Finds the initial set of vreg intervals to allocate.
00140   void findVRegIntervalsToAlloc();
00141 
00142   /// \brief Given a solved PBQP problem maps this solution back to a register
00143   /// assignment.
00144   bool mapPBQPToRegAlloc(const PBQPRAProblem &problem,
00145                          const PBQP::Solution &solution);
00146 
00147   /// \brief Postprocessing before final spilling. Sets basic block "live in"
00148   /// variables.
00149   void finalizeAlloc() const;
00150 
00151 };
00152 
00153 char RegAllocPBQP::ID = 0;
00154 
00155 } // End anonymous namespace.
00156 
00157 unsigned PBQPRAProblem::getVRegForNode(PBQPRAGraph::NodeId node) const {
00158   Node2VReg::const_iterator vregItr = node2VReg.find(node);
00159   assert(vregItr != node2VReg.end() && "No vreg for node.");
00160   return vregItr->second;
00161 }
00162 
00163 PBQPRAGraph::NodeId PBQPRAProblem::getNodeForVReg(unsigned vreg) const {
00164   VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg);
00165   assert(nodeItr != vreg2Node.end() && "No node for vreg.");
00166   return nodeItr->second;
00167 
00168 }
00169 
00170 const PBQPRAProblem::AllowedSet&
00171   PBQPRAProblem::getAllowedSet(unsigned vreg) const {
00172   AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg);
00173   assert(allowedSetItr != allowedSets.end() && "No pregs for vreg.");
00174   const AllowedSet &allowedSet = allowedSetItr->second;
00175   return allowedSet;
00176 }
00177 
00178 unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const {
00179   assert(isPRegOption(vreg, option) && "Not a preg option.");
00180 
00181   const AllowedSet& allowedSet = getAllowedSet(vreg);
00182   assert(option <= allowedSet.size() && "Option outside allowed set.");
00183   return allowedSet[option - 1];
00184 }
00185 
00186 std::unique_ptr<PBQPRAProblem>
00187 PBQPBuilder::build(MachineFunction *mf, const LiveIntervals *lis,
00188                    const MachineBlockFrequencyInfo *mbfi, const RegSet &vregs) {
00189 
00190   LiveIntervals *LIS = const_cast<LiveIntervals*>(lis);
00191   MachineRegisterInfo *mri = &mf->getRegInfo();
00192   const TargetRegisterInfo *tri = mf->getSubtarget().getRegisterInfo();
00193 
00194   auto p = llvm::make_unique<PBQPRAProblem>();
00195   PBQPRAGraph &g = p->getGraph();
00196   RegSet pregs;
00197 
00198   // Collect the set of preg intervals, record that they're used in the MF.
00199   for (unsigned Reg = 1, e = tri->getNumRegs(); Reg != e; ++Reg) {
00200     if (mri->def_empty(Reg))
00201       continue;
00202     pregs.insert(Reg);
00203     mri->setPhysRegUsed(Reg);
00204   }
00205 
00206   // Iterate over vregs.
00207   for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end();
00208        vregItr != vregEnd; ++vregItr) {
00209     unsigned vreg = *vregItr;
00210     const TargetRegisterClass *trc = mri->getRegClass(vreg);
00211     LiveInterval *vregLI = &LIS->getInterval(vreg);
00212 
00213     // Record any overlaps with regmask operands.
00214     BitVector regMaskOverlaps;
00215     LIS->checkRegMaskInterference(*vregLI, regMaskOverlaps);
00216 
00217     // Compute an initial allowed set for the current vreg.
00218     typedef std::vector<unsigned> VRAllowed;
00219     VRAllowed vrAllowed;
00220     ArrayRef<MCPhysReg> rawOrder = trc->getRawAllocationOrder(*mf);
00221     for (unsigned i = 0; i != rawOrder.size(); ++i) {
00222       unsigned preg = rawOrder[i];
00223       if (mri->isReserved(preg))
00224         continue;
00225 
00226       // vregLI crosses a regmask operand that clobbers preg.
00227       if (!regMaskOverlaps.empty() && !regMaskOverlaps.test(preg))
00228         continue;
00229 
00230       // vregLI overlaps fixed regunit interference.
00231       bool Interference = false;
00232       for (MCRegUnitIterator Units(preg, tri); Units.isValid(); ++Units) {
00233         if (vregLI->overlaps(LIS->getRegUnit(*Units))) {
00234           Interference = true;
00235           break;
00236         }
00237       }
00238       if (Interference)
00239         continue;
00240 
00241       // preg is usable for this virtual register.
00242       vrAllowed.push_back(preg);
00243     }
00244 
00245     PBQP::Vector nodeCosts(vrAllowed.size() + 1, 0);
00246 
00247     PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ?
00248         vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min();
00249 
00250     addSpillCosts(nodeCosts, spillCost);
00251 
00252     // Construct the node.
00253     PBQPRAGraph::NodeId nId = g.addNode(std::move(nodeCosts));
00254 
00255     // Record the mapping and allowed set in the problem.
00256     p->recordVReg(vreg, nId, vrAllowed.begin(), vrAllowed.end());
00257 
00258   }
00259 
00260   for (RegSet::const_iterator vr1Itr = vregs.begin(), vrEnd = vregs.end();
00261          vr1Itr != vrEnd; ++vr1Itr) {
00262     unsigned vr1 = *vr1Itr;
00263     const LiveInterval &l1 = lis->getInterval(vr1);
00264     const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1);
00265 
00266     for (RegSet::const_iterator vr2Itr = std::next(vr1Itr); vr2Itr != vrEnd;
00267          ++vr2Itr) {
00268       unsigned vr2 = *vr2Itr;
00269       const LiveInterval &l2 = lis->getInterval(vr2);
00270       const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2);
00271 
00272       assert(!l2.empty() && "Empty interval in vreg set?");
00273       if (l1.overlaps(l2)) {
00274         PBQP::Matrix edgeCosts(vr1Allowed.size()+1, vr2Allowed.size()+1, 0);
00275         addInterferenceCosts(edgeCosts, vr1Allowed, vr2Allowed, tri);
00276 
00277         g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2),
00278                   std::move(edgeCosts));
00279       }
00280     }
00281   }
00282 
00283   return p;
00284 }
00285 
00286 void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec,
00287                                 PBQP::PBQPNum spillCost) {
00288   costVec[0] = spillCost;
00289 }
00290 
00291 void PBQPBuilder::addInterferenceCosts(
00292                                     PBQP::Matrix &costMat,
00293                                     const PBQPRAProblem::AllowedSet &vr1Allowed,
00294                                     const PBQPRAProblem::AllowedSet &vr2Allowed,
00295                                     const TargetRegisterInfo *tri) {
00296   assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch.");
00297   assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch.");
00298 
00299   for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
00300     unsigned preg1 = vr1Allowed[i];
00301 
00302     for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
00303       unsigned preg2 = vr2Allowed[j];
00304 
00305       if (tri->regsOverlap(preg1, preg2)) {
00306         costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
00307       }
00308     }
00309   }
00310 }
00311 
00312 std::unique_ptr<PBQPRAProblem>
00313 PBQPBuilderWithCoalescing::build(MachineFunction *mf, const LiveIntervals *lis,
00314                                  const MachineBlockFrequencyInfo *mbfi,
00315                                  const RegSet &vregs) {
00316 
00317   std::unique_ptr<PBQPRAProblem> p = PBQPBuilder::build(mf, lis, mbfi, vregs);
00318   PBQPRAGraph &g = p->getGraph();
00319 
00320   const TargetMachine &tm = mf->getTarget();
00321   CoalescerPair cp(*tm.getSubtargetImpl()->getRegisterInfo());
00322 
00323   // Scan the machine function and add a coalescing cost whenever CoalescerPair
00324   // gives the Ok.
00325   for (const auto &mbb : *mf) {
00326     for (const auto &mi : mbb) {
00327       if (!cp.setRegisters(&mi)) {
00328         continue; // Not coalescable.
00329       }
00330 
00331       if (cp.getSrcReg() == cp.getDstReg()) {
00332         continue; // Already coalesced.
00333       }
00334 
00335       unsigned dst = cp.getDstReg(),
00336                src = cp.getSrcReg();
00337 
00338       const float copyFactor = 0.5; // Cost of copy relative to load. Current
00339       // value plucked randomly out of the air.
00340 
00341       PBQP::PBQPNum cBenefit =
00342         copyFactor * LiveIntervals::getSpillWeight(false, true, mbfi, &mi);
00343 
00344       if (cp.isPhys()) {
00345         if (!mf->getRegInfo().isAllocatable(dst)) {
00346           continue;
00347         }
00348 
00349         const PBQPRAProblem::AllowedSet &allowed = p->getAllowedSet(src);
00350         unsigned pregOpt = 0;
00351         while (pregOpt < allowed.size() && allowed[pregOpt] != dst) {
00352           ++pregOpt;
00353         }
00354         if (pregOpt < allowed.size()) {
00355           ++pregOpt; // +1 to account for spill option.
00356           PBQPRAGraph::NodeId node = p->getNodeForVReg(src);
00357           DEBUG(llvm::dbgs() << "Reading node costs for node " << node << "\n");
00358           DEBUG(llvm::dbgs() << "Source node: " << &g.getNodeCosts(node) << "\n");
00359           PBQP::Vector newCosts(g.getNodeCosts(node));
00360           addPhysRegCoalesce(newCosts, pregOpt, cBenefit);
00361           g.setNodeCosts(node, newCosts);
00362         }
00363       } else {
00364         const PBQPRAProblem::AllowedSet *allowed1 = &p->getAllowedSet(dst);
00365         const PBQPRAProblem::AllowedSet *allowed2 = &p->getAllowedSet(src);
00366         PBQPRAGraph::NodeId node1 = p->getNodeForVReg(dst);
00367         PBQPRAGraph::NodeId node2 = p->getNodeForVReg(src);
00368         PBQPRAGraph::EdgeId edge = g.findEdge(node1, node2);
00369         if (edge == g.invalidEdgeId()) {
00370           PBQP::Matrix costs(allowed1->size() + 1, allowed2->size() + 1, 0);
00371           addVirtRegCoalesce(costs, *allowed1, *allowed2, cBenefit);
00372           g.addEdge(node1, node2, costs);
00373         } else {
00374           if (g.getEdgeNode1Id(edge) == node2) {
00375             std::swap(node1, node2);
00376             std::swap(allowed1, allowed2);
00377           }
00378           PBQP::Matrix costs(g.getEdgeCosts(edge));
00379           addVirtRegCoalesce(costs, *allowed1, *allowed2, cBenefit);
00380           g.setEdgeCosts(edge, costs);
00381         }
00382       }
00383     }
00384   }
00385 
00386   return p;
00387 }
00388 
00389 void PBQPBuilderWithCoalescing::addPhysRegCoalesce(PBQP::Vector &costVec,
00390                                                    unsigned pregOption,
00391                                                    PBQP::PBQPNum benefit) {
00392   costVec[pregOption] += -benefit;
00393 }
00394 
00395 void PBQPBuilderWithCoalescing::addVirtRegCoalesce(
00396                                     PBQP::Matrix &costMat,
00397                                     const PBQPRAProblem::AllowedSet &vr1Allowed,
00398                                     const PBQPRAProblem::AllowedSet &vr2Allowed,
00399                                     PBQP::PBQPNum benefit) {
00400 
00401   assert(costMat.getRows() == vr1Allowed.size() + 1 && "Size mismatch.");
00402   assert(costMat.getCols() == vr2Allowed.size() + 1 && "Size mismatch.");
00403 
00404   for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
00405     unsigned preg1 = vr1Allowed[i];
00406     for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
00407       unsigned preg2 = vr2Allowed[j];
00408 
00409       if (preg1 == preg2) {
00410         costMat[i + 1][j + 1] += -benefit;
00411       }
00412     }
00413   }
00414 }
00415 
00416 
00417 void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
00418   au.setPreservesCFG();
00419   au.addRequired<AliasAnalysis>();
00420   au.addPreserved<AliasAnalysis>();
00421   au.addRequired<SlotIndexes>();
00422   au.addPreserved<SlotIndexes>();
00423   au.addRequired<LiveIntervals>();
00424   au.addPreserved<LiveIntervals>();
00425   //au.addRequiredID(SplitCriticalEdgesID);
00426   if (customPassID)
00427     au.addRequiredID(*customPassID);
00428   au.addRequired<LiveStacks>();
00429   au.addPreserved<LiveStacks>();
00430   au.addRequired<MachineBlockFrequencyInfo>();
00431   au.addPreserved<MachineBlockFrequencyInfo>();
00432   au.addRequired<MachineLoopInfo>();
00433   au.addPreserved<MachineLoopInfo>();
00434   au.addRequired<MachineDominatorTree>();
00435   au.addPreserved<MachineDominatorTree>();
00436   au.addRequired<VirtRegMap>();
00437   au.addPreserved<VirtRegMap>();
00438   MachineFunctionPass::getAnalysisUsage(au);
00439 }
00440 
00441 void RegAllocPBQP::findVRegIntervalsToAlloc() {
00442 
00443   // Iterate over all live ranges.
00444   for (unsigned i = 0, e = mri->getNumVirtRegs(); i != e; ++i) {
00445     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
00446     if (mri->reg_nodbg_empty(Reg))
00447       continue;
00448     LiveInterval *li = &lis->getInterval(Reg);
00449 
00450     // If this live interval is non-empty we will use pbqp to allocate it.
00451     // Empty intervals we allocate in a simple post-processing stage in
00452     // finalizeAlloc.
00453     if (!li->empty()) {
00454       vregsToAlloc.insert(li->reg);
00455     } else {
00456       emptyIntervalVRegs.insert(li->reg);
00457     }
00458   }
00459 }
00460 
00461 bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAProblem &problem,
00462                                      const PBQP::Solution &solution) {
00463   // Set to true if we have any spills
00464   bool anotherRoundNeeded = false;
00465 
00466   // Clear the existing allocation.
00467   vrm->clearAllVirt();
00468 
00469   const PBQPRAGraph &g = problem.getGraph();
00470   // Iterate over the nodes mapping the PBQP solution to a register
00471   // assignment.
00472   for (auto NId : g.nodeIds()) {
00473     unsigned vreg = problem.getVRegForNode(NId);
00474     unsigned alloc = solution.getSelection(NId);
00475 
00476     if (problem.isPRegOption(vreg, alloc)) {
00477       unsigned preg = problem.getPRegForOption(vreg, alloc);
00478       DEBUG(dbgs() << "VREG " << PrintReg(vreg, tri) << " -> "
00479             << tri->getName(preg) << "\n");
00480       assert(preg != 0 && "Invalid preg selected.");
00481       vrm->assignVirt2Phys(vreg, preg);
00482     } else if (problem.isSpillOption(vreg, alloc)) {
00483       vregsToAlloc.erase(vreg);
00484       SmallVector<unsigned, 8> newSpills;
00485       LiveRangeEdit LRE(&lis->getInterval(vreg), newSpills, *mf, *lis, vrm);
00486       spiller->spill(LRE);
00487 
00488       DEBUG(dbgs() << "VREG " << PrintReg(vreg, tri) << " -> SPILLED (Cost: "
00489                    << LRE.getParent().weight << ", New vregs: ");
00490 
00491       // Copy any newly inserted live intervals into the list of regs to
00492       // allocate.
00493       for (LiveRangeEdit::iterator itr = LRE.begin(), end = LRE.end();
00494            itr != end; ++itr) {
00495         LiveInterval &li = lis->getInterval(*itr);
00496         assert(!li.empty() && "Empty spill range.");
00497         DEBUG(dbgs() << PrintReg(li.reg, tri) << " ");
00498         vregsToAlloc.insert(li.reg);
00499       }
00500 
00501       DEBUG(dbgs() << ")\n");
00502 
00503       // We need another round if spill intervals were added.
00504       anotherRoundNeeded |= !LRE.empty();
00505     } else {
00506       llvm_unreachable("Unknown allocation option.");
00507     }
00508   }
00509 
00510   return !anotherRoundNeeded;
00511 }
00512 
00513 
00514 void RegAllocPBQP::finalizeAlloc() const {
00515   // First allocate registers for the empty intervals.
00516   for (RegSet::const_iterator
00517          itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end();
00518          itr != end; ++itr) {
00519     LiveInterval *li = &lis->getInterval(*itr);
00520 
00521     unsigned physReg = mri->getSimpleHint(li->reg);
00522 
00523     if (physReg == 0) {
00524       const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
00525       physReg = liRC->getRawAllocationOrder(*mf).front();
00526     }
00527 
00528     vrm->assignVirt2Phys(li->reg, physReg);
00529   }
00530 }
00531 
00532 bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
00533 
00534   mf = &MF;
00535   tm = &mf->getTarget();
00536   tri = tm->getSubtargetImpl()->getRegisterInfo();
00537   tii = tm->getSubtargetImpl()->getInstrInfo();
00538   mri = &mf->getRegInfo();
00539 
00540   lis = &getAnalysis<LiveIntervals>();
00541   lss = &getAnalysis<LiveStacks>();
00542   mbfi = &getAnalysis<MachineBlockFrequencyInfo>();
00543 
00544   calculateSpillWeightsAndHints(*lis, MF, getAnalysis<MachineLoopInfo>(),
00545                                 *mbfi);
00546 
00547   vrm = &getAnalysis<VirtRegMap>();
00548   spiller.reset(createInlineSpiller(*this, MF, *vrm));
00549 
00550   mri->freezeReservedRegs(MF);
00551 
00552   DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getName() << "\n");
00553 
00554   // Allocator main loop:
00555   //
00556   // * Map current regalloc problem to a PBQP problem
00557   // * Solve the PBQP problem
00558   // * Map the solution back to a register allocation
00559   // * Spill if necessary
00560   //
00561   // This process is continued till no more spills are generated.
00562 
00563   // Find the vreg intervals in need of allocation.
00564   findVRegIntervalsToAlloc();
00565 
00566 #ifndef NDEBUG
00567   const Function* func = mf->getFunction();
00568   std::string fqn =
00569     func->getParent()->getModuleIdentifier() + "." +
00570     func->getName().str();
00571 #endif
00572 
00573   // If there are non-empty intervals allocate them using pbqp.
00574   if (!vregsToAlloc.empty()) {
00575 
00576     bool pbqpAllocComplete = false;
00577     unsigned round = 0;
00578 
00579     while (!pbqpAllocComplete) {
00580       DEBUG(dbgs() << "  PBQP Regalloc round " << round << ":\n");
00581 
00582       std::unique_ptr<PBQPRAProblem> problem =
00583           builder->build(mf, lis, mbfi, vregsToAlloc);
00584 
00585 #ifndef NDEBUG
00586       if (pbqpDumpGraphs) {
00587         std::ostringstream rs;
00588         rs << round;
00589         std::string graphFileName(fqn + "." + rs.str() + ".pbqpgraph");
00590         std::error_code EC;
00591         raw_fd_ostream os(graphFileName, EC, sys::fs::F_Text);
00592         DEBUG(dbgs() << "Dumping graph for round " << round << " to \""
00593               << graphFileName << "\"\n");
00594         problem->getGraph().dump(os);
00595       }
00596 #endif
00597 
00598       PBQP::Solution solution =
00599         PBQP::RegAlloc::solve(problem->getGraph());
00600 
00601       pbqpAllocComplete = mapPBQPToRegAlloc(*problem, solution);
00602 
00603       ++round;
00604     }
00605   }
00606 
00607   // Finalise allocation, allocate empty ranges.
00608   finalizeAlloc();
00609   vregsToAlloc.clear();
00610   emptyIntervalVRegs.clear();
00611 
00612   DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
00613 
00614   return true;
00615 }
00616 
00617 FunctionPass *
00618 llvm::createPBQPRegisterAllocator(std::unique_ptr<PBQPBuilder> builder,
00619                                   char *customPassID) {
00620   return new RegAllocPBQP(std::move(builder), customPassID);
00621 }
00622 
00623 FunctionPass* llvm::createDefaultPBQPRegisterAllocator() {
00624   std::unique_ptr<PBQPBuilder> Builder;
00625   if (pbqpCoalescing)
00626     Builder = llvm::make_unique<PBQPBuilderWithCoalescing>();
00627   else
00628     Builder = llvm::make_unique<PBQPBuilder>();
00629   return createPBQPRegisterAllocator(std::move(Builder));
00630 }
00631 
00632 #undef DEBUG_TYPE