blob: 06682dd11e651d021ef60506a50cd3e87590a95d [file] [log] [blame]
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001//===-- RegAllocLinearScan.cpp - Linear Scan register allocator -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a linear scan register allocator.
11//
12//===----------------------------------------------------------------------===//
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000013
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000014#define DEBUG_TYPE "regalloc"
Chris Lattnerb9805782005-08-23 22:27:31 +000015#include "LiveIntervalAnalysis.h"
16#include "PhysRegTracker.h"
17#include "VirtRegMap.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000018#include "llvm/Function.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstr.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/CodeGen/SSARegMap.h"
23#include "llvm/Target/MRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattnerb9805782005-08-23 22:27:31 +000025#include "llvm/ADT/EquivalenceClasses.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/STLExtras.h"
Chris Lattnerb9805782005-08-23 22:27:31 +000028#include "llvm/Support/Debug.h"
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000029#include <algorithm>
Alkis Evlogimenos880e8e42004-05-08 03:50:03 +000030#include <cmath>
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +000031#include <set>
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +000032#include <queue>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000033using namespace llvm;
34
35namespace {
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000036
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000037 Statistic<double> efficiency
38 ("regalloc", "Ratio of intervals processed over total intervals");
Chris Lattner19828d42004-11-18 03:49:30 +000039 Statistic<> NumBacktracks("regalloc", "Number of times we had to backtrack");
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000040
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000041 static unsigned numIterations = 0;
42 static unsigned numIntervals = 0;
Alkis Evlogimenosc1560952004-07-04 17:23:35 +000043
Chris Lattnercbb56252004-11-18 02:42:27 +000044 struct RA : public MachineFunctionPass {
45 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
46 typedef std::vector<IntervalPtr> IntervalPtrs;
47 private:
Chris Lattnerb9805782005-08-23 22:27:31 +000048 /// RelatedRegClasses - This structure is built the first time a function is
49 /// compiled, and keeps track of which register classes have registers that
50 /// belong to multiple classes or have aliases that are in other classes.
51 EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
52 std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
53
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000054 MachineFunction* mf_;
55 const TargetMachine* tm_;
56 const MRegisterInfo* mri_;
57 LiveIntervals* li_;
Chris Lattnerb0f31bf2005-01-23 22:45:13 +000058 bool *PhysRegsUsed;
Chris Lattnercbb56252004-11-18 02:42:27 +000059
60 /// handled_ - Intervals are added to the handled_ set in the order of their
61 /// start value. This is uses for backtracking.
62 std::vector<LiveInterval*> handled_;
63
64 /// fixed_ - Intervals that correspond to machine registers.
65 ///
66 IntervalPtrs fixed_;
67
68 /// active_ - Intervals that are currently being processed, and which have a
69 /// live range active for the current point.
70 IntervalPtrs active_;
71
72 /// inactive_ - Intervals that are currently being processed, but which have
73 /// a hold at the current point.
74 IntervalPtrs inactive_;
75
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000076 typedef std::priority_queue<LiveInterval*,
Chris Lattnercbb56252004-11-18 02:42:27 +000077 std::vector<LiveInterval*>,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000078 greater_ptr<LiveInterval> > IntervalHeap;
79 IntervalHeap unhandled_;
80 std::auto_ptr<PhysRegTracker> prt_;
81 std::auto_ptr<VirtRegMap> vrm_;
82 std::auto_ptr<Spiller> spiller_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000083
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 public:
85 virtual const char* getPassName() const {
86 return "Linear Scan Register Allocator";
87 }
88
89 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000090 AU.addRequired<LiveIntervals>();
91 MachineFunctionPass::getAnalysisUsage(AU);
92 }
93
94 /// runOnMachineFunction - register allocate the whole function
95 bool runOnMachineFunction(MachineFunction&);
96
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000097 private:
98 /// linearScan - the linear scan algorithm
99 void linearScan();
100
Chris Lattnercbb56252004-11-18 02:42:27 +0000101 /// initIntervalSets - initialize the interval sets.
102 ///
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000103 void initIntervalSets();
104
Chris Lattnercbb56252004-11-18 02:42:27 +0000105 /// processActiveIntervals - expire old intervals and move non-overlapping
106 /// ones to the inactive list.
107 void processActiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000108
Chris Lattnercbb56252004-11-18 02:42:27 +0000109 /// processInactiveIntervals - expire old intervals and move overlapping
110 /// ones to the active list.
111 void processInactiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000112
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000113 /// assignRegOrStackSlotAtInterval - assign a register if one
114 /// is available, or spill.
115 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
116
117 ///
118 /// register handling helpers
119 ///
120
Chris Lattnercbb56252004-11-18 02:42:27 +0000121 /// getFreePhysReg - return a free physical register for this virtual
122 /// register interval if we have one, otherwise return 0.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000123 unsigned getFreePhysReg(LiveInterval* cur);
124
125 /// assignVirt2StackSlot - assigns this virtual register to a
126 /// stack slot. returns the stack slot
127 int assignVirt2StackSlot(unsigned virtReg);
128
Chris Lattnerb9805782005-08-23 22:27:31 +0000129 void ComputeRelatedRegClasses();
130
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000131 template <typename ItTy>
132 void printIntervals(const char* const str, ItTy i, ItTy e) const {
133 if (str) std::cerr << str << " intervals:\n";
134 for (; i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000135 std::cerr << "\t" << *i->first << " -> ";
136 unsigned reg = i->first->reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000137 if (MRegisterInfo::isVirtualRegister(reg)) {
138 reg = vrm_->getPhys(reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000139 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000140 std::cerr << mri_->getName(reg) << '\n';
141 }
142 }
143 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000144}
145
Chris Lattnerb9805782005-08-23 22:27:31 +0000146void RA::ComputeRelatedRegClasses() {
147 const MRegisterInfo &MRI = *mri_;
148
149 // First pass, add all reg classes to the union, and determine at least one
150 // reg class that each register is in.
151 bool HasAliases = false;
152 for (MRegisterInfo::regclass_iterator RCI = MRI.regclass_begin(),
153 E = MRI.regclass_end(); RCI != E; ++RCI) {
154 RelatedRegClasses.insert(*RCI);
155 for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
156 I != E; ++I) {
157 HasAliases = HasAliases || *MRI.getAliasSet(*I) != 0;
158
159 const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
160 if (PRC) {
161 // Already processed this register. Just make sure we know that
162 // multiple register classes share a register.
163 RelatedRegClasses.unionSets(PRC, *RCI);
164 } else {
165 PRC = *RCI;
166 }
167 }
168 }
169
170 // Second pass, now that we know conservatively what register classes each reg
171 // belongs to, add info about aliases. We don't need to do this for targets
172 // without register aliases.
173 if (HasAliases)
174 for (std::map<unsigned, const TargetRegisterClass*>::iterator
175 I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
176 I != E; ++I)
177 for (const unsigned *AS = MRI.getAliasSet(I->first); *AS; ++AS)
178 RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
179}
180
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000181bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000182 mf_ = &fn;
183 tm_ = &fn.getTarget();
184 mri_ = tm_->getRegisterInfo();
185 li_ = &getAnalysis<LiveIntervals>();
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000186
Chris Lattnerb9805782005-08-23 22:27:31 +0000187 // If this is the first function compiled, compute the related reg classes.
188 if (RelatedRegClasses.empty())
189 ComputeRelatedRegClasses();
190
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000191 PhysRegsUsed = new bool[mri_->getNumRegs()];
192 std::fill(PhysRegsUsed, PhysRegsUsed+mri_->getNumRegs(), false);
193 fn.setUsedPhysRegs(PhysRegsUsed);
194
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000195 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
196 vrm_.reset(new VirtRegMap(*mf_));
197 if (!spiller_.get()) spiller_.reset(createSpiller());
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000198
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000199 initIntervalSets();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000200
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000201 linearScan();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000202
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000203 // Rewrite spill code and update the PhysRegsUsed set.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000204 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000205
Chris Lattner510a3ea2004-09-30 02:02:33 +0000206 vrm_.reset(); // Free the VirtRegMap
Chris Lattnercbb56252004-11-18 02:42:27 +0000207
208
209 while (!unhandled_.empty()) unhandled_.pop();
210 fixed_.clear();
211 active_.clear();
212 inactive_.clear();
213 handled_.clear();
214
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000215 return true;
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000216}
217
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000218/// initIntervalSets - initialize the interval sets.
219///
220void RA::initIntervalSets()
221{
222 assert(unhandled_.empty() && fixed_.empty() &&
223 active_.empty() && inactive_.empty() &&
224 "interval sets should be empty on initialization");
225
226 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000227 if (MRegisterInfo::isPhysicalRegister(i->second.reg)) {
228 PhysRegsUsed[i->second.reg] = true;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000229 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000230 } else
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000231 unhandled_.push(&i->second);
232 }
233}
234
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000235void RA::linearScan()
236{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000237 // linear scan algorithm
238 DEBUG(std::cerr << "********** LINEAR SCAN **********\n");
239 DEBUG(std::cerr << "********** Function: "
240 << mf_->getFunction()->getName() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000241
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000242 // DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
243 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
244 DEBUG(printIntervals("active", active_.begin(), active_.end()));
245 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
246
247 while (!unhandled_.empty()) {
248 // pick the interval with the earliest start point
249 LiveInterval* cur = unhandled_.top();
250 unhandled_.pop();
251 ++numIterations;
252 DEBUG(std::cerr << "\n*** CURRENT ***: " << *cur << '\n');
253
Chris Lattnercbb56252004-11-18 02:42:27 +0000254 processActiveIntervals(cur->beginNumber());
255 processInactiveIntervals(cur->beginNumber());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000256
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000257 assert(MRegisterInfo::isVirtualRegister(cur->reg) &&
258 "Can only allocate virtual registers!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000259
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000260 // Allocating a virtual register. try to find a free
261 // physical register or spill an interval (possibly this one) in order to
262 // assign it one.
263 assignRegOrStackSlotAtInterval(cur);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000264
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000265 DEBUG(printIntervals("active", active_.begin(), active_.end()));
266 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000267 }
268 numIntervals += li_->getNumIntervals();
269 efficiency = double(numIterations) / double(numIntervals);
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000270
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000271 // expire any remaining active intervals
272 for (IntervalPtrs::reverse_iterator
273 i = active_.rbegin(); i != active_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000274 unsigned reg = i->first->reg;
275 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000276 assert(MRegisterInfo::isVirtualRegister(reg) &&
277 "Can only allocate virtual registers!");
278 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000279 prt_->delRegUse(reg);
280 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
281 }
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000282
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000283 // expire any remaining inactive intervals
284 for (IntervalPtrs::reverse_iterator
285 i = inactive_.rbegin(); i != inactive_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000286 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000287 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
288 }
Alkis Evlogimenosb7be1152004-01-13 20:42:08 +0000289
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000290 DEBUG(std::cerr << *vrm_);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000291}
292
Chris Lattnercbb56252004-11-18 02:42:27 +0000293/// processActiveIntervals - expire old intervals and move non-overlapping ones
294/// to the inactive list.
295void RA::processActiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000296{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000297 DEBUG(std::cerr << "\tprocessing active intervals:\n");
Chris Lattner23b71c12004-11-18 01:29:39 +0000298
Chris Lattnercbb56252004-11-18 02:42:27 +0000299 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
300 LiveInterval *Interval = active_[i].first;
301 LiveInterval::iterator IntervalPos = active_[i].second;
302 unsigned reg = Interval->reg;
Alkis Evlogimenosed543732004-09-01 22:52:29 +0000303
Chris Lattnercbb56252004-11-18 02:42:27 +0000304 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
305
306 if (IntervalPos == Interval->end()) { // Remove expired intervals.
307 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000308 assert(MRegisterInfo::isVirtualRegister(reg) &&
309 "Can only allocate virtual registers!");
310 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000311 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000312
313 // Pop off the end of the list.
314 active_[i] = active_.back();
315 active_.pop_back();
316 --i; --e;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000317
Chris Lattnercbb56252004-11-18 02:42:27 +0000318 } else if (IntervalPos->start > CurPoint) {
319 // Move inactive intervals to inactive list.
320 DEBUG(std::cerr << "\t\tinterval " << *Interval << " inactive\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000321 assert(MRegisterInfo::isVirtualRegister(reg) &&
322 "Can only allocate virtual registers!");
323 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000324 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000325 // add to inactive.
326 inactive_.push_back(std::make_pair(Interval, IntervalPos));
327
328 // Pop off the end of the list.
329 active_[i] = active_.back();
330 active_.pop_back();
331 --i; --e;
332 } else {
333 // Otherwise, just update the iterator position.
334 active_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000335 }
336 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000337}
338
Chris Lattnercbb56252004-11-18 02:42:27 +0000339/// processInactiveIntervals - expire old intervals and move overlapping
340/// ones to the active list.
341void RA::processInactiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000342{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000343 DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
Chris Lattner365b95f2004-11-18 04:13:02 +0000344
Chris Lattnercbb56252004-11-18 02:42:27 +0000345 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
346 LiveInterval *Interval = inactive_[i].first;
347 LiveInterval::iterator IntervalPos = inactive_[i].second;
348 unsigned reg = Interval->reg;
Chris Lattner23b71c12004-11-18 01:29:39 +0000349
Chris Lattnercbb56252004-11-18 02:42:27 +0000350 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000351
Chris Lattnercbb56252004-11-18 02:42:27 +0000352 if (IntervalPos == Interval->end()) { // remove expired intervals.
353 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000354
Chris Lattnercbb56252004-11-18 02:42:27 +0000355 // Pop off the end of the list.
356 inactive_[i] = inactive_.back();
357 inactive_.pop_back();
358 --i; --e;
359 } else if (IntervalPos->start <= CurPoint) {
360 // move re-activated intervals in active list
361 DEBUG(std::cerr << "\t\tinterval " << *Interval << " active\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000362 assert(MRegisterInfo::isVirtualRegister(reg) &&
363 "Can only allocate virtual registers!");
364 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000365 prt_->addRegUse(reg);
366 // add to active
Chris Lattnercbb56252004-11-18 02:42:27 +0000367 active_.push_back(std::make_pair(Interval, IntervalPos));
368
369 // Pop off the end of the list.
370 inactive_[i] = inactive_.back();
371 inactive_.pop_back();
372 --i; --e;
373 } else {
374 // Otherwise, just update the iterator position.
375 inactive_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000376 }
377 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000378}
379
Chris Lattnercbb56252004-11-18 02:42:27 +0000380/// updateSpillWeights - updates the spill weights of the specifed physical
381/// register and its weight.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000382static void updateSpillWeights(std::vector<float> &Weights,
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000383 unsigned reg, float weight,
384 const MRegisterInfo *MRI) {
385 Weights[reg] += weight;
386 for (const unsigned* as = MRI->getAliasSet(reg); *as; ++as)
387 Weights[*as] += weight;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000388}
389
Chris Lattnercbb56252004-11-18 02:42:27 +0000390static RA::IntervalPtrs::iterator FindIntervalInVector(RA::IntervalPtrs &IP,
391 LiveInterval *LI) {
392 for (RA::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); I != E; ++I)
393 if (I->first == LI) return I;
394 return IP.end();
395}
396
Chris Lattner19828d42004-11-18 03:49:30 +0000397static void RevertVectorIteratorsTo(RA::IntervalPtrs &V, unsigned Point) {
398 for (unsigned i = 0, e = V.size(); i != e; ++i) {
399 RA::IntervalPtr &IP = V[i];
400 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
401 IP.second, Point);
402 if (I != IP.first->begin()) --I;
403 IP.second = I;
404 }
405}
Chris Lattnercbb56252004-11-18 02:42:27 +0000406
407
408/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
409/// spill.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000410void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000411{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000412 DEBUG(std::cerr << "\tallocating current interval: ");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000413
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000414 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000415
Chris Lattnera6c17502005-08-22 20:20:42 +0000416 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
Chris Lattner365b95f2004-11-18 04:13:02 +0000417 unsigned StartPosition = cur->beginNumber();
Chris Lattnerb9805782005-08-23 22:27:31 +0000418 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
419 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
420
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000421 // for every interval in inactive we overlap with, mark the
Chris Lattnera6c17502005-08-22 20:20:42 +0000422 // register as not free and update spill weights.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000423 for (IntervalPtrs::const_iterator i = inactive_.begin(),
424 e = inactive_.end(); i != e; ++i) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000425 unsigned Reg = i->first->reg;
426 assert(MRegisterInfo::isVirtualRegister(Reg) &&
427 "Can only allocate virtual registers!");
428 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(Reg);
429 // If this is not in a related reg class to the register we're allocating,
430 // don't check it.
431 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
432 cur->overlapsFrom(*i->first, i->second-1)) {
433 Reg = vrm_->getPhys(Reg);
434 prt_->addRegUse(Reg);
435 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000436 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000437 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000438
439 // Speculatively check to see if we can get a register right now. If not,
440 // we know we won't be able to by adding more constraints. If so, we can
441 // check to see if it is valid. Doing an exhaustive search of the fixed_ list
442 // is very bad (it contains all callee clobbered registers for any functions
443 // with a call), so we want to avoid doing that if possible.
444 unsigned physReg = getFreePhysReg(cur);
445 if (physReg) {
446 // We got a register. However, if it's in the fixed_ list, we might
447 // conflict with it. Check to see if we conflict with it.
448 bool ConflictsWithFixed = false;
449 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
450 if (physReg == fixed_[i].first->reg) {
451 // Okay, this reg is on the fixed list. Check to see if we actually
452 // conflict.
453 IntervalPtr &IP = fixed_[i];
454 LiveInterval *I = IP.first;
455 if (I->endNumber() > StartPosition) {
456 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
457 IP.second = II;
458 if (II != I->begin() && II->start > StartPosition)
459 --II;
460 if (cur->overlapsFrom(*I, II))
461 ConflictsWithFixed = true;
462 }
463
464 break;
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000465 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000466 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000467
468 // Okay, the register picked by our speculative getFreePhysReg call turned
469 // out to be in use. Actually add all of the conflicting fixed registers to
470 // prt so we can do an accurate query.
471 if (ConflictsWithFixed) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000472 // For every interval in fixed we overlap with, mark the register as not
473 // free and update spill weights.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000474 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
475 IntervalPtr &IP = fixed_[i];
476 LiveInterval *I = IP.first;
Chris Lattnerb9805782005-08-23 22:27:31 +0000477
478 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
479 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
480 I->endNumber() > StartPosition) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000481 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
482 IP.second = II;
483 if (II != I->begin() && II->start > StartPosition)
484 --II;
485 if (cur->overlapsFrom(*I, II)) {
486 unsigned reg = I->reg;
487 prt_->addRegUse(reg);
488 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
489 }
490 }
491 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000492
Chris Lattnera411cbc2005-08-22 20:59:30 +0000493 // Using the newly updated prt_ object, which includes conflicts in the
494 // future, see if there are any registers available.
495 physReg = getFreePhysReg(cur);
496 }
497 }
498
Chris Lattnera6c17502005-08-22 20:20:42 +0000499 // Restore the physical register tracker, removing information about the
500 // future.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000501 *prt_ = backupPrt;
Chris Lattnera6c17502005-08-22 20:20:42 +0000502
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000503 // if we find a free register, we are done: assign this virtual to
504 // the free physical register and add this interval to the active
505 // list.
506 if (physReg) {
507 DEBUG(std::cerr << mri_->getName(physReg) << '\n');
508 vrm_->assignVirt2Phys(cur->reg, physReg);
509 prt_->addRegUse(physReg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000510 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000511 handled_.push_back(cur);
512 return;
513 }
514 DEBUG(std::cerr << "no free registers\n");
515
Chris Lattnera6c17502005-08-22 20:20:42 +0000516 // Compile the spill weights into an array that is better for scanning.
517 std::vector<float> SpillWeights(mri_->getNumRegs(), 0.0);
518 for (std::vector<std::pair<unsigned, float> >::iterator
519 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
520 updateSpillWeights(SpillWeights, I->first, I->second, mri_);
521
522 // for each interval in active, update spill weights.
523 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
524 i != e; ++i) {
525 unsigned reg = i->first->reg;
526 assert(MRegisterInfo::isVirtualRegister(reg) &&
527 "Can only allocate virtual registers!");
528 reg = vrm_->getPhys(reg);
529 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
530 }
531
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000532 DEBUG(std::cerr << "\tassigning stack slot at interval "<< *cur << ":\n");
533
Chris Lattner5e5fb942005-01-08 19:53:50 +0000534 float minWeight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000535 unsigned minReg = 0;
Chris Lattnerb9805782005-08-23 22:27:31 +0000536 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
537 e = RC->allocation_order_end(*mf_); i != e; ++i) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000538 unsigned reg = *i;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000539 if (minWeight > SpillWeights[reg]) {
540 minWeight = SpillWeights[reg];
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000541 minReg = reg;
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000542 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000543 }
544 DEBUG(std::cerr << "\t\tregister with min weight: "
545 << mri_->getName(minReg) << " (" << minWeight << ")\n");
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000546
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000547 // if the current has the minimum weight, we need to spill it and
548 // add any added intervals back to unhandled, and restart
549 // linearscan.
550 if (cur->weight <= minWeight) {
551 DEBUG(std::cerr << "\t\t\tspilling(c): " << *cur << '\n';);
552 int slot = vrm_->assignVirt2StackSlot(cur->reg);
553 std::vector<LiveInterval*> added =
554 li_->addIntervalsForSpills(*cur, *vrm_, slot);
555 if (added.empty())
556 return; // Early exit if all spills were folded.
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000557
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000558 // Merge added with unhandled. Note that we know that
559 // addIntervalsForSpills returns intervals sorted by their starting
560 // point.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000561 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000562 unhandled_.push(added[i]);
563 return;
564 }
565
Chris Lattner19828d42004-11-18 03:49:30 +0000566 ++NumBacktracks;
567
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000568 // push the current interval back to unhandled since we are going
569 // to re-run at least this iteration. Since we didn't modify it it
570 // should go back right in the front of the list
571 unhandled_.push(cur);
572
573 // otherwise we spill all intervals aliasing the register with
574 // minimum weight, rollback to the interval with the earliest
575 // start point and let the linear scan algorithm run again
576 std::vector<LiveInterval*> added;
577 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
578 "did not choose a register to spill?");
579 std::vector<bool> toSpill(mri_->getNumRegs(), false);
Chris Lattner19828d42004-11-18 03:49:30 +0000580
581 // We are going to spill minReg and all its aliases.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000582 toSpill[minReg] = true;
583 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
584 toSpill[*as] = true;
585
586 // the earliest start of a spilled interval indicates up to where
587 // in handled we need to roll back
Chris Lattner23b71c12004-11-18 01:29:39 +0000588 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000589
590 // set of spilled vregs (used later to rollback properly)
591 std::set<unsigned> spilled;
592
Chris Lattner19828d42004-11-18 03:49:30 +0000593 // spill live intervals of virtual regs mapped to the physical register we
594 // want to clear (and its aliases). We only spill those that overlap with the
595 // current interval as the rest do not affect its allocation. we also keep
596 // track of the earliest start of all spilled live intervals since this will
597 // mark our rollback point.
598 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000599 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000600 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000601 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000602 cur->overlapsFrom(*i->first, i->second)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000603 DEBUG(std::cerr << "\t\t\tspilling(a): " << *i->first << '\n');
604 earliestStart = std::min(earliestStart, i->first->beginNumber());
605 int slot = vrm_->assignVirt2StackSlot(i->first->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000606 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000607 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000608 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
609 spilled.insert(reg);
610 }
611 }
Chris Lattner19828d42004-11-18 03:49:30 +0000612 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnercbb56252004-11-18 02:42:27 +0000613 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000614 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000615 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000616 cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000617 DEBUG(std::cerr << "\t\t\tspilling(i): " << *i->first << '\n');
618 earliestStart = std::min(earliestStart, i->first->beginNumber());
619 int slot = vrm_->assignVirt2StackSlot(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000620 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000621 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000622 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
623 spilled.insert(reg);
624 }
625 }
626
627 DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
Chris Lattnercbb56252004-11-18 02:42:27 +0000628
629 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000630 // spilled live interval and undo each one, restoring the state of
Chris Lattnercbb56252004-11-18 02:42:27 +0000631 // unhandled.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000632 while (!handled_.empty()) {
633 LiveInterval* i = handled_.back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000634 // If this interval starts before t we are done.
Chris Lattner23b71c12004-11-18 01:29:39 +0000635 if (i->beginNumber() < earliestStart)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000636 break;
637 DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
638 handled_.pop_back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000639
640 // When undoing a live interval allocation we must know if it is active or
641 // inactive to properly update the PhysRegTracker and the VirtRegMap.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000642 IntervalPtrs::iterator it;
Chris Lattnercbb56252004-11-18 02:42:27 +0000643 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000644 active_.erase(it);
645 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000646 assert(0 && "daksjlfd");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000647 prt_->delRegUse(i->reg);
648 unhandled_.push(i);
Chris Lattnercbb56252004-11-18 02:42:27 +0000649 } else {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000650 if (!spilled.count(i->reg))
651 unhandled_.push(i);
652 prt_->delRegUse(vrm_->getPhys(i->reg));
653 vrm_->clearVirt(i->reg);
654 }
Chris Lattnercbb56252004-11-18 02:42:27 +0000655 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000656 inactive_.erase(it);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000657 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
658 assert(0 && "daksjlfd");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000659 unhandled_.push(i);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000660 } else {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000661 if (!spilled.count(i->reg))
662 unhandled_.push(i);
663 vrm_->clearVirt(i->reg);
664 }
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000665 } else {
666 assert(MRegisterInfo::isVirtualRegister(i->reg) &&
667 "Can only allocate virtual registers!");
668 vrm_->clearVirt(i->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000669 unhandled_.push(i);
670 }
671 }
672
Chris Lattner19828d42004-11-18 03:49:30 +0000673 // Rewind the iterators in the active, inactive, and fixed lists back to the
674 // point we reverted to.
675 RevertVectorIteratorsTo(active_, earliestStart);
676 RevertVectorIteratorsTo(inactive_, earliestStart);
677 RevertVectorIteratorsTo(fixed_, earliestStart);
678
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000679 // scan the rest and undo each interval that expired after t and
680 // insert it in active (the next iteration of the algorithm will
681 // put it in inactive if required)
Chris Lattnercbb56252004-11-18 02:42:27 +0000682 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
683 LiveInterval *HI = handled_[i];
684 if (!HI->expiredAt(earliestStart) &&
685 HI->expiredAt(cur->beginNumber())) {
686 DEBUG(std::cerr << "\t\t\tundo changes for: " << *HI << '\n');
687 active_.push_back(std::make_pair(HI, HI->begin()));
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000688 if (MRegisterInfo::isPhysicalRegister(HI->reg)) {
689 assert(0 &&"sdflkajsdf");
Chris Lattnercbb56252004-11-18 02:42:27 +0000690 prt_->addRegUse(HI->reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000691 } else
Chris Lattnercbb56252004-11-18 02:42:27 +0000692 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000693 }
694 }
695
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000696 // merge added with unhandled
697 for (unsigned i = 0, e = added.size(); i != e; ++i)
698 unhandled_.push(added[i]);
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000699}
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000700
Chris Lattnercbb56252004-11-18 02:42:27 +0000701/// getFreePhysReg - return a free physical register for this virtual register
702/// interval if we have one, otherwise return 0.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000703unsigned RA::getFreePhysReg(LiveInterval* cur)
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000704{
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000705 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
Chris Lattnerf8355d92005-08-22 16:55:22 +0000706 unsigned MaxInactiveCount = 0;
707
Chris Lattnerb9805782005-08-23 22:27:31 +0000708 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
709 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
710
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000711 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
712 i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000713 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000714 assert(MRegisterInfo::isVirtualRegister(reg) &&
715 "Can only allocate virtual registers!");
Chris Lattnerb9805782005-08-23 22:27:31 +0000716
717 // If this is not in a related reg class to the register we're allocating,
718 // don't check it.
719 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(reg);
720 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
721 reg = vrm_->getPhys(reg);
722 ++inactiveCounts[reg];
723 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
724 }
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000725 }
726
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000727 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000728
Chris Lattnerf8355d92005-08-22 16:55:22 +0000729 unsigned FreeReg = 0;
730 unsigned FreeRegInactiveCount = 0;
731
732 // Scan for the first available register.
733 TargetRegisterClass::iterator I = rc->allocation_order_begin(*mf_);
734 TargetRegisterClass::iterator E = rc->allocation_order_end(*mf_);
735 for (; I != E; ++I)
736 if (prt_->isRegAvail(*I)) {
737 FreeReg = *I;
738 FreeRegInactiveCount = inactiveCounts[FreeReg];
739 break;
740 }
741
742 // If there are no free regs, or if this reg has the max inactive count,
743 // return this register.
744 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
745
746 // Continue scanning the registers, looking for the one with the highest
747 // inactive count. Alkis found that this reduced register pressure very
748 // slightly on X86 (in rev 1.94 of this file), though this should probably be
749 // reevaluated now.
750 for (; I != E; ++I) {
751 unsigned Reg = *I;
752 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
753 FreeReg = Reg;
754 FreeRegInactiveCount = inactiveCounts[Reg];
755 if (FreeRegInactiveCount == MaxInactiveCount)
756 break; // We found the one with the max inactive count.
757 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000758 }
Chris Lattnerf8355d92005-08-22 16:55:22 +0000759
760 return FreeReg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000761}
762
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000763FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000764 return new RA();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000765}