[llvm-commits] [llvm] r41069 - /llvm/trunk/lib/CodeGen/VirtRegMap.cpp

Evan Cheng evan.cheng at apple.com
Tue Aug 14 02:11:19 PDT 2007


Author: evancheng
Date: Tue Aug 14 04:11:18 2007
New Revision: 41069

URL: http://llvm.org/viewvc/llvm-project?rev=41069&view=rev
Log:
If a spilled value is being reused and the use is a kill, that means there are
no more uses within the MBB and the spilled value isn't live out of the MBB.
Then it's safe to delete the spill store.

Modified:
    llvm/trunk/lib/CodeGen/VirtRegMap.cpp

Modified: llvm/trunk/lib/CodeGen/VirtRegMap.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/VirtRegMap.cpp?rev=41069&r1=41068&r2=41069&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/VirtRegMap.cpp (original)
+++ llvm/trunk/lib/CodeGen/VirtRegMap.cpp Tue Aug 14 04:11:18 2007
@@ -554,7 +554,7 @@
     /// a new register to use, or evict the previous reload and use this reg. 
     unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
                              AvailableSpills &Spills,
-                             std::map<int, MachineInstr*> &MaybeDeadStores,
+                             std::vector<MachineInstr*> &MaybeDeadStores,
                              SmallSet<unsigned, 8> &Rejected,
                              BitVector &RegKills,
                              std::vector<MachineOperand*> &KillOps,
@@ -609,14 +609,13 @@
             } else {
               MRI->loadRegFromStackSlot(*MBB, MI, NewPhysReg,
                                         NewOp.StackSlotOrReMat, AliasRC);
+              // Any stores to this stack slot are not dead anymore.
+              MaybeDeadStores[NewOp.StackSlotOrReMat] = NULL;            
               ++NumLoads;
             }
             Spills.ClobberPhysReg(NewPhysReg);
             Spills.ClobberPhysReg(NewOp.PhysRegReused);
             
-            // Any stores to this stack slot are not dead anymore.
-            MaybeDeadStores.erase(NewOp.StackSlotOrReMat);
-            
             MI->getOperand(NewOp.Operand).setReg(NewPhysReg);
             
             Spills.addAvailable(NewOp.StackSlotOrReMat, MI, NewPhysReg);
@@ -649,7 +648,7 @@
     ///       sees r1 is taken by t2, tries t2's reload register r0 ...
     unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
                              AvailableSpills &Spills,
-                             std::map<int, MachineInstr*> &MaybeDeadStores,
+                             std::vector<MachineInstr*> &MaybeDeadStores,
                              BitVector &RegKills,
                              std::vector<MachineOperand*> &KillOps,
                              VirtRegMap &VRM) {
@@ -666,6 +665,8 @@
 void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
   DOUT << MBB.getBasicBlock()->getName() << ":\n";
 
+  MachineFunction &MF = *MBB.getParent();
+
   // Spills - Keep track of which spilled values are available in physregs so
   // that we can choose to reuse the physregs instead of emitting reloads.
   AvailableSpills Spills(MRI, TII);
@@ -676,14 +677,14 @@
   // subsequently stored to, the original store is dead.  This map keeps track
   // of inserted stores that are not used.  If we see a subsequent store to the
   // same stack slot, the original store is deleted.
-  std::map<int, MachineInstr*> MaybeDeadStores;
+  std::vector<MachineInstr*> MaybeDeadStores;
+  MaybeDeadStores.resize(MF.getFrameInfo()->getObjectIndexEnd(), NULL);
 
   // Keep track of kill information.
   BitVector RegKills(MRI->getNumRegs());
   std::vector<MachineOperand*>  KillOps;
   KillOps.resize(MRI->getNumRegs(), NULL);
 
-  MachineFunction &MF = *MBB.getParent();
   for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
        MII != E; ) {
     MachineInstr &MI = *MII;
@@ -806,6 +807,21 @@
             // Only mark it clobbered if this is a use&def operand.
             ReusedOperands.markClobbered(PhysReg);
           ++NumReused;
+
+          if (MI.getOperand(i).isKill() &&
+              ReuseSlot <= VirtRegMap::MAX_STACK_SLOT) {
+            // This was the last use and the spilled value is still available
+            // for reuse. That means the spill was unnecessary!
+            MachineInstr* DeadStore = MaybeDeadStores[ReuseSlot];
+            if (DeadStore) {
+              DOUT << "Removed dead store:\t" << *DeadStore;
+              InvalidateKills(*DeadStore, RegKills, KillOps);
+              MBB.erase(DeadStore);
+              VRM.RemoveFromFoldedVirtMap(DeadStore);
+              MaybeDeadStores[ReuseSlot] = NULL;
+              ++NumDSE;
+            }
+          }
           continue;
         }
         
@@ -892,7 +908,7 @@
 
       // Any stores to this stack slot are not dead anymore.
       if (!DoReMat)
-        MaybeDeadStores.erase(SSorRMId);
+        MaybeDeadStores[SSorRMId] = NULL;
       Spills.addAvailable(SSorRMId, &MI, PhysReg);
       // Assumes this is the last use. IsKill will be unset if reg is reused
       // unless it's a two-address operand.
@@ -953,20 +969,18 @@
 
       // 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 {
+      MachineInstr* DeadStore = MaybeDeadStores[SS];
+      if (DeadStore) {
+        if (!(MR & VirtRegMap::isRef)) {  // Previous store is dead.
           // If we get here, the store is dead, nuke it now.
           assert(VirtRegMap::isMod && "Can't be modref!");
-          DOUT << "Removed dead store:\t" << *MDSI->second;
-          InvalidateKills(*MDSI->second, RegKills, KillOps);
-          MBB.erase(MDSI->second);
-          VRM.RemoveFromFoldedVirtMap(MDSI->second);
-          MaybeDeadStores.erase(MDSI);
+          DOUT << "Removed dead store:\t" << *DeadStore;
+          InvalidateKills(*DeadStore, RegKills, KillOps);
+          MBB.erase(DeadStore);
+          VRM.RemoveFromFoldedVirtMap(DeadStore);
           ++NumDSE;
         }
+        MaybeDeadStores[SS] = NULL;
       }
 
       // If the spill slot value is available, and this is a new definition of





More information about the llvm-commits mailing list