blob: a42a366ef36b8b9841baff72baef120c6bd7a57a [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 Lattner3c3fe462005-09-21 04:19:09 +000015#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Chris Lattnerb9805782005-08-23 22:27:31 +000016#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
Chris Lattnere836ad62005-08-30 21:03:36 +0000447 // conflict with it. Check to see if we conflict with it or any of its
448 // aliases.
449 std::set<unsigned> RegAliases;
450 for (const unsigned *AS = mri_->getAliasSet(physReg); *AS; ++AS)
451 RegAliases.insert(*AS);
452
Chris Lattnera411cbc2005-08-22 20:59:30 +0000453 bool ConflictsWithFixed = false;
454 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
Chris Lattnere836ad62005-08-30 21:03:36 +0000455 if (physReg == fixed_[i].first->reg ||
456 RegAliases.count(fixed_[i].first->reg)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000457 // Okay, this reg is on the fixed list. Check to see if we actually
458 // conflict.
459 IntervalPtr &IP = fixed_[i];
460 LiveInterval *I = IP.first;
461 if (I->endNumber() > StartPosition) {
462 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
463 IP.second = II;
464 if (II != I->begin() && II->start > StartPosition)
465 --II;
Chris Lattnere836ad62005-08-30 21:03:36 +0000466 if (cur->overlapsFrom(*I, II)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000467 ConflictsWithFixed = true;
Chris Lattnere836ad62005-08-30 21:03:36 +0000468 break;
469 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000470 }
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000471 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000472 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000473
474 // Okay, the register picked by our speculative getFreePhysReg call turned
475 // out to be in use. Actually add all of the conflicting fixed registers to
476 // prt so we can do an accurate query.
477 if (ConflictsWithFixed) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000478 // For every interval in fixed we overlap with, mark the register as not
479 // free and update spill weights.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000480 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
481 IntervalPtr &IP = fixed_[i];
482 LiveInterval *I = IP.first;
Chris Lattnerb9805782005-08-23 22:27:31 +0000483
484 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
485 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
486 I->endNumber() > StartPosition) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000487 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
488 IP.second = II;
489 if (II != I->begin() && II->start > StartPosition)
490 --II;
491 if (cur->overlapsFrom(*I, II)) {
492 unsigned reg = I->reg;
493 prt_->addRegUse(reg);
494 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
495 }
496 }
497 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000498
Chris Lattnera411cbc2005-08-22 20:59:30 +0000499 // Using the newly updated prt_ object, which includes conflicts in the
500 // future, see if there are any registers available.
501 physReg = getFreePhysReg(cur);
502 }
503 }
504
Chris Lattnera6c17502005-08-22 20:20:42 +0000505 // Restore the physical register tracker, removing information about the
506 // future.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000507 *prt_ = backupPrt;
Chris Lattnera6c17502005-08-22 20:20:42 +0000508
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000509 // if we find a free register, we are done: assign this virtual to
510 // the free physical register and add this interval to the active
511 // list.
512 if (physReg) {
513 DEBUG(std::cerr << mri_->getName(physReg) << '\n');
514 vrm_->assignVirt2Phys(cur->reg, physReg);
515 prt_->addRegUse(physReg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000516 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000517 handled_.push_back(cur);
518 return;
519 }
520 DEBUG(std::cerr << "no free registers\n");
521
Chris Lattnera6c17502005-08-22 20:20:42 +0000522 // Compile the spill weights into an array that is better for scanning.
523 std::vector<float> SpillWeights(mri_->getNumRegs(), 0.0);
524 for (std::vector<std::pair<unsigned, float> >::iterator
525 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
526 updateSpillWeights(SpillWeights, I->first, I->second, mri_);
527
528 // for each interval in active, update spill weights.
529 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
530 i != e; ++i) {
531 unsigned reg = i->first->reg;
532 assert(MRegisterInfo::isVirtualRegister(reg) &&
533 "Can only allocate virtual registers!");
534 reg = vrm_->getPhys(reg);
535 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
536 }
537
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000538 DEBUG(std::cerr << "\tassigning stack slot at interval "<< *cur << ":\n");
539
Chris Lattner5e5fb942005-01-08 19:53:50 +0000540 float minWeight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000541 unsigned minReg = 0;
Chris Lattnerb9805782005-08-23 22:27:31 +0000542 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
543 e = RC->allocation_order_end(*mf_); i != e; ++i) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000544 unsigned reg = *i;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000545 if (minWeight > SpillWeights[reg]) {
546 minWeight = SpillWeights[reg];
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000547 minReg = reg;
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000548 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000549 }
550 DEBUG(std::cerr << "\t\tregister with min weight: "
551 << mri_->getName(minReg) << " (" << minWeight << ")\n");
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000552
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000553 // if the current has the minimum weight, we need to spill it and
554 // add any added intervals back to unhandled, and restart
555 // linearscan.
556 if (cur->weight <= minWeight) {
557 DEBUG(std::cerr << "\t\t\tspilling(c): " << *cur << '\n';);
558 int slot = vrm_->assignVirt2StackSlot(cur->reg);
559 std::vector<LiveInterval*> added =
560 li_->addIntervalsForSpills(*cur, *vrm_, slot);
561 if (added.empty())
562 return; // Early exit if all spills were folded.
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000563
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000564 // Merge added with unhandled. Note that we know that
565 // addIntervalsForSpills returns intervals sorted by their starting
566 // point.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000567 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000568 unhandled_.push(added[i]);
569 return;
570 }
571
Chris Lattner19828d42004-11-18 03:49:30 +0000572 ++NumBacktracks;
573
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000574 // push the current interval back to unhandled since we are going
575 // to re-run at least this iteration. Since we didn't modify it it
576 // should go back right in the front of the list
577 unhandled_.push(cur);
578
579 // otherwise we spill all intervals aliasing the register with
580 // minimum weight, rollback to the interval with the earliest
581 // start point and let the linear scan algorithm run again
582 std::vector<LiveInterval*> added;
583 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
584 "did not choose a register to spill?");
585 std::vector<bool> toSpill(mri_->getNumRegs(), false);
Chris Lattner19828d42004-11-18 03:49:30 +0000586
587 // We are going to spill minReg and all its aliases.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000588 toSpill[minReg] = true;
589 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
590 toSpill[*as] = true;
591
592 // the earliest start of a spilled interval indicates up to where
593 // in handled we need to roll back
Chris Lattner23b71c12004-11-18 01:29:39 +0000594 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000595
596 // set of spilled vregs (used later to rollback properly)
597 std::set<unsigned> spilled;
598
Chris Lattner19828d42004-11-18 03:49:30 +0000599 // spill live intervals of virtual regs mapped to the physical register we
600 // want to clear (and its aliases). We only spill those that overlap with the
601 // current interval as the rest do not affect its allocation. we also keep
602 // track of the earliest start of all spilled live intervals since this will
603 // mark our rollback point.
604 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000605 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000606 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000607 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000608 cur->overlapsFrom(*i->first, i->second)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000609 DEBUG(std::cerr << "\t\t\tspilling(a): " << *i->first << '\n');
610 earliestStart = std::min(earliestStart, i->first->beginNumber());
611 int slot = vrm_->assignVirt2StackSlot(i->first->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000612 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000613 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000614 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
615 spilled.insert(reg);
616 }
617 }
Chris Lattner19828d42004-11-18 03:49:30 +0000618 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnercbb56252004-11-18 02:42:27 +0000619 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000620 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000621 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000622 cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000623 DEBUG(std::cerr << "\t\t\tspilling(i): " << *i->first << '\n');
624 earliestStart = std::min(earliestStart, i->first->beginNumber());
625 int slot = vrm_->assignVirt2StackSlot(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000626 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000627 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000628 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
629 spilled.insert(reg);
630 }
631 }
632
633 DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
Chris Lattnercbb56252004-11-18 02:42:27 +0000634
635 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000636 // spilled live interval and undo each one, restoring the state of
Chris Lattnercbb56252004-11-18 02:42:27 +0000637 // unhandled.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000638 while (!handled_.empty()) {
639 LiveInterval* i = handled_.back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000640 // If this interval starts before t we are done.
Chris Lattner23b71c12004-11-18 01:29:39 +0000641 if (i->beginNumber() < earliestStart)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000642 break;
643 DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
644 handled_.pop_back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000645
646 // When undoing a live interval allocation we must know if it is active or
647 // inactive to properly update the PhysRegTracker and the VirtRegMap.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000648 IntervalPtrs::iterator it;
Chris Lattnercbb56252004-11-18 02:42:27 +0000649 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000650 active_.erase(it);
651 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000652 assert(0 && "daksjlfd");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000653 prt_->delRegUse(i->reg);
654 unhandled_.push(i);
Chris Lattnercbb56252004-11-18 02:42:27 +0000655 } else {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000656 if (!spilled.count(i->reg))
657 unhandled_.push(i);
658 prt_->delRegUse(vrm_->getPhys(i->reg));
659 vrm_->clearVirt(i->reg);
660 }
Chris Lattnercbb56252004-11-18 02:42:27 +0000661 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000662 inactive_.erase(it);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000663 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
664 assert(0 && "daksjlfd");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000665 unhandled_.push(i);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000666 } else {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000667 if (!spilled.count(i->reg))
668 unhandled_.push(i);
669 vrm_->clearVirt(i->reg);
670 }
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000671 } else {
672 assert(MRegisterInfo::isVirtualRegister(i->reg) &&
673 "Can only allocate virtual registers!");
674 vrm_->clearVirt(i->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000675 unhandled_.push(i);
676 }
677 }
678
Chris Lattner19828d42004-11-18 03:49:30 +0000679 // Rewind the iterators in the active, inactive, and fixed lists back to the
680 // point we reverted to.
681 RevertVectorIteratorsTo(active_, earliestStart);
682 RevertVectorIteratorsTo(inactive_, earliestStart);
683 RevertVectorIteratorsTo(fixed_, earliestStart);
684
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000685 // scan the rest and undo each interval that expired after t and
686 // insert it in active (the next iteration of the algorithm will
687 // put it in inactive if required)
Chris Lattnercbb56252004-11-18 02:42:27 +0000688 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
689 LiveInterval *HI = handled_[i];
690 if (!HI->expiredAt(earliestStart) &&
691 HI->expiredAt(cur->beginNumber())) {
692 DEBUG(std::cerr << "\t\t\tundo changes for: " << *HI << '\n');
693 active_.push_back(std::make_pair(HI, HI->begin()));
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000694 if (MRegisterInfo::isPhysicalRegister(HI->reg)) {
695 assert(0 &&"sdflkajsdf");
Chris Lattnercbb56252004-11-18 02:42:27 +0000696 prt_->addRegUse(HI->reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000697 } else
Chris Lattnercbb56252004-11-18 02:42:27 +0000698 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000699 }
700 }
701
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000702 // merge added with unhandled
703 for (unsigned i = 0, e = added.size(); i != e; ++i)
704 unhandled_.push(added[i]);
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000705}
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000706
Chris Lattnercbb56252004-11-18 02:42:27 +0000707/// getFreePhysReg - return a free physical register for this virtual register
708/// interval if we have one, otherwise return 0.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000709unsigned RA::getFreePhysReg(LiveInterval* cur)
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000710{
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000711 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
Chris Lattnerf8355d92005-08-22 16:55:22 +0000712 unsigned MaxInactiveCount = 0;
713
Chris Lattnerb9805782005-08-23 22:27:31 +0000714 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
715 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
716
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000717 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
718 i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000719 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000720 assert(MRegisterInfo::isVirtualRegister(reg) &&
721 "Can only allocate virtual registers!");
Chris Lattnerb9805782005-08-23 22:27:31 +0000722
723 // If this is not in a related reg class to the register we're allocating,
724 // don't check it.
725 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(reg);
726 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
727 reg = vrm_->getPhys(reg);
728 ++inactiveCounts[reg];
729 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
730 }
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000731 }
732
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000733 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000734
Chris Lattnerf8355d92005-08-22 16:55:22 +0000735 unsigned FreeReg = 0;
736 unsigned FreeRegInactiveCount = 0;
737
738 // Scan for the first available register.
739 TargetRegisterClass::iterator I = rc->allocation_order_begin(*mf_);
740 TargetRegisterClass::iterator E = rc->allocation_order_end(*mf_);
741 for (; I != E; ++I)
742 if (prt_->isRegAvail(*I)) {
743 FreeReg = *I;
744 FreeRegInactiveCount = inactiveCounts[FreeReg];
745 break;
746 }
747
748 // If there are no free regs, or if this reg has the max inactive count,
749 // return this register.
750 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
751
752 // Continue scanning the registers, looking for the one with the highest
753 // inactive count. Alkis found that this reduced register pressure very
754 // slightly on X86 (in rev 1.94 of this file), though this should probably be
755 // reevaluated now.
756 for (; I != E; ++I) {
757 unsigned Reg = *I;
758 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
759 FreeReg = Reg;
760 FreeRegInactiveCount = inactiveCounts[Reg];
761 if (FreeRegInactiveCount == MaxInactiveCount)
762 break; // We found the one with the max inactive count.
763 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000764 }
Chris Lattnerf8355d92005-08-22 16:55:22 +0000765
766 return FreeReg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000767}
768
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000769FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000770 return new RA();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000771}