blob: c70ff9524328d237e0eb48f85cc097c3446c1c22 [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;
Dan Gohman1e57df32008-02-10 18:45:23 +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);
232 if (Reg == SrcReg)
233 return Reg;
234
Chris Lattner1b989192007-12-31 04:13:23 +0000235 const TargetRegisterClass *RC = reginfo_->getRegClass(cur.reg);
Evan Chengc4c75f52007-11-03 07:20:12 +0000236 if (!RC->contains(SrcReg))
237 return Reg;
238
239 // Try to coalesce.
240 if (!li_->conflictsWithPhysRegDef(cur, *vrm_, SrcReg)) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000241 DOUT << "Coalescing: " << cur << " -> " << tri_->getName(SrcReg) << '\n';
Evan Chengc4c75f52007-11-03 07:20:12 +0000242 vrm_->clearVirt(cur.reg);
243 vrm_->assignVirt2Phys(cur.reg, SrcReg);
244 ++NumCoalesce;
245 return SrcReg;
246 }
247
248 return Reg;
249}
250
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251bool RALinScan::runOnMachineFunction(MachineFunction &fn) {
252 mf_ = &fn;
253 tm_ = &fn.getTarget();
Dan Gohman1e57df32008-02-10 18:45:23 +0000254 tri_ = tm_->getRegisterInfo();
Evan Chengc4c75f52007-11-03 07:20:12 +0000255 tii_ = tm_->getInstrInfo();
Chris Lattner1b989192007-12-31 04:13:23 +0000256 reginfo_ = &mf_->getRegInfo();
Dan Gohman1e57df32008-02-10 18:45:23 +0000257 allocatableRegs_ = tri_->getAllocatableSet(fn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 li_ = &getAnalysis<LiveIntervals>();
Evan Cheng26d17df2007-12-11 02:09:15 +0000259 loopInfo = &getAnalysis<MachineLoopInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260
David Greene1d80f1b2007-09-06 16:18:45 +0000261 // We don't run the coalescer here because we have no reason to
262 // interact with it. If the coalescer requires interaction, it
263 // won't do anything. If it doesn't require interaction, we assume
264 // it was run as a separate pass.
265
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 // If this is the first function compiled, compute the related reg classes.
267 if (RelatedRegClasses.empty())
268 ComputeRelatedRegClasses();
269
Dan Gohman1e57df32008-02-10 18:45:23 +0000270 if (!prt_.get()) prt_.reset(new PhysRegTracker(*tri_));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 vrm_.reset(new VirtRegMap(*mf_));
272 if (!spiller_.get()) spiller_.reset(createSpiller());
273
274 initIntervalSets();
275
276 linearScan();
277
278 // Rewrite spill code and update the PhysRegsUsed set.
279 spiller_->runOnMachineFunction(*mf_, *vrm_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 vrm_.reset(); // Free the VirtRegMap
281
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 while (!unhandled_.empty()) unhandled_.pop();
283 fixed_.clear();
284 active_.clear();
285 inactive_.clear();
286 handled_.clear();
287
288 return true;
289}
290
291/// initIntervalSets - initialize the interval sets.
292///
293void RALinScan::initIntervalSets()
294{
295 assert(unhandled_.empty() && fixed_.empty() &&
296 active_.empty() && inactive_.empty() &&
297 "interval sets should be empty on initialization");
298
299 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000300 if (TargetRegisterInfo::isPhysicalRegister(i->second.reg)) {
Chris Lattner1b989192007-12-31 04:13:23 +0000301 reginfo_->setPhysRegUsed(i->second.reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
303 } else
304 unhandled_.push(&i->second);
305 }
306}
307
308void RALinScan::linearScan()
309{
310 // linear scan algorithm
311 DOUT << "********** LINEAR SCAN **********\n";
312 DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n';
313
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315
316 while (!unhandled_.empty()) {
317 // pick the interval with the earliest start point
318 LiveInterval* cur = unhandled_.top();
319 unhandled_.pop();
Evan Chengd48f2bc2007-10-16 21:09:14 +0000320 ++NumIters;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 DOUT << "\n*** CURRENT ***: " << *cur << '\n';
322
323 processActiveIntervals(cur->beginNumber());
324 processInactiveIntervals(cur->beginNumber());
325
Dan Gohman1e57df32008-02-10 18:45:23 +0000326 assert(TargetRegisterInfo::isVirtualRegister(cur->reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 "Can only allocate virtual registers!");
328
329 // Allocating a virtual register. try to find a free
330 // physical register or spill an interval (possibly this one) in order to
331 // assign it one.
332 assignRegOrStackSlotAtInterval(cur);
333
334 DEBUG(printIntervals("active", active_.begin(), active_.end()));
335 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
336 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337
338 // expire any remaining active intervals
Evan Chengd48f2bc2007-10-16 21:09:14 +0000339 while (!active_.empty()) {
340 IntervalPtr &IP = active_.back();
341 unsigned reg = IP.first->reg;
342 DOUT << "\tinterval " << *IP.first << " expired\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000343 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 "Can only allocate virtual registers!");
345 reg = vrm_->getPhys(reg);
346 prt_->delRegUse(reg);
Evan Chengd48f2bc2007-10-16 21:09:14 +0000347 active_.pop_back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 }
349
350 // expire any remaining inactive intervals
Evan Chengd48f2bc2007-10-16 21:09:14 +0000351 DEBUG(for (IntervalPtrs::reverse_iterator
Bill Wendling1817ab82007-11-15 00:40:48 +0000352 i = inactive_.rbegin(); i != inactive_.rend(); ++i)
Evan Chengd48f2bc2007-10-16 21:09:14 +0000353 DOUT << "\tinterval " << *i->first << " expired\n");
354 inactive_.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355
Evan Chengcecc8222007-11-17 00:40:40 +0000356 // Add live-ins to every BB except for entry. Also perform trivial coalescing.
Evan Chengf5cdf122007-10-17 02:12:22 +0000357 MachineFunction::iterator EntryMBB = mf_->begin();
Evan Cheng12d6fcb2007-10-17 06:53:44 +0000358 SmallVector<MachineBasicBlock*, 8> LiveInMBBs;
Evan Chengf5cdf122007-10-17 02:12:22 +0000359 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Evan Chengc4c75f52007-11-03 07:20:12 +0000360 LiveInterval &cur = i->second;
Evan Chengf5cdf122007-10-17 02:12:22 +0000361 unsigned Reg = 0;
Dan Gohman1e57df32008-02-10 18:45:23 +0000362 bool isPhys = TargetRegisterInfo::isPhysicalRegister(cur.reg);
Evan Chengcecc8222007-11-17 00:40:40 +0000363 if (isPhys)
Evan Chengf5cdf122007-10-17 02:12:22 +0000364 Reg = i->second.reg;
365 else if (vrm_->isAssignedReg(cur.reg))
Evan Chengc4c75f52007-11-03 07:20:12 +0000366 Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg));
Evan Chengf5cdf122007-10-17 02:12:22 +0000367 if (!Reg)
368 continue;
Evan Chengcecc8222007-11-17 00:40:40 +0000369 // Ignore splited live intervals.
370 if (!isPhys && vrm_->getPreSplitReg(cur.reg))
371 continue;
Evan Chengf5cdf122007-10-17 02:12:22 +0000372 for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end();
373 I != E; ++I) {
374 const LiveRange &LR = *I;
Evan Chengf5cdf122007-10-17 02:12:22 +0000375 if (li_->findLiveInMBBs(LR, LiveInMBBs)) {
376 for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i)
377 if (LiveInMBBs[i] != EntryMBB)
378 LiveInMBBs[i]->addLiveIn(Reg);
Evan Cheng12d6fcb2007-10-17 06:53:44 +0000379 LiveInMBBs.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 }
381 }
382 }
383
384 DOUT << *vrm_;
385}
386
387/// processActiveIntervals - expire old intervals and move non-overlapping ones
388/// to the inactive list.
389void RALinScan::processActiveIntervals(unsigned CurPoint)
390{
391 DOUT << "\tprocessing active intervals:\n";
392
393 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
394 LiveInterval *Interval = active_[i].first;
395 LiveInterval::iterator IntervalPos = active_[i].second;
396 unsigned reg = Interval->reg;
397
398 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
399
400 if (IntervalPos == Interval->end()) { // Remove expired intervals.
401 DOUT << "\t\tinterval " << *Interval << " expired\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000402 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 "Can only allocate virtual registers!");
404 reg = vrm_->getPhys(reg);
405 prt_->delRegUse(reg);
406
407 // Pop off the end of the list.
408 active_[i] = active_.back();
409 active_.pop_back();
410 --i; --e;
411
412 } else if (IntervalPos->start > CurPoint) {
413 // Move inactive intervals to inactive list.
414 DOUT << "\t\tinterval " << *Interval << " inactive\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000415 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 "Can only allocate virtual registers!");
417 reg = vrm_->getPhys(reg);
418 prt_->delRegUse(reg);
419 // add to inactive.
420 inactive_.push_back(std::make_pair(Interval, IntervalPos));
421
422 // Pop off the end of the list.
423 active_[i] = active_.back();
424 active_.pop_back();
425 --i; --e;
426 } else {
427 // Otherwise, just update the iterator position.
428 active_[i].second = IntervalPos;
429 }
430 }
431}
432
433/// processInactiveIntervals - expire old intervals and move overlapping
434/// ones to the active list.
435void RALinScan::processInactiveIntervals(unsigned CurPoint)
436{
437 DOUT << "\tprocessing inactive intervals:\n";
438
439 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
440 LiveInterval *Interval = inactive_[i].first;
441 LiveInterval::iterator IntervalPos = inactive_[i].second;
442 unsigned reg = Interval->reg;
443
444 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
445
446 if (IntervalPos == Interval->end()) { // remove expired intervals.
447 DOUT << "\t\tinterval " << *Interval << " expired\n";
448
449 // Pop off the end of the list.
450 inactive_[i] = inactive_.back();
451 inactive_.pop_back();
452 --i; --e;
453 } else if (IntervalPos->start <= CurPoint) {
454 // move re-activated intervals in active list
455 DOUT << "\t\tinterval " << *Interval << " active\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000456 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 "Can only allocate virtual registers!");
458 reg = vrm_->getPhys(reg);
459 prt_->addRegUse(reg);
460 // add to active
461 active_.push_back(std::make_pair(Interval, IntervalPos));
462
463 // Pop off the end of the list.
464 inactive_[i] = inactive_.back();
465 inactive_.pop_back();
466 --i; --e;
467 } else {
468 // Otherwise, just update the iterator position.
469 inactive_[i].second = IntervalPos;
470 }
471 }
472}
473
474/// updateSpillWeights - updates the spill weights of the specifed physical
475/// register and its weight.
476static void updateSpillWeights(std::vector<float> &Weights,
477 unsigned reg, float weight,
Dan Gohman1e57df32008-02-10 18:45:23 +0000478 const TargetRegisterInfo *TRI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 Weights[reg] += weight;
Dan Gohman1e57df32008-02-10 18:45:23 +0000480 for (const unsigned* as = TRI->getAliasSet(reg); *as; ++as)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 Weights[*as] += weight;
482}
483
484static
485RALinScan::IntervalPtrs::iterator
486FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) {
487 for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end();
488 I != E; ++I)
489 if (I->first == LI) return I;
490 return IP.end();
491}
492
493static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V, unsigned Point){
494 for (unsigned i = 0, e = V.size(); i != e; ++i) {
495 RALinScan::IntervalPtr &IP = V[i];
496 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
497 IP.second, Point);
498 if (I != IP.first->begin()) --I;
499 IP.second = I;
500 }
501}
502
503/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
504/// spill.
505void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur)
506{
507 DOUT << "\tallocating current interval: ";
508
509 PhysRegTracker backupPrt = *prt_;
510
511 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
512 unsigned StartPosition = cur->beginNumber();
Chris Lattner1b989192007-12-31 04:13:23 +0000513 const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
Evan Chengc4c75f52007-11-03 07:20:12 +0000515
516 // If this live interval is defined by a move instruction and its source is
517 // assigned a physical register that is compatible with the target register
518 // class, then we should try to assign it the same register.
519 // This can happen when the move is from a larger register class to a smaller
520 // one, e.g. X86::mov32to32_. These move instructions are not coalescable.
521 if (!cur->preference && cur->containsOneValue()) {
522 VNInfo *vni = cur->getValNumInfo(0);
523 if (vni->def && vni->def != ~1U && vni->def != ~0U) {
524 MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
525 unsigned SrcReg, DstReg;
526 if (tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) {
527 unsigned Reg = 0;
Dan Gohman1e57df32008-02-10 18:45:23 +0000528 if (TargetRegisterInfo::isPhysicalRegister(SrcReg))
Evan Chengc4c75f52007-11-03 07:20:12 +0000529 Reg = SrcReg;
530 else if (vrm_->isAssignedReg(SrcReg))
531 Reg = vrm_->getPhys(SrcReg);
532 if (Reg && allocatableRegs_[Reg] && RC->contains(Reg))
533 cur->preference = Reg;
534 }
535 }
536 }
537
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 // for every interval in inactive we overlap with, mark the
539 // register as not free and update spill weights.
540 for (IntervalPtrs::const_iterator i = inactive_.begin(),
541 e = inactive_.end(); i != e; ++i) {
542 unsigned Reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000543 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 "Can only allocate virtual registers!");
Chris Lattner1b989192007-12-31 04:13:23 +0000545 const TargetRegisterClass *RegRC = reginfo_->getRegClass(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 // If this is not in a related reg class to the register we're allocating,
547 // don't check it.
548 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
549 cur->overlapsFrom(*i->first, i->second-1)) {
550 Reg = vrm_->getPhys(Reg);
551 prt_->addRegUse(Reg);
552 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
553 }
554 }
555
556 // Speculatively check to see if we can get a register right now. If not,
557 // we know we won't be able to by adding more constraints. If so, we can
558 // check to see if it is valid. Doing an exhaustive search of the fixed_ list
559 // is very bad (it contains all callee clobbered registers for any functions
560 // with a call), so we want to avoid doing that if possible.
561 unsigned physReg = getFreePhysReg(cur);
562 if (physReg) {
563 // We got a register. However, if it's in the fixed_ list, we might
564 // conflict with it. Check to see if we conflict with it or any of its
565 // aliases.
Evan Chengc4c75f52007-11-03 07:20:12 +0000566 SmallSet<unsigned, 8> RegAliases;
Dan Gohman1e57df32008-02-10 18:45:23 +0000567 for (const unsigned *AS = tri_->getAliasSet(physReg); *AS; ++AS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 RegAliases.insert(*AS);
569
570 bool ConflictsWithFixed = false;
571 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
572 IntervalPtr &IP = fixed_[i];
573 if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) {
574 // Okay, this reg is on the fixed list. Check to see if we actually
575 // conflict.
576 LiveInterval *I = IP.first;
577 if (I->endNumber() > StartPosition) {
578 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
579 IP.second = II;
580 if (II != I->begin() && II->start > StartPosition)
581 --II;
582 if (cur->overlapsFrom(*I, II)) {
583 ConflictsWithFixed = true;
584 break;
585 }
586 }
587 }
588 }
589
590 // Okay, the register picked by our speculative getFreePhysReg call turned
591 // out to be in use. Actually add all of the conflicting fixed registers to
592 // prt so we can do an accurate query.
593 if (ConflictsWithFixed) {
594 // For every interval in fixed we overlap with, mark the register as not
595 // free and update spill weights.
596 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
597 IntervalPtr &IP = fixed_[i];
598 LiveInterval *I = IP.first;
599
600 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
601 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
602 I->endNumber() > StartPosition) {
603 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
604 IP.second = II;
605 if (II != I->begin() && II->start > StartPosition)
606 --II;
607 if (cur->overlapsFrom(*I, II)) {
608 unsigned reg = I->reg;
609 prt_->addRegUse(reg);
610 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
611 }
612 }
613 }
614
615 // Using the newly updated prt_ object, which includes conflicts in the
616 // future, see if there are any registers available.
617 physReg = getFreePhysReg(cur);
618 }
619 }
620
621 // Restore the physical register tracker, removing information about the
622 // future.
623 *prt_ = backupPrt;
624
625 // if we find a free register, we are done: assign this virtual to
626 // the free physical register and add this interval to the active
627 // list.
628 if (physReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000629 DOUT << tri_->getName(physReg) << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 vrm_->assignVirt2Phys(cur->reg, physReg);
631 prt_->addRegUse(physReg);
632 active_.push_back(std::make_pair(cur, cur->begin()));
633 handled_.push_back(cur);
634 return;
635 }
636 DOUT << "no free registers\n";
637
638 // Compile the spill weights into an array that is better for scanning.
Dan Gohman1e57df32008-02-10 18:45:23 +0000639 std::vector<float> SpillWeights(tri_->getNumRegs(), 0.0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 for (std::vector<std::pair<unsigned, float> >::iterator
641 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
Dan Gohman1e57df32008-02-10 18:45:23 +0000642 updateSpillWeights(SpillWeights, I->first, I->second, tri_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643
644 // for each interval in active, update spill weights.
645 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
646 i != e; ++i) {
647 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000648 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 "Can only allocate virtual registers!");
650 reg = vrm_->getPhys(reg);
Dan Gohman1e57df32008-02-10 18:45:23 +0000651 updateSpillWeights(SpillWeights, reg, i->first->weight, tri_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 }
653
654 DOUT << "\tassigning stack slot at interval "<< *cur << ":\n";
655
656 // Find a register to spill.
657 float minWeight = HUGE_VALF;
658 unsigned minReg = cur->preference; // Try the preferred register first.
659
660 if (!minReg || SpillWeights[minReg] == HUGE_VALF)
661 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
662 e = RC->allocation_order_end(*mf_); i != e; ++i) {
663 unsigned reg = *i;
664 if (minWeight > SpillWeights[reg]) {
665 minWeight = SpillWeights[reg];
666 minReg = reg;
667 }
668 }
669
670 // If we didn't find a register that is spillable, try aliases?
671 if (!minReg) {
672 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
673 e = RC->allocation_order_end(*mf_); i != e; ++i) {
674 unsigned reg = *i;
675 // No need to worry about if the alias register size < regsize of RC.
676 // We are going to spill all registers that alias it anyway.
Dan Gohman1e57df32008-02-10 18:45:23 +0000677 for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 if (minWeight > SpillWeights[*as]) {
679 minWeight = SpillWeights[*as];
680 minReg = *as;
681 }
682 }
683 }
684
685 // All registers must have inf weight. Just grab one!
686 if (!minReg)
687 minReg = *RC->allocation_order_begin(*mf_);
688 }
689
690 DOUT << "\t\tregister with min weight: "
Dan Gohman1e57df32008-02-10 18:45:23 +0000691 << tri_->getName(minReg) << " (" << minWeight << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692
693 // if the current has the minimum weight, we need to spill it and
694 // add any added intervals back to unhandled, and restart
695 // linearscan.
696 if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
697 DOUT << "\t\t\tspilling(c): " << *cur << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 std::vector<LiveInterval*> added =
Evan Chengcecc8222007-11-17 00:40:40 +0000699 li_->addIntervalsForSpills(*cur, loopInfo, *vrm_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 if (added.empty())
701 return; // Early exit if all spills were folded.
702
703 // Merge added with unhandled. Note that we know that
704 // addIntervalsForSpills returns intervals sorted by their starting
705 // point.
706 for (unsigned i = 0, e = added.size(); i != e; ++i)
707 unhandled_.push(added[i]);
708 return;
709 }
710
711 ++NumBacktracks;
712
713 // push the current interval back to unhandled since we are going
714 // to re-run at least this iteration. Since we didn't modify it it
715 // should go back right in the front of the list
716 unhandled_.push(cur);
717
718 // otherwise we spill all intervals aliasing the register with
719 // minimum weight, rollback to the interval with the earliest
720 // start point and let the linear scan algorithm run again
721 std::vector<LiveInterval*> added;
Dan Gohman1e57df32008-02-10 18:45:23 +0000722 assert(TargetRegisterInfo::isPhysicalRegister(minReg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 "did not choose a register to spill?");
Dan Gohman1e57df32008-02-10 18:45:23 +0000724 BitVector toSpill(tri_->getNumRegs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725
726 // We are going to spill minReg and all its aliases.
727 toSpill[minReg] = true;
Dan Gohman1e57df32008-02-10 18:45:23 +0000728 for (const unsigned* as = tri_->getAliasSet(minReg); *as; ++as)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 toSpill[*as] = true;
730
731 // the earliest start of a spilled interval indicates up to where
732 // in handled we need to roll back
733 unsigned earliestStart = cur->beginNumber();
734
735 // set of spilled vregs (used later to rollback properly)
Evan Chengc4c75f52007-11-03 07:20:12 +0000736 SmallSet<unsigned, 32> spilled;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737
738 // spill live intervals of virtual regs mapped to the physical register we
739 // want to clear (and its aliases). We only spill those that overlap with the
740 // current interval as the rest do not affect its allocation. we also keep
741 // track of the earliest start of all spilled live intervals since this will
742 // mark our rollback point.
743 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
744 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000745 if (//TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746 toSpill[vrm_->getPhys(reg)] &&
747 cur->overlapsFrom(*i->first, i->second)) {
748 DOUT << "\t\t\tspilling(a): " << *i->first << '\n';
749 earliestStart = std::min(earliestStart, i->first->beginNumber());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 std::vector<LiveInterval*> newIs =
Evan Chengcecc8222007-11-17 00:40:40 +0000751 li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
753 spilled.insert(reg);
754 }
755 }
756 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
757 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000758 if (//TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759 toSpill[vrm_->getPhys(reg)] &&
760 cur->overlapsFrom(*i->first, i->second-1)) {
761 DOUT << "\t\t\tspilling(i): " << *i->first << '\n';
762 earliestStart = std::min(earliestStart, i->first->beginNumber());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 std::vector<LiveInterval*> newIs =
Evan Chengcecc8222007-11-17 00:40:40 +0000764 li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
766 spilled.insert(reg);
767 }
768 }
769
770 DOUT << "\t\trolling back to: " << earliestStart << '\n';
771
772 // Scan handled in reverse order up to the earliest start of a
773 // spilled live interval and undo each one, restoring the state of
774 // unhandled.
775 while (!handled_.empty()) {
776 LiveInterval* i = handled_.back();
777 // If this interval starts before t we are done.
778 if (i->beginNumber() < earliestStart)
779 break;
780 DOUT << "\t\t\tundo changes for: " << *i << '\n';
781 handled_.pop_back();
782
783 // When undoing a live interval allocation we must know if it is active or
784 // inactive to properly update the PhysRegTracker and the VirtRegMap.
785 IntervalPtrs::iterator it;
786 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
787 active_.erase(it);
Dan Gohman1e57df32008-02-10 18:45:23 +0000788 assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789 if (!spilled.count(i->reg))
790 unhandled_.push(i);
791 prt_->delRegUse(vrm_->getPhys(i->reg));
792 vrm_->clearVirt(i->reg);
793 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
794 inactive_.erase(it);
Dan Gohman1e57df32008-02-10 18:45:23 +0000795 assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 if (!spilled.count(i->reg))
797 unhandled_.push(i);
798 vrm_->clearVirt(i->reg);
799 } else {
Dan Gohman1e57df32008-02-10 18:45:23 +0000800 assert(TargetRegisterInfo::isVirtualRegister(i->reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 "Can only allocate virtual registers!");
802 vrm_->clearVirt(i->reg);
803 unhandled_.push(i);
804 }
Evan Chengb6aa6712007-11-04 08:32:21 +0000805
806 // It interval has a preference, it must be defined by a copy. Clear the
807 // preference now since the source interval allocation may have been undone
808 // as well.
809 i->preference = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 }
811
812 // Rewind the iterators in the active, inactive, and fixed lists back to the
813 // point we reverted to.
814 RevertVectorIteratorsTo(active_, earliestStart);
815 RevertVectorIteratorsTo(inactive_, earliestStart);
816 RevertVectorIteratorsTo(fixed_, earliestStart);
817
818 // scan the rest and undo each interval that expired after t and
819 // insert it in active (the next iteration of the algorithm will
820 // put it in inactive if required)
821 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
822 LiveInterval *HI = handled_[i];
823 if (!HI->expiredAt(earliestStart) &&
824 HI->expiredAt(cur->beginNumber())) {
825 DOUT << "\t\t\tundo changes for: " << *HI << '\n';
826 active_.push_back(std::make_pair(HI, HI->begin()));
Dan Gohman1e57df32008-02-10 18:45:23 +0000827 assert(!TargetRegisterInfo::isPhysicalRegister(HI->reg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 prt_->addRegUse(vrm_->getPhys(HI->reg));
829 }
830 }
831
832 // merge added with unhandled
833 for (unsigned i = 0, e = added.size(); i != e; ++i)
834 unhandled_.push(added[i]);
835}
836
837/// getFreePhysReg - return a free physical register for this virtual register
838/// interval if we have one, otherwise return 0.
839unsigned RALinScan::getFreePhysReg(LiveInterval *cur) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000840 std::vector<unsigned> inactiveCounts(tri_->getNumRegs(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 unsigned MaxInactiveCount = 0;
842
Chris Lattner1b989192007-12-31 04:13:23 +0000843 const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
845
846 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
847 i != e; ++i) {
848 unsigned reg = i->first->reg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000849 assert(TargetRegisterInfo::isVirtualRegister(reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 "Can only allocate virtual registers!");
851
852 // If this is not in a related reg class to the register we're allocating,
853 // don't check it.
Chris Lattner1b989192007-12-31 04:13:23 +0000854 const TargetRegisterClass *RegRC = reginfo_->getRegClass(reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
856 reg = vrm_->getPhys(reg);
857 ++inactiveCounts[reg];
858 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
859 }
860 }
861
862 unsigned FreeReg = 0;
863 unsigned FreeRegInactiveCount = 0;
864
865 // If copy coalescer has assigned a "preferred" register, check if it's
866 // available first.
867 if (cur->preference)
868 if (prt_->isRegAvail(cur->preference)) {
869 DOUT << "\t\tassigned the preferred register: "
Dan Gohman1e57df32008-02-10 18:45:23 +0000870 << tri_->getName(cur->preference) << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871 return cur->preference;
872 } else
873 DOUT << "\t\tunable to assign the preferred register: "
Dan Gohman1e57df32008-02-10 18:45:23 +0000874 << tri_->getName(cur->preference) << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875
876 // Scan for the first available register.
877 TargetRegisterClass::iterator I = RC->allocation_order_begin(*mf_);
878 TargetRegisterClass::iterator E = RC->allocation_order_end(*mf_);
879 for (; I != E; ++I)
880 if (prt_->isRegAvail(*I)) {
881 FreeReg = *I;
882 FreeRegInactiveCount = inactiveCounts[FreeReg];
883 break;
884 }
885
886 // If there are no free regs, or if this reg has the max inactive count,
887 // return this register.
888 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
889
890 // Continue scanning the registers, looking for the one with the highest
891 // inactive count. Alkis found that this reduced register pressure very
892 // slightly on X86 (in rev 1.94 of this file), though this should probably be
893 // reevaluated now.
894 for (; I != E; ++I) {
895 unsigned Reg = *I;
896 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
897 FreeReg = Reg;
898 FreeRegInactiveCount = inactiveCounts[Reg];
899 if (FreeRegInactiveCount == MaxInactiveCount)
900 break; // We found the one with the max inactive count.
901 }
902 }
903
904 return FreeReg;
905}
906
907FunctionPass* llvm::createLinearScanRegisterAllocator() {
908 return new RALinScan();
909}