blob: b9af3970e0e641ab667a0b8d7ccc4ec361db016a [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"
15#include "llvm/Function.h"
Alkis Evlogimenosff0cbe12003-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 Evlogimenosff0cbe12003-11-20 03:32:25 +000021#include "llvm/Target/TargetMachine.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "Support/Debug.h"
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000023#include "Support/Statistic.h"
Alkis Evlogimenos69240632004-05-30 07:46:27 +000024#include "Support/STLExtras.h"
Chris Lattnera3b8b5c2004-07-23 17:56:30 +000025#include "LiveIntervalAnalysis.h"
Alkis Evlogimenos888b1a62004-02-23 00:53:31 +000026#include "PhysRegTracker.h"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000027#include "VirtRegMap.h"
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000028#include <algorithm>
Alkis Evlogimenos880e8e42004-05-08 03:50:03 +000029#include <cmath>
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +000030#include <set>
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +000031#include <queue>
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000032
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");
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000039
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000040 static unsigned numIterations = 0;
41 static unsigned numIntervals = 0;
Alkis Evlogimenosc1560952004-07-04 17:23:35 +000042
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000043 class RA : public MachineFunctionPass {
44 private:
45 MachineFunction* mf_;
46 const TargetMachine* tm_;
47 const MRegisterInfo* mri_;
48 LiveIntervals* li_;
49 typedef std::vector<LiveInterval*> IntervalPtrs;
50 IntervalPtrs handled_, fixed_, active_, inactive_;
51 typedef std::priority_queue<LiveInterval*,
52 IntervalPtrs,
53 greater_ptr<LiveInterval> > IntervalHeap;
54 IntervalHeap unhandled_;
55 std::auto_ptr<PhysRegTracker> prt_;
56 std::auto_ptr<VirtRegMap> vrm_;
57 std::auto_ptr<Spiller> spiller_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000058
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000059 typedef std::vector<float> SpillWeights;
60 SpillWeights spillWeights_;
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +000061
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000062 public:
63 virtual const char* getPassName() const {
64 return "Linear Scan Register Allocator";
65 }
66
67 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000068 AU.addRequired<LiveIntervals>();
69 MachineFunctionPass::getAnalysisUsage(AU);
70 }
71
72 /// runOnMachineFunction - register allocate the whole function
73 bool runOnMachineFunction(MachineFunction&);
74
75 void releaseMemory();
76
77 private:
78 /// linearScan - the linear scan algorithm
79 void linearScan();
80
81 /// initIntervalSets - initializa the four interval sets:
82 /// unhandled, fixed, active and inactive
83 void initIntervalSets();
84
85 /// processActiveIntervals - expire old intervals and move
86 /// non-overlapping ones to the incative list
87 void processActiveIntervals(LiveInterval* cur);
88
89 /// processInactiveIntervals - expire old intervals and move
90 /// overlapping ones to the active list
91 void processInactiveIntervals(LiveInterval* cur);
92
93 /// updateSpillWeights - updates the spill weights of the
94 /// specifed physical register and its weight
95 void updateSpillWeights(unsigned reg, SpillWeights::value_type weight);
96
97 /// assignRegOrStackSlotAtInterval - assign a register if one
98 /// is available, or spill.
99 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
100
101 ///
102 /// register handling helpers
103 ///
104
105 /// getFreePhysReg - return a free physical register for this
106 /// virtual register interval if we have one, otherwise return
107 /// 0
108 unsigned getFreePhysReg(LiveInterval* cur);
109
110 /// assignVirt2StackSlot - assigns this virtual register to a
111 /// stack slot. returns the stack slot
112 int assignVirt2StackSlot(unsigned virtReg);
113
114 template <typename ItTy>
115 void printIntervals(const char* const str, ItTy i, ItTy e) const {
116 if (str) std::cerr << str << " intervals:\n";
117 for (; i != e; ++i) {
118 std::cerr << "\t" << **i << " -> ";
119 unsigned reg = (*i)->reg;
120 if (MRegisterInfo::isVirtualRegister(reg)) {
121 reg = vrm_->getPhys(reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000122 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000123 std::cerr << mri_->getName(reg) << '\n';
124 }
125 }
126 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000127}
128
Alkis Evlogimenos04667292004-02-01 20:13:26 +0000129void RA::releaseMemory()
130{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000131 while (!unhandled_.empty()) unhandled_.pop();
132 fixed_.clear();
133 active_.clear();
134 inactive_.clear();
135 handled_.clear();
Alkis Evlogimenos04667292004-02-01 20:13:26 +0000136}
137
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000138bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000139 mf_ = &fn;
140 tm_ = &fn.getTarget();
141 mri_ = tm_->getRegisterInfo();
142 li_ = &getAnalysis<LiveIntervals>();
143 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
144 vrm_.reset(new VirtRegMap(*mf_));
145 if (!spiller_.get()) spiller_.reset(createSpiller());
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000146
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000147 initIntervalSets();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000148
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000149 linearScan();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000150
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000151 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000152
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000153 return true;
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000154}
155
156void RA::linearScan()
157{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000158 // linear scan algorithm
159 DEBUG(std::cerr << "********** LINEAR SCAN **********\n");
160 DEBUG(std::cerr << "********** Function: "
161 << mf_->getFunction()->getName() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000162
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000163 // DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
164 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
165 DEBUG(printIntervals("active", active_.begin(), active_.end()));
166 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
167
168 while (!unhandled_.empty()) {
169 // pick the interval with the earliest start point
170 LiveInterval* cur = unhandled_.top();
171 unhandled_.pop();
172 ++numIterations;
173 DEBUG(std::cerr << "\n*** CURRENT ***: " << *cur << '\n');
174
175 processActiveIntervals(cur);
176 processInactiveIntervals(cur);
177
178 // if this register is fixed we are done
179 if (MRegisterInfo::isPhysicalRegister(cur->reg)) {
180 prt_->addRegUse(cur->reg);
181 active_.push_back(cur);
182 handled_.push_back(cur);
183 }
184 // otherwise we are allocating a virtual register. try to find
185 // a free physical register or spill an interval in order to
186 // assign it one (we could spill the current though).
187 else {
188 assignRegOrStackSlotAtInterval(cur);
189 }
190
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000191 DEBUG(printIntervals("active", active_.begin(), active_.end()));
192 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000193 }
194 numIntervals += li_->getNumIntervals();
195 efficiency = double(numIterations) / double(numIntervals);
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000196
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000197 // expire any remaining active intervals
198 for (IntervalPtrs::reverse_iterator
199 i = active_.rbegin(); i != active_.rend(); ) {
200 unsigned reg = (*i)->reg;
201 DEBUG(std::cerr << "\tinterval " << **i << " expired\n");
202 if (MRegisterInfo::isVirtualRegister(reg))
203 reg = vrm_->getPhys(reg);
204 prt_->delRegUse(reg);
205 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
206 }
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000207
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000208 // expire any remaining inactive intervals
209 for (IntervalPtrs::reverse_iterator
210 i = inactive_.rbegin(); i != inactive_.rend(); ) {
211 DEBUG(std::cerr << "\tinterval " << **i << " expired\n");
212 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
213 }
Alkis Evlogimenosb7be1152004-01-13 20:42:08 +0000214
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000215 DEBUG(std::cerr << *vrm_);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000216}
217
Chris Lattner4df98e52004-07-24 03:32:06 +0000218void RA::initIntervalSets()
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000219{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000220 assert(unhandled_.empty() && fixed_.empty() &&
221 active_.empty() && inactive_.empty() &&
222 "interval sets should be empty on initialization");
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000223
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000224 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i){
225 unhandled_.push(&i->second);
226 if (MRegisterInfo::isPhysicalRegister(i->second.reg))
227 fixed_.push_back(&i->second);
228 }
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000229}
230
231void RA::processActiveIntervals(IntervalPtrs::value_type cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000232{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000233 DEBUG(std::cerr << "\tprocessing active intervals:\n");
234 for (IntervalPtrs::reverse_iterator
235 i = active_.rbegin(); i != active_.rend();) {
236 unsigned reg = (*i)->reg;
237 // remove expired intervals
238 if ((*i)->expiredAt(cur->start())) {
239 DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n");
240 if (MRegisterInfo::isVirtualRegister(reg))
241 reg = vrm_->getPhys(reg);
242 prt_->delRegUse(reg);
243 // remove from active
244 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000245 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000246 // move inactive intervals to inactive list
247 else if (!(*i)->liveAt(cur->start())) {
248 DEBUG(std::cerr << "\t\tinterval " << **i << " inactive\n");
249 if (MRegisterInfo::isVirtualRegister(reg))
250 reg = vrm_->getPhys(reg);
251 prt_->delRegUse(reg);
252 // add to inactive
253 inactive_.push_back(*i);
254 // remove from active
255 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
256 }
257 else {
258 ++i;
259 }
260 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000261}
262
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000263void RA::processInactiveIntervals(IntervalPtrs::value_type cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000264{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000265 DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
266 for (IntervalPtrs::reverse_iterator
267 i = inactive_.rbegin(); i != inactive_.rend();) {
268 unsigned reg = (*i)->reg;
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000269
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000270 // remove expired intervals
271 if ((*i)->expiredAt(cur->start())) {
272 DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n");
273 // remove from inactive
274 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000275 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000276 // move re-activated intervals in active list
277 else if ((*i)->liveAt(cur->start())) {
278 DEBUG(std::cerr << "\t\tinterval " << **i << " active\n");
279 if (MRegisterInfo::isVirtualRegister(reg))
280 reg = vrm_->getPhys(reg);
281 prt_->addRegUse(reg);
282 // add to active
283 active_.push_back(*i);
284 // remove from inactive
285 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
286 }
287 else {
288 ++i;
289 }
290 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000291}
292
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000293void RA::updateSpillWeights(unsigned reg, SpillWeights::value_type weight)
294{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000295 spillWeights_[reg] += weight;
296 for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as)
297 spillWeights_[*as] += weight;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000298}
299
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000300void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000301{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000302 DEBUG(std::cerr << "\tallocating current interval: ");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000303
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000304 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000305
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000306 spillWeights_.assign(mri_->getNumRegs(), 0.0);
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000307
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000308 // for each interval in active update spill weights
309 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
310 i != e; ++i) {
311 unsigned reg = (*i)->reg;
312 if (MRegisterInfo::isVirtualRegister(reg))
313 reg = vrm_->getPhys(reg);
314 updateSpillWeights(reg, (*i)->weight);
315 }
316
317 // for every interval in inactive we overlap with, mark the
318 // register as not free and update spill weights
319 for (IntervalPtrs::const_iterator i = inactive_.begin(),
320 e = inactive_.end(); i != e; ++i) {
321 if (cur->overlaps(**i)) {
322 unsigned reg = (*i)->reg;
323 if (MRegisterInfo::isVirtualRegister(reg))
324 reg = vrm_->getPhys(reg);
325 prt_->addRegUse(reg);
326 updateSpillWeights(reg, (*i)->weight);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000327 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000328 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000329
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000330 // for every interval in fixed we overlap with,
331 // mark the register as not free and update spill weights
332 for (IntervalPtrs::const_iterator i = fixed_.begin(),
333 e = fixed_.end(); i != e; ++i) {
334 if (cur->overlaps(**i)) {
335 unsigned reg = (*i)->reg;
336 prt_->addRegUse(reg);
337 updateSpillWeights(reg, (*i)->weight);
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000338 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000339 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000340
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000341 unsigned physReg = getFreePhysReg(cur);
342 // restore the physical register tracker
343 *prt_ = backupPrt;
344 // if we find a free register, we are done: assign this virtual to
345 // the free physical register and add this interval to the active
346 // list.
347 if (physReg) {
348 DEBUG(std::cerr << mri_->getName(physReg) << '\n');
349 vrm_->assignVirt2Phys(cur->reg, physReg);
350 prt_->addRegUse(physReg);
351 active_.push_back(cur);
352 handled_.push_back(cur);
353 return;
354 }
355 DEBUG(std::cerr << "no free registers\n");
356
357 DEBUG(std::cerr << "\tassigning stack slot at interval "<< *cur << ":\n");
358
359 float minWeight = HUGE_VAL;
360 unsigned minReg = 0;
361 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
362 for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_);
363 i != rc->allocation_order_end(*mf_); ++i) {
364 unsigned reg = *i;
365 if (minWeight > spillWeights_[reg]) {
366 minWeight = spillWeights_[reg];
367 minReg = reg;
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000368 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000369 }
370 DEBUG(std::cerr << "\t\tregister with min weight: "
371 << mri_->getName(minReg) << " (" << minWeight << ")\n");
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000372
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000373 // if the current has the minimum weight, we need to spill it and
374 // add any added intervals back to unhandled, and restart
375 // linearscan.
376 if (cur->weight <= minWeight) {
377 DEBUG(std::cerr << "\t\t\tspilling(c): " << *cur << '\n';);
378 int slot = vrm_->assignVirt2StackSlot(cur->reg);
379 std::vector<LiveInterval*> added =
380 li_->addIntervalsForSpills(*cur, *vrm_, slot);
381 if (added.empty())
382 return; // Early exit if all spills were folded.
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000383
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000384 // Merge added with unhandled. Note that we know that
385 // addIntervalsForSpills returns intervals sorted by their starting
386 // point.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000387 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000388 unhandled_.push(added[i]);
389 return;
390 }
391
392 // push the current interval back to unhandled since we are going
393 // to re-run at least this iteration. Since we didn't modify it it
394 // should go back right in the front of the list
395 unhandled_.push(cur);
396
397 // otherwise we spill all intervals aliasing the register with
398 // minimum weight, rollback to the interval with the earliest
399 // start point and let the linear scan algorithm run again
400 std::vector<LiveInterval*> added;
401 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
402 "did not choose a register to spill?");
403 std::vector<bool> toSpill(mri_->getNumRegs(), false);
404 // we are going to spill minReg and all its aliases
405 toSpill[minReg] = true;
406 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
407 toSpill[*as] = true;
408
409 // the earliest start of a spilled interval indicates up to where
410 // in handled we need to roll back
411 unsigned earliestStart = cur->start();
412
413 // set of spilled vregs (used later to rollback properly)
414 std::set<unsigned> spilled;
415
416 // spill live intervals of virtual regs mapped to the physical
417 // register we want to clear (and its aliases). we only spill
418 // those that overlap with the current interval as the rest do not
419 // affect its allocation. we also keep track of the earliest start
420 // of all spilled live intervals since this will mark our rollback
421 // point
422 for (IntervalPtrs::iterator
423 i = active_.begin(); i != active_.end(); ++i) {
424 unsigned reg = (*i)->reg;
425 if (MRegisterInfo::isVirtualRegister(reg) &&
426 toSpill[vrm_->getPhys(reg)] &&
427 cur->overlaps(**i)) {
428 DEBUG(std::cerr << "\t\t\tspilling(a): " << **i << '\n');
429 earliestStart = std::min(earliestStart, (*i)->start());
430 int slot = vrm_->assignVirt2StackSlot((*i)->reg);
431 std::vector<LiveInterval*> newIs =
432 li_->addIntervalsForSpills(**i, *vrm_, slot);
433 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
434 spilled.insert(reg);
435 }
436 }
437 for (IntervalPtrs::iterator
438 i = inactive_.begin(); i != inactive_.end(); ++i) {
439 unsigned reg = (*i)->reg;
440 if (MRegisterInfo::isVirtualRegister(reg) &&
441 toSpill[vrm_->getPhys(reg)] &&
442 cur->overlaps(**i)) {
443 DEBUG(std::cerr << "\t\t\tspilling(i): " << **i << '\n');
444 earliestStart = std::min(earliestStart, (*i)->start());
445 int slot = vrm_->assignVirt2StackSlot((*i)->reg);
446 std::vector<LiveInterval*> newIs =
447 li_->addIntervalsForSpills(**i, *vrm_, slot);
448 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
449 spilled.insert(reg);
450 }
451 }
452
453 DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
454 // scan handled in reverse order up to the earliaset start of a
455 // spilled live interval and undo each one, restoring the state of
456 // unhandled
457 while (!handled_.empty()) {
458 LiveInterval* i = handled_.back();
459 // if this interval starts before t we are done
460 if (i->start() < earliestStart)
461 break;
462 DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
463 handled_.pop_back();
464 // when undoing a live interval allocation we must know if it
465 // is active or inactive to properly update the PhysRegTracker
466 // and the VirtRegMap
467 IntervalPtrs::iterator it;
468 if ((it = find(active_.begin(), active_.end(), i)) != active_.end()) {
469 active_.erase(it);
470 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
471 prt_->delRegUse(i->reg);
472 unhandled_.push(i);
473 }
474 else {
475 if (!spilled.count(i->reg))
476 unhandled_.push(i);
477 prt_->delRegUse(vrm_->getPhys(i->reg));
478 vrm_->clearVirt(i->reg);
479 }
480 }
481 else if ((it = find(inactive_.begin(), inactive_.end(), i)) != inactive_.end()) {
482 inactive_.erase(it);
483 if (MRegisterInfo::isPhysicalRegister(i->reg))
484 unhandled_.push(i);
485 else {
486 if (!spilled.count(i->reg))
487 unhandled_.push(i);
488 vrm_->clearVirt(i->reg);
489 }
490 }
491 else {
492 if (MRegisterInfo::isVirtualRegister(i->reg))
493 vrm_->clearVirt(i->reg);
494 unhandled_.push(i);
495 }
496 }
497
498 // scan the rest and undo each interval that expired after t and
499 // insert it in active (the next iteration of the algorithm will
500 // put it in inactive if required)
501 IntervalPtrs::iterator i = handled_.begin(), e = handled_.end();
502 for (; i != e; ++i) {
503 if (!(*i)->expiredAt(earliestStart) && (*i)->expiredAt(cur->start())) {
504 DEBUG(std::cerr << "\t\t\tundo changes for: " << **i << '\n');
505 active_.push_back(*i);
506 if (MRegisterInfo::isPhysicalRegister((*i)->reg))
507 prt_->addRegUse((*i)->reg);
508 else
509 prt_->addRegUse(vrm_->getPhys((*i)->reg));
510 }
511 }
512
513 std::sort(added.begin(), added.end(), less_ptr<LiveInterval>());
514 // merge added with unhandled
515 for (unsigned i = 0, e = added.size(); i != e; ++i)
516 unhandled_.push(added[i]);
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000517}
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000518
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000519unsigned RA::getFreePhysReg(LiveInterval* cur)
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000520{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000521 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000522
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000523 for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_);
524 i != rc->allocation_order_end(*mf_); ++i) {
525 unsigned reg = *i;
526 if (prt_->isRegAvail(reg))
527 return reg;
528 }
529 return 0;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000530}
531
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000532FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000533 return new RA();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000534}