[llvm-commits] [llvm] r120465 - /llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp

Chris Lattner sabre at nondot.org
Tue Nov 30 13:32:12 PST 2010


Author: lattner
Date: Tue Nov 30 15:32:12 2010
New Revision: 120465

URL: http://llvm.org/viewvc/llvm-project?rev=120465&view=rev
Log:
remove the "undead" terminology, which is nonstandard and never 
made sense to me.  We now have a set of dead stack objects, and
they become live when loaded.  Fix a theoretical problem where
we'd pass in the wrong pointer to the alias query.

Modified:
    llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp?rev=120465&r1=120464&r2=120465&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp Tue Nov 30 15:32:12 2010
@@ -64,9 +64,9 @@
     bool runOnBasicBlock(BasicBlock &BB);
     bool HandleFree(CallInst *F);
     bool handleEndBlock(BasicBlock &BB);
-    bool RemoveUndeadPointers(Value *Ptr, uint64_t killPointerSize,
-                              BasicBlock::iterator &BBI,
-                              SmallPtrSet<Value*, 16> &deadPointers);
+    bool RemoveAccessedObjects(Value *Ptr, uint64_t killPointerSize,
+                               BasicBlock::iterator &BBI,
+                               SmallPtrSet<Value*, 16> &deadPointers);
     void DeleteDeadInstruction(Instruction *I,
                                SmallPtrSet<Value*, 16> *deadPointers = 0);
     
@@ -399,21 +399,22 @@
 bool DSE::handleEndBlock(BasicBlock &BB) {
   bool MadeChange = false;
   
-  // Pointers alloca'd in this function are dead in the end block
-  SmallPtrSet<Value*, 16> DeadPointers;
+  // Keep track of all of the stack objects that are dead at the end of the
+  // function.
+  SmallPtrSet<Value*, 16> DeadStackObjects;
   
   // Find all of the alloca'd pointers in the entry block.
   BasicBlock *Entry = BB.getParent()->begin();
   for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
     if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
-      DeadPointers.insert(AI);
+      DeadStackObjects.insert(AI);
   
   // Treat byval arguments the same, stores to them are dead at the end of the
   // function.
   for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
        AE = BB.getParent()->arg_end(); AI != AE; ++AI)
     if (AI->hasByValAttr())
-      DeadPointers.insert(AI);
+      DeadStackObjects.insert(AI);
   
   // Scan the basic block backwards
   for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
@@ -426,10 +427,10 @@
 
       // Alloca'd pointers or byval arguments (which are functionally like
       // alloca's) are valid candidates for removal.
