1 //===- DeadStoreElimination.cpp - Dead Store Elimination ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a trivial dead store elimination that only considers
11 // basic-block local redundant stores.
13 // FIXME: This should eventually be extended to be a post-dominator tree
14 // traversal. Doing so would be pretty trivial.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/Function.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/Analysis/AliasSetTracker.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Transforms/Utils/Local.h"
25 #include "Support/Statistic.h"
29 Statistic<> NumStores("dse", "Number of stores deleted");
30 Statistic<> NumOther ("dse", "Number of other instrs removed");
32 struct DSE : public FunctionPass {
34 virtual bool runOnFunction(Function &F) {
36 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
37 Changed |= runOnBasicBlock(*I);
41 bool runOnBasicBlock(BasicBlock &BB);
43 void DeleteDeadValueChains(Value *V, AliasSetTracker &AST);
45 // getAnalysisUsage - We require post dominance frontiers (aka Control
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.addRequired<TargetData>();
50 AU.addRequired<AliasAnalysis>();
51 AU.addPreserved<AliasAnalysis>();
54 RegisterOpt<DSE> X("dse", "Dead Store Elimination");
57 Pass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
59 bool DSE::runOnBasicBlock(BasicBlock &BB) {
60 TargetData &TD = getAnalysis<TargetData>();
61 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
62 AliasSetTracker KillLocs(AA);
64 // If this block ends in a return, unwind, and eventually tailcall/barrier,
65 // then all allocas are dead at its end.
66 if (BB.getTerminator()->getNumSuccessors() == 0) {
70 bool MadeChange = false;
71 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ) {
72 Instruction *I = --BBI; // Keep moving iterator backwards
75 // AST doesn't support malloc/free/alloca???
76 if (isa<FreeInst>(I)) {
77 // Free instructions make any stores to the free'd location dead.
82 if (!isa<StoreInst>(I) || cast<StoreInst>(I)->isVolatile()) {
83 // If this is a non-store instruction, it makes everything referenced no
84 // longer killed. Remove anything aliased from the alias set tracker.
89 // If this is a non-volatile store instruction, and if it is already in
90 // the stored location is already in the tracker, then this is a dead
91 // store. We can just delete it here, but while we're at it, we also
92 // delete any trivially dead expression chains.
93 unsigned ValSize = TD.getTypeSize(I->getOperand(0)->getType());
94 Value *Ptr = I->getOperand(1);
95 if (AliasSet *AS = KillLocs.getAliasSetForPointerIfExists(Ptr, ValSize))
96 for (AliasSet::iterator ASI = AS->begin(), E = AS->end(); ASI != E; ++ASI)
97 if (AA.alias(ASI.getPointer(), ASI.getSize(), Ptr, ValSize)
98 == AliasAnalysis::MustAlias) {
99 // If we found a must alias in the killed set, then this store really
100 // is dead. Delete it now.
101 ++BBI; // Don't invalidate iterator.
102 Value *Val = I->getOperand(0);
103 BB.getInstList().erase(I); // Nuke the store!
105 DeleteDeadValueChains(Val, KillLocs); // Delete any now-dead instrs
106 DeleteDeadValueChains(Ptr, KillLocs); // Delete any now-dead instrs
111 // Otherwise, this is a non-dead store just add it to the set of dead
113 KillLocs.add(cast<StoreInst>(I));
119 void DSE::DeleteDeadValueChains(Value *V, AliasSetTracker &AST) {
120 // Value must be dead.
121 if (!V->use_empty()) return;
123 if (Instruction *I = dyn_cast<Instruction>(V))
124 if (isInstructionTriviallyDead(I)) {
126 getAnalysis<AliasAnalysis>().deleteValue(I);
128 // See if this made any operands dead. We do it this way in case the
129 // instruction uses the same operand twice. We don't want to delete a
130 // value then reference it.
131 while (unsigned NumOps = I->getNumOperands()) {
132 Value *Op = I->getOperand(NumOps-1);
133 I->op_erase(I->op_end()-1); // Drop from the operand list.
134 DeleteDeadValueChains(Op, AST); // Attempt to nuke it.
137 I->getParent()->getInstList().erase(I);