blob: 61f337bab49c421538f2f7e21d902c91b024880c [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;
Lang Hames33198392010-09-02 08:27:00 +0000143 RenderMachineFunction *rmf;
Evan Chengb1290a62008-10-02 18:29:27 +0000144
Lang Hames27601ef2008-11-16 12:12:54 +0000145 LiveIntervals *lis;
146 LiveStacks *lss;
Evan Chengb1290a62008-10-02 18:29:27 +0000147 VirtRegMap *vrm;
148
149 LI2NodeMap li2Node;
150 Node2LIMap node2LI;
151 AllowedSetMap allowedSets;
Lang Hames27601ef2008-11-16 12:12:54 +0000152 LiveIntervalSet vregIntervalsToAlloc,
153 emptyVRegIntervals;
Lang Hames030c4bf2010-01-26 04:49:58 +0000154 NodeVector problemNodes;
Evan Chengb1290a62008-10-02 18:29:27 +0000155
Misha Brukman2a835f92009-01-08 15:50:22 +0000156
Lang Hames6699fb22009-08-06 23:32:48 +0000157 /// Builds a PBQP cost vector.
Lang Hames27601ef2008-11-16 12:12:54 +0000158 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000159 PBQP::Vector buildCostVector(unsigned vReg,
160 const RegContainer &allowed,
161 const CoalesceMap &cealesces,
162 PBQP::PBQPNum spillCost) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000163
Lang Hames6699fb22009-08-06 23:32:48 +0000164 /// \brief Builds a PBQP interference matrix.
165 ///
166 /// @return Either a pointer to a non-zero PBQP matrix representing the
167 /// allocation option costs, or a null pointer for a zero matrix.
168 ///
169 /// Expects allowed sets for two interfering LiveIntervals. These allowed
170 /// sets should contain only allocable registers from the LiveInterval's
171 /// register class, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000172 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000173 PBQP::Matrix* buildInterferenceMatrix(const RegContainer &allowed1,
174 const RegContainer &allowed2) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000175
Lang Hames6699fb22009-08-06 23:32:48 +0000176 ///
177 /// Expects allowed sets for two potentially coalescable LiveIntervals,
178 /// and an estimated benefit due to coalescing. The allowed sets should
179 /// contain only allocable registers from the LiveInterval's register
180 /// classes, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000181 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000182 PBQP::Matrix* buildCoalescingMatrix(const RegContainer &allowed1,
183 const RegContainer &allowed2,
184 PBQP::PBQPNum cBenefit) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000185
Lang Hames6699fb22009-08-06 23:32:48 +0000186 /// \brief Finds coalescing opportunities and returns them as a map.
187 ///
188 /// Any entries in the map are guaranteed coalescable, even if their
189 /// corresponding live intervals overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000190 CoalesceMap findCoalesces();
Evan Chengb1290a62008-10-02 18:29:27 +0000191
Lang Hames6699fb22009-08-06 23:32:48 +0000192 /// \brief Finds the initial set of vreg intervals to allocate.
Lang Hames27601ef2008-11-16 12:12:54 +0000193 void findVRegIntervalsToAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000194
Lang Hames6699fb22009-08-06 23:32:48 +0000195 /// \brief Constructs a PBQP problem representation of the register
196 /// allocation problem for this function.
197 ///
198 /// @return a PBQP solver object for the register allocation problem.
Lang Hames030c4bf2010-01-26 04:49:58 +0000199 PBQP::Graph constructPBQPProblem();
Evan Chengb1290a62008-10-02 18:29:27 +0000200
Lang Hames6699fb22009-08-06 23:32:48 +0000201 /// \brief Adds a stack interval if the given live interval has been
202 /// spilled. Used to support stack slot coloring.
Evan Chengc781a242009-05-03 18:32:42 +0000203 void addStackInterval(const LiveInterval *spilled,MachineRegisterInfo* mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000204
Lang Hames6699fb22009-08-06 23:32:48 +0000205 /// \brief Given a solved PBQP problem maps this solution back to a register
206 /// assignment.
207 bool mapPBQPToRegAlloc(const PBQP::Solution &solution);
Evan Chengb1290a62008-10-02 18:29:27 +0000208
Lang Hames6699fb22009-08-06 23:32:48 +0000209 /// \brief Postprocessing before final spilling. Sets basic block "live in"
210 /// variables.
Lang Hames27601ef2008-11-16 12:12:54 +0000211 void finalizeAlloc() const;
212
Evan Chengb1290a62008-10-02 18:29:27 +0000213 };
214
215 char PBQPRegAlloc::ID = 0;
216}
217
218
Lang Hames27601ef2008-11-16 12:12:54 +0000219template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000220PBQP::Vector PBQPRegAlloc::buildCostVector(unsigned vReg,
221 const RegContainer &allowed,
222 const CoalesceMap &coalesces,
223 PBQP::PBQPNum spillCost) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000224
Lang Hames27601ef2008-11-16 12:12:54 +0000225 typedef typename RegContainer::const_iterator AllowedItr;
226
Evan Chengb1290a62008-10-02 18:29:27 +0000227 // Allocate vector. Additional element (0th) used for spill option
Lang Hames6699fb22009-08-06 23:32:48 +0000228 PBQP::Vector v(allowed.size() + 1, 0);
Evan Chengb1290a62008-10-02 18:29:27 +0000229
Lang Hames6699fb22009-08-06 23:32:48 +0000230 v[0] = spillCost;
Evan Chengb1290a62008-10-02 18:29:27 +0000231
Lang Hames27601ef2008-11-16 12:12:54 +0000232 // Iterate over the allowed registers inserting coalesce benefits if there
233 // are any.
234 unsigned ai = 0;
235 for (AllowedItr itr = allowed.begin(), end = allowed.end();
236 itr != end; ++itr, ++ai) {
237
238 unsigned pReg = *itr;
239
240 CoalesceMap::const_iterator cmItr =
241 coalesces.find(RegPair(vReg, pReg));
242
243 // No coalesce - on to the next preg.
244 if (cmItr == coalesces.end())
245 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000246
247 // We have a coalesce - insert the benefit.
Lang Hames6699fb22009-08-06 23:32:48 +0000248 v[ai + 1] = -cmItr->second;
Lang Hames27601ef2008-11-16 12:12:54 +0000249 }
250
Evan Chengb1290a62008-10-02 18:29:27 +0000251 return v;
252}
253
Lang Hames27601ef2008-11-16 12:12:54 +0000254template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000255PBQP::Matrix* PBQPRegAlloc::buildInterferenceMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000256 const RegContainer &allowed1, const RegContainer &allowed2) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000257
Lang Hames27601ef2008-11-16 12:12:54 +0000258 typedef typename RegContainer::const_iterator RegContainerIterator;
Evan Chengb1290a62008-10-02 18:29:27 +0000259
260 // Construct a PBQP matrix representing the cost of allocation options. The
261 // rows and columns correspond to the allocation options for the two live
262 // intervals. Elements will be infinite where corresponding registers alias,
263 // since we cannot allocate aliasing registers to interfering live intervals.
264 // All other elements (non-aliasing combinations) will have zero cost. Note
265 // that the spill option (element 0,0) has zero cost, since we can allocate
266 // both intervals to memory safely (the cost for each individual allocation
267 // to memory is accounted for by the cost vectors for each live interval).
Lang Hames6699fb22009-08-06 23:32:48 +0000268 PBQP::Matrix *m =
269 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Misha Brukman2a835f92009-01-08 15:50:22 +0000270
Evan Chengb1290a62008-10-02 18:29:27 +0000271 // Assume this is a zero matrix until proven otherwise. Zero matrices occur
272 // between interfering live ranges with non-overlapping register sets (e.g.
273 // non-overlapping reg classes, or disjoint sets of allowed regs within the
274 // same class). The term "overlapping" is used advisedly: sets which do not
275 // intersect, but contain registers which alias, will have non-zero matrices.
276 // We optimize zero matrices away to improve solver speed.
277 bool isZeroMatrix = true;
278
279
280 // Row index. Starts at 1, since the 0th row is for the spill option, which
281 // is always zero.
Misha Brukman2a835f92009-01-08 15:50:22 +0000282 unsigned ri = 1;
Evan Chengb1290a62008-10-02 18:29:27 +0000283
Misha Brukman2a835f92009-01-08 15:50:22 +0000284 // Iterate over allowed sets, insert infinities where required.
Lang Hames27601ef2008-11-16 12:12:54 +0000285 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000286 a1Itr != a1End; ++a1Itr) {
287
288 // Column index, starts at 1 as for row index.
289 unsigned ci = 1;
290 unsigned reg1 = *a1Itr;
291
Lang Hames27601ef2008-11-16 12:12:54 +0000292 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000293 a2Itr != a2End; ++a2Itr) {
294
295 unsigned reg2 = *a2Itr;
296
297 // If the row/column regs are identical or alias insert an infinity.
Lang Hames3f2f3f52009-09-03 02:52:02 +0000298 if (tri->regsOverlap(reg1, reg2)) {
Lang Hames6699fb22009-08-06 23:32:48 +0000299 (*m)[ri][ci] = std::numeric_limits<PBQP::PBQPNum>::infinity();
Evan Chengb1290a62008-10-02 18:29:27 +0000300 isZeroMatrix = false;
301 }
302
303 ++ci;
304 }
305
306 ++ri;
307 }
308
309 // If this turns out to be a zero matrix...
310 if (isZeroMatrix) {
311 // free it and return null.
312 delete m;
313 return 0;
314 }
315
316 // ...otherwise return the cost matrix.
317 return m;
318}
319
Lang Hames27601ef2008-11-16 12:12:54 +0000320template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000321PBQP::Matrix* PBQPRegAlloc::buildCoalescingMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000322 const RegContainer &allowed1, const RegContainer &allowed2,
Lang Hames6699fb22009-08-06 23:32:48 +0000323 PBQP::PBQPNum cBenefit) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000324
Lang Hames27601ef2008-11-16 12:12:54 +0000325 typedef typename RegContainer::const_iterator RegContainerIterator;
326
327 // Construct a PBQP Matrix representing the benefits of coalescing. As with
328 // interference matrices the rows and columns represent allowed registers
329 // for the LiveIntervals which are (potentially) to be coalesced. The amount
330 // -cBenefit will be placed in any element representing the same register
331 // for both intervals.
Lang Hames6699fb22009-08-06 23:32:48 +0000332 PBQP::Matrix *m =
333 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Lang Hames27601ef2008-11-16 12:12:54 +0000334
335 // Reset costs to zero.
336 m->reset(0);
337
338 // Assume the matrix is zero till proven otherwise. Zero matrices will be
339 // optimized away as in the interference case.
340 bool isZeroMatrix = true;
341
342 // Row index. Starts at 1, since the 0th row is for the spill option, which
343 // is always zero.
344 unsigned ri = 1;
345
346 // Iterate over the allowed sets, insert coalescing benefits where
347 // appropriate.
348 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
349 a1Itr != a1End; ++a1Itr) {
350
351 // Column index, starts at 1 as for row index.
352 unsigned ci = 1;
353 unsigned reg1 = *a1Itr;
354
355 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
356 a2Itr != a2End; ++a2Itr) {
357
358 // If the row and column represent the same register insert a beneficial
359 // cost to preference this allocation - it would allow us to eliminate a
Misha Brukman2a835f92009-01-08 15:50:22 +0000360 // move instruction.
Lang Hames27601ef2008-11-16 12:12:54 +0000361 if (reg1 == *a2Itr) {
362 (*m)[ri][ci] = -cBenefit;
363 isZeroMatrix = false;
364 }
365
366 ++ci;
367 }
368
369 ++ri;
370 }
371
372 // If this turns out to be a zero matrix...
373 if (isZeroMatrix) {
374 // ...free it and return null.
375 delete m;
376 return 0;
377 }
378
379 return m;
380}
381
382PBQPRegAlloc::CoalesceMap PBQPRegAlloc::findCoalesces() {
383
384 typedef MachineFunction::const_iterator MFIterator;
385 typedef MachineBasicBlock::const_iterator MBBIterator;
386 typedef LiveInterval::const_vni_iterator VNIIterator;
Misha Brukman2a835f92009-01-08 15:50:22 +0000387
Lang Hames27601ef2008-11-16 12:12:54 +0000388 CoalesceMap coalescesFound;
389
390 // To find coalesces we need to iterate over the function looking for
391 // copy instructions.
392 for (MFIterator bbItr = mf->begin(), bbEnd = mf->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000393 bbItr != bbEnd; ++bbItr) {
394
395 const MachineBasicBlock *mbb = &*bbItr;
Evan Chengb1290a62008-10-02 18:29:27 +0000396
Lang Hames27601ef2008-11-16 12:12:54 +0000397 for (MBBIterator iItr = mbb->begin(), iEnd = mbb->end();
398 iItr != iEnd; ++iItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000399
400 const MachineInstr *instr = &*iItr;
401
Lang Hames27601ef2008-11-16 12:12:54 +0000402 // If this isn't a copy then continue to the next instruction.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000403 if (!instr->isCopy())
Lang Hames27601ef2008-11-16 12:12:54 +0000404 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000405
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000406 unsigned srcReg = instr->getOperand(1).getReg();
407 unsigned dstReg = instr->getOperand(0).getReg();
408
Lang Hames27601ef2008-11-16 12:12:54 +0000409 // If the registers are already the same our job is nice and easy.
410 if (dstReg == srcReg)
411 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000412
Lang Hames27601ef2008-11-16 12:12:54 +0000413 bool srcRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(srcReg),
414 dstRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(dstReg);
415
416 // If both registers are physical then we can't coalesce.
417 if (srcRegIsPhysical && dstRegIsPhysical)
418 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000419
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000420 // If it's a copy that includes two virtual register but the source and
421 // destination classes differ then we can't coalesce.
422 if (!srcRegIsPhysical && !dstRegIsPhysical &&
423 mri->getRegClass(srcReg) != mri->getRegClass(dstReg))
Lang Hames27601ef2008-11-16 12:12:54 +0000424 continue;
425
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000426 // If one is physical and one is virtual, check that the physical is
427 // allocatable in the class of the virtual.
428 if (srcRegIsPhysical && !dstRegIsPhysical) {
429 const TargetRegisterClass *dstRegClass = mri->getRegClass(dstReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000430 if (std::find(dstRegClass->allocation_order_begin(*mf),
431 dstRegClass->allocation_order_end(*mf), srcReg) ==
432 dstRegClass->allocation_order_end(*mf))
Evan Chengb1290a62008-10-02 18:29:27 +0000433 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000434 }
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000435 if (!srcRegIsPhysical && dstRegIsPhysical) {
436 const TargetRegisterClass *srcRegClass = mri->getRegClass(srcReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000437 if (std::find(srcRegClass->allocation_order_begin(*mf),
438 srcRegClass->allocation_order_end(*mf), dstReg) ==
439 srcRegClass->allocation_order_end(*mf))
Lang Hames27601ef2008-11-16 12:12:54 +0000440 continue;
441 }
442
443 // If we've made it here we have a copy with compatible register classes.
Misha Brukman2a835f92009-01-08 15:50:22 +0000444 // We can probably coalesce, but we need to consider overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000445 const LiveInterval *srcLI = &lis->getInterval(srcReg),
446 *dstLI = &lis->getInterval(dstReg);
447
448 if (srcLI->overlaps(*dstLI)) {
449 // Even in the case of an overlap we might still be able to coalesce,
450 // but we need to make sure that no definition of either range occurs
451 // while the other range is live.
452
453 // Otherwise start by assuming we're ok.
454 bool badDef = false;
455
456 // Test all defs of the source range.
Misha Brukman2a835f92009-01-08 15:50:22 +0000457 for (VNIIterator
Lang Hames27601ef2008-11-16 12:12:54 +0000458 vniItr = srcLI->vni_begin(), vniEnd = srcLI->vni_end();
459 vniItr != vniEnd; ++vniItr) {
460
Lang Hames0b23dc02010-02-09 00:50:27 +0000461 // If we find a poorly defined def we err on the side of caution.
462 if (!(*vniItr)->def.isValid()) {
463 badDef = true;
464 break;
465 }
466
Lang Hames27601ef2008-11-16 12:12:54 +0000467 // If we find a def that kills the coalescing opportunity then
468 // record it and break from the loop.
469 if (dstLI->liveAt((*vniItr)->def)) {
470 badDef = true;
471 break;
472 }
473 }
474
475 // If we have a bad def give up, continue to the next instruction.
476 if (badDef)
477 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000478
Lang Hames27601ef2008-11-16 12:12:54 +0000479 // Otherwise test definitions of the destination range.
480 for (VNIIterator
481 vniItr = dstLI->vni_begin(), vniEnd = dstLI->vni_end();
482 vniItr != vniEnd; ++vniItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000483
Lang Hames27601ef2008-11-16 12:12:54 +0000484 // We want to make sure we skip the copy instruction itself.
Lang Hames52c1afc2009-08-10 23:43:28 +0000485 if ((*vniItr)->getCopy() == instr)
Lang Hames27601ef2008-11-16 12:12:54 +0000486 continue;
487
Lang Hames0b23dc02010-02-09 00:50:27 +0000488 if (!(*vniItr)->def.isValid()) {
489 badDef = true;
490 break;
491 }
492
Lang Hames27601ef2008-11-16 12:12:54 +0000493 if (srcLI->liveAt((*vniItr)->def)) {
494 badDef = true;
495 break;
496 }
497 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000498
Lang Hames27601ef2008-11-16 12:12:54 +0000499 // As before a bad def we give up and continue to the next instr.
500 if (badDef)
501 continue;
502 }
503
504 // If we make it to here then either the ranges didn't overlap, or they
505 // did, but none of their definitions would prevent us from coalescing.
506 // We're good to go with the coalesce.
507
Chris Lattner87565c12010-05-15 17:10:24 +0000508 float cBenefit = std::pow(10.0f, (float)loopInfo->getLoopDepth(mbb)) / 5.0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000509
Lang Hames27601ef2008-11-16 12:12:54 +0000510 coalescesFound[RegPair(srcReg, dstReg)] = cBenefit;
511 coalescesFound[RegPair(dstReg, srcReg)] = cBenefit;
Evan Chengb1290a62008-10-02 18:29:27 +0000512 }
513
514 }
515
Lang Hames27601ef2008-11-16 12:12:54 +0000516 return coalescesFound;
517}
518
519void PBQPRegAlloc::findVRegIntervalsToAlloc() {
520
521 // Iterate over all live ranges.
522 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
523 itr != end; ++itr) {
524
525 // Ignore physical ones.
526 if (TargetRegisterInfo::isPhysicalRegister(itr->first))
527 continue;
528
529 LiveInterval *li = itr->second;
530
531 // If this live interval is non-empty we will use pbqp to allocate it.
532 // Empty intervals we allocate in a simple post-processing stage in
533 // finalizeAlloc.
534 if (!li->empty()) {
535 vregIntervalsToAlloc.insert(li);
536 }
537 else {
538 emptyVRegIntervals.insert(li);
539 }
540 }
Evan Chengb1290a62008-10-02 18:29:27 +0000541}
542
Lang Hames030c4bf2010-01-26 04:49:58 +0000543PBQP::Graph PBQPRegAlloc::constructPBQPProblem() {
Evan Chengb1290a62008-10-02 18:29:27 +0000544
545 typedef std::vector<const LiveInterval*> LIVector;
Lang Hames27601ef2008-11-16 12:12:54 +0000546 typedef std::vector<unsigned> RegVector;
Evan Chengb1290a62008-10-02 18:29:27 +0000547
Lang Hames27601ef2008-11-16 12:12:54 +0000548 // This will store the physical intervals for easy reference.
549 LIVector physIntervals;
Evan Chengb1290a62008-10-02 18:29:27 +0000550
551 // Start by clearing the old node <-> live interval mappings & allowed sets
552 li2Node.clear();
553 node2LI.clear();
554 allowedSets.clear();
555
Lang Hames27601ef2008-11-16 12:12:54 +0000556 // Populate physIntervals, update preg use:
557 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000558 itr != end; ++itr) {
559
Evan Chengb1290a62008-10-02 18:29:27 +0000560 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
561 physIntervals.push_back(itr->second);
562 mri->setPhysRegUsed(itr->second->reg);
563 }
Evan Chengb1290a62008-10-02 18:29:27 +0000564 }
565
Lang Hames27601ef2008-11-16 12:12:54 +0000566 // Iterate over vreg intervals, construct live interval <-> node number
567 // mappings.
Misha Brukman2a835f92009-01-08 15:50:22 +0000568 for (LiveIntervalSet::const_iterator
Lang Hames27601ef2008-11-16 12:12:54 +0000569 itr = vregIntervalsToAlloc.begin(), end = vregIntervalsToAlloc.end();
570 itr != end; ++itr) {
571 const LiveInterval *li = *itr;
572
573 li2Node[li] = node2LI.size();
574 node2LI.push_back(li);
575 }
576
577 // Get the set of potential coalesces.
Lang Hames8481e3b2009-08-19 01:36:14 +0000578 CoalesceMap coalesces;
579
580 if (pbqpCoalescing) {
581 coalesces = findCoalesces();
582 }
Evan Chengb1290a62008-10-02 18:29:27 +0000583
584 // Construct a PBQP solver for this problem
Lang Hames030c4bf2010-01-26 04:49:58 +0000585 PBQP::Graph problem;
586 problemNodes.resize(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000587
588 // Resize allowedSets container appropriately.
Lang Hames27601ef2008-11-16 12:12:54 +0000589 allowedSets.resize(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000590
Jim Grosbach269354e2010-09-01 21:23:03 +0000591 BitVector ReservedRegs = tri->getReservedRegs(*mf);
592
Evan Chengb1290a62008-10-02 18:29:27 +0000593 // Iterate over virtual register intervals to compute allowed sets...
594 for (unsigned node = 0; node < node2LI.size(); ++node) {
595
596 // Grab pointers to the interval and its register class.
597 const LiveInterval *li = node2LI[node];
598 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000599
Evan Chengb1290a62008-10-02 18:29:27 +0000600 // Start by assuming all allocable registers in the class are allowed...
Jim Grosbach269354e2010-09-01 21:23:03 +0000601 RegVector liAllowed;
602 TargetRegisterClass::iterator aob = liRC->allocation_order_begin(*mf);
603 TargetRegisterClass::iterator aoe = liRC->allocation_order_end(*mf);
604 for (TargetRegisterClass::iterator it = aob; it != aoe; ++it)
605 if (!ReservedRegs.test(*it))
606 liAllowed.push_back(*it);
Evan Chengb1290a62008-10-02 18:29:27 +0000607
Lang Hames27601ef2008-11-16 12:12:54 +0000608 // Eliminate the physical registers which overlap with this range, along
609 // with all their aliases.
610 for (LIVector::iterator pItr = physIntervals.begin(),
611 pEnd = physIntervals.end(); pItr != pEnd; ++pItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000612
Lang Hames27601ef2008-11-16 12:12:54 +0000613 if (!li->overlaps(**pItr))
614 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000615
Lang Hames27601ef2008-11-16 12:12:54 +0000616 unsigned pReg = (*pItr)->reg;
Evan Chengb1290a62008-10-02 18:29:27 +0000617
Lang Hames27601ef2008-11-16 12:12:54 +0000618 // If we get here then the live intervals overlap, but we're still ok
619 // if they're coalescable.
620 if (coalesces.find(RegPair(li->reg, pReg)) != coalesces.end())
621 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000622
Lang Hames27601ef2008-11-16 12:12:54 +0000623 // If we get here then we have a genuine exclusion.
Evan Chengb1290a62008-10-02 18:29:27 +0000624
Lang Hames27601ef2008-11-16 12:12:54 +0000625 // Remove the overlapping reg...
626 RegVector::iterator eraseItr =
627 std::find(liAllowed.begin(), liAllowed.end(), pReg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000628
Lang Hames27601ef2008-11-16 12:12:54 +0000629 if (eraseItr != liAllowed.end())
630 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000631
Lang Hames27601ef2008-11-16 12:12:54 +0000632 const unsigned *aliasItr = tri->getAliasSet(pReg);
633
634 if (aliasItr != 0) {
635 // ...and its aliases.
636 for (; *aliasItr != 0; ++aliasItr) {
637 RegVector::iterator eraseItr =
638 std::find(liAllowed.begin(), liAllowed.end(), *aliasItr);
Misha Brukman2a835f92009-01-08 15:50:22 +0000639
Lang Hames27601ef2008-11-16 12:12:54 +0000640 if (eraseItr != liAllowed.end()) {
641 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000642 }
Evan Chengb1290a62008-10-02 18:29:27 +0000643 }
Evan Chengb1290a62008-10-02 18:29:27 +0000644 }
Evan Chengb1290a62008-10-02 18:29:27 +0000645 }
646
647 // Copy the allowed set into a member vector for use when constructing cost
648 // vectors & matrices, and mapping PBQP solutions back to assignments.
649 allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end());
650
651 // Set the spill cost to the interval weight, or epsilon if the
652 // interval weight is zero
Lang Hames6699fb22009-08-06 23:32:48 +0000653 PBQP::PBQPNum spillCost = (li->weight != 0.0) ?
654 li->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000655
656 // Build a cost vector for this interval.
Lang Hames6699fb22009-08-06 23:32:48 +0000657 problemNodes[node] =
658 problem.addNode(
659 buildCostVector(li->reg, allowedSets[node], coalesces, spillCost));
Evan Chengb1290a62008-10-02 18:29:27 +0000660
661 }
662
Lang Hames27601ef2008-11-16 12:12:54 +0000663
Evan Chengb1290a62008-10-02 18:29:27 +0000664 // Now add the cost matrices...
665 for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) {
Evan Chengb1290a62008-10-02 18:29:27 +0000666 const LiveInterval *li = node2LI[node1];
667
Evan Chengb1290a62008-10-02 18:29:27 +0000668 // Test for live range overlaps and insert interference matrices.
669 for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) {
670 const LiveInterval *li2 = node2LI[node2];
671
Lang Hames27601ef2008-11-16 12:12:54 +0000672 CoalesceMap::const_iterator cmItr =
673 coalesces.find(RegPair(li->reg, li2->reg));
Evan Chengb1290a62008-10-02 18:29:27 +0000674
Lang Hames6699fb22009-08-06 23:32:48 +0000675 PBQP::Matrix *m = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000676
Lang Hames27601ef2008-11-16 12:12:54 +0000677 if (cmItr != coalesces.end()) {
678 m = buildCoalescingMatrix(allowedSets[node1], allowedSets[node2],
679 cmItr->second);
680 }
681 else if (li->overlaps(*li2)) {
682 m = buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]);
683 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000684
Lang Hames27601ef2008-11-16 12:12:54 +0000685 if (m != 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000686 problem.addEdge(problemNodes[node1],
687 problemNodes[node2],
688 *m);
689
Lang Hames27601ef2008-11-16 12:12:54 +0000690 delete m;
Evan Chengb1290a62008-10-02 18:29:27 +0000691 }
692 }
693 }
694
Lang Hames6699fb22009-08-06 23:32:48 +0000695 assert(problem.getNumNodes() == allowedSets.size());
Lang Hames6699fb22009-08-06 23:32:48 +0000696/*
697 std::cerr << "Allocating for " << problem.getNumNodes() << " nodes, "
698 << problem.getNumEdges() << " edges.\n";
699
700 problem.printDot(std::cerr);
701*/
Evan Chengb1290a62008-10-02 18:29:27 +0000702 // We're done, PBQP problem constructed - return it.
Lang Hames6699fb22009-08-06 23:32:48 +0000703 return problem;
Evan Chengb1290a62008-10-02 18:29:27 +0000704}
705
Evan Chengc781a242009-05-03 18:32:42 +0000706void PBQPRegAlloc::addStackInterval(const LiveInterval *spilled,
707 MachineRegisterInfo* mri) {
Lang Hames27601ef2008-11-16 12:12:54 +0000708 int stackSlot = vrm->getStackSlot(spilled->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000709
710 if (stackSlot == VirtRegMap::NO_STACK_SLOT)
Lang Hames27601ef2008-11-16 12:12:54 +0000711 return;
712
Evan Chengc781a242009-05-03 18:32:42 +0000713 const TargetRegisterClass *RC = mri->getRegClass(spilled->reg);
714 LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC);
Lang Hames27601ef2008-11-16 12:12:54 +0000715
716 VNInfo *vni;
717 if (stackInterval.getNumValNums() != 0)
718 vni = stackInterval.getValNumInfo(0);
719 else
Lang Hames86511252009-09-04 20:41:11 +0000720 vni = stackInterval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +0000721 SlotIndex(), 0, false, lss->getVNInfoAllocator());
Lang Hames27601ef2008-11-16 12:12:54 +0000722
723 LiveInterval &rhsInterval = lis->getInterval(spilled->reg);
724 stackInterval.MergeRangesInAsValue(rhsInterval, vni);
725}
726
Lang Hames6699fb22009-08-06 23:32:48 +0000727bool PBQPRegAlloc::mapPBQPToRegAlloc(const PBQP::Solution &solution) {
Lang Hamese98b4b02009-11-15 04:39:51 +0000728
Evan Chengb1290a62008-10-02 18:29:27 +0000729 // Set to true if we have any spills
730 bool anotherRoundNeeded = false;
731
732 // Clear the existing allocation.
733 vrm->clearAllVirt();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000734
Evan Chengb1290a62008-10-02 18:29:27 +0000735 // Iterate over the nodes mapping the PBQP solution to a register assignment.
736 for (unsigned node = 0; node < node2LI.size(); ++node) {
Lang Hames27601ef2008-11-16 12:12:54 +0000737 unsigned virtReg = node2LI[node]->reg,
Lang Hames030c4bf2010-01-26 04:49:58 +0000738 allocSelection = solution.getSelection(problemNodes[node]);
Lang Hames6699fb22009-08-06 23:32:48 +0000739
Evan Chengb1290a62008-10-02 18:29:27 +0000740
741 // If the PBQP solution is non-zero it's a physical register...
742 if (allocSelection != 0) {
743 // Get the physical reg, subtracting 1 to account for the spill option.
744 unsigned physReg = allowedSets[node][allocSelection - 1];
745
David Greene30931542010-01-05 01:25:43 +0000746 DEBUG(dbgs() << "VREG " << virtReg << " -> "
Lang Hames233fd9c2009-08-18 23:34:50 +0000747 << tri->getName(physReg) << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000748
749 assert(physReg != 0);
750
Evan Chengb1290a62008-10-02 18:29:27 +0000751 // Add to the virt reg map and update the used phys regs.
Lang Hames27601ef2008-11-16 12:12:54 +0000752 vrm->assignVirt2Phys(virtReg, physReg);
Evan Chengb1290a62008-10-02 18:29:27 +0000753 }
754 // ...Otherwise it's a spill.
755 else {
756
757 // Make sure we ignore this virtual reg on the next round
758 // of allocation
Lang Hames27601ef2008-11-16 12:12:54 +0000759 vregIntervalsToAlloc.erase(&lis->getInterval(virtReg));
Evan Chengb1290a62008-10-02 18:29:27 +0000760
Evan Chengb1290a62008-10-02 18:29:27 +0000761 // Insert spill ranges for this live range
Lang Hames27601ef2008-11-16 12:12:54 +0000762 const LiveInterval *spillInterval = node2LI[node];
763 double oldSpillWeight = spillInterval->weight;
Evan Chengb1290a62008-10-02 18:29:27 +0000764 SmallVector<LiveInterval*, 8> spillIs;
Lang Hames33198392010-09-02 08:27:00 +0000765 rmf->rememberUseDefs(spillInterval);
Evan Chengb1290a62008-10-02 18:29:27 +0000766 std::vector<LiveInterval*> newSpills =
Evan Chengc781a242009-05-03 18:32:42 +0000767 lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm);
768 addStackInterval(spillInterval, mri);
Lang Hames33198392010-09-02 08:27:00 +0000769 rmf->rememberSpills(spillInterval, newSpills);
Lang Hames27601ef2008-11-16 12:12:54 +0000770
Daniel Dunbarbc84ad92009-08-20 20:01:34 +0000771 (void) oldSpillWeight;
David Greene30931542010-01-05 01:25:43 +0000772 DEBUG(dbgs() << "VREG " << virtReg << " -> SPILLED (Cost: "
Lang Hames233fd9c2009-08-18 23:34:50 +0000773 << oldSpillWeight << ", New vregs: ");
Lang Hames27601ef2008-11-16 12:12:54 +0000774
775 // Copy any newly inserted live intervals into the list of regs to
776 // allocate.
777 for (std::vector<LiveInterval*>::const_iterator
778 itr = newSpills.begin(), end = newSpills.end();
779 itr != end; ++itr) {
780
781 assert(!(*itr)->empty() && "Empty spill range.");
782
David Greene30931542010-01-05 01:25:43 +0000783 DEBUG(dbgs() << (*itr)->reg << " ");
Lang Hames27601ef2008-11-16 12:12:54 +0000784
785 vregIntervalsToAlloc.insert(*itr);
786 }
787
David Greene30931542010-01-05 01:25:43 +0000788 DEBUG(dbgs() << ")\n");
Evan Chengb1290a62008-10-02 18:29:27 +0000789
790 // We need another round if spill intervals were added.
791 anotherRoundNeeded |= !newSpills.empty();
792 }
793 }
794
795 return !anotherRoundNeeded;
796}
797
Lang Hames27601ef2008-11-16 12:12:54 +0000798void PBQPRegAlloc::finalizeAlloc() const {
799 typedef LiveIntervals::iterator LIIterator;
800 typedef LiveInterval::Ranges::const_iterator LRIterator;
801
802 // First allocate registers for the empty intervals.
Argyrios Kyrtzidis3713c0b2008-11-19 12:56:21 +0000803 for (LiveIntervalSet::const_iterator
Daniel Dunbara279bc32009-09-20 02:20:51 +0000804 itr = emptyVRegIntervals.begin(), end = emptyVRegIntervals.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000805 itr != end; ++itr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000806 LiveInterval *li = *itr;
Lang Hames27601ef2008-11-16 12:12:54 +0000807
Evan Cheng90f95f82009-06-14 20:22:55 +0000808 unsigned physReg = vrm->getRegAllocPref(li->reg);
Lang Hames6699fb22009-08-06 23:32:48 +0000809
Lang Hames27601ef2008-11-16 12:12:54 +0000810 if (physReg == 0) {
811 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000812 physReg = *liRC->allocation_order_begin(*mf);
Lang Hames27601ef2008-11-16 12:12:54 +0000813 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000814
815 vrm->assignVirt2Phys(li->reg, physReg);
Lang Hames27601ef2008-11-16 12:12:54 +0000816 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000817
Lang Hames27601ef2008-11-16 12:12:54 +0000818 // Finally iterate over the basic blocks to compute and set the live-in sets.
819 SmallVector<MachineBasicBlock*, 8> liveInMBBs;
820 MachineBasicBlock *entryMBB = &*mf->begin();
821
822 for (LIIterator liItr = lis->begin(), liEnd = lis->end();
823 liItr != liEnd; ++liItr) {
824
825 const LiveInterval *li = liItr->second;
826 unsigned reg = 0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000827
Lang Hames27601ef2008-11-16 12:12:54 +0000828 // Get the physical register for this interval
829 if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
830 reg = li->reg;
831 }
832 else if (vrm->isAssignedReg(li->reg)) {
833 reg = vrm->getPhys(li->reg);
834 }
835 else {
836 // Ranges which are assigned a stack slot only are ignored.
837 continue;
838 }
839
Lang Hamesb0e519f2009-05-17 23:50:36 +0000840 if (reg == 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000841 // Filter out zero regs - they're for intervals that were spilled.
Lang Hamesb0e519f2009-05-17 23:50:36 +0000842 continue;
843 }
844
Lang Hames27601ef2008-11-16 12:12:54 +0000845 // Iterate over the ranges of the current interval...
846 for (LRIterator lrItr = li->begin(), lrEnd = li->end();
847 lrItr != lrEnd; ++lrItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000848
Lang Hames27601ef2008-11-16 12:12:54 +0000849 // Find the set of basic blocks which this range is live into...
850 if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
851 // And add the physreg for this interval to their live-in sets.
852 for (unsigned i = 0; i < liveInMBBs.size(); ++i) {
853 if (liveInMBBs[i] != entryMBB) {
854 if (!liveInMBBs[i]->isLiveIn(reg)) {
855 liveInMBBs[i]->addLiveIn(reg);
856 }
857 }
858 }
859 liveInMBBs.clear();
860 }
861 }
862 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000863
Lang Hames27601ef2008-11-16 12:12:54 +0000864}
865
Evan Chengb1290a62008-10-02 18:29:27 +0000866bool PBQPRegAlloc::runOnMachineFunction(MachineFunction &MF) {
Lang Hames27601ef2008-11-16 12:12:54 +0000867
Evan Chengb1290a62008-10-02 18:29:27 +0000868 mf = &MF;
869 tm = &mf->getTarget();
870 tri = tm->getRegisterInfo();
Lang Hames27601ef2008-11-16 12:12:54 +0000871 tii = tm->getInstrInfo();
Lang Hames233a60e2009-11-03 23:52:08 +0000872 mri = &mf->getRegInfo();
Evan Chengb1290a62008-10-02 18:29:27 +0000873
Lang Hames27601ef2008-11-16 12:12:54 +0000874 lis = &getAnalysis<LiveIntervals>();
875 lss = &getAnalysis<LiveStacks>();
Evan Chengb1290a62008-10-02 18:29:27 +0000876 loopInfo = &getAnalysis<MachineLoopInfo>();
Lang Hames33198392010-09-02 08:27:00 +0000877 rmf = &getAnalysis<RenderMachineFunction>();
Evan Chengb1290a62008-10-02 18:29:27 +0000878
Owen Anderson49c8aa02009-03-13 05:55:11 +0000879 vrm = &getAnalysis<VirtRegMap>();
Evan Chengb1290a62008-10-02 18:29:27 +0000880
Lang Hames54cc2ef2010-07-19 15:22:28 +0000881
Lang Hames030c4bf2010-01-26 04:49:58 +0000882 DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000883
Evan Chengb1290a62008-10-02 18:29:27 +0000884 // Allocator main loop:
Misha Brukman2a835f92009-01-08 15:50:22 +0000885 //
Evan Chengb1290a62008-10-02 18:29:27 +0000886 // * Map current regalloc problem to a PBQP problem
887 // * Solve the PBQP problem
888 // * Map the solution back to a register allocation
889 // * Spill if necessary
Misha Brukman2a835f92009-01-08 15:50:22 +0000890 //
Evan Chengb1290a62008-10-02 18:29:27 +0000891 // This process is continued till no more spills are generated.
892
Lang Hames27601ef2008-11-16 12:12:54 +0000893 // Find the vreg intervals in need of allocation.
894 findVRegIntervalsToAlloc();
Misha Brukman2a835f92009-01-08 15:50:22 +0000895
Lang Hames27601ef2008-11-16 12:12:54 +0000896 // If there are non-empty intervals allocate them using pbqp.
897 if (!vregIntervalsToAlloc.empty()) {
Evan Chengb1290a62008-10-02 18:29:27 +0000898
Lang Hames27601ef2008-11-16 12:12:54 +0000899 bool pbqpAllocComplete = false;
900 unsigned round = 0;
901
902 while (!pbqpAllocComplete) {
David Greene30931542010-01-05 01:25:43 +0000903 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000904
Lang Hames030c4bf2010-01-26 04:49:58 +0000905 PBQP::Graph problem = constructPBQPProblem();
906 PBQP::Solution solution =
907 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(problem);
Lang Hames233fd9c2009-08-18 23:34:50 +0000908
Lang Hames6699fb22009-08-06 23:32:48 +0000909 pbqpAllocComplete = mapPBQPToRegAlloc(solution);
Lang Hames27601ef2008-11-16 12:12:54 +0000910
911 ++round;
912 }
Evan Chengb1290a62008-10-02 18:29:27 +0000913 }
914
Lang Hames27601ef2008-11-16 12:12:54 +0000915 // Finalise allocation, allocate empty ranges.
916 finalizeAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000917
Lang Hamesc4bcc772010-07-20 07:41:44 +0000918 rmf->renderMachineFunction("After PBQP register allocation.", vrm);
919
Lang Hames27601ef2008-11-16 12:12:54 +0000920 vregIntervalsToAlloc.clear();
921 emptyVRegIntervals.clear();
922 li2Node.clear();
923 node2LI.clear();
924 allowedSets.clear();
Lang Hames030c4bf2010-01-26 04:49:58 +0000925 problemNodes.clear();
Lang Hames27601ef2008-11-16 12:12:54 +0000926
David Greene30931542010-01-05 01:25:43 +0000927 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000928
Lang Hames87e3bca2009-05-06 02:36:21 +0000929 // Run rewriter
930 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
931
932 rewriter->runOnMachineFunction(*mf, *vrm, lis);
Lang Hames27601ef2008-11-16 12:12:54 +0000933
Misha Brukman2a835f92009-01-08 15:50:22 +0000934 return true;
Evan Chengb1290a62008-10-02 18:29:27 +0000935}
936
937FunctionPass* llvm::createPBQPRegisterAllocator() {
938 return new PBQPRegAlloc();
939}
940
941
942#undef DEBUG_TYPE