blob: e8b8152b3875f96a0a41ab07f253c6f301d5fab1 [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
Evan Chengb1290a62008-10-02 18:29:27 +000062namespace {
63
Lang Hames6699fb22009-08-06 23:32:48 +000064 ///
65 /// PBQP based allocators solve the register allocation problem by mapping
66 /// register allocation problems to Partitioned Boolean Quadratic
67 /// Programming problems.
Evan Chengb1290a62008-10-02 18:29:27 +000068 class VISIBILITY_HIDDEN PBQPRegAlloc : public MachineFunctionPass {
69 public:
70
71 static char ID;
Lang Hames6699fb22009-08-06 23:32:48 +000072
73 /// Construct a PBQP register allocator.
Dan Gohman1b2d0b82009-08-11 15:15:10 +000074 PBQPRegAlloc() : MachineFunctionPass(&ID) {}
Evan Chengb1290a62008-10-02 18:29:27 +000075
Lang Hames6699fb22009-08-06 23:32:48 +000076 /// Return the pass name.
Dan Gohman00b0a242009-08-11 15:35:57 +000077 virtual const char* getPassName() const {
Evan Chengb1290a62008-10-02 18:29:27 +000078 return "PBQP Register Allocator";
79 }
80
Lang Hames6699fb22009-08-06 23:32:48 +000081 /// PBQP analysis usage.
82 virtual void getAnalysisUsage(AnalysisUsage &au) const {
83 au.addRequired<LiveIntervals>();
84 //au.addRequiredID(SplitCriticalEdgesID);
Lang Hamesf7c553e2009-08-12 21:04:53 +000085 au.addRequired<RegisterCoalescer>();
Lang Hames6699fb22009-08-06 23:32:48 +000086 au.addRequired<LiveStacks>();
87 au.addPreserved<LiveStacks>();
88 au.addRequired<MachineLoopInfo>();
89 au.addPreserved<MachineLoopInfo>();
90 au.addRequired<VirtRegMap>();
91 MachineFunctionPass::getAnalysisUsage(au);
Evan Chengb1290a62008-10-02 18:29:27 +000092 }
93
Lang Hames6699fb22009-08-06 23:32:48 +000094 /// Perform register allocation
Evan Chengb1290a62008-10-02 18:29:27 +000095 virtual bool runOnMachineFunction(MachineFunction &MF);
96
97 private:
98 typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
99 typedef std::vector<const LiveInterval*> Node2LIMap;
100 typedef std::vector<unsigned> AllowedSet;
101 typedef std::vector<AllowedSet> AllowedSetMap;
Lang Hames27601ef2008-11-16 12:12:54 +0000102 typedef std::set<unsigned> RegSet;
103 typedef std::pair<unsigned, unsigned> RegPair;
Lang Hames6699fb22009-08-06 23:32:48 +0000104 typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
Lang Hames27601ef2008-11-16 12:12:54 +0000105
106 typedef std::set<LiveInterval*> LiveIntervalSet;
Evan Chengb1290a62008-10-02 18:29:27 +0000107
108 MachineFunction *mf;
109 const TargetMachine *tm;
110 const TargetRegisterInfo *tri;
111 const TargetInstrInfo *tii;
112 const MachineLoopInfo *loopInfo;
113 MachineRegisterInfo *mri;
114
Lang Hames27601ef2008-11-16 12:12:54 +0000115 LiveIntervals *lis;
116 LiveStacks *lss;
Evan Chengb1290a62008-10-02 18:29:27 +0000117 VirtRegMap *vrm;
118
119 LI2NodeMap li2Node;
120 Node2LIMap node2LI;
121 AllowedSetMap allowedSets;
Lang Hames27601ef2008-11-16 12:12:54 +0000122 LiveIntervalSet vregIntervalsToAlloc,
123 emptyVRegIntervals;
Evan Chengb1290a62008-10-02 18:29:27 +0000124
Misha Brukman2a835f92009-01-08 15:50:22 +0000125
Lang Hames6699fb22009-08-06 23:32:48 +0000126 /// Builds a PBQP cost vector.
Lang Hames27601ef2008-11-16 12:12:54 +0000127 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000128 PBQP::Vector buildCostVector(unsigned vReg,
129 const RegContainer &allowed,
130 const CoalesceMap &cealesces,
131 PBQP::PBQPNum spillCost) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000132
Lang Hames6699fb22009-08-06 23:32:48 +0000133 /// \brief Builds a PBQP interference matrix.
134 ///
135 /// @return Either a pointer to a non-zero PBQP matrix representing the
136 /// allocation option costs, or a null pointer for a zero matrix.
137 ///
138 /// Expects allowed sets for two interfering LiveIntervals. These allowed
139 /// sets should contain only allocable registers from the LiveInterval's
140 /// register class, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000141 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000142 PBQP::Matrix* buildInterferenceMatrix(const RegContainer &allowed1,
143 const RegContainer &allowed2) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000144
Lang Hames6699fb22009-08-06 23:32:48 +0000145 ///
146 /// Expects allowed sets for two potentially coalescable LiveIntervals,
147 /// and an estimated benefit due to coalescing. The allowed sets should
148 /// contain only allocable registers from the LiveInterval's register
149 /// classes, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000150 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000151 PBQP::Matrix* buildCoalescingMatrix(const RegContainer &allowed1,
152 const RegContainer &allowed2,
153 PBQP::PBQPNum cBenefit) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000154
Lang Hames6699fb22009-08-06 23:32:48 +0000155 /// \brief Finds coalescing opportunities and returns them as a map.
156 ///
157 /// Any entries in the map are guaranteed coalescable, even if their
158 /// corresponding live intervals overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000159 CoalesceMap findCoalesces();
Evan Chengb1290a62008-10-02 18:29:27 +0000160
Lang Hames6699fb22009-08-06 23:32:48 +0000161 /// \brief Finds the initial set of vreg intervals to allocate.
Lang Hames27601ef2008-11-16 12:12:54 +0000162 void findVRegIntervalsToAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000163
Lang Hames6699fb22009-08-06 23:32:48 +0000164 /// \brief Constructs a PBQP problem representation of the register
165 /// allocation problem for this function.
166 ///
167 /// @return a PBQP solver object for the register allocation problem.
168 PBQP::SimpleGraph constructPBQPProblem();
Evan Chengb1290a62008-10-02 18:29:27 +0000169
Lang Hames6699fb22009-08-06 23:32:48 +0000170 /// \brief Adds a stack interval if the given live interval has been
171 /// spilled. Used to support stack slot coloring.
Evan Chengc781a242009-05-03 18:32:42 +0000172 void addStackInterval(const LiveInterval *spilled,MachineRegisterInfo* mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000173
Lang Hames6699fb22009-08-06 23:32:48 +0000174 /// \brief Given a solved PBQP problem maps this solution back to a register
175 /// assignment.
176 bool mapPBQPToRegAlloc(const PBQP::Solution &solution);
Evan Chengb1290a62008-10-02 18:29:27 +0000177
Lang Hames6699fb22009-08-06 23:32:48 +0000178 /// \brief Postprocessing before final spilling. Sets basic block "live in"
179 /// variables.
Lang Hames27601ef2008-11-16 12:12:54 +0000180 void finalizeAlloc() const;
181
Evan Chengb1290a62008-10-02 18:29:27 +0000182 };
183
184 char PBQPRegAlloc::ID = 0;
185}
186
187
Lang Hames27601ef2008-11-16 12:12:54 +0000188template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000189PBQP::Vector PBQPRegAlloc::buildCostVector(unsigned vReg,
190 const RegContainer &allowed,
191 const CoalesceMap &coalesces,
192 PBQP::PBQPNum spillCost) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000193
Lang Hames27601ef2008-11-16 12:12:54 +0000194 typedef typename RegContainer::const_iterator AllowedItr;
195
Evan Chengb1290a62008-10-02 18:29:27 +0000196 // Allocate vector. Additional element (0th) used for spill option
Lang Hames6699fb22009-08-06 23:32:48 +0000197 PBQP::Vector v(allowed.size() + 1, 0);
Evan Chengb1290a62008-10-02 18:29:27 +0000198
Lang Hames6699fb22009-08-06 23:32:48 +0000199 v[0] = spillCost;
Evan Chengb1290a62008-10-02 18:29:27 +0000200
Lang Hames27601ef2008-11-16 12:12:54 +0000201 // Iterate over the allowed registers inserting coalesce benefits if there
202 // are any.
203 unsigned ai = 0;
204 for (AllowedItr itr = allowed.begin(), end = allowed.end();
205 itr != end; ++itr, ++ai) {
206
207 unsigned pReg = *itr;
208
209 CoalesceMap::const_iterator cmItr =
210 coalesces.find(RegPair(vReg, pReg));
211
212 // No coalesce - on to the next preg.
213 if (cmItr == coalesces.end())
214 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000215
216 // We have a coalesce - insert the benefit.
Lang Hames6699fb22009-08-06 23:32:48 +0000217 v[ai + 1] = -cmItr->second;
Lang Hames27601ef2008-11-16 12:12:54 +0000218 }
219
Evan Chengb1290a62008-10-02 18:29:27 +0000220 return v;
221}
222
Lang Hames27601ef2008-11-16 12:12:54 +0000223template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000224PBQP::Matrix* PBQPRegAlloc::buildInterferenceMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000225 const RegContainer &allowed1, const RegContainer &allowed2) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000226
Lang Hames27601ef2008-11-16 12:12:54 +0000227 typedef typename RegContainer::const_iterator RegContainerIterator;
Evan Chengb1290a62008-10-02 18:29:27 +0000228
229 // Construct a PBQP matrix representing the cost of allocation options. The
230 // rows and columns correspond to the allocation options for the two live
231 // intervals. Elements will be infinite where corresponding registers alias,
232 // since we cannot allocate aliasing registers to interfering live intervals.
233 // All other elements (non-aliasing combinations) will have zero cost. Note
234 // that the spill option (element 0,0) has zero cost, since we can allocate
235 // both intervals to memory safely (the cost for each individual allocation
236 // to memory is accounted for by the cost vectors for each live interval).
Lang Hames6699fb22009-08-06 23:32:48 +0000237 PBQP::Matrix *m =
238 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Misha Brukman2a835f92009-01-08 15:50:22 +0000239
Evan Chengb1290a62008-10-02 18:29:27 +0000240 // Assume this is a zero matrix until proven otherwise. Zero matrices occur
241 // between interfering live ranges with non-overlapping register sets (e.g.
242 // non-overlapping reg classes, or disjoint sets of allowed regs within the
243 // same class). The term "overlapping" is used advisedly: sets which do not
244 // intersect, but contain registers which alias, will have non-zero matrices.
245 // We optimize zero matrices away to improve solver speed.
246 bool isZeroMatrix = true;
247
248
249 // Row index. Starts at 1, since the 0th row is for the spill option, which
250 // is always zero.
Misha Brukman2a835f92009-01-08 15:50:22 +0000251 unsigned ri = 1;
Evan Chengb1290a62008-10-02 18:29:27 +0000252
Misha Brukman2a835f92009-01-08 15:50:22 +0000253 // Iterate over allowed sets, insert infinities where required.
Lang Hames27601ef2008-11-16 12:12:54 +0000254 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000255 a1Itr != a1End; ++a1Itr) {
256
257 // Column index, starts at 1 as for row index.
258 unsigned ci = 1;
259 unsigned reg1 = *a1Itr;
260
Lang Hames27601ef2008-11-16 12:12:54 +0000261 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000262 a2Itr != a2End; ++a2Itr) {
263
264 unsigned reg2 = *a2Itr;
265
266 // If the row/column regs are identical or alias insert an infinity.
267 if ((reg1 == reg2) || tri->areAliases(reg1, reg2)) {
Lang Hames6699fb22009-08-06 23:32:48 +0000268 (*m)[ri][ci] = std::numeric_limits<PBQP::PBQPNum>::infinity();
Evan Chengb1290a62008-10-02 18:29:27 +0000269 isZeroMatrix = false;
270 }
271
272 ++ci;
273 }
274
275 ++ri;
276 }
277
278 // If this turns out to be a zero matrix...
279 if (isZeroMatrix) {
280 // free it and return null.
281 delete m;
282 return 0;
283 }
284
285 // ...otherwise return the cost matrix.
286 return m;
287}
288
Lang Hames27601ef2008-11-16 12:12:54 +0000289template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000290PBQP::Matrix* PBQPRegAlloc::buildCoalescingMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000291 const RegContainer &allowed1, const RegContainer &allowed2,
Lang Hames6699fb22009-08-06 23:32:48 +0000292 PBQP::PBQPNum cBenefit) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000293
Lang Hames27601ef2008-11-16 12:12:54 +0000294 typedef typename RegContainer::const_iterator RegContainerIterator;
295
296 // Construct a PBQP Matrix representing the benefits of coalescing. As with
297 // interference matrices the rows and columns represent allowed registers
298 // for the LiveIntervals which are (potentially) to be coalesced. The amount
299 // -cBenefit will be placed in any element representing the same register
300 // for both intervals.
Lang Hames6699fb22009-08-06 23:32:48 +0000301 PBQP::Matrix *m =
302 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Lang Hames27601ef2008-11-16 12:12:54 +0000303
304 // Reset costs to zero.
305 m->reset(0);
306
307 // Assume the matrix is zero till proven otherwise. Zero matrices will be
308 // optimized away as in the interference case.
309 bool isZeroMatrix = true;
310
311 // Row index. Starts at 1, since the 0th row is for the spill option, which
312 // is always zero.
313 unsigned ri = 1;
314
315 // Iterate over the allowed sets, insert coalescing benefits where
316 // appropriate.
317 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
318 a1Itr != a1End; ++a1Itr) {
319
320 // Column index, starts at 1 as for row index.
321 unsigned ci = 1;
322 unsigned reg1 = *a1Itr;
323
324 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
325 a2Itr != a2End; ++a2Itr) {
326
327 // If the row and column represent the same register insert a beneficial
328 // cost to preference this allocation - it would allow us to eliminate a
Misha Brukman2a835f92009-01-08 15:50:22 +0000329 // move instruction.
Lang Hames27601ef2008-11-16 12:12:54 +0000330 if (reg1 == *a2Itr) {
331 (*m)[ri][ci] = -cBenefit;
332 isZeroMatrix = false;
333 }
334
335 ++ci;
336 }
337
338 ++ri;
339 }
340
341 // If this turns out to be a zero matrix...
342 if (isZeroMatrix) {
343 // ...free it and return null.
344 delete m;
345 return 0;
346 }
347
348 return m;
349}
350
351PBQPRegAlloc::CoalesceMap PBQPRegAlloc::findCoalesces() {
352
353 typedef MachineFunction::const_iterator MFIterator;
354 typedef MachineBasicBlock::const_iterator MBBIterator;
355 typedef LiveInterval::const_vni_iterator VNIIterator;
Misha Brukman2a835f92009-01-08 15:50:22 +0000356
Lang Hames27601ef2008-11-16 12:12:54 +0000357 CoalesceMap coalescesFound;
358
359 // To find coalesces we need to iterate over the function looking for
360 // copy instructions.
361 for (MFIterator bbItr = mf->begin(), bbEnd = mf->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000362 bbItr != bbEnd; ++bbItr) {
363
364 const MachineBasicBlock *mbb = &*bbItr;
Evan Chengb1290a62008-10-02 18:29:27 +0000365
Lang Hames27601ef2008-11-16 12:12:54 +0000366 for (MBBIterator iItr = mbb->begin(), iEnd = mbb->end();
367 iItr != iEnd; ++iItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000368
369 const MachineInstr *instr = &*iItr;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000370 unsigned srcReg, dstReg, srcSubReg, dstSubReg;
Evan Chengb1290a62008-10-02 18:29:27 +0000371
Lang Hames27601ef2008-11-16 12:12:54 +0000372 // If this isn't a copy then continue to the next instruction.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000373 if (!tii->isMoveInstr(*instr, srcReg, dstReg, srcSubReg, dstSubReg))
Lang Hames27601ef2008-11-16 12:12:54 +0000374 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000375
Lang Hames27601ef2008-11-16 12:12:54 +0000376 // If the registers are already the same our job is nice and easy.
377 if (dstReg == srcReg)
378 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000379
Lang Hames27601ef2008-11-16 12:12:54 +0000380 bool srcRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(srcReg),
381 dstRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(dstReg);
382
383 // If both registers are physical then we can't coalesce.
384 if (srcRegIsPhysical && dstRegIsPhysical)
385 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000386
Lang Hames27601ef2008-11-16 12:12:54 +0000387 // If it's a copy that includes a virtual register but the source and
388 // destination classes differ then we can't coalesce, so continue with
389 // the next instruction.
390 const TargetRegisterClass *srcRegClass = srcRegIsPhysical ?
391 tri->getPhysicalRegisterRegClass(srcReg) : mri->getRegClass(srcReg);
392
393 const TargetRegisterClass *dstRegClass = dstRegIsPhysical ?
394 tri->getPhysicalRegisterRegClass(dstReg) : mri->getRegClass(dstReg);
395
396 if (srcRegClass != dstRegClass)
397 continue;
398
399 // We also need any physical regs to be allocable, coalescing with
400 // a non-allocable register is invalid.
401 if (srcRegIsPhysical) {
402 if (std::find(srcRegClass->allocation_order_begin(*mf),
403 srcRegClass->allocation_order_end(*mf), srcReg) ==
404 srcRegClass->allocation_order_end(*mf))
Evan Chengb1290a62008-10-02 18:29:27 +0000405 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000406 }
407
Lang Hames27601ef2008-11-16 12:12:54 +0000408 if (dstRegIsPhysical) {
409 if (std::find(dstRegClass->allocation_order_begin(*mf),
410 dstRegClass->allocation_order_end(*mf), dstReg) ==
411 dstRegClass->allocation_order_end(*mf))
412 continue;
413 }
414
415 // If we've made it here we have a copy with compatible register classes.
Misha Brukman2a835f92009-01-08 15:50:22 +0000416 // We can probably coalesce, but we need to consider overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000417 const LiveInterval *srcLI = &lis->getInterval(srcReg),
418 *dstLI = &lis->getInterval(dstReg);
419
420 if (srcLI->overlaps(*dstLI)) {
421 // Even in the case of an overlap we might still be able to coalesce,
422 // but we need to make sure that no definition of either range occurs
423 // while the other range is live.
424
425 // Otherwise start by assuming we're ok.
426 bool badDef = false;
427
428 // Test all defs of the source range.
Misha Brukman2a835f92009-01-08 15:50:22 +0000429 for (VNIIterator
Lang Hames27601ef2008-11-16 12:12:54 +0000430 vniItr = srcLI->vni_begin(), vniEnd = srcLI->vni_end();
431 vniItr != vniEnd; ++vniItr) {
432
433 // If we find a def that kills the coalescing opportunity then
434 // record it and break from the loop.
435 if (dstLI->liveAt((*vniItr)->def)) {
436 badDef = true;
437 break;
438 }
439 }
440
441 // If we have a bad def give up, continue to the next instruction.
442 if (badDef)
443 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000444
Lang Hames27601ef2008-11-16 12:12:54 +0000445 // Otherwise test definitions of the destination range.
446 for (VNIIterator
447 vniItr = dstLI->vni_begin(), vniEnd = dstLI->vni_end();
448 vniItr != vniEnd; ++vniItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000449
Lang Hames27601ef2008-11-16 12:12:54 +0000450 // We want to make sure we skip the copy instruction itself.
Lang Hames52c1afc2009-08-10 23:43:28 +0000451 if ((*vniItr)->getCopy() == instr)
Lang Hames27601ef2008-11-16 12:12:54 +0000452 continue;
453
454 if (srcLI->liveAt((*vniItr)->def)) {
455 badDef = true;
456 break;
457 }
458 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000459
Lang Hames27601ef2008-11-16 12:12:54 +0000460 // As before a bad def we give up and continue to the next instr.
461 if (badDef)
462 continue;
463 }
464
465 // If we make it to here then either the ranges didn't overlap, or they
466 // did, but none of their definitions would prevent us from coalescing.
467 // We're good to go with the coalesce.
468
469 float cBenefit = powf(10.0f, loopInfo->getLoopDepth(mbb)) / 5.0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000470
Lang Hames27601ef2008-11-16 12:12:54 +0000471 coalescesFound[RegPair(srcReg, dstReg)] = cBenefit;
472 coalescesFound[RegPair(dstReg, srcReg)] = cBenefit;
Evan Chengb1290a62008-10-02 18:29:27 +0000473 }
474
475 }
476
Lang Hames27601ef2008-11-16 12:12:54 +0000477 return coalescesFound;
478}
479
480void PBQPRegAlloc::findVRegIntervalsToAlloc() {
481
482 // Iterate over all live ranges.
483 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
484 itr != end; ++itr) {
485
486 // Ignore physical ones.
487 if (TargetRegisterInfo::isPhysicalRegister(itr->first))
488 continue;
489
490 LiveInterval *li = itr->second;
491
492 // If this live interval is non-empty we will use pbqp to allocate it.
493 // Empty intervals we allocate in a simple post-processing stage in
494 // finalizeAlloc.
495 if (!li->empty()) {
496 vregIntervalsToAlloc.insert(li);
497 }
498 else {
499 emptyVRegIntervals.insert(li);
500 }
501 }
Evan Chengb1290a62008-10-02 18:29:27 +0000502}
503
Lang Hames6699fb22009-08-06 23:32:48 +0000504PBQP::SimpleGraph PBQPRegAlloc::constructPBQPProblem() {
Evan Chengb1290a62008-10-02 18:29:27 +0000505
506 typedef std::vector<const LiveInterval*> LIVector;
Lang Hames27601ef2008-11-16 12:12:54 +0000507 typedef std::vector<unsigned> RegVector;
Lang Hames6699fb22009-08-06 23:32:48 +0000508 typedef std::vector<PBQP::SimpleGraph::NodeIterator> NodeVector;
Evan Chengb1290a62008-10-02 18:29:27 +0000509
Lang Hames27601ef2008-11-16 12:12:54 +0000510 // This will store the physical intervals for easy reference.
511 LIVector physIntervals;
Evan Chengb1290a62008-10-02 18:29:27 +0000512
513 // Start by clearing the old node <-> live interval mappings & allowed sets
514 li2Node.clear();
515 node2LI.clear();
516 allowedSets.clear();
517
Lang Hames27601ef2008-11-16 12:12:54 +0000518 // Populate physIntervals, update preg use:
519 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000520 itr != end; ++itr) {
521
Evan Chengb1290a62008-10-02 18:29:27 +0000522 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
523 physIntervals.push_back(itr->second);
524 mri->setPhysRegUsed(itr->second->reg);
525 }
Evan Chengb1290a62008-10-02 18:29:27 +0000526 }
527
Lang Hames27601ef2008-11-16 12:12:54 +0000528 // Iterate over vreg intervals, construct live interval <-> node number
529 // mappings.
Misha Brukman2a835f92009-01-08 15:50:22 +0000530 for (LiveIntervalSet::const_iterator
Lang Hames27601ef2008-11-16 12:12:54 +0000531 itr = vregIntervalsToAlloc.begin(), end = vregIntervalsToAlloc.end();
532 itr != end; ++itr) {
533 const LiveInterval *li = *itr;
534
535 li2Node[li] = node2LI.size();
536 node2LI.push_back(li);
537 }
538
539 // Get the set of potential coalesces.
Lang Hames6699fb22009-08-06 23:32:48 +0000540 CoalesceMap coalesces;//(findCoalesces());
Evan Chengb1290a62008-10-02 18:29:27 +0000541
542 // Construct a PBQP solver for this problem
Lang Hames6699fb22009-08-06 23:32:48 +0000543 PBQP::SimpleGraph problem;
544 NodeVector problemNodes(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000545
546 // Resize allowedSets container appropriately.
Lang Hames27601ef2008-11-16 12:12:54 +0000547 allowedSets.resize(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000548
549 // Iterate over virtual register intervals to compute allowed sets...
550 for (unsigned node = 0; node < node2LI.size(); ++node) {
551
552 // Grab pointers to the interval and its register class.
553 const LiveInterval *li = node2LI[node];
554 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000555
Evan Chengb1290a62008-10-02 18:29:27 +0000556 // Start by assuming all allocable registers in the class are allowed...
Lang Hames27601ef2008-11-16 12:12:54 +0000557 RegVector liAllowed(liRC->allocation_order_begin(*mf),
558 liRC->allocation_order_end(*mf));
Evan Chengb1290a62008-10-02 18:29:27 +0000559
Lang Hames27601ef2008-11-16 12:12:54 +0000560 // Eliminate the physical registers which overlap with this range, along
561 // with all their aliases.
562 for (LIVector::iterator pItr = physIntervals.begin(),
563 pEnd = physIntervals.end(); pItr != pEnd; ++pItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000564
Lang Hames27601ef2008-11-16 12:12:54 +0000565 if (!li->overlaps(**pItr))
566 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000567
Lang Hames27601ef2008-11-16 12:12:54 +0000568 unsigned pReg = (*pItr)->reg;
Evan Chengb1290a62008-10-02 18:29:27 +0000569
Lang Hames27601ef2008-11-16 12:12:54 +0000570 // If we get here then the live intervals overlap, but we're still ok
571 // if they're coalescable.
572 if (coalesces.find(RegPair(li->reg, pReg)) != coalesces.end())
573 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000574
Lang Hames27601ef2008-11-16 12:12:54 +0000575 // If we get here then we have a genuine exclusion.
Evan Chengb1290a62008-10-02 18:29:27 +0000576
Lang Hames27601ef2008-11-16 12:12:54 +0000577 // Remove the overlapping reg...
578 RegVector::iterator eraseItr =
579 std::find(liAllowed.begin(), liAllowed.end(), pReg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000580
Lang Hames27601ef2008-11-16 12:12:54 +0000581 if (eraseItr != liAllowed.end())
582 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000583
Lang Hames27601ef2008-11-16 12:12:54 +0000584 const unsigned *aliasItr = tri->getAliasSet(pReg);
585
586 if (aliasItr != 0) {
587 // ...and its aliases.
588 for (; *aliasItr != 0; ++aliasItr) {
589 RegVector::iterator eraseItr =
590 std::find(liAllowed.begin(), liAllowed.end(), *aliasItr);
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 }
Evan Chengb1290a62008-10-02 18:29:27 +0000595 }
Evan Chengb1290a62008-10-02 18:29:27 +0000596 }
Evan Chengb1290a62008-10-02 18:29:27 +0000597 }
598
599 // Copy the allowed set into a member vector for use when constructing cost
600 // vectors & matrices, and mapping PBQP solutions back to assignments.
601 allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end());
602
603 // Set the spill cost to the interval weight, or epsilon if the
604 // interval weight is zero
Lang Hames6699fb22009-08-06 23:32:48 +0000605 PBQP::PBQPNum spillCost = (li->weight != 0.0) ?
606 li->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000607
608 // Build a cost vector for this interval.
Lang Hames6699fb22009-08-06 23:32:48 +0000609 problemNodes[node] =
610 problem.addNode(
611 buildCostVector(li->reg, allowedSets[node], coalesces, spillCost));
Evan Chengb1290a62008-10-02 18:29:27 +0000612
613 }
614
Lang Hames27601ef2008-11-16 12:12:54 +0000615
Evan Chengb1290a62008-10-02 18:29:27 +0000616 // Now add the cost matrices...
617 for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) {
Evan Chengb1290a62008-10-02 18:29:27 +0000618 const LiveInterval *li = node2LI[node1];
619
Evan Chengb1290a62008-10-02 18:29:27 +0000620 // Test for live range overlaps and insert interference matrices.
621 for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) {
622 const LiveInterval *li2 = node2LI[node2];
623
Lang Hames27601ef2008-11-16 12:12:54 +0000624 CoalesceMap::const_iterator cmItr =
625 coalesces.find(RegPair(li->reg, li2->reg));
Evan Chengb1290a62008-10-02 18:29:27 +0000626
Lang Hames6699fb22009-08-06 23:32:48 +0000627 PBQP::Matrix *m = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000628
Lang Hames27601ef2008-11-16 12:12:54 +0000629 if (cmItr != coalesces.end()) {
630 m = buildCoalescingMatrix(allowedSets[node1], allowedSets[node2],
631 cmItr->second);
632 }
633 else if (li->overlaps(*li2)) {
634 m = buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]);
635 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000636
Lang Hames27601ef2008-11-16 12:12:54 +0000637 if (m != 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000638 problem.addEdge(problemNodes[node1],
639 problemNodes[node2],
640 *m);
641
Lang Hames27601ef2008-11-16 12:12:54 +0000642 delete m;
Evan Chengb1290a62008-10-02 18:29:27 +0000643 }
644 }
645 }
646
Lang Hames6699fb22009-08-06 23:32:48 +0000647 problem.assignNodeIDs();
648
649 assert(problem.getNumNodes() == allowedSets.size());
650 for (unsigned i = 0; i < allowedSets.size(); ++i) {
651 assert(problem.getNodeItr(i) == problemNodes[i]);
652 }
653/*
654 std::cerr << "Allocating for " << problem.getNumNodes() << " nodes, "
655 << problem.getNumEdges() << " edges.\n";
656
657 problem.printDot(std::cerr);
658*/
Evan Chengb1290a62008-10-02 18:29:27 +0000659 // We're done, PBQP problem constructed - return it.
Lang Hames6699fb22009-08-06 23:32:48 +0000660 return problem;
Evan Chengb1290a62008-10-02 18:29:27 +0000661}
662
Evan Chengc781a242009-05-03 18:32:42 +0000663void PBQPRegAlloc::addStackInterval(const LiveInterval *spilled,
664 MachineRegisterInfo* mri) {
Lang Hames27601ef2008-11-16 12:12:54 +0000665 int stackSlot = vrm->getStackSlot(spilled->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000666
667 if (stackSlot == VirtRegMap::NO_STACK_SLOT)
Lang Hames27601ef2008-11-16 12:12:54 +0000668 return;
669
Evan Chengc781a242009-05-03 18:32:42 +0000670 const TargetRegisterClass *RC = mri->getRegClass(spilled->reg);
671 LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC);
Lang Hames27601ef2008-11-16 12:12:54 +0000672
673 VNInfo *vni;
674 if (stackInterval.getNumValNums() != 0)
675 vni = stackInterval.getValNumInfo(0);
676 else
Lang Hames857c4e02009-06-17 21:01:20 +0000677 vni = stackInterval.getNextValue(0, 0, false, lss->getVNInfoAllocator());
Lang Hames27601ef2008-11-16 12:12:54 +0000678
679 LiveInterval &rhsInterval = lis->getInterval(spilled->reg);
680 stackInterval.MergeRangesInAsValue(rhsInterval, vni);
681}
682
Lang Hames6699fb22009-08-06 23:32:48 +0000683bool PBQPRegAlloc::mapPBQPToRegAlloc(const PBQP::Solution &solution) {
684
685 static unsigned round = 0;
Daniel Dunbar8a1871d2009-08-08 00:40:46 +0000686 (void) round;
Misha Brukman2a835f92009-01-08 15:50:22 +0000687
Evan Chengb1290a62008-10-02 18:29:27 +0000688 // Set to true if we have any spills
689 bool anotherRoundNeeded = false;
690
691 // Clear the existing allocation.
692 vrm->clearAllVirt();
Lang Hames52c1afc2009-08-10 23:43:28 +0000693
Evan Chengb1290a62008-10-02 18:29:27 +0000694 // Iterate over the nodes mapping the PBQP solution to a register assignment.
695 for (unsigned node = 0; node < node2LI.size(); ++node) {
Lang Hames27601ef2008-11-16 12:12:54 +0000696 unsigned virtReg = node2LI[node]->reg,
Lang Hames6699fb22009-08-06 23:32:48 +0000697 allocSelection = solution.getSelection(node);
698
Evan Chengb1290a62008-10-02 18:29:27 +0000699
700 // If the PBQP solution is non-zero it's a physical register...
701 if (allocSelection != 0) {
702 // Get the physical reg, subtracting 1 to account for the spill option.
703 unsigned physReg = allowedSets[node][allocSelection - 1];
704
Lang Hames27601ef2008-11-16 12:12:54 +0000705 DOUT << "VREG " << virtReg << " -> " << tri->getName(physReg) << "\n";
706
707 assert(physReg != 0);
708
Evan Chengb1290a62008-10-02 18:29:27 +0000709 // Add to the virt reg map and update the used phys regs.
Lang Hames27601ef2008-11-16 12:12:54 +0000710 vrm->assignVirt2Phys(virtReg, physReg);
Evan Chengb1290a62008-10-02 18:29:27 +0000711 }
712 // ...Otherwise it's a spill.
713 else {
714
715 // Make sure we ignore this virtual reg on the next round
716 // of allocation
Lang Hames27601ef2008-11-16 12:12:54 +0000717 vregIntervalsToAlloc.erase(&lis->getInterval(virtReg));
Evan Chengb1290a62008-10-02 18:29:27 +0000718
Evan Chengb1290a62008-10-02 18:29:27 +0000719 // Insert spill ranges for this live range
Lang Hames27601ef2008-11-16 12:12:54 +0000720 const LiveInterval *spillInterval = node2LI[node];
721 double oldSpillWeight = spillInterval->weight;
Evan Chengb1290a62008-10-02 18:29:27 +0000722 SmallVector<LiveInterval*, 8> spillIs;
723 std::vector<LiveInterval*> newSpills =
Evan Chengc781a242009-05-03 18:32:42 +0000724 lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm);
725 addStackInterval(spillInterval, mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000726
727 DOUT << "VREG " << virtReg << " -> SPILLED (Cost: "
728 << oldSpillWeight << ", New vregs: ";
729
730 // Copy any newly inserted live intervals into the list of regs to
731 // allocate.
732 for (std::vector<LiveInterval*>::const_iterator
733 itr = newSpills.begin(), end = newSpills.end();
734 itr != end; ++itr) {
735
736 assert(!(*itr)->empty() && "Empty spill range.");
737
738 DOUT << (*itr)->reg << " ";
739
740 vregIntervalsToAlloc.insert(*itr);
741 }
742
743 DOUT << ")\n";
Evan Chengb1290a62008-10-02 18:29:27 +0000744
745 // We need another round if spill intervals were added.
746 anotherRoundNeeded |= !newSpills.empty();
747 }
748 }
749
750 return !anotherRoundNeeded;
751}
752
Lang Hames27601ef2008-11-16 12:12:54 +0000753void PBQPRegAlloc::finalizeAlloc() const {
754 typedef LiveIntervals::iterator LIIterator;
755 typedef LiveInterval::Ranges::const_iterator LRIterator;
756
757 // First allocate registers for the empty intervals.
Argyrios Kyrtzidis3713c0b2008-11-19 12:56:21 +0000758 for (LiveIntervalSet::const_iterator
Lang Hames6699fb22009-08-06 23:32:48 +0000759 itr = emptyVRegIntervals.begin(), end = emptyVRegIntervals.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000760 itr != end; ++itr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000761 LiveInterval *li = *itr;
Lang Hames27601ef2008-11-16 12:12:54 +0000762
Evan Cheng90f95f82009-06-14 20:22:55 +0000763 unsigned physReg = vrm->getRegAllocPref(li->reg);
Lang Hames6699fb22009-08-06 23:32:48 +0000764
Lang Hames27601ef2008-11-16 12:12:54 +0000765 if (physReg == 0) {
766 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000767 physReg = *liRC->allocation_order_begin(*mf);
Lang Hames27601ef2008-11-16 12:12:54 +0000768 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000769
770 vrm->assignVirt2Phys(li->reg, physReg);
Lang Hames27601ef2008-11-16 12:12:54 +0000771 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000772
Lang Hames27601ef2008-11-16 12:12:54 +0000773 // Finally iterate over the basic blocks to compute and set the live-in sets.
774 SmallVector<MachineBasicBlock*, 8> liveInMBBs;
775 MachineBasicBlock *entryMBB = &*mf->begin();
776
777 for (LIIterator liItr = lis->begin(), liEnd = lis->end();
778 liItr != liEnd; ++liItr) {
779
780 const LiveInterval *li = liItr->second;
781 unsigned reg = 0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000782
Lang Hames27601ef2008-11-16 12:12:54 +0000783 // Get the physical register for this interval
784 if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
785 reg = li->reg;
786 }
787 else if (vrm->isAssignedReg(li->reg)) {
788 reg = vrm->getPhys(li->reg);
789 }
790 else {
791 // Ranges which are assigned a stack slot only are ignored.
792 continue;
793 }
794
Lang Hamesb0e519f2009-05-17 23:50:36 +0000795 if (reg == 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000796 // Filter out zero regs - they're for intervals that were spilled.
Lang Hamesb0e519f2009-05-17 23:50:36 +0000797 continue;
798 }
799
Lang Hames27601ef2008-11-16 12:12:54 +0000800 // Iterate over the ranges of the current interval...
801 for (LRIterator lrItr = li->begin(), lrEnd = li->end();
802 lrItr != lrEnd; ++lrItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000803
Lang Hames27601ef2008-11-16 12:12:54 +0000804 // Find the set of basic blocks which this range is live into...
805 if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
806 // And add the physreg for this interval to their live-in sets.
807 for (unsigned i = 0; i < liveInMBBs.size(); ++i) {
808 if (liveInMBBs[i] != entryMBB) {
809 if (!liveInMBBs[i]->isLiveIn(reg)) {
810 liveInMBBs[i]->addLiveIn(reg);
811 }
812 }
813 }
814 liveInMBBs.clear();
815 }
816 }
817 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000818
Lang Hames27601ef2008-11-16 12:12:54 +0000819}
820
Evan Chengb1290a62008-10-02 18:29:27 +0000821bool PBQPRegAlloc::runOnMachineFunction(MachineFunction &MF) {
Lang Hames27601ef2008-11-16 12:12:54 +0000822
Evan Chengb1290a62008-10-02 18:29:27 +0000823 mf = &MF;
824 tm = &mf->getTarget();
825 tri = tm->getRegisterInfo();
Lang Hames27601ef2008-11-16 12:12:54 +0000826 tii = tm->getInstrInfo();
Evan Chengb1290a62008-10-02 18:29:27 +0000827 mri = &mf->getRegInfo();
828
Lang Hames27601ef2008-11-16 12:12:54 +0000829 lis = &getAnalysis<LiveIntervals>();
830 lss = &getAnalysis<LiveStacks>();
Evan Chengb1290a62008-10-02 18:29:27 +0000831 loopInfo = &getAnalysis<MachineLoopInfo>();
832
Owen Anderson49c8aa02009-03-13 05:55:11 +0000833 vrm = &getAnalysis<VirtRegMap>();
Evan Chengb1290a62008-10-02 18:29:27 +0000834
Lang Hames6699fb22009-08-06 23:32:48 +0000835 DEBUG(errs() << "PBQP2 Register Allocating for " << mf->getFunction()->getName() << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000836
Evan Chengb1290a62008-10-02 18:29:27 +0000837 // Allocator main loop:
Misha Brukman2a835f92009-01-08 15:50:22 +0000838 //
Evan Chengb1290a62008-10-02 18:29:27 +0000839 // * Map current regalloc problem to a PBQP problem
840 // * Solve the PBQP problem
841 // * Map the solution back to a register allocation
842 // * Spill if necessary
Misha Brukman2a835f92009-01-08 15:50:22 +0000843 //
Evan Chengb1290a62008-10-02 18:29:27 +0000844 // This process is continued till no more spills are generated.
845
Lang Hames27601ef2008-11-16 12:12:54 +0000846 // Find the vreg intervals in need of allocation.
847 findVRegIntervalsToAlloc();
Misha Brukman2a835f92009-01-08 15:50:22 +0000848
Lang Hames27601ef2008-11-16 12:12:54 +0000849 // If there aren't any then we're done here.
850 if (vregIntervalsToAlloc.empty() && emptyVRegIntervals.empty())
851 return true;
Evan Chengb1290a62008-10-02 18:29:27 +0000852
Lang Hames27601ef2008-11-16 12:12:54 +0000853 // If there are non-empty intervals allocate them using pbqp.
854 if (!vregIntervalsToAlloc.empty()) {
Evan Chengb1290a62008-10-02 18:29:27 +0000855
Lang Hames27601ef2008-11-16 12:12:54 +0000856 bool pbqpAllocComplete = false;
857 unsigned round = 0;
858
859 while (!pbqpAllocComplete) {
Lang Hames6699fb22009-08-06 23:32:48 +0000860 DEBUG(errs() << " PBQP Regalloc round " << round << ":\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000861
Lang Hames6699fb22009-08-06 23:32:48 +0000862 PBQP::SimpleGraph problem = constructPBQPProblem();
863 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs> solver;
864 problem.assignNodeIDs();
865 PBQP::Solution solution = solver.solve(problem);
866/*
867 std::cerr << "Solution:\n";
868 for (unsigned i = 0; i < solution.numNodes(); ++i) {
869 std::cerr << " " << i << " -> " << solution.getSelection(i) << "\n";
870 }
871*/
872 pbqpAllocComplete = mapPBQPToRegAlloc(solution);
Lang Hames27601ef2008-11-16 12:12:54 +0000873
874 ++round;
875 }
Evan Chengb1290a62008-10-02 18:29:27 +0000876 }
877
Lang Hames27601ef2008-11-16 12:12:54 +0000878 // Finalise allocation, allocate empty ranges.
879 finalizeAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000880
Lang Hames27601ef2008-11-16 12:12:54 +0000881 vregIntervalsToAlloc.clear();
882 emptyVRegIntervals.clear();
883 li2Node.clear();
884 node2LI.clear();
885 allowedSets.clear();
886
Lang Hames6699fb22009-08-06 23:32:48 +0000887 DEBUG(errs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000888
Lang Hames87e3bca2009-05-06 02:36:21 +0000889 // Run rewriter
890 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
891
892 rewriter->runOnMachineFunction(*mf, *vrm, lis);
Lang Hames27601ef2008-11-16 12:12:54 +0000893
Misha Brukman2a835f92009-01-08 15:50:22 +0000894 return true;
Evan Chengb1290a62008-10-02 18:29:27 +0000895}
896
897FunctionPass* llvm::createPBQPRegisterAllocator() {
898 return new PBQPRegAlloc();
899}
900
901
902#undef DEBUG_TYPE