blob: 45104b48fe423ab6f1a6b8623016639702dc775d [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"
Lang Hames030c4bf2010-01-26 04:49:58 +000035#include "PBQP/Graph.h"
Lang Hames6699fb22009-08-06 23:32:48 +000036#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"
Lang Hamesa937f222009-12-14 06:49:42 +000039#include "llvm/CodeGen/CalcSpillWeights.h"
Evan Chengb1290a62008-10-02 18:29:27 +000040#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Lang Hames27601ef2008-11-16 12:12:54 +000041#include "llvm/CodeGen/LiveStackAnalysis.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000042#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chengb1290a62008-10-02 18:29:27 +000043#include "llvm/CodeGen/MachineLoopInfo.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000044#include "llvm/CodeGen/MachineRegisterInfo.h"
45#include "llvm/CodeGen/RegAllocRegistry.h"
46#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengb1290a62008-10-02 18:29:27 +000047#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000048#include "llvm/Support/raw_ostream.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000049#include "llvm/Target/TargetInstrInfo.h"
50#include "llvm/Target/TargetMachine.h"
51#include <limits>
Evan Chengb1290a62008-10-02 18:29:27 +000052#include <map>
Misha Brukman2a835f92009-01-08 15:50:22 +000053#include <memory>
Evan Chengb1290a62008-10-02 18:29:27 +000054#include <set>
55#include <vector>
Evan Chengb1290a62008-10-02 18:29:27 +000056
57using namespace llvm;
58
59static RegisterRegAlloc
Duncan Sands1aecd152010-02-18 14:10:41 +000060registerPBQPRepAlloc("pbqp", "PBQP register allocator",
Lang Hames030c4bf2010-01-26 04:49:58 +000061 llvm::createPBQPRegisterAllocator);
Evan Chengb1290a62008-10-02 18:29:27 +000062
Lang Hames8481e3b2009-08-19 01:36:14 +000063static cl::opt<bool>
64pbqpCoalescing("pbqp-coalescing",
Lang Hames030c4bf2010-01-26 04:49:58 +000065 cl::desc("Attempt coalescing during PBQP register allocation."),
66 cl::init(false), cl::Hidden);
Lang Hames8481e3b2009-08-19 01:36:14 +000067
Evan Chengb1290a62008-10-02 18:29:27 +000068namespace {
69
Lang Hames6699fb22009-08-06 23:32:48 +000070 ///
71 /// PBQP based allocators solve the register allocation problem by mapping
72 /// register allocation problems to Partitioned Boolean Quadratic
73 /// Programming problems.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000074 class PBQPRegAlloc : public MachineFunctionPass {
Evan Chengb1290a62008-10-02 18:29:27 +000075 public:
76
77 static char ID;
Daniel Dunbara279bc32009-09-20 02:20:51 +000078
Lang Hames6699fb22009-08-06 23:32:48 +000079 /// Construct a PBQP register allocator.
Dan Gohman1b2d0b82009-08-11 15:15:10 +000080 PBQPRegAlloc() : MachineFunctionPass(&ID) {}
Evan Chengb1290a62008-10-02 18:29:27 +000081
Lang Hames6699fb22009-08-06 23:32:48 +000082 /// Return the pass name.
Dan Gohman00b0a242009-08-11 15:35:57 +000083 virtual const char* getPassName() const {
Evan Chengb1290a62008-10-02 18:29:27 +000084 return "PBQP Register Allocator";
85 }
86
Lang Hames6699fb22009-08-06 23:32:48 +000087 /// PBQP analysis usage.
88 virtual void getAnalysisUsage(AnalysisUsage &au) const {
Lang Hames233a60e2009-11-03 23:52:08 +000089 au.addRequired<SlotIndexes>();
90 au.addPreserved<SlotIndexes>();
Lang Hames6699fb22009-08-06 23:32:48 +000091 au.addRequired<LiveIntervals>();
92 //au.addRequiredID(SplitCriticalEdgesID);
Lang Hamesf7c553e2009-08-12 21:04:53 +000093 au.addRequired<RegisterCoalescer>();
Lang Hamesa937f222009-12-14 06:49:42 +000094 au.addRequired<CalculateSpillWeights>();
Lang Hames6699fb22009-08-06 23:32:48 +000095 au.addRequired<LiveStacks>();
96 au.addPreserved<LiveStacks>();
97 au.addRequired<MachineLoopInfo>();
98 au.addPreserved<MachineLoopInfo>();
99 au.addRequired<VirtRegMap>();
100 MachineFunctionPass::getAnalysisUsage(au);
Evan Chengb1290a62008-10-02 18:29:27 +0000101 }
102
Lang Hames6699fb22009-08-06 23:32:48 +0000103 /// Perform register allocation
Evan Chengb1290a62008-10-02 18:29:27 +0000104 virtual bool runOnMachineFunction(MachineFunction &MF);
105
106 private:
107 typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
108 typedef std::vector<const LiveInterval*> Node2LIMap;
109 typedef std::vector<unsigned> AllowedSet;
110 typedef std::vector<AllowedSet> AllowedSetMap;
Lang Hames27601ef2008-11-16 12:12:54 +0000111 typedef std::set<unsigned> RegSet;
112 typedef std::pair<unsigned, unsigned> RegPair;
Lang Hames6699fb22009-08-06 23:32:48 +0000113 typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
Lang Hames27601ef2008-11-16 12:12:54 +0000114
115 typedef std::set<LiveInterval*> LiveIntervalSet;
Evan Chengb1290a62008-10-02 18:29:27 +0000116
Lang Hames030c4bf2010-01-26 04:49:58 +0000117 typedef std::vector<PBQP::Graph::NodeItr> NodeVector;
118
Evan Chengb1290a62008-10-02 18:29:27 +0000119 MachineFunction *mf;
120 const TargetMachine *tm;
121 const TargetRegisterInfo *tri;
122 const TargetInstrInfo *tii;
123 const MachineLoopInfo *loopInfo;
124 MachineRegisterInfo *mri;
125
Lang Hames27601ef2008-11-16 12:12:54 +0000126 LiveIntervals *lis;
127 LiveStacks *lss;
Evan Chengb1290a62008-10-02 18:29:27 +0000128 VirtRegMap *vrm;
129
130 LI2NodeMap li2Node;
131 Node2LIMap node2LI;
132 AllowedSetMap allowedSets;
Lang Hames27601ef2008-11-16 12:12:54 +0000133 LiveIntervalSet vregIntervalsToAlloc,
134 emptyVRegIntervals;
Lang Hames030c4bf2010-01-26 04:49:58 +0000135 NodeVector problemNodes;
Evan Chengb1290a62008-10-02 18:29:27 +0000136
Misha Brukman2a835f92009-01-08 15:50:22 +0000137
Lang Hames6699fb22009-08-06 23:32:48 +0000138 /// Builds a PBQP cost vector.
Lang Hames27601ef2008-11-16 12:12:54 +0000139 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000140 PBQP::Vector buildCostVector(unsigned vReg,
141 const RegContainer &allowed,
142 const CoalesceMap &cealesces,
143 PBQP::PBQPNum spillCost) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000144
Lang Hames6699fb22009-08-06 23:32:48 +0000145 /// \brief Builds a PBQP interference matrix.
146 ///
147 /// @return Either a pointer to a non-zero PBQP matrix representing the
148 /// allocation option costs, or a null pointer for a zero matrix.
149 ///
150 /// Expects allowed sets for two interfering LiveIntervals. These allowed
151 /// sets should contain only allocable registers from the LiveInterval's
152 /// register class, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000153 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000154 PBQP::Matrix* buildInterferenceMatrix(const RegContainer &allowed1,
155 const RegContainer &allowed2) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000156
Lang Hames6699fb22009-08-06 23:32:48 +0000157 ///
158 /// Expects allowed sets for two potentially coalescable LiveIntervals,
159 /// and an estimated benefit due to coalescing. The allowed sets should
160 /// contain only allocable registers from the LiveInterval's register
161 /// classes, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000162 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000163 PBQP::Matrix* buildCoalescingMatrix(const RegContainer &allowed1,
164 const RegContainer &allowed2,
165 PBQP::PBQPNum cBenefit) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000166
Lang Hames6699fb22009-08-06 23:32:48 +0000167 /// \brief Finds coalescing opportunities and returns them as a map.
168 ///
169 /// Any entries in the map are guaranteed coalescable, even if their
170 /// corresponding live intervals overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000171 CoalesceMap findCoalesces();
Evan Chengb1290a62008-10-02 18:29:27 +0000172
Lang Hames6699fb22009-08-06 23:32:48 +0000173 /// \brief Finds the initial set of vreg intervals to allocate.
Lang Hames27601ef2008-11-16 12:12:54 +0000174 void findVRegIntervalsToAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000175
Lang Hames6699fb22009-08-06 23:32:48 +0000176 /// \brief Constructs a PBQP problem representation of the register
177 /// allocation problem for this function.
178 ///
179 /// @return a PBQP solver object for the register allocation problem.
Lang Hames030c4bf2010-01-26 04:49:58 +0000180 PBQP::Graph constructPBQPProblem();
Evan Chengb1290a62008-10-02 18:29:27 +0000181
Lang Hames6699fb22009-08-06 23:32:48 +0000182 /// \brief Adds a stack interval if the given live interval has been
183 /// spilled. Used to support stack slot coloring.
Evan Chengc781a242009-05-03 18:32:42 +0000184 void addStackInterval(const LiveInterval *spilled,MachineRegisterInfo* mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000185
Lang Hames6699fb22009-08-06 23:32:48 +0000186 /// \brief Given a solved PBQP problem maps this solution back to a register
187 /// assignment.
188 bool mapPBQPToRegAlloc(const PBQP::Solution &solution);
Evan Chengb1290a62008-10-02 18:29:27 +0000189
Lang Hames6699fb22009-08-06 23:32:48 +0000190 /// \brief Postprocessing before final spilling. Sets basic block "live in"
191 /// variables.
Lang Hames27601ef2008-11-16 12:12:54 +0000192 void finalizeAlloc() const;
193
Evan Chengb1290a62008-10-02 18:29:27 +0000194 };
195
196 char PBQPRegAlloc::ID = 0;
197}
198
199
Lang Hames27601ef2008-11-16 12:12:54 +0000200template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000201PBQP::Vector PBQPRegAlloc::buildCostVector(unsigned vReg,
202 const RegContainer &allowed,
203 const CoalesceMap &coalesces,
204 PBQP::PBQPNum spillCost) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000205
Lang Hames27601ef2008-11-16 12:12:54 +0000206 typedef typename RegContainer::const_iterator AllowedItr;
207
Evan Chengb1290a62008-10-02 18:29:27 +0000208 // Allocate vector. Additional element (0th) used for spill option
Lang Hames6699fb22009-08-06 23:32:48 +0000209 PBQP::Vector v(allowed.size() + 1, 0);
Evan Chengb1290a62008-10-02 18:29:27 +0000210
Lang Hames6699fb22009-08-06 23:32:48 +0000211 v[0] = spillCost;
Evan Chengb1290a62008-10-02 18:29:27 +0000212
Lang Hames27601ef2008-11-16 12:12:54 +0000213 // Iterate over the allowed registers inserting coalesce benefits if there
214 // are any.
215 unsigned ai = 0;
216 for (AllowedItr itr = allowed.begin(), end = allowed.end();
217 itr != end; ++itr, ++ai) {
218
219 unsigned pReg = *itr;
220
221 CoalesceMap::const_iterator cmItr =
222 coalesces.find(RegPair(vReg, pReg));
223
224 // No coalesce - on to the next preg.
225 if (cmItr == coalesces.end())
226 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000227
228 // We have a coalesce - insert the benefit.
Lang Hames6699fb22009-08-06 23:32:48 +0000229 v[ai + 1] = -cmItr->second;
Lang Hames27601ef2008-11-16 12:12:54 +0000230 }
231
Evan Chengb1290a62008-10-02 18:29:27 +0000232 return v;
233}
234
Lang Hames27601ef2008-11-16 12:12:54 +0000235template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000236PBQP::Matrix* PBQPRegAlloc::buildInterferenceMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000237 const RegContainer &allowed1, const RegContainer &allowed2) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000238
Lang Hames27601ef2008-11-16 12:12:54 +0000239 typedef typename RegContainer::const_iterator RegContainerIterator;
Evan Chengb1290a62008-10-02 18:29:27 +0000240
241 // Construct a PBQP matrix representing the cost of allocation options. The
242 // rows and columns correspond to the allocation options for the two live
243 // intervals. Elements will be infinite where corresponding registers alias,
244 // since we cannot allocate aliasing registers to interfering live intervals.
245 // All other elements (non-aliasing combinations) will have zero cost. Note
246 // that the spill option (element 0,0) has zero cost, since we can allocate
247 // both intervals to memory safely (the cost for each individual allocation
248 // to memory is accounted for by the cost vectors for each live interval).
Lang Hames6699fb22009-08-06 23:32:48 +0000249 PBQP::Matrix *m =
250 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Misha Brukman2a835f92009-01-08 15:50:22 +0000251
Evan Chengb1290a62008-10-02 18:29:27 +0000252 // Assume this is a zero matrix until proven otherwise. Zero matrices occur
253 // between interfering live ranges with non-overlapping register sets (e.g.
254 // non-overlapping reg classes, or disjoint sets of allowed regs within the
255 // same class). The term "overlapping" is used advisedly: sets which do not
256 // intersect, but contain registers which alias, will have non-zero matrices.
257 // We optimize zero matrices away to improve solver speed.
258 bool isZeroMatrix = true;
259
260
261 // Row index. Starts at 1, since the 0th row is for the spill option, which
262 // is always zero.
Misha Brukman2a835f92009-01-08 15:50:22 +0000263 unsigned ri = 1;
Evan Chengb1290a62008-10-02 18:29:27 +0000264
Misha Brukman2a835f92009-01-08 15:50:22 +0000265 // Iterate over allowed sets, insert infinities where required.
Lang Hames27601ef2008-11-16 12:12:54 +0000266 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000267 a1Itr != a1End; ++a1Itr) {
268
269 // Column index, starts at 1 as for row index.
270 unsigned ci = 1;
271 unsigned reg1 = *a1Itr;
272
Lang Hames27601ef2008-11-16 12:12:54 +0000273 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000274 a2Itr != a2End; ++a2Itr) {
275
276 unsigned reg2 = *a2Itr;
277
278 // If the row/column regs are identical or alias insert an infinity.
Lang Hames3f2f3f52009-09-03 02:52:02 +0000279 if (tri->regsOverlap(reg1, reg2)) {
Lang Hames6699fb22009-08-06 23:32:48 +0000280 (*m)[ri][ci] = std::numeric_limits<PBQP::PBQPNum>::infinity();
Evan Chengb1290a62008-10-02 18:29:27 +0000281 isZeroMatrix = false;
282 }
283
284 ++ci;
285 }
286
287 ++ri;
288 }
289
290 // If this turns out to be a zero matrix...
291 if (isZeroMatrix) {
292 // free it and return null.
293 delete m;
294 return 0;
295 }
296
297 // ...otherwise return the cost matrix.
298 return m;
299}
300
Lang Hames27601ef2008-11-16 12:12:54 +0000301template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000302PBQP::Matrix* PBQPRegAlloc::buildCoalescingMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000303 const RegContainer &allowed1, const RegContainer &allowed2,
Lang Hames6699fb22009-08-06 23:32:48 +0000304 PBQP::PBQPNum cBenefit) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000305
Lang Hames27601ef2008-11-16 12:12:54 +0000306 typedef typename RegContainer::const_iterator RegContainerIterator;
307
308 // Construct a PBQP Matrix representing the benefits of coalescing. As with
309 // interference matrices the rows and columns represent allowed registers
310 // for the LiveIntervals which are (potentially) to be coalesced. The amount
311 // -cBenefit will be placed in any element representing the same register
312 // for both intervals.
Lang Hames6699fb22009-08-06 23:32:48 +0000313 PBQP::Matrix *m =
314 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Lang Hames27601ef2008-11-16 12:12:54 +0000315
316 // Reset costs to zero.
317 m->reset(0);
318
319 // Assume the matrix is zero till proven otherwise. Zero matrices will be
320 // optimized away as in the interference case.
321 bool isZeroMatrix = true;
322
323 // Row index. Starts at 1, since the 0th row is for the spill option, which
324 // is always zero.
325 unsigned ri = 1;
326
327 // Iterate over the allowed sets, insert coalescing benefits where
328 // appropriate.
329 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
330 a1Itr != a1End; ++a1Itr) {
331
332 // Column index, starts at 1 as for row index.
333 unsigned ci = 1;
334 unsigned reg1 = *a1Itr;
335
336 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
337 a2Itr != a2End; ++a2Itr) {
338
339 // If the row and column represent the same register insert a beneficial
340 // cost to preference this allocation - it would allow us to eliminate a
Misha Brukman2a835f92009-01-08 15:50:22 +0000341 // move instruction.
Lang Hames27601ef2008-11-16 12:12:54 +0000342 if (reg1 == *a2Itr) {
343 (*m)[ri][ci] = -cBenefit;
344 isZeroMatrix = false;
345 }
346
347 ++ci;
348 }
349
350 ++ri;
351 }
352
353 // If this turns out to be a zero matrix...
354 if (isZeroMatrix) {
355 // ...free it and return null.
356 delete m;
357 return 0;
358 }
359
360 return m;
361}
362
363PBQPRegAlloc::CoalesceMap PBQPRegAlloc::findCoalesces() {
364
365 typedef MachineFunction::const_iterator MFIterator;
366 typedef MachineBasicBlock::const_iterator MBBIterator;
367 typedef LiveInterval::const_vni_iterator VNIIterator;
Misha Brukman2a835f92009-01-08 15:50:22 +0000368
Lang Hames27601ef2008-11-16 12:12:54 +0000369 CoalesceMap coalescesFound;
370
371 // To find coalesces we need to iterate over the function looking for
372 // copy instructions.
373 for (MFIterator bbItr = mf->begin(), bbEnd = mf->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000374 bbItr != bbEnd; ++bbItr) {
375
376 const MachineBasicBlock *mbb = &*bbItr;
Evan Chengb1290a62008-10-02 18:29:27 +0000377
Lang Hames27601ef2008-11-16 12:12:54 +0000378 for (MBBIterator iItr = mbb->begin(), iEnd = mbb->end();
379 iItr != iEnd; ++iItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000380
381 const MachineInstr *instr = &*iItr;
382
Lang Hames27601ef2008-11-16 12:12:54 +0000383 // If this isn't a copy then continue to the next instruction.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000384 if (!instr->isCopy())
Lang Hames27601ef2008-11-16 12:12:54 +0000385 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000386
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000387 unsigned srcReg = instr->getOperand(1).getReg();
388 unsigned dstReg = instr->getOperand(0).getReg();
389
Lang Hames27601ef2008-11-16 12:12:54 +0000390 // If the registers are already the same our job is nice and easy.
391 if (dstReg == srcReg)
392 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000393
Lang Hames27601ef2008-11-16 12:12:54 +0000394 bool srcRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(srcReg),
395 dstRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(dstReg);
396
397 // If both registers are physical then we can't coalesce.
398 if (srcRegIsPhysical && dstRegIsPhysical)
399 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000400
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000401 // If it's a copy that includes two virtual register but the source and
402 // destination classes differ then we can't coalesce.
403 if (!srcRegIsPhysical && !dstRegIsPhysical &&
404 mri->getRegClass(srcReg) != mri->getRegClass(dstReg))
Lang Hames27601ef2008-11-16 12:12:54 +0000405 continue;
406
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000407 // If one is physical and one is virtual, check that the physical is
408 // allocatable in the class of the virtual.
409 if (srcRegIsPhysical && !dstRegIsPhysical) {
410 const TargetRegisterClass *dstRegClass = mri->getRegClass(dstReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000411 if (std::find(dstRegClass->allocation_order_begin(*mf),
412 dstRegClass->allocation_order_end(*mf), srcReg) ==
413 dstRegClass->allocation_order_end(*mf))
Evan Chengb1290a62008-10-02 18:29:27 +0000414 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000415 }
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000416 if (!srcRegIsPhysical && dstRegIsPhysical) {
417 const TargetRegisterClass *srcRegClass = mri->getRegClass(srcReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000418 if (std::find(srcRegClass->allocation_order_begin(*mf),
419 srcRegClass->allocation_order_end(*mf), dstReg) ==
420 srcRegClass->allocation_order_end(*mf))
Lang Hames27601ef2008-11-16 12:12:54 +0000421 continue;
422 }
423
424 // If we've made it here we have a copy with compatible register classes.
Misha Brukman2a835f92009-01-08 15:50:22 +0000425 // We can probably coalesce, but we need to consider overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000426 const LiveInterval *srcLI = &lis->getInterval(srcReg),
427 *dstLI = &lis->getInterval(dstReg);
428
429 if (srcLI->overlaps(*dstLI)) {
430 // Even in the case of an overlap we might still be able to coalesce,
431 // but we need to make sure that no definition of either range occurs
432 // while the other range is live.
433
434 // Otherwise start by assuming we're ok.
435 bool badDef = false;
436
437 // Test all defs of the source range.
Misha Brukman2a835f92009-01-08 15:50:22 +0000438 for (VNIIterator
Lang Hames27601ef2008-11-16 12:12:54 +0000439 vniItr = srcLI->vni_begin(), vniEnd = srcLI->vni_end();
440 vniItr != vniEnd; ++vniItr) {
441
Lang Hames0b23dc02010-02-09 00:50:27 +0000442 // If we find a poorly defined def we err on the side of caution.
443 if (!(*vniItr)->def.isValid()) {
444 badDef = true;
445 break;
446 }
447
Lang Hames27601ef2008-11-16 12:12:54 +0000448 // If we find a def that kills the coalescing opportunity then
449 // record it and break from the loop.
450 if (dstLI->liveAt((*vniItr)->def)) {
451 badDef = true;
452 break;
453 }
454 }
455
456 // If we have a bad def give up, continue to the next instruction.
457 if (badDef)
458 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000459
Lang Hames27601ef2008-11-16 12:12:54 +0000460 // Otherwise test definitions of the destination range.
461 for (VNIIterator
462 vniItr = dstLI->vni_begin(), vniEnd = dstLI->vni_end();
463 vniItr != vniEnd; ++vniItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000464
Lang Hames27601ef2008-11-16 12:12:54 +0000465 // We want to make sure we skip the copy instruction itself.
Lang Hames52c1afc2009-08-10 23:43:28 +0000466 if ((*vniItr)->getCopy() == instr)
Lang Hames27601ef2008-11-16 12:12:54 +0000467 continue;
468
Lang Hames0b23dc02010-02-09 00:50:27 +0000469 if (!(*vniItr)->def.isValid()) {
470 badDef = true;
471 break;
472 }
473
Lang Hames27601ef2008-11-16 12:12:54 +0000474 if (srcLI->liveAt((*vniItr)->def)) {
475 badDef = true;
476 break;
477 }
478 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000479
Lang Hames27601ef2008-11-16 12:12:54 +0000480 // As before a bad def we give up and continue to the next instr.
481 if (badDef)
482 continue;
483 }
484
485 // If we make it to here then either the ranges didn't overlap, or they
486 // did, but none of their definitions would prevent us from coalescing.
487 // We're good to go with the coalesce.
488
Chris Lattner87565c12010-05-15 17:10:24 +0000489 float cBenefit = std::pow(10.0f, (float)loopInfo->getLoopDepth(mbb)) / 5.0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000490
Lang Hames27601ef2008-11-16 12:12:54 +0000491 coalescesFound[RegPair(srcReg, dstReg)] = cBenefit;
492 coalescesFound[RegPair(dstReg, srcReg)] = cBenefit;
Evan Chengb1290a62008-10-02 18:29:27 +0000493 }
494
495 }
496
Lang Hames27601ef2008-11-16 12:12:54 +0000497 return coalescesFound;
498}
499
500void PBQPRegAlloc::findVRegIntervalsToAlloc() {
501
502 // Iterate over all live ranges.
503 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
504 itr != end; ++itr) {
505
506 // Ignore physical ones.
507 if (TargetRegisterInfo::isPhysicalRegister(itr->first))
508 continue;
509
510 LiveInterval *li = itr->second;
511
512 // If this live interval is non-empty we will use pbqp to allocate it.
513 // Empty intervals we allocate in a simple post-processing stage in
514 // finalizeAlloc.
515 if (!li->empty()) {
516 vregIntervalsToAlloc.insert(li);
517 }
518 else {
519 emptyVRegIntervals.insert(li);
520 }
521 }
Evan Chengb1290a62008-10-02 18:29:27 +0000522}
523
Lang Hames030c4bf2010-01-26 04:49:58 +0000524PBQP::Graph PBQPRegAlloc::constructPBQPProblem() {
Evan Chengb1290a62008-10-02 18:29:27 +0000525
526 typedef std::vector<const LiveInterval*> LIVector;
Lang Hames27601ef2008-11-16 12:12:54 +0000527 typedef std::vector<unsigned> RegVector;
Evan Chengb1290a62008-10-02 18:29:27 +0000528
Lang Hames27601ef2008-11-16 12:12:54 +0000529 // This will store the physical intervals for easy reference.
530 LIVector physIntervals;
Evan Chengb1290a62008-10-02 18:29:27 +0000531
532 // Start by clearing the old node <-> live interval mappings & allowed sets
533 li2Node.clear();
534 node2LI.clear();
535 allowedSets.clear();
536
Lang Hames27601ef2008-11-16 12:12:54 +0000537 // Populate physIntervals, update preg use:
538 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000539 itr != end; ++itr) {
540
Evan Chengb1290a62008-10-02 18:29:27 +0000541 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
542 physIntervals.push_back(itr->second);
543 mri->setPhysRegUsed(itr->second->reg);
544 }
Evan Chengb1290a62008-10-02 18:29:27 +0000545 }
546
Lang Hames27601ef2008-11-16 12:12:54 +0000547 // Iterate over vreg intervals, construct live interval <-> node number
548 // mappings.
Misha Brukman2a835f92009-01-08 15:50:22 +0000549 for (LiveIntervalSet::const_iterator
Lang Hames27601ef2008-11-16 12:12:54 +0000550 itr = vregIntervalsToAlloc.begin(), end = vregIntervalsToAlloc.end();
551 itr != end; ++itr) {
552 const LiveInterval *li = *itr;
553
554 li2Node[li] = node2LI.size();
555 node2LI.push_back(li);
556 }
557
558 // Get the set of potential coalesces.
Lang Hames8481e3b2009-08-19 01:36:14 +0000559 CoalesceMap coalesces;
560
561 if (pbqpCoalescing) {
562 coalesces = findCoalesces();
563 }
Evan Chengb1290a62008-10-02 18:29:27 +0000564
565 // Construct a PBQP solver for this problem
Lang Hames030c4bf2010-01-26 04:49:58 +0000566 PBQP::Graph problem;
567 problemNodes.resize(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000568
569 // Resize allowedSets container appropriately.
Lang Hames27601ef2008-11-16 12:12:54 +0000570 allowedSets.resize(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000571
572 // Iterate over virtual register intervals to compute allowed sets...
573 for (unsigned node = 0; node < node2LI.size(); ++node) {
574
575 // Grab pointers to the interval and its register class.
576 const LiveInterval *li = node2LI[node];
577 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000578
Evan Chengb1290a62008-10-02 18:29:27 +0000579 // Start by assuming all allocable registers in the class are allowed...
Lang Hames27601ef2008-11-16 12:12:54 +0000580 RegVector liAllowed(liRC->allocation_order_begin(*mf),
581 liRC->allocation_order_end(*mf));
Evan Chengb1290a62008-10-02 18:29:27 +0000582
Lang Hames27601ef2008-11-16 12:12:54 +0000583 // Eliminate the physical registers which overlap with this range, along
584 // with all their aliases.
585 for (LIVector::iterator pItr = physIntervals.begin(),
586 pEnd = physIntervals.end(); pItr != pEnd; ++pItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000587
Lang Hames27601ef2008-11-16 12:12:54 +0000588 if (!li->overlaps(**pItr))
589 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000590
Lang Hames27601ef2008-11-16 12:12:54 +0000591 unsigned pReg = (*pItr)->reg;
Evan Chengb1290a62008-10-02 18:29:27 +0000592
Lang Hames27601ef2008-11-16 12:12:54 +0000593 // If we get here then the live intervals overlap, but we're still ok
594 // if they're coalescable.
595 if (coalesces.find(RegPair(li->reg, pReg)) != coalesces.end())
596 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000597
Lang Hames27601ef2008-11-16 12:12:54 +0000598 // If we get here then we have a genuine exclusion.
Evan Chengb1290a62008-10-02 18:29:27 +0000599
Lang Hames27601ef2008-11-16 12:12:54 +0000600 // Remove the overlapping reg...
601 RegVector::iterator eraseItr =
602 std::find(liAllowed.begin(), liAllowed.end(), pReg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000603
Lang Hames27601ef2008-11-16 12:12:54 +0000604 if (eraseItr != liAllowed.end())
605 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000606
Lang Hames27601ef2008-11-16 12:12:54 +0000607 const unsigned *aliasItr = tri->getAliasSet(pReg);
608
609 if (aliasItr != 0) {
610 // ...and its aliases.
611 for (; *aliasItr != 0; ++aliasItr) {
612 RegVector::iterator eraseItr =
613 std::find(liAllowed.begin(), liAllowed.end(), *aliasItr);
Misha Brukman2a835f92009-01-08 15:50:22 +0000614
Lang Hames27601ef2008-11-16 12:12:54 +0000615 if (eraseItr != liAllowed.end()) {
616 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000617 }
Evan Chengb1290a62008-10-02 18:29:27 +0000618 }
Evan Chengb1290a62008-10-02 18:29:27 +0000619 }
Evan Chengb1290a62008-10-02 18:29:27 +0000620 }
621
622 // Copy the allowed set into a member vector for use when constructing cost
623 // vectors & matrices, and mapping PBQP solutions back to assignments.
624 allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end());
625
626 // Set the spill cost to the interval weight, or epsilon if the
627 // interval weight is zero
Lang Hames6699fb22009-08-06 23:32:48 +0000628 PBQP::PBQPNum spillCost = (li->weight != 0.0) ?
629 li->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000630
631 // Build a cost vector for this interval.
Lang Hames6699fb22009-08-06 23:32:48 +0000632 problemNodes[node] =
633 problem.addNode(
634 buildCostVector(li->reg, allowedSets[node], coalesces, spillCost));
Evan Chengb1290a62008-10-02 18:29:27 +0000635
636 }
637
Lang Hames27601ef2008-11-16 12:12:54 +0000638
Evan Chengb1290a62008-10-02 18:29:27 +0000639 // Now add the cost matrices...
640 for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) {
Evan Chengb1290a62008-10-02 18:29:27 +0000641 const LiveInterval *li = node2LI[node1];
642
Evan Chengb1290a62008-10-02 18:29:27 +0000643 // Test for live range overlaps and insert interference matrices.
644 for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) {
645 const LiveInterval *li2 = node2LI[node2];
646
Lang Hames27601ef2008-11-16 12:12:54 +0000647 CoalesceMap::const_iterator cmItr =
648 coalesces.find(RegPair(li->reg, li2->reg));
Evan Chengb1290a62008-10-02 18:29:27 +0000649
Lang Hames6699fb22009-08-06 23:32:48 +0000650 PBQP::Matrix *m = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000651
Lang Hames27601ef2008-11-16 12:12:54 +0000652 if (cmItr != coalesces.end()) {
653 m = buildCoalescingMatrix(allowedSets[node1], allowedSets[node2],
654 cmItr->second);
655 }
656 else if (li->overlaps(*li2)) {
657 m = buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]);
658 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000659
Lang Hames27601ef2008-11-16 12:12:54 +0000660 if (m != 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000661 problem.addEdge(problemNodes[node1],
662 problemNodes[node2],
663 *m);
664
Lang Hames27601ef2008-11-16 12:12:54 +0000665 delete m;
Evan Chengb1290a62008-10-02 18:29:27 +0000666 }
667 }
668 }
669
Lang Hames6699fb22009-08-06 23:32:48 +0000670 assert(problem.getNumNodes() == allowedSets.size());
Lang Hames6699fb22009-08-06 23:32:48 +0000671/*
672 std::cerr << "Allocating for " << problem.getNumNodes() << " nodes, "
673 << problem.getNumEdges() << " edges.\n";
674
675 problem.printDot(std::cerr);
676*/
Evan Chengb1290a62008-10-02 18:29:27 +0000677 // We're done, PBQP problem constructed - return it.
Lang Hames6699fb22009-08-06 23:32:48 +0000678 return problem;
Evan Chengb1290a62008-10-02 18:29:27 +0000679}
680
Evan Chengc781a242009-05-03 18:32:42 +0000681void PBQPRegAlloc::addStackInterval(const LiveInterval *spilled,
682 MachineRegisterInfo* mri) {
Lang Hames27601ef2008-11-16 12:12:54 +0000683 int stackSlot = vrm->getStackSlot(spilled->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000684
685 if (stackSlot == VirtRegMap::NO_STACK_SLOT)
Lang Hames27601ef2008-11-16 12:12:54 +0000686 return;
687
Evan Chengc781a242009-05-03 18:32:42 +0000688 const TargetRegisterClass *RC = mri->getRegClass(spilled->reg);
689 LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC);
Lang Hames27601ef2008-11-16 12:12:54 +0000690
691 VNInfo *vni;
692 if (stackInterval.getNumValNums() != 0)
693 vni = stackInterval.getValNumInfo(0);
694 else
Lang Hames86511252009-09-04 20:41:11 +0000695 vni = stackInterval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +0000696 SlotIndex(), 0, false, lss->getVNInfoAllocator());
Lang Hames27601ef2008-11-16 12:12:54 +0000697
698 LiveInterval &rhsInterval = lis->getInterval(spilled->reg);
699 stackInterval.MergeRangesInAsValue(rhsInterval, vni);
700}
701
Lang Hames6699fb22009-08-06 23:32:48 +0000702bool PBQPRegAlloc::mapPBQPToRegAlloc(const PBQP::Solution &solution) {
Lang Hamese98b4b02009-11-15 04:39:51 +0000703
Evan Chengb1290a62008-10-02 18:29:27 +0000704 // Set to true if we have any spills
705 bool anotherRoundNeeded = false;
706
707 // Clear the existing allocation.
708 vrm->clearAllVirt();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000709
Evan Chengb1290a62008-10-02 18:29:27 +0000710 // Iterate over the nodes mapping the PBQP solution to a register assignment.
711 for (unsigned node = 0; node < node2LI.size(); ++node) {
Lang Hames27601ef2008-11-16 12:12:54 +0000712 unsigned virtReg = node2LI[node]->reg,
Lang Hames030c4bf2010-01-26 04:49:58 +0000713 allocSelection = solution.getSelection(problemNodes[node]);
Lang Hames6699fb22009-08-06 23:32:48 +0000714
Evan Chengb1290a62008-10-02 18:29:27 +0000715
716 // If the PBQP solution is non-zero it's a physical register...
717 if (allocSelection != 0) {
718 // Get the physical reg, subtracting 1 to account for the spill option.
719 unsigned physReg = allowedSets[node][allocSelection - 1];
720
David Greene30931542010-01-05 01:25:43 +0000721 DEBUG(dbgs() << "VREG " << virtReg << " -> "
Lang Hames233fd9c2009-08-18 23:34:50 +0000722 << tri->getName(physReg) << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000723
724 assert(physReg != 0);
725
Evan Chengb1290a62008-10-02 18:29:27 +0000726 // Add to the virt reg map and update the used phys regs.
Lang Hames27601ef2008-11-16 12:12:54 +0000727 vrm->assignVirt2Phys(virtReg, physReg);
Evan Chengb1290a62008-10-02 18:29:27 +0000728 }
729 // ...Otherwise it's a spill.
730 else {
731
732 // Make sure we ignore this virtual reg on the next round
733 // of allocation
Lang Hames27601ef2008-11-16 12:12:54 +0000734 vregIntervalsToAlloc.erase(&lis->getInterval(virtReg));
Evan Chengb1290a62008-10-02 18:29:27 +0000735
Evan Chengb1290a62008-10-02 18:29:27 +0000736 // Insert spill ranges for this live range
Lang Hames27601ef2008-11-16 12:12:54 +0000737 const LiveInterval *spillInterval = node2LI[node];
738 double oldSpillWeight = spillInterval->weight;
Evan Chengb1290a62008-10-02 18:29:27 +0000739 SmallVector<LiveInterval*, 8> spillIs;
740 std::vector<LiveInterval*> newSpills =
Evan Chengc781a242009-05-03 18:32:42 +0000741 lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm);
742 addStackInterval(spillInterval, mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000743
Daniel Dunbarbc84ad92009-08-20 20:01:34 +0000744 (void) oldSpillWeight;
David Greene30931542010-01-05 01:25:43 +0000745 DEBUG(dbgs() << "VREG " << virtReg << " -> SPILLED (Cost: "
Lang Hames233fd9c2009-08-18 23:34:50 +0000746 << oldSpillWeight << ", New vregs: ");
Lang Hames27601ef2008-11-16 12:12:54 +0000747
748 // Copy any newly inserted live intervals into the list of regs to
749 // allocate.
750 for (std::vector<LiveInterval*>::const_iterator
751 itr = newSpills.begin(), end = newSpills.end();
752 itr != end; ++itr) {
753
754 assert(!(*itr)->empty() && "Empty spill range.");
755
David Greene30931542010-01-05 01:25:43 +0000756 DEBUG(dbgs() << (*itr)->reg << " ");
Lang Hames27601ef2008-11-16 12:12:54 +0000757
758 vregIntervalsToAlloc.insert(*itr);
759 }
760
David Greene30931542010-01-05 01:25:43 +0000761 DEBUG(dbgs() << ")\n");
Evan Chengb1290a62008-10-02 18:29:27 +0000762
763 // We need another round if spill intervals were added.
764 anotherRoundNeeded |= !newSpills.empty();
765 }
766 }
767
768 return !anotherRoundNeeded;
769}
770
Lang Hames27601ef2008-11-16 12:12:54 +0000771void PBQPRegAlloc::finalizeAlloc() const {
772 typedef LiveIntervals::iterator LIIterator;
773 typedef LiveInterval::Ranges::const_iterator LRIterator;
774
775 // First allocate registers for the empty intervals.
Argyrios Kyrtzidis3713c0b2008-11-19 12:56:21 +0000776 for (LiveIntervalSet::const_iterator
Daniel Dunbara279bc32009-09-20 02:20:51 +0000777 itr = emptyVRegIntervals.begin(), end = emptyVRegIntervals.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000778 itr != end; ++itr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000779 LiveInterval *li = *itr;
Lang Hames27601ef2008-11-16 12:12:54 +0000780
Evan Cheng90f95f82009-06-14 20:22:55 +0000781 unsigned physReg = vrm->getRegAllocPref(li->reg);
Lang Hames6699fb22009-08-06 23:32:48 +0000782
Lang Hames27601ef2008-11-16 12:12:54 +0000783 if (physReg == 0) {
784 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000785 physReg = *liRC->allocation_order_begin(*mf);
Lang Hames27601ef2008-11-16 12:12:54 +0000786 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000787
788 vrm->assignVirt2Phys(li->reg, physReg);
Lang Hames27601ef2008-11-16 12:12:54 +0000789 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000790
Lang Hames27601ef2008-11-16 12:12:54 +0000791 // Finally iterate over the basic blocks to compute and set the live-in sets.
792 SmallVector<MachineBasicBlock*, 8> liveInMBBs;
793 MachineBasicBlock *entryMBB = &*mf->begin();
794
795 for (LIIterator liItr = lis->begin(), liEnd = lis->end();
796 liItr != liEnd; ++liItr) {
797
798 const LiveInterval *li = liItr->second;
799 unsigned reg = 0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000800
Lang Hames27601ef2008-11-16 12:12:54 +0000801 // Get the physical register for this interval
802 if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
803 reg = li->reg;
804 }
805 else if (vrm->isAssignedReg(li->reg)) {
806 reg = vrm->getPhys(li->reg);
807 }
808 else {
809 // Ranges which are assigned a stack slot only are ignored.
810 continue;
811 }
812
Lang Hamesb0e519f2009-05-17 23:50:36 +0000813 if (reg == 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000814 // Filter out zero regs - they're for intervals that were spilled.
Lang Hamesb0e519f2009-05-17 23:50:36 +0000815 continue;
816 }
817
Lang Hames27601ef2008-11-16 12:12:54 +0000818 // Iterate over the ranges of the current interval...
819 for (LRIterator lrItr = li->begin(), lrEnd = li->end();
820 lrItr != lrEnd; ++lrItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000821
Lang Hames27601ef2008-11-16 12:12:54 +0000822 // Find the set of basic blocks which this range is live into...
823 if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
824 // And add the physreg for this interval to their live-in sets.
825 for (unsigned i = 0; i < liveInMBBs.size(); ++i) {
826 if (liveInMBBs[i] != entryMBB) {
827 if (!liveInMBBs[i]->isLiveIn(reg)) {
828 liveInMBBs[i]->addLiveIn(reg);
829 }
830 }
831 }
832 liveInMBBs.clear();
833 }
834 }
835 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000836
Lang Hames27601ef2008-11-16 12:12:54 +0000837}
838
Evan Chengb1290a62008-10-02 18:29:27 +0000839bool PBQPRegAlloc::runOnMachineFunction(MachineFunction &MF) {
Lang Hames27601ef2008-11-16 12:12:54 +0000840
Evan Chengb1290a62008-10-02 18:29:27 +0000841 mf = &MF;
842 tm = &mf->getTarget();
843 tri = tm->getRegisterInfo();
Lang Hames27601ef2008-11-16 12:12:54 +0000844 tii = tm->getInstrInfo();
Lang Hames233a60e2009-11-03 23:52:08 +0000845 mri = &mf->getRegInfo();
Evan Chengb1290a62008-10-02 18:29:27 +0000846
Lang Hames27601ef2008-11-16 12:12:54 +0000847 lis = &getAnalysis<LiveIntervals>();
848 lss = &getAnalysis<LiveStacks>();
Evan Chengb1290a62008-10-02 18:29:27 +0000849 loopInfo = &getAnalysis<MachineLoopInfo>();
850
Owen Anderson49c8aa02009-03-13 05:55:11 +0000851 vrm = &getAnalysis<VirtRegMap>();
Evan Chengb1290a62008-10-02 18:29:27 +0000852
Lang Hames030c4bf2010-01-26 04:49:58 +0000853 DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000854
Evan Chengb1290a62008-10-02 18:29:27 +0000855 // Allocator main loop:
Misha Brukman2a835f92009-01-08 15:50:22 +0000856 //
Evan Chengb1290a62008-10-02 18:29:27 +0000857 // * Map current regalloc problem to a PBQP problem
858 // * Solve the PBQP problem
859 // * Map the solution back to a register allocation
860 // * Spill if necessary
Misha Brukman2a835f92009-01-08 15:50:22 +0000861 //
Evan Chengb1290a62008-10-02 18:29:27 +0000862 // This process is continued till no more spills are generated.
863
Lang Hames27601ef2008-11-16 12:12:54 +0000864 // Find the vreg intervals in need of allocation.
865 findVRegIntervalsToAlloc();
Misha Brukman2a835f92009-01-08 15:50:22 +0000866
Lang Hames27601ef2008-11-16 12:12:54 +0000867 // If there are non-empty intervals allocate them using pbqp.
868 if (!vregIntervalsToAlloc.empty()) {
Evan Chengb1290a62008-10-02 18:29:27 +0000869
Lang Hames27601ef2008-11-16 12:12:54 +0000870 bool pbqpAllocComplete = false;
871 unsigned round = 0;
872
873 while (!pbqpAllocComplete) {
David Greene30931542010-01-05 01:25:43 +0000874 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000875
Lang Hames030c4bf2010-01-26 04:49:58 +0000876 PBQP::Graph problem = constructPBQPProblem();
877 PBQP::Solution solution =
878 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(problem);
Lang Hames233fd9c2009-08-18 23:34:50 +0000879
Lang Hames6699fb22009-08-06 23:32:48 +0000880 pbqpAllocComplete = mapPBQPToRegAlloc(solution);
Lang Hames27601ef2008-11-16 12:12:54 +0000881
882 ++round;
883 }
Evan Chengb1290a62008-10-02 18:29:27 +0000884 }
885
Lang Hames27601ef2008-11-16 12:12:54 +0000886 // Finalise allocation, allocate empty ranges.
887 finalizeAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000888
Lang Hames27601ef2008-11-16 12:12:54 +0000889 vregIntervalsToAlloc.clear();
890 emptyVRegIntervals.clear();
891 li2Node.clear();
892 node2LI.clear();
893 allowedSets.clear();
Lang Hames030c4bf2010-01-26 04:49:58 +0000894 problemNodes.clear();
Lang Hames27601ef2008-11-16 12:12:54 +0000895
David Greene30931542010-01-05 01:25:43 +0000896 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000897
Lang Hames87e3bca2009-05-06 02:36:21 +0000898 // Run rewriter
899 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
900
901 rewriter->runOnMachineFunction(*mf, *vrm, lis);
Lang Hames27601ef2008-11-16 12:12:54 +0000902
Misha Brukman2a835f92009-01-08 15:50:22 +0000903 return true;
Evan Chengb1290a62008-10-02 18:29:27 +0000904}
905
906FunctionPass* llvm::createPBQPRegisterAllocator() {
907 return new PBQPRegAlloc();
908}
909
910
911#undef DEBUG_TYPE