blob: ea2a02ddffa88a76607370716d487ba457ee29b9 [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 Hames12f35c52010-07-18 00:57:59 +000037#include "Splitter.h"
Evan Chengb1290a62008-10-02 18:29:27 +000038#include "VirtRegMap.h"
Lang Hames87e3bca2009-05-06 02:36:21 +000039#include "VirtRegRewriter.h"
Lang Hamesa937f222009-12-14 06:49:42 +000040#include "llvm/CodeGen/CalcSpillWeights.h"
Evan Chengb1290a62008-10-02 18:29:27 +000041#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Lang Hames27601ef2008-11-16 12:12:54 +000042#include "llvm/CodeGen/LiveStackAnalysis.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000043#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chengb1290a62008-10-02 18:29:27 +000044#include "llvm/CodeGen/MachineLoopInfo.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000045#include "llvm/CodeGen/MachineRegisterInfo.h"
46#include "llvm/CodeGen/RegAllocRegistry.h"
47#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengb1290a62008-10-02 18:29:27 +000048#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000049#include "llvm/Support/raw_ostream.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000050#include "llvm/Target/TargetInstrInfo.h"
51#include "llvm/Target/TargetMachine.h"
52#include <limits>
Evan Chengb1290a62008-10-02 18:29:27 +000053#include <map>
Misha Brukman2a835f92009-01-08 15:50:22 +000054#include <memory>
Evan Chengb1290a62008-10-02 18:29:27 +000055#include <set>
56#include <vector>
Evan Chengb1290a62008-10-02 18:29:27 +000057
58using namespace llvm;
59
60static RegisterRegAlloc
Duncan Sands1aecd152010-02-18 14:10:41 +000061registerPBQPRepAlloc("pbqp", "PBQP register allocator",
Lang Hames030c4bf2010-01-26 04:49:58 +000062 llvm::createPBQPRegisterAllocator);
Evan Chengb1290a62008-10-02 18:29:27 +000063
Lang Hames8481e3b2009-08-19 01:36:14 +000064static cl::opt<bool>
65pbqpCoalescing("pbqp-coalescing",
Lang Hames030c4bf2010-01-26 04:49:58 +000066 cl::desc("Attempt coalescing during PBQP register allocation."),
67 cl::init(false), cl::Hidden);
Lang Hames8481e3b2009-08-19 01:36:14 +000068
Lang Hames12f35c52010-07-18 00:57:59 +000069static cl::opt<bool>
70pbqpPreSplitting("pbqp-pre-splitting",
71 cl::desc("Pre-splite before PBQP register allocation."),
72 cl::init(false), cl::Hidden);
73
Evan Chengb1290a62008-10-02 18:29:27 +000074namespace {
75
Lang Hames6699fb22009-08-06 23:32:48 +000076 ///
77 /// PBQP based allocators solve the register allocation problem by mapping
78 /// register allocation problems to Partitioned Boolean Quadratic
79 /// Programming problems.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000080 class PBQPRegAlloc : public MachineFunctionPass {
Evan Chengb1290a62008-10-02 18:29:27 +000081 public:
82
83 static char ID;
Daniel Dunbara279bc32009-09-20 02:20:51 +000084
Lang Hames6699fb22009-08-06 23:32:48 +000085 /// Construct a PBQP register allocator.
Dan Gohman1b2d0b82009-08-11 15:15:10 +000086 PBQPRegAlloc() : MachineFunctionPass(&ID) {}
Evan Chengb1290a62008-10-02 18:29:27 +000087
Lang Hames6699fb22009-08-06 23:32:48 +000088 /// Return the pass name.
Dan Gohman00b0a242009-08-11 15:35:57 +000089 virtual const char* getPassName() const {
Evan Chengb1290a62008-10-02 18:29:27 +000090 return "PBQP Register Allocator";
91 }
92
Lang Hames6699fb22009-08-06 23:32:48 +000093 /// PBQP analysis usage.
94 virtual void getAnalysisUsage(AnalysisUsage &au) const {
Lang Hames233a60e2009-11-03 23:52:08 +000095 au.addRequired<SlotIndexes>();
96 au.addPreserved<SlotIndexes>();
Lang Hames6699fb22009-08-06 23:32:48 +000097 au.addRequired<LiveIntervals>();
98 //au.addRequiredID(SplitCriticalEdgesID);
Lang Hamesf7c553e2009-08-12 21:04:53 +000099 au.addRequired<RegisterCoalescer>();
Lang Hamesa937f222009-12-14 06:49:42 +0000100 au.addRequired<CalculateSpillWeights>();
Lang Hames6699fb22009-08-06 23:32:48 +0000101 au.addRequired<LiveStacks>();
102 au.addPreserved<LiveStacks>();
103 au.addRequired<MachineLoopInfo>();
104 au.addPreserved<MachineLoopInfo>();
Lang Hames12f35c52010-07-18 00:57:59 +0000105 if (pbqpPreSplitting)
106 au.addRequired<LoopSplitter>();
Lang Hames6699fb22009-08-06 23:32:48 +0000107 au.addRequired<VirtRegMap>();
108 MachineFunctionPass::getAnalysisUsage(au);
Evan Chengb1290a62008-10-02 18:29:27 +0000109 }
110
Lang Hames6699fb22009-08-06 23:32:48 +0000111 /// Perform register allocation
Evan Chengb1290a62008-10-02 18:29:27 +0000112 virtual bool runOnMachineFunction(MachineFunction &MF);
113
114 private:
Lang Hamesd0f6f012010-07-17 06:31:41 +0000115
116 class LIOrdering {
117 public:
118 bool operator()(const LiveInterval *li1, const LiveInterval *li2) const {
119 return li1->reg < li2->reg;
120 }
121 };
122
123 typedef std::map<const LiveInterval*, unsigned, LIOrdering> LI2NodeMap;
Evan Chengb1290a62008-10-02 18:29:27 +0000124 typedef std::vector<const LiveInterval*> Node2LIMap;
125 typedef std::vector<unsigned> AllowedSet;
126 typedef std::vector<AllowedSet> AllowedSetMap;
Lang Hames27601ef2008-11-16 12:12:54 +0000127 typedef std::set<unsigned> RegSet;
128 typedef std::pair<unsigned, unsigned> RegPair;
Lang Hames6699fb22009-08-06 23:32:48 +0000129 typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
Lang Hames27601ef2008-11-16 12:12:54 +0000130
Lang Hamesd0f6f012010-07-17 06:31:41 +0000131 typedef std::set<LiveInterval*, LIOrdering> LiveIntervalSet;
Evan Chengb1290a62008-10-02 18:29:27 +0000132
Lang Hames030c4bf2010-01-26 04:49:58 +0000133 typedef std::vector<PBQP::Graph::NodeItr> NodeVector;
134
Evan Chengb1290a62008-10-02 18:29:27 +0000135 MachineFunction *mf;
136 const TargetMachine *tm;
137 const TargetRegisterInfo *tri;
138 const TargetInstrInfo *tii;
139 const MachineLoopInfo *loopInfo;
140 MachineRegisterInfo *mri;
141
Lang Hames27601ef2008-11-16 12:12:54 +0000142 LiveIntervals *lis;
143 LiveStacks *lss;
Evan Chengb1290a62008-10-02 18:29:27 +0000144 VirtRegMap *vrm;
145
146 LI2NodeMap li2Node;
147 Node2LIMap node2LI;
148 AllowedSetMap allowedSets;
Lang Hames27601ef2008-11-16 12:12:54 +0000149 LiveIntervalSet vregIntervalsToAlloc,
150 emptyVRegIntervals;
Lang Hames030c4bf2010-01-26 04:49:58 +0000151 NodeVector problemNodes;
Evan Chengb1290a62008-10-02 18:29:27 +0000152
Misha Brukman2a835f92009-01-08 15:50:22 +0000153
Lang Hames6699fb22009-08-06 23:32:48 +0000154 /// Builds a PBQP cost vector.
Lang Hames27601ef2008-11-16 12:12:54 +0000155 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000156 PBQP::Vector buildCostVector(unsigned vReg,
157 const RegContainer &allowed,
158 const CoalesceMap &cealesces,
159 PBQP::PBQPNum spillCost) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000160
Lang Hames6699fb22009-08-06 23:32:48 +0000161 /// \brief Builds a PBQP interference matrix.
162 ///
163 /// @return Either a pointer to a non-zero PBQP matrix representing the
164 /// allocation option costs, or a null pointer for a zero matrix.
165 ///
166 /// Expects allowed sets for two interfering LiveIntervals. These allowed
167 /// sets should contain only allocable registers from the LiveInterval's
168 /// register class, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000169 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000170 PBQP::Matrix* buildInterferenceMatrix(const RegContainer &allowed1,
171 const RegContainer &allowed2) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000172
Lang Hames6699fb22009-08-06 23:32:48 +0000173 ///
174 /// Expects allowed sets for two potentially coalescable LiveIntervals,
175 /// and an estimated benefit due to coalescing. The allowed sets should
176 /// contain only allocable registers from the LiveInterval's register
177 /// classes, with any interfering pre-colored registers removed.
Lang Hames27601ef2008-11-16 12:12:54 +0000178 template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000179 PBQP::Matrix* buildCoalescingMatrix(const RegContainer &allowed1,
180 const RegContainer &allowed2,
181 PBQP::PBQPNum cBenefit) const;
Evan Chengb1290a62008-10-02 18:29:27 +0000182
Lang Hames6699fb22009-08-06 23:32:48 +0000183 /// \brief Finds coalescing opportunities and returns them as a map.
184 ///
185 /// Any entries in the map are guaranteed coalescable, even if their
186 /// corresponding live intervals overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000187 CoalesceMap findCoalesces();
Evan Chengb1290a62008-10-02 18:29:27 +0000188
Lang Hames6699fb22009-08-06 23:32:48 +0000189 /// \brief Finds the initial set of vreg intervals to allocate.
Lang Hames27601ef2008-11-16 12:12:54 +0000190 void findVRegIntervalsToAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000191
Lang Hames6699fb22009-08-06 23:32:48 +0000192 /// \brief Constructs a PBQP problem representation of the register
193 /// allocation problem for this function.
194 ///
195 /// @return a PBQP solver object for the register allocation problem.
Lang Hames030c4bf2010-01-26 04:49:58 +0000196 PBQP::Graph constructPBQPProblem();
Evan Chengb1290a62008-10-02 18:29:27 +0000197
Lang Hames6699fb22009-08-06 23:32:48 +0000198 /// \brief Adds a stack interval if the given live interval has been
199 /// spilled. Used to support stack slot coloring.
Evan Chengc781a242009-05-03 18:32:42 +0000200 void addStackInterval(const LiveInterval *spilled,MachineRegisterInfo* mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000201
Lang Hames6699fb22009-08-06 23:32:48 +0000202 /// \brief Given a solved PBQP problem maps this solution back to a register
203 /// assignment.
204 bool mapPBQPToRegAlloc(const PBQP::Solution &solution);
Evan Chengb1290a62008-10-02 18:29:27 +0000205
Lang Hames6699fb22009-08-06 23:32:48 +0000206 /// \brief Postprocessing before final spilling. Sets basic block "live in"
207 /// variables.
Lang Hames27601ef2008-11-16 12:12:54 +0000208 void finalizeAlloc() const;
209
Evan Chengb1290a62008-10-02 18:29:27 +0000210 };
211
212 char PBQPRegAlloc::ID = 0;
213}
214
215
Lang Hames27601ef2008-11-16 12:12:54 +0000216template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000217PBQP::Vector PBQPRegAlloc::buildCostVector(unsigned vReg,
218 const RegContainer &allowed,
219 const CoalesceMap &coalesces,
220 PBQP::PBQPNum spillCost) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000221
Lang Hames27601ef2008-11-16 12:12:54 +0000222 typedef typename RegContainer::const_iterator AllowedItr;
223
Evan Chengb1290a62008-10-02 18:29:27 +0000224 // Allocate vector. Additional element (0th) used for spill option
Lang Hames6699fb22009-08-06 23:32:48 +0000225 PBQP::Vector v(allowed.size() + 1, 0);
Evan Chengb1290a62008-10-02 18:29:27 +0000226
Lang Hames6699fb22009-08-06 23:32:48 +0000227 v[0] = spillCost;
Evan Chengb1290a62008-10-02 18:29:27 +0000228
Lang Hames27601ef2008-11-16 12:12:54 +0000229 // Iterate over the allowed registers inserting coalesce benefits if there
230 // are any.
231 unsigned ai = 0;
232 for (AllowedItr itr = allowed.begin(), end = allowed.end();
233 itr != end; ++itr, ++ai) {
234
235 unsigned pReg = *itr;
236
237 CoalesceMap::const_iterator cmItr =
238 coalesces.find(RegPair(vReg, pReg));
239
240 // No coalesce - on to the next preg.
241 if (cmItr == coalesces.end())
242 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000243
244 // We have a coalesce - insert the benefit.
Lang Hames6699fb22009-08-06 23:32:48 +0000245 v[ai + 1] = -cmItr->second;
Lang Hames27601ef2008-11-16 12:12:54 +0000246 }
247
Evan Chengb1290a62008-10-02 18:29:27 +0000248 return v;
249}
250
Lang Hames27601ef2008-11-16 12:12:54 +0000251template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000252PBQP::Matrix* PBQPRegAlloc::buildInterferenceMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000253 const RegContainer &allowed1, const RegContainer &allowed2) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000254
Lang Hames27601ef2008-11-16 12:12:54 +0000255 typedef typename RegContainer::const_iterator RegContainerIterator;
Evan Chengb1290a62008-10-02 18:29:27 +0000256
257 // Construct a PBQP matrix representing the cost of allocation options. The
258 // rows and columns correspond to the allocation options for the two live
259 // intervals. Elements will be infinite where corresponding registers alias,
260 // since we cannot allocate aliasing registers to interfering live intervals.
261 // All other elements (non-aliasing combinations) will have zero cost. Note
262 // that the spill option (element 0,0) has zero cost, since we can allocate
263 // both intervals to memory safely (the cost for each individual allocation
264 // to memory is accounted for by the cost vectors for each live interval).
Lang Hames6699fb22009-08-06 23:32:48 +0000265 PBQP::Matrix *m =
266 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Misha Brukman2a835f92009-01-08 15:50:22 +0000267
Evan Chengb1290a62008-10-02 18:29:27 +0000268 // Assume this is a zero matrix until proven otherwise. Zero matrices occur
269 // between interfering live ranges with non-overlapping register sets (e.g.
270 // non-overlapping reg classes, or disjoint sets of allowed regs within the
271 // same class). The term "overlapping" is used advisedly: sets which do not
272 // intersect, but contain registers which alias, will have non-zero matrices.
273 // We optimize zero matrices away to improve solver speed.
274 bool isZeroMatrix = true;
275
276
277 // Row index. Starts at 1, since the 0th row is for the spill option, which
278 // is always zero.
Misha Brukman2a835f92009-01-08 15:50:22 +0000279 unsigned ri = 1;
Evan Chengb1290a62008-10-02 18:29:27 +0000280
Misha Brukman2a835f92009-01-08 15:50:22 +0000281 // Iterate over allowed sets, insert infinities where required.
Lang Hames27601ef2008-11-16 12:12:54 +0000282 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000283 a1Itr != a1End; ++a1Itr) {
284
285 // Column index, starts at 1 as for row index.
286 unsigned ci = 1;
287 unsigned reg1 = *a1Itr;
288
Lang Hames27601ef2008-11-16 12:12:54 +0000289 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000290 a2Itr != a2End; ++a2Itr) {
291
292 unsigned reg2 = *a2Itr;
293
294 // If the row/column regs are identical or alias insert an infinity.
Lang Hames3f2f3f52009-09-03 02:52:02 +0000295 if (tri->regsOverlap(reg1, reg2)) {
Lang Hames6699fb22009-08-06 23:32:48 +0000296 (*m)[ri][ci] = std::numeric_limits<PBQP::PBQPNum>::infinity();
Evan Chengb1290a62008-10-02 18:29:27 +0000297 isZeroMatrix = false;
298 }
299
300 ++ci;
301 }
302
303 ++ri;
304 }
305
306 // If this turns out to be a zero matrix...
307 if (isZeroMatrix) {
308 // free it and return null.
309 delete m;
310 return 0;
311 }
312
313 // ...otherwise return the cost matrix.
314 return m;
315}
316
Lang Hames27601ef2008-11-16 12:12:54 +0000317template <typename RegContainer>
Lang Hames6699fb22009-08-06 23:32:48 +0000318PBQP::Matrix* PBQPRegAlloc::buildCoalescingMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000319 const RegContainer &allowed1, const RegContainer &allowed2,
Lang Hames6699fb22009-08-06 23:32:48 +0000320 PBQP::PBQPNum cBenefit) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000321
Lang Hames27601ef2008-11-16 12:12:54 +0000322 typedef typename RegContainer::const_iterator RegContainerIterator;
323
324 // Construct a PBQP Matrix representing the benefits of coalescing. As with
325 // interference matrices the rows and columns represent allowed registers
326 // for the LiveIntervals which are (potentially) to be coalesced. The amount
327 // -cBenefit will be placed in any element representing the same register
328 // for both intervals.
Lang Hames6699fb22009-08-06 23:32:48 +0000329 PBQP::Matrix *m =
330 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Lang Hames27601ef2008-11-16 12:12:54 +0000331
332 // Reset costs to zero.
333 m->reset(0);
334
335 // Assume the matrix is zero till proven otherwise. Zero matrices will be
336 // optimized away as in the interference case.
337 bool isZeroMatrix = true;
338
339 // Row index. Starts at 1, since the 0th row is for the spill option, which
340 // is always zero.
341 unsigned ri = 1;
342
343 // Iterate over the allowed sets, insert coalescing benefits where
344 // appropriate.
345 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
346 a1Itr != a1End; ++a1Itr) {
347
348 // Column index, starts at 1 as for row index.
349 unsigned ci = 1;
350 unsigned reg1 = *a1Itr;
351
352 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
353 a2Itr != a2End; ++a2Itr) {
354
355 // If the row and column represent the same register insert a beneficial
356 // cost to preference this allocation - it would allow us to eliminate a
Misha Brukman2a835f92009-01-08 15:50:22 +0000357 // move instruction.
Lang Hames27601ef2008-11-16 12:12:54 +0000358 if (reg1 == *a2Itr) {
359 (*m)[ri][ci] = -cBenefit;
360 isZeroMatrix = false;
361 }
362
363 ++ci;
364 }
365
366 ++ri;
367 }
368
369 // If this turns out to be a zero matrix...
370 if (isZeroMatrix) {
371 // ...free it and return null.
372 delete m;
373 return 0;
374 }
375
376 return m;
377}
378
379PBQPRegAlloc::CoalesceMap PBQPRegAlloc::findCoalesces() {
380
381 typedef MachineFunction::const_iterator MFIterator;
382 typedef MachineBasicBlock::const_iterator MBBIterator;
383 typedef LiveInterval::const_vni_iterator VNIIterator;
Misha Brukman2a835f92009-01-08 15:50:22 +0000384
Lang Hames27601ef2008-11-16 12:12:54 +0000385 CoalesceMap coalescesFound;
386
387 // To find coalesces we need to iterate over the function looking for
388 // copy instructions.
389 for (MFIterator bbItr = mf->begin(), bbEnd = mf->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000390 bbItr != bbEnd; ++bbItr) {
391
392 const MachineBasicBlock *mbb = &*bbItr;
Evan Chengb1290a62008-10-02 18:29:27 +0000393
Lang Hames27601ef2008-11-16 12:12:54 +0000394 for (MBBIterator iItr = mbb->begin(), iEnd = mbb->end();
395 iItr != iEnd; ++iItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000396
397 const MachineInstr *instr = &*iItr;
398
Lang Hames27601ef2008-11-16 12:12:54 +0000399 // If this isn't a copy then continue to the next instruction.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000400 if (!instr->isCopy())
Lang Hames27601ef2008-11-16 12:12:54 +0000401 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000402
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000403 unsigned srcReg = instr->getOperand(1).getReg();
404 unsigned dstReg = instr->getOperand(0).getReg();
405
Lang Hames27601ef2008-11-16 12:12:54 +0000406 // If the registers are already the same our job is nice and easy.
407 if (dstReg == srcReg)
408 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000409
Lang Hames27601ef2008-11-16 12:12:54 +0000410 bool srcRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(srcReg),
411 dstRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(dstReg);
412
413 // If both registers are physical then we can't coalesce.
414 if (srcRegIsPhysical && dstRegIsPhysical)
415 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000416
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000417 // If it's a copy that includes two virtual register but the source and
418 // destination classes differ then we can't coalesce.
419 if (!srcRegIsPhysical && !dstRegIsPhysical &&
420 mri->getRegClass(srcReg) != mri->getRegClass(dstReg))
Lang Hames27601ef2008-11-16 12:12:54 +0000421 continue;
422
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000423 // If one is physical and one is virtual, check that the physical is
424 // allocatable in the class of the virtual.
425 if (srcRegIsPhysical && !dstRegIsPhysical) {
426 const TargetRegisterClass *dstRegClass = mri->getRegClass(dstReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000427 if (std::find(dstRegClass->allocation_order_begin(*mf),
428 dstRegClass->allocation_order_end(*mf), srcReg) ==
429 dstRegClass->allocation_order_end(*mf))
Evan Chengb1290a62008-10-02 18:29:27 +0000430 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000431 }
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000432 if (!srcRegIsPhysical && dstRegIsPhysical) {
433 const TargetRegisterClass *srcRegClass = mri->getRegClass(srcReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000434 if (std::find(srcRegClass->allocation_order_begin(*mf),
435 srcRegClass->allocation_order_end(*mf), dstReg) ==
436 srcRegClass->allocation_order_end(*mf))
Lang Hames27601ef2008-11-16 12:12:54 +0000437 continue;
438 }
439
440 // If we've made it here we have a copy with compatible register classes.
Misha Brukman2a835f92009-01-08 15:50:22 +0000441 // We can probably coalesce, but we need to consider overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000442 const LiveInterval *srcLI = &lis->getInterval(srcReg),
443 *dstLI = &lis->getInterval(dstReg);
444
445 if (srcLI->overlaps(*dstLI)) {
446 // Even in the case of an overlap we might still be able to coalesce,
447 // but we need to make sure that no definition of either range occurs
448 // while the other range is live.
449
450 // Otherwise start by assuming we're ok.
451 bool badDef = false;
452
453 // Test all defs of the source range.
Misha Brukman2a835f92009-01-08 15:50:22 +0000454 for (VNIIterator
Lang Hames27601ef2008-11-16 12:12:54 +0000455 vniItr = srcLI->vni_begin(), vniEnd = srcLI->vni_end();
456 vniItr != vniEnd; ++vniItr) {
457
Lang Hames0b23dc02010-02-09 00:50:27 +0000458 // If we find a poorly defined def we err on the side of caution.
459 if (!(*vniItr)->def.isValid()) {
460 badDef = true;
461 break;
462 }
463
Lang Hames27601ef2008-11-16 12:12:54 +0000464 // If we find a def that kills the coalescing opportunity then
465 // record it and break from the loop.
466 if (dstLI->liveAt((*vniItr)->def)) {
467 badDef = true;
468 break;
469 }
470 }
471
472 // If we have a bad def give up, continue to the next instruction.
473 if (badDef)
474 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000475
Lang Hames27601ef2008-11-16 12:12:54 +0000476 // Otherwise test definitions of the destination range.
477 for (VNIIterator
478 vniItr = dstLI->vni_begin(), vniEnd = dstLI->vni_end();
479 vniItr != vniEnd; ++vniItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000480
Lang Hames27601ef2008-11-16 12:12:54 +0000481 // We want to make sure we skip the copy instruction itself.
Lang Hames52c1afc2009-08-10 23:43:28 +0000482 if ((*vniItr)->getCopy() == instr)
Lang Hames27601ef2008-11-16 12:12:54 +0000483 continue;
484
Lang Hames0b23dc02010-02-09 00:50:27 +0000485 if (!(*vniItr)->def.isValid()) {
486 badDef = true;
487 break;
488 }
489
Lang Hames27601ef2008-11-16 12:12:54 +0000490 if (srcLI->liveAt((*vniItr)->def)) {
491 badDef = true;
492 break;
493 }
494 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000495
Lang Hames27601ef2008-11-16 12:12:54 +0000496 // As before a bad def we give up and continue to the next instr.
497 if (badDef)
498 continue;
499 }
500
501 // If we make it to here then either the ranges didn't overlap, or they
502 // did, but none of their definitions would prevent us from coalescing.
503 // We're good to go with the coalesce.
504
Chris Lattner87565c12010-05-15 17:10:24 +0000505 float cBenefit = std::pow(10.0f, (float)loopInfo->getLoopDepth(mbb)) / 5.0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000506
Lang Hames27601ef2008-11-16 12:12:54 +0000507 coalescesFound[RegPair(srcReg, dstReg)] = cBenefit;
508 coalescesFound[RegPair(dstReg, srcReg)] = cBenefit;
Evan Chengb1290a62008-10-02 18:29:27 +0000509 }
510
511 }
512
Lang Hames27601ef2008-11-16 12:12:54 +0000513 return coalescesFound;
514}
515
516void PBQPRegAlloc::findVRegIntervalsToAlloc() {
517
518 // Iterate over all live ranges.
519 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
520 itr != end; ++itr) {
521
522 // Ignore physical ones.
523 if (TargetRegisterInfo::isPhysicalRegister(itr->first))
524 continue;
525
526 LiveInterval *li = itr->second;
527
528 // If this live interval is non-empty we will use pbqp to allocate it.
529 // Empty intervals we allocate in a simple post-processing stage in
530 // finalizeAlloc.
531 if (!li->empty()) {
532 vregIntervalsToAlloc.insert(li);
533 }
534 else {
535 emptyVRegIntervals.insert(li);
536 }
537 }
Evan Chengb1290a62008-10-02 18:29:27 +0000538}
539
Lang Hames030c4bf2010-01-26 04:49:58 +0000540PBQP::Graph PBQPRegAlloc::constructPBQPProblem() {
Evan Chengb1290a62008-10-02 18:29:27 +0000541
542 typedef std::vector<const LiveInterval*> LIVector;
Lang Hames27601ef2008-11-16 12:12:54 +0000543 typedef std::vector<unsigned> RegVector;
Evan Chengb1290a62008-10-02 18:29:27 +0000544
Lang Hames27601ef2008-11-16 12:12:54 +0000545 // This will store the physical intervals for easy reference.
546 LIVector physIntervals;
Evan Chengb1290a62008-10-02 18:29:27 +0000547
548 // Start by clearing the old node <-> live interval mappings & allowed sets
549 li2Node.clear();
550 node2LI.clear();
551 allowedSets.clear();
552
Lang Hames27601ef2008-11-16 12:12:54 +0000553 // Populate physIntervals, update preg use:
554 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000555 itr != end; ++itr) {
556
Evan Chengb1290a62008-10-02 18:29:27 +0000557 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
558 physIntervals.push_back(itr->second);
559 mri->setPhysRegUsed(itr->second->reg);
560 }
Evan Chengb1290a62008-10-02 18:29:27 +0000561 }
562
Lang Hames27601ef2008-11-16 12:12:54 +0000563 // Iterate over vreg intervals, construct live interval <-> node number
564 // mappings.
Misha Brukman2a835f92009-01-08 15:50:22 +0000565 for (LiveIntervalSet::const_iterator
Lang Hames27601ef2008-11-16 12:12:54 +0000566 itr = vregIntervalsToAlloc.begin(), end = vregIntervalsToAlloc.end();
567 itr != end; ++itr) {
568 const LiveInterval *li = *itr;
569
570 li2Node[li] = node2LI.size();
571 node2LI.push_back(li);
572 }
573
574 // Get the set of potential coalesces.
Lang Hames8481e3b2009-08-19 01:36:14 +0000575 CoalesceMap coalesces;
576
577 if (pbqpCoalescing) {
578 coalesces = findCoalesces();
579 }
Evan Chengb1290a62008-10-02 18:29:27 +0000580
581 // Construct a PBQP solver for this problem
Lang Hames030c4bf2010-01-26 04:49:58 +0000582 PBQP::Graph problem;
583 problemNodes.resize(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000584
585 // Resize allowedSets container appropriately.
Lang Hames27601ef2008-11-16 12:12:54 +0000586 allowedSets.resize(vregIntervalsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000587
588 // Iterate over virtual register intervals to compute allowed sets...
589 for (unsigned node = 0; node < node2LI.size(); ++node) {
590
591 // Grab pointers to the interval and its register class.
592 const LiveInterval *li = node2LI[node];
593 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000594
Evan Chengb1290a62008-10-02 18:29:27 +0000595 // Start by assuming all allocable registers in the class are allowed...
Lang Hames27601ef2008-11-16 12:12:54 +0000596 RegVector liAllowed(liRC->allocation_order_begin(*mf),
597 liRC->allocation_order_end(*mf));
Evan Chengb1290a62008-10-02 18:29:27 +0000598
Lang Hames27601ef2008-11-16 12:12:54 +0000599 // Eliminate the physical registers which overlap with this range, along
600 // with all their aliases.
601 for (LIVector::iterator pItr = physIntervals.begin(),
602 pEnd = physIntervals.end(); pItr != pEnd; ++pItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000603
Lang Hames27601ef2008-11-16 12:12:54 +0000604 if (!li->overlaps(**pItr))
605 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000606
Lang Hames27601ef2008-11-16 12:12:54 +0000607 unsigned pReg = (*pItr)->reg;
Evan Chengb1290a62008-10-02 18:29:27 +0000608
Lang Hames27601ef2008-11-16 12:12:54 +0000609 // If we get here then the live intervals overlap, but we're still ok
610 // if they're coalescable.
611 if (coalesces.find(RegPair(li->reg, pReg)) != coalesces.end())
612 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000613
Lang Hames27601ef2008-11-16 12:12:54 +0000614 // If we get here then we have a genuine exclusion.
Evan Chengb1290a62008-10-02 18:29:27 +0000615
Lang Hames27601ef2008-11-16 12:12:54 +0000616 // Remove the overlapping reg...
617 RegVector::iterator eraseItr =
618 std::find(liAllowed.begin(), liAllowed.end(), pReg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000619
Lang Hames27601ef2008-11-16 12:12:54 +0000620 if (eraseItr != liAllowed.end())
621 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000622
Lang Hames27601ef2008-11-16 12:12:54 +0000623 const unsigned *aliasItr = tri->getAliasSet(pReg);
624
625 if (aliasItr != 0) {
626 // ...and its aliases.
627 for (; *aliasItr != 0; ++aliasItr) {
628 RegVector::iterator eraseItr =
629 std::find(liAllowed.begin(), liAllowed.end(), *aliasItr);
Misha Brukman2a835f92009-01-08 15:50:22 +0000630
Lang Hames27601ef2008-11-16 12:12:54 +0000631 if (eraseItr != liAllowed.end()) {
632 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000633 }
Evan Chengb1290a62008-10-02 18:29:27 +0000634 }
Evan Chengb1290a62008-10-02 18:29:27 +0000635 }
Evan Chengb1290a62008-10-02 18:29:27 +0000636 }
637
638 // Copy the allowed set into a member vector for use when constructing cost
639 // vectors & matrices, and mapping PBQP solutions back to assignments.
640 allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end());
641
642 // Set the spill cost to the interval weight, or epsilon if the
643 // interval weight is zero
Lang Hames6699fb22009-08-06 23:32:48 +0000644 PBQP::PBQPNum spillCost = (li->weight != 0.0) ?
645 li->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000646
647 // Build a cost vector for this interval.
Lang Hames6699fb22009-08-06 23:32:48 +0000648 problemNodes[node] =
649 problem.addNode(
650 buildCostVector(li->reg, allowedSets[node], coalesces, spillCost));
Evan Chengb1290a62008-10-02 18:29:27 +0000651
652 }
653
Lang Hames27601ef2008-11-16 12:12:54 +0000654
Evan Chengb1290a62008-10-02 18:29:27 +0000655 // Now add the cost matrices...
656 for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) {
Evan Chengb1290a62008-10-02 18:29:27 +0000657 const LiveInterval *li = node2LI[node1];
658
Evan Chengb1290a62008-10-02 18:29:27 +0000659 // Test for live range overlaps and insert interference matrices.
660 for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) {
661 const LiveInterval *li2 = node2LI[node2];
662
Lang Hames27601ef2008-11-16 12:12:54 +0000663 CoalesceMap::const_iterator cmItr =
664 coalesces.find(RegPair(li->reg, li2->reg));
Evan Chengb1290a62008-10-02 18:29:27 +0000665
Lang Hames6699fb22009-08-06 23:32:48 +0000666 PBQP::Matrix *m = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000667
Lang Hames27601ef2008-11-16 12:12:54 +0000668 if (cmItr != coalesces.end()) {
669 m = buildCoalescingMatrix(allowedSets[node1], allowedSets[node2],
670 cmItr->second);
671 }
672 else if (li->overlaps(*li2)) {
673 m = buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]);
674 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000675
Lang Hames27601ef2008-11-16 12:12:54 +0000676 if (m != 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000677 problem.addEdge(problemNodes[node1],
678 problemNodes[node2],
679 *m);
680
Lang Hames27601ef2008-11-16 12:12:54 +0000681 delete m;
Evan Chengb1290a62008-10-02 18:29:27 +0000682 }
683 }
684 }
685
Lang Hames6699fb22009-08-06 23:32:48 +0000686 assert(problem.getNumNodes() == allowedSets.size());
Lang Hames6699fb22009-08-06 23:32:48 +0000687/*
688 std::cerr << "Allocating for " << problem.getNumNodes() << " nodes, "
689 << problem.getNumEdges() << " edges.\n";
690
691 problem.printDot(std::cerr);
692*/
Evan Chengb1290a62008-10-02 18:29:27 +0000693 // We're done, PBQP problem constructed - return it.
Lang Hames6699fb22009-08-06 23:32:48 +0000694 return problem;
Evan Chengb1290a62008-10-02 18:29:27 +0000695}
696
Evan Chengc781a242009-05-03 18:32:42 +0000697void PBQPRegAlloc::addStackInterval(const LiveInterval *spilled,
698 MachineRegisterInfo* mri) {
Lang Hames27601ef2008-11-16 12:12:54 +0000699 int stackSlot = vrm->getStackSlot(spilled->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000700
701 if (stackSlot == VirtRegMap::NO_STACK_SLOT)
Lang Hames27601ef2008-11-16 12:12:54 +0000702 return;
703
Evan Chengc781a242009-05-03 18:32:42 +0000704 const TargetRegisterClass *RC = mri->getRegClass(spilled->reg);
705 LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC);
Lang Hames27601ef2008-11-16 12:12:54 +0000706
707 VNInfo *vni;
708 if (stackInterval.getNumValNums() != 0)
709 vni = stackInterval.getValNumInfo(0);
710 else
Lang Hames86511252009-09-04 20:41:11 +0000711 vni = stackInterval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +0000712 SlotIndex(), 0, false, lss->getVNInfoAllocator());
Lang Hames27601ef2008-11-16 12:12:54 +0000713
714 LiveInterval &rhsInterval = lis->getInterval(spilled->reg);
715 stackInterval.MergeRangesInAsValue(rhsInterval, vni);
716}
717
Lang Hames6699fb22009-08-06 23:32:48 +0000718bool PBQPRegAlloc::mapPBQPToRegAlloc(const PBQP::Solution &solution) {
Lang Hamese98b4b02009-11-15 04:39:51 +0000719
Evan Chengb1290a62008-10-02 18:29:27 +0000720 // Set to true if we have any spills
721 bool anotherRoundNeeded = false;
722
723 // Clear the existing allocation.
724 vrm->clearAllVirt();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000725
Evan Chengb1290a62008-10-02 18:29:27 +0000726 // Iterate over the nodes mapping the PBQP solution to a register assignment.
727 for (unsigned node = 0; node < node2LI.size(); ++node) {
Lang Hames27601ef2008-11-16 12:12:54 +0000728 unsigned virtReg = node2LI[node]->reg,
Lang Hames030c4bf2010-01-26 04:49:58 +0000729 allocSelection = solution.getSelection(problemNodes[node]);
Lang Hames6699fb22009-08-06 23:32:48 +0000730
Evan Chengb1290a62008-10-02 18:29:27 +0000731
732 // If the PBQP solution is non-zero it's a physical register...
733 if (allocSelection != 0) {
734 // Get the physical reg, subtracting 1 to account for the spill option.
735 unsigned physReg = allowedSets[node][allocSelection - 1];
736
David Greene30931542010-01-05 01:25:43 +0000737 DEBUG(dbgs() << "VREG " << virtReg << " -> "
Lang Hames233fd9c2009-08-18 23:34:50 +0000738 << tri->getName(physReg) << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000739
740 assert(physReg != 0);
741
Evan Chengb1290a62008-10-02 18:29:27 +0000742 // Add to the virt reg map and update the used phys regs.
Lang Hames27601ef2008-11-16 12:12:54 +0000743 vrm->assignVirt2Phys(virtReg, physReg);
Evan Chengb1290a62008-10-02 18:29:27 +0000744 }
745 // ...Otherwise it's a spill.
746 else {
747
748 // Make sure we ignore this virtual reg on the next round
749 // of allocation
Lang Hames27601ef2008-11-16 12:12:54 +0000750 vregIntervalsToAlloc.erase(&lis->getInterval(virtReg));
Evan Chengb1290a62008-10-02 18:29:27 +0000751
Evan Chengb1290a62008-10-02 18:29:27 +0000752 // Insert spill ranges for this live range
Lang Hames27601ef2008-11-16 12:12:54 +0000753 const LiveInterval *spillInterval = node2LI[node];
754 double oldSpillWeight = spillInterval->weight;
Evan Chengb1290a62008-10-02 18:29:27 +0000755 SmallVector<LiveInterval*, 8> spillIs;
756 std::vector<LiveInterval*> newSpills =
Evan Chengc781a242009-05-03 18:32:42 +0000757 lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm);
758 addStackInterval(spillInterval, mri);
Lang Hames27601ef2008-11-16 12:12:54 +0000759
Daniel Dunbarbc84ad92009-08-20 20:01:34 +0000760 (void) oldSpillWeight;
David Greene30931542010-01-05 01:25:43 +0000761 DEBUG(dbgs() << "VREG " << virtReg << " -> SPILLED (Cost: "
Lang Hames233fd9c2009-08-18 23:34:50 +0000762 << oldSpillWeight << ", New vregs: ");
Lang Hames27601ef2008-11-16 12:12:54 +0000763
764 // Copy any newly inserted live intervals into the list of regs to
765 // allocate.
766 for (std::vector<LiveInterval*>::const_iterator
767 itr = newSpills.begin(), end = newSpills.end();
768 itr != end; ++itr) {
769
770 assert(!(*itr)->empty() && "Empty spill range.");
771
David Greene30931542010-01-05 01:25:43 +0000772 DEBUG(dbgs() << (*itr)->reg << " ");
Lang Hames27601ef2008-11-16 12:12:54 +0000773
774 vregIntervalsToAlloc.insert(*itr);
775 }
776
David Greene30931542010-01-05 01:25:43 +0000777 DEBUG(dbgs() << ")\n");
Evan Chengb1290a62008-10-02 18:29:27 +0000778
779 // We need another round if spill intervals were added.
780 anotherRoundNeeded |= !newSpills.empty();
781 }
782 }
783
784 return !anotherRoundNeeded;
785}
786
Lang Hames27601ef2008-11-16 12:12:54 +0000787void PBQPRegAlloc::finalizeAlloc() const {
788 typedef LiveIntervals::iterator LIIterator;
789 typedef LiveInterval::Ranges::const_iterator LRIterator;
790
791 // First allocate registers for the empty intervals.
Argyrios Kyrtzidis3713c0b2008-11-19 12:56:21 +0000792 for (LiveIntervalSet::const_iterator
Daniel Dunbara279bc32009-09-20 02:20:51 +0000793 itr = emptyVRegIntervals.begin(), end = emptyVRegIntervals.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000794 itr != end; ++itr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000795 LiveInterval *li = *itr;
Lang Hames27601ef2008-11-16 12:12:54 +0000796
Evan Cheng90f95f82009-06-14 20:22:55 +0000797 unsigned physReg = vrm->getRegAllocPref(li->reg);
Lang Hames6699fb22009-08-06 23:32:48 +0000798
Lang Hames27601ef2008-11-16 12:12:54 +0000799 if (physReg == 0) {
800 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000801 physReg = *liRC->allocation_order_begin(*mf);
Lang Hames27601ef2008-11-16 12:12:54 +0000802 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000803
804 vrm->assignVirt2Phys(li->reg, physReg);
Lang Hames27601ef2008-11-16 12:12:54 +0000805 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000806
Lang Hames27601ef2008-11-16 12:12:54 +0000807 // Finally iterate over the basic blocks to compute and set the live-in sets.
808 SmallVector<MachineBasicBlock*, 8> liveInMBBs;
809 MachineBasicBlock *entryMBB = &*mf->begin();
810
811 for (LIIterator liItr = lis->begin(), liEnd = lis->end();
812 liItr != liEnd; ++liItr) {
813
814 const LiveInterval *li = liItr->second;
815 unsigned reg = 0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000816
Lang Hames27601ef2008-11-16 12:12:54 +0000817 // Get the physical register for this interval
818 if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
819 reg = li->reg;
820 }
821 else if (vrm->isAssignedReg(li->reg)) {
822 reg = vrm->getPhys(li->reg);
823 }
824 else {
825 // Ranges which are assigned a stack slot only are ignored.
826 continue;
827 }
828
Lang Hamesb0e519f2009-05-17 23:50:36 +0000829 if (reg == 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000830 // Filter out zero regs - they're for intervals that were spilled.
Lang Hamesb0e519f2009-05-17 23:50:36 +0000831 continue;
832 }
833
Lang Hames27601ef2008-11-16 12:12:54 +0000834 // Iterate over the ranges of the current interval...
835 for (LRIterator lrItr = li->begin(), lrEnd = li->end();
836 lrItr != lrEnd; ++lrItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000837
Lang Hames27601ef2008-11-16 12:12:54 +0000838 // Find the set of basic blocks which this range is live into...
839 if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
840 // And add the physreg for this interval to their live-in sets.
841 for (unsigned i = 0; i < liveInMBBs.size(); ++i) {
842 if (liveInMBBs[i] != entryMBB) {
843 if (!liveInMBBs[i]->isLiveIn(reg)) {
844 liveInMBBs[i]->addLiveIn(reg);
845 }
846 }
847 }
848 liveInMBBs.clear();
849 }
850 }
851 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000852
Lang Hames27601ef2008-11-16 12:12:54 +0000853}
854
Evan Chengb1290a62008-10-02 18:29:27 +0000855bool PBQPRegAlloc::runOnMachineFunction(MachineFunction &MF) {
Lang Hames27601ef2008-11-16 12:12:54 +0000856
Evan Chengb1290a62008-10-02 18:29:27 +0000857 mf = &MF;
858 tm = &mf->getTarget();
859 tri = tm->getRegisterInfo();
Lang Hames27601ef2008-11-16 12:12:54 +0000860 tii = tm->getInstrInfo();
Lang Hames233a60e2009-11-03 23:52:08 +0000861 mri = &mf->getRegInfo();
Evan Chengb1290a62008-10-02 18:29:27 +0000862
Lang Hames27601ef2008-11-16 12:12:54 +0000863 lis = &getAnalysis<LiveIntervals>();
864 lss = &getAnalysis<LiveStacks>();
Evan Chengb1290a62008-10-02 18:29:27 +0000865 loopInfo = &getAnalysis<MachineLoopInfo>();
866
Owen Anderson49c8aa02009-03-13 05:55:11 +0000867 vrm = &getAnalysis<VirtRegMap>();
Evan Chengb1290a62008-10-02 18:29:27 +0000868
Lang Hames030c4bf2010-01-26 04:49:58 +0000869 DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000870
Evan Chengb1290a62008-10-02 18:29:27 +0000871 // Allocator main loop:
Misha Brukman2a835f92009-01-08 15:50:22 +0000872 //
Evan Chengb1290a62008-10-02 18:29:27 +0000873 // * Map current regalloc problem to a PBQP problem
874 // * Solve the PBQP problem
875 // * Map the solution back to a register allocation
876 // * Spill if necessary
Misha Brukman2a835f92009-01-08 15:50:22 +0000877 //
Evan Chengb1290a62008-10-02 18:29:27 +0000878 // This process is continued till no more spills are generated.
879
Lang Hames27601ef2008-11-16 12:12:54 +0000880 // Find the vreg intervals in need of allocation.
881 findVRegIntervalsToAlloc();
Misha Brukman2a835f92009-01-08 15:50:22 +0000882
Lang Hames27601ef2008-11-16 12:12:54 +0000883 // If there are non-empty intervals allocate them using pbqp.
884 if (!vregIntervalsToAlloc.empty()) {
Evan Chengb1290a62008-10-02 18:29:27 +0000885
Lang Hames27601ef2008-11-16 12:12:54 +0000886 bool pbqpAllocComplete = false;
887 unsigned round = 0;
888
889 while (!pbqpAllocComplete) {
David Greene30931542010-01-05 01:25:43 +0000890 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000891
Lang Hames030c4bf2010-01-26 04:49:58 +0000892 PBQP::Graph problem = constructPBQPProblem();
893 PBQP::Solution solution =
894 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(problem);
Lang Hames233fd9c2009-08-18 23:34:50 +0000895
Lang Hames6699fb22009-08-06 23:32:48 +0000896 pbqpAllocComplete = mapPBQPToRegAlloc(solution);
Lang Hames27601ef2008-11-16 12:12:54 +0000897
898 ++round;
899 }
Evan Chengb1290a62008-10-02 18:29:27 +0000900 }
901
Lang Hames27601ef2008-11-16 12:12:54 +0000902 // Finalise allocation, allocate empty ranges.
903 finalizeAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +0000904
Lang Hames27601ef2008-11-16 12:12:54 +0000905 vregIntervalsToAlloc.clear();
906 emptyVRegIntervals.clear();
907 li2Node.clear();
908 node2LI.clear();
909 allowedSets.clear();
Lang Hames030c4bf2010-01-26 04:49:58 +0000910 problemNodes.clear();
Lang Hames27601ef2008-11-16 12:12:54 +0000911
David Greene30931542010-01-05 01:25:43 +0000912 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000913
Lang Hames87e3bca2009-05-06 02:36:21 +0000914 // Run rewriter
915 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
916
917 rewriter->runOnMachineFunction(*mf, *vrm, lis);
Lang Hames27601ef2008-11-16 12:12:54 +0000918
Misha Brukman2a835f92009-01-08 15:50:22 +0000919 return true;
Evan Chengb1290a62008-10-02 18:29:27 +0000920}
921
922FunctionPass* llvm::createPBQPRegisterAllocator() {
923 return new PBQPRegAlloc();
924}
925
926
927#undef DEBUG_TYPE