Be more precise when eliminating pointers bue to memcpy's. This allows more
[oota-llvm.git] / lib / Transforms / Scalar / DeadStoreElimination.cpp
index fc359404594317311ede8c71eb956b998465ecd2..f9d1205ada558e2a6feef90bcdf9ce703a409069 100644 (file)
@@ -52,7 +52,7 @@ namespace {
                                             Instruction* dependency,
                                         SetVector<Instruction*>& possiblyDead);
     bool handleEndBlock(BasicBlock& BB, SetVector<Instruction*>& possiblyDead);
-    bool RemoveUndeadPointers(Value* pointer,
+    bool RemoveUndeadPointers(Value* pointer, uint64_t killPointerSize,
                               BasicBlock::iterator& BBI,
                               SmallPtrSet<Value*, 64>& deadPointers, 
                               SetVector<Instruction*>& possiblyDead);
@@ -152,7 +152,7 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
           possiblyDead.insert(D);
         if (Instruction* D = dyn_cast<Instruction>(last->getOperand(1)))
           possiblyDead.insert(D);
-          
+        
         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
-        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);
@@ -322,14 +322,54 @@ bool DSE::handleEndBlock(BasicBlock& BB,
     }
     
     Value* killPointer = 0;
+    uint64_t killPointerSize = ~0UL;
     
     // 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->use_empty()) {
+        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 (isa<MemCpyInst>(BBI) &&
+               isa<ConstantInt>(cast<MemCpyInst>(BBI)->getLength())) {
+      killPointer = cast<MemCpyInst>(BBI)->getSource();
+      killPointerSize = cast<ConstantInt>(
+                            cast<MemCpyInst>(BBI)->getLength())->getZExtValue();
     } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
       deadPointers.erase(A);
+      
+      // Dead alloca's can be DCE'd when we reach them
+      if (A->use_empty()) {
+        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
@@ -383,6 +423,25 @@ bool DSE::handleEndBlock(BasicBlock& BB,
         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->use_empty() && !isa<FreeInst>(CI)) {
+        
+        // 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)
@@ -391,7 +450,7 @@ bool DSE::handleEndBlock(BasicBlock& BB,
     TranslatePointerBitCasts(killPointer);
     
     // Deal with undead pointers
-    MadeChange |= RemoveUndeadPointers(killPointer, BBI,
+    MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
                                        deadPointers, possiblyDead);
   }
   
@@ -400,7 +459,7 @@ bool DSE::handleEndBlock(BasicBlock& BB,
 
 /// RemoveUndeadPointers - check for uses of a pointer that make it
 /// undead when scanning for dead stores to alloca's.
-bool DSE::RemoveUndeadPointers(Value* killPointer,
+bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize,
                                 BasicBlock::iterator& BBI,
                                 SmallPtrSet<Value*, 64>& deadPointers, 
                                 SetVector<Instruction*>& possiblyDead) {
@@ -438,7 +497,7 @@ bool DSE::RemoveUndeadPointers(Value* killPointer,
 
     // See if this pointer could alias it
     AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
-                                            killPointer, ~0U);
+                                            killPointer, killPointerSize);
 
     // If it must-alias and a store, we can delete it
     if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {