1 //===------ RegAllocPBQP.cpp ---- PBQP Register Allocator -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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:
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.
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,
31 // Email: lhames@gmail.com
33 //===----------------------------------------------------------------------===//
37 // * Use of std::set in constructPBQPProblem destroys allocation order preference.
38 // Switch to an order preserving container.
40 // * Coalescing support.
42 #define DEBUG_TYPE "regalloc"
45 #include "VirtRegMap.h"
46 #include "llvm/CodeGen/MachineFunctionPass.h"
47 #include "llvm/CodeGen/RegAllocRegistry.h"
48 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
49 #include "llvm/CodeGen/MachineRegisterInfo.h"
50 #include "llvm/CodeGen/MachineLoopInfo.h"
51 #include "llvm/Target/TargetMachine.h"
52 #include "llvm/Target/TargetInstrInfo.h"
53 #include "llvm/Support/Debug.h"
62 static RegisterRegAlloc
63 registerPBQPRepAlloc("pbqp", " PBQP register allocator",
64 createPBQPRegisterAllocator);
70 //! PBQP based allocators solve the register allocation problem by mapping
71 //! register allocation problems to Partitioned Boolean Quadratic
72 //! Programming problems.
73 class VISIBILITY_HIDDEN PBQPRegAlloc : public MachineFunctionPass {
78 //! Construct a PBQP register allocator.
79 PBQPRegAlloc() : MachineFunctionPass((intptr_t)&ID) {}
81 //! Return the pass name.
82 virtual const char* getPassName() const throw() {
83 return "PBQP Register Allocator";
86 //! PBQP analysis usage.
87 virtual void getAnalysisUsage(AnalysisUsage &au) const {
88 au.addRequired<LiveIntervals>();
89 au.addRequired<MachineLoopInfo>();
90 MachineFunctionPass::getAnalysisUsage(au);
93 //! Perform register allocation
94 virtual bool runOnMachineFunction(MachineFunction &MF);
97 typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
98 typedef std::vector<const LiveInterval*> Node2LIMap;
99 typedef std::vector<unsigned> AllowedSet;
100 typedef std::vector<AllowedSet> AllowedSetMap;
101 typedef std::set<unsigned> IgnoreSet;
104 const TargetMachine *tm;
105 const TargetRegisterInfo *tri;
106 const TargetInstrInfo *tii;
107 const MachineLoopInfo *loopInfo;
108 MachineRegisterInfo *mri;
115 AllowedSetMap allowedSets;
118 //! Builds a PBQP cost vector.
119 template <typename Container>
120 PBQPVector* buildCostVector(const Container &allowed,
121 PBQPNum spillCost) const;
123 //! \brief Builds a PBQP interfernce matrix.
125 //! @return Either a pointer to a non-zero PBQP matrix representing the
126 //! allocation option costs, or a null pointer for a zero matrix.
128 //! Expects allowed sets for two interfering LiveIntervals. These allowed
129 //! sets should contain only allocable registers from the LiveInterval's
130 //! register class, with any interfering pre-colored registers removed.
131 template <typename Container>
132 PBQPMatrix* buildInterferenceMatrix(const Container &allowed1,
133 const Container &allowed2) const;
136 //! Expects allowed sets for two potentially coalescable LiveIntervals,
137 //! and an estimated benefit due to coalescing. The allowed sets should
138 //! contain only allocable registers from the LiveInterval's register
139 //! classes, with any interfering pre-colored registers removed.
140 template <typename Container>
141 PBQPMatrix* buildCoalescingMatrix(const Container &allowed1,
142 const Container &allowed2,
143 PBQPNum cBenefit) const;
145 //! \brief Helper functior for constructInitialPBQPProblem().
147 //! This function iterates over the Function we are about to allocate for
148 //! and computes spill costs.
149 void calcSpillCosts();
151 //! \brief Scans the MachineFunction being allocated to find coalescing
153 void findCoalescingOpportunities();
155 //! \brief Constructs a PBQP problem representation of the register
156 //! allocation problem for this function.
158 //! @return a PBQP solver object for the register allocation problem.
159 pbqp* constructPBQPProblem();
161 //! \brief Given a solved PBQP problem maps this solution back to a register
163 bool mapPBQPToRegAlloc(pbqp *problem);
167 char PBQPRegAlloc::ID = 0;
171 template <typename Container>
172 PBQPVector* PBQPRegAlloc::buildCostVector(const Container &allowed,
173 PBQPNum spillCost) const {
175 // Allocate vector. Additional element (0th) used for spill option
176 PBQPVector *v = new PBQPVector(allowed.size() + 1);
183 template <typename Container>
184 PBQPMatrix* PBQPRegAlloc::buildInterferenceMatrix(
185 const Container &allowed1, const Container &allowed2) const {
187 typedef typename Container::const_iterator ContainerIterator;
189 // Construct a PBQP matrix representing the cost of allocation options. The
190 // rows and columns correspond to the allocation options for the two live
191 // intervals. Elements will be infinite where corresponding registers alias,
192 // since we cannot allocate aliasing registers to interfering live intervals.
193 // All other elements (non-aliasing combinations) will have zero cost. Note
194 // that the spill option (element 0,0) has zero cost, since we can allocate
195 // both intervals to memory safely (the cost for each individual allocation
196 // to memory is accounted for by the cost vectors for each live interval).
197 PBQPMatrix *m = new PBQPMatrix(allowed1.size() + 1, allowed2.size() + 1);
199 // Assume this is a zero matrix until proven otherwise. Zero matrices occur
200 // between interfering live ranges with non-overlapping register sets (e.g.
201 // non-overlapping reg classes, or disjoint sets of allowed regs within the
202 // same class). The term "overlapping" is used advisedly: sets which do not
203 // intersect, but contain registers which alias, will have non-zero matrices.
204 // We optimize zero matrices away to improve solver speed.
205 bool isZeroMatrix = true;
208 // Row index. Starts at 1, since the 0th row is for the spill option, which
212 // Iterate over allowed sets, insert infinities where required.
213 for (ContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
214 a1Itr != a1End; ++a1Itr) {
216 // Column index, starts at 1 as for row index.
218 unsigned reg1 = *a1Itr;
220 for (ContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
221 a2Itr != a2End; ++a2Itr) {
223 unsigned reg2 = *a2Itr;
225 // If the row/column regs are identical or alias insert an infinity.
226 if ((reg1 == reg2) || tri->areAliases(reg1, reg2)) {
227 (*m)[ri][ci] = std::numeric_limits<PBQPNum>::infinity();
228 isZeroMatrix = false;
237 // If this turns out to be a zero matrix...
239 // free it and return null.
244 // ...otherwise return the cost matrix.
248 void PBQPRegAlloc::calcSpillCosts() {
250 // Calculate the spill cost for each live interval by iterating over the
251 // function counting loads and stores, with loop depth taken into account.
252 for (MachineFunction::const_iterator bbItr = mf->begin(), bbEnd = mf->end();
253 bbItr != bbEnd; ++bbItr) {
255 const MachineBasicBlock *mbb = &*bbItr;
256 float loopDepth = loopInfo->getLoopDepth(mbb);
258 for (MachineBasicBlock::const_iterator
259 iItr = mbb->begin(), iEnd = mbb->end(); iItr != iEnd; ++iItr) {
261 const MachineInstr *instr = &*iItr;
263 for (unsigned opNo = 0; opNo < instr->getNumOperands(); ++opNo) {
265 const MachineOperand &mo = instr->getOperand(opNo);
267 // We're not interested in non-registers...
268 if (!mo.isRegister())
271 unsigned moReg = mo.getReg();
273 // ...Or invalid registers...
277 // ...Or physical registers...
278 if (TargetRegisterInfo::isPhysicalRegister(moReg))
281 assert ((mo.isUse() || mo.isDef()) &&
282 "Not a use, not a def, what is it?");
284 //... Just the virtual registers. We treat loads and stores as equal.
285 li->getInterval(moReg).weight += powf(10.0f, loopDepth);
294 pbqp* PBQPRegAlloc::constructPBQPProblem() {
296 typedef std::vector<const LiveInterval*> LIVector;
297 typedef std::set<unsigned> RegSet;
299 // These will store the physical & virtual intervals, respectively.
300 LIVector physIntervals, virtIntervals;
302 // Start by clearing the old node <-> live interval mappings & allowed sets
307 // Iterate over intervals classifying them as physical or virtual, and
308 // constructing live interval <-> node number mappings.
309 for (LiveIntervals::iterator itr = li->begin(), end = li->end();
312 if (itr->second->getNumValNums() != 0) {
313 DOUT << "Live range has " << itr->second->getNumValNums() << ": " << itr->second << "\n";
316 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
317 physIntervals.push_back(itr->second);
318 mri->setPhysRegUsed(itr->second->reg);
322 // If we've allocated this virtual register interval a stack slot on a
323 // previous round then it's not an allocation candidate
324 if (ignoreSet.find(itr->first) != ignoreSet.end())
327 li2Node[itr->second] = node2LI.size();
328 node2LI.push_back(itr->second);
329 virtIntervals.push_back(itr->second);
333 // Early out if there's no regs to allocate for.
334 if (virtIntervals.empty())
337 // Construct a PBQP solver for this problem
338 pbqp *solver = alloc_pbqp(virtIntervals.size());
340 // Resize allowedSets container appropriately.
341 allowedSets.resize(virtIntervals.size());
343 // Iterate over virtual register intervals to compute allowed sets...
344 for (unsigned node = 0; node < node2LI.size(); ++node) {
346 // Grab pointers to the interval and its register class.
347 const LiveInterval *li = node2LI[node];
348 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
350 // Start by assuming all allocable registers in the class are allowed...
351 RegSet liAllowed(liRC->allocation_order_begin(*mf),
352 liRC->allocation_order_end(*mf));
354 // If this range is non-empty then eliminate the physical registers which
355 // overlap with this range, along with all their aliases.
357 for (LIVector::iterator pItr = physIntervals.begin(),
358 pEnd = physIntervals.end(); pItr != pEnd; ++pItr) {
360 if (li->overlaps(**pItr)) {
362 unsigned pReg = (*pItr)->reg;
364 // Remove the overlapping reg...
365 liAllowed.erase(pReg);
367 const unsigned *aliasItr = tri->getAliasSet(pReg);
370 // ...and its aliases.
371 for (; *aliasItr != 0; ++aliasItr) {
372 liAllowed.erase(*aliasItr);
383 // Copy the allowed set into a member vector for use when constructing cost
384 // vectors & matrices, and mapping PBQP solutions back to assignments.
385 allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end());
387 // Set the spill cost to the interval weight, or epsilon if the
388 // interval weight is zero
389 PBQPNum spillCost = (li->weight != 0.0) ?
390 li->weight : std::numeric_limits<PBQPNum>::min();
392 // Build a cost vector for this interval.
393 add_pbqp_nodecosts(solver, node,
394 buildCostVector(allowedSets[node], spillCost));
398 // Now add the cost matrices...
399 for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) {
401 const LiveInterval *li = node2LI[node1];
406 // Test for live range overlaps and insert interference matrices.
407 for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) {
408 const LiveInterval *li2 = node2LI[node2];
413 if (li->overlaps(*li2)) {
415 buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]);
418 add_pbqp_edgecosts(solver, node1, node2, m);
425 // We're done, PBQP problem constructed - return it.
429 bool PBQPRegAlloc::mapPBQPToRegAlloc(pbqp *problem) {
431 // Set to true if we have any spills
432 bool anotherRoundNeeded = false;
434 // Clear the existing allocation.
437 // Iterate over the nodes mapping the PBQP solution to a register assignment.
438 for (unsigned node = 0; node < node2LI.size(); ++node) {
439 unsigned symReg = node2LI[node]->reg,
440 allocSelection = get_pbqp_solution(problem, node);
442 // If the PBQP solution is non-zero it's a physical register...
443 if (allocSelection != 0) {
444 // Get the physical reg, subtracting 1 to account for the spill option.
445 unsigned physReg = allowedSets[node][allocSelection - 1];
447 // Add to the virt reg map and update the used phys regs.
448 vrm->assignVirt2Phys(symReg, physReg);
449 mri->setPhysRegUsed(physReg);
451 // ...Otherwise it's a spill.
454 // Make sure we ignore this virtual reg on the next round
456 ignoreSet.insert(node2LI[node]->reg);
460 // Insert spill ranges for this live range
461 SmallVector<LiveInterval*, 8> spillIs;
462 std::vector<LiveInterval*> newSpills =
463 li->addIntervalsForSpills(*node2LI[node], spillIs, loopInfo, *vrm,
466 // We need another round if spill intervals were added.
467 anotherRoundNeeded |= !newSpills.empty();
471 return !anotherRoundNeeded;
474 bool PBQPRegAlloc::runOnMachineFunction(MachineFunction &MF) {
477 tm = &mf->getTarget();
478 tri = tm->getRegisterInfo();
479 mri = &mf->getRegInfo();
481 li = &getAnalysis<LiveIntervals>();
482 loopInfo = &getAnalysis<MachineLoopInfo>();
484 std::auto_ptr<VirtRegMap> vrmAutoPtr(new VirtRegMap(*mf));
485 vrm = vrmAutoPtr.get();
487 // Allocator main loop:
489 // * Map current regalloc problem to a PBQP problem
490 // * Solve the PBQP problem
491 // * Map the solution back to a register allocation
492 // * Spill if necessary
494 // This process is continued till no more spills are generated.
496 bool regallocComplete = false;
498 // Calculate spill costs for intervals
501 while (!regallocComplete) {
502 pbqp *problem = constructPBQPProblem();
504 // Fast out if there's no problem to solve.
510 regallocComplete = mapPBQPToRegAlloc(problem);
517 std::auto_ptr<Spiller> spiller(createSpiller());
519 spiller->runOnMachineFunction(*mf, *vrm);
524 FunctionPass* llvm::createPBQPRegisterAllocator() {
525 return new PBQPRegAlloc();