[llvm-commits] CVS: llvm/lib/CodeGen/TwoAddressInstructionPass.cpp RegAllocLinearScan.cpp MachineInstr.cpp

Alkis Evlogimenos alkis at cs.uiuc.edu
Wed Feb 4 16:18:25 PST 2004


Changes in directory llvm/lib/CodeGen:

TwoAddressInstructionPass.cpp updated: 1.11 -> 1.12
RegAllocLinearScan.cpp updated: 1.40 -> 1.41
MachineInstr.cpp updated: 1.83 -> 1.84

---
Log message:

Modify the two address instruction pass to remove the duplicate
operand of the instruction and thus simplify the register allocation.


---
Diffs of the changes:  (+93 -103)

Index: llvm/lib/CodeGen/TwoAddressInstructionPass.cpp
diff -u llvm/lib/CodeGen/TwoAddressInstructionPass.cpp:1.11 llvm/lib/CodeGen/TwoAddressInstructionPass.cpp:1.12
--- llvm/lib/CodeGen/TwoAddressInstructionPass.cpp:1.11	Mon Feb  2 17:08:58 2004
+++ llvm/lib/CodeGen/TwoAddressInstructionPass.cpp	Wed Feb  4 16:17:40 2004
@@ -16,11 +16,14 @@
 // to:
 //
 //     A = B
-//     A = A op C
+//     A op= C
 //
-// Note that if a register allocator chooses to use this pass, that it has to
-// be capable of handling the non-SSA nature of these rewritten virtual 
-// registers.
+// Note that if a register allocator chooses to use this pass, that it
+// has to be capable of handling the non-SSA nature of these rewritten
+// virtual registers.
+//
+// It is also worth noting that the duplicate operand of the two
+// address instruction is removed.
 //
 //===----------------------------------------------------------------------===//
 
@@ -98,63 +101,70 @@
                    mi->getOperand(1).isUse() &&
                    "two address instruction invalid");
 
