RegAllocPBQP.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. #define DEBUG_TYPE "regalloc"
  32. #include "LiveRangeEdit.h"
  33. #include "RenderMachineFunction.h"
  34. #include "Spiller.h"
  35. #include "VirtRegMap.h"
  36. #include "RegisterCoalescer.h"
  37. #include "llvm/Analysis/AliasAnalysis.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/MachineDominators.h"
  43. #include "llvm/CodeGen/MachineFunctionPass.h"
  44. #include "llvm/CodeGen/MachineLoopInfo.h"
  45. #include "llvm/CodeGen/MachineRegisterInfo.h"
  46. #include "llvm/CodeGen/PBQP/HeuristicSolver.h"
  47. #include "llvm/CodeGen/PBQP/Graph.h"
  48. #include "llvm/CodeGen/PBQP/Heuristics/Briggs.h"
  49. #include "llvm/CodeGen/RegAllocRegistry.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>
  58. using namespace llvm;
  59. static RegisterRegAlloc
  60. registerPBQPRepAlloc("pbqp", "PBQP register allocator",
  61. createDefaultPBQPRegisterAllocator);
  62. static cl::opt<bool>
  63. pbqpCoalescing("pbqp-coalescing",
  64. cl::desc("Attempt coalescing during PBQP register allocation."),
  65. cl::init(false), cl::Hidden);
  66. namespace {
  67. ///
  68. /// PBQP based allocators solve the register allocation problem by mapping
  69. /// register allocation problems to Partitioned Boolean Quadratic
  70. /// Programming problems.
  71. class RegAllocPBQP : public MachineFunctionPass {
  72. public:
  73. static char ID;
  74. /// Construct a PBQP register allocator.
  75. RegAllocPBQP(std::auto_ptr<PBQPBuilder> b, char *cPassID=0)
  76. : MachineFunctionPass(ID), builder(b), customPassID(cPassID) {
  77. initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
  78. initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
  79. initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
  80. initializeLiveStacksPass(*PassRegistry::getPassRegistry());
  81. initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
  82. initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
  83. initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry());
  84. }
  85. /// Return the pass name.
  86. virtual const char* getPassName() const {
  87. return "PBQP Register Allocator";
  88. }
  89. /// PBQP analysis usage.
  90. virtual void getAnalysisUsage(AnalysisUsage &au) const;
  91. /// Perform register allocation
  92. virtual bool runOnMachineFunction(MachineFunction &MF);
  93. private:
  94. typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
  95. typedef std::vector<const LiveInterval*> Node2LIMap;
  96. typedef std::vector<unsigned> AllowedSet;
  97. typedef std::vector<AllowedSet> AllowedSetMap;
  98. typedef std::pair<unsigned, unsigned> RegPair;
  99. typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
  100. typedef std::vector<PBQP::Graph::NodeItr> NodeVector;
  101. typedef std::set<unsigned> RegSet;
  102. std::auto_ptr<PBQPBuilder> builder;
  103. char *customPassID;
  104. MachineFunction *mf;
  105. const TargetMachine *tm;
  106. const TargetRegisterInfo *tri;
  107. const TargetInstrInfo *tii;
  108. const MachineLoopInfo *loopInfo;
  109. MachineRegisterInfo *mri;
  110. RenderMachineFunction *rmf;
  111. std::auto_ptr<Spiller> spiller;
  112. LiveIntervals *lis;
  113. LiveStacks *lss;
  114. VirtRegMap *vrm;
  115. RegSet vregsToAlloc, emptyIntervalVRegs;
  116. /// \brief Finds the initial set of vreg intervals to allocate.
  117. void findVRegIntervalsToAlloc();
  118. /// \brief Given a solved PBQP problem maps this solution back to a register
  119. /// assignment.
  120. bool mapPBQPToRegAlloc(const PBQPRAProblem &problem,
  121. const PBQP::Solution &solution);
  122. /// \brief Postprocessing before final spilling. Sets basic block "live in"
  123. /// variables.
  124. void finalizeAlloc() const;
  125. };
  126. char RegAllocPBQP::ID = 0;
  127. } // End anonymous namespace.
  128. unsigned PBQPRAProblem::getVRegForNode(PBQP::Graph::ConstNodeItr node) const {
  129. Node2VReg::const_iterator vregItr = node2VReg.find(node);
  130. assert(vregItr != node2VReg.end() && "No vreg for node.");
  131. return vregItr->second;
  132. }
  133. PBQP::Graph::NodeItr PBQPRAProblem::getNodeForVReg(unsigned vreg) const {
  134. VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg);
  135. assert(nodeItr != vreg2Node.end() && "No node for vreg.");
  136. return nodeItr->second;
  137. }
  138. const PBQPRAProblem::AllowedSet&
  139. PBQPRAProblem::getAllowedSet(unsigned vreg) const {
  140. AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg);
  141. assert(allowedSetItr != allowedSets.end() && "No pregs for vreg.");
  142. const AllowedSet &allowedSet = allowedSetItr->second;
  143. return allowedSet;
  144. }
  145. unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const {
  146. assert(isPRegOption(vreg, option) && "Not a preg option.");
  147. const AllowedSet& allowedSet = getAllowedSet(vreg);
  148. assert(option <= allowedSet.size() && "Option outside allowed set.");
  149. return allowedSet[option - 1];
  150. }
  151. std::auto_ptr<PBQPRAProblem> PBQPBuilder::build(MachineFunction *mf,
  152. const LiveIntervals *lis,
  153. const MachineLoopInfo *loopInfo,
  154. const RegSet &vregs) {
  155. typedef std::vector<const LiveInterval*> LIVector;
  156. MachineRegisterInfo *mri = &mf->getRegInfo();
  157. const TargetRegisterInfo *tri = mf->getTarget().getRegisterInfo();
  158. std::auto_ptr<PBQPRAProblem> p(new PBQPRAProblem());
  159. PBQP::Graph &g = p->getGraph();
  160. RegSet pregs;
  161. // Collect the set of preg intervals, record that they're used in the MF.
  162. for (LiveIntervals::const_iterator itr = lis->begin(), end = lis->end();
  163. itr != end; ++itr) {
  164. if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
  165. pregs.insert(itr->first);
  166. mri->setPhysRegUsed(itr->first);
  167. }
  168. }
  169. BitVector reservedRegs = tri->getReservedRegs(*mf);
  170. // Iterate over vregs.
  171. for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end();
  172. vregItr != vregEnd; ++vregItr) {
  173. unsigned vreg = *vregItr;
  174. const TargetRegisterClass *trc = mri->getRegClass(vreg);
  175. const LiveInterval *vregLI = &lis->getInterval(vreg);
  176. // Compute an initial allowed set for the current vreg.
  177. typedef std::vector<unsigned> VRAllowed;
  178. VRAllowed vrAllowed;
  179. ArrayRef<uint16_t> rawOrder = trc->getRawAllocationOrder(*mf);
  180. for (unsigned i = 0; i != rawOrder.size(); ++i) {
  181. unsigned preg = rawOrder[i];
  182. if (!reservedRegs.test(preg)) {
  183. vrAllowed.push_back(preg);
  184. }
  185. }
  186. // Remove any physical registers which overlap.
  187. for (RegSet::const_iterator pregItr = pregs.begin(),
  188. pregEnd = pregs.end();
  189. pregItr != pregEnd; ++pregItr) {
  190. unsigned preg = *pregItr;
  191. const LiveInterval *pregLI = &lis->getInterval(preg);
  192. if (pregLI->empty()) {
  193. continue;
  194. }
  195. if (!vregLI->overlaps(*pregLI)) {
  196. continue;
  197. }
  198. // Remove the register from the allowed set.
  199. VRAllowed::iterator eraseItr =
  200. std::find(vrAllowed.begin(), vrAllowed.end(), preg);
  201. if (eraseItr != vrAllowed.end()) {
  202. vrAllowed.erase(eraseItr);
  203. }
  204. // Also remove any aliases.
  205. const unsigned *aliasItr = tri->getAliasSet(preg);
  206. if (aliasItr != 0) {
  207. for (; *aliasItr != 0; ++aliasItr) {
  208. VRAllowed::iterator eraseItr =
  209. std::find(vrAllowed.begin(), vrAllowed.end(), *aliasItr);
  210. if (eraseItr != vrAllowed.end()) {
  211. vrAllowed.erase(eraseItr);
  212. }
  213. }
  214. }
  215. }
  216. // Construct the node.
  217. PBQP::Graph::NodeItr node =
  218. g.addNode(PBQP::Vector(vrAllowed.size() + 1, 0));
  219. // Record the mapping and allowed set in the problem.
  220. p->recordVReg(vreg, node, vrAllowed.begin(), vrAllowed.end());
  221. PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ?
  222. vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min();
  223. addSpillCosts(g.getNodeCosts(node), spillCost);
  224. }
  225. for (RegSet::const_iterator vr1Itr = vregs.begin(), vrEnd = vregs.end();
  226. vr1Itr != vrEnd; ++vr1Itr) {
  227. unsigned vr1 = *vr1Itr;
  228. const LiveInterval &l1 = lis->getInterval(vr1);
  229. const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1);
  230. for (RegSet::const_iterator vr2Itr = llvm::next(vr1Itr);
  231. vr2Itr != vrEnd; ++vr2Itr) {
  232. unsigned vr2 = *vr2Itr;
  233. const LiveInterval &l2 = lis->getInterval(vr2);
  234. const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2);
  235. assert(!l2.empty() && "Empty interval in vreg set?");
  236. if (l1.overlaps(l2)) {
  237. PBQP::Graph::EdgeItr edge =
  238. g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2),
  239. PBQP::Matrix(vr1Allowed.size()+1, vr2Allowed.size()+1, 0));
  240. addInterferenceCosts(g.getEdgeCosts(edge), vr1Allowed, vr2Allowed, tri);
  241. }
  242. }
  243. }
  244. return p;
  245. }
  246. void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec,
  247. PBQP::PBQPNum spillCost) {
  248. costVec[0] = spillCost;
  249. }
  250. void PBQPBuilder::addInterferenceCosts(
  251. PBQP::Matrix &costMat,
  252. const PBQPRAProblem::AllowedSet &vr1Allowed,
  253. const PBQPRAProblem::AllowedSet &vr2Allowed,
  254. const TargetRegisterInfo *tri) {
  255. assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch.");
  256. assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch.");
  257. for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
  258. unsigned preg1 = vr1Allowed[i];
  259. for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
  260. unsigned preg2 = vr2Allowed[j];
  261. if (tri->regsOverlap(preg1, preg2)) {
  262. costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
  263. }
  264. }
  265. }
  266. }
  267. std::auto_ptr<PBQPRAProblem> PBQPBuilderWithCoalescing::build(
  268. MachineFunction *mf,
  269. const LiveIntervals *lis,
  270. const MachineLoopInfo *loopInfo,
  271. const RegSet &vregs) {
  272. std::auto_ptr<PBQPRAProblem> p = PBQPBuilder::build(mf, lis, loopInfo, vregs);
  273. PBQP::Graph &g = p->getGraph();
  274. const TargetMachine &tm = mf->getTarget();
  275. CoalescerPair cp(*tm.getInstrInfo(), *tm.getRegisterInfo());
  276. // Scan the machine function and add a coalescing cost whenever CoalescerPair
  277. // gives the Ok.
  278. for (MachineFunction::const_iterator mbbItr = mf->begin(),
  279. mbbEnd = mf->end();
  280. mbbItr != mbbEnd; ++mbbItr) {
  281. const MachineBasicBlock *mbb = &*mbbItr;
  282. for (MachineBasicBlock::const_iterator miItr = mbb->begin(),
  283. miEnd = mbb->end();
  284. miItr != miEnd; ++miItr) {
  285. const MachineInstr *mi = &*miItr;
  286. if (!cp.setRegisters(mi)) {
  287. continue; // Not coalescable.
  288. }
  289. if (cp.getSrcReg() == cp.getDstReg()) {
  290. continue; // Already coalesced.
  291. }
  292. unsigned dst = cp.getDstReg(),
  293. src = cp.getSrcReg();
  294. const float copyFactor = 0.5; // Cost of copy relative to load. Current
  295. // value plucked randomly out of the air.
  296. PBQP::PBQPNum cBenefit =
  297. copyFactor * LiveIntervals::getSpillWeight(false, true,
  298. loopInfo->getLoopDepth(mbb));
  299. if (cp.isPhys()) {
  300. if (!lis->isAllocatable(dst)) {
  301. continue;
  302. }
  303. const PBQPRAProblem::AllowedSet &allowed = p->getAllowedSet(src);
  304. unsigned pregOpt = 0;
  305. while (pregOpt < allowed.size() && allowed[pregOpt] != dst) {
  306. ++pregOpt;
  307. }
  308. if (pregOpt < allowed.size()) {
  309. ++pregOpt; // +1 to account for spill option.
  310. PBQP::Graph::NodeItr node = p->getNodeForVReg(src);
  311. addPhysRegCoalesce(g.getNodeCosts(node), pregOpt, cBenefit);
  312. }
  313. } else {
  314. const PBQPRAProblem::AllowedSet *allowed1 = &p->getAllowedSet(dst);
  315. const PBQPRAProblem::AllowedSet *allowed2 = &p->getAllowedSet(src);
  316. PBQP::Graph::NodeItr node1 = p->getNodeForVReg(dst);
  317. PBQP::Graph::NodeItr node2 = p->getNodeForVReg(src);
  318. PBQP::Graph::EdgeItr edge = g.findEdge(node1, node2);
  319. if (edge == g.edgesEnd()) {
  320. edge = g.addEdge(node1, node2, PBQP::Matrix(allowed1->size() + 1,
  321. allowed2->size() + 1,
  322. 0));
  323. } else {
  324. if (g.getEdgeNode1(edge) == node2) {
  325. std::swap(node1, node2);
  326. std::swap(allowed1, allowed2);
  327. }
  328. }
  329. addVirtRegCoalesce(g.getEdgeCosts(edge), *allowed1, *allowed2,
  330. cBenefit);
  331. }
  332. }
  333. }
  334. return p;
  335. }
  336. void PBQPBuilderWithCoalescing::addPhysRegCoalesce(PBQP::Vector &costVec,
  337. unsigned pregOption,
  338. PBQP::PBQPNum benefit) {
  339. costVec[pregOption] += -benefit;
  340. }
  341. void PBQPBuilderWithCoalescing::addVirtRegCoalesce(
  342. PBQP::Matrix &costMat,
  343. const PBQPRAProblem::AllowedSet &vr1Allowed,
  344. const PBQPRAProblem::AllowedSet &vr2Allowed,
  345. PBQP::PBQPNum benefit) {
  346. assert(costMat.getRows() == vr1Allowed.size() + 1 && "Size mismatch.");
  347. assert(costMat.getCols() == vr2Allowed.size() + 1 && "Size mismatch.");
  348. for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
  349. unsigned preg1 = vr1Allowed[i];
  350. for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
  351. unsigned preg2 = vr2Allowed[j];
  352. if (preg1 == preg2) {
  353. costMat[i + 1][j + 1] += -benefit;
  354. }
  355. }
  356. }
  357. }
  358. void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
  359. au.setPreservesCFG();
  360. au.addRequired<AliasAnalysis>();
  361. au.addPreserved<AliasAnalysis>();
  362. au.addRequired<SlotIndexes>();
  363. au.addPreserved<SlotIndexes>();
  364. au.addRequired<LiveIntervals>();
  365. //au.addRequiredID(SplitCriticalEdgesID);
  366. if (customPassID)
  367. au.addRequiredID(*customPassID);
  368. au.addRequired<CalculateSpillWeights>();
  369. au.addRequired<LiveStacks>();
  370. au.addPreserved<LiveStacks>();
  371. au.addRequired<MachineDominatorTree>();
  372. au.addPreserved<MachineDominatorTree>();
  373. au.addRequired<MachineLoopInfo>();
  374. au.addPreserved<MachineLoopInfo>();
  375. au.addRequired<VirtRegMap>();
  376. au.addRequired<RenderMachineFunction>();
  377. MachineFunctionPass::getAnalysisUsage(au);
  378. }
  379. void RegAllocPBQP::findVRegIntervalsToAlloc() {
  380. // Iterate over all live ranges.
  381. for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
  382. itr != end; ++itr) {
  383. // Ignore physical ones.
  384. if (TargetRegisterInfo::isPhysicalRegister(itr->first))
  385. continue;
  386. LiveInterval *li = itr->second;
  387. // If this live interval is non-empty we will use pbqp to allocate it.
  388. // Empty intervals we allocate in a simple post-processing stage in
  389. // finalizeAlloc.
  390. if (!li->empty()) {
  391. vregsToAlloc.insert(li->reg);
  392. } else {
  393. emptyIntervalVRegs.insert(li->reg);
  394. }
  395. }
  396. }
  397. bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAProblem &problem,
  398. const PBQP::Solution &solution) {
  399. // Set to true if we have any spills
  400. bool anotherRoundNeeded = false;
  401. // Clear the existing allocation.
  402. vrm->clearAllVirt();
  403. const PBQP::Graph &g = problem.getGraph();
  404. // Iterate over the nodes mapping the PBQP solution to a register
  405. // assignment.
  406. for (PBQP::Graph::ConstNodeItr node = g.nodesBegin(),
  407. nodeEnd = g.nodesEnd();
  408. node != nodeEnd; ++node) {
  409. unsigned vreg = problem.getVRegForNode(node);
  410. unsigned alloc = solution.getSelection(node);
  411. if (problem.isPRegOption(vreg, alloc)) {
  412. unsigned preg = problem.getPRegForOption(vreg, alloc);
  413. DEBUG(dbgs() << "VREG " << vreg << " -> " << tri->getName(preg) << "\n");
  414. assert(preg != 0 && "Invalid preg selected.");
  415. vrm->assignVirt2Phys(vreg, preg);
  416. } else if (problem.isSpillOption(vreg, alloc)) {
  417. vregsToAlloc.erase(vreg);
  418. SmallVector<LiveInterval*, 8> newSpills;
  419. LiveRangeEdit LRE(lis->getInterval(vreg), newSpills);
  420. spiller->spill(LRE);
  421. DEBUG(dbgs() << "VREG " << vreg << " -> SPILLED (Cost: "
  422. << LRE.getParent().weight << ", New vregs: ");
  423. // Copy any newly inserted live intervals into the list of regs to
  424. // allocate.
  425. for (LiveRangeEdit::iterator itr = LRE.begin(), end = LRE.end();
  426. itr != end; ++itr) {
  427. assert(!(*itr)->empty() && "Empty spill range.");
  428. DEBUG(dbgs() << (*itr)->reg << " ");
  429. vregsToAlloc.insert((*itr)->reg);
  430. }
  431. DEBUG(dbgs() << ")\n");
  432. // We need another round if spill intervals were added.
  433. anotherRoundNeeded |= !LRE.empty();
  434. } else {
  435. llvm_unreachable("Unknown allocation option.");
  436. }
  437. }
  438. return !anotherRoundNeeded;
  439. }
  440. void RegAllocPBQP::finalizeAlloc() const {
  441. typedef LiveIntervals::iterator LIIterator;
  442. typedef LiveInterval::Ranges::const_iterator LRIterator;
  443. // First allocate registers for the empty intervals.
  444. for (RegSet::const_iterator
  445. itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end();
  446. itr != end; ++itr) {
  447. LiveInterval *li = &lis->getInterval(*itr);
  448. unsigned physReg = vrm->getRegAllocPref(li->reg);
  449. if (physReg == 0) {
  450. const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
  451. physReg = liRC->getRawAllocationOrder(*mf).front();
  452. }
  453. vrm->assignVirt2Phys(li->reg, physReg);
  454. }
  455. // Finally iterate over the basic blocks to compute and set the live-in sets.
  456. SmallVector<MachineBasicBlock*, 8> liveInMBBs;
  457. MachineBasicBlock *entryMBB = &*mf->begin();
  458. for (LIIterator liItr = lis->begin(), liEnd = lis->end();
  459. liItr != liEnd; ++liItr) {
  460. const LiveInterval *li = liItr->second;
  461. unsigned reg = 0;
  462. // Get the physical register for this interval
  463. if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
  464. reg = li->reg;
  465. } else if (vrm->isAssignedReg(li->reg)) {
  466. reg = vrm->getPhys(li->reg);
  467. } else {
  468. // Ranges which are assigned a stack slot only are ignored.
  469. continue;
  470. }
  471. if (reg == 0) {
  472. // Filter out zero regs - they're for intervals that were spilled.
  473. continue;
  474. }
  475. // Iterate over the ranges of the current interval...
  476. for (LRIterator lrItr = li->begin(), lrEnd = li->end();
  477. lrItr != lrEnd; ++lrItr) {
  478. // Find the set of basic blocks which this range is live into...
  479. if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
  480. // And add the physreg for this interval to their live-in sets.
  481. for (unsigned i = 0; i != liveInMBBs.size(); ++i) {
  482. if (liveInMBBs[i] != entryMBB) {
  483. if (!liveInMBBs[i]->isLiveIn(reg)) {
  484. liveInMBBs[i]->addLiveIn(reg);
  485. }
  486. }
  487. }
  488. liveInMBBs.clear();
  489. }
  490. }
  491. }
  492. }
  493. bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
  494. mf = &MF;
  495. tm = &mf->getTarget();
  496. tri = tm->getRegisterInfo();
  497. tii = tm->getInstrInfo();
  498. mri = &mf->getRegInfo();
  499. lis = &getAnalysis<LiveIntervals>();
  500. lss = &getAnalysis<LiveStacks>();
  501. loopInfo = &getAnalysis<MachineLoopInfo>();
  502. rmf = &getAnalysis<RenderMachineFunction>();
  503. vrm = &getAnalysis<VirtRegMap>();
  504. spiller.reset(createInlineSpiller(*this, MF, *vrm));
  505. mri->freezeReservedRegs(MF);
  506. DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n");
  507. // Allocator main loop:
  508. //
  509. // * Map current regalloc problem to a PBQP problem
  510. // * Solve the PBQP problem
  511. // * Map the solution back to a register allocation
  512. // * Spill if necessary
  513. //
  514. // This process is continued till no more spills are generated.
  515. // Find the vreg intervals in need of allocation.
  516. findVRegIntervalsToAlloc();
  517. // If there are non-empty intervals allocate them using pbqp.
  518. if (!vregsToAlloc.empty()) {
  519. bool pbqpAllocComplete = false;
  520. unsigned round = 0;
  521. while (!pbqpAllocComplete) {
  522. DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
  523. std::auto_ptr<PBQPRAProblem> problem =
  524. builder->build(mf, lis, loopInfo, vregsToAlloc);
  525. PBQP::Solution solution =
  526. PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(
  527. problem->getGraph());
  528. pbqpAllocComplete = mapPBQPToRegAlloc(*problem, solution);
  529. ++round;
  530. }
  531. }
  532. // Finalise allocation, allocate empty ranges.
  533. finalizeAlloc();
  534. rmf->renderMachineFunction("After PBQP register allocation.", vrm);
  535. vregsToAlloc.clear();
  536. emptyIntervalVRegs.clear();
  537. DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
  538. // Run rewriter
  539. vrm->rewrite(lis->getSlotIndexes());
  540. // All machine operands and other references to virtual registers have been
  541. // replaced. Remove the virtual registers.
  542. vrm->clearAllVirt();
  543. mri->clearVirtRegs();
  544. return true;
  545. }
  546. FunctionPass* llvm::createPBQPRegisterAllocator(
  547. std::auto_ptr<PBQPBuilder> builder,
  548. char *customPassID) {
  549. return new RegAllocPBQP(builder, customPassID);
  550. }
  551. FunctionPass* llvm::createDefaultPBQPRegisterAllocator() {
  552. if (pbqpCoalescing) {
  553. return createPBQPRegisterAllocator(
  554. std::auto_ptr<PBQPBuilder>(new PBQPBuilderWithCoalescing()));
  555. } // else
  556. return createPBQPRegisterAllocator(
  557. std::auto_ptr<PBQPBuilder>(new PBQPBuilder()));
  558. }
  559. #undef DEBUG_TYPE