Fix SingleSource/Regression/C/2005-05-06-LongLongSignedShift.c, we were not
[oota-llvm.git] / lib / CodeGen / VirtRegMap.cpp
index 8e6ff027e97e911cceeaf9c6c8bb5ca95e088be1..b9925e2b20df083be6ae9392a73b2424c873eaac 100644 (file)
@@ -28,6 +28,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/STLExtras.h"
+#include <algorithm>
 using namespace llvm;
 
 namespace {
@@ -77,23 +78,26 @@ void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
   Virt2StackSlotMap[virtReg] = frameIndex;
 }
 
-void VirtRegMap::virtFolded(unsigned virtReg,
-                            MachineInstr* oldMI,
-                            MachineInstr* newMI) {
-  // move previous memory references folded to new instruction
-  std::vector<MI2VirtMapTy::mapped_type> regs;
-  for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(oldMI), 
-         E = MI2VirtMap.end(); I != E && I->first == oldMI; ) {
-    regs.push_back(I->second);
+void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
+                            unsigned OpNo, MachineInstr *NewMI) {
+  // Move previous memory references folded to new instruction.
+  MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
+  for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI), 
+         E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
+    MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
     MI2VirtMap.erase(I++);
   }
 
-  MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(newMI);
-  for (unsigned i = 0, e = regs.size(); i != e; ++i)
-    MI2VirtMap.insert(IP, std::make_pair(newMI, regs[i]));
+  ModRef MRInfo;
+  if (!OldMI->getOperand(OpNo).isDef()) {
+    assert(OldMI->getOperand(OpNo).isUse() && "Operand is not use or def?");
+    MRInfo = isRef;
+  } else {
+    MRInfo = OldMI->getOperand(OpNo).isUse() ? isModRef : isMod;
+  }
 
   // add new memory reference
-  MI2VirtMap.insert(IP, std::make_pair(newMI, virtReg));
+  MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
 }
 
 void VirtRegMap::print(std::ostream &OS) const {
@@ -129,13 +133,14 @@ namespace {
   };
 }
 
-bool SimpleSpiller::runOnMachineFunction(MachineFunctionMF,
-                                         const VirtRegMapVRM) {
+bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF,
+                                         const VirtRegMap &VRM) {
   DEBUG(std::cerr << "********** REWRITE MACHINE CODE **********\n");
   DEBUG(std::cerr << "********** Function: "
                   << MF.getFunction()->getName() << '\n');
-  const TargetMachine& TM = MF.getTarget();
-  const MRegisterInfo& MRI = *TM.getRegisterInfo();
+  const TargetMachine &TM = MF.getTarget();
+  const MRegisterInfo &MRI = *TM.getRegisterInfo();
+  bool *PhysRegsUsed = MF.getUsedPhysregs();
 
   // LoadedRegs - Keep track of which vregs are loaded, so that we only load
   // each vreg once (in the case where a spilled vreg is used by multiple
@@ -152,30 +157,34 @@ bool SimpleSpiller::runOnMachineFunction(MachineFunction& MF,
       MachineInstr &MI = *MII;
       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
         MachineOperand &MO = MI.getOperand(i);
-        if (MO.isRegister() && MO.getReg() &&
-            MRegisterInfo::isVirtualRegister(MO.getReg())) {
-          unsigned VirtReg = MO.getReg();
-          unsigned PhysReg = VRM.getPhys(VirtReg);
-          if (VRM.hasStackSlot(VirtReg)) {
-            int StackSlot = VRM.getStackSlot(VirtReg);
-
-            if (MO.isUse() &&
-                std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
-                           == LoadedRegs.end()) {
-              MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot);
-              LoadedRegs.push_back(VirtReg);
-              ++NumLoads;
-              DEBUG(std::cerr << '\t' << *prior(MII));
-            }
-
-            if (MO.isDef()) {
-              MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot);
-              ++NumStores;
+        if (MO.isRegister() && MO.getReg())
+          if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
+            unsigned VirtReg = MO.getReg();
+            unsigned PhysReg = VRM.getPhys(VirtReg);
+            if (VRM.hasStackSlot(VirtReg)) {
+              int StackSlot = VRM.getStackSlot(VirtReg);
+              
+              if (MO.isUse() &&
+                  std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
+                  == LoadedRegs.end()) {
+                MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot);
+                LoadedRegs.push_back(VirtReg);
+                ++NumLoads;
+                DEBUG(std::cerr << '\t' << *prior(MII));
+              }
+              
+              if (MO.isDef()) {
+                MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot);
+                ++NumStores;
+              }
             }
+            PhysRegsUsed[PhysReg] = true;
+            MI.SetMachineOperandReg(i, PhysReg);
+          } else {
+            PhysRegsUsed[MO.getReg()] = true;
           }