-            // we have nothing to do if the two operands are the same
+            // if the two operands are the same we just remove the use
+            // and mark the def as def&use
             if (mi->getOperand(0).getAllocatedRegNum() ==
-                mi->getOperand(1).getAllocatedRegNum())
-                continue;
-
-            MadeChange = true;
+                mi->getOperand(1).getAllocatedRegNum()) {
+            }
+            else {
+                MadeChange = true;
 
-            // rewrite:
-            //     a = b op c
-            // to:
-            //     a = b
-            //     a = a op c
-            unsigned regA = mi->getOperand(0).getAllocatedRegNum();
-            unsigned regB = mi->getOperand(1).getAllocatedRegNum();
-
-            assert(MRegisterInfo::isVirtualRegister(regA) &&
-                   MRegisterInfo::isVirtualRegister(regB) &&
-                   "cannot update physical register live information");
-
-            // first make sure we do not have a use of a in the
-            // instruction (a = b + a for example) because our
-            // transformation will not work. This should never occur
-            // because we are in SSA form.
-            for (unsigned i = 1; i != mi->getNumOperands(); ++i)
-                assert(!mi->getOperand(i).isRegister() ||
-                       mi->getOperand(i).getAllocatedRegNum() != (int)regA);
-
-            const TargetRegisterClass* rc =MF.getSSARegMap()->getRegClass(regA);
-            unsigned Added = MRI.copyRegToReg(*mbbi, mii, regA, regB, rc);
-            numInstrsAdded += Added;
-
-            MachineInstr* prevMi = *(mii - 1);
-            DEBUG(std::cerr << "\t\tadded instruction: ";
-                  prevMi->print(std::cerr, TM));
-
-            // update live variables for regA
-            assert(Added == 1 && "Cannot handle multi-instruction copies yet!");
-            LiveVariables::VarInfo& varInfo = LV.getVarInfo(regA);
-            varInfo.DefInst = prevMi;
-
-            // update live variables for regB
-            if (LV.removeVirtualRegisterKilled(regB, &*mbbi, mi))
-                LV.addVirtualRegisterKilled(regB, &*mbbi, prevMi);
-
-            if (LV.removeVirtualRegisterDead(regB, &*mbbi, mi))
-                LV.addVirtualRegisterDead(regB, &*mbbi, prevMi);
-
-            // replace all occurences of regB with regA
-            for (unsigned i = 1; i < mi->getNumOperands(); ++i) {
-                if (mi->getOperand(i).isRegister() &&
-                    mi->getOperand(i).getReg() == regB)
-                    mi->SetMachineOperandReg(i, regA);
+                // rewrite:
+                //     a = b op c
+                // to:
+                //     a = b
+                //     a = a op c
+                unsigned regA = mi->getOperand(0).getAllocatedRegNum();
+                unsigned regB = mi->getOperand(1).getAllocatedRegNum();
+
+                assert(MRegisterInfo::isVirtualRegister(regA) &&
+                       MRegisterInfo::isVirtualRegister(regB) &&
+                       "cannot update physical register live information");
+
+                // first make sure we do not have a use of a in the
+                // instruction (a = b + a for example) because our
+                // transformation will not work. This should never occur
+                // because we are in SSA form.
+                for (unsigned i = 1; i != mi->getNumOperands(); ++i)
+                    assert(!mi->getOperand(i).isRegister() ||
+                           mi->getOperand(i).getAllocatedRegNum() != (int)regA);
+
+                const TargetRegisterClass* rc =
+                    MF.getSSARegMap()->getRegClass(regA);
+                unsigned Added = MRI.copyRegToReg(*mbbi, mii, regA, regB, rc);
+                numInstrsAdded += Added;
+
+                MachineInstr* prevMi = *(mii - 1);
+                DEBUG(std::cerr << "\t\tadded instruction: ";
+                      prevMi->print(std::cerr, TM));
+
+                // update live variables for regA
+                assert(Added == 1 &&
+                       "Cannot handle multi-instruction copies yet!");
+                LiveVariables::VarInfo& varInfo = LV.getVarInfo(regA);
+                varInfo.DefInst = prevMi;
+
+                // update live variables for regB
+                if (LV.removeVirtualRegisterKilled(regB, &*mbbi, mi))
+                    LV.addVirtualRegisterKilled(regB, &*mbbi, prevMi);
+
+                if (LV.removeVirtualRegisterDead(regB, &*mbbi, mi))
+                    LV.addVirtualRegisterDead(regB, &*mbbi, prevMi);
+
+                // replace all occurences of regB with regA
+                for (unsigned i = 1, e = mi->getNumOperands(); i != e; ++i) {
+                    if (mi->getOperand(i).isRegister() &&
+                        mi->getOperand(i).getReg() == regB)
+                        mi->SetMachineOperandReg(i, regA);
+                }
             }
+
+            assert(mi->getOperand(0).isDef());
+            mi->getOperand(0).setUse();
+            mi->RemoveOperand(1);
+
             DEBUG(std::cerr << "\t\tmodified original to: ";
                   mi->print(std::cerr, TM));
-            assert(mi->getOperand(0).getAllocatedRegNum() ==
-                   mi->getOperand(1).getAllocatedRegNum());
         }
     }
 


Index: llvm/lib/CodeGen/RegAllocLinearScan.cpp
diff -u llvm/lib/CodeGen/RegAllocLinearScan.cpp:1.40 llvm/lib/CodeGen/RegAllocLinearScan.cpp:1.41
--- llvm/lib/CodeGen/RegAllocLinearScan.cpp:1.40	Mon Feb  2 19:13:06 2004
+++ llvm/lib/CodeGen/RegAllocLinearScan.cpp	Wed Feb  4 16:17:40 2004
@@ -109,10 +109,6 @@
         typedef std::vector<const LiveIntervals::Interval*> IntervalPtrs;
         IntervalPtrs unhandled_, fixed_, active_, inactive_;
 
-        typedef std::vector<unsigned> Regs;
-        Regs tempUseOperands_;
-        Regs tempDefOperands_;
-
         PhysRegTracker prt_;
 
         typedef std::map<unsigned, unsigned> Virt2PhysMap;
@@ -428,7 +424,6 @@
 
         for (currentInstr_ = currentMbb_->begin();
              currentInstr_ != currentMbb_->end(); ) {
-
             DEBUG(std::cerr << "\tinstruction: ";
                   (*currentInstr_)->print(std::cerr, *tm_););
 
@@ -465,13 +460,17 @@
                 continue;
             }
 
