obvious fix
[oota-llvm.git] / lib / Transforms / Scalar / DeadStoreElimination.cpp
1 //===- DeadStoreElimination.cpp - Dead Store Elimination ------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a trivial dead store elimination that only considers
11 // basic-block local redundant stores.
12 //
13 // FIXME: This should eventually be extended to be a post-dominator tree
14 // traversal.  Doing so would be pretty trivial.
15 //
16 //===----------------------------------------------------------------------===//
17
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"
26 using namespace llvm;
27
28 namespace {
29   Statistic<> NumStores("dse", "Number of stores deleted");
30   Statistic<> NumOther ("dse", "Number of other instrs removed");
31
32   struct DSE : public FunctionPass {
33
34     virtual bool runOnFunction(Function &F) {
35       bool Changed = false;
36       for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
37         Changed |= runOnBasicBlock(*I);
38       return Changed;
39     }
40     
41     bool runOnBasicBlock(BasicBlock &BB);
42     
43     void DeleteDeadValueChains(Value *V, AliasSetTracker &AST);
44
45     // getAnalysisUsage - We require post dominance frontiers (aka Control
46     // Dependence Graph)
47     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48       AU.setPreservesCFG();
49       AU.addRequired<TargetData>();
50       AU.addRequired<AliasAnalysis>();
51       AU.addPreserved<AliasAnalysis>();
52     }
53   };
54   RegisterOpt<DSE> X("dse", "Dead Store Elimination");
55 }
56
57 Pass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
58
59 bool DSE::runOnBasicBlock(BasicBlock &BB) {
60   TargetData &TD = getAnalysis<TargetData>();
61   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
62   AliasSetTracker KillLocs(AA);
63
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) {
67
68   }
69
70   bool MadeChange = false;
71   for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ) {
72     Instruction *I = --BBI;   // Keep moving iterator backwards
73     
74 #if 0
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.
78       KillLocs.insert(I);
79     }
80 #endif
81
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.
85       KillLocs.remove(I);
86       continue;
87     }
88
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!
104           ++NumStores;
105           DeleteDeadValueChains(Val, KillLocs);   // Delete any now-dead instrs
106           DeleteDeadValueChains(Ptr, KillLocs);   // Delete any now-dead instrs
107           MadeChange = true;
108           goto BigContinue;
109         }
110
111     // Otherwise, this is a non-dead store just add it to the set of dead
112     // locations.
113     KillLocs.add(cast<StoreInst>(I));
114   BigContinue:;
115   }
116   return MadeChange;
117 }
118
119 void DSE::DeleteDeadValueChains(Value *V, AliasSetTracker &AST) {
120   // Value must be dead.
121   if (!V->use_empty()) return;
122
123   if (Instruction *I = dyn_cast<Instruction>(V))
124     if (isInstructionTriviallyDead(I)) {
125       AST.deleteValue(I);
126       getAnalysis<AliasAnalysis>().deleteValue(I);
127
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.
135       }
136
137       I->getParent()->getInstList().erase(I);
138       ++NumOther;
139     }
140 }