//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Scalar.h"
+#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AliasSetTracker.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Transforms/Utils/Local.h"
-#include "Support/SetVector.h"
-#include "Support/Statistic.h"
+#include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/Statistic.h"
using namespace llvm;
namespace {
RegisterOpt<DSE> X("dse", "Dead Store Elimination");
}
-Pass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
+FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
bool DSE::runOnBasicBlock(BasicBlock &BB) {
TargetData &TD = getAnalysis<TargetData>();
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
AliasSetTracker KillLocs(AA);
- // If this block ends in a return, unwind, and eventually tailcall/barrier,
- // then all allocas are dead at its end.
+ // If this block ends in a return, unwind, unreachable, and eventually
+ // tailcall, then all allocas are dead at its end.
if (BB.getTerminator()->getNumSuccessors() == 0) {
BasicBlock *Entry = BB.getParent()->begin();
for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
- if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
- KillLocs.add(AI, ~0);
+ if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
+ unsigned Size = ~0U;
+ if (!AI->isArrayAllocation() &&
+ AI->getType()->getElementType()->isSized())
+ Size = (unsigned)TD.getTypeSize(AI->getType()->getElementType());
+ KillLocs.add(AI, Size);
+ }
}
// PotentiallyDeadInsts - Deleting dead stores from the program can make other
// 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
// 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;
}