+            typedef std::vector<unsigned> 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, e = (*currentInstr_)->getNumOperands();
-                 i != e; ++i) {
+            for (unsigned i = 0; i != numOperands; ++i) {
                 MachineOperand& op = (*currentInstr_)->getOperand(i);
-                if (op.isVirtualRegister() && op.isUse() &&
-                    !op.isEverDefined(**currentInstr_)) {
+                if (op.isVirtualRegister() && op.isUse()) {
                     unsigned virtReg = op.getAllocatedRegNum();
                     unsigned physReg = 0;
                     Virt2PhysMap::const_iterator it = v2pMap_.find(virtReg);
@@ -481,26 +480,28 @@
                     else {
                         physReg = getFreeTempPhysReg(virtReg);
                         loadVirt2PhysReg(virtReg, physReg);
-                        tempUseOperands_.push_back(virtReg);
+                        // we will clear uses that are not also defs
+                        // before we allocate registers the defs
+                        if (op.isDef())
+                            toSpill.push_back(virtReg);
+                        else
+                            toClear.push_back(virtReg);
                     }
                     (*currentInstr_)->SetMachineOperandReg(i, physReg);
                 }
             }
 
-            DEBUG(std::cerr << "\t\tclearing temporarily used operands:\n");
-            for (unsigned i = 0, e = tempUseOperands_.size(); i != e; ++i) {
-                clearVirtReg(tempUseOperands_[i]);
-            }
-            tempUseOperands_.clear();
+            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, e = (*currentInstr_)->getNumOperands();
-                 i != e; ++i) {
+            for (unsigned i = 0; i != numOperands; ++i) {
                 MachineOperand& op = (*currentInstr_)->getOperand(i);
                 if (op.isVirtualRegister()) {
-                    assert(op.isEverDefined(**currentInstr_) &&
-                           "operand should be defined by this instruction");
+                    assert(!op.isUse() && "we should not have uses here!");
                     unsigned virtReg = op.getAllocatedRegNum();
                     unsigned physReg = 0;
                     Virt2PhysMap::const_iterator it = v2pMap_.find(virtReg);
@@ -510,21 +511,18 @@
                     else {
                         physReg = getFreeTempPhysReg(virtReg);
                         assignVirt2PhysReg(virtReg, physReg);
-                        tempDefOperands_.push_back(virtReg);
+                        // need to spill this after we are done with
+                        // this instruction
+                        toSpill.push_back(virtReg);
                     }
                     (*currentInstr_)->SetMachineOperandReg(i, physReg);
                 }
             }
+            ++currentInstr_; // spills will go after this instruction
 
-            DEBUG(std::cerr << "\t\tspilling temporarily defined operands "
-                  "of this instruction:\n");
-            ++currentInstr_; // we want to insert after this instruction
-            for (unsigned i = 0, e = tempDefOperands_.size(); i != e; ++i) {
-                spillVirtReg(tempDefOperands_[i]);
-            }
-            --currentInstr_; // restore currentInstr_ iterator
-            tempDefOperands_.clear();
-            ++currentInstr_;
+            DEBUG(std::cerr << "\t\tspilling temporarily defined operands:\n");
+            std::for_each(toSpill.begin(), toSpill.end(),
+                          std::bind1st(std::mem_fun(&RA::spillVirtReg), this));
         }
     }
 


Index: llvm/lib/CodeGen/MachineInstr.cpp
diff -u llvm/lib/CodeGen/MachineInstr.cpp:1.83 llvm/lib/CodeGen/MachineInstr.cpp:1.84
--- llvm/lib/CodeGen/MachineInstr.cpp:1.83	Mon Feb  2 19:13:06 2004
+++ llvm/lib/CodeGen/MachineInstr.cpp	Wed Feb  4 16:17:40 2004
@@ -27,24 +27,6 @@
 //
 extern const TargetInstrDescriptor *TargetInstrDescriptors;
 
-bool MachineOperand::isEverUsed(const MachineInstr& mi) const
-{
-    for (int i = 0, e = mi.getNumOperands(); i != e; ++i) {
-        if (*this == mi.getOperand(i) && mi.getOperand(i).isUse())
-            return true;
-    }
-    return false;
-}
-
-bool MachineOperand::isEverDefined(const MachineInstr& mi) const
-{
-    for (int i = 0, e = mi.getNumOperands(); i != e; ++i) {
-        if (*this == mi.getOperand(i) && mi.getOperand(i).isDef())
-            return true;
-    }
-    return false;
-}
-
 // Constructor for instructions with variable #operands
 MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned  numOperands)
   : opCode(OpCode),





More information about the llvm-commits mailing list