blob: 0cb30430a37b92faa76bf22d71f354e2f846b15f [file] [log] [blame]
Alkis Evlogimenos0e9ded72003-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 Evlogimenos1dd872c2004-02-24 08:58:30 +000013
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +000014#define DEBUG_TYPE "regalloc"
15#include "llvm/Function.h"
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/CodeGen/SSARegMap.h"
20#include "llvm/Target/MRegisterInfo.h"
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +000021#include "llvm/Target/TargetMachine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/Debug.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/STLExtras.h"
Chris Lattner85638332004-07-23 17:56:30 +000025#include "LiveIntervalAnalysis.h"
Alkis Evlogimenos14108592004-02-23 00:53:31 +000026#include "PhysRegTracker.h"
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000027#include "VirtRegMap.h"
Alkis Evlogimenos2c5ddd22004-02-15 10:24:21 +000028#include <algorithm>
Alkis Evlogimenos2a54b5d2004-05-08 03:50:03 +000029#include <cmath>
Alkis Evlogimenosa5268e82004-05-30 07:24:39 +000030#include <set>
Alkis Evlogimenos1a876fa2004-07-22 08:14:44 +000031#include <queue>
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +000032using namespace llvm;
33
34namespace {
Alkis Evlogimenos8f3cc032004-07-04 07:59:06 +000035
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000036 Statistic<double> efficiency
37 ("regalloc", "Ratio of intervals processed over total intervals");
Chris Lattner850852c2004-11-18 03:49:30 +000038 Statistic<> NumBacktracks("regalloc", "Number of times we had to backtrack");
Alkis Evlogimenos8f3cc032004-07-04 07:59:06 +000039
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000040 static unsigned numIterations = 0;
41 static unsigned numIntervals = 0;
Alkis Evlogimenos21b3a5b2004-07-04 17:23:35 +000042
Chris Lattnera1f77792004-11-18 02:42:27 +000043 struct RA : public MachineFunctionPass {
44 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
45 typedef std::vector<IntervalPtr> IntervalPtrs;
46 private:
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000047 MachineFunction* mf_;
48 const TargetMachine* tm_;
49 const MRegisterInfo* mri_;
50 LiveIntervals* li_;
Chris Lattnera1f77792004-11-18 02:42:27 +000051
52 /// handled_ - Intervals are added to the handled_ set in the order of their
53 /// start value. This is uses for backtracking.
54 std::vector<LiveInterval*> handled_;
55
56 /// fixed_ - Intervals that correspond to machine registers.
57 ///
58 IntervalPtrs fixed_;
59
60 /// active_ - Intervals that are currently being processed, and which have a
61 /// live range active for the current point.
62 IntervalPtrs active_;
63
64 /// inactive_ - Intervals that are currently being processed, but which have
65 /// a hold at the current point.
66 IntervalPtrs inactive_;
67
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000068 typedef std::priority_queue<LiveInterval*,
Chris Lattnera1f77792004-11-18 02:42:27 +000069 std::vector<LiveInterval*>,
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000070 greater_ptr<LiveInterval> > IntervalHeap;
71 IntervalHeap unhandled_;
72 std::auto_ptr<PhysRegTracker> prt_;
73 std::auto_ptr<VirtRegMap> vrm_;
74 std::auto_ptr<Spiller> spiller_;
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +000075
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000076 typedef std::vector<float> SpillWeights;
77 SpillWeights spillWeights_;
Alkis Evlogimenose82a7072004-02-06 18:08:18 +000078
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000079 public:
80 virtual const char* getPassName() const {
81 return "Linear Scan Register Allocator";
82 }
83
84 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000085 AU.addRequired<LiveIntervals>();
86 MachineFunctionPass::getAnalysisUsage(AU);
87 }
88
89 /// runOnMachineFunction - register allocate the whole function
90 bool runOnMachineFunction(MachineFunction&);
91
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000092 private:
93 /// linearScan - the linear scan algorithm
94 void linearScan();
95
Chris Lattnera1f77792004-11-18 02:42:27 +000096 /// initIntervalSets - initialize the interval sets.
97 ///
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000098 void initIntervalSets();
99
Chris Lattnera1f77792004-11-18 02:42:27 +0000100 /// processActiveIntervals - expire old intervals and move non-overlapping
101 /// ones to the inactive list.
102 void processActiveIntervals(unsigned CurPoint);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000103
Chris Lattnera1f77792004-11-18 02:42:27 +0000104 /// processInactiveIntervals - expire old intervals and move overlapping
105 /// ones to the active list.
106 void processInactiveIntervals(unsigned CurPoint);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000107
108 /// updateSpillWeights - updates the spill weights of the
Chris Lattnera1f77792004-11-18 02:42:27 +0000109 /// specifed physical register and its weight.
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000110 void updateSpillWeights(unsigned reg, SpillWeights::value_type weight);
111
112 /// assignRegOrStackSlotAtInterval - assign a register if one
113 /// is available, or spill.
114 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
115
116 ///
117 /// register handling helpers
118 ///
119
Chris Lattnera1f77792004-11-18 02:42:27 +0000120 /// getFreePhysReg - return a free physical register for this virtual
121 /// register interval if we have one, otherwise return 0.
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000122 unsigned getFreePhysReg(LiveInterval* cur);
123
124 /// assignVirt2StackSlot - assigns this virtual register to a
125 /// stack slot. returns the stack slot
126 int assignVirt2StackSlot(unsigned virtReg);
127
128 template <typename ItTy>
129 void printIntervals(const char* const str, ItTy i, ItTy e) const {
130 if (str) std::cerr << str << " intervals:\n";
131 for (; i != e; ++i) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000132 std::cerr << "\t" << *i->first << " -> ";
133 unsigned reg = i->first->reg;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000134 if (MRegisterInfo::isVirtualRegister(reg)) {
135 reg = vrm_->getPhys(reg);
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000136 }
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000137 std::cerr << mri_->getName(reg) << '\n';
138 }
139 }
140 };
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000141}
142
143bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000144 mf_ = &fn;
145 tm_ = &fn.getTarget();
146 mri_ = tm_->getRegisterInfo();
147 li_ = &getAnalysis<LiveIntervals>();
148 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
149 vrm_.reset(new VirtRegMap(*mf_));
150 if (!spiller_.get()) spiller_.reset(createSpiller());
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000151
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000152 initIntervalSets();
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000153
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000154 linearScan();
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000155
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000156 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000157
Chris Lattnerddd52292004-09-30 02:02:33 +0000158 vrm_.reset(); // Free the VirtRegMap
Chris Lattnera1f77792004-11-18 02:42:27 +0000159
160
161 while (!unhandled_.empty()) unhandled_.pop();
162 fixed_.clear();
163 active_.clear();
164 inactive_.clear();
165 handled_.clear();
166
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000167 return true;
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000168}
169
170void RA::linearScan()
171{
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000172 // linear scan algorithm
173 DEBUG(std::cerr << "********** LINEAR SCAN **********\n");
174 DEBUG(std::cerr << "********** Function: "
175 << mf_->getFunction()->getName() << '\n');
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000176
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000177 // DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
178 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
179 DEBUG(printIntervals("active", active_.begin(), active_.end()));
180 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
181
182 while (!unhandled_.empty()) {
183 // pick the interval with the earliest start point
184 LiveInterval* cur = unhandled_.top();
185 unhandled_.pop();
186 ++numIterations;
187 DEBUG(std::cerr << "\n*** CURRENT ***: " << *cur << '\n');
188
Chris Lattnera1f77792004-11-18 02:42:27 +0000189 processActiveIntervals(cur->beginNumber());
190 processInactiveIntervals(cur->beginNumber());
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000191
192 // if this register is fixed we are done
193 if (MRegisterInfo::isPhysicalRegister(cur->reg)) {
194 prt_->addRegUse(cur->reg);
Chris Lattnera1f77792004-11-18 02:42:27 +0000195 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000196 handled_.push_back(cur);
Chris Lattnera1f77792004-11-18 02:42:27 +0000197 } else {
198 // otherwise we are allocating a virtual register. try to find a free
Chris Lattner850852c2004-11-18 03:49:30 +0000199 // physical register or spill an interval (possibly this one) in order to
200 // assign it one.
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000201 assignRegOrStackSlotAtInterval(cur);
202 }
203
Alkis Evlogimenos76eca062004-02-20 06:15:40 +0000204 DEBUG(printIntervals("active", active_.begin(), active_.end()));
205 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000206 }
207 numIntervals += li_->getNumIntervals();
208 efficiency = double(numIterations) / double(numIntervals);
Alkis Evlogimenosae5b3d42004-01-07 09:20:58 +0000209
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000210 // expire any remaining active intervals
211 for (IntervalPtrs::reverse_iterator
212 i = active_.rbegin(); i != active_.rend(); ) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000213 unsigned reg = i->first->reg;
214 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000215 if (MRegisterInfo::isVirtualRegister(reg))
216 reg = vrm_->getPhys(reg);
217 prt_->delRegUse(reg);
218 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
219 }
Alkis Evlogimenosae5b3d42004-01-07 09:20:58 +0000220
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000221 // expire any remaining inactive intervals
222 for (IntervalPtrs::reverse_iterator
223 i = inactive_.rbegin(); i != inactive_.rend(); ) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000224 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000225 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
226 }
Alkis Evlogimenos65bc9902004-01-13 20:42:08 +0000227
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000228 DEBUG(std::cerr << *vrm_);
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000229}
230
Chris Lattnera1f77792004-11-18 02:42:27 +0000231/// initIntervalSets - initialize the interval sets.
232///
Chris Lattnerc51866a2004-07-24 03:32:06 +0000233void RA::initIntervalSets()
Alkis Evlogimenosae5b3d42004-01-07 09:20:58 +0000234{
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000235 assert(unhandled_.empty() && fixed_.empty() &&
236 active_.empty() && inactive_.empty() &&
237 "interval sets should be empty on initialization");
Alkis Evlogimenosae5b3d42004-01-07 09:20:58 +0000238
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000239 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i){
240 unhandled_.push(&i->second);
241 if (MRegisterInfo::isPhysicalRegister(i->second.reg))
Chris Lattnera1f77792004-11-18 02:42:27 +0000242 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000243 }
Alkis Evlogimenosae5b3d42004-01-07 09:20:58 +0000244}
245
Chris Lattnera1f77792004-11-18 02:42:27 +0000246/// processActiveIntervals - expire old intervals and move non-overlapping ones
247/// to the inactive list.
248void RA::processActiveIntervals(unsigned CurPoint)
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000249{
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000250 DEBUG(std::cerr << "\tprocessing active intervals:\n");
Chris Lattnera51f5ee2004-11-18 01:29:39 +0000251
Chris Lattnera1f77792004-11-18 02:42:27 +0000252 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
253 LiveInterval *Interval = active_[i].first;
254 LiveInterval::iterator IntervalPos = active_[i].second;
255 unsigned reg = Interval->reg;
Alkis Evlogimenosfae8f6a2004-09-01 22:52:29 +0000256
Chris Lattnera1f77792004-11-18 02:42:27 +0000257 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
258
259 if (IntervalPos == Interval->end()) { // Remove expired intervals.
260 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000261 if (MRegisterInfo::isVirtualRegister(reg))
262 reg = vrm_->getPhys(reg);
263 prt_->delRegUse(reg);
Chris Lattnera1f77792004-11-18 02:42:27 +0000264
265 // Pop off the end of the list.
266 active_[i] = active_.back();
267 active_.pop_back();
268 --i; --e;
269
270 } else if (IntervalPos->start > CurPoint) {
271 // Move inactive intervals to inactive list.
272 DEBUG(std::cerr << "\t\tinterval " << *Interval << " inactive\n");
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000273 if (MRegisterInfo::isVirtualRegister(reg))
274 reg = vrm_->getPhys(reg);
275 prt_->delRegUse(reg);
Chris Lattnera1f77792004-11-18 02:42:27 +0000276 // add to inactive.
277 inactive_.push_back(std::make_pair(Interval, IntervalPos));
278
279 // Pop off the end of the list.
280 active_[i] = active_.back();
281 active_.pop_back();
282 --i; --e;
283 } else {
284 // Otherwise, just update the iterator position.
285 active_[i].second = IntervalPos;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000286 }
287 }
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000288}
289
Chris Lattnera1f77792004-11-18 02:42:27 +0000290/// processInactiveIntervals - expire old intervals and move overlapping
291/// ones to the active list.
292void RA::processInactiveIntervals(unsigned CurPoint)
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000293{
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000294 DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
Chris Lattner49ff5f02004-11-18 04:13:02 +0000295
Chris Lattnera1f77792004-11-18 02:42:27 +0000296 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
297 LiveInterval *Interval = inactive_[i].first;
298 LiveInterval::iterator IntervalPos = inactive_[i].second;
299 unsigned reg = Interval->reg;
Chris Lattnera51f5ee2004-11-18 01:29:39 +0000300
Chris Lattnera1f77792004-11-18 02:42:27 +0000301 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
302
303 if (IntervalPos == Interval->end()) { // remove expired intervals.
304 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000305
Chris Lattnera1f77792004-11-18 02:42:27 +0000306 // Pop off the end of the list.
307 inactive_[i] = inactive_.back();
308 inactive_.pop_back();
309 --i; --e;
310 } else if (IntervalPos->start <= CurPoint) {
311 // move re-activated intervals in active list
312 DEBUG(std::cerr << "\t\tinterval " << *Interval << " active\n");
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000313 if (MRegisterInfo::isVirtualRegister(reg))
314 reg = vrm_->getPhys(reg);
315 prt_->addRegUse(reg);
316 // add to active
Chris Lattnera1f77792004-11-18 02:42:27 +0000317 active_.push_back(std::make_pair(Interval, IntervalPos));
318
319 // Pop off the end of the list.
320 inactive_[i] = inactive_.back();
321 inactive_.pop_back();
322 --i; --e;
323 } else {
324 // Otherwise, just update the iterator position.
325 inactive_[i].second = IntervalPos;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000326 }
327 }
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000328}
329
Chris Lattnera1f77792004-11-18 02:42:27 +0000330/// updateSpillWeights - updates the spill weights of the specifed physical
331/// register and its weight.
Alkis Evlogimenose82a7072004-02-06 18:08:18 +0000332void RA::updateSpillWeights(unsigned reg, SpillWeights::value_type weight)
333{
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000334 spillWeights_[reg] += weight;
335 for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as)
336 spillWeights_[*as] += weight;
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000337}
338
Chris Lattnera1f77792004-11-18 02:42:27 +0000339static RA::IntervalPtrs::iterator FindIntervalInVector(RA::IntervalPtrs &IP,
340 LiveInterval *LI) {
341 for (RA::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); I != E; ++I)
342 if (I->first == LI) return I;
343 return IP.end();
344}
345
Chris Lattner850852c2004-11-18 03:49:30 +0000346static void RevertVectorIteratorsTo(RA::IntervalPtrs &V, unsigned Point) {
347 for (unsigned i = 0, e = V.size(); i != e; ++i) {
348 RA::IntervalPtr &IP = V[i];
349 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
350 IP.second, Point);
351 if (I != IP.first->begin()) --I;
352 IP.second = I;
353 }
354}
Chris Lattnera1f77792004-11-18 02:42:27 +0000355
356
357/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
358/// spill.
Alkis Evlogimenos1a876fa2004-07-22 08:14:44 +0000359void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000360{
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000361 DEBUG(std::cerr << "\tallocating current interval: ");
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000362
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000363 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000364
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000365 spillWeights_.assign(mri_->getNumRegs(), 0.0);
Alkis Evlogimenose82a7072004-02-06 18:08:18 +0000366
Chris Lattner49ff5f02004-11-18 04:13:02 +0000367 unsigned StartPosition = cur->beginNumber();
368
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000369 // for each interval in active update spill weights
370 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
371 i != e; ++i) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000372 unsigned reg = i->first->reg;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000373 if (MRegisterInfo::isVirtualRegister(reg))
374 reg = vrm_->getPhys(reg);
Chris Lattnera1f77792004-11-18 02:42:27 +0000375 updateSpillWeights(reg, i->first->weight);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000376 }
377
378 // for every interval in inactive we overlap with, mark the
379 // register as not free and update spill weights
380 for (IntervalPtrs::const_iterator i = inactive_.begin(),
381 e = inactive_.end(); i != e; ++i) {
Chris Lattner850852c2004-11-18 03:49:30 +0000382 if (cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000383 unsigned reg = i->first->reg;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000384 if (MRegisterInfo::isVirtualRegister(reg))
385 reg = vrm_->getPhys(reg);
386 prt_->addRegUse(reg);
Chris Lattnera1f77792004-11-18 02:42:27 +0000387 updateSpillWeights(reg, i->first->weight);
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000388 }
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000389 }
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000390
Chris Lattner850852c2004-11-18 03:49:30 +0000391 // For every interval in fixed we overlap with, mark the register as not free
392 // and update spill weights.
Chris Lattner49ff5f02004-11-18 04:13:02 +0000393 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
394 IntervalPtr &IP = fixed_[i];
395 LiveInterval *I = IP.first;
396 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
397 IP.second = II;
398 if (cur->overlapsFrom(*I, II)) {
399 unsigned reg = I->reg;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000400 prt_->addRegUse(reg);
Chris Lattner49ff5f02004-11-18 04:13:02 +0000401 updateSpillWeights(reg, I->weight);
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000402 }
Chris Lattner49ff5f02004-11-18 04:13:02 +0000403 }
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000404
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000405 unsigned physReg = getFreePhysReg(cur);
406 // restore the physical register tracker
407 *prt_ = backupPrt;
408 // if we find a free register, we are done: assign this virtual to
409 // the free physical register and add this interval to the active
410 // list.
411 if (physReg) {
412 DEBUG(std::cerr << mri_->getName(physReg) << '\n');
413 vrm_->assignVirt2Phys(cur->reg, physReg);
414 prt_->addRegUse(physReg);
Chris Lattnera1f77792004-11-18 02:42:27 +0000415 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000416 handled_.push_back(cur);
417 return;
418 }
419 DEBUG(std::cerr << "no free registers\n");
420
421 DEBUG(std::cerr << "\tassigning stack slot at interval "<< *cur << ":\n");
422
423 float minWeight = HUGE_VAL;
424 unsigned minReg = 0;
425 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
426 for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_);
427 i != rc->allocation_order_end(*mf_); ++i) {
428 unsigned reg = *i;
429 if (minWeight > spillWeights_[reg]) {
430 minWeight = spillWeights_[reg];
431 minReg = reg;
Alkis Evlogimenos7d7d7e82003-12-23 18:00:33 +0000432 }
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000433 }
434 DEBUG(std::cerr << "\t\tregister with min weight: "
435 << mri_->getName(minReg) << " (" << minWeight << ")\n");
Alkis Evlogimenos7d7d7e82003-12-23 18:00:33 +0000436
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000437 // if the current has the minimum weight, we need to spill it and
438 // add any added intervals back to unhandled, and restart
439 // linearscan.
440 if (cur->weight <= minWeight) {
441 DEBUG(std::cerr << "\t\t\tspilling(c): " << *cur << '\n';);
442 int slot = vrm_->assignVirt2StackSlot(cur->reg);
443 std::vector<LiveInterval*> added =
444 li_->addIntervalsForSpills(*cur, *vrm_, slot);
445 if (added.empty())
446 return; // Early exit if all spills were folded.
Alkis Evlogimenose82a7072004-02-06 18:08:18 +0000447
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000448 // Merge added with unhandled. Note that we know that
449 // addIntervalsForSpills returns intervals sorted by their starting
450 // point.
Alkis Evlogimenos1a876fa2004-07-22 08:14:44 +0000451 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000452 unhandled_.push(added[i]);
453 return;
454 }
455
Chris Lattner850852c2004-11-18 03:49:30 +0000456 ++NumBacktracks;
457
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000458 // push the current interval back to unhandled since we are going
459 // to re-run at least this iteration. Since we didn't modify it it
460 // should go back right in the front of the list
461 unhandled_.push(cur);
462
463 // otherwise we spill all intervals aliasing the register with
464 // minimum weight, rollback to the interval with the earliest
465 // start point and let the linear scan algorithm run again
466 std::vector<LiveInterval*> added;
467 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
468 "did not choose a register to spill?");
469 std::vector<bool> toSpill(mri_->getNumRegs(), false);
Chris Lattner850852c2004-11-18 03:49:30 +0000470
471 // We are going to spill minReg and all its aliases.
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000472 toSpill[minReg] = true;
473 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
474 toSpill[*as] = true;
475
476 // the earliest start of a spilled interval indicates up to where
477 // in handled we need to roll back
Chris Lattnera51f5ee2004-11-18 01:29:39 +0000478 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000479
480 // set of spilled vregs (used later to rollback properly)
481 std::set<unsigned> spilled;
482
Chris Lattner850852c2004-11-18 03:49:30 +0000483 // spill live intervals of virtual regs mapped to the physical register we
484 // want to clear (and its aliases). We only spill those that overlap with the
485 // current interval as the rest do not affect its allocation. we also keep
486 // track of the earliest start of all spilled live intervals since this will
487 // mark our rollback point.
488 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000489 unsigned reg = i->first->reg;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000490 if (MRegisterInfo::isVirtualRegister(reg) &&
491 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner850852c2004-11-18 03:49:30 +0000492 cur->overlapsFrom(*i->first, i->second)) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000493 DEBUG(std::cerr << "\t\t\tspilling(a): " << *i->first << '\n');
494 earliestStart = std::min(earliestStart, i->first->beginNumber());
495 int slot = vrm_->assignVirt2StackSlot(i->first->reg);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000496 std::vector<LiveInterval*> newIs =
Chris Lattnera1f77792004-11-18 02:42:27 +0000497 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000498 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
499 spilled.insert(reg);
500 }
501 }
Chris Lattner850852c2004-11-18 03:49:30 +0000502 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnera1f77792004-11-18 02:42:27 +0000503 unsigned reg = i->first->reg;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000504 if (MRegisterInfo::isVirtualRegister(reg) &&
505 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner850852c2004-11-18 03:49:30 +0000506 cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000507 DEBUG(std::cerr << "\t\t\tspilling(i): " << *i->first << '\n');
508 earliestStart = std::min(earliestStart, i->first->beginNumber());
509 int slot = vrm_->assignVirt2StackSlot(reg);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000510 std::vector<LiveInterval*> newIs =
Chris Lattnera1f77792004-11-18 02:42:27 +0000511 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000512 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
513 spilled.insert(reg);
514 }
515 }
516
517 DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
Chris Lattnera1f77792004-11-18 02:42:27 +0000518
519 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000520 // spilled live interval and undo each one, restoring the state of
Chris Lattnera1f77792004-11-18 02:42:27 +0000521 // unhandled.
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000522 while (!handled_.empty()) {
523 LiveInterval* i = handled_.back();
Chris Lattnera1f77792004-11-18 02:42:27 +0000524 // If this interval starts before t we are done.
Chris Lattnera51f5ee2004-11-18 01:29:39 +0000525 if (i->beginNumber() < earliestStart)
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000526 break;
527 DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
528 handled_.pop_back();
Chris Lattnera1f77792004-11-18 02:42:27 +0000529
530 // When undoing a live interval allocation we must know if it is active or
531 // inactive to properly update the PhysRegTracker and the VirtRegMap.
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000532 IntervalPtrs::iterator it;
Chris Lattnera1f77792004-11-18 02:42:27 +0000533 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000534 active_.erase(it);
535 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
536 prt_->delRegUse(i->reg);
537 unhandled_.push(i);
Chris Lattnera1f77792004-11-18 02:42:27 +0000538 } else {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000539 if (!spilled.count(i->reg))
540 unhandled_.push(i);
541 prt_->delRegUse(vrm_->getPhys(i->reg));
542 vrm_->clearVirt(i->reg);
543 }
Chris Lattnera1f77792004-11-18 02:42:27 +0000544 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000545 inactive_.erase(it);
546 if (MRegisterInfo::isPhysicalRegister(i->reg))
547 unhandled_.push(i);
548 else {
549 if (!spilled.count(i->reg))
550 unhandled_.push(i);
551 vrm_->clearVirt(i->reg);
552 }
553 }
554 else {
555 if (MRegisterInfo::isVirtualRegister(i->reg))
556 vrm_->clearVirt(i->reg);
557 unhandled_.push(i);
558 }
559 }
560
Chris Lattner850852c2004-11-18 03:49:30 +0000561 // Rewind the iterators in the active, inactive, and fixed lists back to the
562 // point we reverted to.
563 RevertVectorIteratorsTo(active_, earliestStart);
564 RevertVectorIteratorsTo(inactive_, earliestStart);
565 RevertVectorIteratorsTo(fixed_, earliestStart);
566
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000567 // scan the rest and undo each interval that expired after t and
568 // insert it in active (the next iteration of the algorithm will
569 // put it in inactive if required)
Chris Lattnera1f77792004-11-18 02:42:27 +0000570 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
571 LiveInterval *HI = handled_[i];
572 if (!HI->expiredAt(earliestStart) &&
573 HI->expiredAt(cur->beginNumber())) {
574 DEBUG(std::cerr << "\t\t\tundo changes for: " << *HI << '\n');
575 active_.push_back(std::make_pair(HI, HI->begin()));
576 if (MRegisterInfo::isPhysicalRegister(HI->reg))
577 prt_->addRegUse(HI->reg);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000578 else
Chris Lattnera1f77792004-11-18 02:42:27 +0000579 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000580 }
581 }
582
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000583 // merge added with unhandled
584 for (unsigned i = 0, e = added.size(); i != e; ++i)
585 unhandled_.push(added[i]);
Alkis Evlogimenos2c5ddd22004-02-15 10:24:21 +0000586}
Alkis Evlogimenose82a7072004-02-06 18:08:18 +0000587
Chris Lattnera1f77792004-11-18 02:42:27 +0000588/// getFreePhysReg - return a free physical register for this virtual register
589/// interval if we have one, otherwise return 0.
Alkis Evlogimenos1a876fa2004-07-22 08:14:44 +0000590unsigned RA::getFreePhysReg(LiveInterval* cur)
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000591{
Alkis Evlogimenos095c3a82004-09-02 21:23:32 +0000592 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
593 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
594 i != e; ++i) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000595 unsigned reg = i->first->reg;
Alkis Evlogimenos095c3a82004-09-02 21:23:32 +0000596 if (MRegisterInfo::isVirtualRegister(reg))
597 reg = vrm_->getPhys(reg);
598 ++inactiveCounts[reg];
599 }
600
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000601 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos43b587d2003-12-28 17:58:18 +0000602
Alkis Evlogimenos095c3a82004-09-02 21:23:32 +0000603 unsigned freeReg = 0;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000604 for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_);
605 i != rc->allocation_order_end(*mf_); ++i) {
606 unsigned reg = *i;
Alkis Evlogimenos095c3a82004-09-02 21:23:32 +0000607 if (prt_->isRegAvail(reg) &&
608 (!freeReg || inactiveCounts[freeReg] < inactiveCounts[reg]))
609 freeReg = reg;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000610 }
Alkis Evlogimenos095c3a82004-09-02 21:23:32 +0000611 return freeReg;
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000612}
613
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000614FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000615 return new RA();
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000616}