Make DSE much more aggressive by performing DCE earlier. Update a testcase to reflec...
authorOwen Anderson <resistor@mac.com>
Wed, 30 Jan 2008 01:24:47 +0000 (01:24 +0000)
committerOwen Anderson <resistor@mac.com>
Wed, 30 Jan 2008 01:24:47 +0000 (01:24 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46542 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/DeadStoreElimination.cpp
test/Transforms/DeadStoreElimination/memcpy.ll

index fc359404594317311ede8c71eb956b998465ecd2..7e2588ab77d87dccadd46cbc2a2f87d15a8ed510 100644 (file)
@@ -152,7 +152,7 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
           possiblyDead.insert(D);
         if (Instruction* D = dyn_cast<Instruction>(last->getOperand(1)))
           possiblyDead.insert(D);
           possiblyDead.insert(D);
         if (Instruction* D = dyn_cast<Instruction>(last->getOperand(1)))
           possiblyDead.insert(D);
-          
+        
         last->eraseFromParent();
         NumFastStores++;
         deletedStore = true;
         last->eraseFromParent();
         NumFastStores++;
         deletedStore = true;
@@ -303,7 +303,7 @@ bool DSE::handleEndBlock(BasicBlock& BB,
         MD.removeInstruction(M);
         
         // DCE instructions only used to calculate that memcpy
         MD.removeInstruction(M);
         
         // DCE instructions only used to calculate that memcpy
-        if (Instruction* D = dyn_cast<Instruction>(M->getSource()))
+        if (Instruction* D = dyn_cast<Instruction>(M->getRawSource()))
           possiblyDead.insert(D);
         if (Instruction* D = dyn_cast<Instruction>(M->getLength()))
           possiblyDead.insert(D);
           possiblyDead.insert(D);
         if (Instruction* D = dyn_cast<Instruction>(M->getLength()))
           possiblyDead.insert(D);
@@ -325,11 +325,45 @@ bool DSE::handleEndBlock(BasicBlock& BB,
     
     // If we encounter a use of the pointer, it is no longer considered dead
     if (LoadInst* L = dyn_cast<LoadInst>(BBI)) {
     
     // If we encounter a use of the pointer, it is no longer considered dead
     if (LoadInst* L = dyn_cast<LoadInst>(BBI)) {
+      // However, if this load is unused, we can go ahead and remove it, and
+      // not have to worry about it making our pointer undead!
+      if (L->getNumUses() == 0) {
+        MD.removeInstruction(L);
+        
+        // DCE instructions only used to calculate that load
+        if (Instruction* D = dyn_cast<Instruction>(L->getPointerOperand()))
+          possiblyDead.insert(D);
+        
+        BBI++;
+        L->eraseFromParent();
+        NumFastOther++;
+        MadeChange = true;
+        possiblyDead.remove(L);
+        
+        continue;
+      }
+      
       killPointer = L->getPointerOperand();
     } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
       killPointer = V->getOperand(0);
     } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
       deadPointers.erase(A);
       killPointer = L->getPointerOperand();
     } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
       killPointer = V->getOperand(0);
     } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
       deadPointers.erase(A);
+      
+      // Dead alloca's can be DCE'd when we reach them
+      if (A->getNumUses() == 0) {
+        MD.removeInstruction(A);
+        
+        // DCE instructions only used to calculate that load
+        if (Instruction* D = dyn_cast<Instruction>(A->getArraySize()))
+          possiblyDead.insert(D);
+        
+        BBI++;
+        A->eraseFromParent();
+        NumFastOther++;
+        MadeChange = true;
+        possiblyDead.remove(A);
+      }
+      
       continue;
     } else if (CallSite::get(BBI).getInstruction() != 0) {
       // If this call does not access memory, it can't
       continue;
     } else if (CallSite::get(BBI).getInstruction() != 0) {
       // If this call does not access memory, it can't
@@ -383,6 +417,25 @@ bool DSE::handleEndBlock(BasicBlock& BB,
         deadPointers.erase(*I);
       
       continue;
         deadPointers.erase(*I);
       
       continue;
+    } else {
+      // For any non-memory-affecting non-terminators, DCE them as we reach them
+      Instruction *CI = BBI;
+      if (!CI->isTerminator() && CI->getNumUses() == 0) {
+        
+        // DCE instructions only used to calculate that load
+        for (Instruction::op_iterator OI = CI->op_begin(), OE = CI->op_end();
+             OI != OE; ++OI)
+          if (Instruction* D = dyn_cast<Instruction>(OI))
+            possiblyDead.insert(D);
+        
+        BBI++;
+        CI->eraseFromParent();
+        NumFastOther++;
+        MadeChange = true;
+        possiblyDead.remove(CI);
+        
+        continue;
+      }
     }
     
     if (!killPointer)
     }
     
     if (!killPointer)
index 550f7645b161eb26632db2916b725a9d5eb9de90..e96b2e8fb1ed1603b66e4e8c9fb721cc6a1ce03a 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: llvm-as < %s | opt -dse | llvm-dis | not grep tmp180
+; RUN: llvm-as < %s | opt -dse | llvm-dis | not grep alloca
 ; ModuleID = 'placeholder.adb'
 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"
 target triple = "i686-pc-linux-gnu"
 ; ModuleID = 'placeholder.adb'
 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"
 target triple = "i686-pc-linux-gnu"