blob: a45ead868d742734aefb723d8e4f33cb90c9a3ac [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 Hames54cc2ef2010-07-19 15:22:28 +000034#include "RenderMachineFunction.h"
Lang Hames12f35c52010-07-18 00:57:59 +000035#include "Splitter.h"
Evan Chengb1290a62008-10-02 18:29:27 +000036#include "VirtRegMap.h"
Lang Hames87e3bca2009-05-06 02:36:21 +000037#include "VirtRegRewriter.h"
Lang Hamesa937f222009-12-14 06:49:42 +000038#include "llvm/CodeGen/CalcSpillWeights.h"
Evan Chengb1290a62008-10-02 18:29:27 +000039#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Lang Hames27601ef2008-11-16 12:12:54 +000040#include "llvm/CodeGen/LiveStackAnalysis.h"
Lang Hameseb6c8f52010-09-18 09:07:10 +000041#include "llvm/CodeGen/RegAllocPBQP.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000042#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chengb1290a62008-10-02 18:29:27 +000043#include "llvm/CodeGen/MachineLoopInfo.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000044#include "llvm/CodeGen/MachineRegisterInfo.h"
Lang Hameseb6c8f52010-09-18 09:07:10 +000045#include "llvm/CodeGen/PBQP/HeuristicSolver.h"
46#include "llvm/CodeGen/PBQP/Graph.h"
47#include "llvm/CodeGen/PBQP/Heuristics/Briggs.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000048#include "llvm/CodeGen/RegAllocRegistry.h"
49#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengb1290a62008-10-02 18:29:27 +000050#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000051#include "llvm/Support/raw_ostream.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000052#include "llvm/Target/TargetInstrInfo.h"
53#include "llvm/Target/TargetMachine.h"
54#include <limits>
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
Lang Hamesf70e7cc2010-09-23 04:28:54 +000059using namespace llvm;
Lang Hameseb6c8f52010-09-18 09:07:10 +000060
Evan Chengb1290a62008-10-02 18:29:27 +000061static RegisterRegAlloc
Duncan Sands1aecd152010-02-18 14:10:41 +000062registerPBQPRepAlloc("pbqp", "PBQP register allocator",
Lang Hamesf70e7cc2010-09-23 04:28:54 +000063 createDefaultPBQPRegisterAllocator);
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>
Lang Hameseb6c8f52010-09-18 09:07:10 +000071pbqpBuilder("pbqp-builder",
Lang Hamesf70e7cc2010-09-23 04:28:54 +000072 cl::desc("Use new builder system."),
73 cl::init(true), cl::Hidden);
Lang Hameseb6c8f52010-09-18 09:07:10 +000074
75
76static cl::opt<bool>
Lang Hames12f35c52010-07-18 00:57:59 +000077pbqpPreSplitting("pbqp-pre-splitting",
Lang Hamesf70e7cc2010-09-23 04:28:54 +000078 cl::desc("Pre-split before PBQP register allocation."),
Lang Hames12f35c52010-07-18 00:57:59 +000079 cl::init(false), cl::Hidden);
80
Lang Hamesf70e7cc2010-09-23 04:28:54 +000081namespace {
82
83///
84/// PBQP based allocators solve the register allocation problem by mapping
85/// register allocation problems to Partitioned Boolean Quadratic
86/// Programming problems.
87class RegAllocPBQP : public MachineFunctionPass {
88public:
89
90 static char ID;
91
92 /// Construct a PBQP register allocator.
93 RegAllocPBQP(std::auto_ptr<PBQPBuilder> b) : MachineFunctionPass(ID), builder(b) {}
94
95 /// Return the pass name.
96 virtual const char* getPassName() const {
97 return "PBQP Register Allocator";
98 }
99
100 /// PBQP analysis usage.
101 virtual void getAnalysisUsage(AnalysisUsage &au) const;
102
103 /// Perform register allocation
104 virtual bool runOnMachineFunction(MachineFunction &MF);
105
106private:
107
108 typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
109 typedef std::vector<const LiveInterval*> Node2LIMap;
110 typedef std::vector<unsigned> AllowedSet;
111 typedef std::vector<AllowedSet> AllowedSetMap;
112 typedef std::pair<unsigned, unsigned> RegPair;
113 typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
114 typedef std::vector<PBQP::Graph::NodeItr> NodeVector;
115 typedef std::set<unsigned> RegSet;
116
117
118 std::auto_ptr<PBQPBuilder> builder;
119
120 MachineFunction *mf;
121 const TargetMachine *tm;
122 const TargetRegisterInfo *tri;
123 const TargetInstrInfo *tii;
124 const MachineLoopInfo *loopInfo;
125 MachineRegisterInfo *mri;
126 RenderMachineFunction *rmf;
127
128 LiveIntervals *lis;
129 LiveStacks *lss;
130 VirtRegMap *vrm;
131
132 LI2NodeMap li2Node;
133 Node2LIMap node2LI;
134 AllowedSetMap allowedSets;
135 RegSet vregsToAlloc, emptyIntervalVRegs;
136 NodeVector problemNodes;
137
138
139 /// Builds a PBQP cost vector.
140 template <typename RegContainer>
141 PBQP::Vector buildCostVector(unsigned vReg,
142 const RegContainer &allowed,
143 const CoalesceMap &cealesces,
144 PBQP::PBQPNum spillCost) const;
145
146 /// \brief Builds a PBQP interference matrix.
147 ///
148 /// @return Either a pointer to a non-zero PBQP matrix representing the
149 /// allocation option costs, or a null pointer for a zero matrix.
150 ///
151 /// Expects allowed sets for two interfering LiveIntervals. These allowed
152 /// sets should contain only allocable registers from the LiveInterval's
153 /// register class, with any interfering pre-colored registers removed.
154 template <typename RegContainer>
155 PBQP::Matrix* buildInterferenceMatrix(const RegContainer &allowed1,
156 const RegContainer &allowed2) const;
157
158 ///
159 /// Expects allowed sets for two potentially coalescable LiveIntervals,
160 /// and an estimated benefit due to coalescing. The allowed sets should
161 /// contain only allocable registers from the LiveInterval's register
162 /// classes, with any interfering pre-colored registers removed.
163 template <typename RegContainer>
164 PBQP::Matrix* buildCoalescingMatrix(const RegContainer &allowed1,
165 const RegContainer &allowed2,
166 PBQP::PBQPNum cBenefit) const;
167
168 /// \brief Finds coalescing opportunities and returns them as a map.
169 ///
170 /// Any entries in the map are guaranteed coalescable, even if their
171 /// corresponding live intervals overlap.
172 CoalesceMap findCoalesces();
173
174 /// \brief Finds the initial set of vreg intervals to allocate.
175 void findVRegIntervalsToAlloc();
176
177 /// \brief Constructs a PBQP problem representation of the register
178 /// allocation problem for this function.
179 ///
180 /// Old Construction Process - this functionality has been subsumed
181 /// by PBQPBuilder. This function will only be hanging around for a little
182 /// while until the new system has been fully tested.
183 ///
184 /// @return a PBQP solver object for the register allocation problem.
185 PBQP::Graph constructPBQPProblemOld();
186
187 /// \brief Adds a stack interval if the given live interval has been
188 /// spilled. Used to support stack slot coloring.
189 void addStackInterval(const LiveInterval *spilled,MachineRegisterInfo* mri);
190
191 /// \brief Given a solved PBQP problem maps this solution back to a register
192 /// assignment.
193 ///
194 /// Old Construction Process - this functionality has been subsumed
195 /// by PBQPBuilder. This function will only be hanging around for a little
196 /// while until the new system has been fully tested.
197 ///
198 bool mapPBQPToRegAllocOld(const PBQP::Solution &solution);
199
200 /// \brief Given a solved PBQP problem maps this solution back to a register
201 /// assignment.
202 bool mapPBQPToRegAlloc(const PBQPRAProblem &problem,
203 const PBQP::Solution &solution);
204
205 /// \brief Postprocessing before final spilling. Sets basic block "live in"
206 /// variables.
207 void finalizeAlloc() const;
208
209};
210
Lang Hameseb6c8f52010-09-18 09:07:10 +0000211char RegAllocPBQP::ID = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000212
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000213} // End anonymous namespace.
214
Lang Hameseb6c8f52010-09-18 09:07:10 +0000215unsigned PBQPRAProblem::getVRegForNode(PBQP::Graph::ConstNodeItr node) const {
216 Node2VReg::const_iterator vregItr = node2VReg.find(node);
217 assert(vregItr != node2VReg.end() && "No vreg for node.");
218 return vregItr->second;
219}
Evan Chengb1290a62008-10-02 18:29:27 +0000220
Lang Hameseb6c8f52010-09-18 09:07:10 +0000221PBQP::Graph::NodeItr PBQPRAProblem::getNodeForVReg(unsigned vreg) const {
222 VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg);
223 assert(nodeItr != vreg2Node.end() && "No node for vreg.");
224 return nodeItr->second;
225
226}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000227
Lang Hameseb6c8f52010-09-18 09:07:10 +0000228const PBQPRAProblem::AllowedSet&
229 PBQPRAProblem::getAllowedSet(unsigned vreg) const {
230 AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg);
231 assert(allowedSetItr != allowedSets.end() && "No pregs for vreg.");
232 const AllowedSet &allowedSet = allowedSetItr->second;
233 return allowedSet;
234}
Evan Chengb1290a62008-10-02 18:29:27 +0000235
Lang Hameseb6c8f52010-09-18 09:07:10 +0000236unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const {
237 assert(isPRegOption(vreg, option) && "Not a preg option.");
238
239 const AllowedSet& allowedSet = getAllowedSet(vreg);
240 assert(option <= allowedSet.size() && "Option outside allowed set.");
241 return allowedSet[option - 1];
242}
243
Lang Hamese9c93562010-09-21 13:19:36 +0000244std::auto_ptr<PBQPRAProblem> PBQPBuilder::build(MachineFunction *mf,
245 const LiveIntervals *lis,
246 const MachineLoopInfo *loopInfo,
247 const RegSet &vregs) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000248
249 typedef std::vector<const LiveInterval*> LIVector;
250
251 MachineRegisterInfo *mri = &mf->getRegInfo();
252 const TargetRegisterInfo *tri = mf->getTarget().getRegisterInfo();
253
254 std::auto_ptr<PBQPRAProblem> p(new PBQPRAProblem());
255 PBQP::Graph &g = p->getGraph();
256 RegSet pregs;
257
258 // Collect the set of preg intervals, record that they're used in the MF.
259 for (LiveIntervals::const_iterator itr = lis->begin(), end = lis->end();
260 itr != end; ++itr) {
261 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
262 pregs.insert(itr->first);
263 mri->setPhysRegUsed(itr->first);
Evan Chengb1290a62008-10-02 18:29:27 +0000264 }
Lang Hameseb6c8f52010-09-18 09:07:10 +0000265 }
Evan Chengb1290a62008-10-02 18:29:27 +0000266
Lang Hameseb6c8f52010-09-18 09:07:10 +0000267 BitVector reservedRegs = tri->getReservedRegs(*mf);
Evan Chengb1290a62008-10-02 18:29:27 +0000268
Lang Hameseb6c8f52010-09-18 09:07:10 +0000269 // Iterate over vregs.
270 for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end();
271 vregItr != vregEnd; ++vregItr) {
272 unsigned vreg = *vregItr;
273 const TargetRegisterClass *trc = mri->getRegClass(vreg);
274 const LiveInterval *vregLI = &lis->getInterval(vreg);
Evan Chengb1290a62008-10-02 18:29:27 +0000275
Lang Hameseb6c8f52010-09-18 09:07:10 +0000276 // Compute an initial allowed set for the current vreg.
277 typedef std::vector<unsigned> VRAllowed;
278 VRAllowed vrAllowed;
279 for (TargetRegisterClass::iterator aoItr = trc->allocation_order_begin(*mf),
280 aoEnd = trc->allocation_order_end(*mf);
281 aoItr != aoEnd; ++aoItr) {
282 unsigned preg = *aoItr;
283 if (!reservedRegs.test(preg)) {
284 vrAllowed.push_back(preg);
Lang Hamesd0f6f012010-07-17 06:31:41 +0000285 }
Lang Hameseb6c8f52010-09-18 09:07:10 +0000286 }
Lang Hamesd0f6f012010-07-17 06:31:41 +0000287
Lang Hameseb6c8f52010-09-18 09:07:10 +0000288 // Remove any physical registers which overlap.
289 for (RegSet::const_iterator pregItr = pregs.begin(),
290 pregEnd = pregs.end();
291 pregItr != pregEnd; ++pregItr) {
292 unsigned preg = *pregItr;
293 const LiveInterval *pregLI = &lis->getInterval(preg);
Lang Hames27601ef2008-11-16 12:12:54 +0000294
Lang Hameseb6c8f52010-09-18 09:07:10 +0000295 if (pregLI->empty())
296 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000297
Lang Hameseb6c8f52010-09-18 09:07:10 +0000298 if (!vregLI->overlaps(*pregLI))
299 continue;
Lang Hames030c4bf2010-01-26 04:49:58 +0000300
Lang Hameseb6c8f52010-09-18 09:07:10 +0000301 // Remove the register from the allowed set.
302 VRAllowed::iterator eraseItr =
303 std::find(vrAllowed.begin(), vrAllowed.end(), preg);
Evan Chengb1290a62008-10-02 18:29:27 +0000304
Lang Hameseb6c8f52010-09-18 09:07:10 +0000305 if (eraseItr != vrAllowed.end()) {
306 vrAllowed.erase(eraseItr);
307 }
Evan Chengb1290a62008-10-02 18:29:27 +0000308
Lang Hameseb6c8f52010-09-18 09:07:10 +0000309 // Also remove any aliases.
310 const unsigned *aliasItr = tri->getAliasSet(preg);
311 if (aliasItr != 0) {
312 for (; *aliasItr != 0; ++aliasItr) {
313 VRAllowed::iterator eraseItr =
314 std::find(vrAllowed.begin(), vrAllowed.end(), *aliasItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000315
Lang Hameseb6c8f52010-09-18 09:07:10 +0000316 if (eraseItr != vrAllowed.end()) {
317 vrAllowed.erase(eraseItr);
318 }
319 }
320 }
321 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000322
Lang Hameseb6c8f52010-09-18 09:07:10 +0000323 // Construct the node.
324 PBQP::Graph::NodeItr node =
325 g.addNode(PBQP::Vector(vrAllowed.size() + 1, 0));
Evan Chengb1290a62008-10-02 18:29:27 +0000326
Lang Hameseb6c8f52010-09-18 09:07:10 +0000327 // Record the mapping and allowed set in the problem.
328 p->recordVReg(vreg, node, vrAllowed.begin(), vrAllowed.end());
Evan Chengb1290a62008-10-02 18:29:27 +0000329
Lang Hameseb6c8f52010-09-18 09:07:10 +0000330 PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ?
331 vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000332
Lang Hameseb6c8f52010-09-18 09:07:10 +0000333 addSpillCosts(g.getNodeCosts(node), spillCost);
334 }
Evan Chengb1290a62008-10-02 18:29:27 +0000335
Lang Hames481630d2010-09-18 09:49:08 +0000336 for (RegSet::const_iterator vr1Itr = vregs.begin(), vrEnd = vregs.end();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000337 vr1Itr != vrEnd; ++vr1Itr) {
338 unsigned vr1 = *vr1Itr;
339 const LiveInterval &l1 = lis->getInterval(vr1);
340 const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1);
Evan Chengb1290a62008-10-02 18:29:27 +0000341
Benjamin Kramer9e8d1f92010-09-18 14:41:26 +0000342 for (RegSet::const_iterator vr2Itr = llvm::next(vr1Itr);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000343 vr2Itr != vrEnd; ++vr2Itr) {
344 unsigned vr2 = *vr2Itr;
345 const LiveInterval &l2 = lis->getInterval(vr2);
346 const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2);
Evan Chengb1290a62008-10-02 18:29:27 +0000347
Lang Hameseb6c8f52010-09-18 09:07:10 +0000348 assert(!l2.empty() && "Empty interval in vreg set?");
349 if (l1.overlaps(l2)) {
350 PBQP::Graph::EdgeItr edge =
351 g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2),
352 PBQP::Matrix(vr1Allowed.size()+1, vr2Allowed.size()+1, 0));
Lang Hames27601ef2008-11-16 12:12:54 +0000353
Lang Hameseb6c8f52010-09-18 09:07:10 +0000354 addInterferenceCosts(g.getEdgeCosts(edge), vr1Allowed, vr2Allowed, tri);
355 }
356 }
357 }
Evan Chengb1290a62008-10-02 18:29:27 +0000358
Lang Hameseb6c8f52010-09-18 09:07:10 +0000359 return p;
360}
Lang Hames27601ef2008-11-16 12:12:54 +0000361
Lang Hameseb6c8f52010-09-18 09:07:10 +0000362void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec,
363 PBQP::PBQPNum spillCost) {
364 costVec[0] = spillCost;
365}
Evan Chengb1290a62008-10-02 18:29:27 +0000366
Lang Hamese9c93562010-09-21 13:19:36 +0000367void PBQPBuilder::addInterferenceCosts(
368 PBQP::Matrix &costMat,
369 const PBQPRAProblem::AllowedSet &vr1Allowed,
370 const PBQPRAProblem::AllowedSet &vr2Allowed,
371 const TargetRegisterInfo *tri) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000372 assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch.");
373 assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch.");
374
375 for (unsigned i = 0; i < vr1Allowed.size(); ++i) {
376 unsigned preg1 = vr1Allowed[i];
377
378 for (unsigned j = 0; j < vr2Allowed.size(); ++j) {
379 unsigned preg2 = vr2Allowed[j];
380
381 if (tri->regsOverlap(preg1, preg2)) {
382 costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
383 }
384 }
385 }
Evan Chengb1290a62008-10-02 18:29:27 +0000386}
387
Lang Hamese9c93562010-09-21 13:19:36 +0000388std::auto_ptr<PBQPRAProblem> PBQPBuilderWithCoalescing::build(
389 MachineFunction *mf,
390 const LiveIntervals *lis,
391 const MachineLoopInfo *loopInfo,
392 const RegSet &vregs) {
393
394 std::auto_ptr<PBQPRAProblem> p = PBQPBuilder::build(mf, lis, loopInfo, vregs);
395 PBQP::Graph &g = p->getGraph();
396
397 const TargetMachine &tm = mf->getTarget();
398 CoalescerPair cp(*tm.getInstrInfo(), *tm.getRegisterInfo());
399
400 // Scan the machine function and add a coalescing cost whenever CoalescerPair
401 // gives the Ok.
402 for (MachineFunction::const_iterator mbbItr = mf->begin(),
403 mbbEnd = mf->end();
404 mbbItr != mbbEnd; ++mbbItr) {
405 const MachineBasicBlock *mbb = &*mbbItr;
406
407 for (MachineBasicBlock::const_iterator miItr = mbb->begin(),
408 miEnd = mbb->end();
409 miItr != miEnd; ++miItr) {
410 const MachineInstr *mi = &*miItr;
411
Lang Hamese9c93562010-09-21 13:19:36 +0000412 if (!cp.setRegisters(mi))
413 continue; // Not coalescable.
414
415 if (cp.getSrcReg() == cp.getDstReg())
416 continue; // Already coalesced.
417
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000418 unsigned dst = cp.getDstReg(),
419 src = cp.getSrcReg();
Lang Hamese9c93562010-09-21 13:19:36 +0000420
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000421 const float copyFactor = 0.5; // Cost of copy relative to load. Current
422 // value plucked randomly out of the air.
423
424 PBQP::PBQPNum cBenefit =
425 copyFactor * LiveIntervals::getSpillWeight(false, true,
426 loopInfo->getLoopDepth(mbb));
Lang Hamese9c93562010-09-21 13:19:36 +0000427
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000428 if (cp.isPhys()) {
429 if (!lis->isAllocatable(dst))
430 continue;
Lang Hamese9c93562010-09-21 13:19:36 +0000431
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000432 const PBQPRAProblem::AllowedSet &allowed = p->getAllowedSet(src);
433 unsigned pregOpt = 0;
434 while (pregOpt < allowed.size() && allowed[pregOpt] != dst)
435 ++pregOpt;
436 if (pregOpt < allowed.size()) {
437 ++pregOpt; // +1 to account for spill option.
438 PBQP::Graph::NodeItr node = p->getNodeForVReg(src);
439 addPhysRegCoalesce(g.getNodeCosts(node), pregOpt, cBenefit);
Lang Hamese9c93562010-09-21 13:19:36 +0000440 }
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000441 } else {
442 const PBQPRAProblem::AllowedSet *allowed1 = &p->getAllowedSet(dst);
443 const PBQPRAProblem::AllowedSet *allowed2 = &p->getAllowedSet(src);
444 PBQP::Graph::NodeItr node1 = p->getNodeForVReg(dst);
445 PBQP::Graph::NodeItr node2 = p->getNodeForVReg(src);
446 PBQP::Graph::EdgeItr edge = g.findEdge(node1, node2);
447 if (edge == g.edgesEnd()) {
448 edge = g.addEdge(node1, node2, PBQP::Matrix(allowed1->size() + 1,
449 allowed2->size() + 1,
450 0));
451 } else {
452 if (g.getEdgeNode1(edge) == node2) {
453 std::swap(node1, node2);
454 std::swap(allowed1, allowed2);
455 }
456 }
457
458 addVirtRegCoalesce(g.getEdgeCosts(edge), *allowed1, *allowed2,
459 cBenefit);
Lang Hamese9c93562010-09-21 13:19:36 +0000460 }
461 }
462 }
463
464 return p;
465}
466
Lang Hamese9c93562010-09-21 13:19:36 +0000467void PBQPBuilderWithCoalescing::addPhysRegCoalesce(PBQP::Vector &costVec,
468 unsigned pregOption,
469 PBQP::PBQPNum benefit) {
470 costVec[pregOption] += -benefit;
471}
472
473void PBQPBuilderWithCoalescing::addVirtRegCoalesce(
474 PBQP::Matrix &costMat,
475 const PBQPRAProblem::AllowedSet &vr1Allowed,
476 const PBQPRAProblem::AllowedSet &vr2Allowed,
477 PBQP::PBQPNum benefit) {
478
479 assert(costMat.getRows() == vr1Allowed.size() + 1 && "Size mismatch.");
480 assert(costMat.getCols() == vr2Allowed.size() + 1 && "Size mismatch.");
481
482 for (unsigned i = 0; i < vr1Allowed.size(); ++i) {
483 unsigned preg1 = vr1Allowed[i];
484 for (unsigned j = 0; j < vr2Allowed.size(); ++j) {
485 unsigned preg2 = vr2Allowed[j];
486
487 if (preg1 == preg2) {
488 costMat[i + 1][j + 1] += -benefit;
489 }
490 }
491 }
492}
Evan Chengb1290a62008-10-02 18:29:27 +0000493
Lang Hameseb6c8f52010-09-18 09:07:10 +0000494
495void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
496 au.addRequired<SlotIndexes>();
497 au.addPreserved<SlotIndexes>();
498 au.addRequired<LiveIntervals>();
499 //au.addRequiredID(SplitCriticalEdgesID);
500 au.addRequired<RegisterCoalescer>();
501 au.addRequired<CalculateSpillWeights>();
502 au.addRequired<LiveStacks>();
503 au.addPreserved<LiveStacks>();
504 au.addRequired<MachineLoopInfo>();
505 au.addPreserved<MachineLoopInfo>();
506 if (pbqpPreSplitting)
507 au.addRequired<LoopSplitter>();
508 au.addRequired<VirtRegMap>();
509 au.addRequired<RenderMachineFunction>();
510 MachineFunctionPass::getAnalysisUsage(au);
511}
512
Lang Hames27601ef2008-11-16 12:12:54 +0000513template <typename RegContainer>
Lang Hameseb6c8f52010-09-18 09:07:10 +0000514PBQP::Vector RegAllocPBQP::buildCostVector(unsigned vReg,
Lang Hames6699fb22009-08-06 23:32:48 +0000515 const RegContainer &allowed,
516 const CoalesceMap &coalesces,
517 PBQP::PBQPNum spillCost) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000518
Lang Hames27601ef2008-11-16 12:12:54 +0000519 typedef typename RegContainer::const_iterator AllowedItr;
520
Evan Chengb1290a62008-10-02 18:29:27 +0000521 // Allocate vector. Additional element (0th) used for spill option
Lang Hames6699fb22009-08-06 23:32:48 +0000522 PBQP::Vector v(allowed.size() + 1, 0);
Evan Chengb1290a62008-10-02 18:29:27 +0000523
Lang Hames6699fb22009-08-06 23:32:48 +0000524 v[0] = spillCost;
Evan Chengb1290a62008-10-02 18:29:27 +0000525
Lang Hames27601ef2008-11-16 12:12:54 +0000526 // Iterate over the allowed registers inserting coalesce benefits if there
527 // are any.
528 unsigned ai = 0;
529 for (AllowedItr itr = allowed.begin(), end = allowed.end();
530 itr != end; ++itr, ++ai) {
531
532 unsigned pReg = *itr;
533
534 CoalesceMap::const_iterator cmItr =
535 coalesces.find(RegPair(vReg, pReg));
536
537 // No coalesce - on to the next preg.
538 if (cmItr == coalesces.end())
539 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000540
541 // We have a coalesce - insert the benefit.
Lang Hames6699fb22009-08-06 23:32:48 +0000542 v[ai + 1] = -cmItr->second;
Lang Hames27601ef2008-11-16 12:12:54 +0000543 }
544
Evan Chengb1290a62008-10-02 18:29:27 +0000545 return v;
546}
547
Lang Hames27601ef2008-11-16 12:12:54 +0000548template <typename RegContainer>
Lang Hameseb6c8f52010-09-18 09:07:10 +0000549PBQP::Matrix* RegAllocPBQP::buildInterferenceMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000550 const RegContainer &allowed1, const RegContainer &allowed2) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000551
Lang Hames27601ef2008-11-16 12:12:54 +0000552 typedef typename RegContainer::const_iterator RegContainerIterator;
Evan Chengb1290a62008-10-02 18:29:27 +0000553
554 // Construct a PBQP matrix representing the cost of allocation options. The
555 // rows and columns correspond to the allocation options for the two live
556 // intervals. Elements will be infinite where corresponding registers alias,
557 // since we cannot allocate aliasing registers to interfering live intervals.
558 // All other elements (non-aliasing combinations) will have zero cost. Note
559 // that the spill option (element 0,0) has zero cost, since we can allocate
560 // both intervals to memory safely (the cost for each individual allocation
561 // to memory is accounted for by the cost vectors for each live interval).
Lang Hames6699fb22009-08-06 23:32:48 +0000562 PBQP::Matrix *m =
563 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Misha Brukman2a835f92009-01-08 15:50:22 +0000564
Evan Chengb1290a62008-10-02 18:29:27 +0000565 // Assume this is a zero matrix until proven otherwise. Zero matrices occur
566 // between interfering live ranges with non-overlapping register sets (e.g.
567 // non-overlapping reg classes, or disjoint sets of allowed regs within the
568 // same class). The term "overlapping" is used advisedly: sets which do not
569 // intersect, but contain registers which alias, will have non-zero matrices.
570 // We optimize zero matrices away to improve solver speed.
571 bool isZeroMatrix = true;
572
573
574 // Row index. Starts at 1, since the 0th row is for the spill option, which
575 // is always zero.
Misha Brukman2a835f92009-01-08 15:50:22 +0000576 unsigned ri = 1;
Evan Chengb1290a62008-10-02 18:29:27 +0000577
Misha Brukman2a835f92009-01-08 15:50:22 +0000578 // Iterate over allowed sets, insert infinities where required.
Lang Hames27601ef2008-11-16 12:12:54 +0000579 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000580 a1Itr != a1End; ++a1Itr) {
581
582 // Column index, starts at 1 as for row index.
583 unsigned ci = 1;
584 unsigned reg1 = *a1Itr;
585
Lang Hames27601ef2008-11-16 12:12:54 +0000586 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000587 a2Itr != a2End; ++a2Itr) {
588
589 unsigned reg2 = *a2Itr;
590
591 // If the row/column regs are identical or alias insert an infinity.
Lang Hames3f2f3f52009-09-03 02:52:02 +0000592 if (tri->regsOverlap(reg1, reg2)) {
Lang Hames6699fb22009-08-06 23:32:48 +0000593 (*m)[ri][ci] = std::numeric_limits<PBQP::PBQPNum>::infinity();
Evan Chengb1290a62008-10-02 18:29:27 +0000594 isZeroMatrix = false;
595 }
596
597 ++ci;
598 }
599
600 ++ri;
601 }
602
603 // If this turns out to be a zero matrix...
604 if (isZeroMatrix) {
605 // free it and return null.
606 delete m;
607 return 0;
608 }
609
610 // ...otherwise return the cost matrix.
611 return m;
612}
613
Lang Hames27601ef2008-11-16 12:12:54 +0000614template <typename RegContainer>
Lang Hameseb6c8f52010-09-18 09:07:10 +0000615PBQP::Matrix* RegAllocPBQP::buildCoalescingMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000616 const RegContainer &allowed1, const RegContainer &allowed2,
Lang Hames6699fb22009-08-06 23:32:48 +0000617 PBQP::PBQPNum cBenefit) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000618
Lang Hames27601ef2008-11-16 12:12:54 +0000619 typedef typename RegContainer::const_iterator RegContainerIterator;
620
621 // Construct a PBQP Matrix representing the benefits of coalescing. As with
622 // interference matrices the rows and columns represent allowed registers
623 // for the LiveIntervals which are (potentially) to be coalesced. The amount
624 // -cBenefit will be placed in any element representing the same register
625 // for both intervals.
Lang Hames6699fb22009-08-06 23:32:48 +0000626 PBQP::Matrix *m =
627 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Lang Hames27601ef2008-11-16 12:12:54 +0000628
629 // Reset costs to zero.
630 m->reset(0);
631
632 // Assume the matrix is zero till proven otherwise. Zero matrices will be
633 // optimized away as in the interference case.
634 bool isZeroMatrix = true;
635
636 // Row index. Starts at 1, since the 0th row is for the spill option, which
637 // is always zero.
638 unsigned ri = 1;
639
640 // Iterate over the allowed sets, insert coalescing benefits where
641 // appropriate.
642 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
643 a1Itr != a1End; ++a1Itr) {
644
645 // Column index, starts at 1 as for row index.
646 unsigned ci = 1;
647 unsigned reg1 = *a1Itr;
648
649 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
650 a2Itr != a2End; ++a2Itr) {
651
652 // If the row and column represent the same register insert a beneficial
653 // cost to preference this allocation - it would allow us to eliminate a
Misha Brukman2a835f92009-01-08 15:50:22 +0000654 // move instruction.
Lang Hames27601ef2008-11-16 12:12:54 +0000655 if (reg1 == *a2Itr) {
656 (*m)[ri][ci] = -cBenefit;
657 isZeroMatrix = false;
658 }
659
660 ++ci;
661 }
662
663 ++ri;
664 }
665
666 // If this turns out to be a zero matrix...
667 if (isZeroMatrix) {
668 // ...free it and return null.
669 delete m;
670 return 0;
671 }
672
673 return m;
674}
675
Lang Hameseb6c8f52010-09-18 09:07:10 +0000676RegAllocPBQP::CoalesceMap RegAllocPBQP::findCoalesces() {
Lang Hames27601ef2008-11-16 12:12:54 +0000677
678 typedef MachineFunction::const_iterator MFIterator;
679 typedef MachineBasicBlock::const_iterator MBBIterator;
680 typedef LiveInterval::const_vni_iterator VNIIterator;
Misha Brukman2a835f92009-01-08 15:50:22 +0000681
Lang Hames27601ef2008-11-16 12:12:54 +0000682 CoalesceMap coalescesFound;
683
684 // To find coalesces we need to iterate over the function looking for
685 // copy instructions.
686 for (MFIterator bbItr = mf->begin(), bbEnd = mf->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000687 bbItr != bbEnd; ++bbItr) {
688
689 const MachineBasicBlock *mbb = &*bbItr;
Evan Chengb1290a62008-10-02 18:29:27 +0000690
Lang Hames27601ef2008-11-16 12:12:54 +0000691 for (MBBIterator iItr = mbb->begin(), iEnd = mbb->end();
692 iItr != iEnd; ++iItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000693
694 const MachineInstr *instr = &*iItr;
695
Lang Hames27601ef2008-11-16 12:12:54 +0000696 // If this isn't a copy then continue to the next instruction.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000697 if (!instr->isCopy())
Lang Hames27601ef2008-11-16 12:12:54 +0000698 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000699
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000700 unsigned srcReg = instr->getOperand(1).getReg();
701 unsigned dstReg = instr->getOperand(0).getReg();
702
Lang Hames27601ef2008-11-16 12:12:54 +0000703 // If the registers are already the same our job is nice and easy.
704 if (dstReg == srcReg)
705 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000706
Lang Hames27601ef2008-11-16 12:12:54 +0000707 bool srcRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(srcReg),
708 dstRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(dstReg);
709
710 // If both registers are physical then we can't coalesce.
711 if (srcRegIsPhysical && dstRegIsPhysical)
712 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000713
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000714 // If it's a copy that includes two virtual register but the source and
715 // destination classes differ then we can't coalesce.
716 if (!srcRegIsPhysical && !dstRegIsPhysical &&
717 mri->getRegClass(srcReg) != mri->getRegClass(dstReg))
Lang Hames27601ef2008-11-16 12:12:54 +0000718 continue;
719
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000720 // If one is physical and one is virtual, check that the physical is
721 // allocatable in the class of the virtual.
722 if (srcRegIsPhysical && !dstRegIsPhysical) {
723 const TargetRegisterClass *dstRegClass = mri->getRegClass(dstReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000724 if (std::find(dstRegClass->allocation_order_begin(*mf),
725 dstRegClass->allocation_order_end(*mf), srcReg) ==
726 dstRegClass->allocation_order_end(*mf))
Evan Chengb1290a62008-10-02 18:29:27 +0000727 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000728 }
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000729 if (!srcRegIsPhysical && dstRegIsPhysical) {
730 const TargetRegisterClass *srcRegClass = mri->getRegClass(srcReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000731 if (std::find(srcRegClass->allocation_order_begin(*mf),
732 srcRegClass->allocation_order_end(*mf), dstReg) ==
733 srcRegClass->allocation_order_end(*mf))
Lang Hames27601ef2008-11-16 12:12:54 +0000734 continue;
735 }
736
737 // If we've made it here we have a copy with compatible register classes.
Misha Brukman2a835f92009-01-08 15:50:22 +0000738 // We can probably coalesce, but we need to consider overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000739 const LiveInterval *srcLI = &lis->getInterval(srcReg),
740 *dstLI = &lis->getInterval(dstReg);
741
742 if (srcLI->overlaps(*dstLI)) {
743 // Even in the case of an overlap we might still be able to coalesce,
744 // but we need to make sure that no definition of either range occurs
745 // while the other range is live.
746
747 // Otherwise start by assuming we're ok.
748 bool badDef = false;
749
750 // Test all defs of the source range.
Misha Brukman2a835f92009-01-08 15:50:22 +0000751 for (VNIIterator
Lang Hames27601ef2008-11-16 12:12:54 +0000752 vniItr = srcLI->vni_begin(), vniEnd = srcLI->vni_end();
753 vniItr != vniEnd; ++vniItr) {
754
Lang Hames0b23dc02010-02-09 00:50:27 +0000755 // If we find a poorly defined def we err on the side of caution.
756 if (!(*vniItr)->def.isValid()) {
757 badDef = true;
758 break;
759 }
760
Lang Hames27601ef2008-11-16 12:12:54 +0000761 // If we find a def that kills the coalescing opportunity then
762 // record it and break from the loop.
763 if (dstLI->liveAt((*vniItr)->def)) {
764 badDef = true;
765 break;
766 }
767 }
768
769 // If we have a bad def give up, continue to the next instruction.
770 if (badDef)
771 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000772
Lang Hames27601ef2008-11-16 12:12:54 +0000773 // Otherwise test definitions of the destination range.
774 for (VNIIterator
775 vniItr = dstLI->vni_begin(), vniEnd = dstLI->vni_end();
776 vniItr != vniEnd; ++vniItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000777
Lang Hames27601ef2008-11-16 12:12:54 +0000778 // We want to make sure we skip the copy instruction itself.
Lang Hames52c1afc2009-08-10 23:43:28 +0000779 if ((*vniItr)->getCopy() == instr)
Lang Hames27601ef2008-11-16 12:12:54 +0000780 continue;
781
Lang Hames0b23dc02010-02-09 00:50:27 +0000782 if (!(*vniItr)->def.isValid()) {
783 badDef = true;
784 break;
785 }
786
Lang Hames27601ef2008-11-16 12:12:54 +0000787 if (srcLI->liveAt((*vniItr)->def)) {
788 badDef = true;
789 break;
790 }
791 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000792
Lang Hames27601ef2008-11-16 12:12:54 +0000793 // As before a bad def we give up and continue to the next instr.
794 if (badDef)
795 continue;
796 }
797
798 // If we make it to here then either the ranges didn't overlap, or they
799 // did, but none of their definitions would prevent us from coalescing.
800 // We're good to go with the coalesce.
801
Chris Lattner87565c12010-05-15 17:10:24 +0000802 float cBenefit = std::pow(10.0f, (float)loopInfo->getLoopDepth(mbb)) / 5.0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000803
Lang Hames27601ef2008-11-16 12:12:54 +0000804 coalescesFound[RegPair(srcReg, dstReg)] = cBenefit;
805 coalescesFound[RegPair(dstReg, srcReg)] = cBenefit;
Evan Chengb1290a62008-10-02 18:29:27 +0000806 }
807
808 }
809
Lang Hames27601ef2008-11-16 12:12:54 +0000810 return coalescesFound;
811}
812
Lang Hameseb6c8f52010-09-18 09:07:10 +0000813void RegAllocPBQP::findVRegIntervalsToAlloc() {
Lang Hames27601ef2008-11-16 12:12:54 +0000814
815 // Iterate over all live ranges.
816 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
817 itr != end; ++itr) {
818
819 // Ignore physical ones.
820 if (TargetRegisterInfo::isPhysicalRegister(itr->first))
821 continue;
822
823 LiveInterval *li = itr->second;
824
825 // If this live interval is non-empty we will use pbqp to allocate it.
826 // Empty intervals we allocate in a simple post-processing stage in
827 // finalizeAlloc.
828 if (!li->empty()) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000829 vregsToAlloc.insert(li->reg);
Lang Hames27601ef2008-11-16 12:12:54 +0000830 }
831 else {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000832 emptyIntervalVRegs.insert(li->reg);
Lang Hames27601ef2008-11-16 12:12:54 +0000833 }
834 }
Evan Chengb1290a62008-10-02 18:29:27 +0000835}
836
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000837PBQP::Graph RegAllocPBQP::constructPBQPProblemOld() {
Evan Chengb1290a62008-10-02 18:29:27 +0000838
839 typedef std::vector<const LiveInterval*> LIVector;
Lang Hames27601ef2008-11-16 12:12:54 +0000840 typedef std::vector<unsigned> RegVector;
Evan Chengb1290a62008-10-02 18:29:27 +0000841
Lang Hames27601ef2008-11-16 12:12:54 +0000842 // This will store the physical intervals for easy reference.
843 LIVector physIntervals;
Evan Chengb1290a62008-10-02 18:29:27 +0000844
845 // Start by clearing the old node <-> live interval mappings & allowed sets
846 li2Node.clear();
847 node2LI.clear();
848 allowedSets.clear();
849
Lang Hames27601ef2008-11-16 12:12:54 +0000850 // Populate physIntervals, update preg use:
851 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000852 itr != end; ++itr) {
853
Evan Chengb1290a62008-10-02 18:29:27 +0000854 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
855 physIntervals.push_back(itr->second);
856 mri->setPhysRegUsed(itr->second->reg);
857 }
Evan Chengb1290a62008-10-02 18:29:27 +0000858 }
859
Lang Hames27601ef2008-11-16 12:12:54 +0000860 // Iterate over vreg intervals, construct live interval <-> node number
861 // mappings.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000862 for (RegSet::const_iterator itr = vregsToAlloc.begin(),
863 end = vregsToAlloc.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000864 itr != end; ++itr) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000865 const LiveInterval *li = &lis->getInterval(*itr);
Lang Hames27601ef2008-11-16 12:12:54 +0000866
867 li2Node[li] = node2LI.size();
868 node2LI.push_back(li);
869 }
870
871 // Get the set of potential coalesces.
Lang Hames8481e3b2009-08-19 01:36:14 +0000872 CoalesceMap coalesces;
873
874 if (pbqpCoalescing) {
875 coalesces = findCoalesces();
876 }
Evan Chengb1290a62008-10-02 18:29:27 +0000877
878 // Construct a PBQP solver for this problem
Lang Hames030c4bf2010-01-26 04:49:58 +0000879 PBQP::Graph problem;
Lang Hameseb6c8f52010-09-18 09:07:10 +0000880 problemNodes.resize(vregsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000881
882 // Resize allowedSets container appropriately.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000883 allowedSets.resize(vregsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000884
Jim Grosbach269354e2010-09-01 21:23:03 +0000885 BitVector ReservedRegs = tri->getReservedRegs(*mf);
886
Evan Chengb1290a62008-10-02 18:29:27 +0000887 // Iterate over virtual register intervals to compute allowed sets...
888 for (unsigned node = 0; node < node2LI.size(); ++node) {
889
890 // Grab pointers to the interval and its register class.
891 const LiveInterval *li = node2LI[node];
892 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000893
Evan Chengb1290a62008-10-02 18:29:27 +0000894 // Start by assuming all allocable registers in the class are allowed...
Jim Grosbach269354e2010-09-01 21:23:03 +0000895 RegVector liAllowed;
896 TargetRegisterClass::iterator aob = liRC->allocation_order_begin(*mf);
897 TargetRegisterClass::iterator aoe = liRC->allocation_order_end(*mf);
898 for (TargetRegisterClass::iterator it = aob; it != aoe; ++it)
899 if (!ReservedRegs.test(*it))
900 liAllowed.push_back(*it);
Evan Chengb1290a62008-10-02 18:29:27 +0000901
Lang Hames27601ef2008-11-16 12:12:54 +0000902 // Eliminate the physical registers which overlap with this range, along
903 // with all their aliases.
904 for (LIVector::iterator pItr = physIntervals.begin(),
905 pEnd = physIntervals.end(); pItr != pEnd; ++pItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000906
Lang Hames27601ef2008-11-16 12:12:54 +0000907 if (!li->overlaps(**pItr))
908 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000909
Lang Hames27601ef2008-11-16 12:12:54 +0000910 unsigned pReg = (*pItr)->reg;
Evan Chengb1290a62008-10-02 18:29:27 +0000911
Lang Hames27601ef2008-11-16 12:12:54 +0000912 // If we get here then the live intervals overlap, but we're still ok
913 // if they're coalescable.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000914 if (coalesces.find(RegPair(li->reg, pReg)) != coalesces.end()) {
915 DEBUG(dbgs() << "CoalescingOverride: (" << li->reg << ", " << pReg << ")\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000916 continue;
Lang Hameseb6c8f52010-09-18 09:07:10 +0000917 }
Evan Chengb1290a62008-10-02 18:29:27 +0000918
Lang Hames27601ef2008-11-16 12:12:54 +0000919 // If we get here then we have a genuine exclusion.
Evan Chengb1290a62008-10-02 18:29:27 +0000920
Lang Hames27601ef2008-11-16 12:12:54 +0000921 // Remove the overlapping reg...
922 RegVector::iterator eraseItr =
923 std::find(liAllowed.begin(), liAllowed.end(), pReg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000924
Lang Hames27601ef2008-11-16 12:12:54 +0000925 if (eraseItr != liAllowed.end())
926 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000927
Lang Hames27601ef2008-11-16 12:12:54 +0000928 const unsigned *aliasItr = tri->getAliasSet(pReg);
929
930 if (aliasItr != 0) {
931 // ...and its aliases.
932 for (; *aliasItr != 0; ++aliasItr) {
933 RegVector::iterator eraseItr =
934 std::find(liAllowed.begin(), liAllowed.end(), *aliasItr);
Misha Brukman2a835f92009-01-08 15:50:22 +0000935
Lang Hames27601ef2008-11-16 12:12:54 +0000936 if (eraseItr != liAllowed.end()) {
937 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000938 }
Evan Chengb1290a62008-10-02 18:29:27 +0000939 }
Evan Chengb1290a62008-10-02 18:29:27 +0000940 }
Evan Chengb1290a62008-10-02 18:29:27 +0000941 }
942
943 // Copy the allowed set into a member vector for use when constructing cost
944 // vectors & matrices, and mapping PBQP solutions back to assignments.
945 allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end());
946
947 // Set the spill cost to the interval weight, or epsilon if the
948 // interval weight is zero
Lang Hames6699fb22009-08-06 23:32:48 +0000949 PBQP::PBQPNum spillCost = (li->weight != 0.0) ?
950 li->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000951
952 // Build a cost vector for this interval.
Lang Hames6699fb22009-08-06 23:32:48 +0000953 problemNodes[node] =
954 problem.addNode(
955 buildCostVector(li->reg, allowedSets[node], coalesces, spillCost));
Evan Chengb1290a62008-10-02 18:29:27 +0000956
957 }
958
Lang Hames27601ef2008-11-16 12:12:54 +0000959
Evan Chengb1290a62008-10-02 18:29:27 +0000960 // Now add the cost matrices...
961 for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) {
Evan Chengb1290a62008-10-02 18:29:27 +0000962 const LiveInterval *li = node2LI[node1];
963
Evan Chengb1290a62008-10-02 18:29:27 +0000964 // Test for live range overlaps and insert interference matrices.
965 for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) {
966 const LiveInterval *li2 = node2LI[node2];
967
Lang Hames27601ef2008-11-16 12:12:54 +0000968 CoalesceMap::const_iterator cmItr =
969 coalesces.find(RegPair(li->reg, li2->reg));
Evan Chengb1290a62008-10-02 18:29:27 +0000970
Lang Hames6699fb22009-08-06 23:32:48 +0000971 PBQP::Matrix *m = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000972
Lang Hames27601ef2008-11-16 12:12:54 +0000973 if (cmItr != coalesces.end()) {
974 m = buildCoalescingMatrix(allowedSets[node1], allowedSets[node2],
975 cmItr->second);
976 }
977 else if (li->overlaps(*li2)) {
978 m = buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]);
979 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000980
Lang Hames27601ef2008-11-16 12:12:54 +0000981 if (m != 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000982 problem.addEdge(problemNodes[node1],
983 problemNodes[node2],
984 *m);
985
Lang Hames27601ef2008-11-16 12:12:54 +0000986 delete m;
Evan Chengb1290a62008-10-02 18:29:27 +0000987 }
988 }
989 }
990
Lang Hames6699fb22009-08-06 23:32:48 +0000991 assert(problem.getNumNodes() == allowedSets.size());
Lang Hames6699fb22009-08-06 23:32:48 +0000992/*
993 std::cerr << "Allocating for " << problem.getNumNodes() << " nodes, "
994 << problem.getNumEdges() << " edges.\n";
995
996 problem.printDot(std::cerr);
997*/
Evan Chengb1290a62008-10-02 18:29:27 +0000998 // We're done, PBQP problem constructed - return it.
Lang Hames6699fb22009-08-06 23:32:48 +0000999 return problem;
Evan Chengb1290a62008-10-02 18:29:27 +00001000}
1001
Lang Hameseb6c8f52010-09-18 09:07:10 +00001002void RegAllocPBQP::addStackInterval(const LiveInterval *spilled,
Evan Chengc781a242009-05-03 18:32:42 +00001003 MachineRegisterInfo* mri) {
Lang Hames27601ef2008-11-16 12:12:54 +00001004 int stackSlot = vrm->getStackSlot(spilled->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +00001005
1006 if (stackSlot == VirtRegMap::NO_STACK_SLOT)
Lang Hames27601ef2008-11-16 12:12:54 +00001007 return;
1008
Evan Chengc781a242009-05-03 18:32:42 +00001009 const TargetRegisterClass *RC = mri->getRegClass(spilled->reg);
1010 LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC);
Lang Hames27601ef2008-11-16 12:12:54 +00001011
1012 VNInfo *vni;
1013 if (stackInterval.getNumValNums() != 0)
1014 vni = stackInterval.getValNumInfo(0);
1015 else
Lang Hames86511252009-09-04 20:41:11 +00001016 vni = stackInterval.getNextValue(
Lang Hames6e2968c2010-09-25 12:04:16 +00001017 SlotIndex(), 0, lss->getVNInfoAllocator());
Lang Hames27601ef2008-11-16 12:12:54 +00001018
1019 LiveInterval &rhsInterval = lis->getInterval(spilled->reg);
1020 stackInterval.MergeRangesInAsValue(rhsInterval, vni);
1021}
1022
Lang Hamesf70e7cc2010-09-23 04:28:54 +00001023bool RegAllocPBQP::mapPBQPToRegAllocOld(const PBQP::Solution &solution) {
Lang Hamese98b4b02009-11-15 04:39:51 +00001024
Evan Chengb1290a62008-10-02 18:29:27 +00001025 // Set to true if we have any spills
1026 bool anotherRoundNeeded = false;
1027
1028 // Clear the existing allocation.
1029 vrm->clearAllVirt();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001030
Evan Chengb1290a62008-10-02 18:29:27 +00001031 // Iterate over the nodes mapping the PBQP solution to a register assignment.
1032 for (unsigned node = 0; node < node2LI.size(); ++node) {
Lang Hames27601ef2008-11-16 12:12:54 +00001033 unsigned virtReg = node2LI[node]->reg,
Lang Hames030c4bf2010-01-26 04:49:58 +00001034 allocSelection = solution.getSelection(problemNodes[node]);
Lang Hames6699fb22009-08-06 23:32:48 +00001035
Evan Chengb1290a62008-10-02 18:29:27 +00001036
1037 // If the PBQP solution is non-zero it's a physical register...
1038 if (allocSelection != 0) {
1039 // Get the physical reg, subtracting 1 to account for the spill option.
1040 unsigned physReg = allowedSets[node][allocSelection - 1];
1041
David Greene30931542010-01-05 01:25:43 +00001042 DEBUG(dbgs() << "VREG " << virtReg << " -> "
Lang Hameseb6c8f52010-09-18 09:07:10 +00001043 << tri->getName(physReg) << " (Option: " << allocSelection << ")\n");
Lang Hames27601ef2008-11-16 12:12:54 +00001044
1045 assert(physReg != 0);
1046
Evan Chengb1290a62008-10-02 18:29:27 +00001047 // Add to the virt reg map and update the used phys regs.
Lang Hames27601ef2008-11-16 12:12:54 +00001048 vrm->assignVirt2Phys(virtReg, physReg);
Evan Chengb1290a62008-10-02 18:29:27 +00001049 }
1050 // ...Otherwise it's a spill.
1051 else {
1052
1053 // Make sure we ignore this virtual reg on the next round
1054 // of allocation
Lang Hameseb6c8f52010-09-18 09:07:10 +00001055 vregsToAlloc.erase(virtReg);
Evan Chengb1290a62008-10-02 18:29:27 +00001056
Evan Chengb1290a62008-10-02 18:29:27 +00001057 // Insert spill ranges for this live range
Lang Hames27601ef2008-11-16 12:12:54 +00001058 const LiveInterval *spillInterval = node2LI[node];
1059 double oldSpillWeight = spillInterval->weight;
Evan Chengb1290a62008-10-02 18:29:27 +00001060 SmallVector<LiveInterval*, 8> spillIs;
Lang Hames33198392010-09-02 08:27:00 +00001061 rmf->rememberUseDefs(spillInterval);
Evan Chengb1290a62008-10-02 18:29:27 +00001062 std::vector<LiveInterval*> newSpills =
Evan Chengc781a242009-05-03 18:32:42 +00001063 lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm);
1064 addStackInterval(spillInterval, mri);
Lang Hames33198392010-09-02 08:27:00 +00001065 rmf->rememberSpills(spillInterval, newSpills);
Lang Hames27601ef2008-11-16 12:12:54 +00001066
Daniel Dunbarbc84ad92009-08-20 20:01:34 +00001067 (void) oldSpillWeight;
Lang Hameseb6c8f52010-09-18 09:07:10 +00001068 DEBUG(dbgs() << "VREG " << virtReg << " -> SPILLED (Option: 0, Cost: "
Lang Hames233fd9c2009-08-18 23:34:50 +00001069 << oldSpillWeight << ", New vregs: ");
Lang Hames27601ef2008-11-16 12:12:54 +00001070
1071 // Copy any newly inserted live intervals into the list of regs to
1072 // allocate.
1073 for (std::vector<LiveInterval*>::const_iterator
1074 itr = newSpills.begin(), end = newSpills.end();
1075 itr != end; ++itr) {
1076
1077 assert(!(*itr)->empty() && "Empty spill range.");
1078
David Greene30931542010-01-05 01:25:43 +00001079 DEBUG(dbgs() << (*itr)->reg << " ");
Lang Hames27601ef2008-11-16 12:12:54 +00001080
Lang Hameseb6c8f52010-09-18 09:07:10 +00001081 vregsToAlloc.insert((*itr)->reg);
Lang Hames27601ef2008-11-16 12:12:54 +00001082 }
1083
David Greene30931542010-01-05 01:25:43 +00001084 DEBUG(dbgs() << ")\n");
Evan Chengb1290a62008-10-02 18:29:27 +00001085
1086 // We need another round if spill intervals were added.
1087 anotherRoundNeeded |= !newSpills.empty();
1088 }
1089 }
1090
1091 return !anotherRoundNeeded;
1092}
1093
Lang Hamesf70e7cc2010-09-23 04:28:54 +00001094bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAProblem &problem,
1095 const PBQP::Solution &solution) {
Lang Hameseb6c8f52010-09-18 09:07:10 +00001096 // Set to true if we have any spills
1097 bool anotherRoundNeeded = false;
1098
1099 // Clear the existing allocation.
1100 vrm->clearAllVirt();
1101
1102 const PBQP::Graph &g = problem.getGraph();
1103 // Iterate over the nodes mapping the PBQP solution to a register
1104 // assignment.
1105 for (PBQP::Graph::ConstNodeItr node = g.nodesBegin(),
1106 nodeEnd = g.nodesEnd();
1107 node != nodeEnd; ++node) {
1108 unsigned vreg = problem.getVRegForNode(node);
1109 unsigned alloc = solution.getSelection(node);
1110
1111 if (problem.isPRegOption(vreg, alloc)) {
1112 unsigned preg = problem.getPRegForOption(vreg, alloc);
1113 DEBUG(dbgs() << "VREG " << vreg << " -> " << tri->getName(preg) << "\n");
1114 assert(preg != 0 && "Invalid preg selected.");
1115 vrm->assignVirt2Phys(vreg, preg);
1116 } else if (problem.isSpillOption(vreg, alloc)) {
1117 vregsToAlloc.erase(vreg);
1118 const LiveInterval* spillInterval = &lis->getInterval(vreg);
1119 double oldWeight = spillInterval->weight;
1120 SmallVector<LiveInterval*, 8> spillIs;
1121 rmf->rememberUseDefs(spillInterval);
1122 std::vector<LiveInterval*> newSpills =
1123 lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm);
1124 addStackInterval(spillInterval, mri);
1125 rmf->rememberSpills(spillInterval, newSpills);
1126
1127 (void) oldWeight;
1128 DEBUG(dbgs() << "VREG " << vreg << " -> SPILLED (Cost: "
1129 << oldWeight << ", New vregs: ");
1130
1131 // Copy any newly inserted live intervals into the list of regs to
1132 // allocate.
1133 for (std::vector<LiveInterval*>::const_iterator
1134 itr = newSpills.begin(), end = newSpills.end();
1135 itr != end; ++itr) {
1136 assert(!(*itr)->empty() && "Empty spill range.");
1137 DEBUG(dbgs() << (*itr)->reg << " ");
1138 vregsToAlloc.insert((*itr)->reg);
1139 }
1140
1141 DEBUG(dbgs() << ")\n");
1142
1143 // We need another round if spill intervals were added.
1144 anotherRoundNeeded |= !newSpills.empty();
1145 } else {
1146 assert(false && "Unknown allocation option.");
1147 }
1148 }
1149
1150 return !anotherRoundNeeded;
1151}
1152
1153
1154void RegAllocPBQP::finalizeAlloc() const {
Lang Hames27601ef2008-11-16 12:12:54 +00001155 typedef LiveIntervals::iterator LIIterator;
1156 typedef LiveInterval::Ranges::const_iterator LRIterator;
1157
1158 // First allocate registers for the empty intervals.
Lang Hameseb6c8f52010-09-18 09:07:10 +00001159 for (RegSet::const_iterator
1160 itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end();
Lang Hames27601ef2008-11-16 12:12:54 +00001161 itr != end; ++itr) {
Lang Hameseb6c8f52010-09-18 09:07:10 +00001162 LiveInterval *li = &lis->getInterval(*itr);
Lang Hames27601ef2008-11-16 12:12:54 +00001163
Evan Cheng90f95f82009-06-14 20:22:55 +00001164 unsigned physReg = vrm->getRegAllocPref(li->reg);
Lang Hames6699fb22009-08-06 23:32:48 +00001165
Lang Hames27601ef2008-11-16 12:12:54 +00001166 if (physReg == 0) {
1167 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +00001168 physReg = *liRC->allocation_order_begin(*mf);
Lang Hames27601ef2008-11-16 12:12:54 +00001169 }
Misha Brukman2a835f92009-01-08 15:50:22 +00001170
1171 vrm->assignVirt2Phys(li->reg, physReg);
Lang Hames27601ef2008-11-16 12:12:54 +00001172 }
Misha Brukman2a835f92009-01-08 15:50:22 +00001173
Lang Hames27601ef2008-11-16 12:12:54 +00001174 // Finally iterate over the basic blocks to compute and set the live-in sets.
1175 SmallVector<MachineBasicBlock*, 8> liveInMBBs;
1176 MachineBasicBlock *entryMBB = &*mf->begin();
1177
1178 for (LIIterator liItr = lis->begin(), liEnd = lis->end();
1179 liItr != liEnd; ++liItr) {
1180
1181 const LiveInterval *li = liItr->second;
1182 unsigned reg = 0;
Misha Brukman2a835f92009-01-08 15:50:22 +00001183
Lang Hames27601ef2008-11-16 12:12:54 +00001184 // Get the physical register for this interval
1185 if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
1186 reg = li->reg;
1187 }
1188 else if (vrm->isAssignedReg(li->reg)) {
1189 reg = vrm->getPhys(li->reg);
1190 }
1191 else {
1192 // Ranges which are assigned a stack slot only are ignored.
1193 continue;
1194 }
1195
Lang Hamesb0e519f2009-05-17 23:50:36 +00001196 if (reg == 0) {
Lang Hames6699fb22009-08-06 23:32:48 +00001197 // Filter out zero regs - they're for intervals that were spilled.
Lang Hamesb0e519f2009-05-17 23:50:36 +00001198 continue;
1199 }
1200
Lang Hames27601ef2008-11-16 12:12:54 +00001201 // Iterate over the ranges of the current interval...
1202 for (LRIterator lrItr = li->begin(), lrEnd = li->end();
1203 lrItr != lrEnd; ++lrItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +00001204
Lang Hames27601ef2008-11-16 12:12:54 +00001205 // Find the set of basic blocks which this range is live into...
1206 if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
1207 // And add the physreg for this interval to their live-in sets.
1208 for (unsigned i = 0; i < liveInMBBs.size(); ++i) {
1209 if (liveInMBBs[i] != entryMBB) {
1210 if (!liveInMBBs[i]->isLiveIn(reg)) {
1211 liveInMBBs[i]->addLiveIn(reg);
1212 }
1213 }
1214 }
1215 liveInMBBs.clear();
1216 }
1217 }
1218 }
Misha Brukman2a835f92009-01-08 15:50:22 +00001219
Lang Hames27601ef2008-11-16 12:12:54 +00001220}
1221
Lang Hameseb6c8f52010-09-18 09:07:10 +00001222bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
Lang Hames27601ef2008-11-16 12:12:54 +00001223
Evan Chengb1290a62008-10-02 18:29:27 +00001224 mf = &MF;
1225 tm = &mf->getTarget();
1226 tri = tm->getRegisterInfo();
Lang Hames27601ef2008-11-16 12:12:54 +00001227 tii = tm->getInstrInfo();
Lang Hames233a60e2009-11-03 23:52:08 +00001228 mri = &mf->getRegInfo();
Evan Chengb1290a62008-10-02 18:29:27 +00001229
Lang Hames27601ef2008-11-16 12:12:54 +00001230 lis = &getAnalysis<LiveIntervals>();
1231 lss = &getAnalysis<LiveStacks>();
Evan Chengb1290a62008-10-02 18:29:27 +00001232 loopInfo = &getAnalysis<MachineLoopInfo>();
Lang Hames33198392010-09-02 08:27:00 +00001233 rmf = &getAnalysis<RenderMachineFunction>();
Evan Chengb1290a62008-10-02 18:29:27 +00001234
Owen Anderson49c8aa02009-03-13 05:55:11 +00001235 vrm = &getAnalysis<VirtRegMap>();
Evan Chengb1290a62008-10-02 18:29:27 +00001236
Lang Hames54cc2ef2010-07-19 15:22:28 +00001237
Lang Hames030c4bf2010-01-26 04:49:58 +00001238 DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +00001239
Evan Chengb1290a62008-10-02 18:29:27 +00001240 // Allocator main loop:
Misha Brukman2a835f92009-01-08 15:50:22 +00001241 //
Evan Chengb1290a62008-10-02 18:29:27 +00001242 // * Map current regalloc problem to a PBQP problem
1243 // * Solve the PBQP problem
1244 // * Map the solution back to a register allocation
1245 // * Spill if necessary
Misha Brukman2a835f92009-01-08 15:50:22 +00001246 //
Evan Chengb1290a62008-10-02 18:29:27 +00001247 // This process is continued till no more spills are generated.
1248
Lang Hames27601ef2008-11-16 12:12:54 +00001249 // Find the vreg intervals in need of allocation.
1250 findVRegIntervalsToAlloc();
Misha Brukman2a835f92009-01-08 15:50:22 +00001251
Lang Hames27601ef2008-11-16 12:12:54 +00001252 // If there are non-empty intervals allocate them using pbqp.
Lang Hameseb6c8f52010-09-18 09:07:10 +00001253 if (!vregsToAlloc.empty()) {
Evan Chengb1290a62008-10-02 18:29:27 +00001254
Lang Hames27601ef2008-11-16 12:12:54 +00001255 bool pbqpAllocComplete = false;
1256 unsigned round = 0;
1257
Lang Hameseb6c8f52010-09-18 09:07:10 +00001258 if (!pbqpBuilder) {
1259 while (!pbqpAllocComplete) {
1260 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
Lang Hames27601ef2008-11-16 12:12:54 +00001261
Lang Hamesf70e7cc2010-09-23 04:28:54 +00001262 PBQP::Graph problem = constructPBQPProblemOld();
Lang Hameseb6c8f52010-09-18 09:07:10 +00001263 PBQP::Solution solution =
1264 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(problem);
Lang Hames233fd9c2009-08-18 23:34:50 +00001265
Lang Hamesf70e7cc2010-09-23 04:28:54 +00001266 pbqpAllocComplete = mapPBQPToRegAllocOld(solution);
Lang Hames27601ef2008-11-16 12:12:54 +00001267
Lang Hameseb6c8f52010-09-18 09:07:10 +00001268 ++round;
1269 }
1270 } else {
1271 while (!pbqpAllocComplete) {
1272 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
1273
1274 std::auto_ptr<PBQPRAProblem> problem =
Lang Hamese9c93562010-09-21 13:19:36 +00001275 builder->build(mf, lis, loopInfo, vregsToAlloc);
Lang Hameseb6c8f52010-09-18 09:07:10 +00001276 PBQP::Solution solution =
Lang Hamese9c93562010-09-21 13:19:36 +00001277 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(
1278 problem->getGraph());
Lang Hameseb6c8f52010-09-18 09:07:10 +00001279
Lang Hamesf70e7cc2010-09-23 04:28:54 +00001280 pbqpAllocComplete = mapPBQPToRegAlloc(*problem, solution);
Lang Hameseb6c8f52010-09-18 09:07:10 +00001281
1282 ++round;
1283 }
Lang Hames27601ef2008-11-16 12:12:54 +00001284 }
Evan Chengb1290a62008-10-02 18:29:27 +00001285 }
1286
Lang Hames27601ef2008-11-16 12:12:54 +00001287 // Finalise allocation, allocate empty ranges.
1288 finalizeAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +00001289
Lang Hamesc4bcc772010-07-20 07:41:44 +00001290 rmf->renderMachineFunction("After PBQP register allocation.", vrm);
1291
Lang Hameseb6c8f52010-09-18 09:07:10 +00001292 vregsToAlloc.clear();
1293 emptyIntervalVRegs.clear();
Lang Hames27601ef2008-11-16 12:12:54 +00001294 li2Node.clear();
1295 node2LI.clear();
1296 allowedSets.clear();
Lang Hames030c4bf2010-01-26 04:49:58 +00001297 problemNodes.clear();
Lang Hames27601ef2008-11-16 12:12:54 +00001298
David Greene30931542010-01-05 01:25:43 +00001299 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +00001300
Lang Hames87e3bca2009-05-06 02:36:21 +00001301 // Run rewriter
1302 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
1303
1304 rewriter->runOnMachineFunction(*mf, *vrm, lis);
Lang Hames27601ef2008-11-16 12:12:54 +00001305
Misha Brukman2a835f92009-01-08 15:50:22 +00001306 return true;
Evan Chengb1290a62008-10-02 18:29:27 +00001307}
1308
Lang Hamesf70e7cc2010-09-23 04:28:54 +00001309FunctionPass* llvm::createPBQPRegisterAllocator(
1310 std::auto_ptr<PBQPBuilder> builder) {
1311 return new RegAllocPBQP(builder);
Evan Chengb1290a62008-10-02 18:29:27 +00001312}
1313
Lang Hamesf70e7cc2010-09-23 04:28:54 +00001314FunctionPass* llvm::createDefaultPBQPRegisterAllocator() {
1315 if (pbqpCoalescing) {
1316 return createPBQPRegisterAllocator(
1317 std::auto_ptr<PBQPBuilder>(new PBQPBuilderWithCoalescing()));
1318 } // else
1319 return createPBQPRegisterAllocator(
1320 std::auto_ptr<PBQPBuilder>(new PBQPBuilder()));
Lang Hameseb6c8f52010-09-18 09:07:10 +00001321}
Evan Chengb1290a62008-10-02 18:29:27 +00001322
1323#undef DEBUG_TYPE