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