1 //===-- RegAllocLinearScan.cpp - Linear Scan register allocator -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements a linear scan register allocator.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "regalloc"
15 #include "llvm/Function.h"
16 #include "llvm/CodeGen/LiveVariables.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/CodeGen/SSARegMap.h"
21 #include "llvm/Target/MRegisterInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "Support/Debug.h"
24 #include "LiveIntervals.h"
25 #include "PhysRegTracker.h"
26 #include "VirtRegMap.h"
33 class RA : public MachineFunctionPass {
36 const TargetMachine* tm_;
37 const MRegisterInfo* mri_;
39 typedef std::list<LiveIntervals::Interval*> IntervalPtrs;
40 IntervalPtrs unhandled_, fixed_, active_, inactive_, handled_;
42 std::auto_ptr<PhysRegTracker> prt_;
43 std::auto_ptr<VirtRegMap> vrm_;
44 std::auto_ptr<Spiller> spiller_;
46 typedef std::vector<float> SpillWeights;
47 SpillWeights spillWeights_;
50 virtual const char* getPassName() const {
51 return "Linear Scan Register Allocator";
54 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55 AU.addRequired<LiveVariables>();
56 AU.addRequired<LiveIntervals>();
57 MachineFunctionPass::getAnalysisUsage(AU);
60 /// runOnMachineFunction - register allocate the whole function
61 bool runOnMachineFunction(MachineFunction&);
66 /// linearScan - the linear scan algorithm
69 /// initIntervalSets - initializa the four interval sets:
70 /// unhandled, fixed, active and inactive
71 void initIntervalSets(LiveIntervals::Intervals& li);
73 /// processActiveIntervals - expire old intervals and move
74 /// non-overlapping ones to the incative list
75 void processActiveIntervals(IntervalPtrs::value_type cur);
77 /// processInactiveIntervals - expire old intervals and move
78 /// overlapping ones to the active list
79 void processInactiveIntervals(IntervalPtrs::value_type cur);
81 /// updateSpillWeights - updates the spill weights of the
82 /// specifed physical register and its weight
83 void updateSpillWeights(unsigned reg, SpillWeights::value_type weight);
85 /// assignRegOrStackSlotAtInterval - assign a register if one
86 /// is available, or spill.
87 void assignRegOrStackSlotAtInterval(IntervalPtrs::value_type cur);
90 /// register handling helpers
93 /// getFreePhysReg - return a free physical register for this
94 /// virtual register interval if we have one, otherwise return
96 unsigned getFreePhysReg(IntervalPtrs::value_type cur);
98 /// assignVirt2StackSlot - assigns this virtual register to a
99 /// stack slot. returns the stack slot
100 int assignVirt2StackSlot(unsigned virtReg);
102 void printIntervals(const char* const str,
103 RA::IntervalPtrs::const_iterator i,
104 RA::IntervalPtrs::const_iterator e) const {
105 if (str) std::cerr << str << " intervals:\n";
106 for (; i != e; ++i) {
107 std::cerr << "\t" << **i << " -> ";
108 unsigned reg = (*i)->reg;
109 if (MRegisterInfo::isVirtualRegister(reg)) {
110 reg = vrm_->getPhys(reg);
112 std::cerr << mri_->getName(reg) << '\n';
116 // void verifyAssignment() const {
117 // for (Virt2PhysMap::const_iterator i = v2pMap_.begin(),
118 // e = v2pMap_.end(); i != e; ++i)
119 // for (Virt2PhysMap::const_iterator i2 = next(i); i2 != e; ++i2)
120 // if (MRegisterInfo::isVirtualRegister(i->second) &&
121 // (i->second == i2->second ||
122 // mri_->areAliases(i->second, i2->second))) {
123 // const LiveIntervals::Interval
124 // &in = li_->getInterval(i->second),
125 // &in2 = li_->getInterval(i2->second);
126 // if (in.overlaps(in2)) {
127 // std::cerr << in << " overlaps " << in2 << '\n';
135 void RA::releaseMemory()
144 bool RA::runOnMachineFunction(MachineFunction &fn) {
146 tm_ = &fn.getTarget();
147 mri_ = tm_->getRegisterInfo();
148 li_ = &getAnalysis<LiveIntervals>();
149 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
150 vrm_.reset(new VirtRegMap(*mf_));
151 if (!spiller_.get()) spiller_.reset(createSpiller());
153 initIntervalSets(li_->getIntervals());
157 spiller_->runOnMachineFunction(*mf_, *vrm_);
162 void RA::linearScan()
164 // linear scan algorithm
165 DEBUG(std::cerr << "********** LINEAR SCAN **********\n");
166 DEBUG(std::cerr << "********** Function: "
167 << mf_->getFunction()->getName() << '\n');
169 DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
170 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
171 DEBUG(printIntervals("active", active_.begin(), active_.end()));
172 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
174 while (!unhandled_.empty()) {
175 // pick the interval with the earliest start point
176 IntervalPtrs::value_type cur = unhandled_.front();
177 unhandled_.pop_front();
179 DEBUG(std::cerr << "\n*** CURRENT ***: " << *cur << '\n');
181 processActiveIntervals(cur);
182 processInactiveIntervals(cur);
184 // if this register is fixed we are done
185 if (MRegisterInfo::isPhysicalRegister(cur->reg)) {
186 prt_->addRegUse(cur->reg);
187 active_.push_back(cur);
188 handled_.push_back(cur);
190 // otherwise we are allocating a virtual register. try to find
191 // a free physical register or spill an interval in order to
192 // assign it one (we could spill the current though).
194 assignRegOrStackSlotAtInterval(cur);
197 DEBUG(printIntervals("active", active_.begin(), active_.end()));
198 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
199 // DEBUG(verifyAssignment());
202 // expire any remaining active intervals
203 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
204 unsigned reg = (*i)->reg;
205 DEBUG(std::cerr << "\tinterval " << **i << " expired\n");
206 if (MRegisterInfo::isVirtualRegister(reg))
207 reg = vrm_->getPhys(reg);
208 prt_->delRegUse(reg);
211 DEBUG(std::cerr << *vrm_);
214 void RA::initIntervalSets(LiveIntervals::Intervals& li)
216 assert(unhandled_.empty() && fixed_.empty() &&
217 active_.empty() && inactive_.empty() &&
218 "interval sets should be empty on initialization");
220 for (LiveIntervals::Intervals::iterator i = li.begin(), e = li.end();
222 unhandled_.push_back(&*i);
223 if (MRegisterInfo::isPhysicalRegister(i->reg))
224 fixed_.push_back(&*i);
228 void RA::processActiveIntervals(IntervalPtrs::value_type cur)
230 DEBUG(std::cerr << "\tprocessing active intervals:\n");
231 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end();) {
232 unsigned reg = (*i)->reg;
233 // remove expired intervals
234 if ((*i)->expiredAt(cur->start())) {
235 DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n");
236 if (MRegisterInfo::isVirtualRegister(reg))
237 reg = vrm_->getPhys(reg);
238 prt_->delRegUse(reg);
239 // remove from active
240 i = active_.erase(i);
242 // move inactive intervals to inactive list
243 else if (!(*i)->liveAt(cur->start())) {
244 DEBUG(std::cerr << "\t\tinterval " << **i << " inactive\n");
245 if (MRegisterInfo::isVirtualRegister(reg))
246 reg = vrm_->getPhys(reg);
247 prt_->delRegUse(reg);
249 inactive_.push_back(*i);
250 // remove from active
251 i = active_.erase(i);
259 void RA::processInactiveIntervals(IntervalPtrs::value_type cur)
261 DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
262 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end();) {
263 unsigned reg = (*i)->reg;
265 // remove expired intervals
266 if ((*i)->expiredAt(cur->start())) {
267 DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n");
268 // remove from inactive
269 i = inactive_.erase(i);
271 // move re-activated intervals in active list
272 else if ((*i)->liveAt(cur->start())) {
273 DEBUG(std::cerr << "\t\tinterval " << **i << " active\n");
274 if (MRegisterInfo::isVirtualRegister(reg))
275 reg = vrm_->getPhys(reg);
276 prt_->addRegUse(reg);
278 active_.push_back(*i);
279 // remove from inactive
280 i = inactive_.erase(i);
288 void RA::updateSpillWeights(unsigned reg, SpillWeights::value_type weight)
290 spillWeights_[reg] += weight;
291 for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as)
292 spillWeights_[*as] += weight;
295 void RA::assignRegOrStackSlotAtInterval(IntervalPtrs::value_type cur)
297 DEBUG(std::cerr << "\tallocating current interval: ");
299 PhysRegTracker backupPrt = *prt_;
301 spillWeights_.assign(mri_->getNumRegs(), 0.0);
303 // for each interval in active update spill weights
304 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
306 unsigned reg = (*i)->reg;
307 if (MRegisterInfo::isVirtualRegister(reg))
308 reg = vrm_->getPhys(reg);
309 updateSpillWeights(reg, (*i)->weight);
312 // for every interval in inactive we overlap with, mark the
313 // register as not free and update spill weights
314 for (IntervalPtrs::const_iterator i = inactive_.begin(),
315 e = inactive_.end(); i != e; ++i) {
316 if (cur->overlaps(**i)) {
317 unsigned reg = (*i)->reg;
318 if (MRegisterInfo::isVirtualRegister(reg))
319 reg = vrm_->getPhys(reg);
320 prt_->addRegUse(reg);
321 updateSpillWeights(reg, (*i)->weight);
325 // for every interval in fixed we overlap with,
326 // mark the register as not free and update spill weights
327 for (IntervalPtrs::const_iterator i = fixed_.begin(),
328 e = fixed_.end(); i != e; ++i) {
329 if (cur->overlaps(**i)) {
330 unsigned reg = (*i)->reg;
331 prt_->addRegUse(reg);
332 updateSpillWeights(reg, (*i)->weight);
336 unsigned physReg = getFreePhysReg(cur);
337 // restore the physical register tracker
339 // if we find a free register, we are done: assign this virtual to
340 // the free physical register and add this interval to the active
343 DEBUG(std::cerr << mri_->getName(physReg) << '\n');
344 vrm_->assignVirt2Phys(cur->reg, physReg);
345 prt_->addRegUse(physReg);
346 active_.push_back(cur);
347 handled_.push_back(cur);
350 DEBUG(std::cerr << "no free registers\n");
352 DEBUG(std::cerr << "\tassigning stack slot at interval "<< *cur << ":\n");
354 float minWeight = std::numeric_limits<float>::infinity();
356 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
357 for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_);
358 i != rc->allocation_order_end(*mf_); ++i) {
360 if (minWeight > spillWeights_[reg]) {
361 minWeight = spillWeights_[reg];
365 DEBUG(std::cerr << "\t\tregister with min weight: "
366 << mri_->getName(minReg) << " (" << minWeight << ")\n");
368 // if the current has the minimum weight, we need to modify it,
369 // push it back in unhandled and let the linear scan algorithm run
371 if (cur->weight <= minWeight) {
372 DEBUG(std::cerr << "\t\t\tspilling(c): " << *cur << '\n';);
373 int slot = vrm_->assignVirt2StackSlot(cur->reg);
374 li_->updateSpilledInterval(*cur, *vrm_, slot);
376 // if we didn't eliminate the interval find where to add it
377 // back to unhandled. We need to scan since unhandled are
378 // sorted on earliest start point and we may have changed our
381 IntervalPtrs::iterator it = unhandled_.begin();
382 while (it != unhandled_.end() && (*it)->start() < cur->start())
384 unhandled_.insert(it, cur);
389 // push the current interval back to unhandled since we are going
390 // to re-run at least this iteration. Since we didn't modify it it
391 // should go back right in the front of the list
392 unhandled_.push_front(cur);
394 // otherwise we spill all intervals aliasing the register with
395 // minimum weight, rollback to the interval with the earliest
396 // start point and let the linear scan algorithm run again
397 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
398 "did not choose a register to spill?");
399 std::vector<bool> toSpill(mri_->getNumRegs(), false);
400 toSpill[minReg] = true;
401 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
403 unsigned earliestStart = cur->start();
405 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
406 unsigned reg = (*i)->reg;
407 if (MRegisterInfo::isVirtualRegister(reg) &&
408 toSpill[vrm_->getPhys(reg)] &&
409 cur->overlaps(**i)) {
410 DEBUG(std::cerr << "\t\t\tspilling(a): " << **i << '\n');
411 earliestStart = std::min(earliestStart, (*i)->start());
412 int slot = vrm_->assignVirt2StackSlot((*i)->reg);
413 li_->updateSpilledInterval(**i, *vrm_, slot);
416 for (IntervalPtrs::iterator i = inactive_.begin();
417 i != inactive_.end(); ++i) {
418 unsigned reg = (*i)->reg;
419 if (MRegisterInfo::isVirtualRegister(reg) &&
420 toSpill[vrm_->getPhys(reg)] &&
421 cur->overlaps(**i)) {
422 DEBUG(std::cerr << "\t\t\tspilling(i): " << **i << '\n');
423 earliestStart = std::min(earliestStart, (*i)->start());
424 int slot = vrm_->assignVirt2StackSlot((*i)->reg);
425 li_->updateSpilledInterval(**i, *vrm_, slot);
429 DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
430 // scan handled in reverse order and undo each one, restoring the
431 // state of unhandled
432 while (!handled_.empty()) {
433 IntervalPtrs::value_type i = handled_.back();
434 // if this interval starts before t we are done
435 if (!i->empty() && i->start() < earliestStart)
437 DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
439 IntervalPtrs::iterator it;
440 if ((it = find(active_.begin(), active_.end(), i)) != active_.end()) {
442 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
443 prt_->delRegUse(i->reg);
444 unhandled_.push_front(i);
447 prt_->delRegUse(vrm_->getPhys(i->reg));
448 vrm_->clearVirt(i->reg);
451 IntervalPtrs::iterator it = unhandled_.begin();
452 while (it != unhandled_.end() &&
453 (*it)->start() < i->start())
455 unhandled_.insert(it, i);
459 unhandled_.push_front(i);
463 else if ((it = find(inactive_.begin(), inactive_.end(), i)) != inactive_.end()) {
465 if (MRegisterInfo::isPhysicalRegister(i->reg))
466 unhandled_.push_front(i);
468 vrm_->clearVirt(i->reg);
471 IntervalPtrs::iterator it = unhandled_.begin();
472 while (it != unhandled_.end() &&
473 (*it)->start() < i->start())
475 unhandled_.insert(it, i);
479 unhandled_.push_front(i);
483 if (MRegisterInfo::isVirtualRegister(i->reg))
484 vrm_->clearVirt(i->reg);
485 unhandled_.push_front(i);
489 // scan the rest and undo each interval that expired after t and
490 // insert it in active (the next iteration of the algorithm will
491 // put it in inactive if required)
492 IntervalPtrs::iterator i = handled_.begin(), e = handled_.end();
493 for (; i != e; ++i) {
494 if (!(*i)->expiredAt(earliestStart) && (*i)->expiredAt(cur->start())) {
495 DEBUG(std::cerr << "\t\t\tundo changes for: " << **i << '\n');
496 active_.push_back(*i);
497 if (MRegisterInfo::isPhysicalRegister((*i)->reg))
498 prt_->addRegUse((*i)->reg);
500 prt_->addRegUse(vrm_->getPhys((*i)->reg));
505 unsigned RA::getFreePhysReg(IntervalPtrs::value_type cur)
507 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
509 for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_);
510 i != rc->allocation_order_end(*mf_); ++i) {
512 if (prt_->isRegAvail(reg))
518 FunctionPass* llvm::createLinearScanRegisterAllocator() {