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 //===----------------------------------------------------------------------===//
13 #define DEBUG_TYPE "regalloc"
14 #include "llvm/Function.h"
15 #include "llvm/CodeGen/LiveIntervals.h"
16 #include "llvm/CodeGen/LiveVariables.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/CodeGen/SSARegMap.h"
22 #include "llvm/Target/MRegisterInfo.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/CFG.h"
26 #include "Support/Debug.h"
27 #include "Support/DepthFirstIterator.h"
28 #include "Support/Statistic.h"
29 #include "Support/STLExtras.h"
33 Statistic<> numSpilled ("ra-linearscan", "Number of registers spilled");
34 Statistic<> numReloaded("ra-linearscan", "Number of registers reloaded");
35 Statistic<> numPeep ("ra-linearscan",
36 "Number of identity moves eliminated");
38 class PhysRegTracker {
40 const MRegisterInfo* mri_;
41 std::vector<bool> reserved_;
42 std::vector<unsigned> regUse_;
45 PhysRegTracker(MachineFunction* mf)
46 : mri_(mf ? mf->getTarget().getRegisterInfo() : NULL) {
48 reserved_.assign(mri_->getNumRegs(), false);
49 regUse_.assign(mri_->getNumRegs(), 0);
53 PhysRegTracker(const PhysRegTracker& rhs)
55 reserved_(rhs.reserved_),
56 regUse_(rhs.regUse_) {
59 const PhysRegTracker& operator=(const PhysRegTracker& rhs) {
61 reserved_ = rhs.reserved_;
62 regUse_ = rhs.regUse_;
66 void reservePhysReg(unsigned physReg) {
67 reserved_[physReg] = true;
70 void addPhysRegUse(unsigned physReg) {
72 for (const unsigned* as = mri_->getAliasSet(physReg); *as; ++as) {
78 void delPhysRegUse(unsigned physReg) {
79 assert(regUse_[physReg] != 0);
81 for (const unsigned* as = mri_->getAliasSet(physReg); *as; ++as) {
83 assert(regUse_[physReg] != 0);
88 bool isPhysRegReserved(unsigned physReg) const {
89 return reserved_[physReg];
92 bool isPhysRegAvail(unsigned physReg) const {
93 return regUse_[physReg] == 0 && !isPhysRegReserved(physReg);
96 bool isReservedPhysRegAvail(unsigned physReg) const {
97 return regUse_[physReg] == 0 && isPhysRegReserved(physReg);
101 class RA : public MachineFunctionPass {
103 MachineFunction* mf_;
104 const TargetMachine* tm_;
105 const MRegisterInfo* mri_;
107 MachineFunction::iterator currentMbb_;
108 MachineBasicBlock::iterator currentInstr_;
109 typedef std::vector<const LiveIntervals::Interval*> IntervalPtrs;
110 IntervalPtrs unhandled_, fixed_, active_, inactive_;
112 typedef std::vector<unsigned> Regs;
113 Regs tempUseOperands_;
114 Regs tempDefOperands_;
118 typedef std::map<unsigned, unsigned> Virt2PhysMap;
119 Virt2PhysMap v2pMap_;
121 typedef std::map<unsigned, int> Virt2StackSlotMap;
122 Virt2StackSlotMap v2ssMap_;
132 virtual const char* getPassName() const {
133 return "Linear Scan Register Allocator";
136 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
137 AU.addRequired<LiveVariables>();
138 AU.addRequired<LiveIntervals>();
139 MachineFunctionPass::getAnalysisUsage(AU);
142 /// runOnMachineFunction - register allocate the whole function
143 bool runOnMachineFunction(MachineFunction&);
145 void releaseMemory();
148 /// initIntervalSets - initializa the four interval sets:
149 /// unhandled, fixed, active and inactive
150 void initIntervalSets(const LiveIntervals::Intervals& li);
152 /// processActiveIntervals - expire old intervals and move
153 /// non-overlapping ones to the incative list
154 void processActiveIntervals(IntervalPtrs::value_type cur);
156 /// processInactiveIntervals - expire old intervals and move
157 /// overlapping ones to the active list
158 void processInactiveIntervals(IntervalPtrs::value_type cur);
160 /// assignStackSlotAtInterval - choose and spill
161 /// interval. Currently we spill the interval with the last
162 /// end point in the active and inactive lists and the current
164 void assignStackSlotAtInterval(IntervalPtrs::value_type cur,
165 const PhysRegTracker& backupPtr);
168 /// register handling helpers
171 /// getFreePhysReg - return a free physical register for this
172 /// virtual register interval if we have one, otherwise return
174 unsigned getFreePhysReg(IntervalPtrs::value_type cur);
176 /// getFreeTempPhysReg - return a free temprorary physical
177 /// register for this virtual register if we have one (should
179 unsigned getFreeTempPhysReg(unsigned virtReg);
181 /// assignVirt2PhysReg - assigns the free physical register to
182 /// the virtual register passed as arguments
183 void assignVirt2PhysReg(unsigned virtReg, unsigned physReg);
185 /// clearVirtReg - free the physical register associated with this
186 /// virtual register and disassociate virtual->physical and
187 /// physical->virtual mappings
188 void clearVirtReg(unsigned virtReg);
190 /// assignVirt2StackSlot - assigns this virtual register to a
192 void assignVirt2StackSlot(unsigned virtReg);
194 /// getStackSlot - returns the offset of the specified
195 /// register on the stack
196 int getStackSlot(unsigned virtReg);
198 /// spillVirtReg - spills the virtual register
199 void spillVirtReg(unsigned virtReg);
201 /// loadPhysReg - loads to the physical register the value of
202 /// the virtual register specifed. Virtual register must have
203 /// an assigned stack slot
204 void loadVirt2PhysReg(unsigned virtReg, unsigned physReg);
206 void printVirtRegAssignment() const {
207 std::cerr << "register assignment:\n";
209 for (Virt2PhysMap::const_iterator
210 i = v2pMap_.begin(), e = v2pMap_.end(); i != e; ++i) {
211 assert(i->second != 0);
212 std::cerr << '[' << i->first << ','
213 << mri_->getName(i->second) << "]\n";
215 for (Virt2StackSlotMap::const_iterator
216 i = v2ssMap_.begin(), e = v2ssMap_.end(); i != e; ++i) {
217 std::cerr << '[' << i->first << ",ss#" << i->second << "]\n";
222 void printIntervals(const char* const str,
223 RA::IntervalPtrs::const_iterator i,
224 RA::IntervalPtrs::const_iterator e) const {
225 if (str) std::cerr << str << " intervals:\n";
226 for (; i != e; ++i) {
227 std::cerr << "\t\t" << **i << " -> ";
228 unsigned reg = (*i)->reg;
229 if (MRegisterInfo::isVirtualRegister(reg)) {
230 Virt2PhysMap::const_iterator it = v2pMap_.find(reg);
231 reg = (it == v2pMap_.end() ? 0 : it->second);
233 std::cerr << mri_->getName(reg) << '\n';
237 // void printFreeRegs(const char* const str,
238 // const TargetRegisterClass* rc) const {
239 // if (str) std::cerr << str << ':';
240 // for (TargetRegisterClass::iterator i =
241 // rc->allocation_order_begin(*mf_);
242 // i != rc->allocation_order_end(*mf_); ++i) {
243 // unsigned reg = *i;
244 // if (!regUse_[reg]) {
245 // std::cerr << ' ' << mri_->getName(reg);
246 // if (reserved_[reg]) std::cerr << "*";
249 // std::cerr << '\n';
254 void RA::releaseMemory()
265 bool RA::runOnMachineFunction(MachineFunction &fn) {
267 tm_ = &fn.getTarget();
268 mri_ = tm_->getRegisterInfo();
269 li_ = &getAnalysis<LiveIntervals>();
270 prt_ = PhysRegTracker(mf_);
272 initIntervalSets(li_->getIntervals());
274 // FIXME: this will work only for the X86 backend. I need to
275 // device an algorthm to select the minimal (considering register
276 // aliasing) number of temp registers to reserve so that we have 2
277 // registers for each register class available.
279 // reserve R8: CH, CL
283 prt_.reservePhysReg( 8); /* CH */
284 prt_.reservePhysReg( 9); /* CL */
285 prt_.reservePhysReg(10); /* CX */
286 prt_.reservePhysReg(12); /* DI */
287 prt_.reservePhysReg(18); /* ECX */
288 prt_.reservePhysReg(19); /* EDI */
289 prt_.reservePhysReg(28); /* FP5 */
290 prt_.reservePhysReg(29); /* FP6 */
292 // linear scan algorithm
293 DEBUG(std::cerr << "Machine Function\n");
295 DEBUG(printIntervals("\tunhandled", unhandled_.begin(), unhandled_.end()));
296 DEBUG(printIntervals("\tfixed", fixed_.begin(), fixed_.end()));
297 DEBUG(printIntervals("\tactive", active_.begin(), active_.end()));
298 DEBUG(printIntervals("\tinactive", inactive_.begin(), inactive_.end()));
300 while (!unhandled_.empty() || !fixed_.empty()) {
301 // pick the interval with the earliest start point
302 IntervalPtrs::value_type cur;
303 if (fixed_.empty()) {
304 cur = unhandled_.front();
305 unhandled_.erase(unhandled_.begin());
307 else if (unhandled_.empty()) {
308 cur = fixed_.front();
309 fixed_.erase(fixed_.begin());
311 else if (unhandled_.front()->start() < fixed_.front()->start()) {
312 cur = unhandled_.front();
313 unhandled_.erase(unhandled_.begin());
316 cur = fixed_.front();
317 fixed_.erase(fixed_.begin());
320 DEBUG(std::cerr << *cur << '\n');
322 processActiveIntervals(cur);
323 processInactiveIntervals(cur);
325 // if this register is fixed we are done
326 if (MRegisterInfo::isPhysicalRegister(cur->reg)) {
327 prt_.addPhysRegUse(cur->reg);
328 active_.push_back(cur);
330 // otherwise we are allocating a virtual register. try to find
331 // a free physical register or spill an interval in order to
332 // assign it one (we could spill the current though).
334 PhysRegTracker backupPrt = prt_;
336 // for every interval in inactive we overlap with, mark the
337 // register as not free
338 for (IntervalPtrs::const_iterator i = inactive_.begin(),
339 e = inactive_.end(); i != e; ++i) {
340 unsigned reg = (*i)->reg;
341 if (MRegisterInfo::isVirtualRegister(reg))
344 if (cur->overlaps(**i)) {
345 prt_.addPhysRegUse(reg);
349 // for every interval in fixed we overlap with,
350 // mark the register as not free
351 for (IntervalPtrs::const_iterator i = fixed_.begin(),
352 e = fixed_.end(); i != e; ++i) {
353 assert(MRegisterInfo::isPhysicalRegister((*i)->reg) &&
354 "virtual register interval in fixed set?");
355 if (cur->overlaps(**i))
356 prt_.addPhysRegUse((*i)->reg);
359 DEBUG(std::cerr << "\tallocating current interval:\n");
361 unsigned physReg = getFreePhysReg(cur);
363 assignStackSlotAtInterval(cur, backupPrt);
367 assignVirt2PhysReg(cur->reg, physReg);
368 active_.push_back(cur);
372 DEBUG(printIntervals("\tactive", active_.begin(), active_.end()));
373 DEBUG(printIntervals("\tinactive", inactive_.begin(), inactive_.end())); }
375 // expire any remaining active intervals
376 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
377 unsigned reg = (*i)->reg;
378 DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n");
379 if (MRegisterInfo::isVirtualRegister(reg)) {
382 prt_.delPhysRegUse(reg);
385 typedef LiveIntervals::Reg2RegMap Reg2RegMap;
386 const Reg2RegMap& r2rMap = li_->getJoinedRegMap();
388 DEBUG(printVirtRegAssignment());
389 DEBUG(std::cerr << "Performing coalescing on joined intervals\n");
390 // perform coalescing if we were passed joined intervals
391 for(Reg2RegMap::const_iterator i = r2rMap.begin(), e = r2rMap.end();
393 unsigned reg = i->first;
394 unsigned rep = li_->rep(reg);
396 assert((MRegisterInfo::isPhysicalRegister(rep) ||
397 v2pMap_.count(rep) || v2ssMap_.count(rep)) &&
398 "representative register is not allocated!");
400 assert(MRegisterInfo::isVirtualRegister(reg) &&
401 !v2pMap_.count(reg) && !v2ssMap_.count(reg) &&
402 "coalesced register is already allocated!");
404 if (MRegisterInfo::isPhysicalRegister(rep)) {
405 v2pMap_.insert(std::make_pair(reg, rep));
408 Virt2PhysMap::const_iterator pr = v2pMap_.find(rep);
409 if (pr != v2pMap_.end()) {
410 v2pMap_.insert(std::make_pair(reg, pr->second));
413 Virt2StackSlotMap::const_iterator ss = v2ssMap_.find(rep);
414 assert(ss != v2ssMap_.end());
415 v2ssMap_.insert(std::make_pair(reg, ss->second));
420 DEBUG(printVirtRegAssignment());
421 DEBUG(std::cerr << "finished register allocation\n");
423 const TargetInstrInfo& tii = tm_->getInstrInfo();
425 DEBUG(std::cerr << "Rewrite machine code:\n");
426 for (currentMbb_ = mf_->begin(); currentMbb_ != mf_->end(); ++currentMbb_) {
429 for (currentInstr_ = currentMbb_->begin();
430 currentInstr_ != currentMbb_->end(); ) {
432 DEBUG(std::cerr << "\tinstruction: ";
433 (*currentInstr_)->print(std::cerr, *tm_););
435 // use our current mapping and actually replace and
436 // virtual register with its allocated physical registers
437 DEBUG(std::cerr << "\t\treplacing virtual registers with mapped "
438 "physical registers:\n");
439 for (unsigned i = 0, e = (*currentInstr_)->getNumOperands();
441 MachineOperand& op = (*currentInstr_)->getOperand(i);
442 if (op.isVirtualRegister()) {
443 unsigned virtReg = op.getAllocatedRegNum();
444 Virt2PhysMap::const_iterator it = v2pMap_.find(virtReg);
445 if (it != v2pMap_.end()) {
446 DEBUG(std::cerr << "\t\t\t%reg" << it->first
447 << " -> " << mri_->getName(it->second) << '\n');
448 (*currentInstr_)->SetMachineOperandReg(i, it->second);
453 unsigned srcReg, dstReg;
454 if (tii.isMoveInstr(**currentInstr_, srcReg, dstReg) &&
455 ((MRegisterInfo::isPhysicalRegister(srcReg) &&
456 MRegisterInfo::isPhysicalRegister(dstReg) &&
458 (MRegisterInfo::isVirtualRegister(srcReg) &&
459 MRegisterInfo::isVirtualRegister(dstReg) &&
460 v2ssMap_[srcReg] == v2ssMap_[dstReg]))) {
461 delete *currentInstr_;
462 currentInstr_ = currentMbb_->erase(currentInstr_);
464 DEBUG(std::cerr << "\t\tdeleting instruction\n");
468 DEBUG(std::cerr << "\t\tloading temporarily used operands to "
470 for (unsigned i = 0, e = (*currentInstr_)->getNumOperands();
472 MachineOperand& op = (*currentInstr_)->getOperand(i);
473 if (op.isVirtualRegister() && op.isUse() && !op.isDef()) {
474 unsigned virtReg = op.getAllocatedRegNum();
475 unsigned physReg = 0;
476 Virt2PhysMap::const_iterator it = v2pMap_.find(virtReg);
477 if (it != v2pMap_.end()) {
478 physReg = it->second;
481 physReg = getFreeTempPhysReg(virtReg);
482 loadVirt2PhysReg(virtReg, physReg);
483 tempUseOperands_.push_back(virtReg);
485 (*currentInstr_)->SetMachineOperandReg(i, physReg);
489 DEBUG(std::cerr << "\t\tclearing temporarily used operands:\n");
490 for (unsigned i = 0, e = tempUseOperands_.size(); i != e; ++i) {
491 clearVirtReg(tempUseOperands_[i]);
493 tempUseOperands_.clear();
495 DEBUG(std::cerr << "\t\tassigning temporarily defined operands to "
497 for (unsigned i = 0, e = (*currentInstr_)->getNumOperands();
499 MachineOperand& op = (*currentInstr_)->getOperand(i);
500 if (op.isVirtualRegister() && op.isDef()) {
501 unsigned virtReg = op.getAllocatedRegNum();
502 unsigned physReg = 0;
503 Virt2PhysMap::const_iterator it = v2pMap_.find(virtReg);
504 if (it != v2pMap_.end()) {
505 physReg = it->second;
508 physReg = getFreeTempPhysReg(virtReg);
510 if (op.isUse()) { // def and use
511 loadVirt2PhysReg(virtReg, physReg);
514 assignVirt2PhysReg(virtReg, physReg);
516 tempDefOperands_.push_back(virtReg);
517 (*currentInstr_)->SetMachineOperandReg(i, physReg);
521 DEBUG(std::cerr << "\t\tspilling temporarily defined operands "
522 "of this instruction:\n");
523 ++currentInstr_; // we want to insert after this instruction
524 for (unsigned i = 0, e = tempDefOperands_.size(); i != e; ++i) {
525 spillVirtReg(tempDefOperands_[i]);
527 --currentInstr_; // restore currentInstr_ iterator
528 tempDefOperands_.clear();
536 void RA::initIntervalSets(const LiveIntervals::Intervals& li)
538 assert(unhandled_.empty() && fixed_.empty() &&
539 active_.empty() && inactive_.empty() &&
540 "interval sets should be empty on initialization");
542 for (LiveIntervals::Intervals::const_iterator i = li.begin(), e = li.end();
544 if (MRegisterInfo::isPhysicalRegister(i->reg))
545 fixed_.push_back(&*i);
547 unhandled_.push_back(&*i);
551 void RA::processActiveIntervals(IntervalPtrs::value_type cur)
553 DEBUG(std::cerr << "\tprocessing active intervals:\n");
554 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end();) {
555 unsigned reg = (*i)->reg;
556 // remove expired intervals
557 if ((*i)->expiredAt(cur->start())) {
558 DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n");
559 if (MRegisterInfo::isVirtualRegister(reg)) {
562 prt_.delPhysRegUse(reg);
563 // remove from active
564 i = active_.erase(i);
566 // move inactive intervals to inactive list
567 else if (!(*i)->liveAt(cur->start())) {
568 DEBUG(std::cerr << "\t\t\tinterval " << **i << " inactive\n");
569 if (MRegisterInfo::isVirtualRegister(reg)) {
572 prt_.delPhysRegUse(reg);
574 inactive_.push_back(*i);
575 // remove from active
576 i = active_.erase(i);
584 void RA::processInactiveIntervals(IntervalPtrs::value_type cur)
586 DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
587 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end();) {
588 unsigned reg = (*i)->reg;
590 // remove expired intervals
591 if ((*i)->expiredAt(cur->start())) {
592 DEBUG(std::cerr << "\t\t\tinterval " << **i << " expired\n");
593 // remove from inactive
594 i = inactive_.erase(i);
596 // move re-activated intervals in active list
597 else if ((*i)->liveAt(cur->start())) {
598 DEBUG(std::cerr << "\t\t\tinterval " << **i << " active\n");
599 if (MRegisterInfo::isVirtualRegister(reg)) {
602 prt_.addPhysRegUse(reg);
604 active_.push_back(*i);
605 // remove from inactive
606 i = inactive_.erase(i);
615 template <typename T>
616 void updateWeight(std::vector<T>& rw, int reg, T w)
618 if (rw[reg] == std::numeric_limits<T>::max() ||
619 w == std::numeric_limits<T>::max())
620 rw[reg] = std::numeric_limits<T>::max();
626 void RA::assignStackSlotAtInterval(IntervalPtrs::value_type cur,
627 const PhysRegTracker& backupPrt)
629 DEBUG(std::cerr << "\t\tassigning stack slot at interval "
632 std::vector<float> regWeight(mri_->getNumRegs(), 0.0);
634 // for each interval in active
635 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
637 unsigned reg = (*i)->reg;
638 if (MRegisterInfo::isVirtualRegister(reg)) {
641 updateWeight(regWeight, reg, (*i)->weight);
642 for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as)
643 updateWeight(regWeight, *as, (*i)->weight);
646 // for each interval in inactive that overlaps
647 for (IntervalPtrs::const_iterator i = inactive_.begin(),
648 e = inactive_.end(); i != e; ++i) {
649 if (!cur->overlaps(**i))
652 unsigned reg = (*i)->reg;
653 if (MRegisterInfo::isVirtualRegister(reg)) {
656 updateWeight(regWeight, reg, (*i)->weight);
657 for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as)
658 updateWeight(regWeight, *as, (*i)->weight);
661 // for each fixed interval that overlaps
662 for (IntervalPtrs::const_iterator i = fixed_.begin(), e = fixed_.end();
664 if (!cur->overlaps(**i))
667 assert(MRegisterInfo::isPhysicalRegister((*i)->reg) &&
668 "virtual register interval in fixed set?");
669 updateWeight(regWeight, (*i)->reg, (*i)->weight);
670 for (const unsigned* as = mri_->getAliasSet((*i)->reg); *as; ++as)
671 updateWeight(regWeight, *as, (*i)->weight);
674 float minWeight = std::numeric_limits<float>::max();
676 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
677 for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_);
678 i != rc->allocation_order_end(*mf_); ++i) {
680 if (!prt_.isPhysRegReserved(reg) && minWeight > regWeight[reg]) {
681 minWeight = regWeight[reg];
685 DEBUG(std::cerr << "\t\t\tregister with min weight: "
686 << mri_->getName(minReg) << " (" << minWeight << ")\n");
688 if (cur->weight < minWeight) {
690 DEBUG(std::cerr << "\t\t\t\tspilling: " << *cur << '\n');
691 assignVirt2StackSlot(cur->reg);
694 std::vector<bool> toSpill(mri_->getNumRegs(), false);
695 toSpill[minReg] = true;
696 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
699 std::vector<unsigned> spilled;
700 for (IntervalPtrs::iterator i = active_.begin();
701 i != active_.end(); ) {
702 unsigned reg = (*i)->reg;
703 if (MRegisterInfo::isVirtualRegister(reg) &&
704 toSpill[v2pMap_[reg]] &&
705 cur->overlaps(**i)) {
706 spilled.push_back(v2pMap_[reg]);
707 DEBUG(std::cerr << "\t\t\t\tspilling : " << **i << '\n');
708 assignVirt2StackSlot(reg);
709 i = active_.erase(i);
715 for (IntervalPtrs::iterator i = inactive_.begin();
716 i != inactive_.end(); ) {
717 unsigned reg = (*i)->reg;
718 if (MRegisterInfo::isVirtualRegister(reg) &&
719 toSpill[v2pMap_[reg]] &&
720 cur->overlaps(**i)) {
721 DEBUG(std::cerr << "\t\t\t\tspilling : " << **i << '\n');
722 assignVirt2StackSlot(reg);
723 i = inactive_.erase(i);
730 unsigned physReg = getFreePhysReg(cur);
731 assert(physReg && "no free physical register after spill?");
734 for (unsigned i = 0; i < spilled.size(); ++i)
735 prt_.delPhysRegUse(spilled[i]);
737 assignVirt2PhysReg(cur->reg, physReg);
738 active_.push_back(cur);
742 unsigned RA::getFreePhysReg(IntervalPtrs::value_type cur)
744 DEBUG(std::cerr << "\t\tgetting free physical register: ");
745 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
747 for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_);
748 i != rc->allocation_order_end(*mf_); ++i) {
750 if (prt_.isPhysRegAvail(reg)) {
751 DEBUG(std::cerr << mri_->getName(reg) << '\n');
756 DEBUG(std::cerr << "no free register\n");
760 unsigned RA::getFreeTempPhysReg(unsigned virtReg)
762 DEBUG(std::cerr << "\t\tgetting free temporary physical register: ");
764 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(virtReg);
765 // go in reverse allocation order for the temp registers
766 typedef std::reverse_iterator<TargetRegisterClass::iterator> TRCRevIter;
768 i(rc->allocation_order_end(*mf_)),
769 e(rc->allocation_order_begin(*mf_)); i != e; ++i) {
771 if (prt_.isReservedPhysRegAvail(reg)) {
772 DEBUG(std::cerr << mri_->getName(reg) << '\n');
777 assert(0 && "no free temporary physical register?");
781 void RA::assignVirt2PhysReg(unsigned virtReg, unsigned physReg)
783 bool inserted = v2pMap_.insert(std::make_pair(virtReg, physReg)).second;
784 assert(inserted && "attempting to assign a virt->phys mapping to an "
785 "already mapped register");
786 prt_.addPhysRegUse(physReg);
789 void RA::clearVirtReg(unsigned virtReg)
791 Virt2PhysMap::iterator it = v2pMap_.find(virtReg);
792 assert(it != v2pMap_.end() &&
793 "attempting to clear a not allocated virtual register");
794 unsigned physReg = it->second;
795 prt_.delPhysRegUse(physReg);
797 DEBUG(std::cerr << "\t\t\tcleared register " << mri_->getName(physReg)
801 void RA::assignVirt2StackSlot(unsigned virtReg)
803 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(virtReg);
804 int frameIndex = mf_->getFrameInfo()->CreateStackObject(rc);
806 bool inserted = v2ssMap_.insert(std::make_pair(virtReg, frameIndex)).second;
808 "attempt to assign stack slot to already assigned register?");
809 // if the virtual register was previously assigned clear the mapping
810 // and free the virtual register
811 if (v2pMap_.count(virtReg)) {
812 clearVirtReg(virtReg);
816 int RA::getStackSlot(unsigned virtReg)
818 // use lower_bound so that we can do a possibly O(1) insert later
820 Virt2StackSlotMap::iterator it = v2ssMap_.find(virtReg);
821 assert(it != v2ssMap_.end() &&
822 "attempt to get stack slot on register that does not live on the stack");
826 void RA::spillVirtReg(unsigned virtReg)
828 DEBUG(std::cerr << "\t\t\tspilling register: " << virtReg);
829 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(virtReg);
830 int frameIndex = getStackSlot(virtReg);
831 DEBUG(std::cerr << " to stack slot #" << frameIndex << '\n');
833 instrAdded_ += mri_->storeRegToStackSlot(*currentMbb_, currentInstr_,
834 v2pMap_[virtReg], frameIndex, rc);
835 clearVirtReg(virtReg);
838 void RA::loadVirt2PhysReg(unsigned virtReg, unsigned physReg)
840 DEBUG(std::cerr << "\t\t\tloading register: " << virtReg);
841 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(virtReg);
842 int frameIndex = getStackSlot(virtReg);
843 DEBUG(std::cerr << " from stack slot #" << frameIndex << '\n');
845 instrAdded_ += mri_->loadRegFromStackSlot(*currentMbb_, currentInstr_,
846 physReg, frameIndex, rc);
847 assignVirt2PhysReg(virtReg, physReg);
850 FunctionPass* llvm::createLinearScanRegisterAllocator() {