blob: c6fdf04c7af1ba50bb4583577b0b23c3039ca287 [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"
Lang Hames54cc2ef2010-07-19 15:22:28 +000037#include "RenderMachineFunction.h"
Lang Hames12f35c52010-07-18 00:57:59 +000038#include "Splitter.h"
Evan Chengb1290a62008-10-02 18:29:27 +000039#include "VirtRegMap.h"
Lang Hames87e3bca2009-05-06 02:36:21 +000040#include "VirtRegRewriter.h"
Lang Hamesa937f222009-12-14 06:49:42 +000041#include "llvm/CodeGen/CalcSpillWeights.h"
Evan Chengb1290a62008-10-02 18:29:27 +000042#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Lang Hames27601ef2008-11-16 12:12:54 +000043#include "llvm/CodeGen/LiveStackAnalysis.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000044#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chengb1290a62008-10-02 18:29:27 +000045#include "llvm/CodeGen/MachineLoopInfo.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000046#include "llvm/CodeGen/MachineRegisterInfo.h"
47#include "llvm/CodeGen/RegAllocRegistry.h"
48#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengb1290a62008-10-02 18:29:27 +000049#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000050#include "llvm/Support/raw_ostream.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000051#include "llvm/Target/TargetInstrInfo.h"
52#include "llvm/Target/TargetMachine.h"
53#include <limits>
Evan Chengb1290a62008-10-02 18:29:27 +000054#include <map>
Misha Brukman2a835f92009-01-08 15:50:22 +000055#include <memory>
Evan Chengb1290a62008-10-02 18:29:27 +000056#include <set>
57#include <vector>
Evan Chengb1290a62008-10-02 18:29:27 +000058
59using namespace llvm;
60
61static RegisterRegAlloc
Duncan Sands1aecd152010-02-18 14:10:41 +000062registerPBQPRepAlloc("pbqp", "PBQP register allocator",
Lang Hames030c4bf2010-01-26 04:49:58 +000063 llvm::createPBQPRegisterAllocator);
Evan Chengb1290a62008-10-02 18:29:27 +000064
Lang Hames8481e3b2009-08-19 01:36:14 +000065static cl::opt<bool>
66pbqpCoalescing("pbqp-coalescing",
Lang Hames030c4bf2010-01-26 04:49:58 +000067 cl::desc("Attempt coalescing during PBQP register allocation."),
68 cl::init(false), cl::Hidden);
Lang Hames8481e3b2009-08-19 01:36:14 +000069
Lang Hames12f35c52010-07-18 00:57:59 +000070static cl::opt<bool>
71pbqpPreSplitting("pbqp-pre-splitting",
72 cl::desc("Pre-splite before PBQP register allocation."),
73 cl::init(false), cl::Hidden);
74
Evan Chengb1290a62008-10-02 18:29:27 +000075namespace {
76
Lang Hames6699fb22009-08-06 23:32:48 +000077 ///
78 /// PBQP based allocators solve the register allocation problem by mapping
79 /// register allocation problems to Partitioned Boolean Quadratic
80 /// Programming problems.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000081 class PBQPRegAlloc : public MachineFunctionPass {
Evan Chengb1290a62008-10-02 18:29:27 +000082 public:
83
84 static char ID;
Daniel Dunbara279bc32009-09-20 02:20:51 +000085
Lang Hames6699fb22009-08-06 23:32:48 +000086 /// Construct a PBQP register allocator.
Owen Anderson90c579d2010-08-06 18:33:48 +000087 PBQPRegAlloc() : MachineFunctionPass(ID) {}
Evan Chengb1290a62008-10-02 18:29:27 +000088
Lang Hames6699fb22009-08-06 23:32:48 +000089 /// Return the pass name.
Dan Gohman00b0a242009-08-11 15:35:57 +000090 virtual const char* getPassName() const {
Evan Chengb1290a62008-10-02 18:29:27 +000091 return "PBQP Register Allocator";
92 }
93
Lang Hames6699fb22009-08-06 23:32:48 +000094 /// PBQP analysis usage.
95 virtual void getAnalysisUsage(AnalysisUsage &au) const {
Lang Hames233a60e2009-11-03 23:52:08 +000096 au.addRequired<SlotIndexes>();
97 au.addPreserved<SlotIndexes>();
Lang Hames6699fb22009-08-06 23:32:48 +000098 au.addRequired<LiveIntervals>();
99 //au.addRequiredID(SplitCriticalEdgesID);
Lang Hamesf7c553e2009-08-12 21:04:53 +0000100 au.addRequired<RegisterCoalescer>();
Lang Hamesa937f222009-12-14 06:49:42 +0000101 au.addRequired<CalculateSpillWeights>();
Lang Hames6699fb22009-08-06 23:32:48 +0000102 au.addRequired<LiveStacks>();
103 au.addPreserved<LiveStacks>();
104 au.addRequired<MachineLoopInfo>();
105 au.addPreserved<MachineLoopInfo>();
Lang Hames12f35c52010-07-18 00:57:59 +0000106 if (pbqpPreSplitting)
107 au.addRequired<LoopSplitter>();
Lang Hames6699fb22009-08-06 23:32:48 +0000108 au.addRequired<VirtRegMap>();
Lang Hames54cc2ef2010-07-19 15:22:28 +0000109 au.addRequired<RenderMachineFunction>();
Lang Hames6699fb22009-08-06 23:32:48 +0000110 MachineFunctionPass::getAnalysisUsage(au);
Evan Chengb1290a62008-10-02 18:29:27 +0000111 }
112
Lang Hames6699fb22009-08-06 23:32:48 +0000113 /// Perform register allocation
Evan Chengb1290a62008-10-02 18:29:27 +0000114 virtual bool runOnMachineFunction(MachineFunction &MF);
115
116 private:
Lang Hamesd0f6f012010-07-17 06:31:41 +0000117
118 class LIOrdering {
119 public:
120 bool operator()(const LiveInterval *li1, const LiveInterval *li2) const {
121 return li1->reg < li2->reg;
122 }
123 };
124
125 typedef std::map<const LiveInterval*, unsigned, LIOrdering> LI2NodeMap;
Evan Chengb1290a62008-10-02 18:29:27 +0000126 typedef std::vector<const LiveInterval*> Node2LIMap;
127 typedef std::vector<unsigned> AllowedSet;
128 typedef std::vector<AllowedSet> AllowedSetMap;
Lang Hames27601ef2008-11-16 12:12:54 +0000129 typedef std::set<unsigned> RegSet;
130 typedef std::pair<unsigned, unsigned> RegPair;
Lang Hames6699fb22009-08-06 23:32:48 +0000131 typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
Lang Hames27601ef2008-11-16 12:12:54 +0000132
Lang Hamesd0f6f012010-07-17 06:31:41 +0000133 typedef std::set<LiveInterval*, LIOrdering> LiveIntervalSet;
Evan Chengb1290a62008-10-02 18:29:27 +0000134
Lang Hames030c4bf2010-01-26 04:49:58 +0000135 typedef std::vector<PBQP::Graph::NodeItr> NodeVector;
136
Evan Chengb1290a62008-10-02 18:29:27 +0000137 MachineFunction *mf;
138 const TargetMachine *tm;
139 const TargetRegisterInfo *tri;
140 const TargetInstrInfo *tii;
141 const MachineLoopInfo *loopInfo;
142 MachineRegisterInfo *mri;
143
Lang Hames27601ef2008-11-16 12:12:54 +0000144 LiveIntervals *lis;
145 LiveStacks *lss;
Evan Chengb1290a62008-10-02 18:29:27 +0000146 VirtRegMap *vrm;
147
148 LI2NodeMap li2Node;
149 Node2LIMap node2LI;
150 AllowedSetMap allowedSets;
Lang Hames27601ef2008-11-16 12:12:54 +0000151 LiveIntervalSet vregIntervalsToAlloc,
152 emptyVRegIntervals;
Lang Hames030c4bf2010-01-26 04:49:58 +0000153 NodeVector problemNodes;
Evan Chengb1290a62008-10-02 18:29:27 +0000154
Misha Brukman2a835f92009-01-08 15:50:22 +0000155
Lang Hames6699fb22009-08-06 23:32:48 +0000156 /// Builds a PBQP cost vector.
Lang Hames27601ef2008-11-16 12:12:54 +0000157 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000158 PBQP::Vector buildCostVector(unsigned vReg,
159 const RegContainer &allowed,
160 const CoalesceMap &cealesces,
161 PBQP::PBQPNum spillCost) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000162
Lang Hames6699fb22009-08-06 23:32:48 +0000163 /// \brief Builds a PBQP interference matrix.
164 ///
165 /// @return Either a pointer to a non-zero PBQP matrix representing the
166 /// allocation option costs, or a null pointer for a zero matrix.
167 ///
168 /// Expects allowed sets for two interfering LiveIntervals. These allowed
169 /// sets should contain only allocable registers from the LiveInterval's
170 /// register class, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000171 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000172 PBQP::Matrix* buildInterferenceMatrix(const RegContainer &allowed1,
173 const RegContainer &allowed2) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000174
Lang Hames6699fb22009-08-06 23:32:48 +0000175 ///
176 /// Expects allowed sets for two potentially coalescable LiveIntervals,
177 /// and an estimated benefit due to coalescing. The allowed sets should
178 /// contain only allocable registers from the LiveInterval's register
179 /// classes, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000180 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000181 PBQP::Matrix* buildCoalescingMatrix(const RegContainer &allowed1,
182 const RegContainer &allowed2,
183 PBQP::PBQPNum cBenefit) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000184
Lang Hames6699fb22009-08-06 23:32:48 +0000185 /// \brief Finds coalescing opportunities and returns them as a map.
186 ///
187 /// Any entries in the map are guaranteed coalescable, even if their
188 /// corresponding live intervals overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000189 CoalesceMap findCoalesces();
Evan Chengb1290a62008-10-02 18:29:27 +0000190
Lang Hames6699fb22009-08-06 23:32:48 +0000191 /// \brief Finds the initial set of vreg intervals to allocate.
Lang Hames27601ef2008-11-16 12:12:54 +0000192 void findVRegIntervalsToAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000193
Lang Hames6699fb22009-08-06 23:32:48 +0000194 /// \brief Constructs a PBQP problem representation of the register
195 /// allocation problem for this function.
196 ///
197 /// @return a PBQP solver object for the register allocation problem.
Lang Hames030c4bf2010-01-26 04:49:58 +0000198 PBQP::Graph constructPBQPProblem();
Evan Chengb1290a62008-10-02 18:29:27 +0000199
Lang Hames6699fb22009-08-06 23:32:48 +0000200 /// \brief Adds a stack interval if the given live interval has been
201 /// spilled. Used to support stack slot coloring.
Evan Chengc781a242009-05-03 18:32:42 +0000202 void addStackInterval(const LiveInterval *spilled,MachineRegisterInfo* mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000203
Lang Hames6699fb22009-08-06 23:32:48 +0000204 /// \brief Given a solved PBQP problem maps this solution back to a register
205 /// assignment.
206 bool mapPBQPToRegAlloc(const PBQP::Solution &solution);
Evan Chengb1290a62008-10-02 18:29:27 +0000207
Lang Hames6699fb22009-08-06 23:32:48 +0000208 /// \brief Postprocessing before final spilling. Sets basic block "live in"
209 /// variables.
Lang Hames27601ef2008-11-16 12:12:54 +0000210 void finalizeAlloc() const;
211
Evan Chengb1290a62008-10-02 18:29:27 +0000212 };
213
214 char PBQPRegAlloc::ID = 0;
215}
216
217
Lang Hames27601ef2008-11-16 12:12:54 +0000218template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000219PBQP::Vector PBQPRegAlloc::buildCostVector(unsigned vReg,
220 const RegContainer &allowed,
221 const CoalesceMap &coalesces,
222 PBQP::PBQPNum spillCost) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000223
Lang Hames27601ef2008-11-16 12:12:54 +0000224 typedef typename RegContainer::const_iterator AllowedItr;
225
Evan Chengb1290a62008-10-02 18:29:27 +0000226 // Allocate vector. Additional element (0th) used for spill option
Lang Hames6699fb22009-08-06 23:32:48 +0000227 PBQP::Vector v(allowed.size() + 1, 0);
Evan Chengb1290a62008-10-02 18:29:27 +0000228
Lang Hames6699fb22009-08-06 23:32:48 +0000229 v[0] = spillCost;
Evan Chengb1290a62008-10-02 18:29:27 +0000230
Lang Hames27601ef2008-11-16 12:12:54 +0000231 // Iterate over the allowed registers inserting coalesce benefits if there
232 // are any.
233 unsigned ai = 0;
234 for (AllowedItr itr = allowed.begin(), end = allowed.end();
235 itr != end; ++itr, ++ai) {
236
237 unsigned pReg = *itr;
238
239 CoalesceMap::const_iterator cmItr =
240 coalesces.find(RegPair(vReg, pReg));
241
242 // No coalesce - on to the next preg.
243 if (cmItr == coalesces.end())
244 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000245
246 // We have a coalesce - insert the benefit.
Lang Hames6699fb22009-08-06 23:32:48 +0000247 v[ai + 1] = -cmItr->second;
Lang Hames27601ef2008-11-16 12:12:54 +0000248 }
249
Evan Chengb1290a62008-10-02 18:29:27 +0000250 return v;
251}
252
Lang Hames27601ef2008-11-16 12:12:54 +0000253template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000254PBQP::Matrix* PBQPRegAlloc::buildInterferenceMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000255 const RegContainer &allowed1, const RegContainer &allowed2) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000256
Lang Hames27601ef2008-11-16 12:12:54 +0000257 typedef typename RegContainer::const_iterator RegContainerIterator;
Evan Chengb1290a62008-10-02 18:29:27 +0000258
259 // Construct a PBQP matrix representing the cost of allocation options. The
260 // rows and columns correspond to the allocation options for the two live
261 // intervals. Elements will be infinite where corresponding registers alias,
262 // since we cannot allocate aliasing registers to interfering live intervals.
263 // All other elements (non-aliasing combinations) will have zero cost. Note
264 // that the spill option (element 0,0) has zero cost, since we can allocate
265 // both intervals to memory safely (the cost for each individual allocation
266 // to memory is accounted for by the cost vectors for each live interval).
Lang Hames6699fb22009-08-06 23:32:48 +0000267 PBQP::Matrix *m =
268 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Misha Brukman2a835f92009-01-08 15:50:22 +0000269
Evan Chengb1290a62008-10-02 18:29:27 +0000270 // Assume this is a zero matrix until proven otherwise. Zero matrices occur
271 // between interfering live ranges with non-overlapping register sets (e.g.
272 // non-overlapping reg classes, or disjoint sets of allowed regs within the
273 // same class). The term "overlapping" is used advisedly: sets which do not
274 // intersect, but contain registers which alias, will have non-zero matrices.
275 // We optimize zero matrices away to improve solver speed.
276 bool isZeroMatrix = true;
277
278
279 // Row index. Starts at 1, since the 0th row is for the spill option, which
280 // is always zero.
Misha Brukman2a835f92009-01-08 15:50:22 +0000281 unsigned ri = 1;
Evan Chengb1290a62008-10-02 18:29:27 +0000282
Misha Brukman2a835f92009-01-08 15:50:22 +0000283 // Iterate over allowed sets, insert infinities where required.
Lang Hames27601ef2008-11-16 12:12:54 +0000284 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000285 a1Itr != a1End; ++a1Itr) {
286
287 // Column index, starts at 1 as for row index.
288 unsigned ci = 1;
289 unsigned reg1 = *a1Itr;
290
Lang Hames27601ef2008-11-16 12:12:54 +0000291 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000292 a2Itr != a2End; ++a2Itr) {
293
294 unsigned reg2 = *a2Itr;
295
296 // If the row/column regs are identical or alias insert an infinity.
Lang Hames3f2f3f52009-09-03 02:52:02 +0000297 if (tri->regsOverlap(reg1, reg2)) {
Lang Hames6699fb22009-08-06 23:32:48 +0000298 (*m)[ri][ci] = std::numeric_limits<PBQP::PBQPNum>::infinity();
Evan Chengb1290a62008-10-02 18:29:27 +0000299 isZeroMatrix = false;
300 }
301
302 ++ci;
303 }
304
305 ++ri;
306 }
307
308 // If this turns out to be a zero matrix...
309 if (isZeroMatrix) {
310 // free it and return null.
311 delete m;
312 return 0;
313 }
314
315 // ...otherwise return the cost matrix.
316 return m;
317}
318
Lang Hames27601ef2008-11-16 12:12:54 +0000319template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000320PBQP::Matrix* PBQPRegAlloc::buildCoalescingMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000321 const RegContainer &allowed1, const RegContainer &allowed2,
Lang Hames6699fb22009-08-06 23:32:48 +0000322 PBQP::PBQPNum cBenefit) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000323
Lang Hames27601ef2008-11-16 12:12:54 +0000324 typedef typename RegContainer::const_iterator RegContainerIterator;
325
326 // Construct a PBQP Matrix representing the benefits of coalescing. As with
327 // interference matrices the rows and columns represent allowed registers
328 // for the LiveIntervals which are (potentially) to be coalesced. The amount
329 // -cBenefit will be placed in any element representing the same register
330 // for both intervals.
Lang Hames6699fb22009-08-06 23:32:48 +0000331 PBQP::Matrix *m =
332 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Lang Hames27601ef2008-11-16 12:12:54 +0000333
334 // Reset costs to zero.
335 m->reset(0);
336
337 // Assume the matrix is zero till proven otherwise. Zero matrices will be
338 // optimized away as in the interference case.
339 bool isZeroMatrix = true;
340
341 // Row index. Starts at 1, since the 0th row is for the spill option, which
342 // is always zero.
343 unsigned ri = 1;
344
345 // Iterate over the allowed sets, insert coalescing benefits where
346 // appropriate.
347 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
348 a1Itr != a1End; ++a1Itr) {
349
350 // Column index, starts at 1 as for row index.
351 unsigned ci = 1;
352 unsigned reg1 = *a1Itr;
353
354 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
355 a2Itr != a2End; ++a2Itr) {
356
357 // If the row and column represent the same register insert a beneficial
358 // cost to preference this allocation - it would allow us to eliminate a
Misha Brukman2a835f92009-01-08 15:50:22 +0000359 // move instruction.
Lang Hames27601ef2008-11-16 12:12:54 +0000360 if (reg1 == *a2Itr) {
361 (*m)[ri][ci] = -cBenefit;
362 isZeroMatrix = false;
363 }
364
365 ++ci;
366 }
367
368 ++ri;
369 }
370
371 // If this turns out to be a zero matrix...
372 if (isZeroMatrix) {
373 // ...free it and return null.
374 delete m;
375 return 0;
376 }
377
378 return m;
379}
380
381PBQPRegAlloc::CoalesceMap PBQPRegAlloc::findCoalesces() {
382
383 typedef MachineFunction::const_iterator MFIterator;
384 typedef MachineBasicBlock::const_iterator MBBIterator;
385 typedef LiveInterval::const_vni_iterator VNIIterator;
Misha Brukman2a835f92009-01-08 15:50:22 +0000386
Lang Hames27601ef2008-11-16 12:12:54 +0000387 CoalesceMap coalescesFound;
388
389 // To find coalesces we need to iterate over the function looking for
390 // copy instructions.
391 for (MFIterator bbItr = mf->begin(), bbEnd = mf->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000392 bbItr != bbEnd; ++bbItr) {
393
394 const MachineBasicBlock *mbb = &*bbItr;
Evan Chengb1290a62008-10-02 18:29:27 +0000395
Lang Hames27601ef2008-11-16 12:12:54 +0000396 for (MBBIterator iItr = mbb->begin(), iEnd = mbb->end();
397 iItr != iEnd; ++iItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000398
399 const MachineInstr *instr = &*iItr;
400
Lang Hames27601ef2008-11-16 12:12:54 +0000401 // If this isn't a copy then continue to the next instruction.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000402 if (!instr->isCopy())
Lang Hames27601ef2008-11-16 12:12:54 +0000403 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000404
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000405 unsigned srcReg = instr->getOperand(1).getReg();
406 unsigned dstReg = instr->getOperand(0).getReg();
407
Lang Hames27601ef2008-11-16 12:12:54 +0000408 // If the registers are already the same our job is nice and easy.
409 if (dstReg == srcReg)
410 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000411
Lang Hames27601ef2008-11-16 12:12:54 +0000412 bool srcRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(srcReg),
413 dstRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(dstReg);
414
415 // If both registers are physical then we can't coalesce.
416 if (srcRegIsPhysical && dstRegIsPhysical)
417 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000418
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000419 // If it's a copy that includes two virtual register but the source and
420 // destination classes differ then we can't coalesce.
421 if (!srcRegIsPhysical && !dstRegIsPhysical &&
422 mri->getRegClass(srcReg) != mri->getRegClass(dstReg))
Lang Hames27601ef2008-11-16 12:12:54 +0000423 continue;
424
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000425 // If one is physical and one is virtual, check that the physical is
426 // allocatable in the class of the virtual.
427 if (srcRegIsPhysical && !dstRegIsPhysical) {
428 const TargetRegisterClass *dstRegClass = mri->getRegClass(dstReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000429 if (std::find(dstRegClass->allocation_order_begin(*mf),
430 dstRegClass->allocation_order_end(*mf), srcReg) ==
431 dstRegClass->allocation_order_end(*mf))
Evan Chengb1290a62008-10-02 18:29:27 +0000432 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000433 }
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000434 if (!srcRegIsPhysical && dstRegIsPhysical) {
435 const TargetRegisterClass *srcRegClass = mri->getRegClass(srcReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000436 if (std::find(srcRegClass->allocation_order_begin(*mf),
437 srcRegClass->allocation_order_end(*mf), dstReg) ==
438 srcRegClass->allocation_order_end(*mf))
Lang Hames27601ef2008-11-16 12:12:54 +0000439 continue;
440 }
441
442 // If we've made it here we have a copy with compatible register classes.
Misha Brukman2a835f92009-01-08 15:50:22 +0000443 // We can probably coalesce, but we need to consider overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000444 const LiveInterval *srcLI = &lis->getInterval(srcReg),
445 *dstLI = &lis->getInterval(dstReg);
446
447 if (srcLI->overlaps(*dstLI)) {
448 // Even in the case of an overlap we might still be able to coalesce,
449 // but we need to make sure that no definition of either range occurs
450 // while the other range is live.
451
452 // Otherwise start by assuming we're ok.
453 bool badDef = false;
454
455 // Test all defs of the source range.
Misha Brukman2a835f92009-01-08 15:50:22 +0000456 for (VNIIterator
Lang Hames27601ef2008-11-16 12:12:54 +0000457 vniItr = srcLI->vni_begin(), vniEnd = srcLI->vni_end();
458 vniItr != vniEnd; ++vniItr) {
459
Lang Hames0b23dc02010-02-09 00:50:27 +0000460 // If we find a poorly defined def we err on the side of caution.
461 if (!(*vniItr)->def.isValid()) {
462 badDef = true;
463 break;
464 }
465
Lang Hames27601ef2008-11-16 12:12:54 +0000466 // If we find a def that kills the coalescing opportunity then
467 // record it and break from the loop.
468 if (dstLI->liveAt((*vniItr)->def)) {
469 badDef = true;
470 break;
471 }
472 }
473
474 // If we have a bad def give up, continue to the next instruction.
475 if (badDef)
476 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000477
Lang Hames27601ef2008-11-16 12:12:54 +0000478 // Otherwise test definitions of the destination range.
479 for (VNIIterator
480 vniItr = dstLI->vni_begin(), vniEnd = dstLI->vni_end();
481 vniItr != vniEnd; ++vniItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000482
Lang Hames27601ef2008-11-16 12:12:54 +0000483 // We want to make sure we skip the copy instruction itself.
Lang Hames52c1afc2009-08-10 23:43:28 +0000484 if ((*vniItr)->getCopy() == instr)
Lang Hames27601ef2008-11-16 12:12:54 +0000485 continue;
486
Lang Hames0b23dc02010-02-09 00:50:27 +0000487 if (!(*vniItr)->def.isValid()) {
488 badDef = true;
489 break;
490 }
491
Lang Hames27601ef2008-11-16 12:12:54 +0000492 if (srcLI->liveAt((*vniItr)->def)) {
493 badDef = true;
494 break;
495 }
496 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000497
Lang Hames27601ef2008-11-16 12:12:54 +0000498 // As before a bad def we give up and continue to the next instr.
499 if (badDef)
500 continue;
501 }
502
503 // If we make it to here then either the ranges didn't overlap, or they
504 // did, but none of their definitions would prevent us from coalescing.
505 // We're good to go with the coalesce.
506
Chris Lattner87565c12010-05-15 17:10:24 +0000507 float cBenefit = std::pow(10.0f, (float)loopInfo->getLoopDepth(mbb)) / 5.0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000508
Lang Hames27601ef2008-11-16 12:12:54 +0000509 coalescesFound[RegPair(srcReg, dstReg)] = cBenefit;
510 coalescesFound[RegPair(dstReg, srcReg)] = cBenefit;
Evan Chengb1290a62008-10-02 18:29:27 +0000511 }
512
513 }
514
Lang Hames27601ef2008-11-16 12:12:54 +0000515 return coalescesFound;
516}
517
518void PBQPRegAlloc::findVRegIntervalsToAlloc() {
519
520 // Iterate over all live ranges.
521 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
522 itr != end; ++itr) {
523
524 // Ignore physical ones.
525 if (TargetRegisterInfo::isPhysicalRegister(itr->first))
526 continue;
527
528 LiveInterval *li = itr->second;
529
530 // If this live interval is non-empty we will use pbqp to allocate it.
531 // Empty intervals we allocate in a simple post-processing stage in
532 // finalizeAlloc.
533 if (!li->empty()) {
534 vregIntervalsToAlloc.insert(li);
535 }
536 else {
537 emptyVRegIntervals.insert(li);
538 }
539 }
Evan Chengb1290a62008-10-02 18:29:27 +0000540}
541
Lang Hames030c4bf2010-01-26 04:49:58 +0000542PBQP::Graph PBQPRegAlloc::constructPBQPProblem() {
Evan Chengb1290a62008-10-02 18:29:27 +0000543
544 typedef std::vector<const LiveInterval*> LIVector;
Lang Hames27601ef2008-11-16 12:12:54 +0000545 typedef std::vector<unsigned> RegVector;
Evan Chengb1290a62008-10-02 18:29:27 +0000546
Lang Hames27601ef2008-11-16 12:12:54 +0000547 // This will store the physical intervals for easy reference.
548 LIVector physIntervals;
Evan Chengb1290a62008-10-02 18:29:27 +0000549
550 // Start by clearing the old node <-> live interval mappings & allowed sets
551 li2Node.clear();
552 node2LI.clear();
553 allowedSets.clear();
554
Lang Hames27601ef2008-11-16 12:12:54 +0000555 // Populate physIntervals, update preg use:
556 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000557 itr != end; ++itr) {
558
Evan Chengb1290a62008-10-02 18:29:27 +0000559 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
560 physIntervals.push_back(itr->second);
561 mri->setPhysRegUsed(itr->second->reg);
562 }
Evan Chengb1290a62008-10-02 18:29:27 +0000563 }
564
Lang Hames27601ef2008-11-16 12:12:54 +0000565 // Iterate over vreg intervals, construct live interval <-> node number
566 // mappings.
Misha Brukman2a835f92009-01-08 15:50:22 +0000567 for (LiveIntervalSet::const_iterator
Lang Hames27601ef2008-11-16 12:12:54 +0000568 itr = vregIntervalsToAlloc.begin(), end = vregIntervalsToAlloc.end();
569 itr != end; ++itr) {
570 const LiveInterval *li = *itr;
571
572 li2Node[li] = node2LI.size();
573 node2LI.push_back(li);
574 }
575
576 // Get the set of potential coalesces.
Lang Hames8481e3b2009-08-19 01:36:14 +0000577 CoalesceMap coalesces;
578
579 if (pbqpCoalescing) {
580 coalesces = findCoalesces();
581 }
Evan Chengb1290a62008-10-02 18:29:27 +0000582
583 // Construct a PBQP solver for this problem
Lang Hames030c4bf2010-01-26 04:49:58 +0000584 PBQP::Graph problem;
585 problemNodes.resize(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000586
587 // Resize allowedSets container appropriately.
Lang Hames27601ef2008-11-16 12:12:54 +0000588 allowedSets.resize(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000589
Jim Grosbach269354e2010-09-01 21:23:03 +0000590 BitVector ReservedRegs = tri->getReservedRegs(*mf);
591
Evan Chengb1290a62008-10-02 18:29:27 +0000592 // Iterate over virtual register intervals to compute allowed sets...
593 for (unsigned node = 0; node < node2LI.size(); ++node) {
594
595 // Grab pointers to the interval and its register class.
596 const LiveInterval *li = node2LI[node];
597 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000598
Evan Chengb1290a62008-10-02 18:29:27 +0000599 // Start by assuming all allocable registers in the class are allowed...
Jim Grosbach269354e2010-09-01 21:23:03 +0000600 RegVector liAllowed;
601 TargetRegisterClass::iterator aob = liRC->allocation_order_begin(*mf);
602 TargetRegisterClass::iterator aoe = liRC->allocation_order_end(*mf);
603 for (TargetRegisterClass::iterator it = aob; it != aoe; ++it)
604 if (!ReservedRegs.test(*it))
605 liAllowed.push_back(*it);
Evan Chengb1290a62008-10-02 18:29:27 +0000606
Lang Hames27601ef2008-11-16 12:12:54 +0000607 // Eliminate the physical registers which overlap with this range, along
608 // with all their aliases.
609 for (LIVector::iterator pItr = physIntervals.begin(),
610 pEnd = physIntervals.end(); pItr != pEnd; ++pItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000611
Lang Hames27601ef2008-11-16 12:12:54 +0000612 if (!li->overlaps(**pItr))
613 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000614
Lang Hames27601ef2008-11-16 12:12:54 +0000615 unsigned pReg = (*pItr)->reg;
Evan Chengb1290a62008-10-02 18:29:27 +0000616
Lang Hames27601ef2008-11-16 12:12:54 +0000617 // If we get here then the live intervals overlap, but we're still ok
618 // if they're coalescable.
619 if (coalesces.find(RegPair(li->reg, pReg)) != coalesces.end())
620 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000621
Lang Hames27601ef2008-11-16 12:12:54 +0000622 // If we get here then we have a genuine exclusion.
Evan Chengb1290a62008-10-02 18:29:27 +0000623
Lang Hames27601ef2008-11-16 12:12:54 +0000624 // Remove the overlapping reg...
625 RegVector::iterator eraseItr =
626 std::find(liAllowed.begin(), liAllowed.end(), pReg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000627
Lang Hames27601ef2008-11-16 12:12:54 +0000628 if (eraseItr != liAllowed.end())
629 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000630
Lang Hames27601ef2008-11-16 12:12:54 +0000631 const unsigned *aliasItr = tri->getAliasSet(pReg);
632
633 if (aliasItr != 0) {
634 // ...and its aliases.
635 for (; *aliasItr != 0; ++aliasItr) {
636 RegVector::iterator eraseItr =
637 std::find(liAllowed.begin(), liAllowed.end(), *aliasItr);
Misha Brukman2a835f92009-01-08 15:50:22 +0000638
Lang Hames27601ef2008-11-16 12:12:54 +0000639 if (eraseItr != liAllowed.end()) {
640 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000641 }
Evan Chengb1290a62008-10-02 18:29:27 +0000642 }
Evan Chengb1290a62008-10-02 18:29:27 +0000643 }
Evan Chengb1290a62008-10-02 18:29:27 +0000644 }
645
646 // Copy the allowed set into a member vector for use when constructing cost
647 // vectors & matrices, and mapping PBQP solutions back to assignments.
648 allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end());
649
650 // Set the spill cost to the interval weight, or epsilon if the
651 // interval weight is zero
Lang Hames6699fb22009-08-06 23:32:48 +0000652 PBQP::PBQPNum spillCost = (li->weight != 0.0) ?
653 li->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000654
655 // Build a cost vector for this interval.
Lang Hames6699fb22009-08-06 23:32:48 +0000656 problemNodes[node] =
657 problem.addNode(
658 buildCostVector(li->reg, allowedSets[node], coalesces, spillCost));
Evan Chengb1290a62008-10-02 18:29:27 +0000659
660 }
661
Lang Hames27601ef2008-11-16 12:12:54 +0000662
Evan Chengb1290a62008-10-02 18:29:27 +0000663 // Now add the cost matrices...
664 for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) {
Evan Chengb1290a62008-10-02 18:29:27 +0000665 const LiveInterval *li = node2LI[node1];
666
Evan Chengb1290a62008-10-02 18:29:27 +0000667 // Test for live range overlaps and insert interference matrices.
668 for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) {
669 const LiveInterval *li2 = node2LI[node2];
670
Lang Hames27601ef2008-11-16 12:12:54 +0000671 CoalesceMap::const_iterator cmItr =
672 coalesces.find(RegPair(li->reg, li2->reg));
Evan Chengb1290a62008-10-02 18:29:27 +0000673
Lang Hames6699fb22009-08-06 23:32:48 +0000674 PBQP::Matrix *m = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000675
Lang Hames27601ef2008-11-16 12:12:54 +0000676 if (cmItr != coalesces.end()) {
677 m = buildCoalescingMatrix(allowedSets[node1], allowedSets[node2],
678 cmItr->second);
679 }
680 else if (li->overlaps(*li2)) {
681 m = buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]);
682 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000683
Lang Hames27601ef2008-11-16 12:12:54 +0000684 if (m != 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000685 problem.addEdge(problemNodes[node1],
686 problemNodes[node2],
687 *m);
688
Lang Hames27601ef2008-11-16 12:12:54 +0000689 delete m;
Evan Chengb1290a62008-10-02 18:29:27 +0000690 }
691 }
692 }
693
Lang Hames6699fb22009-08-06 23:32:48 +0000694 assert(problem.getNumNodes() == allowedSets.size());
Lang Hames6699fb22009-08-06 23:32:48 +0000695/*
696 std::cerr << "Allocating for " << problem.getNumNodes() << " nodes, "
697 << problem.getNumEdges() << " edges.\n";
698
699 problem.printDot(std::cerr);
700*/
Evan Chengb1290a62008-10-02 18:29:27 +0000701 // We're done, PBQP problem constructed - return it.
Lang Hames6699fb22009-08-06 23:32:48 +0000702 return problem;
Evan Chengb1290a62008-10-02 18:29:27 +0000703}
704
Evan Chengc781a242009-05-03 18:32:42 +0000705void PBQPRegAlloc::addStackInterval(const LiveInterval *spilled,
706 MachineRegisterInfo* mri) {
Lang Hames27601ef2008-11-16 12:12:54 +0000707 int stackSlot = vrm->getStackSlot(spilled->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000708
709 if (stackSlot == VirtRegMap::NO_STACK_SLOT)
Lang Hames27601ef2008-11-16 12:12:54 +0000710 return;
711
Evan Chengc781a242009-05-03 18:32:42 +0000712 const TargetRegisterClass *RC = mri->getRegClass(spilled->reg);
713 LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC);
Lang Hames27601ef2008-11-16 12:12:54 +0000714
715 VNInfo *vni;
716 if (stackInterval.getNumValNums() != 0)
717 vni = stackInterval.getValNumInfo(0);
718 else
Lang Hames86511252009-09-04 20:41:11 +0000719 vni = stackInterval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +0000720 SlotIndex(), 0, false, lss->getVNInfoAllocator());
Lang Hames27601ef2008-11-16 12:12:54 +0000721
722 LiveInterval &rhsInterval = lis->getInterval(spilled->reg);
723 stackInterval.MergeRangesInAsValue(rhsInterval, vni);
724}
725
Lang Hames6699fb22009-08-06 23:32:48 +0000726bool PBQPRegAlloc::mapPBQPToRegAlloc(const PBQP::Solution &solution) {
Lang Hamese98b4b02009-11-15 04:39:51 +0000727
Evan Chengb1290a62008-10-02 18:29:27 +0000728 // Set to true if we have any spills
729 bool anotherRoundNeeded = false;
730
731 // Clear the existing allocation.
732 vrm->clearAllVirt();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000733
Evan Chengb1290a62008-10-02 18:29:27 +0000734 // Iterate over the nodes mapping the PBQP solution to a register assignment.
735 for (unsigned node = 0; node < node2LI.size(); ++node) {
Lang Hames27601ef2008-11-16 12:12:54 +0000736 unsigned virtReg = node2LI[node]->reg,
Lang Hames030c4bf2010-01-26 04:49:58 +0000737 allocSelection = solution.getSelection(problemNodes[node]);
Lang Hames6699fb22009-08-06 23:32:48 +0000738
Evan Chengb1290a62008-10-02 18:29:27 +0000739
740 // If the PBQP solution is non-zero it's a physical register...
741 if (allocSelection != 0) {
742 // Get the physical reg, subtracting 1 to account for the spill option.
743 unsigned physReg = allowedSets[node][allocSelection - 1];
744
David Greene30931542010-01-05 01:25:43 +0000745 DEBUG(dbgs() << "VREG " << virtReg << " -> "
Lang Hames233fd9c2009-08-18 23:34:50 +0000746 << tri->getName(physReg) << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000747
748 assert(physReg != 0);
749
Evan Chengb1290a62008-10-02 18:29:27 +0000750 // Add to the virt reg map and update the used phys regs.
Lang Hames27601ef2008-11-16 12:12:54 +0000751 vrm->assignVirt2Phys(virtReg, physReg);
Evan Chengb1290a62008-10-02 18:29:27 +0000752 }
753 // ...Otherwise it's a spill.
754 else {
755
756 // Make sure we ignore this virtual reg on the next round
757 // of allocation
Lang Hames27601ef2008-11-16 12:12:54 +0000758 vregIntervalsToAlloc.erase(&lis->getInterval(virtReg));
Evan Chengb1290a62008-10-02 18:29:27 +0000759
Evan Chengb1290a62008-10-02 18:29:27 +0000760 // Insert spill ranges for this live range
Lang Hames27601ef2008-11-16 12:12:54 +0000761 const LiveInterval *spillInterval = node2LI[node];
762 double oldSpillWeight = spillInterval->weight;
Evan Chengb1290a62008-10-02 18:29:27 +0000763 SmallVector<LiveInterval*, 8> spillIs;
764 std::vector<LiveInterval*> newSpills =
Evan Chengc781a242009-05-03 18:32:42 +0000765 lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm);
766 addStackInterval(spillInterval, mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000767
Daniel Dunbarbc84ad92009-08-20 20:01:34 +0000768 (void) oldSpillWeight;
David Greene30931542010-01-05 01:25:43 +0000769 DEBUG(dbgs() << "VREG " << virtReg << " -> SPILLED (Cost: "
Lang Hames233fd9c2009-08-18 23:34:50 +0000770 << oldSpillWeight << ", New vregs: ");
Lang Hames27601ef2008-11-16 12:12:54 +0000771
772 // Copy any newly inserted live intervals into the list of regs to
773 // allocate.
774 for (std::vector<LiveInterval*>::const_iterator
775 itr = newSpills.begin(), end = newSpills.end();
776 itr != end; ++itr) {
777
778 assert(!(*itr)->empty() && "Empty spill range.");
779
David Greene30931542010-01-05 01:25:43 +0000780 DEBUG(dbgs() << (*itr)->reg << " ");
Lang Hames27601ef2008-11-16 12:12:54 +0000781
782 vregIntervalsToAlloc.insert(*itr);
783 }
784
David Greene30931542010-01-05 01:25:43 +0000785 DEBUG(dbgs() << ")\n");
Evan Chengb1290a62008-10-02 18:29:27 +0000786
787 // We need another round if spill intervals were added.
788 anotherRoundNeeded |= !newSpills.empty();
789 }
790 }
791
792 return !anotherRoundNeeded;
793}
794
Lang Hames27601ef2008-11-16 12:12:54 +0000795void PBQPRegAlloc::finalizeAlloc() const {
796 typedef LiveIntervals::iterator LIIterator;
797 typedef LiveInterval::Ranges::const_iterator LRIterator;
798
799 // First allocate registers for the empty intervals.
Argyrios Kyrtzidis3713c0b2008-11-19 12:56:21 +0000800 for (LiveIntervalSet::const_iterator
Daniel Dunbara279bc32009-09-20 02:20:51 +0000801 itr = emptyVRegIntervals.begin(), end = emptyVRegIntervals.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000802 itr != end; ++itr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000803 LiveInterval *li = *itr;
Lang Hames27601ef2008-11-16 12:12:54 +0000804
Evan Cheng90f95f82009-06-14 20:22:55 +0000805 unsigned physReg = vrm->getRegAllocPref(li->reg);
Lang Hames6699fb22009-08-06 23:32:48 +0000806
Lang Hames27601ef2008-11-16 12:12:54 +0000807 if (physReg == 0) {
808 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000809 physReg = *liRC->allocation_order_begin(*mf);
Lang Hames27601ef2008-11-16 12:12:54 +0000810 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000811
812 vrm->assignVirt2Phys(li->reg, physReg);
Lang Hames27601ef2008-11-16 12:12:54 +0000813 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000814
Lang Hames27601ef2008-11-16 12:12:54 +0000815 // Finally iterate over the basic blocks to compute and set the live-in sets.
816 SmallVector<MachineBasicBlock*, 8> liveInMBBs;
817 MachineBasicBlock *entryMBB = &*mf->begin();
818
819 for (LIIterator liItr = lis->begin(), liEnd = lis->end();
820 liItr != liEnd; ++liItr) {
821
822 const LiveInterval *li = liItr->second;
823 unsigned reg = 0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000824
Lang Hames27601ef2008-11-16 12:12:54 +0000825 // Get the physical register for this interval
826 if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
827 reg = li->reg;
828 }
829 else if (vrm->isAssignedReg(li->reg)) {
830 reg = vrm->getPhys(li->reg);
831 }
832 else {
833 // Ranges which are assigned a stack slot only are ignored.
834 continue;
835 }
836
Lang Hamesb0e519f2009-05-17 23:50:36 +0000837 if (reg == 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000838 // Filter out zero regs - they're for intervals that were spilled.
Lang Hamesb0e519f2009-05-17 23:50:36 +0000839 continue;
840 }
841
Lang Hames27601ef2008-11-16 12:12:54 +0000842 // Iterate over the ranges of the current interval...
843 for (LRIterator lrItr = li->begin(), lrEnd = li->end();
844 lrItr != lrEnd; ++lrItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000845
Lang Hames27601ef2008-11-16 12:12:54 +0000846 // Find the set of basic blocks which this range is live into...
847 if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
848 // And add the physreg for this interval to their live-in sets.
849 for (unsigned i = 0; i < liveInMBBs.size(); ++i) {
850 if (liveInMBBs[i] != entryMBB) {
851 if (!liveInMBBs[i]->isLiveIn(reg)) {
852 liveInMBBs[i]->addLiveIn(reg);
853 }
854 }
855 }
856 liveInMBBs.clear();
857 }
858 }
859 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000860
Lang Hames27601ef2008-11-16 12:12:54 +0000861}
862
Evan Chengb1290a62008-10-02 18:29:27 +0000863bool PBQPRegAlloc::runOnMachineFunction(MachineFunction &MF) {
Lang Hames27601ef2008-11-16 12:12:54 +0000864
Evan Chengb1290a62008-10-02 18:29:27 +0000865 mf = &MF;
866 tm = &mf->getTarget();
867 tri = tm->getRegisterInfo();
Lang Hames27601ef2008-11-16 12:12:54 +0000868 tii = tm->getInstrInfo();
Lang Hames233a60e2009-11-03 23:52:08 +0000869 mri = &mf->getRegInfo();
Evan Chengb1290a62008-10-02 18:29:27 +0000870
Lang Hames27601ef2008-11-16 12:12:54 +0000871 lis = &getAnalysis<LiveIntervals>();
872 lss = &getAnalysis<LiveStacks>();
Evan Chengb1290a62008-10-02 18:29:27 +0000873 loopInfo = &getAnalysis<MachineLoopInfo>();
Lang Hamesc4bcc772010-07-20 07:41:44 +0000874 RenderMachineFunction *rmf = &getAnalysis<RenderMachineFunction>();
Evan Chengb1290a62008-10-02 18:29:27 +0000875
Owen Anderson49c8aa02009-03-13 05:55:11 +0000876 vrm = &getAnalysis<VirtRegMap>();
Evan Chengb1290a62008-10-02 18:29:27 +0000877
Lang Hames54cc2ef2010-07-19 15:22:28 +0000878
Lang Hames030c4bf2010-01-26 04:49:58 +0000879 DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000880
Evan Chengb1290a62008-10-02 18:29:27 +0000881 // Allocator main loop:
Misha Brukman2a835f92009-01-08 15:50:22 +0000882 //
Evan Chengb1290a62008-10-02 18:29:27 +0000883 // * Map current regalloc problem to a PBQP problem
884 // * Solve the PBQP problem
885 // * Map the solution back to a register allocation
886 // * Spill if necessary
Misha Brukman2a835f92009-01-08 15:50:22 +0000887 //
Evan Chengb1290a62008-10-02 18:29:27 +0000888 // This process is continued till no more spills are generated.
889
Lang Hames27601ef2008-11-16 12:12:54 +0000890 // Find the vreg intervals in need of allocation.
891 findVRegIntervalsToAlloc();
Misha Brukman2a835f92009-01-08 15:50:22 +0000892
Lang Hames27601ef2008-11-16 12:12:54 +0000893 // If there are non-empty intervals allocate them using pbqp.
894 if (!vregIntervalsToAlloc.empty()) {
Evan Chengb1290a62008-10-02 18:29:27 +0000895
Lang Hames27601ef2008-11-16 12:12:54 +0000896 bool pbqpAllocComplete = false;
897 unsigned round = 0;
898
899 while (!pbqpAllocComplete) {
David Greene30931542010-01-05 01:25:43 +0000900 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000901
Lang Hames030c4bf2010-01-26 04:49:58 +0000902 PBQP::Graph problem = constructPBQPProblem();
903 PBQP::Solution solution =
904 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(problem);
Lang Hames233fd9c2009-08-18 23:34:50 +0000905
Lang Hames6699fb22009-08-06 23:32:48 +0000906 pbqpAllocComplete = mapPBQPToRegAlloc(solution);
Lang Hames27601ef2008-11-16 12:12:54 +0000907
908 ++round;
909 }
Evan Chengb1290a62008-10-02 18:29:27 +0000910 }
911
Lang Hames27601ef2008-11-16 12:12:54 +0000912 // Finalise allocation, allocate empty ranges.
913 finalizeAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000914
Lang Hamesc4bcc772010-07-20 07:41:44 +0000915 rmf->renderMachineFunction("After PBQP register allocation.", vrm);
916
Lang Hames27601ef2008-11-16 12:12:54 +0000917 vregIntervalsToAlloc.clear();
918 emptyVRegIntervals.clear();
919 li2Node.clear();
920 node2LI.clear();
921 allowedSets.clear();
Lang Hames030c4bf2010-01-26 04:49:58 +0000922 problemNodes.clear();
Lang Hames27601ef2008-11-16 12:12:54 +0000923
David Greene30931542010-01-05 01:25:43 +0000924 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000925
Lang Hames87e3bca2009-05-06 02:36:21 +0000926 // Run rewriter
927 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
928
929 rewriter->runOnMachineFunction(*mf, *vrm, lis);
Lang Hames27601ef2008-11-16 12:12:54 +0000930
Misha Brukman2a835f92009-01-08 15:50:22 +0000931 return true;
Evan Chengb1290a62008-10-02 18:29:27 +0000932}
933
934FunctionPass* llvm::createPBQPRegisterAllocator() {
935 return new PBQPRegAlloc();
936}
937
938
939#undef DEBUG_TYPE