blob: c677d341bef9f88689fc7ffb06066bb0da30fdbd [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 Hames6699fb22009-08-06 23:32:48 +000034#include "PBQP/HeuristicSolver.h"
35#include "PBQP/SimpleGraph.h"
36#include "PBQP/Heuristics/Briggs.h"
Evan Chengb1290a62008-10-02 18:29:27 +000037#include "VirtRegMap.h"
Lang Hames87e3bca2009-05-06 02:36:21 +000038#include "VirtRegRewriter.h"
Evan Chengb1290a62008-10-02 18:29:27 +000039#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Lang Hames27601ef2008-11-16 12:12:54 +000040#include "llvm/CodeGen/LiveStackAnalysis.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000041#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chengb1290a62008-10-02 18:29:27 +000042#include "llvm/CodeGen/MachineLoopInfo.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000043#include "llvm/CodeGen/MachineRegisterInfo.h"
44#include "llvm/CodeGen/RegAllocRegistry.h"
45#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengb1290a62008-10-02 18:29:27 +000046#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000047#include "llvm/Support/raw_ostream.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000048#include "llvm/Target/TargetInstrInfo.h"
49#include "llvm/Target/TargetMachine.h"
50#include <limits>
Evan Chengb1290a62008-10-02 18:29:27 +000051#include <map>
Misha Brukman2a835f92009-01-08 15:50:22 +000052#include <memory>
Evan Chengb1290a62008-10-02 18:29:27 +000053#include <set>
54#include <vector>
Evan Chengb1290a62008-10-02 18:29:27 +000055
56using namespace llvm;
57
58static RegisterRegAlloc
Lang Hames6699fb22009-08-06 23:32:48 +000059registerPBQPRepAlloc("pbqp", "PBQP register allocator.",
60 llvm::createPBQPRegisterAllocator);
Evan Chengb1290a62008-10-02 18:29:27 +000061
Lang Hames8481e3b2009-08-19 01:36:14 +000062static cl::opt<bool>
63pbqpCoalescing("pbqp-coalescing",
64 cl::desc("Attempt coalescing during PBQP register allocation."),
65 cl::init(false), cl::Hidden);
66
Evan Chengb1290a62008-10-02 18:29:27 +000067namespace {
68
Lang Hames6699fb22009-08-06 23:32:48 +000069 ///
70 /// PBQP based allocators solve the register allocation problem by mapping
71 /// register allocation problems to Partitioned Boolean Quadratic
72 /// Programming problems.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000073 class PBQPRegAlloc : public MachineFunctionPass {
Evan Chengb1290a62008-10-02 18:29:27 +000074 public:
75
76 static char ID;
Daniel Dunbara279bc32009-09-20 02:20:51 +000077
Lang Hames6699fb22009-08-06 23:32:48 +000078 /// Construct a PBQP register allocator.
Dan Gohman1b2d0b82009-08-11 15:15:10 +000079 PBQPRegAlloc() : MachineFunctionPass(&ID) {}
Evan Chengb1290a62008-10-02 18:29:27 +000080
Lang Hames6699fb22009-08-06 23:32:48 +000081 /// Return the pass name.
Dan Gohman00b0a242009-08-11 15:35:57 +000082 virtual const char* getPassName() const {
Evan Chengb1290a62008-10-02 18:29:27 +000083 return "PBQP Register Allocator";
84 }
85
Lang Hames6699fb22009-08-06 23:32:48 +000086 /// PBQP analysis usage.
87 virtual void getAnalysisUsage(AnalysisUsage &au) const {
Lang Hames233a60e2009-11-03 23:52:08 +000088 au.addRequired<SlotIndexes>();
89 au.addPreserved<SlotIndexes>();
Lang Hames6699fb22009-08-06 23:32:48 +000090 au.addRequired<LiveIntervals>();
91 //au.addRequiredID(SplitCriticalEdgesID);
Lang Hamesf7c553e2009-08-12 21:04:53 +000092 au.addRequired<RegisterCoalescer>();
Lang Hames6699fb22009-08-06 23:32:48 +000093 au.addRequired<LiveStacks>();
94 au.addPreserved<LiveStacks>();
95 au.addRequired<MachineLoopInfo>();
96 au.addPreserved<MachineLoopInfo>();
97 au.addRequired<VirtRegMap>();
98 MachineFunctionPass::getAnalysisUsage(au);
Evan Chengb1290a62008-10-02 18:29:27 +000099 }
100
Lang Hames6699fb22009-08-06 23:32:48 +0000101 /// Perform register allocation
Evan Chengb1290a62008-10-02 18:29:27 +0000102 virtual bool runOnMachineFunction(MachineFunction &MF);
103
104 private:
105 typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
106 typedef std::vector<const LiveInterval*> Node2LIMap;
107 typedef std::vector<unsigned> AllowedSet;
108 typedef std::vector<AllowedSet> AllowedSetMap;
Lang Hames27601ef2008-11-16 12:12:54 +0000109 typedef std::set<unsigned> RegSet;
110 typedef std::pair<unsigned, unsigned> RegPair;
Lang Hames6699fb22009-08-06 23:32:48 +0000111 typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
Lang Hames27601ef2008-11-16 12:12:54 +0000112
113 typedef std::set<LiveInterval*> LiveIntervalSet;
Evan Chengb1290a62008-10-02 18:29:27 +0000114
115 MachineFunction *mf;
116 const TargetMachine *tm;
117 const TargetRegisterInfo *tri;
118 const TargetInstrInfo *tii;
119 const MachineLoopInfo *loopInfo;
120 MachineRegisterInfo *mri;
121
Lang Hames27601ef2008-11-16 12:12:54 +0000122 LiveIntervals *lis;
123 LiveStacks *lss;
Evan Chengb1290a62008-10-02 18:29:27 +0000124 VirtRegMap *vrm;
125
126 LI2NodeMap li2Node;
127 Node2LIMap node2LI;
128 AllowedSetMap allowedSets;
Lang Hames27601ef2008-11-16 12:12:54 +0000129 LiveIntervalSet vregIntervalsToAlloc,
130 emptyVRegIntervals;
Evan Chengb1290a62008-10-02 18:29:27 +0000131
Misha Brukman2a835f92009-01-08 15:50:22 +0000132
Lang Hames6699fb22009-08-06 23:32:48 +0000133 /// Builds a PBQP cost vector.
Lang Hames27601ef2008-11-16 12:12:54 +0000134 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000135 PBQP::Vector buildCostVector(unsigned vReg,
136 const RegContainer &allowed,
137 const CoalesceMap &cealesces,
138 PBQP::PBQPNum spillCost) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000139
Lang Hames6699fb22009-08-06 23:32:48 +0000140 /// \brief Builds a PBQP interference matrix.
141 ///
142 /// @return Either a pointer to a non-zero PBQP matrix representing the
143 /// allocation option costs, or a null pointer for a zero matrix.
144 ///
145 /// Expects allowed sets for two interfering LiveIntervals. These allowed
146 /// sets should contain only allocable registers from the LiveInterval's
147 /// register class, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000148 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000149 PBQP::Matrix* buildInterferenceMatrix(const RegContainer &allowed1,
150 const RegContainer &allowed2) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000151
Lang Hames6699fb22009-08-06 23:32:48 +0000152 ///
153 /// Expects allowed sets for two potentially coalescable LiveIntervals,
154 /// and an estimated benefit due to coalescing. The allowed sets should
155 /// contain only allocable registers from the LiveInterval's register
156 /// classes, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000157 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000158 PBQP::Matrix* buildCoalescingMatrix(const RegContainer &allowed1,
159 const RegContainer &allowed2,
160 PBQP::PBQPNum cBenefit) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000161
Lang Hames6699fb22009-08-06 23:32:48 +0000162 /// \brief Finds coalescing opportunities and returns them as a map.
163 ///
164 /// Any entries in the map are guaranteed coalescable, even if their
165 /// corresponding live intervals overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000166 CoalesceMap findCoalesces();
Evan Chengb1290a62008-10-02 18:29:27 +0000167
Lang Hames6699fb22009-08-06 23:32:48 +0000168 /// \brief Finds the initial set of vreg intervals to allocate.
Lang Hames27601ef2008-11-16 12:12:54 +0000169 void findVRegIntervalsToAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000170
Lang Hames6699fb22009-08-06 23:32:48 +0000171 /// \brief Constructs a PBQP problem representation of the register
172 /// allocation problem for this function.
173 ///
174 /// @return a PBQP solver object for the register allocation problem.
175 PBQP::SimpleGraph constructPBQPProblem();
Evan Chengb1290a62008-10-02 18:29:27 +0000176
Lang Hames6699fb22009-08-06 23:32:48 +0000177 /// \brief Adds a stack interval if the given live interval has been
178 /// spilled. Used to support stack slot coloring.
Evan Chengc781a242009-05-03 18:32:42 +0000179 void addStackInterval(const LiveInterval *spilled,MachineRegisterInfo* mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000180
Lang Hames6699fb22009-08-06 23:32:48 +0000181 /// \brief Given a solved PBQP problem maps this solution back to a register
182 /// assignment.
183 bool mapPBQPToRegAlloc(const PBQP::Solution &solution);
Evan Chengb1290a62008-10-02 18:29:27 +0000184
Lang Hames6699fb22009-08-06 23:32:48 +0000185 /// \brief Postprocessing before final spilling. Sets basic block "live in"
186 /// variables.
Lang Hames27601ef2008-11-16 12:12:54 +0000187 void finalizeAlloc() const;
188
Evan Chengb1290a62008-10-02 18:29:27 +0000189 };
190
191 char PBQPRegAlloc::ID = 0;
192}
193
194
Lang Hames27601ef2008-11-16 12:12:54 +0000195template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000196PBQP::Vector PBQPRegAlloc::buildCostVector(unsigned vReg,
197 const RegContainer &allowed,
198 const CoalesceMap &coalesces,
199 PBQP::PBQPNum spillCost) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000200
Lang Hames27601ef2008-11-16 12:12:54 +0000201 typedef typename RegContainer::const_iterator AllowedItr;
202
Evan Chengb1290a62008-10-02 18:29:27 +0000203 // Allocate vector. Additional element (0th) used for spill option
Lang Hames6699fb22009-08-06 23:32:48 +0000204 PBQP::Vector v(allowed.size() + 1, 0);
Evan Chengb1290a62008-10-02 18:29:27 +0000205
Lang Hames6699fb22009-08-06 23:32:48 +0000206 v[0] = spillCost;
Evan Chengb1290a62008-10-02 18:29:27 +0000207
Lang Hames27601ef2008-11-16 12:12:54 +0000208 // Iterate over the allowed registers inserting coalesce benefits if there
209 // are any.
210 unsigned ai = 0;
211 for (AllowedItr itr = allowed.begin(), end = allowed.end();
212 itr != end; ++itr, ++ai) {
213
214 unsigned pReg = *itr;
215
216 CoalesceMap::const_iterator cmItr =
217 coalesces.find(RegPair(vReg, pReg));
218
219 // No coalesce - on to the next preg.
220 if (cmItr == coalesces.end())
221 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000222
223 // We have a coalesce - insert the benefit.
Lang Hames6699fb22009-08-06 23:32:48 +0000224 v[ai + 1] = -cmItr->second;
Lang Hames27601ef2008-11-16 12:12:54 +0000225 }
226
Evan Chengb1290a62008-10-02 18:29:27 +0000227 return v;
228}
229
Lang Hames27601ef2008-11-16 12:12:54 +0000230template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000231PBQP::Matrix* PBQPRegAlloc::buildInterferenceMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000232 const RegContainer &allowed1, const RegContainer &allowed2) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000233
Lang Hames27601ef2008-11-16 12:12:54 +0000234 typedef typename RegContainer::const_iterator RegContainerIterator;
Evan Chengb1290a62008-10-02 18:29:27 +0000235
236 // Construct a PBQP matrix representing the cost of allocation options. The
237 // rows and columns correspond to the allocation options for the two live
238 // intervals. Elements will be infinite where corresponding registers alias,
239 // since we cannot allocate aliasing registers to interfering live intervals.
240 // All other elements (non-aliasing combinations) will have zero cost. Note
241 // that the spill option (element 0,0) has zero cost, since we can allocate
242 // both intervals to memory safely (the cost for each individual allocation
243 // to memory is accounted for by the cost vectors for each live interval).
Lang Hames6699fb22009-08-06 23:32:48 +0000244 PBQP::Matrix *m =
245 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Misha Brukman2a835f92009-01-08 15:50:22 +0000246
Evan Chengb1290a62008-10-02 18:29:27 +0000247 // Assume this is a zero matrix until proven otherwise. Zero matrices occur
248 // between interfering live ranges with non-overlapping register sets (e.g.
249 // non-overlapping reg classes, or disjoint sets of allowed regs within the
250 // same class). The term "overlapping" is used advisedly: sets which do not
251 // intersect, but contain registers which alias, will have non-zero matrices.
252 // We optimize zero matrices away to improve solver speed.
253 bool isZeroMatrix = true;
254
255
256 // Row index. Starts at 1, since the 0th row is for the spill option, which
257 // is always zero.
Misha Brukman2a835f92009-01-08 15:50:22 +0000258 unsigned ri = 1;
Evan Chengb1290a62008-10-02 18:29:27 +0000259
Misha Brukman2a835f92009-01-08 15:50:22 +0000260 // Iterate over allowed sets, insert infinities where required.
Lang Hames27601ef2008-11-16 12:12:54 +0000261 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000262 a1Itr != a1End; ++a1Itr) {
263
264 // Column index, starts at 1 as for row index.
265 unsigned ci = 1;
266 unsigned reg1 = *a1Itr;
267
Lang Hames27601ef2008-11-16 12:12:54 +0000268 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000269 a2Itr != a2End; ++a2Itr) {
270
271 unsigned reg2 = *a2Itr;
272
273 // If the row/column regs are identical or alias insert an infinity.
Lang Hames3f2f3f52009-09-03 02:52:02 +0000274 if (tri->regsOverlap(reg1, reg2)) {
Lang Hames6699fb22009-08-06 23:32:48 +0000275 (*m)[ri][ci] = std::numeric_limits<PBQP::PBQPNum>::infinity();
Evan Chengb1290a62008-10-02 18:29:27 +0000276 isZeroMatrix = false;
277 }
278
279 ++ci;
280 }
281
282 ++ri;
283 }
284
285 // If this turns out to be a zero matrix...
286 if (isZeroMatrix) {
287 // free it and return null.
288 delete m;
289 return 0;
290 }
291
292 // ...otherwise return the cost matrix.
293 return m;
294}
295
Lang Hames27601ef2008-11-16 12:12:54 +0000296template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000297PBQP::Matrix* PBQPRegAlloc::buildCoalescingMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000298 const RegContainer &allowed1, const RegContainer &allowed2,
Lang Hames6699fb22009-08-06 23:32:48 +0000299 PBQP::PBQPNum cBenefit) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000300
Lang Hames27601ef2008-11-16 12:12:54 +0000301 typedef typename RegContainer::const_iterator RegContainerIterator;
302
303 // Construct a PBQP Matrix representing the benefits of coalescing. As with
304 // interference matrices the rows and columns represent allowed registers
305 // for the LiveIntervals which are (potentially) to be coalesced. The amount
306 // -cBenefit will be placed in any element representing the same register
307 // for both intervals.
Lang Hames6699fb22009-08-06 23:32:48 +0000308 PBQP::Matrix *m =
309 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Lang Hames27601ef2008-11-16 12:12:54 +0000310
311 // Reset costs to zero.
312 m->reset(0);
313
314 // Assume the matrix is zero till proven otherwise. Zero matrices will be
315 // optimized away as in the interference case.
316 bool isZeroMatrix = true;
317
318 // Row index. Starts at 1, since the 0th row is for the spill option, which
319 // is always zero.
320 unsigned ri = 1;
321
322 // Iterate over the allowed sets, insert coalescing benefits where
323 // appropriate.
324 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
325 a1Itr != a1End; ++a1Itr) {
326
327 // Column index, starts at 1 as for row index.
328 unsigned ci = 1;
329 unsigned reg1 = *a1Itr;
330
331 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
332 a2Itr != a2End; ++a2Itr) {
333
334 // If the row and column represent the same register insert a beneficial
335 // cost to preference this allocation - it would allow us to eliminate a
Misha Brukman2a835f92009-01-08 15:50:22 +0000336 // move instruction.
Lang Hames27601ef2008-11-16 12:12:54 +0000337 if (reg1 == *a2Itr) {
338 (*m)[ri][ci] = -cBenefit;
339 isZeroMatrix = false;
340 }
341
342 ++ci;
343 }
344
345 ++ri;
346 }
347
348 // If this turns out to be a zero matrix...
349 if (isZeroMatrix) {
350 // ...free it and return null.
351 delete m;
352 return 0;
353 }
354
355 return m;
356}
357
358PBQPRegAlloc::CoalesceMap PBQPRegAlloc::findCoalesces() {
359
360 typedef MachineFunction::const_iterator MFIterator;
361 typedef MachineBasicBlock::const_iterator MBBIterator;
362 typedef LiveInterval::const_vni_iterator VNIIterator;
Misha Brukman2a835f92009-01-08 15:50:22 +0000363
Lang Hames27601ef2008-11-16 12:12:54 +0000364 CoalesceMap coalescesFound;
365
366 // To find coalesces we need to iterate over the function looking for
367 // copy instructions.
368 for (MFIterator bbItr = mf->begin(), bbEnd = mf->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000369 bbItr != bbEnd; ++bbItr) {
370
371 const MachineBasicBlock *mbb = &*bbItr;
Evan Chengb1290a62008-10-02 18:29:27 +0000372
Lang Hames27601ef2008-11-16 12:12:54 +0000373 for (MBBIterator iItr = mbb->begin(), iEnd = mbb->end();
374 iItr != iEnd; ++iItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000375
376 const MachineInstr *instr = &*iItr;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000377 unsigned srcReg, dstReg, srcSubReg, dstSubReg;
Evan Chengb1290a62008-10-02 18:29:27 +0000378
Lang Hames27601ef2008-11-16 12:12:54 +0000379 // If this isn't a copy then continue to the next instruction.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000380 if (!tii->isMoveInstr(*instr, srcReg, dstReg, srcSubReg, dstSubReg))
Lang Hames27601ef2008-11-16 12:12:54 +0000381 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000382
Lang Hames27601ef2008-11-16 12:12:54 +0000383 // If the registers are already the same our job is nice and easy.
384 if (dstReg == srcReg)
385 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000386
Lang Hames27601ef2008-11-16 12:12:54 +0000387 bool srcRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(srcReg),
388 dstRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(dstReg);
389
390 // If both registers are physical then we can't coalesce.
391 if (srcRegIsPhysical && dstRegIsPhysical)
392 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000393
Lang Hames27601ef2008-11-16 12:12:54 +0000394 // If it's a copy that includes a virtual register but the source and
395 // destination classes differ then we can't coalesce, so continue with
396 // the next instruction.
397 const TargetRegisterClass *srcRegClass = srcRegIsPhysical ?
398 tri->getPhysicalRegisterRegClass(srcReg) : mri->getRegClass(srcReg);
399
400 const TargetRegisterClass *dstRegClass = dstRegIsPhysical ?
401 tri->getPhysicalRegisterRegClass(dstReg) : mri->getRegClass(dstReg);
402
403 if (srcRegClass != dstRegClass)
404 continue;
405
406 // We also need any physical regs to be allocable, coalescing with
407 // a non-allocable register is invalid.
408 if (srcRegIsPhysical) {
409 if (std::find(srcRegClass->allocation_order_begin(*mf),
410 srcRegClass->allocation_order_end(*mf), srcReg) ==
411 srcRegClass->allocation_order_end(*mf))
Evan Chengb1290a62008-10-02 18:29:27 +0000412 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000413 }
414
Lang Hames27601ef2008-11-16 12:12:54 +0000415 if (dstRegIsPhysical) {
416 if (std::find(dstRegClass->allocation_order_begin(*mf),
417 dstRegClass->allocation_order_end(*mf), dstReg) ==
418 dstRegClass->allocation_order_end(*mf))
419 continue;
420 }
421
422 // If we've made it here we have a copy with compatible register classes.
Misha Brukman2a835f92009-01-08 15:50:22 +0000423 // We can probably coalesce, but we need to consider overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000424 const LiveInterval *srcLI = &lis->getInterval(srcReg),
425 *dstLI = &lis->getInterval(dstReg);
426
427 if (srcLI->overlaps(*dstLI)) {
428 // Even in the case of an overlap we might still be able to coalesce,
429 // but we need to make sure that no definition of either range occurs
430 // while the other range is live.
431
432 // Otherwise start by assuming we're ok.
433 bool badDef = false;
434
435 // Test all defs of the source range.
Misha Brukman2a835f92009-01-08 15:50:22 +0000436 for (VNIIterator
Lang Hames27601ef2008-11-16 12:12:54 +0000437 vniItr = srcLI->vni_begin(), vniEnd = srcLI->vni_end();
438 vniItr != vniEnd; ++vniItr) {
439
440 // If we find a def that kills the coalescing opportunity then
441 // record it and break from the loop.
442 if (dstLI->liveAt((*vniItr)->def)) {
443 badDef = true;
444 break;
445 }
446 }
447
448 // If we have a bad def give up, continue to the next instruction.
449 if (badDef)
450 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000451
Lang Hames27601ef2008-11-16 12:12:54 +0000452 // Otherwise test definitions of the destination range.
453 for (VNIIterator
454 vniItr = dstLI->vni_begin(), vniEnd = dstLI->vni_end();
455 vniItr != vniEnd; ++vniItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000456
Lang Hames27601ef2008-11-16 12:12:54 +0000457 // We want to make sure we skip the copy instruction itself.
Lang Hames52c1afc2009-08-10 23:43:28 +0000458 if ((*vniItr)->getCopy() == instr)
Lang Hames27601ef2008-11-16 12:12:54 +0000459 continue;
460
461 if (srcLI->liveAt((*vniItr)->def)) {
462 badDef = true;
463 break;
464 }
465 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000466
Lang Hames27601ef2008-11-16 12:12:54 +0000467 // As before a bad def we give up and continue to the next instr.
468 if (badDef)
469 continue;
470 }
471
472 // If we make it to here then either the ranges didn't overlap, or they
473 // did, but none of their definitions would prevent us from coalescing.
474 // We're good to go with the coalesce.
475
476 float cBenefit = powf(10.0f, loopInfo->getLoopDepth(mbb)) / 5.0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000477
Lang Hames27601ef2008-11-16 12:12:54 +0000478 coalescesFound[RegPair(srcReg, dstReg)] = cBenefit;
479 coalescesFound[RegPair(dstReg, srcReg)] = cBenefit;
Evan Chengb1290a62008-10-02 18:29:27 +0000480 }
481
482 }
483
Lang Hames27601ef2008-11-16 12:12:54 +0000484 return coalescesFound;
485}
486
487void PBQPRegAlloc::findVRegIntervalsToAlloc() {
488
489 // Iterate over all live ranges.
490 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
491 itr != end; ++itr) {
492
493 // Ignore physical ones.
494 if (TargetRegisterInfo::isPhysicalRegister(itr->first))
495 continue;
496
497 LiveInterval *li = itr->second;
498
499 // If this live interval is non-empty we will use pbqp to allocate it.
500 // Empty intervals we allocate in a simple post-processing stage in
501 // finalizeAlloc.
502 if (!li->empty()) {
503 vregIntervalsToAlloc.insert(li);
504 }
505 else {
506 emptyVRegIntervals.insert(li);
507 }
508 }
Evan Chengb1290a62008-10-02 18:29:27 +0000509}
510
Lang Hames6699fb22009-08-06 23:32:48 +0000511PBQP::SimpleGraph PBQPRegAlloc::constructPBQPProblem() {
Evan Chengb1290a62008-10-02 18:29:27 +0000512
513 typedef std::vector<const LiveInterval*> LIVector;
Lang Hames27601ef2008-11-16 12:12:54 +0000514 typedef std::vector<unsigned> RegVector;
Lang Hames6699fb22009-08-06 23:32:48 +0000515 typedef std::vector<PBQP::SimpleGraph::NodeIterator> NodeVector;
Evan Chengb1290a62008-10-02 18:29:27 +0000516
Lang Hames27601ef2008-11-16 12:12:54 +0000517 // This will store the physical intervals for easy reference.
518 LIVector physIntervals;
Evan Chengb1290a62008-10-02 18:29:27 +0000519
520 // Start by clearing the old node <-> live interval mappings & allowed sets
521 li2Node.clear();
522 node2LI.clear();
523 allowedSets.clear();
524
Lang Hames27601ef2008-11-16 12:12:54 +0000525 // Populate physIntervals, update preg use:
526 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000527 itr != end; ++itr) {
528
Evan Chengb1290a62008-10-02 18:29:27 +0000529 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
530 physIntervals.push_back(itr->second);
531 mri->setPhysRegUsed(itr->second->reg);
532 }
Evan Chengb1290a62008-10-02 18:29:27 +0000533 }
534
Lang Hames27601ef2008-11-16 12:12:54 +0000535 // Iterate over vreg intervals, construct live interval <-> node number
536 // mappings.
Misha Brukman2a835f92009-01-08 15:50:22 +0000537 for (LiveIntervalSet::const_iterator
Lang Hames27601ef2008-11-16 12:12:54 +0000538 itr = vregIntervalsToAlloc.begin(), end = vregIntervalsToAlloc.end();
539 itr != end; ++itr) {
540 const LiveInterval *li = *itr;
541
542 li2Node[li] = node2LI.size();
543 node2LI.push_back(li);
544 }
545
546 // Get the set of potential coalesces.
Lang Hames8481e3b2009-08-19 01:36:14 +0000547 CoalesceMap coalesces;
548
549 if (pbqpCoalescing) {
550 coalesces = findCoalesces();
551 }
Evan Chengb1290a62008-10-02 18:29:27 +0000552
553 // Construct a PBQP solver for this problem
Lang Hames6699fb22009-08-06 23:32:48 +0000554 PBQP::SimpleGraph problem;
555 NodeVector problemNodes(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000556
557 // Resize allowedSets container appropriately.
Lang Hames27601ef2008-11-16 12:12:54 +0000558 allowedSets.resize(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000559
560 // Iterate over virtual register intervals to compute allowed sets...
561 for (unsigned node = 0; node < node2LI.size(); ++node) {
562
563 // Grab pointers to the interval and its register class.
564 const LiveInterval *li = node2LI[node];
565 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000566
Evan Chengb1290a62008-10-02 18:29:27 +0000567 // Start by assuming all allocable registers in the class are allowed...
Lang Hames27601ef2008-11-16 12:12:54 +0000568 RegVector liAllowed(liRC->allocation_order_begin(*mf),
569 liRC->allocation_order_end(*mf));
Evan Chengb1290a62008-10-02 18:29:27 +0000570
Lang Hames27601ef2008-11-16 12:12:54 +0000571 // Eliminate the physical registers which overlap with this range, along
572 // with all their aliases.
573 for (LIVector::iterator pItr = physIntervals.begin(),
574 pEnd = physIntervals.end(); pItr != pEnd; ++pItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000575
Lang Hames27601ef2008-11-16 12:12:54 +0000576 if (!li->overlaps(**pItr))
577 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000578
Lang Hames27601ef2008-11-16 12:12:54 +0000579 unsigned pReg = (*pItr)->reg;
Evan Chengb1290a62008-10-02 18:29:27 +0000580
Lang Hames27601ef2008-11-16 12:12:54 +0000581 // If we get here then the live intervals overlap, but we're still ok
582 // if they're coalescable.
583 if (coalesces.find(RegPair(li->reg, pReg)) != coalesces.end())
584 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000585
Lang Hames27601ef2008-11-16 12:12:54 +0000586 // If we get here then we have a genuine exclusion.
Evan Chengb1290a62008-10-02 18:29:27 +0000587
Lang Hames27601ef2008-11-16 12:12:54 +0000588 // Remove the overlapping reg...
589 RegVector::iterator eraseItr =
590 std::find(liAllowed.begin(), liAllowed.end(), pReg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000591
Lang Hames27601ef2008-11-16 12:12:54 +0000592 if (eraseItr != liAllowed.end())
593 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000594
Lang Hames27601ef2008-11-16 12:12:54 +0000595 const unsigned *aliasItr = tri->getAliasSet(pReg);
596
597 if (aliasItr != 0) {
598 // ...and its aliases.
599 for (; *aliasItr != 0; ++aliasItr) {
600 RegVector::iterator eraseItr =
601 std::find(liAllowed.begin(), liAllowed.end(), *aliasItr);
Misha Brukman2a835f92009-01-08 15:50:22 +0000602
Lang Hames27601ef2008-11-16 12:12:54 +0000603 if (eraseItr != liAllowed.end()) {
604 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000605 }
Evan Chengb1290a62008-10-02 18:29:27 +0000606 }
Evan Chengb1290a62008-10-02 18:29:27 +0000607 }
Evan Chengb1290a62008-10-02 18:29:27 +0000608 }
609
610 // Copy the allowed set into a member vector for use when constructing cost
611 // vectors & matrices, and mapping PBQP solutions back to assignments.
612 allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end());
613
614 // Set the spill cost to the interval weight, or epsilon if the
615 // interval weight is zero
Lang Hames6699fb22009-08-06 23:32:48 +0000616 PBQP::PBQPNum spillCost = (li->weight != 0.0) ?
617 li->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000618
619 // Build a cost vector for this interval.
Lang Hames6699fb22009-08-06 23:32:48 +0000620 problemNodes[node] =
621 problem.addNode(
622 buildCostVector(li->reg, allowedSets[node], coalesces, spillCost));
Evan Chengb1290a62008-10-02 18:29:27 +0000623
624 }
625
Lang Hames27601ef2008-11-16 12:12:54 +0000626
Evan Chengb1290a62008-10-02 18:29:27 +0000627 // Now add the cost matrices...
628 for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) {
Evan Chengb1290a62008-10-02 18:29:27 +0000629 const LiveInterval *li = node2LI[node1];
630
Evan Chengb1290a62008-10-02 18:29:27 +0000631 // Test for live range overlaps and insert interference matrices.
632 for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) {
633 const LiveInterval *li2 = node2LI[node2];
634
Lang Hames27601ef2008-11-16 12:12:54 +0000635 CoalesceMap::const_iterator cmItr =
636 coalesces.find(RegPair(li->reg, li2->reg));
Evan Chengb1290a62008-10-02 18:29:27 +0000637
Lang Hames6699fb22009-08-06 23:32:48 +0000638 PBQP::Matrix *m = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000639
Lang Hames27601ef2008-11-16 12:12:54 +0000640 if (cmItr != coalesces.end()) {
641 m = buildCoalescingMatrix(allowedSets[node1], allowedSets[node2],
642 cmItr->second);
643 }
644 else if (li->overlaps(*li2)) {
645 m = buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]);
646 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000647
Lang Hames27601ef2008-11-16 12:12:54 +0000648 if (m != 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000649 problem.addEdge(problemNodes[node1],
650 problemNodes[node2],
651 *m);
652
Lang Hames27601ef2008-11-16 12:12:54 +0000653 delete m;
Evan Chengb1290a62008-10-02 18:29:27 +0000654 }
655 }
656 }
657
Lang Hames6699fb22009-08-06 23:32:48 +0000658 problem.assignNodeIDs();
659
660 assert(problem.getNumNodes() == allowedSets.size());
661 for (unsigned i = 0; i < allowedSets.size(); ++i) {
662 assert(problem.getNodeItr(i) == problemNodes[i]);
663 }
664/*
665 std::cerr << "Allocating for " << problem.getNumNodes() << " nodes, "
666 << problem.getNumEdges() << " edges.\n";
667
668 problem.printDot(std::cerr);
669*/
Evan Chengb1290a62008-10-02 18:29:27 +0000670 // We're done, PBQP problem constructed - return it.
Lang Hames6699fb22009-08-06 23:32:48 +0000671 return problem;
Evan Chengb1290a62008-10-02 18:29:27 +0000672}
673
Evan Chengc781a242009-05-03 18:32:42 +0000674void PBQPRegAlloc::addStackInterval(const LiveInterval *spilled,
675 MachineRegisterInfo* mri) {
Lang Hames27601ef2008-11-16 12:12:54 +0000676 int stackSlot = vrm->getStackSlot(spilled->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000677
678 if (stackSlot == VirtRegMap::NO_STACK_SLOT)
Lang Hames27601ef2008-11-16 12:12:54 +0000679 return;
680
Evan Chengc781a242009-05-03 18:32:42 +0000681 const TargetRegisterClass *RC = mri->getRegClass(spilled->reg);
682 LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC);
Lang Hames27601ef2008-11-16 12:12:54 +0000683
684 VNInfo *vni;
685 if (stackInterval.getNumValNums() != 0)
686 vni = stackInterval.getValNumInfo(0);
687 else
Lang Hames86511252009-09-04 20:41:11 +0000688 vni = stackInterval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +0000689 SlotIndex(), 0, false, lss->getVNInfoAllocator());
Lang Hames27601ef2008-11-16 12:12:54 +0000690
691 LiveInterval &rhsInterval = lis->getInterval(spilled->reg);
692 stackInterval.MergeRangesInAsValue(rhsInterval, vni);
693}
694
Lang Hames6699fb22009-08-06 23:32:48 +0000695bool PBQPRegAlloc::mapPBQPToRegAlloc(const PBQP::Solution &solution) {
Lang Hamese98b4b02009-11-15 04:39:51 +0000696
697 // Assert that this is a valid solution to the regalloc problem.
698 assert(solution.getCost() != std::numeric_limits<PBQP::PBQPNum>::infinity() &&
699 "Invalid (infinite cost) solution for PBQP problem.");
700
Evan Chengb1290a62008-10-02 18:29:27 +0000701 // Set to true if we have any spills
702 bool anotherRoundNeeded = false;
703
704 // Clear the existing allocation.
705 vrm->clearAllVirt();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000706
Evan Chengb1290a62008-10-02 18:29:27 +0000707 // Iterate over the nodes mapping the PBQP solution to a register assignment.
708 for (unsigned node = 0; node < node2LI.size(); ++node) {
Lang Hames27601ef2008-11-16 12:12:54 +0000709 unsigned virtReg = node2LI[node]->reg,
Lang Hames6699fb22009-08-06 23:32:48 +0000710 allocSelection = solution.getSelection(node);
711
Evan Chengb1290a62008-10-02 18:29:27 +0000712
713 // If the PBQP solution is non-zero it's a physical register...
714 if (allocSelection != 0) {
715 // Get the physical reg, subtracting 1 to account for the spill option.
716 unsigned physReg = allowedSets[node][allocSelection - 1];
717
Lang Hames233fd9c2009-08-18 23:34:50 +0000718 DEBUG(errs() << "VREG " << virtReg << " -> "
719 << tri->getName(physReg) << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000720
721 assert(physReg != 0);
722
Evan Chengb1290a62008-10-02 18:29:27 +0000723 // Add to the virt reg map and update the used phys regs.
Lang Hames27601ef2008-11-16 12:12:54 +0000724 vrm->assignVirt2Phys(virtReg, physReg);
Evan Chengb1290a62008-10-02 18:29:27 +0000725 }
726 // ...Otherwise it's a spill.
727 else {
728
729 // Make sure we ignore this virtual reg on the next round
730 // of allocation
Lang Hames27601ef2008-11-16 12:12:54 +0000731 vregIntervalsToAlloc.erase(&lis->getInterval(virtReg));
Evan Chengb1290a62008-10-02 18:29:27 +0000732
Evan Chengb1290a62008-10-02 18:29:27 +0000733 // Insert spill ranges for this live range
Lang Hames27601ef2008-11-16 12:12:54 +0000734 const LiveInterval *spillInterval = node2LI[node];
735 double oldSpillWeight = spillInterval->weight;
Evan Chengb1290a62008-10-02 18:29:27 +0000736 SmallVector<LiveInterval*, 8> spillIs;
737 std::vector<LiveInterval*> newSpills =
Evan Chengc781a242009-05-03 18:32:42 +0000738 lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm);
739 addStackInterval(spillInterval, mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000740
Daniel Dunbarbc84ad92009-08-20 20:01:34 +0000741 (void) oldSpillWeight;
Lang Hames233fd9c2009-08-18 23:34:50 +0000742 DEBUG(errs() << "VREG " << virtReg << " -> SPILLED (Cost: "
743 << oldSpillWeight << ", New vregs: ");
Lang Hames27601ef2008-11-16 12:12:54 +0000744
745 // Copy any newly inserted live intervals into the list of regs to
746 // allocate.
747 for (std::vector<LiveInterval*>::const_iterator
748 itr = newSpills.begin(), end = newSpills.end();
749 itr != end; ++itr) {
750
751 assert(!(*itr)->empty() && "Empty spill range.");
752
Lang Hames233fd9c2009-08-18 23:34:50 +0000753 DEBUG(errs() << (*itr)->reg << " ");
Lang Hames27601ef2008-11-16 12:12:54 +0000754
755 vregIntervalsToAlloc.insert(*itr);
756 }
757
Lang Hames233fd9c2009-08-18 23:34:50 +0000758 DEBUG(errs() << ")\n");
Evan Chengb1290a62008-10-02 18:29:27 +0000759
760 // We need another round if spill intervals were added.
761 anotherRoundNeeded |= !newSpills.empty();
762 }
763 }
764
765 return !anotherRoundNeeded;
766}
767
Lang Hames27601ef2008-11-16 12:12:54 +0000768void PBQPRegAlloc::finalizeAlloc() const {
769 typedef LiveIntervals::iterator LIIterator;
770 typedef LiveInterval::Ranges::const_iterator LRIterator;
771
772 // First allocate registers for the empty intervals.
Argyrios Kyrtzidis3713c0b2008-11-19 12:56:21 +0000773 for (LiveIntervalSet::const_iterator
Daniel Dunbara279bc32009-09-20 02:20:51 +0000774 itr = emptyVRegIntervals.begin(), end = emptyVRegIntervals.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000775 itr != end; ++itr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000776 LiveInterval *li = *itr;
Lang Hames27601ef2008-11-16 12:12:54 +0000777
Evan Cheng90f95f82009-06-14 20:22:55 +0000778 unsigned physReg = vrm->getRegAllocPref(li->reg);
Lang Hames6699fb22009-08-06 23:32:48 +0000779
Lang Hames27601ef2008-11-16 12:12:54 +0000780 if (physReg == 0) {
781 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000782 physReg = *liRC->allocation_order_begin(*mf);
Lang Hames27601ef2008-11-16 12:12:54 +0000783 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000784
785 vrm->assignVirt2Phys(li->reg, physReg);
Lang Hames27601ef2008-11-16 12:12:54 +0000786 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000787
Lang Hames27601ef2008-11-16 12:12:54 +0000788 // Finally iterate over the basic blocks to compute and set the live-in sets.
789 SmallVector<MachineBasicBlock*, 8> liveInMBBs;
790 MachineBasicBlock *entryMBB = &*mf->begin();
791
792 for (LIIterator liItr = lis->begin(), liEnd = lis->end();
793 liItr != liEnd; ++liItr) {
794
795 const LiveInterval *li = liItr->second;
796 unsigned reg = 0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000797
Lang Hames27601ef2008-11-16 12:12:54 +0000798 // Get the physical register for this interval
799 if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
800 reg = li->reg;
801 }
802 else if (vrm->isAssignedReg(li->reg)) {
803 reg = vrm->getPhys(li->reg);
804 }
805 else {
806 // Ranges which are assigned a stack slot only are ignored.
807 continue;
808 }
809
Lang Hamesb0e519f2009-05-17 23:50:36 +0000810 if (reg == 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000811 // Filter out zero regs - they're for intervals that were spilled.
Lang Hamesb0e519f2009-05-17 23:50:36 +0000812 continue;
813 }
814
Lang Hames27601ef2008-11-16 12:12:54 +0000815 // Iterate over the ranges of the current interval...
816 for (LRIterator lrItr = li->begin(), lrEnd = li->end();
817 lrItr != lrEnd; ++lrItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000818
Lang Hames27601ef2008-11-16 12:12:54 +0000819 // Find the set of basic blocks which this range is live into...
820 if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
821 // And add the physreg for this interval to their live-in sets.
822 for (unsigned i = 0; i < liveInMBBs.size(); ++i) {
823 if (liveInMBBs[i] != entryMBB) {
824 if (!liveInMBBs[i]->isLiveIn(reg)) {
825 liveInMBBs[i]->addLiveIn(reg);
826 }
827 }
828 }
829 liveInMBBs.clear();
830 }
831 }
832 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000833
Lang Hames27601ef2008-11-16 12:12:54 +0000834}
835
Evan Chengb1290a62008-10-02 18:29:27 +0000836bool PBQPRegAlloc::runOnMachineFunction(MachineFunction &MF) {
Lang Hames27601ef2008-11-16 12:12:54 +0000837
Evan Chengb1290a62008-10-02 18:29:27 +0000838 mf = &MF;
839 tm = &mf->getTarget();
840 tri = tm->getRegisterInfo();
Lang Hames27601ef2008-11-16 12:12:54 +0000841 tii = tm->getInstrInfo();
Lang Hames233a60e2009-11-03 23:52:08 +0000842 mri = &mf->getRegInfo();
Evan Chengb1290a62008-10-02 18:29:27 +0000843
Lang Hames27601ef2008-11-16 12:12:54 +0000844 lis = &getAnalysis<LiveIntervals>();
845 lss = &getAnalysis<LiveStacks>();
Evan Chengb1290a62008-10-02 18:29:27 +0000846 loopInfo = &getAnalysis<MachineLoopInfo>();
847
Owen Anderson49c8aa02009-03-13 05:55:11 +0000848 vrm = &getAnalysis<VirtRegMap>();
Evan Chengb1290a62008-10-02 18:29:27 +0000849
Lang Hames6699fb22009-08-06 23:32:48 +0000850 DEBUG(errs() << "PBQP2 Register Allocating for " << mf->getFunction()->getName() << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000851
Evan Chengb1290a62008-10-02 18:29:27 +0000852 // Allocator main loop:
Misha Brukman2a835f92009-01-08 15:50:22 +0000853 //
Evan Chengb1290a62008-10-02 18:29:27 +0000854 // * Map current regalloc problem to a PBQP problem
855 // * Solve the PBQP problem
856 // * Map the solution back to a register allocation
857 // * Spill if necessary
Misha Brukman2a835f92009-01-08 15:50:22 +0000858 //
Evan Chengb1290a62008-10-02 18:29:27 +0000859 // This process is continued till no more spills are generated.
860
Lang Hames27601ef2008-11-16 12:12:54 +0000861 // Find the vreg intervals in need of allocation.
862 findVRegIntervalsToAlloc();
Misha Brukman2a835f92009-01-08 15:50:22 +0000863
Lang Hames27601ef2008-11-16 12:12:54 +0000864 // If there aren't any then we're done here.
865 if (vregIntervalsToAlloc.empty() && emptyVRegIntervals.empty())
866 return true;
Evan Chengb1290a62008-10-02 18:29:27 +0000867
Lang Hames27601ef2008-11-16 12:12:54 +0000868 // If there are non-empty intervals allocate them using pbqp.
869 if (!vregIntervalsToAlloc.empty()) {
Evan Chengb1290a62008-10-02 18:29:27 +0000870
Lang Hames27601ef2008-11-16 12:12:54 +0000871 bool pbqpAllocComplete = false;
872 unsigned round = 0;
873
874 while (!pbqpAllocComplete) {
Lang Hames6699fb22009-08-06 23:32:48 +0000875 DEBUG(errs() << " PBQP Regalloc round " << round << ":\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000876
Lang Hames6699fb22009-08-06 23:32:48 +0000877 PBQP::SimpleGraph problem = constructPBQPProblem();
878 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs> solver;
879 problem.assignNodeIDs();
880 PBQP::Solution solution = solver.solve(problem);
Lang Hames233fd9c2009-08-18 23:34:50 +0000881
Lang Hames6699fb22009-08-06 23:32:48 +0000882 pbqpAllocComplete = mapPBQPToRegAlloc(solution);
Lang Hames27601ef2008-11-16 12:12:54 +0000883
884 ++round;
885 }
Evan Chengb1290a62008-10-02 18:29:27 +0000886 }
887
Lang Hames27601ef2008-11-16 12:12:54 +0000888 // Finalise allocation, allocate empty ranges.
889 finalizeAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000890
Lang Hames27601ef2008-11-16 12:12:54 +0000891 vregIntervalsToAlloc.clear();
892 emptyVRegIntervals.clear();
893 li2Node.clear();
894 node2LI.clear();
895 allowedSets.clear();
896
Lang Hames6699fb22009-08-06 23:32:48 +0000897 DEBUG(errs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000898
Lang Hames87e3bca2009-05-06 02:36:21 +0000899 // Run rewriter
900 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
901
902 rewriter->runOnMachineFunction(*mf, *vrm, lis);
Lang Hames27601ef2008-11-16 12:12:54 +0000903
Misha Brukman2a835f92009-01-08 15:50:22 +0000904 return true;
Evan Chengb1290a62008-10-02 18:29:27 +0000905}
906
907FunctionPass* llvm::createPBQPRegisterAllocator() {
908 return new PBQPRegAlloc();
909}
910
911
912#undef DEBUG_TYPE