- DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n");
- if (MRegisterInfo::isVirtualRegister(reg)) {
- reg = v2pMap_[reg];
- }
- prt_.delPhysRegUse(reg);
- }
-
- typedef LiveIntervals::Reg2RegMap Reg2RegMap;
- const Reg2RegMap& r2rMap = li_->getJoinedRegMap();
-
- DEBUG(printVirtRegAssignment());
- DEBUG(std::cerr << "Performing coalescing on joined intervals\n");
- // perform coalescing if we were passed joined intervals
- for(Reg2RegMap::const_iterator i = r2rMap.begin(), e = r2rMap.end();
- i != e; ++i) {
- unsigned reg = i->first;
- unsigned rep = li_->rep(reg);
-
- assert((MRegisterInfo::isPhysicalRegister(rep) ||
- v2pMap_.count(rep) || v2ssMap_.count(rep)) &&
- "representative register is not allocated!");
-
- assert(MRegisterInfo::isVirtualRegister(reg) &&
- !v2pMap_.count(reg) && !v2ssMap_.count(reg) &&
- "coalesced register is already allocated!");
-
- if (MRegisterInfo::isPhysicalRegister(rep)) {
- v2pMap_.insert(std::make_pair(reg, rep));
- }
- else {
- Virt2PhysMap::const_iterator pr = v2pMap_.find(rep);
- if (pr != v2pMap_.end()) {
- v2pMap_.insert(std::make_pair(reg, pr->second));
- }
- else {
- Virt2StackSlotMap::const_iterator ss = v2ssMap_.find(rep);
- assert(ss != v2ssMap_.end());
- v2ssMap_.insert(std::make_pair(reg, ss->second));
- }
- }
- }
-
- DEBUG(printVirtRegAssignment());
- DEBUG(std::cerr << "finished register allocation\n");
-
- const TargetInstrInfo& tii = tm_->getInstrInfo();
-
- DEBUG(std::cerr << "Rewrite machine code:\n");
- for (currentMbb_ = mf_->begin(); currentMbb_ != mf_->end(); ++currentMbb_) {
- instrAdded_ = 0;
-
- for (currentInstr_ = currentMbb_->begin();
- currentInstr_ != currentMbb_->end(); ) {
- DEBUG(std::cerr << "\tinstruction: ";
- (*currentInstr_)->print(std::cerr, *tm_););
-
- // use our current mapping and actually replace and
- // virtual register with its allocated physical registers
- DEBUG(std::cerr << "\t\treplacing virtual registers with mapped "
- "physical registers:\n");
- for (unsigned i = 0, e = (*currentInstr_)->getNumOperands();
- i != e; ++i) {
- MachineOperand& op = (*currentInstr_)->getOperand(i);
- if (op.isRegister() &&
- MRegisterInfo::isVirtualRegister(op.getReg())) {
- unsigned virtReg = op.getReg();
- Virt2PhysMap::const_iterator it = v2pMap_.find(virtReg);
- if (it != v2pMap_.end()) {
- DEBUG(std::cerr << "\t\t\t%reg" << it->first
- << " -> " << mri_->getName(it->second) << '\n');
- (*currentInstr_)->SetMachineOperandReg(i, it->second);
- }
- }
- }
-
- unsigned srcReg, dstReg;
- if (tii.isMoveInstr(**currentInstr_, srcReg, dstReg) &&
- ((MRegisterInfo::isPhysicalRegister(srcReg) &&
- MRegisterInfo::isPhysicalRegister(dstReg) &&
- srcReg == dstReg) ||
- (MRegisterInfo::isVirtualRegister(srcReg) &&
- MRegisterInfo::isVirtualRegister(dstReg) &&
- v2ssMap_[srcReg] == v2ssMap_[dstReg]))) {
- delete *currentInstr_;
- currentInstr_ = currentMbb_->erase(currentInstr_);
- ++numPeep;
- DEBUG(std::cerr << "\t\tdeleting instruction\n");
- continue;
- }
-
- typedef std::vector<Virt2PhysMap::iterator> Regs;
- Regs toClear;
- Regs toSpill;
-
- const unsigned numOperands = (*currentInstr_)->getNumOperands();
-
- DEBUG(std::cerr << "\t\tloading temporarily used operands to "
- "registers:\n");
- for (unsigned i = 0; i != numOperands; ++i) {
- MachineOperand& op = (*currentInstr_)->getOperand(i);
- if (op.isRegister() && op.isUse() &&
- MRegisterInfo::isVirtualRegister(op.getReg())) {
- unsigned virtReg = op.getAllocatedRegNum();
- unsigned physReg = 0;
- Virt2PhysMap::iterator it = v2pMap_.find(virtReg);
- if (it != v2pMap_.end()) {
- physReg = it->second;
- }
- else {
- physReg = getFreeTempPhysReg(virtReg);
- it = loadVirt2PhysReg(virtReg, physReg);
- // we will clear uses that are not also defs
- // before we allocate registers the defs
- if (op.isDef())
- toSpill.push_back(it);
- else
- toClear.push_back(it);
- }
- (*currentInstr_)->SetMachineOperandReg(i, physReg);
- }
- }
-
- DEBUG(std::cerr << "\t\tclearing temporarily used but not defined "
- "operands:\n");
- std::for_each(toClear.begin(), toClear.end(),
- std::bind1st(std::mem_fun(&RA::clearVirtReg), this));
-
- DEBUG(std::cerr << "\t\tassigning temporarily defined operands to "
- "registers:\n");
- for (unsigned i = 0; i != numOperands; ++i) {
- MachineOperand& op = (*currentInstr_)->getOperand(i);
- if (op.isRegister() &&
- MRegisterInfo::isVirtualRegister(op.getReg())) {
- assert(!op.isUse() && "we should not have uses here!");
- unsigned virtReg = op.getReg();
- unsigned physReg = 0;
- Virt2PhysMap::iterator it = v2pMap_.find(virtReg);
- if (it != v2pMap_.end()) {
- physReg = it->second;
- }
- else {
- physReg = getFreeTempPhysReg(virtReg);
- it = assignVirt2PhysReg(virtReg, physReg);
- // need to spill this after we are done with
- // this instruction
- toSpill.push_back(it);
- }
- (*currentInstr_)->SetMachineOperandReg(i, physReg);
- }
- }
- ++currentInstr_; // spills will go after this instruction
-
- DEBUG(std::cerr << "\t\tspilling temporarily defined operands:\n");
- std::for_each(toSpill.begin(), toSpill.end(),
- std::bind1st(std::mem_fun(&RA::spillVirtReg), this));
- }