Fix command-line option printing to print two spaces where needed,
[oota-llvm.git] / lib / CodeGen / RegAllocPBQP.cpp
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 // Author: Lang Hames
31 // Email: lhames@gmail.com
32 //
33 //===----------------------------------------------------------------------===//
34
35 // TODO:
36 // 
37 // * Use of std::set in constructPBQPProblem destroys allocation order preference.
38 // Switch to an order preserving container.
39 // 
40 // * Coalescing support.
41
42 #define DEBUG_TYPE "regalloc"
43
44 #include "PBQP.h"
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"
54 #include <memory>
55 #include <map>
56 #include <set>
57 #include <vector>
58 #include <limits>
59
60 using namespace llvm;
61
62 static RegisterRegAlloc
63 registerPBQPRepAlloc("pbqp", "PBQP register allocator",
64                      createPBQPRegisterAllocator);
65
66
67 namespace {
68
69   //!
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 {
74   public:
75
76     static char ID;
77     
78     //! Construct a PBQP register allocator.
79     PBQPRegAlloc() : MachineFunctionPass((intptr_t)&ID) {}
80
81     //! Return the pass name.
82     virtual const char* getPassName() const throw() {
83       return "PBQP Register Allocator";
84     }
85
86     //! PBQP analysis usage.
87     virtual void getAnalysisUsage(AnalysisUsage &au) const {
88       au.addRequired<LiveIntervals>();
89       au.addRequired<MachineLoopInfo>();
90       MachineFunctionPass::getAnalysisUsage(au);
91     }
92
93     //! Perform register allocation
94     virtual bool runOnMachineFunction(MachineFunction &MF);
95
96   private:
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;
102
103     MachineFunction *mf;
104     const TargetMachine *tm;
105     const TargetRegisterInfo *tri;
106     const TargetInstrInfo *tii;
107     const MachineLoopInfo *loopInfo;
108     MachineRegisterInfo *mri;
109
110     LiveIntervals *li;
111     VirtRegMap *vrm;
112
113     LI2NodeMap li2Node;
114     Node2LIMap node2LI;
115     AllowedSetMap allowedSets;
116     IgnoreSet ignoreSet;
117
118     //! Builds a PBQP cost vector.
119     template <typename Container>
120     PBQPVector* buildCostVector(const Container &allowed,
121                                 PBQPNum spillCost) const;
122
123     //! \brief Builds a PBQP interference matrix.
124     //!
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.
127     //!
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;
134
135     //!
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;
144
145     //! \brief Helper function for constructInitialPBQPProblem().
146     //!
147     //! This function iterates over the Function we are about to allocate for
148     //! and computes spill costs.
149     void calcSpillCosts();
150
151     //! \brief Scans the MachineFunction being allocated to find coalescing
152     //  opportunities.
153     void findCoalescingOpportunities();
154
155     //! \brief Constructs a PBQP problem representation of the register
156     //! allocation problem for this function.
157     //!
158     //! @return a PBQP solver object for the register allocation problem.
159     pbqp* constructPBQPProblem();
160
161     //! \brief Given a solved PBQP problem maps this solution back to a register
162     //! assignment.
163     bool mapPBQPToRegAlloc(pbqp *problem); 
164
165   };
166
167   char PBQPRegAlloc::ID = 0;
168 }
169
170
171 template <typename Container>
172 PBQPVector* PBQPRegAlloc::buildCostVector(const Container &allowed,
173                                           PBQPNum spillCost) const {
174
175   // Allocate vector. Additional element (0th) used for spill option
176   PBQPVector *v = new PBQPVector(allowed.size() + 1);
177
178   (*v)[0] = spillCost;
179
180   return v;
181 }
182
183 template <typename Container>
184 PBQPMatrix* PBQPRegAlloc::buildInterferenceMatrix(
185       const Container &allowed1, const Container &allowed2) const {
186
187   typedef typename Container::const_iterator ContainerIterator;
188
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);
198  
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;
206
207
208   // Row index. Starts at 1, since the 0th row is for the spill option, which
209   // is always zero.
210   unsigned ri = 1; 
211
212   // Iterate over allowed sets, insert infinities where required. 
213   for (ContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
214        a1Itr != a1End; ++a1Itr) {
215
216     // Column index, starts at 1 as for row index.
217     unsigned ci = 1;
218     unsigned reg1 = *a1Itr;
219
220     for (ContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
221          a2Itr != a2End; ++a2Itr) {
222
223       unsigned reg2 = *a2Itr;
224
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;
229       }
230
231       ++ci;
232     }
233
234     ++ri;
235   }
236
237   // If this turns out to be a zero matrix...
238   if (isZeroMatrix) {
239     // free it and return null.
240     delete m;
241     return 0;
242   }
243
244   // ...otherwise return the cost matrix.
245   return m;
246 }
247
248 void PBQPRegAlloc::calcSpillCosts() {
249
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) {
254
255     const MachineBasicBlock *mbb = &*bbItr;
256     float loopDepth = loopInfo->getLoopDepth(mbb);
257
258     for (MachineBasicBlock::const_iterator
259          iItr = mbb->begin(), iEnd = mbb->end(); iItr != iEnd; ++iItr) {
260
261       const MachineInstr *instr = &*iItr;
262
263       for (unsigned opNo = 0; opNo < instr->getNumOperands(); ++opNo) {
264
265         const MachineOperand &mo = instr->getOperand(opNo);
266
267         // We're not interested in non-registers...
268         if (!mo.isReg())
269           continue;
270  
271         unsigned moReg = mo.getReg();
272
273         // ...Or invalid registers...
274         if (moReg == 0)
275           continue;
276
277         // ...Or physical registers...
278         if (TargetRegisterInfo::isPhysicalRegister(moReg)) 
279           continue;
280
281         assert ((mo.isUse() || mo.isDef()) &&
282                 "Not a use, not a def, what is it?");
283
284         //... Just the virtual registers. We treat loads and stores as equal.
285         li->getInterval(moReg).weight += powf(10.0f, loopDepth);
286       }
287
288     }
289
290   }
291
292 }
293
294 pbqp* PBQPRegAlloc::constructPBQPProblem() {
295
296   typedef std::vector<const LiveInterval*> LIVector;
297   typedef std::set<unsigned> RegSet;
298
299   // These will store the physical & virtual intervals, respectively.
300   LIVector physIntervals, virtIntervals;
301
302   // Start by clearing the old node <-> live interval mappings & allowed sets
303   li2Node.clear();
304   node2LI.clear();
305   allowedSets.clear();
306
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();
310        itr != end; ++itr) {
311
312     if (itr->second->getNumValNums() != 0) {
313       DOUT << "Live range has " << itr->second->getNumValNums() << ": " << itr->second << "\n";
314     }
315
316     if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
317       physIntervals.push_back(itr->second);
318       mri->setPhysRegUsed(itr->second->reg);
319     }
320     else {
321
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())
325         continue;
326
327       li2Node[itr->second] = node2LI.size();
328       node2LI.push_back(itr->second);
329       virtIntervals.push_back(itr->second);
330     }
331   }
332
333   // Early out if there's no regs to allocate for.
334   if (virtIntervals.empty())
335     return 0;
336
337   // Construct a PBQP solver for this problem
338   pbqp *solver = alloc_pbqp(virtIntervals.size());
339
340   // Resize allowedSets container appropriately.
341   allowedSets.resize(virtIntervals.size());
342
343   // Iterate over virtual register intervals to compute allowed sets...
344   for (unsigned node = 0; node < node2LI.size(); ++node) {
345
346     // Grab pointers to the interval and its register class.
347     const LiveInterval *li = node2LI[node];
348     const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
349     
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));
353
354     // If this range is non-empty then eliminate the physical registers which
355     // overlap with this range, along with all their aliases.
356     if (!li->empty()) {
357       for (LIVector::iterator pItr = physIntervals.begin(),
358            pEnd = physIntervals.end(); pItr != pEnd; ++pItr) {
359
360         if (li->overlaps(**pItr)) {
361
362           unsigned pReg = (*pItr)->reg;
363
364           // Remove the overlapping reg...
365           liAllowed.erase(pReg);
366
367           const unsigned *aliasItr = tri->getAliasSet(pReg);
368
369           if (aliasItr != 0) {
370             // ...and its aliases.
371             for (; *aliasItr != 0; ++aliasItr) {
372               liAllowed.erase(*aliasItr);
373             }
374
375           }
376         
377         }
378
379       }
380
381     }
382
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());
386
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();
391
392     // Build a cost vector for this interval.
393     add_pbqp_nodecosts(solver, node,
394                        buildCostVector(allowedSets[node], spillCost));
395
396   }
397
398   // Now add the cost matrices...
399   for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) {
400       
401     const LiveInterval *li = node2LI[node1];
402
403     if (li->empty())
404       continue;
405  
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];
409
410       if (li2->empty())
411         continue;
412
413       if (li->overlaps(*li2)) {
414         PBQPMatrix *m =
415           buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]);
416
417         if (m != 0) {
418           add_pbqp_edgecosts(solver, node1, node2, m);
419           delete m;
420         }
421       }
422     }
423   }
424
425   // We're done, PBQP problem constructed - return it.
426   return solver; 
427 }
428
429 bool PBQPRegAlloc::mapPBQPToRegAlloc(pbqp *problem) {
430   
431   // Set to true if we have any spills
432   bool anotherRoundNeeded = false;
433
434   // Clear the existing allocation.
435   vrm->clearAllVirt();
436   
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);
441
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];
446
447       // Add to the virt reg map and update the used phys regs.
448       vrm->assignVirt2Phys(symReg, physReg);
449       mri->setPhysRegUsed(physReg);
450     }
451     // ...Otherwise it's a spill.
452     else {
453
454       // Make sure we ignore this virtual reg on the next round
455       // of allocation
456       ignoreSet.insert(node2LI[node]->reg);
457
458       float SSWeight;
459
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,
464                                   SSWeight);
465
466       // We need another round if spill intervals were added.
467       anotherRoundNeeded |= !newSpills.empty();
468     }
469   }
470
471   return !anotherRoundNeeded;
472 }
473
474 bool PBQPRegAlloc::runOnMachineFunction(MachineFunction &MF) {
475   
476   mf = &MF;
477   tm = &mf->getTarget();
478   tri = tm->getRegisterInfo();
479   mri = &mf->getRegInfo();
480
481   li = &getAnalysis<LiveIntervals>();
482   loopInfo = &getAnalysis<MachineLoopInfo>();
483
484   std::auto_ptr<VirtRegMap> vrmAutoPtr(new VirtRegMap(*mf));
485   vrm = vrmAutoPtr.get();
486
487   // Allocator main loop:
488   // 
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
493   // 
494   // This process is continued till no more spills are generated.
495
496   bool regallocComplete = false;
497   
498   // Calculate spill costs for intervals
499   calcSpillCosts();
500
501   while (!regallocComplete) {
502     pbqp *problem = constructPBQPProblem();
503    
504     // Fast out if there's no problem to solve.
505     if (problem == 0)
506       return true;
507  
508     solve_pbqp(problem);
509    
510     regallocComplete = mapPBQPToRegAlloc(problem);
511
512     free_pbqp(problem); 
513   }
514
515   ignoreSet.clear();
516
517   std::auto_ptr<Spiller> spiller(createSpiller());
518
519   spiller->runOnMachineFunction(*mf, *vrm);
520     
521   return true; 
522 }
523
524 FunctionPass* llvm::createPBQPRegisterAllocator() {
525   return new PBQPRegAlloc();
526 }
527
528
529 #undef DEBUG_TYPE