blob: cfdfa59d6d4d0a16b4dcb13e5a51e67c401a14cf [file] [log] [blame]
Evan Chengb1290a62008-10-02 18:29:27 +00001//===------ 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//===----------------------------------------------------------------------===//
Misha Brukman2a835f92009-01-08 15:50:22 +00009//
Evan Chengb1290a62008-10-02 18:29:27 +000010// 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
Misha Brukman2a835f92009-01-08 15:50:22 +000015// code is inserted and the process repeated.
Evan Chengb1290a62008-10-02 18:29:27 +000016//
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
Misha Brukmance07e992009-01-08 16:40:25 +000019// allocation, see the following papers:
Evan Chengb1290a62008-10-02 18:29:27 +000020//
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.
Misha Brukman2a835f92009-01-08 15:50:22 +000029//
Evan Chengb1290a62008-10-02 18:29:27 +000030//===----------------------------------------------------------------------===//
31
Evan Chengb1290a62008-10-02 18:29:27 +000032#define DEBUG_TYPE "regalloc"
33
Lang Hames54cc2ef2010-07-19 15:22:28 +000034#include "RenderMachineFunction.h"
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +000035#include "Spiller.h"
Evan Chengb1290a62008-10-02 18:29:27 +000036#include "VirtRegMap.h"
Rafael Espindolafdf16ca2011-06-26 21:41:06 +000037#include "RegisterCoalescer.h"
Lang Hames20df03c2012-03-26 23:07:23 +000038#include "llvm/Module.h"
Lang Hames9ad7e072011-12-06 01:45:57 +000039#include "llvm/Analysis/AliasAnalysis.h"
Lang Hamesa937f222009-12-14 06:49:42 +000040#include "llvm/CodeGen/CalcSpillWeights.h"
Evan Chengb1290a62008-10-02 18:29:27 +000041#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000042#include "llvm/CodeGen/LiveRangeEdit.h"
Lang Hames27601ef2008-11-16 12:12:54 +000043#include "llvm/CodeGen/LiveStackAnalysis.h"
Lang Hameseb6c8f52010-09-18 09:07:10 +000044#include "llvm/CodeGen/RegAllocPBQP.h"
Lang Hames9ad7e072011-12-06 01:45:57 +000045#include "llvm/CodeGen/MachineDominators.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000046#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chengb1290a62008-10-02 18:29:27 +000047#include "llvm/CodeGen/MachineLoopInfo.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000048#include "llvm/CodeGen/MachineRegisterInfo.h"
Lang Hameseb6c8f52010-09-18 09:07:10 +000049#include "llvm/CodeGen/PBQP/HeuristicSolver.h"
50#include "llvm/CodeGen/PBQP/Graph.h"
51#include "llvm/CodeGen/PBQP/Heuristics/Briggs.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000052#include "llvm/CodeGen/RegAllocRegistry.h"
Evan Chengb1290a62008-10-02 18:29:27 +000053#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000054#include "llvm/Support/raw_ostream.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000055#include "llvm/Target/TargetInstrInfo.h"
56#include "llvm/Target/TargetMachine.h"
57#include <limits>
Misha Brukman2a835f92009-01-08 15:50:22 +000058#include <memory>
Evan Chengb1290a62008-10-02 18:29:27 +000059#include <set>
Lang Hames20df03c2012-03-26 23:07:23 +000060#include <sstream>
Evan Chengb1290a62008-10-02 18:29:27 +000061#include <vector>
Evan Chengb1290a62008-10-02 18:29:27 +000062
Lang Hamesf70e7cc2010-09-23 04:28:54 +000063using namespace llvm;
Lang Hameseb6c8f52010-09-18 09:07:10 +000064
Evan Chengb1290a62008-10-02 18:29:27 +000065static RegisterRegAlloc
Duncan Sands1aecd152010-02-18 14:10:41 +000066registerPBQPRepAlloc("pbqp", "PBQP register allocator",
Lang Hamesf70e7cc2010-09-23 04:28:54 +000067 createDefaultPBQPRegisterAllocator);
Evan Chengb1290a62008-10-02 18:29:27 +000068
Lang Hames8481e3b2009-08-19 01:36:14 +000069static cl::opt<bool>
70pbqpCoalescing("pbqp-coalescing",
Lang Hames030c4bf2010-01-26 04:49:58 +000071 cl::desc("Attempt coalescing during PBQP register allocation."),
72 cl::init(false), cl::Hidden);
Lang Hames8481e3b2009-08-19 01:36:14 +000073
Lang Hames20df03c2012-03-26 23:07:23 +000074#ifndef NDEBUG
75static cl::opt<bool>
76pbqpDumpGraphs("pbqp-dump-graphs",
77 cl::desc("Dump graphs for each function/round in the compilation unit."),
78 cl::init(false), cl::Hidden);
79#endif
80
Lang Hamesf70e7cc2010-09-23 04:28:54 +000081namespace {
82
83///
84/// PBQP based allocators solve the register allocation problem by mapping
85/// register allocation problems to Partitioned Boolean Quadratic
86/// Programming problems.
87class RegAllocPBQP : public MachineFunctionPass {
88public:
89
90 static char ID;
91
92 /// Construct a PBQP register allocator.
Lang Hames8d857662011-06-17 07:09:01 +000093 RegAllocPBQP(std::auto_ptr<PBQPBuilder> b, char *cPassID=0)
94 : MachineFunctionPass(ID), builder(b), customPassID(cPassID) {
Owen Anderson081c34b2010-10-19 17:21:58 +000095 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
96 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
Owen Anderson081c34b2010-10-19 17:21:58 +000097 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
98 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
99 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
Owen Anderson081c34b2010-10-19 17:21:58 +0000100 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
101 initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry());
102 }
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000103
104 /// Return the pass name.
105 virtual const char* getPassName() const {
106 return "PBQP Register Allocator";
107 }
108
109 /// PBQP analysis usage.
110 virtual void getAnalysisUsage(AnalysisUsage &au) const;
111
112 /// Perform register allocation
113 virtual bool runOnMachineFunction(MachineFunction &MF);
114
115private:
116
117 typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
118 typedef std::vector<const LiveInterval*> Node2LIMap;
119 typedef std::vector<unsigned> AllowedSet;
120 typedef std::vector<AllowedSet> AllowedSetMap;
121 typedef std::pair<unsigned, unsigned> RegPair;
122 typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
123 typedef std::vector<PBQP::Graph::NodeItr> NodeVector;
124 typedef std::set<unsigned> RegSet;
125
126
127 std::auto_ptr<PBQPBuilder> builder;
128
Lang Hames8d857662011-06-17 07:09:01 +0000129 char *customPassID;
130
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000131 MachineFunction *mf;
132 const TargetMachine *tm;
133 const TargetRegisterInfo *tri;
134 const TargetInstrInfo *tii;
135 const MachineLoopInfo *loopInfo;
136 MachineRegisterInfo *mri;
137 RenderMachineFunction *rmf;
138
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000139 std::auto_ptr<Spiller> spiller;
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000140 LiveIntervals *lis;
141 LiveStacks *lss;
142 VirtRegMap *vrm;
143
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000144 RegSet vregsToAlloc, emptyIntervalVRegs;
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000145
146 /// \brief Finds the initial set of vreg intervals to allocate.
147 void findVRegIntervalsToAlloc();
148
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000149 /// \brief Given a solved PBQP problem maps this solution back to a register
150 /// assignment.
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000151 bool mapPBQPToRegAlloc(const PBQPRAProblem &problem,
152 const PBQP::Solution &solution);
153
154 /// \brief Postprocessing before final spilling. Sets basic block "live in"
155 /// variables.
156 void finalizeAlloc() const;
157
158};
159
Lang Hameseb6c8f52010-09-18 09:07:10 +0000160char RegAllocPBQP::ID = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000161
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000162} // End anonymous namespace.
163
Lang Hameseb6c8f52010-09-18 09:07:10 +0000164unsigned PBQPRAProblem::getVRegForNode(PBQP::Graph::ConstNodeItr node) const {
165 Node2VReg::const_iterator vregItr = node2VReg.find(node);
166 assert(vregItr != node2VReg.end() && "No vreg for node.");
167 return vregItr->second;
168}
Evan Chengb1290a62008-10-02 18:29:27 +0000169
Lang Hameseb6c8f52010-09-18 09:07:10 +0000170PBQP::Graph::NodeItr PBQPRAProblem::getNodeForVReg(unsigned vreg) const {
171 VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg);
172 assert(nodeItr != vreg2Node.end() && "No node for vreg.");
173 return nodeItr->second;
Andrew Trick16f72dd2012-02-10 04:10:26 +0000174
Lang Hameseb6c8f52010-09-18 09:07:10 +0000175}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000176
Lang Hameseb6c8f52010-09-18 09:07:10 +0000177const PBQPRAProblem::AllowedSet&
178 PBQPRAProblem::getAllowedSet(unsigned vreg) const {
179 AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg);
180 assert(allowedSetItr != allowedSets.end() && "No pregs for vreg.");
181 const AllowedSet &allowedSet = allowedSetItr->second;
182 return allowedSet;
183}
Evan Chengb1290a62008-10-02 18:29:27 +0000184
Lang Hameseb6c8f52010-09-18 09:07:10 +0000185unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const {
186 assert(isPRegOption(vreg, option) && "Not a preg option.");
187
188 const AllowedSet& allowedSet = getAllowedSet(vreg);
189 assert(option <= allowedSet.size() && "Option outside allowed set.");
190 return allowedSet[option - 1];
191}
192
Lang Hamese9c93562010-09-21 13:19:36 +0000193std::auto_ptr<PBQPRAProblem> PBQPBuilder::build(MachineFunction *mf,
194 const LiveIntervals *lis,
195 const MachineLoopInfo *loopInfo,
196 const RegSet &vregs) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000197
198 typedef std::vector<const LiveInterval*> LIVector;
Lang Hamesf1113ef2012-03-23 17:33:42 +0000199 ArrayRef<SlotIndex> regMaskSlots = lis->getRegMaskSlots();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000200 MachineRegisterInfo *mri = &mf->getRegInfo();
Andrew Trick16f72dd2012-02-10 04:10:26 +0000201 const TargetRegisterInfo *tri = mf->getTarget().getRegisterInfo();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000202
203 std::auto_ptr<PBQPRAProblem> p(new PBQPRAProblem());
204 PBQP::Graph &g = p->getGraph();
205 RegSet pregs;
206
207 // Collect the set of preg intervals, record that they're used in the MF.
208 for (LiveIntervals::const_iterator itr = lis->begin(), end = lis->end();
209 itr != end; ++itr) {
210 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
211 pregs.insert(itr->first);
212 mri->setPhysRegUsed(itr->first);
Evan Chengb1290a62008-10-02 18:29:27 +0000213 }
Lang Hameseb6c8f52010-09-18 09:07:10 +0000214 }
Evan Chengb1290a62008-10-02 18:29:27 +0000215
Lang Hameseb6c8f52010-09-18 09:07:10 +0000216 BitVector reservedRegs = tri->getReservedRegs(*mf);
Evan Chengb1290a62008-10-02 18:29:27 +0000217
Andrew Trick16f72dd2012-02-10 04:10:26 +0000218 // Iterate over vregs.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000219 for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end();
220 vregItr != vregEnd; ++vregItr) {
221 unsigned vreg = *vregItr;
222 const TargetRegisterClass *trc = mri->getRegClass(vreg);
223 const LiveInterval *vregLI = &lis->getInterval(vreg);
Evan Chengb1290a62008-10-02 18:29:27 +0000224
Lang Hameseb6c8f52010-09-18 09:07:10 +0000225 // Compute an initial allowed set for the current vreg.
226 typedef std::vector<unsigned> VRAllowed;
227 VRAllowed vrAllowed;
Craig Topperb6632ba2012-03-04 10:16:38 +0000228 ArrayRef<uint16_t> rawOrder = trc->getRawAllocationOrder(*mf);
Jakob Stoklund Olesen714c0eb2011-06-16 20:37:45 +0000229 for (unsigned i = 0; i != rawOrder.size(); ++i) {
230 unsigned preg = rawOrder[i];
Lang Hameseb6c8f52010-09-18 09:07:10 +0000231 if (!reservedRegs.test(preg)) {
232 vrAllowed.push_back(preg);
Lang Hamesd0f6f012010-07-17 06:31:41 +0000233 }
Lang Hameseb6c8f52010-09-18 09:07:10 +0000234 }
Lang Hamesd0f6f012010-07-17 06:31:41 +0000235
Lang Hamesf1113ef2012-03-23 17:33:42 +0000236 RegSet overlappingPRegs;
237
238 // Record physical registers whose ranges overlap.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000239 for (RegSet::const_iterator pregItr = pregs.begin(),
240 pregEnd = pregs.end();
241 pregItr != pregEnd; ++pregItr) {
242 unsigned preg = *pregItr;
243 const LiveInterval *pregLI = &lis->getInterval(preg);
Lang Hames27601ef2008-11-16 12:12:54 +0000244
Lang Hames5e77f4b2010-11-12 05:47:21 +0000245 if (pregLI->empty()) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000246 continue;
Lang Hames5e77f4b2010-11-12 05:47:21 +0000247 }
Evan Chengb1290a62008-10-02 18:29:27 +0000248
Lang Hamesf1113ef2012-03-23 17:33:42 +0000249 if (vregLI->overlaps(*pregLI))
250 overlappingPRegs.insert(preg);
251 }
252
253 // Record any overlaps with regmask operands.
254 BitVector regMaskOverlaps(tri->getNumRegs());
255 for (ArrayRef<SlotIndex>::iterator rmItr = regMaskSlots.begin(),
256 rmEnd = regMaskSlots.end();
257 rmItr != rmEnd; ++rmItr) {
258 SlotIndex rmIdx = *rmItr;
259 if (vregLI->liveAt(rmIdx)) {
260 MachineInstr *rmMI = lis->getInstructionFromIndex(rmIdx);
261 const uint32_t* regMask = 0;
262 for (MachineInstr::mop_iterator mopItr = rmMI->operands_begin(),
263 mopEnd = rmMI->operands_end();
264 mopItr != mopEnd; ++mopItr) {
265 if (mopItr->isRegMask()) {
266 regMask = mopItr->getRegMask();
267 break;
268 }
269 }
270 assert(regMask != 0 && "Couldn't find register mask.");
271 regMaskOverlaps.setBitsNotInMask(regMask);
Lang Hames5e77f4b2010-11-12 05:47:21 +0000272 }
Lang Hamesf1113ef2012-03-23 17:33:42 +0000273 }
274
275 for (unsigned preg = 0; preg < tri->getNumRegs(); ++preg) {
276 if (regMaskOverlaps.test(preg))
277 overlappingPRegs.insert(preg);
278 }
279
280 for (RegSet::const_iterator pregItr = overlappingPRegs.begin(),
281 pregEnd = overlappingPRegs.end();
282 pregItr != pregEnd; ++pregItr) {
283 unsigned preg = *pregItr;
Lang Hames030c4bf2010-01-26 04:49:58 +0000284
Lang Hameseb6c8f52010-09-18 09:07:10 +0000285 // Remove the register from the allowed set.
286 VRAllowed::iterator eraseItr =
287 std::find(vrAllowed.begin(), vrAllowed.end(), preg);
Evan Chengb1290a62008-10-02 18:29:27 +0000288
Lang Hameseb6c8f52010-09-18 09:07:10 +0000289 if (eraseItr != vrAllowed.end()) {
290 vrAllowed.erase(eraseItr);
291 }
Evan Chengb1290a62008-10-02 18:29:27 +0000292
Lang Hameseb6c8f52010-09-18 09:07:10 +0000293 // Also remove any aliases.
Craig Toppere4fd9072012-03-04 10:43:23 +0000294 const uint16_t *aliasItr = tri->getAliasSet(preg);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000295 if (aliasItr != 0) {
296 for (; *aliasItr != 0; ++aliasItr) {
297 VRAllowed::iterator eraseItr =
298 std::find(vrAllowed.begin(), vrAllowed.end(), *aliasItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000299
Lang Hameseb6c8f52010-09-18 09:07:10 +0000300 if (eraseItr != vrAllowed.end()) {
301 vrAllowed.erase(eraseItr);
302 }
303 }
304 }
305 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000306
Lang Hameseb6c8f52010-09-18 09:07:10 +0000307 // Construct the node.
Andrew Trick16f72dd2012-02-10 04:10:26 +0000308 PBQP::Graph::NodeItr node =
Lang Hameseb6c8f52010-09-18 09:07:10 +0000309 g.addNode(PBQP::Vector(vrAllowed.size() + 1, 0));
Evan Chengb1290a62008-10-02 18:29:27 +0000310
Lang Hameseb6c8f52010-09-18 09:07:10 +0000311 // Record the mapping and allowed set in the problem.
312 p->recordVReg(vreg, node, vrAllowed.begin(), vrAllowed.end());
Evan Chengb1290a62008-10-02 18:29:27 +0000313
Lang Hameseb6c8f52010-09-18 09:07:10 +0000314 PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ?
315 vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000316
Lang Hameseb6c8f52010-09-18 09:07:10 +0000317 addSpillCosts(g.getNodeCosts(node), spillCost);
318 }
Evan Chengb1290a62008-10-02 18:29:27 +0000319
Lang Hames481630d2010-09-18 09:49:08 +0000320 for (RegSet::const_iterator vr1Itr = vregs.begin(), vrEnd = vregs.end();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000321 vr1Itr != vrEnd; ++vr1Itr) {
322 unsigned vr1 = *vr1Itr;
323 const LiveInterval &l1 = lis->getInterval(vr1);
324 const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1);
Evan Chengb1290a62008-10-02 18:29:27 +0000325
Benjamin Kramer9e8d1f92010-09-18 14:41:26 +0000326 for (RegSet::const_iterator vr2Itr = llvm::next(vr1Itr);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000327 vr2Itr != vrEnd; ++vr2Itr) {
328 unsigned vr2 = *vr2Itr;
329 const LiveInterval &l2 = lis->getInterval(vr2);
330 const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2);
Evan Chengb1290a62008-10-02 18:29:27 +0000331
Lang Hameseb6c8f52010-09-18 09:07:10 +0000332 assert(!l2.empty() && "Empty interval in vreg set?");
333 if (l1.overlaps(l2)) {
334 PBQP::Graph::EdgeItr edge =
335 g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2),
336 PBQP::Matrix(vr1Allowed.size()+1, vr2Allowed.size()+1, 0));
Lang Hames27601ef2008-11-16 12:12:54 +0000337
Lang Hameseb6c8f52010-09-18 09:07:10 +0000338 addInterferenceCosts(g.getEdgeCosts(edge), vr1Allowed, vr2Allowed, tri);
339 }
340 }
341 }
Evan Chengb1290a62008-10-02 18:29:27 +0000342
Lang Hameseb6c8f52010-09-18 09:07:10 +0000343 return p;
344}
Lang Hames27601ef2008-11-16 12:12:54 +0000345
Lang Hameseb6c8f52010-09-18 09:07:10 +0000346void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec,
347 PBQP::PBQPNum spillCost) {
348 costVec[0] = spillCost;
349}
Evan Chengb1290a62008-10-02 18:29:27 +0000350
Lang Hamese9c93562010-09-21 13:19:36 +0000351void PBQPBuilder::addInterferenceCosts(
352 PBQP::Matrix &costMat,
353 const PBQPRAProblem::AllowedSet &vr1Allowed,
354 const PBQPRAProblem::AllowedSet &vr2Allowed,
355 const TargetRegisterInfo *tri) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000356 assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch.");
357 assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch.");
358
Lang Hames5e77f4b2010-11-12 05:47:21 +0000359 for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000360 unsigned preg1 = vr1Allowed[i];
361
Lang Hames5e77f4b2010-11-12 05:47:21 +0000362 for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000363 unsigned preg2 = vr2Allowed[j];
364
365 if (tri->regsOverlap(preg1, preg2)) {
366 costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
367 }
368 }
369 }
Evan Chengb1290a62008-10-02 18:29:27 +0000370}
371
Lang Hamese9c93562010-09-21 13:19:36 +0000372std::auto_ptr<PBQPRAProblem> PBQPBuilderWithCoalescing::build(
373 MachineFunction *mf,
374 const LiveIntervals *lis,
375 const MachineLoopInfo *loopInfo,
376 const RegSet &vregs) {
377
378 std::auto_ptr<PBQPRAProblem> p = PBQPBuilder::build(mf, lis, loopInfo, vregs);
379 PBQP::Graph &g = p->getGraph();
380
381 const TargetMachine &tm = mf->getTarget();
382 CoalescerPair cp(*tm.getInstrInfo(), *tm.getRegisterInfo());
383
384 // Scan the machine function and add a coalescing cost whenever CoalescerPair
385 // gives the Ok.
386 for (MachineFunction::const_iterator mbbItr = mf->begin(),
387 mbbEnd = mf->end();
388 mbbItr != mbbEnd; ++mbbItr) {
389 const MachineBasicBlock *mbb = &*mbbItr;
390
391 for (MachineBasicBlock::const_iterator miItr = mbb->begin(),
392 miEnd = mbb->end();
393 miItr != miEnd; ++miItr) {
394 const MachineInstr *mi = &*miItr;
395
Lang Hames5e77f4b2010-11-12 05:47:21 +0000396 if (!cp.setRegisters(mi)) {
Lang Hamese9c93562010-09-21 13:19:36 +0000397 continue; // Not coalescable.
Lang Hames5e77f4b2010-11-12 05:47:21 +0000398 }
Lang Hamese9c93562010-09-21 13:19:36 +0000399
Lang Hames5e77f4b2010-11-12 05:47:21 +0000400 if (cp.getSrcReg() == cp.getDstReg()) {
Lang Hamese9c93562010-09-21 13:19:36 +0000401 continue; // Already coalesced.
Lang Hames5e77f4b2010-11-12 05:47:21 +0000402 }
Lang Hamese9c93562010-09-21 13:19:36 +0000403
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000404 unsigned dst = cp.getDstReg(),
405 src = cp.getSrcReg();
Lang Hamese9c93562010-09-21 13:19:36 +0000406
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000407 const float copyFactor = 0.5; // Cost of copy relative to load. Current
408 // value plucked randomly out of the air.
Andrew Trick16f72dd2012-02-10 04:10:26 +0000409
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000410 PBQP::PBQPNum cBenefit =
411 copyFactor * LiveIntervals::getSpillWeight(false, true,
412 loopInfo->getLoopDepth(mbb));
Lang Hamese9c93562010-09-21 13:19:36 +0000413
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000414 if (cp.isPhys()) {
Lang Hames5e77f4b2010-11-12 05:47:21 +0000415 if (!lis->isAllocatable(dst)) {
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000416 continue;
Lang Hames5e77f4b2010-11-12 05:47:21 +0000417 }
Lang Hamese9c93562010-09-21 13:19:36 +0000418
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000419 const PBQPRAProblem::AllowedSet &allowed = p->getAllowedSet(src);
Andrew Trick16f72dd2012-02-10 04:10:26 +0000420 unsigned pregOpt = 0;
Lang Hames5e77f4b2010-11-12 05:47:21 +0000421 while (pregOpt < allowed.size() && allowed[pregOpt] != dst) {
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000422 ++pregOpt;
Lang Hames5e77f4b2010-11-12 05:47:21 +0000423 }
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000424 if (pregOpt < allowed.size()) {
425 ++pregOpt; // +1 to account for spill option.
426 PBQP::Graph::NodeItr node = p->getNodeForVReg(src);
427 addPhysRegCoalesce(g.getNodeCosts(node), pregOpt, cBenefit);
Lang Hamese9c93562010-09-21 13:19:36 +0000428 }
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000429 } else {
430 const PBQPRAProblem::AllowedSet *allowed1 = &p->getAllowedSet(dst);
431 const PBQPRAProblem::AllowedSet *allowed2 = &p->getAllowedSet(src);
432 PBQP::Graph::NodeItr node1 = p->getNodeForVReg(dst);
433 PBQP::Graph::NodeItr node2 = p->getNodeForVReg(src);
434 PBQP::Graph::EdgeItr edge = g.findEdge(node1, node2);
435 if (edge == g.edgesEnd()) {
436 edge = g.addEdge(node1, node2, PBQP::Matrix(allowed1->size() + 1,
437 allowed2->size() + 1,
438 0));
439 } else {
440 if (g.getEdgeNode1(edge) == node2) {
441 std::swap(node1, node2);
442 std::swap(allowed1, allowed2);
443 }
444 }
Andrew Trick16f72dd2012-02-10 04:10:26 +0000445
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000446 addVirtRegCoalesce(g.getEdgeCosts(edge), *allowed1, *allowed2,
447 cBenefit);
Lang Hamese9c93562010-09-21 13:19:36 +0000448 }
449 }
450 }
451
452 return p;
453}
454
Lang Hamese9c93562010-09-21 13:19:36 +0000455void PBQPBuilderWithCoalescing::addPhysRegCoalesce(PBQP::Vector &costVec,
456 unsigned pregOption,
457 PBQP::PBQPNum benefit) {
458 costVec[pregOption] += -benefit;
459}
460
461void PBQPBuilderWithCoalescing::addVirtRegCoalesce(
462 PBQP::Matrix &costMat,
463 const PBQPRAProblem::AllowedSet &vr1Allowed,
464 const PBQPRAProblem::AllowedSet &vr2Allowed,
465 PBQP::PBQPNum benefit) {
466
467 assert(costMat.getRows() == vr1Allowed.size() + 1 && "Size mismatch.");
468 assert(costMat.getCols() == vr2Allowed.size() + 1 && "Size mismatch.");
469
Lang Hames5e77f4b2010-11-12 05:47:21 +0000470 for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
Lang Hamese9c93562010-09-21 13:19:36 +0000471 unsigned preg1 = vr1Allowed[i];
Lang Hames5e77f4b2010-11-12 05:47:21 +0000472 for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
Lang Hamese9c93562010-09-21 13:19:36 +0000473 unsigned preg2 = vr2Allowed[j];
474
475 if (preg1 == preg2) {
476 costMat[i + 1][j + 1] += -benefit;
Andrew Trick16f72dd2012-02-10 04:10:26 +0000477 }
Lang Hamese9c93562010-09-21 13:19:36 +0000478 }
479 }
480}
Evan Chengb1290a62008-10-02 18:29:27 +0000481
Lang Hameseb6c8f52010-09-18 09:07:10 +0000482
483void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
Lang Hames9ad7e072011-12-06 01:45:57 +0000484 au.setPreservesCFG();
485 au.addRequired<AliasAnalysis>();
486 au.addPreserved<AliasAnalysis>();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000487 au.addRequired<SlotIndexes>();
488 au.addPreserved<SlotIndexes>();
489 au.addRequired<LiveIntervals>();
490 //au.addRequiredID(SplitCriticalEdgesID);
Lang Hames8d857662011-06-17 07:09:01 +0000491 if (customPassID)
492 au.addRequiredID(*customPassID);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000493 au.addRequired<CalculateSpillWeights>();
494 au.addRequired<LiveStacks>();
495 au.addPreserved<LiveStacks>();
Lang Hames9ad7e072011-12-06 01:45:57 +0000496 au.addRequired<MachineDominatorTree>();
497 au.addPreserved<MachineDominatorTree>();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000498 au.addRequired<MachineLoopInfo>();
499 au.addPreserved<MachineLoopInfo>();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000500 au.addRequired<VirtRegMap>();
501 au.addRequired<RenderMachineFunction>();
502 MachineFunctionPass::getAnalysisUsage(au);
503}
504
Lang Hameseb6c8f52010-09-18 09:07:10 +0000505void RegAllocPBQP::findVRegIntervalsToAlloc() {
Lang Hames27601ef2008-11-16 12:12:54 +0000506
507 // Iterate over all live ranges.
508 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
509 itr != end; ++itr) {
510
511 // Ignore physical ones.
512 if (TargetRegisterInfo::isPhysicalRegister(itr->first))
513 continue;
514
515 LiveInterval *li = itr->second;
516
517 // If this live interval is non-empty we will use pbqp to allocate it.
518 // Empty intervals we allocate in a simple post-processing stage in
519 // finalizeAlloc.
520 if (!li->empty()) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000521 vregsToAlloc.insert(li->reg);
Lang Hames5e77f4b2010-11-12 05:47:21 +0000522 } else {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000523 emptyIntervalVRegs.insert(li->reg);
Lang Hames27601ef2008-11-16 12:12:54 +0000524 }
525 }
Evan Chengb1290a62008-10-02 18:29:27 +0000526}
527
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000528bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAProblem &problem,
529 const PBQP::Solution &solution) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000530 // Set to true if we have any spills
531 bool anotherRoundNeeded = false;
532
533 // Clear the existing allocation.
534 vrm->clearAllVirt();
535
536 const PBQP::Graph &g = problem.getGraph();
537 // Iterate over the nodes mapping the PBQP solution to a register
538 // assignment.
539 for (PBQP::Graph::ConstNodeItr node = g.nodesBegin(),
540 nodeEnd = g.nodesEnd();
541 node != nodeEnd; ++node) {
542 unsigned vreg = problem.getVRegForNode(node);
543 unsigned alloc = solution.getSelection(node);
544
545 if (problem.isPRegOption(vreg, alloc)) {
Andrew Trick16f72dd2012-02-10 04:10:26 +0000546 unsigned preg = problem.getPRegForOption(vreg, alloc);
Patrik Hägglundd7693872012-05-23 12:12:58 +0000547 DEBUG(dbgs() << "VREG " << PrintReg(vreg, tri) << " -> "
548 << tri->getName(preg) << "\n");
Lang Hameseb6c8f52010-09-18 09:07:10 +0000549 assert(preg != 0 && "Invalid preg selected.");
Andrew Trick16f72dd2012-02-10 04:10:26 +0000550 vrm->assignVirt2Phys(vreg, preg);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000551 } else if (problem.isSpillOption(vreg, alloc)) {
552 vregsToAlloc.erase(vreg);
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000553 SmallVector<LiveInterval*, 8> newSpills;
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +0000554 LiveRangeEdit LRE(&lis->getInterval(vreg), newSpills, *mf, *lis, vrm);
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000555 spiller->spill(LRE);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000556
Patrik Hägglundd7693872012-05-23 12:12:58 +0000557 DEBUG(dbgs() << "VREG " << PrintReg(vreg, tri) << " -> SPILLED (Cost: "
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000558 << LRE.getParent().weight << ", New vregs: ");
Lang Hameseb6c8f52010-09-18 09:07:10 +0000559
560 // Copy any newly inserted live intervals into the list of regs to
561 // allocate.
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000562 for (LiveRangeEdit::iterator itr = LRE.begin(), end = LRE.end();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000563 itr != end; ++itr) {
564 assert(!(*itr)->empty() && "Empty spill range.");
Patrik Hägglundd7693872012-05-23 12:12:58 +0000565 DEBUG(dbgs() << PrintReg((*itr)->reg, tri) << " ");
Lang Hameseb6c8f52010-09-18 09:07:10 +0000566 vregsToAlloc.insert((*itr)->reg);
567 }
568
569 DEBUG(dbgs() << ")\n");
570
571 // We need another round if spill intervals were added.
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000572 anotherRoundNeeded |= !LRE.empty();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000573 } else {
Craig Topper5e25ee82012-02-05 08:31:47 +0000574 llvm_unreachable("Unknown allocation option.");
Lang Hameseb6c8f52010-09-18 09:07:10 +0000575 }
576 }
577
578 return !anotherRoundNeeded;
579}
580
581
582void RegAllocPBQP::finalizeAlloc() const {
Lang Hames27601ef2008-11-16 12:12:54 +0000583 typedef LiveIntervals::iterator LIIterator;
584 typedef LiveInterval::Ranges::const_iterator LRIterator;
585
586 // First allocate registers for the empty intervals.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000587 for (RegSet::const_iterator
588 itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000589 itr != end; ++itr) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000590 LiveInterval *li = &lis->getInterval(*itr);
Lang Hames27601ef2008-11-16 12:12:54 +0000591
Evan Cheng90f95f82009-06-14 20:22:55 +0000592 unsigned physReg = vrm->getRegAllocPref(li->reg);
Lang Hames6699fb22009-08-06 23:32:48 +0000593
Lang Hames27601ef2008-11-16 12:12:54 +0000594 if (physReg == 0) {
595 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Jakob Stoklund Olesen714c0eb2011-06-16 20:37:45 +0000596 physReg = liRC->getRawAllocationOrder(*mf).front();
Lang Hames27601ef2008-11-16 12:12:54 +0000597 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000598
599 vrm->assignVirt2Phys(li->reg, physReg);
Lang Hames27601ef2008-11-16 12:12:54 +0000600 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000601
Lang Hames27601ef2008-11-16 12:12:54 +0000602 // Finally iterate over the basic blocks to compute and set the live-in sets.
603 SmallVector<MachineBasicBlock*, 8> liveInMBBs;
604 MachineBasicBlock *entryMBB = &*mf->begin();
605
606 for (LIIterator liItr = lis->begin(), liEnd = lis->end();
607 liItr != liEnd; ++liItr) {
608
609 const LiveInterval *li = liItr->second;
610 unsigned reg = 0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000611
Lang Hames27601ef2008-11-16 12:12:54 +0000612 // Get the physical register for this interval
613 if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
614 reg = li->reg;
Lang Hames5e77f4b2010-11-12 05:47:21 +0000615 } else if (vrm->isAssignedReg(li->reg)) {
Lang Hames27601ef2008-11-16 12:12:54 +0000616 reg = vrm->getPhys(li->reg);
Lang Hames5e77f4b2010-11-12 05:47:21 +0000617 } else {
Lang Hames27601ef2008-11-16 12:12:54 +0000618 // Ranges which are assigned a stack slot only are ignored.
619 continue;
620 }
621
Lang Hamesb0e519f2009-05-17 23:50:36 +0000622 if (reg == 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000623 // Filter out zero regs - they're for intervals that were spilled.
Lang Hamesb0e519f2009-05-17 23:50:36 +0000624 continue;
625 }
626
Lang Hames27601ef2008-11-16 12:12:54 +0000627 // Iterate over the ranges of the current interval...
628 for (LRIterator lrItr = li->begin(), lrEnd = li->end();
629 lrItr != lrEnd; ++lrItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000630
Lang Hames27601ef2008-11-16 12:12:54 +0000631 // Find the set of basic blocks which this range is live into...
632 if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
633 // And add the physreg for this interval to their live-in sets.
Lang Hames5e77f4b2010-11-12 05:47:21 +0000634 for (unsigned i = 0; i != liveInMBBs.size(); ++i) {
Lang Hames27601ef2008-11-16 12:12:54 +0000635 if (liveInMBBs[i] != entryMBB) {
636 if (!liveInMBBs[i]->isLiveIn(reg)) {
637 liveInMBBs[i]->addLiveIn(reg);
638 }
639 }
640 }
641 liveInMBBs.clear();
642 }
643 }
644 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000645
Lang Hames27601ef2008-11-16 12:12:54 +0000646}
647
Lang Hameseb6c8f52010-09-18 09:07:10 +0000648bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
Lang Hames27601ef2008-11-16 12:12:54 +0000649
Evan Chengb1290a62008-10-02 18:29:27 +0000650 mf = &MF;
651 tm = &mf->getTarget();
652 tri = tm->getRegisterInfo();
Lang Hames27601ef2008-11-16 12:12:54 +0000653 tii = tm->getInstrInfo();
Andrew Trick16f72dd2012-02-10 04:10:26 +0000654 mri = &mf->getRegInfo();
Evan Chengb1290a62008-10-02 18:29:27 +0000655
Lang Hames27601ef2008-11-16 12:12:54 +0000656 lis = &getAnalysis<LiveIntervals>();
657 lss = &getAnalysis<LiveStacks>();
Evan Chengb1290a62008-10-02 18:29:27 +0000658 loopInfo = &getAnalysis<MachineLoopInfo>();
Lang Hames33198392010-09-02 08:27:00 +0000659 rmf = &getAnalysis<RenderMachineFunction>();
Evan Chengb1290a62008-10-02 18:29:27 +0000660
Owen Anderson49c8aa02009-03-13 05:55:11 +0000661 vrm = &getAnalysis<VirtRegMap>();
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000662 spiller.reset(createInlineSpiller(*this, MF, *vrm));
Evan Chengb1290a62008-10-02 18:29:27 +0000663
Jakob Stoklund Olesend9e5c762012-01-05 00:26:49 +0000664 mri->freezeReservedRegs(MF);
Lang Hames54cc2ef2010-07-19 15:22:28 +0000665
Lang Hames030c4bf2010-01-26 04:49:58 +0000666 DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000667
Evan Chengb1290a62008-10-02 18:29:27 +0000668 // Allocator main loop:
Misha Brukman2a835f92009-01-08 15:50:22 +0000669 //
Evan Chengb1290a62008-10-02 18:29:27 +0000670 // * Map current regalloc problem to a PBQP problem
671 // * Solve the PBQP problem
672 // * Map the solution back to a register allocation
673 // * Spill if necessary
Misha Brukman2a835f92009-01-08 15:50:22 +0000674 //
Evan Chengb1290a62008-10-02 18:29:27 +0000675 // This process is continued till no more spills are generated.
676
Lang Hames27601ef2008-11-16 12:12:54 +0000677 // Find the vreg intervals in need of allocation.
678 findVRegIntervalsToAlloc();
Misha Brukman2a835f92009-01-08 15:50:22 +0000679
Lang Hames20df03c2012-03-26 23:07:23 +0000680 const Function* func = mf->getFunction();
681 std::string fqn =
682 func->getParent()->getModuleIdentifier() + "." +
683 func->getName().str();
684 (void)fqn;
685
Lang Hames27601ef2008-11-16 12:12:54 +0000686 // If there are non-empty intervals allocate them using pbqp.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000687 if (!vregsToAlloc.empty()) {
Evan Chengb1290a62008-10-02 18:29:27 +0000688
Lang Hames27601ef2008-11-16 12:12:54 +0000689 bool pbqpAllocComplete = false;
690 unsigned round = 0;
691
Lang Hamesab62b7e2010-10-04 12:13:07 +0000692 while (!pbqpAllocComplete) {
693 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000694
Lang Hamesab62b7e2010-10-04 12:13:07 +0000695 std::auto_ptr<PBQPRAProblem> problem =
696 builder->build(mf, lis, loopInfo, vregsToAlloc);
Lang Hames20df03c2012-03-26 23:07:23 +0000697
698#ifndef NDEBUG
699 if (pbqpDumpGraphs) {
700 std::ostringstream rs;
701 rs << round;
702 std::string graphFileName(fqn + "." + rs.str() + ".pbqpgraph");
703 std::string tmp;
704 raw_fd_ostream os(graphFileName.c_str(), tmp);
705 DEBUG(dbgs() << "Dumping graph for round " << round << " to \""
706 << graphFileName << "\"\n");
707 problem->getGraph().dump(os);
708 }
709#endif
710
Lang Hamesab62b7e2010-10-04 12:13:07 +0000711 PBQP::Solution solution =
712 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(
713 problem->getGraph());
Lang Hames233fd9c2009-08-18 23:34:50 +0000714
Lang Hamesab62b7e2010-10-04 12:13:07 +0000715 pbqpAllocComplete = mapPBQPToRegAlloc(*problem, solution);
Lang Hames27601ef2008-11-16 12:12:54 +0000716
Lang Hamesab62b7e2010-10-04 12:13:07 +0000717 ++round;
Lang Hames27601ef2008-11-16 12:12:54 +0000718 }
Evan Chengb1290a62008-10-02 18:29:27 +0000719 }
720
Lang Hames27601ef2008-11-16 12:12:54 +0000721 // Finalise allocation, allocate empty ranges.
722 finalizeAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000723
Lang Hamesc4bcc772010-07-20 07:41:44 +0000724 rmf->renderMachineFunction("After PBQP register allocation.", vrm);
725
Lang Hameseb6c8f52010-09-18 09:07:10 +0000726 vregsToAlloc.clear();
727 emptyIntervalVRegs.clear();
Lang Hames27601ef2008-11-16 12:12:54 +0000728
David Greene30931542010-01-05 01:25:43 +0000729 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000730
Lang Hames87e3bca2009-05-06 02:36:21 +0000731 // Run rewriter
Jakob Stoklund Olesenc3f27222011-11-13 00:02:24 +0000732 vrm->rewrite(lis->getSlotIndexes());
Lang Hames27601ef2008-11-16 12:12:54 +0000733
Andrew Trick19273ae2012-02-21 04:51:23 +0000734 // All machine operands and other references to virtual registers have been
735 // replaced. Remove the virtual registers.
736 vrm->clearAllVirt();
737 mri->clearVirtRegs();
738
Misha Brukman2a835f92009-01-08 15:50:22 +0000739 return true;
Evan Chengb1290a62008-10-02 18:29:27 +0000740}
741
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000742FunctionPass* llvm::createPBQPRegisterAllocator(
Lang Hames8d857662011-06-17 07:09:01 +0000743 std::auto_ptr<PBQPBuilder> builder,
744 char *customPassID) {
745 return new RegAllocPBQP(builder, customPassID);
Evan Chengb1290a62008-10-02 18:29:27 +0000746}
747
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000748FunctionPass* llvm::createDefaultPBQPRegisterAllocator() {
749 if (pbqpCoalescing) {
750 return createPBQPRegisterAllocator(
751 std::auto_ptr<PBQPBuilder>(new PBQPBuilderWithCoalescing()));
752 } // else
753 return createPBQPRegisterAllocator(
754 std::auto_ptr<PBQPBuilder>(new PBQPBuilder()));
Lang Hameseb6c8f52010-09-18 09:07:10 +0000755}
Evan Chengb1290a62008-10-02 18:29:27 +0000756
757#undef DEBUG_TYPE