blob: 984aab2a7a812359c45928eab14d3cebac21c209 [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
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +000034#include "Spiller.h"
Evan Chengb1290a62008-10-02 18:29:27 +000035#include "VirtRegMap.h"
Rafael Espindolafdf16ca2011-06-26 21:41:06 +000036#include "RegisterCoalescer.h"
Lang Hames20df03c2012-03-26 23:07:23 +000037#include "llvm/Module.h"
Lang Hames9ad7e072011-12-06 01:45:57 +000038#include "llvm/Analysis/AliasAnalysis.h"
Lang Hamesa937f222009-12-14 06:49:42 +000039#include "llvm/CodeGen/CalcSpillWeights.h"
Evan Chengb1290a62008-10-02 18:29:27 +000040#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000041#include "llvm/CodeGen/LiveRangeEdit.h"
Lang Hames27601ef2008-11-16 12:12:54 +000042#include "llvm/CodeGen/LiveStackAnalysis.h"
Lang Hameseb6c8f52010-09-18 09:07:10 +000043#include "llvm/CodeGen/RegAllocPBQP.h"
Lang Hames9ad7e072011-12-06 01:45:57 +000044#include "llvm/CodeGen/MachineDominators.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000045#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chengb1290a62008-10-02 18:29:27 +000046#include "llvm/CodeGen/MachineLoopInfo.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000047#include "llvm/CodeGen/MachineRegisterInfo.h"
Lang Hameseb6c8f52010-09-18 09:07:10 +000048#include "llvm/CodeGen/PBQP/HeuristicSolver.h"
49#include "llvm/CodeGen/PBQP/Graph.h"
50#include "llvm/CodeGen/PBQP/Heuristics/Briggs.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000051#include "llvm/CodeGen/RegAllocRegistry.h"
Evan Chengb1290a62008-10-02 18:29:27 +000052#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000053#include "llvm/Support/raw_ostream.h"
Misha Brukman2a835f92009-01-08 15:50:22 +000054#include "llvm/Target/TargetInstrInfo.h"
55#include "llvm/Target/TargetMachine.h"
56#include <limits>
Misha Brukman2a835f92009-01-08 15:50:22 +000057#include <memory>
Evan Chengb1290a62008-10-02 18:29:27 +000058#include <set>
Lang Hames20df03c2012-03-26 23:07:23 +000059#include <sstream>
Evan Chengb1290a62008-10-02 18:29:27 +000060#include <vector>
Evan Chengb1290a62008-10-02 18:29:27 +000061
Lang Hamesf70e7cc2010-09-23 04:28:54 +000062using namespace llvm;
Lang Hameseb6c8f52010-09-18 09:07:10 +000063
Evan Chengb1290a62008-10-02 18:29:27 +000064static RegisterRegAlloc
Duncan Sands1aecd152010-02-18 14:10:41 +000065registerPBQPRepAlloc("pbqp", "PBQP register allocator",
Lang Hamesf70e7cc2010-09-23 04:28:54 +000066 createDefaultPBQPRegisterAllocator);
Evan Chengb1290a62008-10-02 18:29:27 +000067
Lang Hames8481e3b2009-08-19 01:36:14 +000068static cl::opt<bool>
69pbqpCoalescing("pbqp-coalescing",
Lang Hames030c4bf2010-01-26 04:49:58 +000070 cl::desc("Attempt coalescing during PBQP register allocation."),
71 cl::init(false), cl::Hidden);
Lang Hames8481e3b2009-08-19 01:36:14 +000072
Lang Hames20df03c2012-03-26 23:07:23 +000073#ifndef NDEBUG
74static cl::opt<bool>
75pbqpDumpGraphs("pbqp-dump-graphs",
76 cl::desc("Dump graphs for each function/round in the compilation unit."),
77 cl::init(false), cl::Hidden);
78#endif
79
Lang Hamesf70e7cc2010-09-23 04:28:54 +000080namespace {
81
82///
83/// PBQP based allocators solve the register allocation problem by mapping
84/// register allocation problems to Partitioned Boolean Quadratic
85/// Programming problems.
86class RegAllocPBQP : public MachineFunctionPass {
87public:
88
89 static char ID;
90
91 /// Construct a PBQP register allocator.
Lang Hames8d857662011-06-17 07:09:01 +000092 RegAllocPBQP(std::auto_ptr<PBQPBuilder> b, char *cPassID=0)
93 : MachineFunctionPass(ID), builder(b), customPassID(cPassID) {
Owen Anderson081c34b2010-10-19 17:21:58 +000094 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
95 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
Owen Anderson081c34b2010-10-19 17:21:58 +000096 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
97 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
98 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
Owen Anderson081c34b2010-10-19 17:21:58 +000099 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Owen Anderson081c34b2010-10-19 17:21:58 +0000100 }
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000101
102 /// Return the pass name.
103 virtual const char* getPassName() const {
104 return "PBQP Register Allocator";
105 }
106
107 /// PBQP analysis usage.
108 virtual void getAnalysisUsage(AnalysisUsage &au) const;
109
110 /// Perform register allocation
111 virtual bool runOnMachineFunction(MachineFunction &MF);
112
113private:
114
115 typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
116 typedef std::vector<const LiveInterval*> Node2LIMap;
117 typedef std::vector<unsigned> AllowedSet;
118 typedef std::vector<AllowedSet> AllowedSetMap;
119 typedef std::pair<unsigned, unsigned> RegPair;
120 typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
121 typedef std::vector<PBQP::Graph::NodeItr> NodeVector;
122 typedef std::set<unsigned> RegSet;
123
124
125 std::auto_ptr<PBQPBuilder> builder;
126
Lang Hames8d857662011-06-17 07:09:01 +0000127 char *customPassID;
128
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000129 MachineFunction *mf;
130 const TargetMachine *tm;
131 const TargetRegisterInfo *tri;
132 const TargetInstrInfo *tii;
133 const MachineLoopInfo *loopInfo;
134 MachineRegisterInfo *mri;
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000135
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000136 std::auto_ptr<Spiller> spiller;
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000137 LiveIntervals *lis;
138 LiveStacks *lss;
139 VirtRegMap *vrm;
140
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000141 RegSet vregsToAlloc, emptyIntervalVRegs;
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000142
143 /// \brief Finds the initial set of vreg intervals to allocate.
144 void findVRegIntervalsToAlloc();
145
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000146 /// \brief Given a solved PBQP problem maps this solution back to a register
147 /// assignment.
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000148 bool mapPBQPToRegAlloc(const PBQPRAProblem &problem,
149 const PBQP::Solution &solution);
150
151 /// \brief Postprocessing before final spilling. Sets basic block "live in"
152 /// variables.
153 void finalizeAlloc() const;
154
155};
156
Lang Hameseb6c8f52010-09-18 09:07:10 +0000157char RegAllocPBQP::ID = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000158
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000159} // End anonymous namespace.
160
Lang Hameseb6c8f52010-09-18 09:07:10 +0000161unsigned PBQPRAProblem::getVRegForNode(PBQP::Graph::ConstNodeItr node) const {
162 Node2VReg::const_iterator vregItr = node2VReg.find(node);
163 assert(vregItr != node2VReg.end() && "No vreg for node.");
164 return vregItr->second;
165}
Evan Chengb1290a62008-10-02 18:29:27 +0000166
Lang Hameseb6c8f52010-09-18 09:07:10 +0000167PBQP::Graph::NodeItr PBQPRAProblem::getNodeForVReg(unsigned vreg) const {
168 VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg);
169 assert(nodeItr != vreg2Node.end() && "No node for vreg.");
170 return nodeItr->second;
Andrew Trick16f72dd2012-02-10 04:10:26 +0000171
Lang Hameseb6c8f52010-09-18 09:07:10 +0000172}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000173
Lang Hameseb6c8f52010-09-18 09:07:10 +0000174const PBQPRAProblem::AllowedSet&
175 PBQPRAProblem::getAllowedSet(unsigned vreg) const {
176 AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg);
177 assert(allowedSetItr != allowedSets.end() && "No pregs for vreg.");
178 const AllowedSet &allowedSet = allowedSetItr->second;
179 return allowedSet;
180}
Evan Chengb1290a62008-10-02 18:29:27 +0000181
Lang Hameseb6c8f52010-09-18 09:07:10 +0000182unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const {
183 assert(isPRegOption(vreg, option) && "Not a preg option.");
184
185 const AllowedSet& allowedSet = getAllowedSet(vreg);
186 assert(option <= allowedSet.size() && "Option outside allowed set.");
187 return allowedSet[option - 1];
188}
189
Lang Hamese9c93562010-09-21 13:19:36 +0000190std::auto_ptr<PBQPRAProblem> PBQPBuilder::build(MachineFunction *mf,
191 const LiveIntervals *lis,
192 const MachineLoopInfo *loopInfo,
193 const RegSet &vregs) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000194
Jakob Stoklund Olesen3b30bca2012-06-20 22:32:05 +0000195 LiveIntervals *LIS = const_cast<LiveIntervals*>(lis);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000196 MachineRegisterInfo *mri = &mf->getRegInfo();
Andrew Trick16f72dd2012-02-10 04:10:26 +0000197 const TargetRegisterInfo *tri = mf->getTarget().getRegisterInfo();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000198
199 std::auto_ptr<PBQPRAProblem> p(new PBQPRAProblem());
200 PBQP::Graph &g = p->getGraph();
201 RegSet pregs;
202
203 // Collect the set of preg intervals, record that they're used in the MF.
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +0000204 for (unsigned Reg = 1, e = tri->getNumRegs(); Reg != e; ++Reg) {
Jakob Stoklund Olesen3b30bca2012-06-20 22:32:05 +0000205 if (mri->def_empty(Reg))
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +0000206 continue;
207 pregs.insert(Reg);
208 mri->setPhysRegUsed(Reg);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000209 }
Evan Chengb1290a62008-10-02 18:29:27 +0000210
Lang Hameseb6c8f52010-09-18 09:07:10 +0000211 BitVector reservedRegs = tri->getReservedRegs(*mf);
Evan Chengb1290a62008-10-02 18:29:27 +0000212
Andrew Trick16f72dd2012-02-10 04:10:26 +0000213 // Iterate over vregs.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000214 for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end();
215 vregItr != vregEnd; ++vregItr) {
216 unsigned vreg = *vregItr;
217 const TargetRegisterClass *trc = mri->getRegClass(vreg);
Jakob Stoklund Olesen3b30bca2012-06-20 22:32:05 +0000218 LiveInterval *vregLI = &LIS->getInterval(vreg);
219
220 // Record any overlaps with regmask operands.
Lang Hames8a8cf962012-10-10 06:39:48 +0000221 BitVector regMaskOverlaps;
Jakob Stoklund Olesen3b30bca2012-06-20 22:32:05 +0000222 LIS->checkRegMaskInterference(*vregLI, regMaskOverlaps);
Evan Chengb1290a62008-10-02 18:29:27 +0000223
Lang Hameseb6c8f52010-09-18 09:07:10 +0000224 // Compute an initial allowed set for the current vreg.
225 typedef std::vector<unsigned> VRAllowed;
226 VRAllowed vrAllowed;
Craig Topperb6632ba2012-03-04 10:16:38 +0000227 ArrayRef<uint16_t> rawOrder = trc->getRawAllocationOrder(*mf);
Jakob Stoklund Olesen714c0eb2011-06-16 20:37:45 +0000228 for (unsigned i = 0; i != rawOrder.size(); ++i) {
229 unsigned preg = rawOrder[i];
Jakob Stoklund Olesen3b30bca2012-06-20 22:32:05 +0000230 if (reservedRegs.test(preg))
231 continue;
232
233 // vregLI crosses a regmask operand that clobbers preg.
Lang Hames8a8cf962012-10-10 06:39:48 +0000234 if (!regMaskOverlaps.empty() && !regMaskOverlaps.test(preg))
Jakob Stoklund Olesen3b30bca2012-06-20 22:32:05 +0000235 continue;
236
237 // vregLI overlaps fixed regunit interference.
Jakob Stoklund Olesen241d0202012-06-22 16:46:44 +0000238 bool Interference = false;
239 for (MCRegUnitIterator Units(preg, tri); Units.isValid(); ++Units) {
240 if (vregLI->overlaps(LIS->getRegUnit(*Units))) {
241 Interference = true;
242 break;
Jakob Stoklund Olesen3b30bca2012-06-20 22:32:05 +0000243 }
Lang Hamesd0f6f012010-07-17 06:31:41 +0000244 }
Jakob Stoklund Olesen241d0202012-06-22 16:46:44 +0000245 if (Interference)
246 continue;
Jakob Stoklund Olesen3b30bca2012-06-20 22:32:05 +0000247
248 // preg is usable for this virtual register.
249 vrAllowed.push_back(preg);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000250 }
Lang Hamesd0f6f012010-07-17 06:31:41 +0000251
Lang Hameseb6c8f52010-09-18 09:07:10 +0000252 // Construct the node.
Andrew Trick16f72dd2012-02-10 04:10:26 +0000253 PBQP::Graph::NodeItr node =
Lang Hameseb6c8f52010-09-18 09:07:10 +0000254 g.addNode(PBQP::Vector(vrAllowed.size() + 1, 0));
Evan Chengb1290a62008-10-02 18:29:27 +0000255
Lang Hameseb6c8f52010-09-18 09:07:10 +0000256 // Record the mapping and allowed set in the problem.
257 p->recordVReg(vreg, node, vrAllowed.begin(), vrAllowed.end());
Evan Chengb1290a62008-10-02 18:29:27 +0000258
Lang Hameseb6c8f52010-09-18 09:07:10 +0000259 PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ?
260 vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000261
Lang Hameseb6c8f52010-09-18 09:07:10 +0000262 addSpillCosts(g.getNodeCosts(node), spillCost);
263 }
Evan Chengb1290a62008-10-02 18:29:27 +0000264
Lang Hames481630d2010-09-18 09:49:08 +0000265 for (RegSet::const_iterator vr1Itr = vregs.begin(), vrEnd = vregs.end();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000266 vr1Itr != vrEnd; ++vr1Itr) {
267 unsigned vr1 = *vr1Itr;
268 const LiveInterval &l1 = lis->getInterval(vr1);
269 const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1);
Evan Chengb1290a62008-10-02 18:29:27 +0000270
Benjamin Kramer9e8d1f92010-09-18 14:41:26 +0000271 for (RegSet::const_iterator vr2Itr = llvm::next(vr1Itr);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000272 vr2Itr != vrEnd; ++vr2Itr) {
273 unsigned vr2 = *vr2Itr;
274 const LiveInterval &l2 = lis->getInterval(vr2);
275 const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2);
Evan Chengb1290a62008-10-02 18:29:27 +0000276
Lang Hameseb6c8f52010-09-18 09:07:10 +0000277 assert(!l2.empty() && "Empty interval in vreg set?");
278 if (l1.overlaps(l2)) {
279 PBQP::Graph::EdgeItr edge =
280 g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2),
281 PBQP::Matrix(vr1Allowed.size()+1, vr2Allowed.size()+1, 0));
Lang Hames27601ef2008-11-16 12:12:54 +0000282
Lang Hameseb6c8f52010-09-18 09:07:10 +0000283 addInterferenceCosts(g.getEdgeCosts(edge), vr1Allowed, vr2Allowed, tri);
284 }
285 }
286 }
Evan Chengb1290a62008-10-02 18:29:27 +0000287
Lang Hameseb6c8f52010-09-18 09:07:10 +0000288 return p;
289}
Lang Hames27601ef2008-11-16 12:12:54 +0000290
Lang Hameseb6c8f52010-09-18 09:07:10 +0000291void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec,
292 PBQP::PBQPNum spillCost) {
293 costVec[0] = spillCost;
294}
Evan Chengb1290a62008-10-02 18:29:27 +0000295
Lang Hamese9c93562010-09-21 13:19:36 +0000296void PBQPBuilder::addInterferenceCosts(
297 PBQP::Matrix &costMat,
298 const PBQPRAProblem::AllowedSet &vr1Allowed,
299 const PBQPRAProblem::AllowedSet &vr2Allowed,
300 const TargetRegisterInfo *tri) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000301 assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch.");
302 assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch.");
303
Lang Hames5e77f4b2010-11-12 05:47:21 +0000304 for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000305 unsigned preg1 = vr1Allowed[i];
306
Lang Hames5e77f4b2010-11-12 05:47:21 +0000307 for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000308 unsigned preg2 = vr2Allowed[j];
309
310 if (tri->regsOverlap(preg1, preg2)) {
311 costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
312 }
313 }
314 }
Evan Chengb1290a62008-10-02 18:29:27 +0000315}
316
Lang Hamese9c93562010-09-21 13:19:36 +0000317std::auto_ptr<PBQPRAProblem> PBQPBuilderWithCoalescing::build(
318 MachineFunction *mf,
319 const LiveIntervals *lis,
320 const MachineLoopInfo *loopInfo,
321 const RegSet &vregs) {
322
323 std::auto_ptr<PBQPRAProblem> p = PBQPBuilder::build(mf, lis, loopInfo, vregs);
324 PBQP::Graph &g = p->getGraph();
325
326 const TargetMachine &tm = mf->getTarget();
Benjamin Kramera7542d52012-06-06 18:25:08 +0000327 CoalescerPair cp(*tm.getRegisterInfo());
Lang Hamese9c93562010-09-21 13:19:36 +0000328
329 // Scan the machine function and add a coalescing cost whenever CoalescerPair
330 // gives the Ok.
331 for (MachineFunction::const_iterator mbbItr = mf->begin(),
332 mbbEnd = mf->end();
333 mbbItr != mbbEnd; ++mbbItr) {
334 const MachineBasicBlock *mbb = &*mbbItr;
335
336 for (MachineBasicBlock::const_iterator miItr = mbb->begin(),
337 miEnd = mbb->end();
338 miItr != miEnd; ++miItr) {
339 const MachineInstr *mi = &*miItr;
340
Lang Hames5e77f4b2010-11-12 05:47:21 +0000341 if (!cp.setRegisters(mi)) {
Lang Hamese9c93562010-09-21 13:19:36 +0000342 continue; // Not coalescable.
Lang Hames5e77f4b2010-11-12 05:47:21 +0000343 }
Lang Hamese9c93562010-09-21 13:19:36 +0000344
Lang Hames5e77f4b2010-11-12 05:47:21 +0000345 if (cp.getSrcReg() == cp.getDstReg()) {
Lang Hamese9c93562010-09-21 13:19:36 +0000346 continue; // Already coalesced.
Lang Hames5e77f4b2010-11-12 05:47:21 +0000347 }
Lang Hamese9c93562010-09-21 13:19:36 +0000348
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000349 unsigned dst = cp.getDstReg(),
350 src = cp.getSrcReg();
Lang Hamese9c93562010-09-21 13:19:36 +0000351
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000352 const float copyFactor = 0.5; // Cost of copy relative to load. Current
353 // value plucked randomly out of the air.
Andrew Trick16f72dd2012-02-10 04:10:26 +0000354
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000355 PBQP::PBQPNum cBenefit =
356 copyFactor * LiveIntervals::getSpillWeight(false, true,
357 loopInfo->getLoopDepth(mbb));
Lang Hamese9c93562010-09-21 13:19:36 +0000358
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000359 if (cp.isPhys()) {
Lang Hames5e77f4b2010-11-12 05:47:21 +0000360 if (!lis->isAllocatable(dst)) {
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000361 continue;
Lang Hames5e77f4b2010-11-12 05:47:21 +0000362 }
Lang Hamese9c93562010-09-21 13:19:36 +0000363
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000364 const PBQPRAProblem::AllowedSet &allowed = p->getAllowedSet(src);
Andrew Trick16f72dd2012-02-10 04:10:26 +0000365 unsigned pregOpt = 0;
Lang Hames5e77f4b2010-11-12 05:47:21 +0000366 while (pregOpt < allowed.size() && allowed[pregOpt] != dst) {
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000367 ++pregOpt;
Lang Hames5e77f4b2010-11-12 05:47:21 +0000368 }
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000369 if (pregOpt < allowed.size()) {
370 ++pregOpt; // +1 to account for spill option.
371 PBQP::Graph::NodeItr node = p->getNodeForVReg(src);
372 addPhysRegCoalesce(g.getNodeCosts(node), pregOpt, cBenefit);
Lang Hamese9c93562010-09-21 13:19:36 +0000373 }
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000374 } else {
375 const PBQPRAProblem::AllowedSet *allowed1 = &p->getAllowedSet(dst);
376 const PBQPRAProblem::AllowedSet *allowed2 = &p->getAllowedSet(src);
377 PBQP::Graph::NodeItr node1 = p->getNodeForVReg(dst);
378 PBQP::Graph::NodeItr node2 = p->getNodeForVReg(src);
379 PBQP::Graph::EdgeItr edge = g.findEdge(node1, node2);
380 if (edge == g.edgesEnd()) {
381 edge = g.addEdge(node1, node2, PBQP::Matrix(allowed1->size() + 1,
382 allowed2->size() + 1,
383 0));
384 } else {
385 if (g.getEdgeNode1(edge) == node2) {
386 std::swap(node1, node2);
387 std::swap(allowed1, allowed2);
388 }
389 }
Andrew Trick16f72dd2012-02-10 04:10:26 +0000390
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000391 addVirtRegCoalesce(g.getEdgeCosts(edge), *allowed1, *allowed2,
392 cBenefit);
Lang Hamese9c93562010-09-21 13:19:36 +0000393 }
394 }
395 }
396
397 return p;
398}
399
Lang Hamese9c93562010-09-21 13:19:36 +0000400void PBQPBuilderWithCoalescing::addPhysRegCoalesce(PBQP::Vector &costVec,
401 unsigned pregOption,
402 PBQP::PBQPNum benefit) {
403 costVec[pregOption] += -benefit;
404}
405
406void PBQPBuilderWithCoalescing::addVirtRegCoalesce(
407 PBQP::Matrix &costMat,
408 const PBQPRAProblem::AllowedSet &vr1Allowed,
409 const PBQPRAProblem::AllowedSet &vr2Allowed,
410 PBQP::PBQPNum benefit) {
411
412 assert(costMat.getRows() == vr1Allowed.size() + 1 && "Size mismatch.");
413 assert(costMat.getCols() == vr2Allowed.size() + 1 && "Size mismatch.");
414
Lang Hames5e77f4b2010-11-12 05:47:21 +0000415 for (unsigned i = 0; i != vr1Allowed.size(); ++i) {
Lang Hamese9c93562010-09-21 13:19:36 +0000416 unsigned preg1 = vr1Allowed[i];
Lang Hames5e77f4b2010-11-12 05:47:21 +0000417 for (unsigned j = 0; j != vr2Allowed.size(); ++j) {
Lang Hamese9c93562010-09-21 13:19:36 +0000418 unsigned preg2 = vr2Allowed[j];
419
420 if (preg1 == preg2) {
421 costMat[i + 1][j + 1] += -benefit;
Andrew Trick16f72dd2012-02-10 04:10:26 +0000422 }
Lang Hamese9c93562010-09-21 13:19:36 +0000423 }
424 }
425}
Evan Chengb1290a62008-10-02 18:29:27 +0000426
Lang Hameseb6c8f52010-09-18 09:07:10 +0000427
428void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
Lang Hames9ad7e072011-12-06 01:45:57 +0000429 au.setPreservesCFG();
430 au.addRequired<AliasAnalysis>();
431 au.addPreserved<AliasAnalysis>();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000432 au.addRequired<SlotIndexes>();
433 au.addPreserved<SlotIndexes>();
434 au.addRequired<LiveIntervals>();
Lang Hames442c59f2012-10-04 04:50:53 +0000435 au.addPreserved<LiveIntervals>();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000436 //au.addRequiredID(SplitCriticalEdgesID);
Lang Hames8d857662011-06-17 07:09:01 +0000437 if (customPassID)
438 au.addRequiredID(*customPassID);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000439 au.addRequired<CalculateSpillWeights>();
440 au.addRequired<LiveStacks>();
441 au.addPreserved<LiveStacks>();
Lang Hames9ad7e072011-12-06 01:45:57 +0000442 au.addRequired<MachineDominatorTree>();
443 au.addPreserved<MachineDominatorTree>();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000444 au.addRequired<MachineLoopInfo>();
445 au.addPreserved<MachineLoopInfo>();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000446 au.addRequired<VirtRegMap>();
Lang Hames442c59f2012-10-04 04:50:53 +0000447 au.addPreserved<VirtRegMap>();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000448 MachineFunctionPass::getAnalysisUsage(au);
449}
450
Lang Hameseb6c8f52010-09-18 09:07:10 +0000451void RegAllocPBQP::findVRegIntervalsToAlloc() {
Lang Hames27601ef2008-11-16 12:12:54 +0000452
453 // Iterate over all live ranges.
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +0000454 for (unsigned i = 0, e = mri->getNumVirtRegs(); i != e; ++i) {
455 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
456 if (mri->reg_nodbg_empty(Reg))
Lang Hames27601ef2008-11-16 12:12:54 +0000457 continue;
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +0000458 LiveInterval *li = &lis->getInterval(Reg);
Lang Hames27601ef2008-11-16 12:12:54 +0000459
460 // If this live interval is non-empty we will use pbqp to allocate it.
461 // Empty intervals we allocate in a simple post-processing stage in
462 // finalizeAlloc.
463 if (!li->empty()) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000464 vregsToAlloc.insert(li->reg);
Lang Hames5e77f4b2010-11-12 05:47:21 +0000465 } else {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000466 emptyIntervalVRegs.insert(li->reg);
Lang Hames27601ef2008-11-16 12:12:54 +0000467 }
468 }
Evan Chengb1290a62008-10-02 18:29:27 +0000469}
470
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000471bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAProblem &problem,
472 const PBQP::Solution &solution) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000473 // Set to true if we have any spills
474 bool anotherRoundNeeded = false;
475
476 // Clear the existing allocation.
477 vrm->clearAllVirt();
478
479 const PBQP::Graph &g = problem.getGraph();
480 // Iterate over the nodes mapping the PBQP solution to a register
481 // assignment.
482 for (PBQP::Graph::ConstNodeItr node = g.nodesBegin(),
483 nodeEnd = g.nodesEnd();
484 node != nodeEnd; ++node) {
485 unsigned vreg = problem.getVRegForNode(node);
486 unsigned alloc = solution.getSelection(node);
487
488 if (problem.isPRegOption(vreg, alloc)) {
Andrew Trick16f72dd2012-02-10 04:10:26 +0000489 unsigned preg = problem.getPRegForOption(vreg, alloc);
Patrik Hägglundd7693872012-05-23 12:12:58 +0000490 DEBUG(dbgs() << "VREG " << PrintReg(vreg, tri) << " -> "
491 << tri->getName(preg) << "\n");
Lang Hameseb6c8f52010-09-18 09:07:10 +0000492 assert(preg != 0 && "Invalid preg selected.");
Andrew Trick16f72dd2012-02-10 04:10:26 +0000493 vrm->assignVirt2Phys(vreg, preg);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000494 } else if (problem.isSpillOption(vreg, alloc)) {
495 vregsToAlloc.erase(vreg);
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000496 SmallVector<LiveInterval*, 8> newSpills;
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +0000497 LiveRangeEdit LRE(&lis->getInterval(vreg), newSpills, *mf, *lis, vrm);
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000498 spiller->spill(LRE);
Lang Hameseb6c8f52010-09-18 09:07:10 +0000499
Patrik Hägglundd7693872012-05-23 12:12:58 +0000500 DEBUG(dbgs() << "VREG " << PrintReg(vreg, tri) << " -> SPILLED (Cost: "
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000501 << LRE.getParent().weight << ", New vregs: ");
Lang Hameseb6c8f52010-09-18 09:07:10 +0000502
503 // Copy any newly inserted live intervals into the list of regs to
504 // allocate.
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000505 for (LiveRangeEdit::iterator itr = LRE.begin(), end = LRE.end();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000506 itr != end; ++itr) {
507 assert(!(*itr)->empty() && "Empty spill range.");
Patrik Hägglundd7693872012-05-23 12:12:58 +0000508 DEBUG(dbgs() << PrintReg((*itr)->reg, tri) << " ");
Lang Hameseb6c8f52010-09-18 09:07:10 +0000509 vregsToAlloc.insert((*itr)->reg);
510 }
511
512 DEBUG(dbgs() << ")\n");
513
514 // We need another round if spill intervals were added.
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000515 anotherRoundNeeded |= !LRE.empty();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000516 } else {
Craig Topper5e25ee82012-02-05 08:31:47 +0000517 llvm_unreachable("Unknown allocation option.");
Lang Hameseb6c8f52010-09-18 09:07:10 +0000518 }
519 }
520
521 return !anotherRoundNeeded;
522}
523
524
525void RegAllocPBQP::finalizeAlloc() const {
Lang Hames27601ef2008-11-16 12:12:54 +0000526 // First allocate registers for the empty intervals.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000527 for (RegSet::const_iterator
528 itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000529 itr != end; ++itr) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000530 LiveInterval *li = &lis->getInterval(*itr);
Lang Hames27601ef2008-11-16 12:12:54 +0000531
Evan Cheng90f95f82009-06-14 20:22:55 +0000532 unsigned physReg = vrm->getRegAllocPref(li->reg);
Lang Hames6699fb22009-08-06 23:32:48 +0000533
Lang Hames27601ef2008-11-16 12:12:54 +0000534 if (physReg == 0) {
535 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Jakob Stoklund Olesen714c0eb2011-06-16 20:37:45 +0000536 physReg = liRC->getRawAllocationOrder(*mf).front();
Lang Hames27601ef2008-11-16 12:12:54 +0000537 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000538
539 vrm->assignVirt2Phys(li->reg, physReg);
Lang Hames27601ef2008-11-16 12:12:54 +0000540 }
Lang Hames27601ef2008-11-16 12:12:54 +0000541}
542
Lang Hameseb6c8f52010-09-18 09:07:10 +0000543bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
Lang Hames27601ef2008-11-16 12:12:54 +0000544
Evan Chengb1290a62008-10-02 18:29:27 +0000545 mf = &MF;
546 tm = &mf->getTarget();
547 tri = tm->getRegisterInfo();
Lang Hames27601ef2008-11-16 12:12:54 +0000548 tii = tm->getInstrInfo();
Andrew Trick16f72dd2012-02-10 04:10:26 +0000549 mri = &mf->getRegInfo();
Evan Chengb1290a62008-10-02 18:29:27 +0000550
Lang Hames27601ef2008-11-16 12:12:54 +0000551 lis = &getAnalysis<LiveIntervals>();
552 lss = &getAnalysis<LiveStacks>();
Evan Chengb1290a62008-10-02 18:29:27 +0000553 loopInfo = &getAnalysis<MachineLoopInfo>();
554
Owen Anderson49c8aa02009-03-13 05:55:11 +0000555 vrm = &getAnalysis<VirtRegMap>();
Jakob Stoklund Olesencfa81012011-11-12 23:17:52 +0000556 spiller.reset(createInlineSpiller(*this, MF, *vrm));
Evan Chengb1290a62008-10-02 18:29:27 +0000557
Jakob Stoklund Olesend9e5c762012-01-05 00:26:49 +0000558 mri->freezeReservedRegs(MF);
Lang Hames54cc2ef2010-07-19 15:22:28 +0000559
Craig Topper96601ca2012-08-22 06:07:19 +0000560 DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getName() << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000561
Evan Chengb1290a62008-10-02 18:29:27 +0000562 // Allocator main loop:
Misha Brukman2a835f92009-01-08 15:50:22 +0000563 //
Evan Chengb1290a62008-10-02 18:29:27 +0000564 // * Map current regalloc problem to a PBQP problem
565 // * Solve the PBQP problem
566 // * Map the solution back to a register allocation
567 // * Spill if necessary
Misha Brukman2a835f92009-01-08 15:50:22 +0000568 //
Evan Chengb1290a62008-10-02 18:29:27 +0000569 // This process is continued till no more spills are generated.
570
Lang Hames27601ef2008-11-16 12:12:54 +0000571 // Find the vreg intervals in need of allocation.
572 findVRegIntervalsToAlloc();
Misha Brukman2a835f92009-01-08 15:50:22 +0000573
Craig Topper96601ca2012-08-22 06:07:19 +0000574#ifndef NDEBUG
Lang Hames20df03c2012-03-26 23:07:23 +0000575 const Function* func = mf->getFunction();
576 std::string fqn =
577 func->getParent()->getModuleIdentifier() + "." +
578 func->getName().str();
Craig Topper96601ca2012-08-22 06:07:19 +0000579#endif
Lang Hames20df03c2012-03-26 23:07:23 +0000580
Lang Hames27601ef2008-11-16 12:12:54 +0000581 // If there are non-empty intervals allocate them using pbqp.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000582 if (!vregsToAlloc.empty()) {
Evan Chengb1290a62008-10-02 18:29:27 +0000583
Lang Hames27601ef2008-11-16 12:12:54 +0000584 bool pbqpAllocComplete = false;
585 unsigned round = 0;
586
Lang Hamesab62b7e2010-10-04 12:13:07 +0000587 while (!pbqpAllocComplete) {
588 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000589
Lang Hamesab62b7e2010-10-04 12:13:07 +0000590 std::auto_ptr<PBQPRAProblem> problem =
591 builder->build(mf, lis, loopInfo, vregsToAlloc);
Lang Hames20df03c2012-03-26 23:07:23 +0000592
593#ifndef NDEBUG
594 if (pbqpDumpGraphs) {
595 std::ostringstream rs;
596 rs << round;
597 std::string graphFileName(fqn + "." + rs.str() + ".pbqpgraph");
598 std::string tmp;
599 raw_fd_ostream os(graphFileName.c_str(), tmp);
600 DEBUG(dbgs() << "Dumping graph for round " << round << " to \""
601 << graphFileName << "\"\n");
602 problem->getGraph().dump(os);
603 }
604#endif
605
Lang Hamesab62b7e2010-10-04 12:13:07 +0000606 PBQP::Solution solution =
607 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(
608 problem->getGraph());
Lang Hames233fd9c2009-08-18 23:34:50 +0000609
Lang Hamesab62b7e2010-10-04 12:13:07 +0000610 pbqpAllocComplete = mapPBQPToRegAlloc(*problem, solution);
Lang Hames27601ef2008-11-16 12:12:54 +0000611
Lang Hamesab62b7e2010-10-04 12:13:07 +0000612 ++round;
Lang Hames27601ef2008-11-16 12:12:54 +0000613 }
Evan Chengb1290a62008-10-02 18:29:27 +0000614 }
615
Lang Hames27601ef2008-11-16 12:12:54 +0000616 // Finalise allocation, allocate empty ranges.
617 finalizeAlloc();
Lang Hameseb6c8f52010-09-18 09:07:10 +0000618 vregsToAlloc.clear();
619 emptyIntervalVRegs.clear();
Lang Hames27601ef2008-11-16 12:12:54 +0000620
David Greene30931542010-01-05 01:25:43 +0000621 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000622
Misha Brukman2a835f92009-01-08 15:50:22 +0000623 return true;
Evan Chengb1290a62008-10-02 18:29:27 +0000624}
625
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000626FunctionPass* llvm::createPBQPRegisterAllocator(
Lang Hames8d857662011-06-17 07:09:01 +0000627 std::auto_ptr<PBQPBuilder> builder,
628 char *customPassID) {
629 return new RegAllocPBQP(builder, customPassID);
Evan Chengb1290a62008-10-02 18:29:27 +0000630}
631
Lang Hamesf70e7cc2010-09-23 04:28:54 +0000632FunctionPass* llvm::createDefaultPBQPRegisterAllocator() {
633 if (pbqpCoalescing) {
634 return createPBQPRegisterAllocator(
635 std::auto_ptr<PBQPBuilder>(new PBQPBuilderWithCoalescing()));
636 } // else
637 return createPBQPRegisterAllocator(
638 std::auto_ptr<PBQPBuilder>(new PBQPBuilder()));
Lang Hameseb6c8f52010-09-18 09:07:10 +0000639}
Evan Chengb1290a62008-10-02 18:29:27 +0000640
641#undef DEBUG_TYPE