blob: fbf669285c31dd0fd92ab1e5b7815e992fea6897 [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>
Duraid Madina30059612005-12-28 04:55:42 +000033#include <memory>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000034using namespace llvm;
35
36namespace {
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000037
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000038 Statistic<double> efficiency
39 ("regalloc", "Ratio of intervals processed over total intervals");
Chris Lattner19828d42004-11-18 03:49:30 +000040 Statistic<> NumBacktracks("regalloc", "Number of times we had to backtrack");
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000041
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000042 static unsigned numIterations = 0;
43 static unsigned numIntervals = 0;
Alkis Evlogimenosc1560952004-07-04 17:23:35 +000044
Chris Lattnercbb56252004-11-18 02:42:27 +000045 struct RA : public MachineFunctionPass {
46 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
47 typedef std::vector<IntervalPtr> IntervalPtrs;
48 private:
Chris Lattnerb9805782005-08-23 22:27:31 +000049 /// RelatedRegClasses - This structure is built the first time a function is
50 /// compiled, and keeps track of which register classes have registers that
51 /// belong to multiple classes or have aliases that are in other classes.
52 EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
53 std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
54
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000055 MachineFunction* mf_;
56 const TargetMachine* tm_;
57 const MRegisterInfo* mri_;
58 LiveIntervals* li_;
Chris Lattnerb0f31bf2005-01-23 22:45:13 +000059 bool *PhysRegsUsed;
Chris Lattnercbb56252004-11-18 02:42:27 +000060
61 /// handled_ - Intervals are added to the handled_ set in the order of their
62 /// start value. This is uses for backtracking.
63 std::vector<LiveInterval*> handled_;
64
65 /// fixed_ - Intervals that correspond to machine registers.
66 ///
67 IntervalPtrs fixed_;
68
69 /// active_ - Intervals that are currently being processed, and which have a
70 /// live range active for the current point.
71 IntervalPtrs active_;
72
73 /// inactive_ - Intervals that are currently being processed, but which have
74 /// a hold at the current point.
75 IntervalPtrs inactive_;
76
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000077 typedef std::priority_queue<LiveInterval*,
Chris Lattnercbb56252004-11-18 02:42:27 +000078 std::vector<LiveInterval*>,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000079 greater_ptr<LiveInterval> > IntervalHeap;
80 IntervalHeap unhandled_;
81 std::auto_ptr<PhysRegTracker> prt_;
82 std::auto_ptr<VirtRegMap> vrm_;
83 std::auto_ptr<Spiller> spiller_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000084
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 public:
86 virtual const char* getPassName() const {
87 return "Linear Scan Register Allocator";
88 }
89
90 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000091 AU.addRequired<LiveIntervals>();
92 MachineFunctionPass::getAnalysisUsage(AU);
93 }
94
95 /// runOnMachineFunction - register allocate the whole function
96 bool runOnMachineFunction(MachineFunction&);
97
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000098 private:
99 /// linearScan - the linear scan algorithm
100 void linearScan();
101
Chris Lattnercbb56252004-11-18 02:42:27 +0000102 /// initIntervalSets - initialize the interval sets.
103 ///
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000104 void initIntervalSets();
105
Chris Lattnercbb56252004-11-18 02:42:27 +0000106 /// processActiveIntervals - expire old intervals and move non-overlapping
107 /// ones to the inactive list.
108 void processActiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000109
Chris Lattnercbb56252004-11-18 02:42:27 +0000110 /// processInactiveIntervals - expire old intervals and move overlapping
111 /// ones to the active list.
112 void processInactiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000113
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000114 /// assignRegOrStackSlotAtInterval - assign a register if one
115 /// is available, or spill.
116 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
117
118 ///
119 /// register handling helpers
120 ///
121
Chris Lattnercbb56252004-11-18 02:42:27 +0000122 /// getFreePhysReg - return a free physical register for this virtual
123 /// register interval if we have one, otherwise return 0.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000124 unsigned getFreePhysReg(LiveInterval* cur);
125
126 /// assignVirt2StackSlot - assigns this virtual register to a
127 /// stack slot. returns the stack slot
128 int assignVirt2StackSlot(unsigned virtReg);
129
Chris Lattnerb9805782005-08-23 22:27:31 +0000130 void ComputeRelatedRegClasses();
131
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000132 template <typename ItTy>
133 void printIntervals(const char* const str, ItTy i, ItTy e) const {
134 if (str) std::cerr << str << " intervals:\n";
135 for (; i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000136 std::cerr << "\t" << *i->first << " -> ";
137 unsigned reg = i->first->reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000138 if (MRegisterInfo::isVirtualRegister(reg)) {
139 reg = vrm_->getPhys(reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000140 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000141 std::cerr << mri_->getName(reg) << '\n';
142 }
143 }
144 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000145}
146
Chris Lattnerb9805782005-08-23 22:27:31 +0000147void RA::ComputeRelatedRegClasses() {
148 const MRegisterInfo &MRI = *mri_;
149
150 // First pass, add all reg classes to the union, and determine at least one
151 // reg class that each register is in.
152 bool HasAliases = false;
153 for (MRegisterInfo::regclass_iterator RCI = MRI.regclass_begin(),
154 E = MRI.regclass_end(); RCI != E; ++RCI) {
155 RelatedRegClasses.insert(*RCI);
156 for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
157 I != E; ++I) {
158 HasAliases = HasAliases || *MRI.getAliasSet(*I) != 0;
159
160 const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
161 if (PRC) {
162 // Already processed this register. Just make sure we know that
163 // multiple register classes share a register.
164 RelatedRegClasses.unionSets(PRC, *RCI);
165 } else {
166 PRC = *RCI;
167 }
168 }
169 }
170
171 // Second pass, now that we know conservatively what register classes each reg
172 // belongs to, add info about aliases. We don't need to do this for targets
173 // without register aliases.
174 if (HasAliases)
175 for (std::map<unsigned, const TargetRegisterClass*>::iterator
176 I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
177 I != E; ++I)
178 for (const unsigned *AS = MRI.getAliasSet(I->first); *AS; ++AS)
179 RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
180}
181
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000182bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000183 mf_ = &fn;
184 tm_ = &fn.getTarget();
185 mri_ = tm_->getRegisterInfo();
186 li_ = &getAnalysis<LiveIntervals>();
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000187
Chris Lattnerb9805782005-08-23 22:27:31 +0000188 // If this is the first function compiled, compute the related reg classes.
189 if (RelatedRegClasses.empty())
190 ComputeRelatedRegClasses();
191
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000192 PhysRegsUsed = new bool[mri_->getNumRegs()];
193 std::fill(PhysRegsUsed, PhysRegsUsed+mri_->getNumRegs(), false);
194 fn.setUsedPhysRegs(PhysRegsUsed);
195
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000196 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
197 vrm_.reset(new VirtRegMap(*mf_));
198 if (!spiller_.get()) spiller_.reset(createSpiller());
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000199
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000200 initIntervalSets();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000201
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000202 linearScan();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000203
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000204 // Rewrite spill code and update the PhysRegsUsed set.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000205 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000206
Chris Lattner510a3ea2004-09-30 02:02:33 +0000207 vrm_.reset(); // Free the VirtRegMap
Chris Lattnercbb56252004-11-18 02:42:27 +0000208
209
210 while (!unhandled_.empty()) unhandled_.pop();
211 fixed_.clear();
212 active_.clear();
213 inactive_.clear();
214 handled_.clear();
215
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000216 return true;
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000217}
218
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000219/// initIntervalSets - initialize the interval sets.
220///
221void RA::initIntervalSets()
222{
223 assert(unhandled_.empty() && fixed_.empty() &&
224 active_.empty() && inactive_.empty() &&
225 "interval sets should be empty on initialization");
226
227 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000228 if (MRegisterInfo::isPhysicalRegister(i->second.reg)) {
229 PhysRegsUsed[i->second.reg] = true;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000230 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000231 } else
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000232 unhandled_.push(&i->second);
233 }
234}
235
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000236void RA::linearScan()
237{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000238 // linear scan algorithm
239 DEBUG(std::cerr << "********** LINEAR SCAN **********\n");
240 DEBUG(std::cerr << "********** Function: "
241 << mf_->getFunction()->getName() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000242
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000243 // DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
244 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
245 DEBUG(printIntervals("active", active_.begin(), active_.end()));
246 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
247
248 while (!unhandled_.empty()) {
249 // pick the interval with the earliest start point
250 LiveInterval* cur = unhandled_.top();
251 unhandled_.pop();
252 ++numIterations;
253 DEBUG(std::cerr << "\n*** CURRENT ***: " << *cur << '\n');
254
Chris Lattnercbb56252004-11-18 02:42:27 +0000255 processActiveIntervals(cur->beginNumber());
256 processInactiveIntervals(cur->beginNumber());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000257
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000258 assert(MRegisterInfo::isVirtualRegister(cur->reg) &&
259 "Can only allocate virtual registers!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000260
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000261 // Allocating a virtual register. try to find a free
262 // physical register or spill an interval (possibly this one) in order to
263 // assign it one.
264 assignRegOrStackSlotAtInterval(cur);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000265
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000266 DEBUG(printIntervals("active", active_.begin(), active_.end()));
267 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000268 }
269 numIntervals += li_->getNumIntervals();
270 efficiency = double(numIterations) / double(numIntervals);
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000271
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000272 // expire any remaining active intervals
273 for (IntervalPtrs::reverse_iterator
274 i = active_.rbegin(); i != active_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000275 unsigned reg = i->first->reg;
276 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000277 assert(MRegisterInfo::isVirtualRegister(reg) &&
278 "Can only allocate virtual registers!");
279 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000280 prt_->delRegUse(reg);
281 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
282 }
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000283
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000284 // expire any remaining inactive intervals
285 for (IntervalPtrs::reverse_iterator
286 i = inactive_.rbegin(); i != inactive_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000287 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000288 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
289 }
Alkis Evlogimenosb7be1152004-01-13 20:42:08 +0000290
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000291 DEBUG(std::cerr << *vrm_);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000292}
293
Chris Lattnercbb56252004-11-18 02:42:27 +0000294/// processActiveIntervals - expire old intervals and move non-overlapping ones
295/// to the inactive list.
296void RA::processActiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000297{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000298 DEBUG(std::cerr << "\tprocessing active intervals:\n");
Chris Lattner23b71c12004-11-18 01:29:39 +0000299
Chris Lattnercbb56252004-11-18 02:42:27 +0000300 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
301 LiveInterval *Interval = active_[i].first;
302 LiveInterval::iterator IntervalPos = active_[i].second;
303 unsigned reg = Interval->reg;
Alkis Evlogimenosed543732004-09-01 22:52:29 +0000304
Chris Lattnercbb56252004-11-18 02:42:27 +0000305 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
306
307 if (IntervalPos == Interval->end()) { // Remove expired intervals.
308 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000309 assert(MRegisterInfo::isVirtualRegister(reg) &&
310 "Can only allocate virtual registers!");
311 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000312 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000313
314 // Pop off the end of the list.
315 active_[i] = active_.back();
316 active_.pop_back();
317 --i; --e;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000318
Chris Lattnercbb56252004-11-18 02:42:27 +0000319 } else if (IntervalPos->start > CurPoint) {
320 // Move inactive intervals to inactive list.
321 DEBUG(std::cerr << "\t\tinterval " << *Interval << " inactive\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000322 assert(MRegisterInfo::isVirtualRegister(reg) &&
323 "Can only allocate virtual registers!");
324 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000325 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000326 // add to inactive.
327 inactive_.push_back(std::make_pair(Interval, IntervalPos));
328
329 // Pop off the end of the list.
330 active_[i] = active_.back();
331 active_.pop_back();
332 --i; --e;
333 } else {
334 // Otherwise, just update the iterator position.
335 active_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000336 }
337 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000338}
339
Chris Lattnercbb56252004-11-18 02:42:27 +0000340/// processInactiveIntervals - expire old intervals and move overlapping
341/// ones to the active list.
342void RA::processInactiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000343{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000344 DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
Chris Lattner365b95f2004-11-18 04:13:02 +0000345
Chris Lattnercbb56252004-11-18 02:42:27 +0000346 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
347 LiveInterval *Interval = inactive_[i].first;
348 LiveInterval::iterator IntervalPos = inactive_[i].second;
349 unsigned reg = Interval->reg;
Chris Lattner23b71c12004-11-18 01:29:39 +0000350
Chris Lattnercbb56252004-11-18 02:42:27 +0000351 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000352
Chris Lattnercbb56252004-11-18 02:42:27 +0000353 if (IntervalPos == Interval->end()) { // remove expired intervals.
354 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000355
Chris Lattnercbb56252004-11-18 02:42:27 +0000356 // Pop off the end of the list.
357 inactive_[i] = inactive_.back();
358 inactive_.pop_back();
359 --i; --e;
360 } else if (IntervalPos->start <= CurPoint) {
361 // move re-activated intervals in active list
362 DEBUG(std::cerr << "\t\tinterval " << *Interval << " active\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000363 assert(MRegisterInfo::isVirtualRegister(reg) &&
364 "Can only allocate virtual registers!");
365 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000366 prt_->addRegUse(reg);
367 // add to active
Chris Lattnercbb56252004-11-18 02:42:27 +0000368 active_.push_back(std::make_pair(Interval, IntervalPos));
369
370 // Pop off the end of the list.
371 inactive_[i] = inactive_.back();
372 inactive_.pop_back();
373 --i; --e;
374 } else {
375 // Otherwise, just update the iterator position.
376 inactive_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000377 }
378 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000379}
380
Chris Lattnercbb56252004-11-18 02:42:27 +0000381/// updateSpillWeights - updates the spill weights of the specifed physical
382/// register and its weight.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000383static void updateSpillWeights(std::vector<float> &Weights,
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000384 unsigned reg, float weight,
385 const MRegisterInfo *MRI) {
386 Weights[reg] += weight;
387 for (const unsigned* as = MRI->getAliasSet(reg); *as; ++as)
388 Weights[*as] += weight;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000389}
390
Chris Lattnercbb56252004-11-18 02:42:27 +0000391static RA::IntervalPtrs::iterator FindIntervalInVector(RA::IntervalPtrs &IP,
392 LiveInterval *LI) {
393 for (RA::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); I != E; ++I)
394 if (I->first == LI) return I;
395 return IP.end();
396}
397
Chris Lattner19828d42004-11-18 03:49:30 +0000398static void RevertVectorIteratorsTo(RA::IntervalPtrs &V, unsigned Point) {
399 for (unsigned i = 0, e = V.size(); i != e; ++i) {
400 RA::IntervalPtr &IP = V[i];
401 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
402 IP.second, Point);
403 if (I != IP.first->begin()) --I;
404 IP.second = I;
405 }
406}
Chris Lattnercbb56252004-11-18 02:42:27 +0000407
408
409/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
410/// spill.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000411void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000412{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000413 DEBUG(std::cerr << "\tallocating current interval: ");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000414
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000415 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000416
Chris Lattnera6c17502005-08-22 20:20:42 +0000417 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
Chris Lattner365b95f2004-11-18 04:13:02 +0000418 unsigned StartPosition = cur->beginNumber();
Chris Lattnerb9805782005-08-23 22:27:31 +0000419 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
420 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
421
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000422 // for every interval in inactive we overlap with, mark the
Chris Lattnera6c17502005-08-22 20:20:42 +0000423 // register as not free and update spill weights.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000424 for (IntervalPtrs::const_iterator i = inactive_.begin(),
425 e = inactive_.end(); i != e; ++i) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000426 unsigned Reg = i->first->reg;
427 assert(MRegisterInfo::isVirtualRegister(Reg) &&
428 "Can only allocate virtual registers!");
429 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(Reg);
430 // If this is not in a related reg class to the register we're allocating,
431 // don't check it.
432 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
433 cur->overlapsFrom(*i->first, i->second-1)) {
434 Reg = vrm_->getPhys(Reg);
435 prt_->addRegUse(Reg);
436 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000437 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000438 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000439
440 // Speculatively check to see if we can get a register right now. If not,
441 // we know we won't be able to by adding more constraints. If so, we can
442 // check to see if it is valid. Doing an exhaustive search of the fixed_ list
443 // is very bad (it contains all callee clobbered registers for any functions
444 // with a call), so we want to avoid doing that if possible.
445 unsigned physReg = getFreePhysReg(cur);
446 if (physReg) {
447 // We got a register. However, if it's in the fixed_ list, we might
Chris Lattnere836ad62005-08-30 21:03:36 +0000448 // conflict with it. Check to see if we conflict with it or any of its
449 // aliases.
450 std::set<unsigned> RegAliases;
451 for (const unsigned *AS = mri_->getAliasSet(physReg); *AS; ++AS)
452 RegAliases.insert(*AS);
453
Chris Lattnera411cbc2005-08-22 20:59:30 +0000454 bool ConflictsWithFixed = false;
455 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
Chris Lattnere836ad62005-08-30 21:03:36 +0000456 if (physReg == fixed_[i].first->reg ||
457 RegAliases.count(fixed_[i].first->reg)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000458 // Okay, this reg is on the fixed list. Check to see if we actually
459 // conflict.
460 IntervalPtr &IP = fixed_[i];
461 LiveInterval *I = IP.first;
462 if (I->endNumber() > StartPosition) {
463 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
464 IP.second = II;
465 if (II != I->begin() && II->start > StartPosition)
466 --II;
Chris Lattnere836ad62005-08-30 21:03:36 +0000467 if (cur->overlapsFrom(*I, II)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000468 ConflictsWithFixed = true;
Chris Lattnere836ad62005-08-30 21:03:36 +0000469 break;
470 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000471 }
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000472 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000473 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000474
475 // Okay, the register picked by our speculative getFreePhysReg call turned
476 // out to be in use. Actually add all of the conflicting fixed registers to
477 // prt so we can do an accurate query.
478 if (ConflictsWithFixed) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000479 // For every interval in fixed we overlap with, mark the register as not
480 // free and update spill weights.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000481 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
482 IntervalPtr &IP = fixed_[i];
483 LiveInterval *I = IP.first;
Chris Lattnerb9805782005-08-23 22:27:31 +0000484
485 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
486 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
487 I->endNumber() > StartPosition) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000488 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
489 IP.second = II;
490 if (II != I->begin() && II->start > StartPosition)
491 --II;
492 if (cur->overlapsFrom(*I, II)) {
493 unsigned reg = I->reg;
494 prt_->addRegUse(reg);
495 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
496 }
497 }
498 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000499
Chris Lattnera411cbc2005-08-22 20:59:30 +0000500 // Using the newly updated prt_ object, which includes conflicts in the
501 // future, see if there are any registers available.
502 physReg = getFreePhysReg(cur);
503 }
504 }
505
Chris Lattnera6c17502005-08-22 20:20:42 +0000506 // Restore the physical register tracker, removing information about the
507 // future.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000508 *prt_ = backupPrt;
Chris Lattnera6c17502005-08-22 20:20:42 +0000509
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000510 // if we find a free register, we are done: assign this virtual to
511 // the free physical register and add this interval to the active
512 // list.
513 if (physReg) {
514 DEBUG(std::cerr << mri_->getName(physReg) << '\n');
515 vrm_->assignVirt2Phys(cur->reg, physReg);
516 prt_->addRegUse(physReg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000517 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000518 handled_.push_back(cur);
519 return;
520 }
521 DEBUG(std::cerr << "no free registers\n");
522
Chris Lattnera6c17502005-08-22 20:20:42 +0000523 // Compile the spill weights into an array that is better for scanning.
524 std::vector<float> SpillWeights(mri_->getNumRegs(), 0.0);
525 for (std::vector<std::pair<unsigned, float> >::iterator
526 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
527 updateSpillWeights(SpillWeights, I->first, I->second, mri_);
528
529 // for each interval in active, update spill weights.
530 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
531 i != e; ++i) {
532 unsigned reg = i->first->reg;
533 assert(MRegisterInfo::isVirtualRegister(reg) &&
534 "Can only allocate virtual registers!");
535 reg = vrm_->getPhys(reg);
536 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
537 }
538
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000539 DEBUG(std::cerr << "\tassigning stack slot at interval "<< *cur << ":\n");
540
Chris Lattner5e5fb942005-01-08 19:53:50 +0000541 float minWeight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000542 unsigned minReg = 0;
Chris Lattnerb9805782005-08-23 22:27:31 +0000543 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
544 e = RC->allocation_order_end(*mf_); i != e; ++i) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000545 unsigned reg = *i;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000546 if (minWeight > SpillWeights[reg]) {
547 minWeight = SpillWeights[reg];
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000548 minReg = reg;
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000549 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000550 }
Duraid Madinae0b632a2005-11-21 14:09:40 +0000551// FIXME: assert(minReg && "Didn't find any reg!");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000552 DEBUG(std::cerr << "\t\tregister with min weight: "
553 << mri_->getName(minReg) << " (" << minWeight << ")\n");
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000554
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000555 // if the current has the minimum weight, we need to spill it and
556 // add any added intervals back to unhandled, and restart
557 // linearscan.
558 if (cur->weight <= minWeight) {
559 DEBUG(std::cerr << "\t\t\tspilling(c): " << *cur << '\n';);
560 int slot = vrm_->assignVirt2StackSlot(cur->reg);
561 std::vector<LiveInterval*> added =
562 li_->addIntervalsForSpills(*cur, *vrm_, slot);
563 if (added.empty())
564 return; // Early exit if all spills were folded.
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000565
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000566 // Merge added with unhandled. Note that we know that
567 // addIntervalsForSpills returns intervals sorted by their starting
568 // point.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000569 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000570 unhandled_.push(added[i]);
571 return;
572 }
573
Chris Lattner19828d42004-11-18 03:49:30 +0000574 ++NumBacktracks;
575
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000576 // push the current interval back to unhandled since we are going
577 // to re-run at least this iteration. Since we didn't modify it it
578 // should go back right in the front of the list
579 unhandled_.push(cur);
580
581 // otherwise we spill all intervals aliasing the register with
582 // minimum weight, rollback to the interval with the earliest
583 // start point and let the linear scan algorithm run again
584 std::vector<LiveInterval*> added;
585 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
586 "did not choose a register to spill?");
587 std::vector<bool> toSpill(mri_->getNumRegs(), false);
Chris Lattner19828d42004-11-18 03:49:30 +0000588
589 // We are going to spill minReg and all its aliases.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000590 toSpill[minReg] = true;
591 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
592 toSpill[*as] = true;
593
594 // the earliest start of a spilled interval indicates up to where
595 // in handled we need to roll back
Chris Lattner23b71c12004-11-18 01:29:39 +0000596 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000597
598 // set of spilled vregs (used later to rollback properly)
599 std::set<unsigned> spilled;
600
Chris Lattner19828d42004-11-18 03:49:30 +0000601 // spill live intervals of virtual regs mapped to the physical register we
602 // want to clear (and its aliases). We only spill those that overlap with the
603 // current interval as the rest do not affect its allocation. we also keep
604 // track of the earliest start of all spilled live intervals since this will
605 // mark our rollback point.
606 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000607 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000608 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000609 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000610 cur->overlapsFrom(*i->first, i->second)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000611 DEBUG(std::cerr << "\t\t\tspilling(a): " << *i->first << '\n');
612 earliestStart = std::min(earliestStart, i->first->beginNumber());
613 int slot = vrm_->assignVirt2StackSlot(i->first->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000614 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000615 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000616 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
617 spilled.insert(reg);
618 }
619 }
Chris Lattner19828d42004-11-18 03:49:30 +0000620 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnercbb56252004-11-18 02:42:27 +0000621 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000622 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000623 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000624 cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000625 DEBUG(std::cerr << "\t\t\tspilling(i): " << *i->first << '\n');
626 earliestStart = std::min(earliestStart, i->first->beginNumber());
627 int slot = vrm_->assignVirt2StackSlot(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000628 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000629 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000630 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
631 spilled.insert(reg);
632 }
633 }
634
635 DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
Chris Lattnercbb56252004-11-18 02:42:27 +0000636
637 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000638 // spilled live interval and undo each one, restoring the state of
Chris Lattnercbb56252004-11-18 02:42:27 +0000639 // unhandled.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000640 while (!handled_.empty()) {
641 LiveInterval* i = handled_.back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000642 // If this interval starts before t we are done.
Chris Lattner23b71c12004-11-18 01:29:39 +0000643 if (i->beginNumber() < earliestStart)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000644 break;
645 DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
646 handled_.pop_back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000647
648 // When undoing a live interval allocation we must know if it is active or
649 // inactive to properly update the PhysRegTracker and the VirtRegMap.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000650 IntervalPtrs::iterator it;
Chris Lattnercbb56252004-11-18 02:42:27 +0000651 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000652 active_.erase(it);
653 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000654 assert(0 && "daksjlfd");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000655 prt_->delRegUse(i->reg);
656 unhandled_.push(i);
Chris Lattnercbb56252004-11-18 02:42:27 +0000657 } else {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000658 if (!spilled.count(i->reg))
659 unhandled_.push(i);
660 prt_->delRegUse(vrm_->getPhys(i->reg));
661 vrm_->clearVirt(i->reg);
662 }
Chris Lattnercbb56252004-11-18 02:42:27 +0000663 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000664 inactive_.erase(it);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000665 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
666 assert(0 && "daksjlfd");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000667 unhandled_.push(i);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000668 } else {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000669 if (!spilled.count(i->reg))
670 unhandled_.push(i);
671 vrm_->clearVirt(i->reg);
672 }
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000673 } else {
674 assert(MRegisterInfo::isVirtualRegister(i->reg) &&
675 "Can only allocate virtual registers!");
676 vrm_->clearVirt(i->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000677 unhandled_.push(i);
678 }
679 }
680
Chris Lattner19828d42004-11-18 03:49:30 +0000681 // Rewind the iterators in the active, inactive, and fixed lists back to the
682 // point we reverted to.
683 RevertVectorIteratorsTo(active_, earliestStart);
684 RevertVectorIteratorsTo(inactive_, earliestStart);
685 RevertVectorIteratorsTo(fixed_, earliestStart);
686
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000687 // scan the rest and undo each interval that expired after t and
688 // insert it in active (the next iteration of the algorithm will
689 // put it in inactive if required)
Chris Lattnercbb56252004-11-18 02:42:27 +0000690 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
691 LiveInterval *HI = handled_[i];
692 if (!HI->expiredAt(earliestStart) &&
693 HI->expiredAt(cur->beginNumber())) {
694 DEBUG(std::cerr << "\t\t\tundo changes for: " << *HI << '\n');
695 active_.push_back(std::make_pair(HI, HI->begin()));
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000696 if (MRegisterInfo::isPhysicalRegister(HI->reg)) {
697 assert(0 &&"sdflkajsdf");
Chris Lattnercbb56252004-11-18 02:42:27 +0000698 prt_->addRegUse(HI->reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000699 } else
Chris Lattnercbb56252004-11-18 02:42:27 +0000700 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000701 }
702 }
703
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000704 // merge added with unhandled
705 for (unsigned i = 0, e = added.size(); i != e; ++i)
706 unhandled_.push(added[i]);
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000707}
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000708
Chris Lattnercbb56252004-11-18 02:42:27 +0000709/// getFreePhysReg - return a free physical register for this virtual register
710/// interval if we have one, otherwise return 0.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000711unsigned RA::getFreePhysReg(LiveInterval* cur)
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000712{
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000713 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
Chris Lattnerf8355d92005-08-22 16:55:22 +0000714 unsigned MaxInactiveCount = 0;
715
Chris Lattnerb9805782005-08-23 22:27:31 +0000716 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
717 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
718
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000719 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
720 i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000721 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000722 assert(MRegisterInfo::isVirtualRegister(reg) &&
723 "Can only allocate virtual registers!");
Chris Lattnerb9805782005-08-23 22:27:31 +0000724
725 // If this is not in a related reg class to the register we're allocating,
726 // don't check it.
727 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(reg);
728 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
729 reg = vrm_->getPhys(reg);
730 ++inactiveCounts[reg];
731 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
732 }
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000733 }
734
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000735 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000736
Chris Lattnerf8355d92005-08-22 16:55:22 +0000737 unsigned FreeReg = 0;
738 unsigned FreeRegInactiveCount = 0;
739
740 // Scan for the first available register.
741 TargetRegisterClass::iterator I = rc->allocation_order_begin(*mf_);
742 TargetRegisterClass::iterator E = rc->allocation_order_end(*mf_);
743 for (; I != E; ++I)
744 if (prt_->isRegAvail(*I)) {
745 FreeReg = *I;
746 FreeRegInactiveCount = inactiveCounts[FreeReg];
747 break;
748 }
749
750 // If there are no free regs, or if this reg has the max inactive count,
751 // return this register.
752 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
753
754 // Continue scanning the registers, looking for the one with the highest
755 // inactive count. Alkis found that this reduced register pressure very
756 // slightly on X86 (in rev 1.94 of this file), though this should probably be
757 // reevaluated now.
758 for (; I != E; ++I) {
759 unsigned Reg = *I;
760 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
761 FreeReg = Reg;
762 FreeRegInactiveCount = inactiveCounts[Reg];
763 if (FreeRegInactiveCount == MaxInactiveCount)
764 break; // We found the one with the max inactive count.
765 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000766 }
Chris Lattnerf8355d92005-08-22 16:55:22 +0000767
768 return FreeReg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000769}
770
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000771FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000772 return new RA();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000773}