blob: f4f994c9e73d1bc74d4cda881b16ea951bc74162 [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 "llvm/CodeGen/LiveIntervalAnalysis.h"
16#include "PhysRegTracker.h"
17#include "VirtRegMap.h"
18#include "llvm/Function.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng26d17df2007-12-11 02:09:15 +000021#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/Passes.h"
24#include "llvm/CodeGen/RegAllocRegistry.h"
David Greene1d80f1b2007-09-06 16:18:45 +000025#include "llvm/CodeGen/RegisterCoalescer.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000026#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Target/TargetMachine.h"
Evan Chengc4c75f52007-11-03 07:20:12 +000028#include "llvm/Target/TargetInstrInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/ADT/EquivalenceClasses.h"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/ADT/STLExtras.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/Compiler.h"
34#include <algorithm>
35#include <set>
36#include <queue>
37#include <memory>
38#include <cmath>
39using namespace llvm;
40
41STATISTIC(NumIters , "Number of iterations performed");
42STATISTIC(NumBacktracks, "Number of times we had to backtrack");
Evan Chengc4c75f52007-11-03 07:20:12 +000043STATISTIC(NumCoalesce, "Number of copies coalesced");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
45static RegisterRegAlloc
46linearscanRegAlloc("linearscan", " linear scan register allocator",
47 createLinearScanRegisterAllocator);
48
49namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 struct VISIBILITY_HIDDEN RALinScan : public MachineFunctionPass {
51 static char ID;
52 RALinScan() : MachineFunctionPass((intptr_t)&ID) {}
53
54 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
55 typedef std::vector<IntervalPtr> IntervalPtrs;
56 private:
57 /// RelatedRegClasses - This structure is built the first time a function is
58 /// compiled, and keeps track of which register classes have registers that
59 /// belong to multiple classes or have aliases that are in other classes.
60 EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
61 std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
62
63 MachineFunction* mf_;
64 const TargetMachine* tm_;
Dan Gohman1e57df32008-02-10 18:45:23 +000065 const TargetRegisterInfo* tri_;
Evan Chengc4c75f52007-11-03 07:20:12 +000066 const TargetInstrInfo* tii_;
Chris Lattner1b989192007-12-31 04:13:23 +000067 MachineRegisterInfo *reginfo_;
Evan Chengc4c75f52007-11-03 07:20:12 +000068 BitVector allocatableRegs_;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 LiveIntervals* li_;
Evan Cheng26d17df2007-12-11 02:09:15 +000070 const MachineLoopInfo *loopInfo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071
72 /// handled_ - Intervals are added to the handled_ set in the order of their
73 /// start value. This is uses for backtracking.
74 std::vector<LiveInterval*> handled_;
75
76 /// fixed_ - Intervals that correspond to machine registers.
77 ///
78 IntervalPtrs fixed_;
79
80 /// active_ - Intervals that are currently being processed, and which have a
81 /// live range active for the current point.
82 IntervalPtrs active_;
83
84 /// inactive_ - Intervals that are currently being processed, but which have
85 /// a hold at the current point.
86 IntervalPtrs inactive_;
87
88 typedef std::priority_queue<LiveInterval*,
89 std::vector<LiveInterval*>,
90 greater_ptr<LiveInterval> > IntervalHeap;
91 IntervalHeap unhandled_;
92 std::auto_ptr<PhysRegTracker> prt_;
93 std::auto_ptr<VirtRegMap> vrm_;
94 std::auto_ptr<Spiller> spiller_;
95
96 public:
97 virtual const char* getPassName() const {
98 return "Linear Scan Register Allocator";
99 }
100
101 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
102 AU.addRequired<LiveIntervals>();
David Greene1d80f1b2007-09-06 16:18:45 +0000103 // Make sure PassManager knows which analyses to make available
104 // to coalescing and which analyses coalescing invalidates.
105 AU.addRequiredTransitive<RegisterCoalescer>();
Evan Cheng26d17df2007-12-11 02:09:15 +0000106 AU.addRequired<MachineLoopInfo>();
Bill Wendling62264362008-01-04 20:54:55 +0000107 AU.addPreserved<MachineLoopInfo>();
108 AU.addPreservedID(MachineDominatorsID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 MachineFunctionPass::getAnalysisUsage(AU);
110 }
111
112 /// runOnMachineFunction - register allocate the whole function
113 bool runOnMachineFunction(MachineFunction&);
114
115 private:
116 /// linearScan - the linear scan algorithm
117 void linearScan();
118
119 /// initIntervalSets - initialize the interval sets.
120 ///
121 void initIntervalSets();
122
123 /// processActiveIntervals - expire old intervals and move non-overlapping
124 /// ones to the inactive list.
125 void processActiveIntervals(unsigned CurPoint);
126
127 /// processInactiveIntervals - expire old intervals and move overlapping
128 /// ones to the active list.
129 void processInactiveIntervals(unsigned CurPoint);
130
131 /// assignRegOrStackSlotAtInterval - assign a register if one
132 /// is available, or spill.
133 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
134
Evan Chengc4c75f52007-11-03 07:20:12 +0000135 /// attemptTrivialCoalescing - If a simple interval is defined by a copy,
136 /// try allocate the definition the same register as the source register
137 /// if the register is not defined during live time of the interval. This
138 /// eliminate a copy. This is used to coalesce copies which were not
139 /// coalesced away before allocation either due to dest and src being in
140 /// different register classes or because the coalescer was overly
141 /// conservative.
142 unsigned attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg);
143
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 ///
145 /// register handling helpers
146 ///
147
148 /// getFreePhysReg - return a free physical register for this virtual
149 /// register interval if we have one, otherwise return 0.
150 unsigned getFreePhysReg(LiveInterval* cur);
151
152 /// assignVirt2StackSlot - assigns this virtual register to a
153 /// stack slot. returns the stack slot
154 int assignVirt2StackSlot(unsigned virtReg);
155
156 void ComputeRelatedRegClasses();
157
158 template <typename ItTy>
159 void printIntervals(const char* const str, ItTy i, ItTy e) const {
160 if (str) DOUT << str << " intervals:\n";
161 for (; i != e; ++i) {
162 DOUT << "\t" << *i->first << " -> ";
163 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000164 if (TargetRegisterInfo::isVirtualRegister(reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 reg = vrm_->getPhys(reg);
166 }
Dan Gohman1e57df32008-02-10 18:45:23 +0000167 DOUT << tri_->getName(reg) << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 }
169 }
170 };
171 char RALinScan::ID = 0;
172}
173
174void RALinScan::ComputeRelatedRegClasses() {
Dan Gohman1e57df32008-02-10 18:45:23 +0000175 const TargetRegisterInfo &TRI = *tri_;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176
177 // First pass, add all reg classes to the union, and determine at least one
178 // reg class that each register is in.
179 bool HasAliases = false;
Dan Gohman1e57df32008-02-10 18:45:23 +0000180 for (TargetRegisterInfo::regclass_iterator RCI = TRI.regclass_begin(),
181 E = TRI.regclass_end(); RCI != E; ++RCI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 RelatedRegClasses.insert(*RCI);
183 for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
184 I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000185 HasAliases = HasAliases || *TRI.getAliasSet(*I) != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
187 const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
188 if (PRC) {
189 // Already processed this register. Just make sure we know that
190 // multiple register classes share a register.
191 RelatedRegClasses.unionSets(PRC, *RCI);
192 } else {
193 PRC = *RCI;
194 }
195 }
196 }
197
198 // Second pass, now that we know conservatively what register classes each reg
199 // belongs to, add info about aliases. We don't need to do this for targets
200 // without register aliases.
201 if (HasAliases)
202 for (std::map<unsigned, const TargetRegisterClass*>::iterator
203 I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
204 I != E; ++I)
Dan Gohman1e57df32008-02-10 18:45:23 +0000205 for (const unsigned *AS = TRI.getAliasSet(I->first); *AS; ++AS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
207}
208
Evan Chengc4c75f52007-11-03 07:20:12 +0000209/// attemptTrivialCoalescing - If a simple interval is defined by a copy,
210/// try allocate the definition the same register as the source register
211/// if the register is not defined during live time of the interval. This
212/// eliminate a copy. This is used to coalesce copies which were not
213/// coalesced away before allocation either due to dest and src being in
214/// different register classes or because the coalescer was overly
215/// conservative.
216unsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) {
Evan Chengb6aa6712007-11-04 08:32:21 +0000217 if ((cur.preference && cur.preference == Reg) || !cur.containsOneValue())
Evan Chengc4c75f52007-11-03 07:20:12 +0000218 return Reg;
219
220 VNInfo *vni = cur.getValNumInfo(0);
221 if (!vni->def || vni->def == ~1U || vni->def == ~0U)
222 return Reg;
223 MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
224 unsigned SrcReg, DstReg;
225 if (!CopyMI || !tii_->isMoveInstr(*CopyMI, SrcReg, DstReg))
226 return Reg;
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +0000227 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Chengc4c75f52007-11-03 07:20:12 +0000228 if (!vrm_->isAssignedReg(SrcReg))
229 return Reg;
230 else
231 SrcReg = vrm_->getPhys(SrcReg);
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +0000232 }
Evan Chengc4c75f52007-11-03 07:20:12 +0000233 if (Reg == SrcReg)
234 return Reg;
235
Chris Lattner1b989192007-12-31 04:13:23 +0000236 const TargetRegisterClass *RC = reginfo_->getRegClass(cur.reg);
Evan Chengc4c75f52007-11-03 07:20:12 +0000237 if (!RC->contains(SrcReg))
238 return Reg;
239
240 // Try to coalesce.
241 if (!li_->conflictsWithPhysRegDef(cur, *vrm_, SrcReg)) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000242 DOUT << "Coalescing: " << cur << " -> " << tri_->getName(SrcReg) << '\n';
Evan Chengc4c75f52007-11-03 07:20:12 +0000243 vrm_->clearVirt(cur.reg);
244 vrm_->assignVirt2Phys(cur.reg, SrcReg);
245 ++NumCoalesce;
246 return SrcReg;
247 }
248
249 return Reg;
250}
251
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252bool RALinScan::runOnMachineFunction(MachineFunction &fn) {
253 mf_ = &fn;
254 tm_ = &fn.getTarget();
Dan Gohman1e57df32008-02-10 18:45:23 +0000255 tri_ = tm_->getRegisterInfo();
Evan Chengc4c75f52007-11-03 07:20:12 +0000256 tii_ = tm_->getInstrInfo();
Chris Lattner1b989192007-12-31 04:13:23 +0000257 reginfo_ = &mf_->getRegInfo();
Dan Gohman1e57df32008-02-10 18:45:23 +0000258 allocatableRegs_ = tri_->getAllocatableSet(fn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 li_ = &getAnalysis<LiveIntervals>();
Evan Cheng26d17df2007-12-11 02:09:15 +0000260 loopInfo = &getAnalysis<MachineLoopInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261
David Greene1d80f1b2007-09-06 16:18:45 +0000262 // We don't run the coalescer here because we have no reason to
263 // interact with it. If the coalescer requires interaction, it
264 // won't do anything. If it doesn't require interaction, we assume
265 // it was run as a separate pass.
266
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 // If this is the first function compiled, compute the related reg classes.
268 if (RelatedRegClasses.empty())
269 ComputeRelatedRegClasses();
270
Dan Gohman1e57df32008-02-10 18:45:23 +0000271 if (!prt_.get()) prt_.reset(new PhysRegTracker(*tri_));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 vrm_.reset(new VirtRegMap(*mf_));
273 if (!spiller_.get()) spiller_.reset(createSpiller());
274
275 initIntervalSets();
276
277 linearScan();
278
279 // Rewrite spill code and update the PhysRegsUsed set.
280 spiller_->runOnMachineFunction(*mf_, *vrm_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 vrm_.reset(); // Free the VirtRegMap
282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 while (!unhandled_.empty()) unhandled_.pop();
284 fixed_.clear();
285 active_.clear();
286 inactive_.clear();
287 handled_.clear();
288
289 return true;
290}
291
292/// initIntervalSets - initialize the interval sets.
293///
294void RALinScan::initIntervalSets()
295{
296 assert(unhandled_.empty() && fixed_.empty() &&
297 active_.empty() && inactive_.empty() &&
298 "interval sets should be empty on initialization");
299
300 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000301 if (TargetRegisterInfo::isPhysicalRegister(i->second.reg)) {
Chris Lattner1b989192007-12-31 04:13:23 +0000302 reginfo_->setPhysRegUsed(i->second.reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
304 } else
305 unhandled_.push(&i->second);
306 }
307}
308
309void RALinScan::linearScan()
310{
311 // linear scan algorithm
312 DOUT << "********** LINEAR SCAN **********\n";
313 DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n';
314
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316
317 while (!unhandled_.empty()) {
318 // pick the interval with the earliest start point
319 LiveInterval* cur = unhandled_.top();
320 unhandled_.pop();
Evan Chengd48f2bc2007-10-16 21:09:14 +0000321 ++NumIters;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 DOUT << "\n*** CURRENT ***: " << *cur << '\n';
323
324 processActiveIntervals(cur->beginNumber());
325 processInactiveIntervals(cur->beginNumber());
326
Dan Gohman1e57df32008-02-10 18:45:23 +0000327 assert(TargetRegisterInfo::isVirtualRegister(cur->reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 "Can only allocate virtual registers!");
329
330 // Allocating a virtual register. try to find a free
331 // physical register or spill an interval (possibly this one) in order to
332 // assign it one.
333 assignRegOrStackSlotAtInterval(cur);
334
335 DEBUG(printIntervals("active", active_.begin(), active_.end()));
336 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
337 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338
339 // expire any remaining active intervals
Evan Chengd48f2bc2007-10-16 21:09:14 +0000340 while (!active_.empty()) {
341 IntervalPtr &IP = active_.back();
342 unsigned reg = IP.first->reg;
343 DOUT << "\tinterval " << *IP.first << " expired\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000344 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 "Can only allocate virtual registers!");
346 reg = vrm_->getPhys(reg);
347 prt_->delRegUse(reg);
Evan Chengd48f2bc2007-10-16 21:09:14 +0000348 active_.pop_back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 }
350
351 // expire any remaining inactive intervals
Evan Chengd48f2bc2007-10-16 21:09:14 +0000352 DEBUG(for (IntervalPtrs::reverse_iterator
Bill Wendling1817ab82007-11-15 00:40:48 +0000353 i = inactive_.rbegin(); i != inactive_.rend(); ++i)
Evan Chengd48f2bc2007-10-16 21:09:14 +0000354 DOUT << "\tinterval " << *i->first << " expired\n");
355 inactive_.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356
Evan Chengcecc8222007-11-17 00:40:40 +0000357 // Add live-ins to every BB except for entry. Also perform trivial coalescing.
Evan Chengf5cdf122007-10-17 02:12:22 +0000358 MachineFunction::iterator EntryMBB = mf_->begin();
Evan Cheng12d6fcb2007-10-17 06:53:44 +0000359 SmallVector<MachineBasicBlock*, 8> LiveInMBBs;
Evan Chengf5cdf122007-10-17 02:12:22 +0000360 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Evan Chengc4c75f52007-11-03 07:20:12 +0000361 LiveInterval &cur = i->second;
Evan Chengf5cdf122007-10-17 02:12:22 +0000362 unsigned Reg = 0;
Dan Gohman1e57df32008-02-10 18:45:23 +0000363 bool isPhys = TargetRegisterInfo::isPhysicalRegister(cur.reg);
Evan Chengcecc8222007-11-17 00:40:40 +0000364 if (isPhys)
Evan Chengf5cdf122007-10-17 02:12:22 +0000365 Reg = i->second.reg;
366 else if (vrm_->isAssignedReg(cur.reg))
Evan Chengc4c75f52007-11-03 07:20:12 +0000367 Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg));
Evan Chengf5cdf122007-10-17 02:12:22 +0000368 if (!Reg)
369 continue;
Evan Chengcecc8222007-11-17 00:40:40 +0000370 // Ignore splited live intervals.
371 if (!isPhys && vrm_->getPreSplitReg(cur.reg))
372 continue;
Evan Chengf5cdf122007-10-17 02:12:22 +0000373 for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end();
374 I != E; ++I) {
375 const LiveRange &LR = *I;
Evan Chengf5cdf122007-10-17 02:12:22 +0000376 if (li_->findLiveInMBBs(LR, LiveInMBBs)) {
377 for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i)
378 if (LiveInMBBs[i] != EntryMBB)
379 LiveInMBBs[i]->addLiveIn(Reg);
Evan Cheng12d6fcb2007-10-17 06:53:44 +0000380 LiveInMBBs.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 }
382 }
383 }
384
385 DOUT << *vrm_;
386}
387
388/// processActiveIntervals - expire old intervals and move non-overlapping ones
389/// to the inactive list.
390void RALinScan::processActiveIntervals(unsigned CurPoint)
391{
392 DOUT << "\tprocessing active intervals:\n";
393
394 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
395 LiveInterval *Interval = active_[i].first;
396 LiveInterval::iterator IntervalPos = active_[i].second;
397 unsigned reg = Interval->reg;
398
399 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
400
401 if (IntervalPos == Interval->end()) { // Remove expired intervals.
402 DOUT << "\t\tinterval " << *Interval << " expired\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000403 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 "Can only allocate virtual registers!");
405 reg = vrm_->getPhys(reg);
406 prt_->delRegUse(reg);
407
408 // Pop off the end of the list.
409 active_[i] = active_.back();
410 active_.pop_back();
411 --i; --e;
412
413 } else if (IntervalPos->start > CurPoint) {
414 // Move inactive intervals to inactive list.
415 DOUT << "\t\tinterval " << *Interval << " inactive\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000416 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 "Can only allocate virtual registers!");
418 reg = vrm_->getPhys(reg);
419 prt_->delRegUse(reg);
420 // add to inactive.
421 inactive_.push_back(std::make_pair(Interval, IntervalPos));
422
423 // Pop off the end of the list.
424 active_[i] = active_.back();
425 active_.pop_back();
426 --i; --e;
427 } else {
428 // Otherwise, just update the iterator position.
429 active_[i].second = IntervalPos;
430 }
431 }
432}
433
434/// processInactiveIntervals - expire old intervals and move overlapping
435/// ones to the active list.
436void RALinScan::processInactiveIntervals(unsigned CurPoint)
437{
438 DOUT << "\tprocessing inactive intervals:\n";
439
440 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
441 LiveInterval *Interval = inactive_[i].first;
442 LiveInterval::iterator IntervalPos = inactive_[i].second;
443 unsigned reg = Interval->reg;
444
445 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
446
447 if (IntervalPos == Interval->end()) { // remove expired intervals.
448 DOUT << "\t\tinterval " << *Interval << " expired\n";
449
450 // Pop off the end of the list.
451 inactive_[i] = inactive_.back();
452 inactive_.pop_back();
453 --i; --e;
454 } else if (IntervalPos->start <= CurPoint) {
455 // move re-activated intervals in active list
456 DOUT << "\t\tinterval " << *Interval << " active\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000457 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 "Can only allocate virtual registers!");
459 reg = vrm_->getPhys(reg);
460 prt_->addRegUse(reg);
461 // add to active
462 active_.push_back(std::make_pair(Interval, IntervalPos));
463
464 // Pop off the end of the list.
465 inactive_[i] = inactive_.back();
466 inactive_.pop_back();
467 --i; --e;
468 } else {
469 // Otherwise, just update the iterator position.
470 inactive_[i].second = IntervalPos;
471 }
472 }
473}
474
475/// updateSpillWeights - updates the spill weights of the specifed physical
476/// register and its weight.
477static void updateSpillWeights(std::vector<float> &Weights,
478 unsigned reg, float weight,
Dan Gohman1e57df32008-02-10 18:45:23 +0000479 const TargetRegisterInfo *TRI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 Weights[reg] += weight;
Dan Gohman1e57df32008-02-10 18:45:23 +0000481 for (const unsigned* as = TRI->getAliasSet(reg); *as; ++as)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 Weights[*as] += weight;
483}
484
485static
486RALinScan::IntervalPtrs::iterator
487FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) {
488 for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end();
489 I != E; ++I)
490 if (I->first == LI) return I;
491 return IP.end();
492}
493
494static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V, unsigned Point){
495 for (unsigned i = 0, e = V.size(); i != e; ++i) {
496 RALinScan::IntervalPtr &IP = V[i];
497 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
498 IP.second, Point);
499 if (I != IP.first->begin()) --I;
500 IP.second = I;
501 }
502}
503
504/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
505/// spill.
506void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur)
507{
508 DOUT << "\tallocating current interval: ";
509
510 PhysRegTracker backupPrt = *prt_;
511
512 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
513 unsigned StartPosition = cur->beginNumber();
Chris Lattner1b989192007-12-31 04:13:23 +0000514 const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
Evan Chengc4c75f52007-11-03 07:20:12 +0000516
517 // If this live interval is defined by a move instruction and its source is
518 // assigned a physical register that is compatible with the target register
519 // class, then we should try to assign it the same register.
520 // This can happen when the move is from a larger register class to a smaller
521 // one, e.g. X86::mov32to32_. These move instructions are not coalescable.
522 if (!cur->preference && cur->containsOneValue()) {
523 VNInfo *vni = cur->getValNumInfo(0);
524 if (vni->def && vni->def != ~1U && vni->def != ~0U) {
525 MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
526 unsigned SrcReg, DstReg;
527 if (tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) {
528 unsigned Reg = 0;
Dan Gohman1e57df32008-02-10 18:45:23 +0000529 if (TargetRegisterInfo::isPhysicalRegister(SrcReg))
Evan Chengc4c75f52007-11-03 07:20:12 +0000530 Reg = SrcReg;
531 else if (vrm_->isAssignedReg(SrcReg))
532 Reg = vrm_->getPhys(SrcReg);
533 if (Reg && allocatableRegs_[Reg] && RC->contains(Reg))
534 cur->preference = Reg;
535 }
536 }
537 }
538
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 // for every interval in inactive we overlap with, mark the
540 // register as not free and update spill weights.
541 for (IntervalPtrs::const_iterator i = inactive_.begin(),
542 e = inactive_.end(); i != e; ++i) {
543 unsigned Reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000544 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 "Can only allocate virtual registers!");
Chris Lattner1b989192007-12-31 04:13:23 +0000546 const TargetRegisterClass *RegRC = reginfo_->getRegClass(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 // If this is not in a related reg class to the register we're allocating,
548 // don't check it.
549 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
550 cur->overlapsFrom(*i->first, i->second-1)) {
551 Reg = vrm_->getPhys(Reg);
552 prt_->addRegUse(Reg);
553 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
554 }
555 }
556
557 // Speculatively check to see if we can get a register right now. If not,
558 // we know we won't be able to by adding more constraints. If so, we can
559 // check to see if it is valid. Doing an exhaustive search of the fixed_ list
560 // is very bad (it contains all callee clobbered registers for any functions
561 // with a call), so we want to avoid doing that if possible.
562 unsigned physReg = getFreePhysReg(cur);
563 if (physReg) {
564 // We got a register. However, if it's in the fixed_ list, we might
565 // conflict with it. Check to see if we conflict with it or any of its
566 // aliases.
Evan Chengc4c75f52007-11-03 07:20:12 +0000567 SmallSet<unsigned, 8> RegAliases;
Dan Gohman1e57df32008-02-10 18:45:23 +0000568 for (const unsigned *AS = tri_->getAliasSet(physReg); *AS; ++AS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 RegAliases.insert(*AS);
570
571 bool ConflictsWithFixed = false;
572 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
573 IntervalPtr &IP = fixed_[i];
574 if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) {
575 // Okay, this reg is on the fixed list. Check to see if we actually
576 // conflict.
577 LiveInterval *I = IP.first;
578 if (I->endNumber() > StartPosition) {
579 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
580 IP.second = II;
581 if (II != I->begin() && II->start > StartPosition)
582 --II;
583 if (cur->overlapsFrom(*I, II)) {
584 ConflictsWithFixed = true;
585 break;
586 }
587 }
588 }
589 }
590
591 // Okay, the register picked by our speculative getFreePhysReg call turned
592 // out to be in use. Actually add all of the conflicting fixed registers to
593 // prt so we can do an accurate query.
594 if (ConflictsWithFixed) {
595 // For every interval in fixed we overlap with, mark the register as not
596 // free and update spill weights.
597 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
598 IntervalPtr &IP = fixed_[i];
599 LiveInterval *I = IP.first;
600
601 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
602 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
603 I->endNumber() > StartPosition) {
604 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
605 IP.second = II;
606 if (II != I->begin() && II->start > StartPosition)
607 --II;
608 if (cur->overlapsFrom(*I, II)) {
609 unsigned reg = I->reg;
610 prt_->addRegUse(reg);
611 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
612 }
613 }
614 }
615
616 // Using the newly updated prt_ object, which includes conflicts in the
617 // future, see if there are any registers available.
618 physReg = getFreePhysReg(cur);
619 }
620 }
621
622 // Restore the physical register tracker, removing information about the
623 // future.
624 *prt_ = backupPrt;
625
626 // if we find a free register, we are done: assign this virtual to
627 // the free physical register and add this interval to the active
628 // list.
629 if (physReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000630 DOUT << tri_->getName(physReg) << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 vrm_->assignVirt2Phys(cur->reg, physReg);
632 prt_->addRegUse(physReg);
633 active_.push_back(std::make_pair(cur, cur->begin()));
634 handled_.push_back(cur);
635 return;
636 }
637 DOUT << "no free registers\n";
638
639 // Compile the spill weights into an array that is better for scanning.
Dan Gohman1e57df32008-02-10 18:45:23 +0000640 std::vector<float> SpillWeights(tri_->getNumRegs(), 0.0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 for (std::vector<std::pair<unsigned, float> >::iterator
642 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
Dan Gohman1e57df32008-02-10 18:45:23 +0000643 updateSpillWeights(SpillWeights, I->first, I->second, tri_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644
645 // for each interval in active, update spill weights.
646 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
647 i != e; ++i) {
648 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000649 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 "Can only allocate virtual registers!");
651 reg = vrm_->getPhys(reg);
Dan Gohman1e57df32008-02-10 18:45:23 +0000652 updateSpillWeights(SpillWeights, reg, i->first->weight, tri_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 }
654
655 DOUT << "\tassigning stack slot at interval "<< *cur << ":\n";
656
657 // Find a register to spill.
658 float minWeight = HUGE_VALF;
659 unsigned minReg = cur->preference; // Try the preferred register first.
660
661 if (!minReg || SpillWeights[minReg] == HUGE_VALF)
662 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
663 e = RC->allocation_order_end(*mf_); i != e; ++i) {
664 unsigned reg = *i;
665 if (minWeight > SpillWeights[reg]) {
666 minWeight = SpillWeights[reg];
667 minReg = reg;
668 }
669 }
670
671 // If we didn't find a register that is spillable, try aliases?
672 if (!minReg) {
673 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
674 e = RC->allocation_order_end(*mf_); i != e; ++i) {
675 unsigned reg = *i;
676 // No need to worry about if the alias register size < regsize of RC.
677 // We are going to spill all registers that alias it anyway.
Dan Gohman1e57df32008-02-10 18:45:23 +0000678 for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679 if (minWeight > SpillWeights[*as]) {
680 minWeight = SpillWeights[*as];
681 minReg = *as;
682 }
683 }
684 }
685
686 // All registers must have inf weight. Just grab one!
687 if (!minReg)
688 minReg = *RC->allocation_order_begin(*mf_);
689 }
690
691 DOUT << "\t\tregister with min weight: "
Dan Gohman1e57df32008-02-10 18:45:23 +0000692 << tri_->getName(minReg) << " (" << minWeight << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693
694 // if the current has the minimum weight, we need to spill it and
695 // add any added intervals back to unhandled, and restart
696 // linearscan.
697 if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
698 DOUT << "\t\t\tspilling(c): " << *cur << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 std::vector<LiveInterval*> added =
Evan Chengcecc8222007-11-17 00:40:40 +0000700 li_->addIntervalsForSpills(*cur, loopInfo, *vrm_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 if (added.empty())
702 return; // Early exit if all spills were folded.
703
704 // Merge added with unhandled. Note that we know that
705 // addIntervalsForSpills returns intervals sorted by their starting
706 // point.
707 for (unsigned i = 0, e = added.size(); i != e; ++i)
708 unhandled_.push(added[i]);
709 return;
710 }
711
712 ++NumBacktracks;
713
714 // push the current interval back to unhandled since we are going
715 // to re-run at least this iteration. Since we didn't modify it it
716 // should go back right in the front of the list
717 unhandled_.push(cur);
718
719 // otherwise we spill all intervals aliasing the register with
720 // minimum weight, rollback to the interval with the earliest
721 // start point and let the linear scan algorithm run again
722 std::vector<LiveInterval*> added;
Dan Gohman1e57df32008-02-10 18:45:23 +0000723 assert(TargetRegisterInfo::isPhysicalRegister(minReg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 "did not choose a register to spill?");
Dan Gohman1e57df32008-02-10 18:45:23 +0000725 BitVector toSpill(tri_->getNumRegs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726
727 // We are going to spill minReg and all its aliases.
728 toSpill[minReg] = true;
Dan Gohman1e57df32008-02-10 18:45:23 +0000729 for (const unsigned* as = tri_->getAliasSet(minReg); *as; ++as)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 toSpill[*as] = true;
731
732 // the earliest start of a spilled interval indicates up to where
733 // in handled we need to roll back
734 unsigned earliestStart = cur->beginNumber();
735
736 // set of spilled vregs (used later to rollback properly)
Evan Chengc4c75f52007-11-03 07:20:12 +0000737 SmallSet<unsigned, 32> spilled;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738
739 // spill live intervals of virtual regs mapped to the physical register we
740 // want to clear (and its aliases). We only spill those that overlap with the
741 // current interval as the rest do not affect its allocation. we also keep
742 // track of the earliest start of all spilled live intervals since this will
743 // mark our rollback point.
744 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
745 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000746 if (//TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747 toSpill[vrm_->getPhys(reg)] &&
748 cur->overlapsFrom(*i->first, i->second)) {
749 DOUT << "\t\t\tspilling(a): " << *i->first << '\n';
750 earliestStart = std::min(earliestStart, i->first->beginNumber());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751 std::vector<LiveInterval*> newIs =
Evan Chengcecc8222007-11-17 00:40:40 +0000752 li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
754 spilled.insert(reg);
755 }
756 }
757 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
758 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000759 if (//TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 toSpill[vrm_->getPhys(reg)] &&
761 cur->overlapsFrom(*i->first, i->second-1)) {
762 DOUT << "\t\t\tspilling(i): " << *i->first << '\n';
763 earliestStart = std::min(earliestStart, i->first->beginNumber());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 std::vector<LiveInterval*> newIs =
Evan Chengcecc8222007-11-17 00:40:40 +0000765 li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
767 spilled.insert(reg);
768 }
769 }
770
771 DOUT << "\t\trolling back to: " << earliestStart << '\n';
772
773 // Scan handled in reverse order up to the earliest start of a
774 // spilled live interval and undo each one, restoring the state of
775 // unhandled.
776 while (!handled_.empty()) {
777 LiveInterval* i = handled_.back();
778 // If this interval starts before t we are done.
779 if (i->beginNumber() < earliestStart)
780 break;
781 DOUT << "\t\t\tundo changes for: " << *i << '\n';
782 handled_.pop_back();
783
784 // When undoing a live interval allocation we must know if it is active or
785 // inactive to properly update the PhysRegTracker and the VirtRegMap.
786 IntervalPtrs::iterator it;
787 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
788 active_.erase(it);
Dan Gohman1e57df32008-02-10 18:45:23 +0000789 assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790 if (!spilled.count(i->reg))
791 unhandled_.push(i);
792 prt_->delRegUse(vrm_->getPhys(i->reg));
793 vrm_->clearVirt(i->reg);
794 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
795 inactive_.erase(it);
Dan Gohman1e57df32008-02-10 18:45:23 +0000796 assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 if (!spilled.count(i->reg))
798 unhandled_.push(i);
799 vrm_->clearVirt(i->reg);
800 } else {
Dan Gohman1e57df32008-02-10 18:45:23 +0000801 assert(TargetRegisterInfo::isVirtualRegister(i->reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 "Can only allocate virtual registers!");
803 vrm_->clearVirt(i->reg);
804 unhandled_.push(i);
805 }
Evan Chengb6aa6712007-11-04 08:32:21 +0000806
807 // It interval has a preference, it must be defined by a copy. Clear the
808 // preference now since the source interval allocation may have been undone
809 // as well.
810 i->preference = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 }
812
813 // Rewind the iterators in the active, inactive, and fixed lists back to the
814 // point we reverted to.
815 RevertVectorIteratorsTo(active_, earliestStart);
816 RevertVectorIteratorsTo(inactive_, earliestStart);
817 RevertVectorIteratorsTo(fixed_, earliestStart);
818
819 // scan the rest and undo each interval that expired after t and
820 // insert it in active (the next iteration of the algorithm will
821 // put it in inactive if required)
822 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
823 LiveInterval *HI = handled_[i];
824 if (!HI->expiredAt(earliestStart) &&
825 HI->expiredAt(cur->beginNumber())) {
826 DOUT << "\t\t\tundo changes for: " << *HI << '\n';
827 active_.push_back(std::make_pair(HI, HI->begin()));
Dan Gohman1e57df32008-02-10 18:45:23 +0000828 assert(!TargetRegisterInfo::isPhysicalRegister(HI->reg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000829 prt_->addRegUse(vrm_->getPhys(HI->reg));
830 }
831 }
832
833 // merge added with unhandled
834 for (unsigned i = 0, e = added.size(); i != e; ++i)
835 unhandled_.push(added[i]);
836}
837
838/// getFreePhysReg - return a free physical register for this virtual register
839/// interval if we have one, otherwise return 0.
840unsigned RALinScan::getFreePhysReg(LiveInterval *cur) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000841 std::vector<unsigned> inactiveCounts(tri_->getNumRegs(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 unsigned MaxInactiveCount = 0;
843
Chris Lattner1b989192007-12-31 04:13:23 +0000844 const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
846
847 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
848 i != e; ++i) {
849 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000850 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 "Can only allocate virtual registers!");
852
853 // If this is not in a related reg class to the register we're allocating,
854 // don't check it.
Chris Lattner1b989192007-12-31 04:13:23 +0000855 const TargetRegisterClass *RegRC = reginfo_->getRegClass(reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
857 reg = vrm_->getPhys(reg);
858 ++inactiveCounts[reg];
859 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
860 }
861 }
862
863 unsigned FreeReg = 0;
864 unsigned FreeRegInactiveCount = 0;
865
866 // If copy coalescer has assigned a "preferred" register, check if it's
867 // available first.
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +0000868 if (cur->preference) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 if (prt_->isRegAvail(cur->preference)) {
870 DOUT << "\t\tassigned the preferred register: "
Dan Gohman1e57df32008-02-10 18:45:23 +0000871 << tri_->getName(cur->preference) << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 return cur->preference;
873 } else
874 DOUT << "\t\tunable to assign the preferred register: "
Dan Gohman1e57df32008-02-10 18:45:23 +0000875 << tri_->getName(cur->preference) << "\n";
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +0000876 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877
878 // Scan for the first available register.
879 TargetRegisterClass::iterator I = RC->allocation_order_begin(*mf_);
880 TargetRegisterClass::iterator E = RC->allocation_order_end(*mf_);
881 for (; I != E; ++I)
882 if (prt_->isRegAvail(*I)) {
883 FreeReg = *I;
884 FreeRegInactiveCount = inactiveCounts[FreeReg];
885 break;
886 }
887
888 // If there are no free regs, or if this reg has the max inactive count,
889 // return this register.
890 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
891
892 // Continue scanning the registers, looking for the one with the highest
893 // inactive count. Alkis found that this reduced register pressure very
894 // slightly on X86 (in rev 1.94 of this file), though this should probably be
895 // reevaluated now.
896 for (; I != E; ++I) {
897 unsigned Reg = *I;
898 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
899 FreeReg = Reg;
900 FreeRegInactiveCount = inactiveCounts[Reg];
901 if (FreeRegInactiveCount == MaxInactiveCount)
902 break; // We found the one with the max inactive count.
903 }
904 }
905
906 return FreeReg;
907}
908
909FunctionPass* llvm::createLinearScanRegisterAllocator() {
910 return new RALinScan();
911}