- 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]))) {
- 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));
- }