2004-07-22 08:00:28 +00:00
|
|
|
//===- DeadStoreElimination.cpp - Dead Store Elimination ------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-07-22 08:00:28 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-07-22 08:00:28 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a trivial dead store elimination that only considers
|
|
|
|
// basic-block local redundant stores.
|
|
|
|
//
|
|
|
|
// FIXME: This should eventually be extended to be a post-dominator tree
|
|
|
|
// traversal. Doing so would be pretty trivial.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-11-28 20:44:37 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-07-22 08:00:28 +00:00
|
|
|
#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"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-07-22 08:00:28 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
Statistic<> NumStores("dse", "Number of stores deleted");
|
|
|
|
Statistic<> NumOther ("dse", "Number of other instrs removed");
|
|
|
|
|
|
|
|
struct DSE : public FunctionPass {
|
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
|
|
|
|
Changed |= runOnBasicBlock(*I);
|
|
|
|
return Changed;
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-07-22 08:00:28 +00:00
|
|
|
bool runOnBasicBlock(BasicBlock &BB);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-07-25 11:09:56 +00:00
|
|
|
void DeleteDeadInstructionChains(Instruction *I,
|
|
|
|
SetVector<Instruction*> &DeadInsts);
|
2004-07-22 08:00:28 +00:00
|
|
|
|
|
|
|
// getAnalysisUsage - We require post dominance frontiers (aka Control
|
|
|
|
// Dependence Graph)
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2004-07-24 07:51:27 +00:00
|
|
|
AU.setPreservesCFG();
|
2004-07-22 08:00:28 +00:00
|
|
|
AU.addRequired<TargetData>();
|
|
|
|
AU.addRequired<AliasAnalysis>();
|
|
|
|
AU.addPreserved<AliasAnalysis>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
RegisterOpt<DSE> X("dse", "Dead Store Elimination");
|
|
|
|
}
|
|
|
|
|
2004-09-20 04:43:15 +00:00
|
|
|
FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
|
2004-07-22 08:00:28 +00:00
|
|
|
|
|
|
|
bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
|
|
|
TargetData &TD = getAnalysis<TargetData>();
|
|
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
|
|
|
AliasSetTracker KillLocs(AA);
|
|
|
|
|
2004-11-28 20:44:37 +00:00
|
|
|
// If this block ends in a return, unwind, unreachable, and eventually
|
|
|
|
// tailcall, then all allocas are dead at its end.
|
2004-07-22 08:00:28 +00:00
|
|
|
if (BB.getTerminator()->getNumSuccessors() == 0) {
|
2004-07-26 06:14:11 +00:00
|
|
|
BasicBlock *Entry = BB.getParent()->begin();
|
|
|
|
for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
|
2004-11-28 20:44:37 +00:00
|
|
|
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
|
|
|
|
unsigned Size = ~0U;
|
|
|
|
if (!AI->isArrayAllocation() &&
|
|
|
|
AI->getType()->getElementType()->isSized())
|
2005-01-08 19:42:22 +00:00
|
|
|
Size = (unsigned)TD.getTypeSize(AI->getType()->getElementType());
|
2004-11-28 20:44:37 +00:00
|
|
|
KillLocs.add(AI, Size);
|
|
|
|
}
|
2004-07-22 08:00:28 +00:00
|
|
|
}
|
|
|
|
|
2004-07-25 11:09:56 +00:00
|
|
|
// PotentiallyDeadInsts - Deleting dead stores from the program can make other
|
|
|
|
// instructions die if they were only used as operands to stores. Keep track
|
|
|
|
// of the operands to stores so that we can try deleting them at the end of
|
|
|
|
// the traversal.
|
|
|
|
SetVector<Instruction*> PotentiallyDeadInsts;
|
|
|
|
|
2004-07-22 08:00:28 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ) {
|
|
|
|
Instruction *I = --BBI; // Keep moving iterator backwards
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-07-25 11:09:56 +00:00
|
|
|
// If this is a free instruction, it makes the free'd location dead!
|
|
|
|
if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
|
2004-07-22 08:00:28 +00:00
|
|
|
// Free instructions make any stores to the free'd location dead.
|
2004-07-25 11:09:56 +00:00
|
|
|
KillLocs.add(FI);
|
|
|
|
continue;
|
2004-07-22 08:00:28 +00:00
|
|
|
}
|
|
|
|
|
2004-07-25 11:09:56 +00:00
|
|
|
if (!isa<StoreInst>(I) || cast<StoreInst>(I)->isVolatile()) {
|
2004-07-22 08:00:28 +00:00
|
|
|
// If this is a non-store instruction, it makes everything referenced no
|
|
|
|
// longer killed. Remove anything aliased from the alias set tracker.
|
|
|
|
KillLocs.remove(I);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a non-volatile store instruction, and if it is already in
|
|
|
|
// 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.
|
2005-01-08 19:42:22 +00:00
|
|
|
unsigned ValSize = (unsigned)TD.getTypeSize(I->getOperand(0)->getType());
|
2004-07-25 11:09:56 +00:00
|
|
|
Value *Ptr = I->getOperand(1);
|
2004-07-25 07:58:38 +00:00
|
|
|
|
2004-07-22 08:00:28 +00:00
|
|
|
if (AliasSet *AS = KillLocs.getAliasSetForPointerIfExists(Ptr, ValSize))
|
|
|
|
for (AliasSet::iterator ASI = AS->begin(), E = AS->end(); ASI != E; ++ASI)
|
2004-12-29 04:36:02 +00:00
|
|
|
if (ASI.getSize() >= ValSize && // Overwriting all of this store.
|
|
|
|
AA.alias(ASI.getPointer(), ASI.getSize(), Ptr, ValSize)
|
2004-07-22 08:00:28 +00:00
|
|
|
== AliasAnalysis::MustAlias) {
|
|
|
|
// If we found a must alias in the killed set, then this store really
|
2004-07-25 11:09:56 +00:00
|
|
|
// is dead. Remember that the various operands of the store now have
|
|
|
|
// fewer users. At the end we will see if we can delete any values
|
|
|
|
// that are dead as part of the store becoming dead.
|
|
|
|
if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(0)))
|
|
|
|
PotentiallyDeadInsts.insert(Op);
|
|
|
|
if (Instruction *Op = dyn_cast<Instruction>(Ptr))
|
|
|
|
PotentiallyDeadInsts.insert(Op);
|
|
|
|
|
|
|
|
// Delete it now.
|
2004-07-22 08:00:28 +00:00
|
|
|
++BBI; // Don't invalidate iterator.
|
|
|
|
BB.getInstList().erase(I); // Nuke the store!
|
|
|
|
++NumStores;
|
|
|
|
MadeChange = true;
|
|
|
|
goto BigContinue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this is a non-dead store just add it to the set of dead
|
|
|
|
// locations.
|
2004-07-25 11:09:56 +00:00
|
|
|
KillLocs.add(cast<StoreInst>(I));
|
2004-07-22 08:00:28 +00:00
|
|
|
BigContinue:;
|
|
|
|
}
|
2004-07-25 11:09:56 +00:00
|
|
|
|
|
|
|
while (!PotentiallyDeadInsts.empty()) {
|
|
|
|
Instruction *I = PotentiallyDeadInsts.back();
|
|
|
|
PotentiallyDeadInsts.pop_back();
|
|
|
|
DeleteDeadInstructionChains(I, PotentiallyDeadInsts);
|
|
|
|
}
|
2004-07-22 08:00:28 +00:00
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2004-07-25 11:09:56 +00:00
|
|
|
void DSE::DeleteDeadInstructionChains(Instruction *I,
|
|
|
|
SetVector<Instruction*> &DeadInsts) {
|
|
|
|
// Instruction must be dead.
|
|
|
|
if (!I->use_empty() || !isInstructionTriviallyDead(I)) return;
|
|
|
|
|
|
|
|
// Let the alias analysis know that we have nuked a value.
|
|
|
|
getAnalysis<AliasAnalysis>().deleteValue(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.
|
2005-01-29 00:39:08 +00:00
|
|
|
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.
|
2004-07-25 11:09:56 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2005-01-29 00:39:08 +00:00
|
|
|
I->eraseFromParent();
|
2004-07-25 11:09:56 +00:00
|
|
|
++NumOther;
|
2004-07-22 08:00:28 +00:00
|
|
|
}
|