blob: ba2d3aad9ed1505bd0a56f6ba43d6ebf3b96cd1b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- RegAllocLinearScan.cpp - Linear Scan register allocator -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a linear scan register allocator.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "regalloc"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "PhysRegTracker.h"
16#include "VirtRegMap.h"
Owen Anderson860d4822009-03-11 22:31:21 +000017#include "Spiller.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Function.h"
Evan Cheng14f8a502008-06-04 09:18:41 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
20#include "llvm/CodeGen/LiveStackAnalysis.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng26d17df2007-12-11 02:09:15 +000023#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/CodeGen/Passes.h"
26#include "llvm/CodeGen/RegAllocRegistry.h"
David Greene1d80f1b2007-09-06 16:18:45 +000027#include "llvm/CodeGen/RegisterCoalescer.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Target/TargetMachine.h"
Owen Andersonbac9ae22008-10-07 20:22:28 +000030#include "llvm/Target/TargetOptions.h"
Evan Chengc4c75f52007-11-03 07:20:12 +000031#include "llvm/Target/TargetInstrInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/EquivalenceClasses.h"
Dan Gohmanc24a3f82009-01-05 17:59:02 +000033#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/ADT/Statistic.h"
35#include "llvm/ADT/STLExtras.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/Compiler.h"
38#include <algorithm>
39#include <set>
40#include <queue>
41#include <memory>
42#include <cmath>
43using namespace llvm;
44
45STATISTIC(NumIters , "Number of iterations performed");
46STATISTIC(NumBacktracks, "Number of times we had to backtrack");
Evan Chengc4c75f52007-11-03 07:20:12 +000047STATISTIC(NumCoalesce, "Number of copies coalesced");
Evan Cheng29b4cf62009-04-20 08:01:12 +000048STATISTIC(NumDowngrade, "Number of registers downgraded");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049
Evan Chengc5952452008-06-20 21:45:16 +000050static cl::opt<bool>
51NewHeuristic("new-spilling-heuristic",
52 cl::desc("Use new spilling heuristic"),
53 cl::init(false), cl::Hidden);
54
Evan Cheng99dcc172008-10-23 20:43:13 +000055static cl::opt<bool>
56PreSplitIntervals("pre-alloc-split",
57 cl::desc("Pre-register allocation live interval splitting"),
58 cl::init(false), cl::Hidden);
59
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060static RegisterRegAlloc
Dan Gohman669b9bf2008-10-14 20:25:08 +000061linearscanRegAlloc("linearscan", "linear scan register allocator",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 createLinearScanRegisterAllocator);
63
64namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 struct VISIBILITY_HIDDEN RALinScan : public MachineFunctionPass {
66 static char ID;
Dan Gohman26f8c272008-09-04 17:05:41 +000067 RALinScan() : MachineFunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
69 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
Owen Andersonba926a32008-08-15 18:49:41 +000070 typedef SmallVector<IntervalPtr, 32> IntervalPtrs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 private:
72 /// RelatedRegClasses - This structure is built the first time a function is
73 /// compiled, and keeps track of which register classes have registers that
74 /// belong to multiple classes or have aliases that are in other classes.
75 EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
Owen Anderson4a472712008-08-13 23:36:23 +000076 DenseMap<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077
Evan Cheng29b4cf62009-04-20 08:01:12 +000078 // NextReloadMap - For each register in the map, it maps to the another
79 // register which is defined by a reload from the same stack slot and
80 // both reloads are in the same basic block.
81 DenseMap<unsigned, unsigned> NextReloadMap;
82
83 // DowngradedRegs - A set of registers which are being "downgraded", i.e.
84 // un-favored for allocation.
85 SmallSet<unsigned, 8> DowngradedRegs;
86
87 // DowngradeMap - A map from virtual registers to physical registers being
88 // downgraded for the virtual registers.
89 DenseMap<unsigned, unsigned> DowngradeMap;
90
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 MachineFunction* mf_;
Evan Chengc5952452008-06-20 21:45:16 +000092 MachineRegisterInfo* mri_;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 const TargetMachine* tm_;
Dan Gohman1e57df32008-02-10 18:45:23 +000094 const TargetRegisterInfo* tri_;
Evan Chengc4c75f52007-11-03 07:20:12 +000095 const TargetInstrInfo* tii_;
Evan Chengc4c75f52007-11-03 07:20:12 +000096 BitVector allocatableRegs_;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 LiveIntervals* li_;
Evan Cheng14f8a502008-06-04 09:18:41 +000098 LiveStacks* ls_;
Evan Cheng26d17df2007-12-11 02:09:15 +000099 const MachineLoopInfo *loopInfo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
101 /// handled_ - Intervals are added to the handled_ set in the order of their
102 /// start value. This is uses for backtracking.
103 std::vector<LiveInterval*> handled_;
104
105 /// fixed_ - Intervals that correspond to machine registers.
106 ///
107 IntervalPtrs fixed_;
108
109 /// active_ - Intervals that are currently being processed, and which have a
110 /// live range active for the current point.
111 IntervalPtrs active_;
112
113 /// inactive_ - Intervals that are currently being processed, but which have
114 /// a hold at the current point.
115 IntervalPtrs inactive_;
116
117 typedef std::priority_queue<LiveInterval*,
Owen Andersonba926a32008-08-15 18:49:41 +0000118 SmallVector<LiveInterval*, 64>,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 greater_ptr<LiveInterval> > IntervalHeap;
120 IntervalHeap unhandled_;
121 std::auto_ptr<PhysRegTracker> prt_;
Owen Andersondd56ab72009-03-13 05:55:11 +0000122 VirtRegMap* vrm_;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 std::auto_ptr<Spiller> spiller_;
124
125 public:
126 virtual const char* getPassName() const {
127 return "Linear Scan Register Allocator";
128 }
129
130 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
131 AU.addRequired<LiveIntervals>();
Owen Andersonbac9ae22008-10-07 20:22:28 +0000132 if (StrongPHIElim)
133 AU.addRequiredID(StrongPHIEliminationID);
David Greene1d80f1b2007-09-06 16:18:45 +0000134 // Make sure PassManager knows which analyses to make available
135 // to coalescing and which analyses coalescing invalidates.
136 AU.addRequiredTransitive<RegisterCoalescer>();
Evan Cheng99dcc172008-10-23 20:43:13 +0000137 if (PreSplitIntervals)
138 AU.addRequiredID(PreAllocSplittingID);
Evan Cheng14f8a502008-06-04 09:18:41 +0000139 AU.addRequired<LiveStacks>();
140 AU.addPreserved<LiveStacks>();
Evan Cheng26d17df2007-12-11 02:09:15 +0000141 AU.addRequired<MachineLoopInfo>();
Bill Wendling62264362008-01-04 20:54:55 +0000142 AU.addPreserved<MachineLoopInfo>();
Owen Andersondd56ab72009-03-13 05:55:11 +0000143 AU.addRequired<VirtRegMap>();
144 AU.addPreserved<VirtRegMap>();
Bill Wendling62264362008-01-04 20:54:55 +0000145 AU.addPreservedID(MachineDominatorsID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 MachineFunctionPass::getAnalysisUsage(AU);
147 }
148
149 /// runOnMachineFunction - register allocate the whole function
150 bool runOnMachineFunction(MachineFunction&);
151
152 private:
153 /// linearScan - the linear scan algorithm
154 void linearScan();
155
156 /// initIntervalSets - initialize the interval sets.
157 ///
158 void initIntervalSets();
159
160 /// processActiveIntervals - expire old intervals and move non-overlapping
161 /// ones to the inactive list.
162 void processActiveIntervals(unsigned CurPoint);
163
164 /// processInactiveIntervals - expire old intervals and move overlapping
165 /// ones to the active list.
166 void processInactiveIntervals(unsigned CurPoint);
167
Evan Cheng29b4cf62009-04-20 08:01:12 +0000168 /// hasNextReloadInterval - Return the next liveinterval that's being
169 /// defined by a reload from the same SS as the specified one.
170 LiveInterval *hasNextReloadInterval(LiveInterval *cur);
171
172 /// DowngradeRegister - Downgrade a register for allocation.
173 void DowngradeRegister(LiveInterval *li, unsigned Reg);
174
175 /// UpgradeRegister - Upgrade a register for allocation.
176 void UpgradeRegister(unsigned Reg);
177
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 /// assignRegOrStackSlotAtInterval - assign a register if one
179 /// is available, or spill.
180 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
181
Evan Chengc8a4a882009-03-23 22:57:19 +0000182 void updateSpillWeights(std::vector<float> &Weights,
183 unsigned reg, float weight,
184 const TargetRegisterClass *RC);
185
Evan Chengc5952452008-06-20 21:45:16 +0000186 /// findIntervalsToSpill - Determine the intervals to spill for the
187 /// specified interval. It's passed the physical registers whose spill
188 /// weight is the lowest among all the registers whose live intervals
189 /// conflict with the interval.
190 void findIntervalsToSpill(LiveInterval *cur,
191 std::vector<std::pair<unsigned,float> > &Candidates,
192 unsigned NumCands,
193 SmallVector<LiveInterval*, 8> &SpillIntervals);
194
Evan Chengc4c75f52007-11-03 07:20:12 +0000195 /// attemptTrivialCoalescing - If a simple interval is defined by a copy,
196 /// try allocate the definition the same register as the source register
197 /// if the register is not defined during live time of the interval. This
198 /// eliminate a copy. This is used to coalesce copies which were not
199 /// coalesced away before allocation either due to dest and src being in
200 /// different register classes or because the coalescer was overly
201 /// conservative.
202 unsigned attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg);
203
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 ///
205 /// register handling helpers
206 ///
207
208 /// getFreePhysReg - return a free physical register for this virtual
209 /// register interval if we have one, otherwise return 0.
210 unsigned getFreePhysReg(LiveInterval* cur);
Evan Cheng29b4cf62009-04-20 08:01:12 +0000211 unsigned getFreePhysReg(const TargetRegisterClass *RC,
212 unsigned MaxInactiveCount,
213 SmallVector<unsigned, 256> &inactiveCounts,
214 bool SkipDGRegs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215
216 /// assignVirt2StackSlot - assigns this virtual register to a
217 /// stack slot. returns the stack slot
218 int assignVirt2StackSlot(unsigned virtReg);
219
220 void ComputeRelatedRegClasses();
221
222 template <typename ItTy>
223 void printIntervals(const char* const str, ItTy i, ItTy e) const {
224 if (str) DOUT << str << " intervals:\n";
225 for (; i != e; ++i) {
226 DOUT << "\t" << *i->first << " -> ";
227 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000228 if (TargetRegisterInfo::isVirtualRegister(reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 reg = vrm_->getPhys(reg);
230 }
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000231 DOUT << tri_->getName(reg) << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 }
233 }
234 };
235 char RALinScan::ID = 0;
236}
237
Evan Cheng14f8a502008-06-04 09:18:41 +0000238static RegisterPass<RALinScan>
239X("linearscan-regalloc", "Linear Scan Register Allocator");
240
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241void RALinScan::ComputeRelatedRegClasses() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 // First pass, add all reg classes to the union, and determine at least one
243 // reg class that each register is in.
244 bool HasAliases = false;
Evan Cheng29b4cf62009-04-20 08:01:12 +0000245 for (TargetRegisterInfo::regclass_iterator RCI = tri_->regclass_begin(),
246 E = tri_->regclass_end(); RCI != E; ++RCI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 RelatedRegClasses.insert(*RCI);
248 for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
249 I != E; ++I) {
Evan Cheng29b4cf62009-04-20 08:01:12 +0000250 HasAliases = HasAliases || *tri_->getAliasSet(*I) != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251
252 const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
253 if (PRC) {
254 // Already processed this register. Just make sure we know that
255 // multiple register classes share a register.
256 RelatedRegClasses.unionSets(PRC, *RCI);
257 } else {
258 PRC = *RCI;
259 }
260 }
261 }
262
263 // Second pass, now that we know conservatively what register classes each reg
264 // belongs to, add info about aliases. We don't need to do this for targets
265 // without register aliases.
266 if (HasAliases)
Owen Anderson4a472712008-08-13 23:36:23 +0000267 for (DenseMap<unsigned, const TargetRegisterClass*>::iterator
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
269 I != E; ++I)
Evan Cheng29b4cf62009-04-20 08:01:12 +0000270 for (const unsigned *AS = tri_->getAliasSet(I->first); *AS; ++AS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
272}
273
Evan Chengc4c75f52007-11-03 07:20:12 +0000274/// attemptTrivialCoalescing - If a simple interval is defined by a copy,
275/// try allocate the definition the same register as the source register
276/// if the register is not defined during live time of the interval. This
277/// eliminate a copy. This is used to coalesce copies which were not
278/// coalesced away before allocation either due to dest and src being in
279/// different register classes or because the coalescer was overly
280/// conservative.
281unsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) {
Evan Chengb6aa6712007-11-04 08:32:21 +0000282 if ((cur.preference && cur.preference == Reg) || !cur.containsOneValue())
Evan Chengc4c75f52007-11-03 07:20:12 +0000283 return Reg;
284
Evan Chengdb4b2602009-01-20 00:16:18 +0000285 VNInfo *vni = cur.begin()->valno;
Evan Chengc4c75f52007-11-03 07:20:12 +0000286 if (!vni->def || vni->def == ~1U || vni->def == ~0U)
287 return Reg;
288 MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
Evan Chengf97496a2009-01-20 19:12:24 +0000289 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
290 if (!CopyMI ||
291 !tii_->isMoveInstr(*CopyMI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc4c75f52007-11-03 07:20:12 +0000292 return Reg;
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +0000293 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Chengc4c75f52007-11-03 07:20:12 +0000294 if (!vrm_->isAssignedReg(SrcReg))
295 return Reg;
296 else
297 SrcReg = vrm_->getPhys(SrcReg);
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +0000298 }
Evan Chengc4c75f52007-11-03 07:20:12 +0000299 if (Reg == SrcReg)
300 return Reg;
301
Evan Cheng06b74c52008-09-18 22:38:47 +0000302 const TargetRegisterClass *RC = mri_->getRegClass(cur.reg);
Evan Chengc4c75f52007-11-03 07:20:12 +0000303 if (!RC->contains(SrcReg))
304 return Reg;
305
306 // Try to coalesce.
307 if (!li_->conflictsWithPhysRegDef(cur, *vrm_, SrcReg)) {
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000308 DOUT << "Coalescing: " << cur << " -> " << tri_->getName(SrcReg)
Bill Wendling8eeb9792008-02-26 21:11:01 +0000309 << '\n';
Evan Chengc4c75f52007-11-03 07:20:12 +0000310 vrm_->clearVirt(cur.reg);
311 vrm_->assignVirt2Phys(cur.reg, SrcReg);
312 ++NumCoalesce;
313 return SrcReg;
314 }
315
316 return Reg;
317}
318
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319bool RALinScan::runOnMachineFunction(MachineFunction &fn) {
320 mf_ = &fn;
Evan Chengc5952452008-06-20 21:45:16 +0000321 mri_ = &fn.getRegInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 tm_ = &fn.getTarget();
Dan Gohman1e57df32008-02-10 18:45:23 +0000323 tri_ = tm_->getRegisterInfo();
Evan Chengc4c75f52007-11-03 07:20:12 +0000324 tii_ = tm_->getInstrInfo();
Dan Gohman1e57df32008-02-10 18:45:23 +0000325 allocatableRegs_ = tri_->getAllocatableSet(fn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 li_ = &getAnalysis<LiveIntervals>();
Evan Cheng14f8a502008-06-04 09:18:41 +0000327 ls_ = &getAnalysis<LiveStacks>();
Evan Cheng26d17df2007-12-11 02:09:15 +0000328 loopInfo = &getAnalysis<MachineLoopInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329
David Greene1d80f1b2007-09-06 16:18:45 +0000330 // We don't run the coalescer here because we have no reason to
331 // interact with it. If the coalescer requires interaction, it
332 // won't do anything. If it doesn't require interaction, we assume
333 // it was run as a separate pass.
334
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 // If this is the first function compiled, compute the related reg classes.
336 if (RelatedRegClasses.empty())
337 ComputeRelatedRegClasses();
338
Dan Gohman1e57df32008-02-10 18:45:23 +0000339 if (!prt_.get()) prt_.reset(new PhysRegTracker(*tri_));
Owen Andersondd56ab72009-03-13 05:55:11 +0000340 vrm_ = &getAnalysis<VirtRegMap>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 if (!spiller_.get()) spiller_.reset(createSpiller());
342
343 initIntervalSets();
344
345 linearScan();
346
347 // Rewrite spill code and update the PhysRegsUsed set.
348 spiller_->runOnMachineFunction(*mf_, *vrm_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349
Dan Gohman79a9f152008-06-23 23:51:16 +0000350 assert(unhandled_.empty() && "Unhandled live intervals remain!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 fixed_.clear();
352 active_.clear();
353 inactive_.clear();
354 handled_.clear();
Evan Cheng29b4cf62009-04-20 08:01:12 +0000355 NextReloadMap.clear();
356 DowngradedRegs.clear();
357 DowngradeMap.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358
359 return true;
360}
361
362/// initIntervalSets - initialize the interval sets.
363///
364void RALinScan::initIntervalSets()
365{
366 assert(unhandled_.empty() && fixed_.empty() &&
367 active_.empty() && inactive_.empty() &&
368 "interval sets should be empty on initialization");
369
Owen Andersonba926a32008-08-15 18:49:41 +0000370 handled_.reserve(li_->getNumIntervals());
371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Owen Anderson348d1d82008-08-13 21:49:13 +0000373 if (TargetRegisterInfo::isPhysicalRegister(i->second->reg)) {
Evan Cheng06b74c52008-09-18 22:38:47 +0000374 mri_->setPhysRegUsed(i->second->reg);
Owen Anderson348d1d82008-08-13 21:49:13 +0000375 fixed_.push_back(std::make_pair(i->second, i->second->begin()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 } else
Owen Anderson348d1d82008-08-13 21:49:13 +0000377 unhandled_.push(i->second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 }
379}
380
381void RALinScan::linearScan()
382{
383 // linear scan algorithm
384 DOUT << "********** LINEAR SCAN **********\n";
385 DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n';
386
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388
389 while (!unhandled_.empty()) {
390 // pick the interval with the earliest start point
391 LiveInterval* cur = unhandled_.top();
392 unhandled_.pop();
Evan Chengd48f2bc2007-10-16 21:09:14 +0000393 ++NumIters;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 DOUT << "\n*** CURRENT ***: " << *cur << '\n';
395
Evan Chenga3186992008-04-03 16:40:27 +0000396 if (!cur->empty()) {
397 processActiveIntervals(cur->beginNumber());
398 processInactiveIntervals(cur->beginNumber());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399
Evan Chenga3186992008-04-03 16:40:27 +0000400 assert(TargetRegisterInfo::isVirtualRegister(cur->reg) &&
401 "Can only allocate virtual registers!");
402 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403
404 // Allocating a virtual register. try to find a free
405 // physical register or spill an interval (possibly this one) in order to
406 // assign it one.
407 assignRegOrStackSlotAtInterval(cur);
408
409 DEBUG(printIntervals("active", active_.begin(), active_.end()));
410 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
411 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412
413 // expire any remaining active intervals
Evan Chengd48f2bc2007-10-16 21:09:14 +0000414 while (!active_.empty()) {
415 IntervalPtr &IP = active_.back();
416 unsigned reg = IP.first->reg;
417 DOUT << "\tinterval " << *IP.first << " expired\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000418 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 "Can only allocate virtual registers!");
420 reg = vrm_->getPhys(reg);
421 prt_->delRegUse(reg);
Evan Chengd48f2bc2007-10-16 21:09:14 +0000422 active_.pop_back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 }
424
425 // expire any remaining inactive intervals
Evan Chengd48f2bc2007-10-16 21:09:14 +0000426 DEBUG(for (IntervalPtrs::reverse_iterator
Bill Wendling1817ab82007-11-15 00:40:48 +0000427 i = inactive_.rbegin(); i != inactive_.rend(); ++i)
Evan Chengd48f2bc2007-10-16 21:09:14 +0000428 DOUT << "\tinterval " << *i->first << " expired\n");
429 inactive_.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430
Evan Chengcecc8222007-11-17 00:40:40 +0000431 // Add live-ins to every BB except for entry. Also perform trivial coalescing.
Evan Chengf5cdf122007-10-17 02:12:22 +0000432 MachineFunction::iterator EntryMBB = mf_->begin();
Evan Cheng12d6fcb2007-10-17 06:53:44 +0000433 SmallVector<MachineBasicBlock*, 8> LiveInMBBs;
Evan Chengf5cdf122007-10-17 02:12:22 +0000434 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Owen Anderson348d1d82008-08-13 21:49:13 +0000435 LiveInterval &cur = *i->second;
Evan Chengf5cdf122007-10-17 02:12:22 +0000436 unsigned Reg = 0;
Dan Gohman1e57df32008-02-10 18:45:23 +0000437 bool isPhys = TargetRegisterInfo::isPhysicalRegister(cur.reg);
Evan Chengcecc8222007-11-17 00:40:40 +0000438 if (isPhys)
Owen Anderson348d1d82008-08-13 21:49:13 +0000439 Reg = cur.reg;
Evan Chengf5cdf122007-10-17 02:12:22 +0000440 else if (vrm_->isAssignedReg(cur.reg))
Evan Chengc4c75f52007-11-03 07:20:12 +0000441 Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg));
Evan Chengf5cdf122007-10-17 02:12:22 +0000442 if (!Reg)
443 continue;
Evan Chengcecc8222007-11-17 00:40:40 +0000444 // Ignore splited live intervals.
445 if (!isPhys && vrm_->getPreSplitReg(cur.reg))
446 continue;
Evan Chengf5cdf122007-10-17 02:12:22 +0000447 for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end();
448 I != E; ++I) {
449 const LiveRange &LR = *I;
Evan Cheng84f9fc22008-10-29 05:06:14 +0000450 if (li_->findLiveInMBBs(LR.start, LR.end, LiveInMBBs)) {
Evan Chengf5cdf122007-10-17 02:12:22 +0000451 for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i)
452 if (LiveInMBBs[i] != EntryMBB)
453 LiveInMBBs[i]->addLiveIn(Reg);
Evan Cheng12d6fcb2007-10-17 06:53:44 +0000454 LiveInMBBs.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 }
456 }
457 }
458
459 DOUT << *vrm_;
460}
461
462/// processActiveIntervals - expire old intervals and move non-overlapping ones
463/// to the inactive list.
464void RALinScan::processActiveIntervals(unsigned CurPoint)
465{
466 DOUT << "\tprocessing active intervals:\n";
467
468 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
469 LiveInterval *Interval = active_[i].first;
470 LiveInterval::iterator IntervalPos = active_[i].second;
471 unsigned reg = Interval->reg;
472
473 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
474
475 if (IntervalPos == Interval->end()) { // Remove expired intervals.
476 DOUT << "\t\tinterval " << *Interval << " expired\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000477 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 "Can only allocate virtual registers!");
479 reg = vrm_->getPhys(reg);
480 prt_->delRegUse(reg);
481
482 // Pop off the end of the list.
483 active_[i] = active_.back();
484 active_.pop_back();
485 --i; --e;
486
487 } else if (IntervalPos->start > CurPoint) {
488 // Move inactive intervals to inactive list.
489 DOUT << "\t\tinterval " << *Interval << " inactive\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000490 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 "Can only allocate virtual registers!");
492 reg = vrm_->getPhys(reg);
493 prt_->delRegUse(reg);
494 // add to inactive.
495 inactive_.push_back(std::make_pair(Interval, IntervalPos));
496
497 // Pop off the end of the list.
498 active_[i] = active_.back();
499 active_.pop_back();
500 --i; --e;
501 } else {
502 // Otherwise, just update the iterator position.
503 active_[i].second = IntervalPos;
504 }
505 }
506}
507
508/// processInactiveIntervals - expire old intervals and move overlapping
509/// ones to the active list.
510void RALinScan::processInactiveIntervals(unsigned CurPoint)
511{
512 DOUT << "\tprocessing inactive intervals:\n";
513
514 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
515 LiveInterval *Interval = inactive_[i].first;
516 LiveInterval::iterator IntervalPos = inactive_[i].second;
517 unsigned reg = Interval->reg;
518
519 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
520
521 if (IntervalPos == Interval->end()) { // remove expired intervals.
522 DOUT << "\t\tinterval " << *Interval << " expired\n";
523
524 // Pop off the end of the list.
525 inactive_[i] = inactive_.back();
526 inactive_.pop_back();
527 --i; --e;
528 } else if (IntervalPos->start <= CurPoint) {
529 // move re-activated intervals in active list
530 DOUT << "\t\tinterval " << *Interval << " active\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000531 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 "Can only allocate virtual registers!");
533 reg = vrm_->getPhys(reg);
534 prt_->addRegUse(reg);
535 // add to active
536 active_.push_back(std::make_pair(Interval, IntervalPos));
537
538 // Pop off the end of the list.
539 inactive_[i] = inactive_.back();
540 inactive_.pop_back();
541 --i; --e;
542 } else {
543 // Otherwise, just update the iterator position.
544 inactive_[i].second = IntervalPos;
545 }
546 }
547}
548
549/// updateSpillWeights - updates the spill weights of the specifed physical
550/// register and its weight.
Evan Chengc8a4a882009-03-23 22:57:19 +0000551void RALinScan::updateSpillWeights(std::vector<float> &Weights,
552 unsigned reg, float weight,
553 const TargetRegisterClass *RC) {
554 SmallSet<unsigned, 4> Processed;
555 SmallSet<unsigned, 4> SuperAdded;
556 SmallVector<unsigned, 4> Supers;
Evan Cheng29b4cf62009-04-20 08:01:12 +0000557 // Unfavor downgraded registers for spilling.
558 if (DowngradedRegs.count(reg))
559 weight *= 2.0f;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 Weights[reg] += weight;
Evan Chengc8a4a882009-03-23 22:57:19 +0000561 Processed.insert(reg);
562 for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 Weights[*as] += weight;
Evan Chengc8a4a882009-03-23 22:57:19 +0000564 Processed.insert(*as);
565 if (tri_->isSubRegister(*as, reg) &&
566 SuperAdded.insert(*as) &&
567 RC->contains(*as)) {
568 Supers.push_back(*as);
569 }
570 }
571
572 // If the alias is a super-register, and the super-register is in the
573 // register class we are trying to allocate. Then add the weight to all
574 // sub-registers of the super-register even if they are not aliases.
575 // e.g. allocating for GR32, bh is not used, updating bl spill weight.
576 // bl should get the same spill weight otherwise it will be choosen
577 // as a spill candidate since spilling bh doesn't make ebx available.
578 for (unsigned i = 0, e = Supers.size(); i != e; ++i) {
579 for (const unsigned *sr = tri_->getSubRegisters(Supers[i]); *sr; ++sr)
580 if (!Processed.count(*sr))
581 Weights[*sr] += weight;
582 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583}
584
585static
586RALinScan::IntervalPtrs::iterator
587FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) {
588 for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end();
589 I != E; ++I)
590 if (I->first == LI) return I;
591 return IP.end();
592}
593
594static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V, unsigned Point){
595 for (unsigned i = 0, e = V.size(); i != e; ++i) {
596 RALinScan::IntervalPtr &IP = V[i];
597 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
598 IP.second, Point);
599 if (I != IP.first->begin()) --I;
600 IP.second = I;
601 }
602}
603
Evan Cheng14f8a502008-06-04 09:18:41 +0000604/// addStackInterval - Create a LiveInterval for stack if the specified live
605/// interval has been spilled.
606static void addStackInterval(LiveInterval *cur, LiveStacks *ls_,
Evan Chengba221ca2008-06-06 07:54:39 +0000607 LiveIntervals *li_, float &Weight,
608 VirtRegMap &vrm_) {
Evan Cheng14f8a502008-06-04 09:18:41 +0000609 int SS = vrm_.getStackSlot(cur->reg);
610 if (SS == VirtRegMap::NO_STACK_SLOT)
611 return;
612 LiveInterval &SI = ls_->getOrCreateInterval(SS);
Evan Chengba221ca2008-06-06 07:54:39 +0000613 SI.weight += Weight;
614
Evan Cheng14f8a502008-06-04 09:18:41 +0000615 VNInfo *VNI;
Evan Cheng29f36f52008-10-29 08:39:34 +0000616 if (SI.hasAtLeastOneValue())
Evan Cheng14f8a502008-06-04 09:18:41 +0000617 VNI = SI.getValNumInfo(0);
618 else
619 VNI = SI.getNextValue(~0U, 0, ls_->getVNInfoAllocator());
620
621 LiveInterval &RI = li_->getInterval(cur->reg);
622 // FIXME: This may be overly conservative.
623 SI.MergeRangesInAsValue(RI, VNI);
Evan Cheng14f8a502008-06-04 09:18:41 +0000624}
625
Evan Chengc5952452008-06-20 21:45:16 +0000626/// getConflictWeight - Return the number of conflicts between cur
627/// live interval and defs and uses of Reg weighted by loop depthes.
628static float getConflictWeight(LiveInterval *cur, unsigned Reg,
629 LiveIntervals *li_,
630 MachineRegisterInfo *mri_,
631 const MachineLoopInfo *loopInfo) {
632 float Conflicts = 0;
633 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(Reg),
634 E = mri_->reg_end(); I != E; ++I) {
635 MachineInstr *MI = &*I;
636 if (cur->liveAt(li_->getInstructionIndex(MI))) {
637 unsigned loopDepth = loopInfo->getLoopDepth(MI->getParent());
638 Conflicts += powf(10.0f, (float)loopDepth);
639 }
640 }
641 return Conflicts;
642}
643
644/// findIntervalsToSpill - Determine the intervals to spill for the
645/// specified interval. It's passed the physical registers whose spill
646/// weight is the lowest among all the registers whose live intervals
647/// conflict with the interval.
648void RALinScan::findIntervalsToSpill(LiveInterval *cur,
649 std::vector<std::pair<unsigned,float> > &Candidates,
650 unsigned NumCands,
651 SmallVector<LiveInterval*, 8> &SpillIntervals) {
652 // We have figured out the *best* register to spill. But there are other
653 // registers that are pretty good as well (spill weight within 3%). Spill
654 // the one that has fewest defs and uses that conflict with cur.
655 float Conflicts[3] = { 0.0f, 0.0f, 0.0f };
656 SmallVector<LiveInterval*, 8> SLIs[3];
657
658 DOUT << "\tConsidering " << NumCands << " candidates: ";
659 DEBUG(for (unsigned i = 0; i != NumCands; ++i)
660 DOUT << tri_->getName(Candidates[i].first) << " ";
661 DOUT << "\n";);
662
663 // Calculate the number of conflicts of each candidate.
664 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
665 unsigned Reg = i->first->reg;
666 unsigned PhysReg = vrm_->getPhys(Reg);
667 if (!cur->overlapsFrom(*i->first, i->second))
668 continue;
669 for (unsigned j = 0; j < NumCands; ++j) {
670 unsigned Candidate = Candidates[j].first;
671 if (tri_->regsOverlap(PhysReg, Candidate)) {
672 if (NumCands > 1)
673 Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo);
674 SLIs[j].push_back(i->first);
675 }
676 }
677 }
678
679 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
680 unsigned Reg = i->first->reg;
681 unsigned PhysReg = vrm_->getPhys(Reg);
682 if (!cur->overlapsFrom(*i->first, i->second-1))
683 continue;
684 for (unsigned j = 0; j < NumCands; ++j) {
685 unsigned Candidate = Candidates[j].first;
686 if (tri_->regsOverlap(PhysReg, Candidate)) {
687 if (NumCands > 1)
688 Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo);
689 SLIs[j].push_back(i->first);
690 }
691 }
692 }
693
694 // Which is the best candidate?
695 unsigned BestCandidate = 0;
696 float MinConflicts = Conflicts[0];
697 for (unsigned i = 1; i != NumCands; ++i) {
698 if (Conflicts[i] < MinConflicts) {
699 BestCandidate = i;
700 MinConflicts = Conflicts[i];
701 }
702 }
703
704 std::copy(SLIs[BestCandidate].begin(), SLIs[BestCandidate].end(),
705 std::back_inserter(SpillIntervals));
706}
707
708namespace {
709 struct WeightCompare {
710 typedef std::pair<unsigned, float> RegWeightPair;
711 bool operator()(const RegWeightPair &LHS, const RegWeightPair &RHS) const {
712 return LHS.second < RHS.second;
713 }
714 };
715}
716
717static bool weightsAreClose(float w1, float w2) {
718 if (!NewHeuristic)
719 return false;
720
721 float diff = w1 - w2;
722 if (diff <= 0.02f) // Within 0.02f
723 return true;
724 return (diff / w2) <= 0.05f; // Within 5%.
725}
726
Evan Cheng29b4cf62009-04-20 08:01:12 +0000727LiveInterval *RALinScan::hasNextReloadInterval(LiveInterval *cur) {
728 DenseMap<unsigned, unsigned>::iterator I = NextReloadMap.find(cur->reg);
729 if (I == NextReloadMap.end())
730 return 0;
731 return &li_->getInterval(I->second);
732}
733
734void RALinScan::DowngradeRegister(LiveInterval *li, unsigned Reg) {
735 bool isNew = DowngradedRegs.insert(Reg);
736 isNew = isNew; // Silence compiler warning.
737 assert(isNew && "Multiple reloads holding the same register?");
738 DowngradeMap.insert(std::make_pair(li->reg, Reg));
739 for (const unsigned *AS = tri_->getAliasSet(Reg); *AS; ++AS) {
740 isNew = DowngradedRegs.insert(*AS);
741 isNew = isNew; // Silence compiler warning.
742 assert(isNew && "Multiple reloads holding the same register?");
743 DowngradeMap.insert(std::make_pair(li->reg, *AS));
744 }
745 ++NumDowngrade;
746}
747
748void RALinScan::UpgradeRegister(unsigned Reg) {
749 if (Reg) {
750 DowngradedRegs.erase(Reg);
751 for (const unsigned *AS = tri_->getAliasSet(Reg); *AS; ++AS)
752 DowngradedRegs.erase(*AS);
753 }
754}
755
756namespace {
757 struct LISorter {
758 bool operator()(LiveInterval* A, LiveInterval* B) {
759 return A->beginNumber() < B->beginNumber();
760 }
761 };
762}
763
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
765/// spill.
766void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur)
767{
768 DOUT << "\tallocating current interval: ";
769
Evan Chenga3186992008-04-03 16:40:27 +0000770 // This is an implicitly defined live interval, just assign any register.
Evan Cheng06b74c52008-09-18 22:38:47 +0000771 const TargetRegisterClass *RC = mri_->getRegClass(cur->reg);
Evan Chenga3186992008-04-03 16:40:27 +0000772 if (cur->empty()) {
773 unsigned physReg = cur->preference;
774 if (!physReg)
775 physReg = *RC->allocation_order_begin(*mf_);
776 DOUT << tri_->getName(physReg) << '\n';
777 // Note the register is not really in use.
778 vrm_->assignVirt2Phys(cur->reg, physReg);
Evan Chenga3186992008-04-03 16:40:27 +0000779 return;
780 }
781
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 PhysRegTracker backupPrt = *prt_;
783
784 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
785 unsigned StartPosition = cur->beginNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
Evan Chengc4c75f52007-11-03 07:20:12 +0000787
Evan Chengdb4b2602009-01-20 00:16:18 +0000788 // If start of this live interval is defined by a move instruction and its
789 // source is assigned a physical register that is compatible with the target
790 // register class, then we should try to assign it the same register.
Evan Chengc4c75f52007-11-03 07:20:12 +0000791 // This can happen when the move is from a larger register class to a smaller
792 // one, e.g. X86::mov32to32_. These move instructions are not coalescable.
Evan Chengdb4b2602009-01-20 00:16:18 +0000793 if (!cur->preference && cur->hasAtLeastOneValue()) {
794 VNInfo *vni = cur->begin()->valno;
Evan Chengc4c75f52007-11-03 07:20:12 +0000795 if (vni->def && vni->def != ~1U && vni->def != ~0U) {
796 MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
Evan Chengf97496a2009-01-20 19:12:24 +0000797 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
798 if (CopyMI &&
799 tii_->isMoveInstr(*CopyMI, SrcReg, DstReg, SrcSubReg, DstSubReg)) {
Evan Chengc4c75f52007-11-03 07:20:12 +0000800 unsigned Reg = 0;
Dan Gohman1e57df32008-02-10 18:45:23 +0000801 if (TargetRegisterInfo::isPhysicalRegister(SrcReg))
Evan Chengc4c75f52007-11-03 07:20:12 +0000802 Reg = SrcReg;
803 else if (vrm_->isAssignedReg(SrcReg))
804 Reg = vrm_->getPhys(SrcReg);
805 if (Reg && allocatableRegs_[Reg] && RC->contains(Reg))
806 cur->preference = Reg;
807 }
808 }
809 }
810
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 // for every interval in inactive we overlap with, mark the
812 // register as not free and update spill weights.
813 for (IntervalPtrs::const_iterator i = inactive_.begin(),
814 e = inactive_.end(); i != e; ++i) {
815 unsigned Reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000816 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 "Can only allocate virtual registers!");
Evan Cheng06b74c52008-09-18 22:38:47 +0000818 const TargetRegisterClass *RegRC = mri_->getRegClass(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 // If this is not in a related reg class to the register we're allocating,
820 // don't check it.
821 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
822 cur->overlapsFrom(*i->first, i->second-1)) {
823 Reg = vrm_->getPhys(Reg);
824 prt_->addRegUse(Reg);
825 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
826 }
827 }
828
829 // Speculatively check to see if we can get a register right now. If not,
830 // we know we won't be able to by adding more constraints. If so, we can
831 // check to see if it is valid. Doing an exhaustive search of the fixed_ list
832 // is very bad (it contains all callee clobbered registers for any functions
833 // with a call), so we want to avoid doing that if possible.
834 unsigned physReg = getFreePhysReg(cur);
Evan Cheng14cc83f2008-03-11 07:19:34 +0000835 unsigned BestPhysReg = physReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 if (physReg) {
837 // We got a register. However, if it's in the fixed_ list, we might
838 // conflict with it. Check to see if we conflict with it or any of its
839 // aliases.
Evan Chengc4c75f52007-11-03 07:20:12 +0000840 SmallSet<unsigned, 8> RegAliases;
Dan Gohman1e57df32008-02-10 18:45:23 +0000841 for (const unsigned *AS = tri_->getAliasSet(physReg); *AS; ++AS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 RegAliases.insert(*AS);
843
844 bool ConflictsWithFixed = false;
845 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
846 IntervalPtr &IP = fixed_[i];
847 if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) {
848 // Okay, this reg is on the fixed list. Check to see if we actually
849 // conflict.
850 LiveInterval *I = IP.first;
851 if (I->endNumber() > StartPosition) {
852 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
853 IP.second = II;
854 if (II != I->begin() && II->start > StartPosition)
855 --II;
856 if (cur->overlapsFrom(*I, II)) {
857 ConflictsWithFixed = true;
858 break;
859 }
860 }
861 }
862 }
863
864 // Okay, the register picked by our speculative getFreePhysReg call turned
865 // out to be in use. Actually add all of the conflicting fixed registers to
866 // prt so we can do an accurate query.
867 if (ConflictsWithFixed) {
868 // For every interval in fixed we overlap with, mark the register as not
869 // free and update spill weights.
870 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
871 IntervalPtr &IP = fixed_[i];
872 LiveInterval *I = IP.first;
873
874 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
875 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
876 I->endNumber() > StartPosition) {
877 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
878 IP.second = II;
879 if (II != I->begin() && II->start > StartPosition)
880 --II;
881 if (cur->overlapsFrom(*I, II)) {
882 unsigned reg = I->reg;
883 prt_->addRegUse(reg);
884 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
885 }
886 }
887 }
888
889 // Using the newly updated prt_ object, which includes conflicts in the
890 // future, see if there are any registers available.
891 physReg = getFreePhysReg(cur);
892 }
893 }
894
895 // Restore the physical register tracker, removing information about the
896 // future.
897 *prt_ = backupPrt;
898
899 // if we find a free register, we are done: assign this virtual to
900 // the free physical register and add this interval to the active
901 // list.
902 if (physReg) {
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000903 DOUT << tri_->getName(physReg) << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 vrm_->assignVirt2Phys(cur->reg, physReg);
905 prt_->addRegUse(physReg);
906 active_.push_back(std::make_pair(cur, cur->begin()));
907 handled_.push_back(cur);
Evan Cheng29b4cf62009-04-20 08:01:12 +0000908
909 // "Upgrade" the physical register since it has been allocated.
910 UpgradeRegister(physReg);
911 if (LiveInterval *NextReloadLI = hasNextReloadInterval(cur)) {
912 // "Downgrade" physReg to try to keep physReg from being allocated until
913 // the next reload from the same SS is allocated.
914 NextReloadLI->preference = physReg;
915 DowngradeRegister(cur, physReg);
916 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 return;
918 }
919 DOUT << "no free registers\n";
920
921 // Compile the spill weights into an array that is better for scanning.
Evan Chengc5952452008-06-20 21:45:16 +0000922 std::vector<float> SpillWeights(tri_->getNumRegs(), 0.0f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 for (std::vector<std::pair<unsigned, float> >::iterator
924 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
Evan Chengc8a4a882009-03-23 22:57:19 +0000925 updateSpillWeights(SpillWeights, I->first, I->second, RC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926
927 // for each interval in active, update spill weights.
928 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
929 i != e; ++i) {
930 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000931 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 "Can only allocate virtual registers!");
933 reg = vrm_->getPhys(reg);
Evan Chengc8a4a882009-03-23 22:57:19 +0000934 updateSpillWeights(SpillWeights, reg, i->first->weight, RC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 }
936
937 DOUT << "\tassigning stack slot at interval "<< *cur << ":\n";
938
939 // Find a register to spill.
940 float minWeight = HUGE_VALF;
Evan Chengc8a4a882009-03-23 22:57:19 +0000941 unsigned minReg = 0; /*cur->preference*/; // Try the pref register first.
Evan Chengc5952452008-06-20 21:45:16 +0000942
943 bool Found = false;
944 std::vector<std::pair<unsigned,float> > RegsWeights;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 if (!minReg || SpillWeights[minReg] == HUGE_VALF)
946 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
947 e = RC->allocation_order_end(*mf_); i != e; ++i) {
948 unsigned reg = *i;
Evan Chengc5952452008-06-20 21:45:16 +0000949 float regWeight = SpillWeights[reg];
950 if (minWeight > regWeight)
951 Found = true;
952 RegsWeights.push_back(std::make_pair(reg, regWeight));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953 }
954
955 // If we didn't find a register that is spillable, try aliases?
Evan Chengc5952452008-06-20 21:45:16 +0000956 if (!Found) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
958 e = RC->allocation_order_end(*mf_); i != e; ++i) {
959 unsigned reg = *i;
960 // No need to worry about if the alias register size < regsize of RC.
961 // We are going to spill all registers that alias it anyway.
Evan Chengc5952452008-06-20 21:45:16 +0000962 for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as)
963 RegsWeights.push_back(std::make_pair(*as, SpillWeights[*as]));
Evan Cheng14cc83f2008-03-11 07:19:34 +0000964 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000965 }
Evan Chengc5952452008-06-20 21:45:16 +0000966
967 // Sort all potential spill candidates by weight.
968 std::sort(RegsWeights.begin(), RegsWeights.end(), WeightCompare());
969 minReg = RegsWeights[0].first;
970 minWeight = RegsWeights[0].second;
971 if (minWeight == HUGE_VALF) {
972 // All registers must have inf weight. Just grab one!
973 minReg = BestPhysReg ? BestPhysReg : *RC->allocation_order_begin(*mf_);
Owen Andersona0e65132008-07-22 22:46:49 +0000974 if (cur->weight == HUGE_VALF ||
Evan Chengaf3c4e32008-09-20 01:28:05 +0000975 li_->getApproximateInstructionCount(*cur) == 0) {
Evan Chengc5952452008-06-20 21:45:16 +0000976 // Spill a physical register around defs and uses.
Evan Cheng29b4cf62009-04-20 08:01:12 +0000977 if (li_->spillPhysRegAroundRegDefsUses(*cur, minReg, *vrm_)) {
978 DowngradedRegs.clear();
Evan Cheng973473b2009-03-23 18:24:37 +0000979 assignRegOrStackSlotAtInterval(cur);
Evan Cheng29b4cf62009-04-20 08:01:12 +0000980 } else {
Evan Cheng973473b2009-03-23 18:24:37 +0000981 cerr << "Ran out of registers during register allocation!\n";
982 exit(1);
983 }
Evan Chengaf3c4e32008-09-20 01:28:05 +0000984 return;
985 }
Evan Chengc5952452008-06-20 21:45:16 +0000986 }
987
988 // Find up to 3 registers to consider as spill candidates.
989 unsigned LastCandidate = RegsWeights.size() >= 3 ? 3 : 1;
990 while (LastCandidate > 1) {
991 if (weightsAreClose(RegsWeights[LastCandidate-1].second, minWeight))
992 break;
993 --LastCandidate;
994 }
995
996 DOUT << "\t\tregister(s) with min weight(s): ";
997 DEBUG(for (unsigned i = 0; i != LastCandidate; ++i)
998 DOUT << tri_->getName(RegsWeights[i].first)
999 << " (" << RegsWeights[i].second << ")\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000
Evan Cheng29b4cf62009-04-20 08:01:12 +00001001 // If the current has the minimum weight, we need to spill it and
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 // add any added intervals back to unhandled, and restart
1003 // linearscan.
1004 if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
1005 DOUT << "\t\t\tspilling(c): " << *cur << '\n';
Evan Chengba221ca2008-06-06 07:54:39 +00001006 float SSWeight;
Evan Chengc84ea132008-09-30 15:44:16 +00001007 SmallVector<LiveInterval*, 8> spillIs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008 std::vector<LiveInterval*> added =
Evan Chengc84ea132008-09-30 15:44:16 +00001009 li_->addIntervalsForSpills(*cur, spillIs, loopInfo, *vrm_, SSWeight);
Evan Cheng29b4cf62009-04-20 08:01:12 +00001010 std::sort(added.begin(), added.end(), LISorter());
Evan Chengba221ca2008-06-06 07:54:39 +00001011 addStackInterval(cur, ls_, li_, SSWeight, *vrm_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 if (added.empty())
1013 return; // Early exit if all spills were folded.
1014
Evan Cheng29b4cf62009-04-20 08:01:12 +00001015 // Merge added with unhandled. Note that we have already sorted
1016 // intervals returned by addIntervalsForSpills by their starting
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 // point.
1018 for (unsigned i = 0, e = added.size(); i != e; ++i)
1019 unhandled_.push(added[i]);
1020 return;
1021 }
1022
1023 ++NumBacktracks;
1024
Evan Cheng29b4cf62009-04-20 08:01:12 +00001025 // Push the current interval back to unhandled since we are going
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 // to re-run at least this iteration. Since we didn't modify it it
1027 // should go back right in the front of the list
1028 unhandled_.push(cur);
1029
Dan Gohman1e57df32008-02-10 18:45:23 +00001030 assert(TargetRegisterInfo::isPhysicalRegister(minReg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 "did not choose a register to spill?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032
Evan Chengc5952452008-06-20 21:45:16 +00001033 // We spill all intervals aliasing the register with
1034 // minimum weight, rollback to the interval with the earliest
1035 // start point and let the linear scan algorithm run again
1036 SmallVector<LiveInterval*, 8> spillIs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037
Evan Chengc5952452008-06-20 21:45:16 +00001038 // Determine which intervals have to be spilled.
1039 findIntervalsToSpill(cur, RegsWeights, LastCandidate, spillIs);
1040
1041 // Set of spilled vregs (used later to rollback properly)
1042 SmallSet<unsigned, 8> spilled;
1043
1044 // The earliest start of a Spilled interval indicates up to where
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 // in handled we need to roll back
1046 unsigned earliestStart = cur->beginNumber();
1047
Evan Chengc5952452008-06-20 21:45:16 +00001048 // Spill live intervals of virtual regs mapped to the physical register we
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 // want to clear (and its aliases). We only spill those that overlap with the
1050 // current interval as the rest do not affect its allocation. we also keep
1051 // track of the earliest start of all spilled live intervals since this will
1052 // mark our rollback point.
Evan Chengc5952452008-06-20 21:45:16 +00001053 std::vector<LiveInterval*> added;
1054 while (!spillIs.empty()) {
1055 LiveInterval *sli = spillIs.back();
1056 spillIs.pop_back();
1057 DOUT << "\t\t\tspilling(a): " << *sli << '\n';
1058 earliestStart = std::min(earliestStart, sli->beginNumber());
1059 float SSWeight;
1060 std::vector<LiveInterval*> newIs =
Evan Chengc84ea132008-09-30 15:44:16 +00001061 li_->addIntervalsForSpills(*sli, spillIs, loopInfo, *vrm_, SSWeight);
Evan Chengc5952452008-06-20 21:45:16 +00001062 addStackInterval(sli, ls_, li_, SSWeight, *vrm_);
1063 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
1064 spilled.insert(sli->reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 }
1066
1067 DOUT << "\t\trolling back to: " << earliestStart << '\n';
1068
1069 // Scan handled in reverse order up to the earliest start of a
1070 // spilled live interval and undo each one, restoring the state of
1071 // unhandled.
1072 while (!handled_.empty()) {
1073 LiveInterval* i = handled_.back();
1074 // If this interval starts before t we are done.
1075 if (i->beginNumber() < earliestStart)
1076 break;
1077 DOUT << "\t\t\tundo changes for: " << *i << '\n';
1078 handled_.pop_back();
1079
1080 // When undoing a live interval allocation we must know if it is active or
1081 // inactive to properly update the PhysRegTracker and the VirtRegMap.
1082 IntervalPtrs::iterator it;
1083 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
1084 active_.erase(it);
Dan Gohman1e57df32008-02-10 18:45:23 +00001085 assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 if (!spilled.count(i->reg))
1087 unhandled_.push(i);
1088 prt_->delRegUse(vrm_->getPhys(i->reg));
1089 vrm_->clearVirt(i->reg);
1090 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
1091 inactive_.erase(it);
Dan Gohman1e57df32008-02-10 18:45:23 +00001092 assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 if (!spilled.count(i->reg))
1094 unhandled_.push(i);
1095 vrm_->clearVirt(i->reg);
1096 } else {
Dan Gohman1e57df32008-02-10 18:45:23 +00001097 assert(TargetRegisterInfo::isVirtualRegister(i->reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 "Can only allocate virtual registers!");
1099 vrm_->clearVirt(i->reg);
1100 unhandled_.push(i);
1101 }
Evan Chengb6aa6712007-11-04 08:32:21 +00001102
Evan Cheng29b4cf62009-04-20 08:01:12 +00001103 DenseMap<unsigned, unsigned>::iterator ii = DowngradeMap.find(i->reg);
1104 if (ii == DowngradeMap.end())
1105 // It interval has a preference, it must be defined by a copy. Clear the
1106 // preference now since the source interval allocation may have been
1107 // undone as well.
1108 i->preference = 0;
1109 else {
1110 UpgradeRegister(ii->second);
1111 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001112 }
1113
1114 // Rewind the iterators in the active, inactive, and fixed lists back to the
1115 // point we reverted to.
1116 RevertVectorIteratorsTo(active_, earliestStart);
1117 RevertVectorIteratorsTo(inactive_, earliestStart);
1118 RevertVectorIteratorsTo(fixed_, earliestStart);
1119
Evan Cheng29b4cf62009-04-20 08:01:12 +00001120 // Scan the rest and undo each interval that expired after t and
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001121 // insert it in active (the next iteration of the algorithm will
1122 // put it in inactive if required)
1123 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
1124 LiveInterval *HI = handled_[i];
1125 if (!HI->expiredAt(earliestStart) &&
1126 HI->expiredAt(cur->beginNumber())) {
1127 DOUT << "\t\t\tundo changes for: " << *HI << '\n';
1128 active_.push_back(std::make_pair(HI, HI->begin()));
Dan Gohman1e57df32008-02-10 18:45:23 +00001129 assert(!TargetRegisterInfo::isPhysicalRegister(HI->reg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 prt_->addRegUse(vrm_->getPhys(HI->reg));
1131 }
1132 }
1133
Evan Cheng29b4cf62009-04-20 08:01:12 +00001134 // Merge added with unhandled.
1135 // This also update the NextReloadMap. That is, it adds mapping from a
1136 // register defined by a reload from SS to the next reload from SS in the
1137 // same basic block.
1138 MachineBasicBlock *LastReloadMBB = 0;
1139 LiveInterval *LastReload = 0;
1140 int LastReloadSS = VirtRegMap::NO_STACK_SLOT;
1141 std::sort(added.begin(), added.end(), LISorter());
1142 for (unsigned i = 0, e = added.size(); i != e; ++i) {
1143 LiveInterval *ReloadLi = added[i];
1144 if (ReloadLi->weight == HUGE_VALF &&
1145 li_->getApproximateInstructionCount(*ReloadLi) == 0) {
1146 unsigned ReloadIdx = ReloadLi->beginNumber();
1147 MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx);
1148 int ReloadSS = vrm_->getStackSlot(ReloadLi->reg);
1149 if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) {
1150 // Last reload of same SS is in the same MBB. We want to try to
1151 // allocate both reloads the same register and make sure the reg
1152 // isn't clobbered in between if at all possible.
1153 assert(LastReload->beginNumber() < ReloadIdx);
1154 NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg));
1155 }
1156 LastReloadMBB = ReloadMBB;
1157 LastReload = ReloadLi;
1158 LastReloadSS = ReloadSS;
1159 }
1160 unhandled_.push(ReloadLi);
1161 }
1162}
1163
1164unsigned RALinScan::getFreePhysReg(const TargetRegisterClass *RC,
1165 unsigned MaxInactiveCount,
1166 SmallVector<unsigned, 256> &inactiveCounts,
1167 bool SkipDGRegs) {
1168 unsigned FreeReg = 0;
1169 unsigned FreeRegInactiveCount = 0;
1170
1171 TargetRegisterClass::iterator I = RC->allocation_order_begin(*mf_);
1172 TargetRegisterClass::iterator E = RC->allocation_order_end(*mf_);
1173 assert(I != E && "No allocatable register in this register class!");
1174
1175 // Scan for the first available register.
1176 for (; I != E; ++I) {
1177 unsigned Reg = *I;
1178 // Ignore "downgraded" registers.
1179 if (SkipDGRegs && DowngradedRegs.count(Reg))
1180 continue;
1181 if (prt_->isRegAvail(Reg)) {
1182 FreeReg = Reg;
1183 if (FreeReg < inactiveCounts.size())
1184 FreeRegInactiveCount = inactiveCounts[FreeReg];
1185 else
1186 FreeRegInactiveCount = 0;
1187 break;
1188 }
1189 }
1190
1191 // If there are no free regs, or if this reg has the max inactive count,
1192 // return this register.
1193 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount)
1194 return FreeReg;
1195
1196 // Continue scanning the registers, looking for the one with the highest
1197 // inactive count. Alkis found that this reduced register pressure very
1198 // slightly on X86 (in rev 1.94 of this file), though this should probably be
1199 // reevaluated now.
1200 for (; I != E; ++I) {
1201 unsigned Reg = *I;
1202 // Ignore "downgraded" registers.
1203 if (SkipDGRegs && DowngradedRegs.count(Reg))
1204 continue;
1205 if (prt_->isRegAvail(Reg) && Reg < inactiveCounts.size() &&
1206 FreeRegInactiveCount < inactiveCounts[Reg]) {
1207 FreeReg = Reg;
1208 FreeRegInactiveCount = inactiveCounts[Reg];
1209 if (FreeRegInactiveCount == MaxInactiveCount)
1210 break; // We found the one with the max inactive count.
1211 }
1212 }
1213
1214 return FreeReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001215}
1216
1217/// getFreePhysReg - return a free physical register for this virtual register
1218/// interval if we have one, otherwise return 0.
1219unsigned RALinScan::getFreePhysReg(LiveInterval *cur) {
Chris Lattner9f6dc2c2008-02-26 22:08:41 +00001220 SmallVector<unsigned, 256> inactiveCounts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001221 unsigned MaxInactiveCount = 0;
1222
Evan Cheng06b74c52008-09-18 22:38:47 +00001223 const TargetRegisterClass *RC = mri_->getRegClass(cur->reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001224 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
1225
1226 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
1227 i != e; ++i) {
1228 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +00001229 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001230 "Can only allocate virtual registers!");
1231
1232 // If this is not in a related reg class to the register we're allocating,
1233 // don't check it.
Evan Cheng06b74c52008-09-18 22:38:47 +00001234 const TargetRegisterClass *RegRC = mri_->getRegClass(reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001235 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
1236 reg = vrm_->getPhys(reg);
Chris Lattner9f6dc2c2008-02-26 22:08:41 +00001237 if (inactiveCounts.size() <= reg)
1238 inactiveCounts.resize(reg+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001239 ++inactiveCounts[reg];
1240 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
1241 }
1242 }
1243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 // If copy coalescer has assigned a "preferred" register, check if it's
Dale Johannesen94464072008-09-24 01:07:17 +00001245 // available first.
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +00001246 if (cur->preference) {
Dale Johannesend9e4fd62008-09-20 02:03:04 +00001247 if (prt_->isRegAvail(cur->preference) &&
Dale Johannesen94464072008-09-24 01:07:17 +00001248 RC->contains(cur->preference)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001249 DOUT << "\t\tassigned the preferred register: "
Bill Wendling9b0baeb2008-02-26 21:47:57 +00001250 << tri_->getName(cur->preference) << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001251 return cur->preference;
1252 } else
1253 DOUT << "\t\tunable to assign the preferred register: "
Bill Wendling9b0baeb2008-02-26 21:47:57 +00001254 << tri_->getName(cur->preference) << "\n";
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +00001255 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001256
Evan Cheng29b4cf62009-04-20 08:01:12 +00001257 if (!DowngradedRegs.empty()) {
1258 unsigned FreeReg = getFreePhysReg(RC, MaxInactiveCount, inactiveCounts,
1259 true);
1260 if (FreeReg)
1261 return FreeReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262 }
Evan Cheng29b4cf62009-04-20 08:01:12 +00001263 return getFreePhysReg(RC, MaxInactiveCount, inactiveCounts, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264}
1265
1266FunctionPass* llvm::createLinearScanRegisterAllocator() {
1267 return new RALinScan();
1268}