blob: 6b2e5c04807629f40cfa64374213bf6ec35b4be1 [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 Hameseb6c8f52010-09-18 09:07:10 +000059namespace llvm {
60
61using namespace PBQP;
62 using namespace PBQP::Heuristics;
Evan Chengb1290a62008-10-02 18:29:27 +000063
64static RegisterRegAlloc
Duncan Sands1aecd152010-02-18 14:10:41 +000065registerPBQPRepAlloc("pbqp", "PBQP register allocator",
Lang Hames030c4bf2010-01-26 04:49:58 +000066 llvm::createPBQPRegisterAllocator);
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 Hames12f35c52010-07-18 00:57:59 +000073static cl::opt<bool>
Lang Hameseb6c8f52010-09-18 09:07:10 +000074pbqpBuilder("pbqp-builder",
75 cl::desc("Use new builder system."),
76 cl::init(false), cl::Hidden);
77
78
79static cl::opt<bool>
Lang Hames12f35c52010-07-18 00:57:59 +000080pbqpPreSplitting("pbqp-pre-splitting",
81 cl::desc("Pre-splite before PBQP register allocation."),
82 cl::init(false), cl::Hidden);
83
Lang Hameseb6c8f52010-09-18 09:07:10 +000084char RegAllocPBQP::ID = 0;
Evan Chengb1290a62008-10-02 18:29:27 +000085
Lang Hameseb6c8f52010-09-18 09:07:10 +000086unsigned PBQPRAProblem::getVRegForNode(PBQP::Graph::ConstNodeItr node) const {
87 Node2VReg::const_iterator vregItr = node2VReg.find(node);
88 assert(vregItr != node2VReg.end() && "No vreg for node.");
89 return vregItr->second;
90}
Evan Chengb1290a62008-10-02 18:29:27 +000091
Lang Hameseb6c8f52010-09-18 09:07:10 +000092PBQP::Graph::NodeItr PBQPRAProblem::getNodeForVReg(unsigned vreg) const {
93 VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg);
94 assert(nodeItr != vreg2Node.end() && "No node for vreg.");
95 return nodeItr->second;
96
97}
Daniel Dunbara279bc32009-09-20 02:20:51 +000098
Lang Hameseb6c8f52010-09-18 09:07:10 +000099const PBQPRAProblem::AllowedSet&
100 PBQPRAProblem::getAllowedSet(unsigned vreg) const {
101 AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg);
102 assert(allowedSetItr != allowedSets.end() && "No pregs for vreg.");
103 const AllowedSet &allowedSet = allowedSetItr->second;
104 return allowedSet;
105}
Evan Chengb1290a62008-10-02 18:29:27 +0000106
Lang Hameseb6c8f52010-09-18 09:07:10 +0000107unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const {
108 assert(isPRegOption(vreg, option) && "Not a preg option.");
109
110 const AllowedSet& allowedSet = getAllowedSet(vreg);
111 assert(option <= allowedSet.size() && "Option outside allowed set.");
112 return allowedSet[option - 1];
113}
114
115std::auto_ptr<PBQPRAProblem> PBQPBuilder::build(
116 MachineFunction *mf,
117 const LiveIntervals *lis,
118 const RegSet &vregs) {
119
120 typedef std::vector<const LiveInterval*> LIVector;
121
122 MachineRegisterInfo *mri = &mf->getRegInfo();
123 const TargetRegisterInfo *tri = mf->getTarget().getRegisterInfo();
124
125 std::auto_ptr<PBQPRAProblem> p(new PBQPRAProblem());
126 PBQP::Graph &g = p->getGraph();
127 RegSet pregs;
128
129 // Collect the set of preg intervals, record that they're used in the MF.
130 for (LiveIntervals::const_iterator itr = lis->begin(), end = lis->end();
131 itr != end; ++itr) {
132 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
133 pregs.insert(itr->first);
134 mri->setPhysRegUsed(itr->first);
Evan Chengb1290a62008-10-02 18:29:27 +0000135 }
Lang Hameseb6c8f52010-09-18 09:07:10 +0000136 }
Evan Chengb1290a62008-10-02 18:29:27 +0000137
Lang Hameseb6c8f52010-09-18 09:07:10 +0000138 BitVector reservedRegs = tri->getReservedRegs(*mf);
Evan Chengb1290a62008-10-02 18:29:27 +0000139
Lang Hameseb6c8f52010-09-18 09:07:10 +0000140 // Iterate over vregs.
141 for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end();
142 vregItr != vregEnd; ++vregItr) {
143 unsigned vreg = *vregItr;
144 const TargetRegisterClass *trc = mri->getRegClass(vreg);
145 const LiveInterval *vregLI = &lis->getInterval(vreg);
Evan Chengb1290a62008-10-02 18:29:27 +0000146
Lang Hameseb6c8f52010-09-18 09:07:10 +0000147 // Compute an initial allowed set for the current vreg.
148 typedef std::vector<unsigned> VRAllowed;
149 VRAllowed vrAllowed;
150 for (TargetRegisterClass::iterator aoItr = trc->allocation_order_begin(*mf),
151 aoEnd = trc->allocation_order_end(*mf);
152 aoItr != aoEnd; ++aoItr) {
153 unsigned preg = *aoItr;
154 if (!reservedRegs.test(preg)) {
155 vrAllowed.push_back(preg);
Lang Hamesd0f6f012010-07-17 06:31:41 +0000156 }
Lang Hameseb6c8f52010-09-18 09:07:10 +0000157 }
Lang Hamesd0f6f012010-07-17 06:31:41 +0000158
Lang Hameseb6c8f52010-09-18 09:07:10 +0000159 // Remove any physical registers which overlap.
160 for (RegSet::const_iterator pregItr = pregs.begin(),
161 pregEnd = pregs.end();
162 pregItr != pregEnd; ++pregItr) {
163 unsigned preg = *pregItr;
164 const LiveInterval *pregLI = &lis->getInterval(preg);
Lang Hames27601ef2008-11-16 12:12:54 +0000165
Lang Hameseb6c8f52010-09-18 09:07:10 +0000166 if (pregLI->empty())
167 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000168
Lang Hameseb6c8f52010-09-18 09:07:10 +0000169 if (!vregLI->overlaps(*pregLI))
170 continue;
Lang Hames030c4bf2010-01-26 04:49:58 +0000171
Lang Hameseb6c8f52010-09-18 09:07:10 +0000172 // Remove the register from the allowed set.
173 VRAllowed::iterator eraseItr =
174 std::find(vrAllowed.begin(), vrAllowed.end(), preg);
Evan Chengb1290a62008-10-02 18:29:27 +0000175
Lang Hameseb6c8f52010-09-18 09:07:10 +0000176 if (eraseItr != vrAllowed.end()) {
177 vrAllowed.erase(eraseItr);
178 }
Evan Chengb1290a62008-10-02 18:29:27 +0000179
Lang Hameseb6c8f52010-09-18 09:07:10 +0000180 // Also remove any aliases.
181 const unsigned *aliasItr = tri->getAliasSet(preg);
182 if (aliasItr != 0) {
183 for (; *aliasItr != 0; ++aliasItr) {
184 VRAllowed::iterator eraseItr =
185 std::find(vrAllowed.begin(), vrAllowed.end(), *aliasItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000186
Lang Hameseb6c8f52010-09-18 09:07:10 +0000187 if (eraseItr != vrAllowed.end()) {
188 vrAllowed.erase(eraseItr);
189 }
190 }
191 }
192 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000193
Lang Hameseb6c8f52010-09-18 09:07:10 +0000194 // Construct the node.
195 PBQP::Graph::NodeItr node =
196 g.addNode(PBQP::Vector(vrAllowed.size() + 1, 0));
Evan Chengb1290a62008-10-02 18:29:27 +0000197
Lang Hameseb6c8f52010-09-18 09:07:10 +0000198 // Record the mapping and allowed set in the problem.
199 p->recordVReg(vreg, node, vrAllowed.begin(), vrAllowed.end());
Evan Chengb1290a62008-10-02 18:29:27 +0000200
Lang Hameseb6c8f52010-09-18 09:07:10 +0000201 PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ?
202 vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000203
Lang Hameseb6c8f52010-09-18 09:07:10 +0000204 addSpillCosts(g.getNodeCosts(node), spillCost);
205 }
Evan Chengb1290a62008-10-02 18:29:27 +0000206
Lang Hameseb6c8f52010-09-18 09:07:10 +0000207 for (RegSet::iterator vr1Itr = vregs.begin(), vrEnd = vregs.end();
208 vr1Itr != vrEnd; ++vr1Itr) {
209 unsigned vr1 = *vr1Itr;
210 const LiveInterval &l1 = lis->getInterval(vr1);
211 const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1);
Evan Chengb1290a62008-10-02 18:29:27 +0000212
Lang Hameseb6c8f52010-09-18 09:07:10 +0000213 for (RegSet::iterator vr2Itr = llvm::next(vr1Itr);
214 vr2Itr != vrEnd; ++vr2Itr) {
215 unsigned vr2 = *vr2Itr;
216 const LiveInterval &l2 = lis->getInterval(vr2);
217 const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2);
Evan Chengb1290a62008-10-02 18:29:27 +0000218
Lang Hameseb6c8f52010-09-18 09:07:10 +0000219 assert(!l2.empty() && "Empty interval in vreg set?");
220 if (l1.overlaps(l2)) {
221 PBQP::Graph::EdgeItr edge =
222 g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2),
223 PBQP::Matrix(vr1Allowed.size()+1, vr2Allowed.size()+1, 0));
Lang Hames27601ef2008-11-16 12:12:54 +0000224
Lang Hameseb6c8f52010-09-18 09:07:10 +0000225 addInterferenceCosts(g.getEdgeCosts(edge), vr1Allowed, vr2Allowed, tri);
226 }
227 }
228 }
Evan Chengb1290a62008-10-02 18:29:27 +0000229
Lang Hameseb6c8f52010-09-18 09:07:10 +0000230 return p;
231}
Lang Hames27601ef2008-11-16 12:12:54 +0000232
Lang Hameseb6c8f52010-09-18 09:07:10 +0000233void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec,
234 PBQP::PBQPNum spillCost) {
235 costVec[0] = spillCost;
236}
Evan Chengb1290a62008-10-02 18:29:27 +0000237
Lang Hameseb6c8f52010-09-18 09:07:10 +0000238void PBQPBuilder::addInterferenceCosts(PBQP::Matrix &costMat,
239 const PBQPRAProblem::AllowedSet &vr1Allowed,
240 const PBQPRAProblem::AllowedSet &vr2Allowed,
241 const TargetRegisterInfo *tri) {
242 assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch.");
243 assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch.");
244
245 for (unsigned i = 0; i < vr1Allowed.size(); ++i) {
246 unsigned preg1 = vr1Allowed[i];
247
248 for (unsigned j = 0; j < vr2Allowed.size(); ++j) {
249 unsigned preg2 = vr2Allowed[j];
250
251 if (tri->regsOverlap(preg1, preg2)) {
252 costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
253 }
254 }
255 }
Evan Chengb1290a62008-10-02 18:29:27 +0000256}
257
258
Lang Hameseb6c8f52010-09-18 09:07:10 +0000259
260void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
261 au.addRequired<SlotIndexes>();
262 au.addPreserved<SlotIndexes>();
263 au.addRequired<LiveIntervals>();
264 //au.addRequiredID(SplitCriticalEdgesID);
265 au.addRequired<RegisterCoalescer>();
266 au.addRequired<CalculateSpillWeights>();
267 au.addRequired<LiveStacks>();
268 au.addPreserved<LiveStacks>();
269 au.addRequired<MachineLoopInfo>();
270 au.addPreserved<MachineLoopInfo>();
271 if (pbqpPreSplitting)
272 au.addRequired<LoopSplitter>();
273 au.addRequired<VirtRegMap>();
274 au.addRequired<RenderMachineFunction>();
275 MachineFunctionPass::getAnalysisUsage(au);
276}
277
Lang Hames27601ef2008-11-16 12:12:54 +0000278template <typename RegContainer>
Lang Hameseb6c8f52010-09-18 09:07:10 +0000279PBQP::Vector RegAllocPBQP::buildCostVector(unsigned vReg,
Lang Hames6699fb22009-08-06 23:32:48 +0000280 const RegContainer &allowed,
281 const CoalesceMap &coalesces,
282 PBQP::PBQPNum spillCost) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000283
Lang Hames27601ef2008-11-16 12:12:54 +0000284 typedef typename RegContainer::const_iterator AllowedItr;
285
Evan Chengb1290a62008-10-02 18:29:27 +0000286 // Allocate vector. Additional element (0th) used for spill option
Lang Hames6699fb22009-08-06 23:32:48 +0000287 PBQP::Vector v(allowed.size() + 1, 0);
Evan Chengb1290a62008-10-02 18:29:27 +0000288
Lang Hames6699fb22009-08-06 23:32:48 +0000289 v[0] = spillCost;
Evan Chengb1290a62008-10-02 18:29:27 +0000290
Lang Hames27601ef2008-11-16 12:12:54 +0000291 // Iterate over the allowed registers inserting coalesce benefits if there
292 // are any.
293 unsigned ai = 0;
294 for (AllowedItr itr = allowed.begin(), end = allowed.end();
295 itr != end; ++itr, ++ai) {
296
297 unsigned pReg = *itr;
298
299 CoalesceMap::const_iterator cmItr =
300 coalesces.find(RegPair(vReg, pReg));
301
302 // No coalesce - on to the next preg.
303 if (cmItr == coalesces.end())
304 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000305
306 // We have a coalesce - insert the benefit.
Lang Hames6699fb22009-08-06 23:32:48 +0000307 v[ai + 1] = -cmItr->second;
Lang Hames27601ef2008-11-16 12:12:54 +0000308 }
309
Evan Chengb1290a62008-10-02 18:29:27 +0000310 return v;
311}
312
Lang Hames27601ef2008-11-16 12:12:54 +0000313template <typename RegContainer>
Lang Hameseb6c8f52010-09-18 09:07:10 +0000314PBQP::Matrix* RegAllocPBQP::buildInterferenceMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000315 const RegContainer &allowed1, const RegContainer &allowed2) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000316
Lang Hames27601ef2008-11-16 12:12:54 +0000317 typedef typename RegContainer::const_iterator RegContainerIterator;
Evan Chengb1290a62008-10-02 18:29:27 +0000318
319 // Construct a PBQP matrix representing the cost of allocation options. The
320 // rows and columns correspond to the allocation options for the two live
321 // intervals. Elements will be infinite where corresponding registers alias,
322 // since we cannot allocate aliasing registers to interfering live intervals.
323 // All other elements (non-aliasing combinations) will have zero cost. Note
324 // that the spill option (element 0,0) has zero cost, since we can allocate
325 // both intervals to memory safely (the cost for each individual allocation
326 // to memory is accounted for by the cost vectors for each live interval).
Lang Hames6699fb22009-08-06 23:32:48 +0000327 PBQP::Matrix *m =
328 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Misha Brukman2a835f92009-01-08 15:50:22 +0000329
Evan Chengb1290a62008-10-02 18:29:27 +0000330 // Assume this is a zero matrix until proven otherwise. Zero matrices occur
331 // between interfering live ranges with non-overlapping register sets (e.g.
332 // non-overlapping reg classes, or disjoint sets of allowed regs within the
333 // same class). The term "overlapping" is used advisedly: sets which do not
334 // intersect, but contain registers which alias, will have non-zero matrices.
335 // We optimize zero matrices away to improve solver speed.
336 bool isZeroMatrix = true;
337
338
339 // Row index. Starts at 1, since the 0th row is for the spill option, which
340 // is always zero.
Misha Brukman2a835f92009-01-08 15:50:22 +0000341 unsigned ri = 1;
Evan Chengb1290a62008-10-02 18:29:27 +0000342
Misha Brukman2a835f92009-01-08 15:50:22 +0000343 // Iterate over allowed sets, insert infinities where required.
Lang Hames27601ef2008-11-16 12:12:54 +0000344 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000345 a1Itr != a1End; ++a1Itr) {
346
347 // Column index, starts at 1 as for row index.
348 unsigned ci = 1;
349 unsigned reg1 = *a1Itr;
350
Lang Hames27601ef2008-11-16 12:12:54 +0000351 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
Evan Chengb1290a62008-10-02 18:29:27 +0000352 a2Itr != a2End; ++a2Itr) {
353
354 unsigned reg2 = *a2Itr;
355
356 // If the row/column regs are identical or alias insert an infinity.
Lang Hames3f2f3f52009-09-03 02:52:02 +0000357 if (tri->regsOverlap(reg1, reg2)) {
Lang Hames6699fb22009-08-06 23:32:48 +0000358 (*m)[ri][ci] = std::numeric_limits<PBQP::PBQPNum>::infinity();
Evan Chengb1290a62008-10-02 18:29:27 +0000359 isZeroMatrix = false;
360 }
361
362 ++ci;
363 }
364
365 ++ri;
366 }
367
368 // If this turns out to be a zero matrix...
369 if (isZeroMatrix) {
370 // free it and return null.
371 delete m;
372 return 0;
373 }
374
375 // ...otherwise return the cost matrix.
376 return m;
377}
378
Lang Hames27601ef2008-11-16 12:12:54 +0000379template <typename RegContainer>
Lang Hameseb6c8f52010-09-18 09:07:10 +0000380PBQP::Matrix* RegAllocPBQP::buildCoalescingMatrix(
Lang Hames27601ef2008-11-16 12:12:54 +0000381 const RegContainer &allowed1, const RegContainer &allowed2,
Lang Hames6699fb22009-08-06 23:32:48 +0000382 PBQP::PBQPNum cBenefit) const {
Evan Chengb1290a62008-10-02 18:29:27 +0000383
Lang Hames27601ef2008-11-16 12:12:54 +0000384 typedef typename RegContainer::const_iterator RegContainerIterator;
385
386 // Construct a PBQP Matrix representing the benefits of coalescing. As with
387 // interference matrices the rows and columns represent allowed registers
388 // for the LiveIntervals which are (potentially) to be coalesced. The amount
389 // -cBenefit will be placed in any element representing the same register
390 // for both intervals.
Lang Hames6699fb22009-08-06 23:32:48 +0000391 PBQP::Matrix *m =
392 new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0);
Lang Hames27601ef2008-11-16 12:12:54 +0000393
394 // Reset costs to zero.
395 m->reset(0);
396
397 // Assume the matrix is zero till proven otherwise. Zero matrices will be
398 // optimized away as in the interference case.
399 bool isZeroMatrix = true;
400
401 // Row index. Starts at 1, since the 0th row is for the spill option, which
402 // is always zero.
403 unsigned ri = 1;
404
405 // Iterate over the allowed sets, insert coalescing benefits where
406 // appropriate.
407 for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end();
408 a1Itr != a1End; ++a1Itr) {
409
410 // Column index, starts at 1 as for row index.
411 unsigned ci = 1;
412 unsigned reg1 = *a1Itr;
413
414 for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end();
415 a2Itr != a2End; ++a2Itr) {
416
417 // If the row and column represent the same register insert a beneficial
418 // cost to preference this allocation - it would allow us to eliminate a
Misha Brukman2a835f92009-01-08 15:50:22 +0000419 // move instruction.
Lang Hames27601ef2008-11-16 12:12:54 +0000420 if (reg1 == *a2Itr) {
421 (*m)[ri][ci] = -cBenefit;
422 isZeroMatrix = false;
423 }
424
425 ++ci;
426 }
427
428 ++ri;
429 }
430
431 // If this turns out to be a zero matrix...
432 if (isZeroMatrix) {
433 // ...free it and return null.
434 delete m;
435 return 0;
436 }
437
438 return m;
439}
440
Lang Hameseb6c8f52010-09-18 09:07:10 +0000441RegAllocPBQP::CoalesceMap RegAllocPBQP::findCoalesces() {
Lang Hames27601ef2008-11-16 12:12:54 +0000442
443 typedef MachineFunction::const_iterator MFIterator;
444 typedef MachineBasicBlock::const_iterator MBBIterator;
445 typedef LiveInterval::const_vni_iterator VNIIterator;
Misha Brukman2a835f92009-01-08 15:50:22 +0000446
Lang Hames27601ef2008-11-16 12:12:54 +0000447 CoalesceMap coalescesFound;
448
449 // To find coalesces we need to iterate over the function looking for
450 // copy instructions.
451 for (MFIterator bbItr = mf->begin(), bbEnd = mf->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000452 bbItr != bbEnd; ++bbItr) {
453
454 const MachineBasicBlock *mbb = &*bbItr;
Evan Chengb1290a62008-10-02 18:29:27 +0000455
Lang Hames27601ef2008-11-16 12:12:54 +0000456 for (MBBIterator iItr = mbb->begin(), iEnd = mbb->end();
457 iItr != iEnd; ++iItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000458
459 const MachineInstr *instr = &*iItr;
460
Lang Hames27601ef2008-11-16 12:12:54 +0000461 // If this isn't a copy then continue to the next instruction.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000462 if (!instr->isCopy())
Lang Hames27601ef2008-11-16 12:12:54 +0000463 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000464
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000465 unsigned srcReg = instr->getOperand(1).getReg();
466 unsigned dstReg = instr->getOperand(0).getReg();
467
Lang Hames27601ef2008-11-16 12:12:54 +0000468 // If the registers are already the same our job is nice and easy.
469 if (dstReg == srcReg)
470 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000471
Lang Hames27601ef2008-11-16 12:12:54 +0000472 bool srcRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(srcReg),
473 dstRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(dstReg);
474
475 // If both registers are physical then we can't coalesce.
476 if (srcRegIsPhysical && dstRegIsPhysical)
477 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000478
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000479 // If it's a copy that includes two virtual register but the source and
480 // destination classes differ then we can't coalesce.
481 if (!srcRegIsPhysical && !dstRegIsPhysical &&
482 mri->getRegClass(srcReg) != mri->getRegClass(dstReg))
Lang Hames27601ef2008-11-16 12:12:54 +0000483 continue;
484
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000485 // If one is physical and one is virtual, check that the physical is
486 // allocatable in the class of the virtual.
487 if (srcRegIsPhysical && !dstRegIsPhysical) {
488 const TargetRegisterClass *dstRegClass = mri->getRegClass(dstReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000489 if (std::find(dstRegClass->allocation_order_begin(*mf),
490 dstRegClass->allocation_order_end(*mf), srcReg) ==
491 dstRegClass->allocation_order_end(*mf))
Evan Chengb1290a62008-10-02 18:29:27 +0000492 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000493 }
Rafael Espindolacbeb3db2010-07-12 01:45:38 +0000494 if (!srcRegIsPhysical && dstRegIsPhysical) {
495 const TargetRegisterClass *srcRegClass = mri->getRegClass(srcReg);
Lang Hames0b23dc02010-02-09 00:50:27 +0000496 if (std::find(srcRegClass->allocation_order_begin(*mf),
497 srcRegClass->allocation_order_end(*mf), dstReg) ==
498 srcRegClass->allocation_order_end(*mf))
Lang Hames27601ef2008-11-16 12:12:54 +0000499 continue;
500 }
501
502 // If we've made it here we have a copy with compatible register classes.
Misha Brukman2a835f92009-01-08 15:50:22 +0000503 // We can probably coalesce, but we need to consider overlap.
Lang Hames27601ef2008-11-16 12:12:54 +0000504 const LiveInterval *srcLI = &lis->getInterval(srcReg),
505 *dstLI = &lis->getInterval(dstReg);
506
507 if (srcLI->overlaps(*dstLI)) {
508 // Even in the case of an overlap we might still be able to coalesce,
509 // but we need to make sure that no definition of either range occurs
510 // while the other range is live.
511
512 // Otherwise start by assuming we're ok.
513 bool badDef = false;
514
515 // Test all defs of the source range.
Misha Brukman2a835f92009-01-08 15:50:22 +0000516 for (VNIIterator
Lang Hames27601ef2008-11-16 12:12:54 +0000517 vniItr = srcLI->vni_begin(), vniEnd = srcLI->vni_end();
518 vniItr != vniEnd; ++vniItr) {
519
Lang Hames0b23dc02010-02-09 00:50:27 +0000520 // If we find a poorly defined def we err on the side of caution.
521 if (!(*vniItr)->def.isValid()) {
522 badDef = true;
523 break;
524 }
525
Lang Hames27601ef2008-11-16 12:12:54 +0000526 // If we find a def that kills the coalescing opportunity then
527 // record it and break from the loop.
528 if (dstLI->liveAt((*vniItr)->def)) {
529 badDef = true;
530 break;
531 }
532 }
533
534 // If we have a bad def give up, continue to the next instruction.
535 if (badDef)
536 continue;
Misha Brukman2a835f92009-01-08 15:50:22 +0000537
Lang Hames27601ef2008-11-16 12:12:54 +0000538 // Otherwise test definitions of the destination range.
539 for (VNIIterator
540 vniItr = dstLI->vni_begin(), vniEnd = dstLI->vni_end();
541 vniItr != vniEnd; ++vniItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000542
Lang Hames27601ef2008-11-16 12:12:54 +0000543 // We want to make sure we skip the copy instruction itself.
Lang Hames52c1afc2009-08-10 23:43:28 +0000544 if ((*vniItr)->getCopy() == instr)
Lang Hames27601ef2008-11-16 12:12:54 +0000545 continue;
546
Lang Hames0b23dc02010-02-09 00:50:27 +0000547 if (!(*vniItr)->def.isValid()) {
548 badDef = true;
549 break;
550 }
551
Lang Hames27601ef2008-11-16 12:12:54 +0000552 if (srcLI->liveAt((*vniItr)->def)) {
553 badDef = true;
554 break;
555 }
556 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000557
Lang Hames27601ef2008-11-16 12:12:54 +0000558 // As before a bad def we give up and continue to the next instr.
559 if (badDef)
560 continue;
561 }
562
563 // If we make it to here then either the ranges didn't overlap, or they
564 // did, but none of their definitions would prevent us from coalescing.
565 // We're good to go with the coalesce.
566
Chris Lattner87565c12010-05-15 17:10:24 +0000567 float cBenefit = std::pow(10.0f, (float)loopInfo->getLoopDepth(mbb)) / 5.0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000568
Lang Hames27601ef2008-11-16 12:12:54 +0000569 coalescesFound[RegPair(srcReg, dstReg)] = cBenefit;
570 coalescesFound[RegPair(dstReg, srcReg)] = cBenefit;
Evan Chengb1290a62008-10-02 18:29:27 +0000571 }
572
573 }
574
Lang Hames27601ef2008-11-16 12:12:54 +0000575 return coalescesFound;
576}
577
Lang Hameseb6c8f52010-09-18 09:07:10 +0000578void RegAllocPBQP::findVRegIntervalsToAlloc() {
Lang Hames27601ef2008-11-16 12:12:54 +0000579
580 // Iterate over all live ranges.
581 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
582 itr != end; ++itr) {
583
584 // Ignore physical ones.
585 if (TargetRegisterInfo::isPhysicalRegister(itr->first))
586 continue;
587
588 LiveInterval *li = itr->second;
589
590 // If this live interval is non-empty we will use pbqp to allocate it.
591 // Empty intervals we allocate in a simple post-processing stage in
592 // finalizeAlloc.
593 if (!li->empty()) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000594 vregsToAlloc.insert(li->reg);
Lang Hames27601ef2008-11-16 12:12:54 +0000595 }
596 else {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000597 emptyIntervalVRegs.insert(li->reg);
Lang Hames27601ef2008-11-16 12:12:54 +0000598 }
599 }
Evan Chengb1290a62008-10-02 18:29:27 +0000600}
601
Lang Hameseb6c8f52010-09-18 09:07:10 +0000602PBQP::Graph RegAllocPBQP::constructPBQPProblem() {
Evan Chengb1290a62008-10-02 18:29:27 +0000603
604 typedef std::vector<const LiveInterval*> LIVector;
Lang Hames27601ef2008-11-16 12:12:54 +0000605 typedef std::vector<unsigned> RegVector;
Evan Chengb1290a62008-10-02 18:29:27 +0000606
Lang Hames27601ef2008-11-16 12:12:54 +0000607 // This will store the physical intervals for easy reference.
608 LIVector physIntervals;
Evan Chengb1290a62008-10-02 18:29:27 +0000609
610 // Start by clearing the old node <-> live interval mappings & allowed sets
611 li2Node.clear();
612 node2LI.clear();
613 allowedSets.clear();
614
Lang Hames27601ef2008-11-16 12:12:54 +0000615 // Populate physIntervals, update preg use:
616 for (LiveIntervals::iterator itr = lis->begin(), end = lis->end();
Evan Chengb1290a62008-10-02 18:29:27 +0000617 itr != end; ++itr) {
618
Evan Chengb1290a62008-10-02 18:29:27 +0000619 if (TargetRegisterInfo::isPhysicalRegister(itr->first)) {
620 physIntervals.push_back(itr->second);
621 mri->setPhysRegUsed(itr->second->reg);
622 }
Evan Chengb1290a62008-10-02 18:29:27 +0000623 }
624
Lang Hames27601ef2008-11-16 12:12:54 +0000625 // Iterate over vreg intervals, construct live interval <-> node number
626 // mappings.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000627 for (RegSet::const_iterator itr = vregsToAlloc.begin(),
628 end = vregsToAlloc.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000629 itr != end; ++itr) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000630 const LiveInterval *li = &lis->getInterval(*itr);
Lang Hames27601ef2008-11-16 12:12:54 +0000631
632 li2Node[li] = node2LI.size();
633 node2LI.push_back(li);
634 }
635
636 // Get the set of potential coalesces.
Lang Hames8481e3b2009-08-19 01:36:14 +0000637 CoalesceMap coalesces;
638
639 if (pbqpCoalescing) {
640 coalesces = findCoalesces();
641 }
Evan Chengb1290a62008-10-02 18:29:27 +0000642
643 // Construct a PBQP solver for this problem
Lang Hames030c4bf2010-01-26 04:49:58 +0000644 PBQP::Graph problem;
Lang Hameseb6c8f52010-09-18 09:07:10 +0000645 problemNodes.resize(vregsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000646
647 // Resize allowedSets container appropriately.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000648 allowedSets.resize(vregsToAlloc.size());
Evan Chengb1290a62008-10-02 18:29:27 +0000649
Jim Grosbach269354e2010-09-01 21:23:03 +0000650 BitVector ReservedRegs = tri->getReservedRegs(*mf);
651
Evan Chengb1290a62008-10-02 18:29:27 +0000652 // Iterate over virtual register intervals to compute allowed sets...
653 for (unsigned node = 0; node < node2LI.size(); ++node) {
654
655 // Grab pointers to the interval and its register class.
656 const LiveInterval *li = node2LI[node];
657 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000658
Evan Chengb1290a62008-10-02 18:29:27 +0000659 // Start by assuming all allocable registers in the class are allowed...
Jim Grosbach269354e2010-09-01 21:23:03 +0000660 RegVector liAllowed;
661 TargetRegisterClass::iterator aob = liRC->allocation_order_begin(*mf);
662 TargetRegisterClass::iterator aoe = liRC->allocation_order_end(*mf);
663 for (TargetRegisterClass::iterator it = aob; it != aoe; ++it)
664 if (!ReservedRegs.test(*it))
665 liAllowed.push_back(*it);
Evan Chengb1290a62008-10-02 18:29:27 +0000666
Lang Hames27601ef2008-11-16 12:12:54 +0000667 // Eliminate the physical registers which overlap with this range, along
668 // with all their aliases.
669 for (LIVector::iterator pItr = physIntervals.begin(),
670 pEnd = physIntervals.end(); pItr != pEnd; ++pItr) {
Evan Chengb1290a62008-10-02 18:29:27 +0000671
Lang Hames27601ef2008-11-16 12:12:54 +0000672 if (!li->overlaps(**pItr))
673 continue;
Evan Chengb1290a62008-10-02 18:29:27 +0000674
Lang Hames27601ef2008-11-16 12:12:54 +0000675 unsigned pReg = (*pItr)->reg;
Evan Chengb1290a62008-10-02 18:29:27 +0000676
Lang Hames27601ef2008-11-16 12:12:54 +0000677 // If we get here then the live intervals overlap, but we're still ok
678 // if they're coalescable.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000679 if (coalesces.find(RegPair(li->reg, pReg)) != coalesces.end()) {
680 DEBUG(dbgs() << "CoalescingOverride: (" << li->reg << ", " << pReg << ")\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000681 continue;
Lang Hameseb6c8f52010-09-18 09:07:10 +0000682 }
Evan Chengb1290a62008-10-02 18:29:27 +0000683
Lang Hames27601ef2008-11-16 12:12:54 +0000684 // If we get here then we have a genuine exclusion.
Evan Chengb1290a62008-10-02 18:29:27 +0000685
Lang Hames27601ef2008-11-16 12:12:54 +0000686 // Remove the overlapping reg...
687 RegVector::iterator eraseItr =
688 std::find(liAllowed.begin(), liAllowed.end(), pReg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000689
Lang Hames27601ef2008-11-16 12:12:54 +0000690 if (eraseItr != liAllowed.end())
691 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000692
Lang Hames27601ef2008-11-16 12:12:54 +0000693 const unsigned *aliasItr = tri->getAliasSet(pReg);
694
695 if (aliasItr != 0) {
696 // ...and its aliases.
697 for (; *aliasItr != 0; ++aliasItr) {
698 RegVector::iterator eraseItr =
699 std::find(liAllowed.begin(), liAllowed.end(), *aliasItr);
Misha Brukman2a835f92009-01-08 15:50:22 +0000700
Lang Hames27601ef2008-11-16 12:12:54 +0000701 if (eraseItr != liAllowed.end()) {
702 liAllowed.erase(eraseItr);
Evan Chengb1290a62008-10-02 18:29:27 +0000703 }
Evan Chengb1290a62008-10-02 18:29:27 +0000704 }
Evan Chengb1290a62008-10-02 18:29:27 +0000705 }
Evan Chengb1290a62008-10-02 18:29:27 +0000706 }
707
708 // Copy the allowed set into a member vector for use when constructing cost
709 // vectors & matrices, and mapping PBQP solutions back to assignments.
710 allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end());
711
712 // Set the spill cost to the interval weight, or epsilon if the
713 // interval weight is zero
Lang Hames6699fb22009-08-06 23:32:48 +0000714 PBQP::PBQPNum spillCost = (li->weight != 0.0) ?
715 li->weight : std::numeric_limits<PBQP::PBQPNum>::min();
Evan Chengb1290a62008-10-02 18:29:27 +0000716
717 // Build a cost vector for this interval.
Lang Hames6699fb22009-08-06 23:32:48 +0000718 problemNodes[node] =
719 problem.addNode(
720 buildCostVector(li->reg, allowedSets[node], coalesces, spillCost));
Evan Chengb1290a62008-10-02 18:29:27 +0000721
722 }
723
Lang Hames27601ef2008-11-16 12:12:54 +0000724
Evan Chengb1290a62008-10-02 18:29:27 +0000725 // Now add the cost matrices...
726 for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) {
Evan Chengb1290a62008-10-02 18:29:27 +0000727 const LiveInterval *li = node2LI[node1];
728
Evan Chengb1290a62008-10-02 18:29:27 +0000729 // Test for live range overlaps and insert interference matrices.
730 for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) {
731 const LiveInterval *li2 = node2LI[node2];
732
Lang Hames27601ef2008-11-16 12:12:54 +0000733 CoalesceMap::const_iterator cmItr =
734 coalesces.find(RegPair(li->reg, li2->reg));
Evan Chengb1290a62008-10-02 18:29:27 +0000735
Lang Hames6699fb22009-08-06 23:32:48 +0000736 PBQP::Matrix *m = 0;
Evan Chengb1290a62008-10-02 18:29:27 +0000737
Lang Hames27601ef2008-11-16 12:12:54 +0000738 if (cmItr != coalesces.end()) {
739 m = buildCoalescingMatrix(allowedSets[node1], allowedSets[node2],
740 cmItr->second);
741 }
742 else if (li->overlaps(*li2)) {
743 m = buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]);
744 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000745
Lang Hames27601ef2008-11-16 12:12:54 +0000746 if (m != 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000747 problem.addEdge(problemNodes[node1],
748 problemNodes[node2],
749 *m);
750
Lang Hames27601ef2008-11-16 12:12:54 +0000751 delete m;
Evan Chengb1290a62008-10-02 18:29:27 +0000752 }
753 }
754 }
755
Lang Hames6699fb22009-08-06 23:32:48 +0000756 assert(problem.getNumNodes() == allowedSets.size());
Lang Hames6699fb22009-08-06 23:32:48 +0000757/*
758 std::cerr << "Allocating for " << problem.getNumNodes() << " nodes, "
759 << problem.getNumEdges() << " edges.\n";
760
761 problem.printDot(std::cerr);
762*/
Evan Chengb1290a62008-10-02 18:29:27 +0000763 // We're done, PBQP problem constructed - return it.
Lang Hames6699fb22009-08-06 23:32:48 +0000764 return problem;
Evan Chengb1290a62008-10-02 18:29:27 +0000765}
766
Lang Hameseb6c8f52010-09-18 09:07:10 +0000767void RegAllocPBQP::addStackInterval(const LiveInterval *spilled,
Evan Chengc781a242009-05-03 18:32:42 +0000768 MachineRegisterInfo* mri) {
Lang Hames27601ef2008-11-16 12:12:54 +0000769 int stackSlot = vrm->getStackSlot(spilled->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000770
771 if (stackSlot == VirtRegMap::NO_STACK_SLOT)
Lang Hames27601ef2008-11-16 12:12:54 +0000772 return;
773
Evan Chengc781a242009-05-03 18:32:42 +0000774 const TargetRegisterClass *RC = mri->getRegClass(spilled->reg);
775 LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC);
Lang Hames27601ef2008-11-16 12:12:54 +0000776
777 VNInfo *vni;
778 if (stackInterval.getNumValNums() != 0)
779 vni = stackInterval.getValNumInfo(0);
780 else
Lang Hames86511252009-09-04 20:41:11 +0000781 vni = stackInterval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +0000782 SlotIndex(), 0, false, lss->getVNInfoAllocator());
Lang Hames27601ef2008-11-16 12:12:54 +0000783
784 LiveInterval &rhsInterval = lis->getInterval(spilled->reg);
785 stackInterval.MergeRangesInAsValue(rhsInterval, vni);
786}
787
Lang Hameseb6c8f52010-09-18 09:07:10 +0000788bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQP::Solution &solution) {
Lang Hamese98b4b02009-11-15 04:39:51 +0000789
Evan Chengb1290a62008-10-02 18:29:27 +0000790 // Set to true if we have any spills
791 bool anotherRoundNeeded = false;
792
793 // Clear the existing allocation.
794 vrm->clearAllVirt();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000795
Evan Chengb1290a62008-10-02 18:29:27 +0000796 // Iterate over the nodes mapping the PBQP solution to a register assignment.
797 for (unsigned node = 0; node < node2LI.size(); ++node) {
Lang Hames27601ef2008-11-16 12:12:54 +0000798 unsigned virtReg = node2LI[node]->reg,
Lang Hames030c4bf2010-01-26 04:49:58 +0000799 allocSelection = solution.getSelection(problemNodes[node]);
Lang Hames6699fb22009-08-06 23:32:48 +0000800
Evan Chengb1290a62008-10-02 18:29:27 +0000801
802 // If the PBQP solution is non-zero it's a physical register...
803 if (allocSelection != 0) {
804 // Get the physical reg, subtracting 1 to account for the spill option.
805 unsigned physReg = allowedSets[node][allocSelection - 1];
806
David Greene30931542010-01-05 01:25:43 +0000807 DEBUG(dbgs() << "VREG " << virtReg << " -> "
Lang Hameseb6c8f52010-09-18 09:07:10 +0000808 << tri->getName(physReg) << " (Option: " << allocSelection << ")\n");
Lang Hames27601ef2008-11-16 12:12:54 +0000809
810 assert(physReg != 0);
811
Evan Chengb1290a62008-10-02 18:29:27 +0000812 // Add to the virt reg map and update the used phys regs.
Lang Hames27601ef2008-11-16 12:12:54 +0000813 vrm->assignVirt2Phys(virtReg, physReg);
Evan Chengb1290a62008-10-02 18:29:27 +0000814 }
815 // ...Otherwise it's a spill.
816 else {
817
818 // Make sure we ignore this virtual reg on the next round
819 // of allocation
Lang Hameseb6c8f52010-09-18 09:07:10 +0000820 vregsToAlloc.erase(virtReg);
Evan Chengb1290a62008-10-02 18:29:27 +0000821
Evan Chengb1290a62008-10-02 18:29:27 +0000822 // Insert spill ranges for this live range
Lang Hames27601ef2008-11-16 12:12:54 +0000823 const LiveInterval *spillInterval = node2LI[node];
824 double oldSpillWeight = spillInterval->weight;
Evan Chengb1290a62008-10-02 18:29:27 +0000825 SmallVector<LiveInterval*, 8> spillIs;
Lang Hames33198392010-09-02 08:27:00 +0000826 rmf->rememberUseDefs(spillInterval);
Evan Chengb1290a62008-10-02 18:29:27 +0000827 std::vector<LiveInterval*> newSpills =
Evan Chengc781a242009-05-03 18:32:42 +0000828 lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm);
829 addStackInterval(spillInterval, mri);
Lang Hames33198392010-09-02 08:27:00 +0000830 rmf->rememberSpills(spillInterval, newSpills);
Lang Hames27601ef2008-11-16 12:12:54 +0000831
Daniel Dunbarbc84ad92009-08-20 20:01:34 +0000832 (void) oldSpillWeight;
Lang Hameseb6c8f52010-09-18 09:07:10 +0000833 DEBUG(dbgs() << "VREG " << virtReg << " -> SPILLED (Option: 0, Cost: "
Lang Hames233fd9c2009-08-18 23:34:50 +0000834 << oldSpillWeight << ", New vregs: ");
Lang Hames27601ef2008-11-16 12:12:54 +0000835
836 // Copy any newly inserted live intervals into the list of regs to
837 // allocate.
838 for (std::vector<LiveInterval*>::const_iterator
839 itr = newSpills.begin(), end = newSpills.end();
840 itr != end; ++itr) {
841
842 assert(!(*itr)->empty() && "Empty spill range.");
843
David Greene30931542010-01-05 01:25:43 +0000844 DEBUG(dbgs() << (*itr)->reg << " ");
Lang Hames27601ef2008-11-16 12:12:54 +0000845
Lang Hameseb6c8f52010-09-18 09:07:10 +0000846 vregsToAlloc.insert((*itr)->reg);
Lang Hames27601ef2008-11-16 12:12:54 +0000847 }
848
David Greene30931542010-01-05 01:25:43 +0000849 DEBUG(dbgs() << ")\n");
Evan Chengb1290a62008-10-02 18:29:27 +0000850
851 // We need another round if spill intervals were added.
852 anotherRoundNeeded |= !newSpills.empty();
853 }
854 }
855
856 return !anotherRoundNeeded;
857}
858
Lang Hameseb6c8f52010-09-18 09:07:10 +0000859bool RegAllocPBQP::mapPBQPToRegAlloc2(const PBQPRAProblem &problem,
860 const PBQP::Solution &solution) {
861 // Set to true if we have any spills
862 bool anotherRoundNeeded = false;
863
864 // Clear the existing allocation.
865 vrm->clearAllVirt();
866
867 const PBQP::Graph &g = problem.getGraph();
868 // Iterate over the nodes mapping the PBQP solution to a register
869 // assignment.
870 for (PBQP::Graph::ConstNodeItr node = g.nodesBegin(),
871 nodeEnd = g.nodesEnd();
872 node != nodeEnd; ++node) {
873 unsigned vreg = problem.getVRegForNode(node);
874 unsigned alloc = solution.getSelection(node);
875
876 if (problem.isPRegOption(vreg, alloc)) {
877 unsigned preg = problem.getPRegForOption(vreg, alloc);
878 DEBUG(dbgs() << "VREG " << vreg << " -> " << tri->getName(preg) << "\n");
879 assert(preg != 0 && "Invalid preg selected.");
880 vrm->assignVirt2Phys(vreg, preg);
881 } else if (problem.isSpillOption(vreg, alloc)) {
882 vregsToAlloc.erase(vreg);
883 const LiveInterval* spillInterval = &lis->getInterval(vreg);
884 double oldWeight = spillInterval->weight;
885 SmallVector<LiveInterval*, 8> spillIs;
886 rmf->rememberUseDefs(spillInterval);
887 std::vector<LiveInterval*> newSpills =
888 lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm);
889 addStackInterval(spillInterval, mri);
890 rmf->rememberSpills(spillInterval, newSpills);
891
892 (void) oldWeight;
893 DEBUG(dbgs() << "VREG " << vreg << " -> SPILLED (Cost: "
894 << oldWeight << ", New vregs: ");
895
896 // Copy any newly inserted live intervals into the list of regs to
897 // allocate.
898 for (std::vector<LiveInterval*>::const_iterator
899 itr = newSpills.begin(), end = newSpills.end();
900 itr != end; ++itr) {
901 assert(!(*itr)->empty() && "Empty spill range.");
902 DEBUG(dbgs() << (*itr)->reg << " ");
903 vregsToAlloc.insert((*itr)->reg);
904 }
905
906 DEBUG(dbgs() << ")\n");
907
908 // We need another round if spill intervals were added.
909 anotherRoundNeeded |= !newSpills.empty();
910 } else {
911 assert(false && "Unknown allocation option.");
912 }
913 }
914
915 return !anotherRoundNeeded;
916}
917
918
919void RegAllocPBQP::finalizeAlloc() const {
Lang Hames27601ef2008-11-16 12:12:54 +0000920 typedef LiveIntervals::iterator LIIterator;
921 typedef LiveInterval::Ranges::const_iterator LRIterator;
922
923 // First allocate registers for the empty intervals.
Lang Hameseb6c8f52010-09-18 09:07:10 +0000924 for (RegSet::const_iterator
925 itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end();
Lang Hames27601ef2008-11-16 12:12:54 +0000926 itr != end; ++itr) {
Lang Hameseb6c8f52010-09-18 09:07:10 +0000927 LiveInterval *li = &lis->getInterval(*itr);
Lang Hames27601ef2008-11-16 12:12:54 +0000928
Evan Cheng90f95f82009-06-14 20:22:55 +0000929 unsigned physReg = vrm->getRegAllocPref(li->reg);
Lang Hames6699fb22009-08-06 23:32:48 +0000930
Lang Hames27601ef2008-11-16 12:12:54 +0000931 if (physReg == 0) {
932 const TargetRegisterClass *liRC = mri->getRegClass(li->reg);
Misha Brukman2a835f92009-01-08 15:50:22 +0000933 physReg = *liRC->allocation_order_begin(*mf);
Lang Hames27601ef2008-11-16 12:12:54 +0000934 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000935
936 vrm->assignVirt2Phys(li->reg, physReg);
Lang Hames27601ef2008-11-16 12:12:54 +0000937 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000938
Lang Hames27601ef2008-11-16 12:12:54 +0000939 // Finally iterate over the basic blocks to compute and set the live-in sets.
940 SmallVector<MachineBasicBlock*, 8> liveInMBBs;
941 MachineBasicBlock *entryMBB = &*mf->begin();
942
943 for (LIIterator liItr = lis->begin(), liEnd = lis->end();
944 liItr != liEnd; ++liItr) {
945
946 const LiveInterval *li = liItr->second;
947 unsigned reg = 0;
Misha Brukman2a835f92009-01-08 15:50:22 +0000948
Lang Hames27601ef2008-11-16 12:12:54 +0000949 // Get the physical register for this interval
950 if (TargetRegisterInfo::isPhysicalRegister(li->reg)) {
951 reg = li->reg;
952 }
953 else if (vrm->isAssignedReg(li->reg)) {
954 reg = vrm->getPhys(li->reg);
955 }
956 else {
957 // Ranges which are assigned a stack slot only are ignored.
958 continue;
959 }
960
Lang Hamesb0e519f2009-05-17 23:50:36 +0000961 if (reg == 0) {
Lang Hames6699fb22009-08-06 23:32:48 +0000962 // Filter out zero regs - they're for intervals that were spilled.
Lang Hamesb0e519f2009-05-17 23:50:36 +0000963 continue;
964 }
965
Lang Hames27601ef2008-11-16 12:12:54 +0000966 // Iterate over the ranges of the current interval...
967 for (LRIterator lrItr = li->begin(), lrEnd = li->end();
968 lrItr != lrEnd; ++lrItr) {
Misha Brukman2a835f92009-01-08 15:50:22 +0000969
Lang Hames27601ef2008-11-16 12:12:54 +0000970 // Find the set of basic blocks which this range is live into...
971 if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) {
972 // And add the physreg for this interval to their live-in sets.
973 for (unsigned i = 0; i < liveInMBBs.size(); ++i) {
974 if (liveInMBBs[i] != entryMBB) {
975 if (!liveInMBBs[i]->isLiveIn(reg)) {
976 liveInMBBs[i]->addLiveIn(reg);
977 }
978 }
979 }
980 liveInMBBs.clear();
981 }
982 }
983 }
Misha Brukman2a835f92009-01-08 15:50:22 +0000984
Lang Hames27601ef2008-11-16 12:12:54 +0000985}
986
Lang Hameseb6c8f52010-09-18 09:07:10 +0000987bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
Lang Hames27601ef2008-11-16 12:12:54 +0000988
Evan Chengb1290a62008-10-02 18:29:27 +0000989 mf = &MF;
990 tm = &mf->getTarget();
991 tri = tm->getRegisterInfo();
Lang Hames27601ef2008-11-16 12:12:54 +0000992 tii = tm->getInstrInfo();
Lang Hames233a60e2009-11-03 23:52:08 +0000993 mri = &mf->getRegInfo();
Evan Chengb1290a62008-10-02 18:29:27 +0000994
Lang Hames27601ef2008-11-16 12:12:54 +0000995 lis = &getAnalysis<LiveIntervals>();
996 lss = &getAnalysis<LiveStacks>();
Evan Chengb1290a62008-10-02 18:29:27 +0000997 loopInfo = &getAnalysis<MachineLoopInfo>();
Lang Hames33198392010-09-02 08:27:00 +0000998 rmf = &getAnalysis<RenderMachineFunction>();
Evan Chengb1290a62008-10-02 18:29:27 +0000999
Owen Anderson49c8aa02009-03-13 05:55:11 +00001000 vrm = &getAnalysis<VirtRegMap>();
Evan Chengb1290a62008-10-02 18:29:27 +00001001
Lang Hames54cc2ef2010-07-19 15:22:28 +00001002
Lang Hames030c4bf2010-01-26 04:49:58 +00001003 DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +00001004
Evan Chengb1290a62008-10-02 18:29:27 +00001005 // Allocator main loop:
Misha Brukman2a835f92009-01-08 15:50:22 +00001006 //
Evan Chengb1290a62008-10-02 18:29:27 +00001007 // * Map current regalloc problem to a PBQP problem
1008 // * Solve the PBQP problem
1009 // * Map the solution back to a register allocation
1010 // * Spill if necessary
Misha Brukman2a835f92009-01-08 15:50:22 +00001011 //
Evan Chengb1290a62008-10-02 18:29:27 +00001012 // This process is continued till no more spills are generated.
1013
Lang Hames27601ef2008-11-16 12:12:54 +00001014 // Find the vreg intervals in need of allocation.
1015 findVRegIntervalsToAlloc();
Misha Brukman2a835f92009-01-08 15:50:22 +00001016
Lang Hames27601ef2008-11-16 12:12:54 +00001017 // If there are non-empty intervals allocate them using pbqp.
Lang Hameseb6c8f52010-09-18 09:07:10 +00001018 if (!vregsToAlloc.empty()) {
Evan Chengb1290a62008-10-02 18:29:27 +00001019
Lang Hames27601ef2008-11-16 12:12:54 +00001020 bool pbqpAllocComplete = false;
1021 unsigned round = 0;
1022
Lang Hameseb6c8f52010-09-18 09:07:10 +00001023 if (!pbqpBuilder) {
1024 while (!pbqpAllocComplete) {
1025 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
Lang Hames27601ef2008-11-16 12:12:54 +00001026
Lang Hameseb6c8f52010-09-18 09:07:10 +00001027 PBQP::Graph problem = constructPBQPProblem();
1028 PBQP::Solution solution =
1029 PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(problem);
Lang Hames233fd9c2009-08-18 23:34:50 +00001030
Lang Hameseb6c8f52010-09-18 09:07:10 +00001031 pbqpAllocComplete = mapPBQPToRegAlloc(solution);
Lang Hames27601ef2008-11-16 12:12:54 +00001032
Lang Hameseb6c8f52010-09-18 09:07:10 +00001033 ++round;
1034 }
1035 } else {
1036 while (!pbqpAllocComplete) {
1037 DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n");
1038
1039 std::auto_ptr<PBQPRAProblem> problem =
1040 builder->build(mf, lis, vregsToAlloc);
1041 PBQP::Solution solution =
1042 HeuristicSolver<Briggs>::solve(problem->getGraph());
1043
1044 pbqpAllocComplete = mapPBQPToRegAlloc2(*problem, solution);
1045
1046 ++round;
1047 }
Lang Hames27601ef2008-11-16 12:12:54 +00001048 }
Evan Chengb1290a62008-10-02 18:29:27 +00001049 }
1050
Lang Hames27601ef2008-11-16 12:12:54 +00001051 // Finalise allocation, allocate empty ranges.
1052 finalizeAlloc();
Evan Chengb1290a62008-10-02 18:29:27 +00001053
Lang Hamesc4bcc772010-07-20 07:41:44 +00001054 rmf->renderMachineFunction("After PBQP register allocation.", vrm);
1055
Lang Hameseb6c8f52010-09-18 09:07:10 +00001056 vregsToAlloc.clear();
1057 emptyIntervalVRegs.clear();
Lang Hames27601ef2008-11-16 12:12:54 +00001058 li2Node.clear();
1059 node2LI.clear();
1060 allowedSets.clear();
Lang Hames030c4bf2010-01-26 04:49:58 +00001061 problemNodes.clear();
Lang Hames27601ef2008-11-16 12:12:54 +00001062
David Greene30931542010-01-05 01:25:43 +00001063 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n");
Lang Hames27601ef2008-11-16 12:12:54 +00001064
Lang Hames87e3bca2009-05-06 02:36:21 +00001065 // Run rewriter
1066 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
1067
1068 rewriter->runOnMachineFunction(*mf, *vrm, lis);
Lang Hames27601ef2008-11-16 12:12:54 +00001069
Misha Brukman2a835f92009-01-08 15:50:22 +00001070 return true;
Evan Chengb1290a62008-10-02 18:29:27 +00001071}
1072
Lang Hameseb6c8f52010-09-18 09:07:10 +00001073FunctionPass* createPBQPRegisterAllocator() {
1074 return new RegAllocPBQP(std::auto_ptr<PBQPBuilder>(new PBQPBuilder()));
Evan Chengb1290a62008-10-02 18:29:27 +00001075}
1076
Lang Hameseb6c8f52010-09-18 09:07:10 +00001077}
Evan Chengb1290a62008-10-02 18:29:27 +00001078
1079#undef DEBUG_TYPE