-          MI.SetMachineOperandReg(i, PhysReg);
-        }
       }
+
       DEBUG(std::cerr << '\t' << MI);
       LoadedRegs.clear();
     }
@@ -292,6 +301,8 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
   // same stack slot, the original store is deleted.
   std::map<int, MachineInstr*> MaybeDeadStores;
 
+  bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs();
+
   for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
        MII != E; ) {
     MachineInstr &MI = *MII;
@@ -304,12 +315,17 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
       MachineOperand &MO = MI.getOperand(i);
       if (MO.isRegister() && MO.getReg() &&
-          MRegisterInfo::isVirtualRegister(MO.getReg())) {
+          MRegisterInfo::isPhysicalRegister(MO.getReg()))
+        PhysRegsUsed[MO.getReg()] = true;
+      else if (MO.isRegister() && MO.getReg() &&
+               MRegisterInfo::isVirtualRegister(MO.getReg())) {
         unsigned VirtReg = MO.getReg();
 
         if (!VRM.hasStackSlot(VirtReg)) {
           // This virtual register was assigned a physreg!
-          MI.SetMachineOperandReg(i, VRM.getPhys(VirtReg));
+          unsigned Phys = VRM.getPhys(VirtReg);
+          PhysRegsUsed[Phys] = true;
+          MI.SetMachineOperandReg(i, Phys);
         } else {
           // Is this virtual register a spilled value?
           if (MO.isUse()) {
@@ -320,11 +336,13 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
             std::map<int, unsigned>::iterator SSI =
               SpillSlotsAvailable.find(StackSlot);
             if (SSI != SpillSlotsAvailable.end()) {
+              DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
+                              << MRI->getName(SSI->second) << " for vreg"
+                              << VirtReg <<" instead of reloading into physreg "
+                              << MRI->getName(VRM.getPhys(VirtReg)) << "\n");
               // If this stack slot value is already available, reuse it!
               PhysReg = SSI->second;
               MI.SetMachineOperandReg(i, PhysReg);
-              DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
-                              << MRI->getName(SSI->second) << "\n");
 
               // The only technical detail we have is that we don't know that
               // PhysReg won't be clobbered by a reloaded stack slot that occurs
@@ -347,6 +365,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
               // Otherwise, reload it and remember that we have it.
               PhysReg = VRM.getPhys(VirtReg);
 
+            RecheckRegister:
               // Note that, if we reused a register for a previous operand, the
               // register we want to reload into might not actually be
               // available.  If this occurs, use the register indicated by the
@@ -356,7 +375,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
                   if (ReusedOperands[ro].PhysRegReused == PhysReg) {
                     // Yup, use the reload register that we didn't use before.
                     PhysReg = ReusedOperands[ro].AssignedPhysReg;
-                    break;
+                    goto RecheckRegister;
                   } else {
                     ReusedOp &Op = ReusedOperands[ro];
                     unsigned PRRU = Op.PhysRegReused;
@@ -390,7 +409,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
                       }
                   }
             ContinueReload:
-
+              PhysRegsUsed[PhysReg] = true;
               MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot);
               // This invalidates PhysReg.
               ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
@@ -422,29 +441,50 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
 
     // Loop over all of the implicit defs, clearing them from our available
     // sets.
-    const TargetInstrDescriptor &InstrDesc = TII->get(MI.getOpcode());
-    for (const unsigned* ImpDef = InstrDesc.ImplicitDefs; *ImpDef; ++ImpDef)
+    for (const unsigned *ImpDef = TII->getImplicitDefs(MI.getOpcode());
+         *ImpDef; ++ImpDef) {
+      PhysRegsUsed[*ImpDef] = true;
       ClobberPhysReg(*ImpDef, SpillSlotsAvailable, PhysRegsAvailable);
+    }
 
     DEBUG(std::cerr << '\t' << MI);
 
     // If we have folded references to memory operands, make sure we clear all
     // physical registers that may contain the value of the spilled virtual
     // register
-    VirtRegMap::MI2VirtMapTy::const_iterator I, E;
-    for (tie(I, E) = VRM.getFoldedVirts(&MI); I != E; ++I) {
-      DEBUG(std::cerr << "Folded vreg: " << I->second);
-      if (VRM.hasStackSlot(I->second)) {
-        int SS = VRM.getStackSlot(I->second);
+    VirtRegMap::MI2VirtMapTy::const_iterator I, End;
+    for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
+      DEBUG(std::cerr << "Folded vreg: " << I->second.first << "  MR: "
+                      << I->second.second);
+      unsigned VirtReg = I->second.first;
+      VirtRegMap::ModRef MR = I->second.second;
+      if (VRM.hasStackSlot(VirtReg)) {
+        int SS = VRM.getStackSlot(VirtReg);
         DEBUG(std::cerr << " - StackSlot: " << SS << "\n");
 
-        // Any stores to this stack slot are not dead anymore.
-        MaybeDeadStores.erase(SS);
+        // If this reference is not a use, any previous store is now dead.
+        // Otherwise, the store to this stack slot is not dead anymore.
+        std::map<int, MachineInstr*>::iterator MDSI = MaybeDeadStores.find(SS);
+        if (MDSI != MaybeDeadStores.end()) {
+          if (MR & VirtRegMap::isRef)   // Previous store is not dead.
+            MaybeDeadStores.erase(MDSI);
+          else {
+            // If we get here, the store is dead, nuke it now.
+            assert(MR == VirtRegMap::isMod && "Can't be modref!");
+            MBB.erase(MDSI->second);
+            MaybeDeadStores.erase(MDSI);
+            ++NumDSE;
+          }
+        }
 
-        std::map<int, unsigned>::iterator I = SpillSlotsAvailable.find(SS);
-        if (I != SpillSlotsAvailable.end()) {
-          PhysRegsAvailable.erase(I->second);
-          SpillSlotsAvailable.erase(I);
+        // If the spill slot value is available, and this is a new definition of
+        // the value, the value is not available anymore.
+        if (MR & VirtRegMap::isMod) {
+          std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(SS);
+          if (It != SpillSlotsAvailable.end()) {
+            PhysRegsAvailable.erase(It->second);
+            SpillSlotsAvailable.erase(It);
+          }
         }
       } else {
         DEBUG(std::cerr << ": No stack slot!\n");
@@ -490,6 +530,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
           else
             PhysReg = MO.getReg();
 
+          PhysRegsUsed[PhysReg] = true;
           MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot);
           DEBUG(std::cerr << "Store:\t" << *next(MII));
           MI.SetMachineOperandReg(i, PhysReg);
@@ -497,6 +538,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
           // If there is a dead store to this stack slot, nuke it now.
           MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
           if (LastStore) {
+            DEBUG(std::cerr << " Killed store:\t" << *LastStore);
             ++NumDSE;
             MBB.erase(LastStore);
           }
@@ -517,7 +559,8 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
           PhysRegsAvailable[PhysReg] = StackSlot;
           SpillSlotsAvailable[StackSlot] = PhysReg;
           DEBUG(std::cerr << "Updating SS#" << StackSlot <<" in physreg "
-                          << MRI->getName(PhysReg) << "\n");
+                          << MRI->getName(PhysReg) << " for virtreg #"
+                          << VirtReg << "\n");
 
           ++NumStores;
           VirtReg = PhysReg;