Implement Transforms/InstCombine/cast-load-gep.ll, which allows us to devirtualize
[oota-llvm.git] / lib / Transforms / Scalar / DeadStoreElimination.cpp
index 27a0b0a881f39074ff02c68b337126da449dc21f..a823e14c0db5a981e4099b572397d15185eacc66 100644 (file)
@@ -73,7 +73,7 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
         unsigned Size = ~0U;
         if (!AI->isArrayAllocation() &&
             AI->getType()->getElementType()->isSized())
-          Size = TD.getTypeSize(AI->getType()->getElementType());
+          Size = (unsigned)TD.getTypeSize(AI->getType()->getElementType());
         KillLocs.add(AI, Size);
       }
   }
@@ -106,12 +106,13 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
     // the stored location is already in the tracker, then this is a dead
     // store.  We can just delete it here, but while we're at it, we also
     // delete any trivially dead expression chains.
-    unsigned ValSize = TD.getTypeSize(I->getOperand(0)->getType());
+    unsigned ValSize = (unsigned)TD.getTypeSize(I->getOperand(0)->getType());
     Value *Ptr = I->getOperand(1);
 
     if (AliasSet *AS = KillLocs.getAliasSetForPointerIfExists(Ptr, ValSize))
       for (AliasSet::iterator ASI = AS->begin(), E = AS->end(); ASI != E; ++ASI)
-        if (AA.alias(ASI.getPointer(), ASI.getSize(), Ptr, ValSize)
+        if (ASI.getSize() >= ValSize &&  // Overwriting all of this store.
+            AA.alias(ASI.getPointer(), ASI.getSize(), Ptr, ValSize)
                == AliasAnalysis::MustAlias) {
           // If we found a must alias in the killed set, then this store really
           // is dead.  Remember that the various operands of the store now have
@@ -155,13 +156,12 @@ void DSE::DeleteDeadInstructionChains(Instruction *I,
   // See if this made any operands dead.  We do it this way in case the
   // instruction uses the same operand twice.  We don't want to delete a
   // value then reference it.
-  while (unsigned NumOps = I->getNumOperands()) {
-    Instruction *Op = dyn_cast<Instruction>(I->getOperand(NumOps-1));
-    I->op_erase(I->op_end()-1);         // Drop from the operand list.
-    
-    if (Op) DeadInsts.insert(Op);       // Attempt to nuke it later.
+  for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
+    if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
+      DeadInsts.insert(Op);      // Attempt to nuke it later.
+    I->setOperand(i, 0);         // Drop from the operand list.
   }
   
-  I->getParent()->getInstList().erase(I);
+  I->eraseFromParent();
   ++NumOther;
 }