-      if (DeadPointers.count(Pointer)) {
+      if (DeadStackObjects.count(Pointer)) {
         // DCE instructions only used to calculate that store.
         Instruction *Dead = BBI++;
-        DeleteDeadInstruction(Dead, &DeadPointers);
+        DeleteDeadInstruction(Dead, &DeadStackObjects);
         ++NumFastStores;
         MadeChange = true;
         continue;
@@ -439,14 +440,14 @@
     // Remove any dead non-memory-mutating instructions.
     if (isInstructionTriviallyDead(BBI)) {
       Instruction *Inst = BBI++;
-      DeleteDeadInstruction(Inst, &DeadPointers);
+      DeleteDeadInstruction(Inst, &DeadStackObjects);
       ++NumFastOther;
       MadeChange = true;
       continue;
     }
     
     if (AllocaInst *A = dyn_cast<AllocaInst>(BBI)) {
-      DeadPointers.erase(A);
+      DeadStackObjects.erase(A);
       continue;
     }
     
@@ -461,8 +462,8 @@
       // If the call might load from any of our allocas, then any store above
       // the call is live.
       SmallVector<Value*, 8> LiveAllocas;
-      for (SmallPtrSet<Value*, 16>::iterator I = DeadPointers.begin(),
-           E = DeadPointers.end(); I != E; ++I) {
+      for (SmallPtrSet<Value*, 16>::iterator I = DeadStackObjects.begin(),
+           E = DeadStackObjects.end(); I != E; ++I) {
         // If we detect that our AA is imprecise, it's not worth it to scan the
         // rest of the DeadPointers set.  Just assume that the AA will return
         // ModRef for everything, and go ahead and bail out.
@@ -484,11 +485,11 @@
       
       for (SmallVector<Value*, 8>::iterator I = LiveAllocas.begin(),
            E = LiveAllocas.end(); I != E; ++I)
-        DeadPointers.erase(*I);
+        DeadStackObjects.erase(*I);
       
       // If all of the allocas were clobbered by the call then we're not going
       // to find anything else to process.
-      if (DeadPointers.empty())
+      if (DeadStackObjects.empty())
         return MadeChange;
       
       continue;
@@ -511,41 +512,47 @@
       continue;
     }
 
-    KillPointer = KillPointer->getUnderlyingObject();
-
-    // Deal with undead pointers
-    MadeChange |= RemoveUndeadPointers(KillPointer, KillPointerSize, BBI,
-                                       DeadPointers);
+    // Remove any allocas from the DeadPointer set that are loaded, as this
+    // makes any stores above the access live.
+    MadeChange |= RemoveAccessedObjects(KillPointer, KillPointerSize, BBI,
+                                        DeadStackObjects);
+
+    // If all of the allocas were clobbered by the access then we're not going
+    // to find anything else to process.
+    if (DeadStackObjects.empty())
+      break;
   }
   
   return MadeChange;
 }
 
-/// RemoveUndeadPointers - check for uses of a pointer that make it
-/// undead when scanning for dead stores to alloca's.
-bool DSE::RemoveUndeadPointers(Value *killPointer, uint64_t killPointerSize,
-                               BasicBlock::iterator &BBI,
-                               SmallPtrSet<Value*, 16> &DeadPointers) {
-  // If the kill pointer can be easily reduced to an alloca,
-  // don't bother doing extraneous AA queries.
-  if (DeadPointers.count(killPointer)) {
-    DeadPointers.erase(killPointer);
+/// RemoveAccessedObjects - Check to see if the specified location may alias any
+/// of the stack objects in the DeadStackObjects set.  If so, they become live
+/// because the location is being loaded.
+bool DSE::RemoveAccessedObjects(Value *KillPointer, uint64_t KillPointerSize,
+                                BasicBlock::iterator &BBI,
+                                SmallPtrSet<Value*, 16> &DeadStackObjects) {
+  Value *UnderlyingPointer = KillPointer->getUnderlyingObject();
+
+  // A constant can't be in the dead pointer set.
+  if (isa<Constant>(UnderlyingPointer))
     return false;
-  }
   
-  // A global can't be in the dead pointer set.
-  if (isa<GlobalValue>(killPointer))
+  // If the kill pointer can be easily reduced to an alloca, don't bother doing
+  // extraneous AA queries.
+  if (DeadStackObjects.count(UnderlyingPointer)) {
+    DeadStackObjects.erase(UnderlyingPointer);
     return false;
+  }
   
   bool MadeChange = false;
+  SmallVector<Value*, 16> NowLive;
   
-  SmallVector<Value*, 16> undead;
-  
-  for (SmallPtrSet<Value*, 64>::iterator I = DeadPointers.begin(),
-       E = DeadPointers.end(); I != E; ++I) {
+  for (SmallPtrSet<Value*, 16>::iterator I = DeadStackObjects.begin(),
+       E = DeadStackObjects.end(); I != E; ++I) {
     // See if this pointer could alias it
     AliasAnalysis::AliasResult A = AA->alias(*I, getPointerSize(*I, *AA),
-                                             killPointer, killPointerSize);
+                                             KillPointer, KillPointerSize);
 
     // If it must-alias and a store, we can delete it
     if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
@@ -553,7 +560,7 @@
 
       // Remove it!
       ++BBI;
-      DeleteDeadInstruction(S, &DeadPointers);
+      DeleteDeadInstruction(S, &DeadStackObjects);
       ++NumFastStores;
       MadeChange = true;
 
@@ -561,12 +568,12 @@
 
       // Otherwise, it is undead
     } else if (A != AliasAnalysis::NoAlias)
-      undead.push_back(*I);
+      NowLive.push_back(*I);
   }
 
-  for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
+  for (SmallVector<Value*, 16>::iterator I = NowLive.begin(), E = NowLive.end();
        I != E; ++I)
-    DeadPointers.erase(*I);
+    DeadStackObjects.erase(*I);
   
   return MadeChange;
 }





More information about the llvm-commits mailing list