Convert several more passes to use getAnalysisIfAvailable<TargetData>()
[oota-llvm.git] / lib / Transforms / Scalar / DeadStoreElimination.cpp
1 //===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 #define DEBUG_TYPE "dse"
19 #include "llvm/Transforms/Scalar.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Function.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/IntrinsicInst.h"
24 #include "llvm/Pass.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/Dominators.h"
29 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Transforms/Utils/Local.h"
32 #include "llvm/Support/Compiler.h"
33 using namespace llvm;
34
35 STATISTIC(NumFastStores, "Number of stores deleted");
36 STATISTIC(NumFastOther , "Number of other instrs removed");
37
38 namespace {
39   struct VISIBILITY_HIDDEN DSE : public FunctionPass {
40     TargetData *TD;
41
42     static char ID; // Pass identification, replacement for typeid
43     DSE() : FunctionPass(&ID) {}
44
45     virtual bool runOnFunction(Function &F) {
46       bool Changed = false;
47       for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
48         Changed |= runOnBasicBlock(*I);
49       return Changed;
50     }
51     
52     bool runOnBasicBlock(BasicBlock &BB);
53     bool handleFreeWithNonTrivialDependency(FreeInst *F, MemDepResult Dep);
54     bool handleEndBlock(BasicBlock &BB);
55     bool RemoveUndeadPointers(Value* Ptr, uint64_t killPointerSize,
56                               BasicBlock::iterator& BBI,
57                               SmallPtrSet<Value*, 64>& deadPointers);
58     void DeleteDeadInstruction(Instruction *I,
59                                SmallPtrSet<Value*, 64> *deadPointers = 0);
60     
61
62     // getAnalysisUsage - We require post dominance frontiers (aka Control
63     // Dependence Graph)
64     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
65       AU.setPreservesCFG();
66       AU.addRequired<DominatorTree>();
67       AU.addRequired<AliasAnalysis>();
68       AU.addRequired<MemoryDependenceAnalysis>();
69       AU.addPreserved<DominatorTree>();
70       AU.addPreserved<AliasAnalysis>();
71       AU.addPreserved<MemoryDependenceAnalysis>();
72     }
73   };
74 }
75
76 char DSE::ID = 0;
77 static RegisterPass<DSE> X("dse", "Dead Store Elimination");
78
79 FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
80
81 bool DSE::runOnBasicBlock(BasicBlock &BB) {
82   MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
83   TD = getAnalysisIfAvailable<TargetData>();
84
85   bool MadeChange = false;
86   
87   // Do a top-down walk on the BB
88   for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
89     Instruction *Inst = BBI++;
90     
91     // If we find a store or a free, get its memory dependence.
92     if (!isa<StoreInst>(Inst) && !isa<FreeInst>(Inst))
93       continue;
94     
95     // Don't molest volatile stores or do queries that will return "clobber".
96     if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
97       if (SI->isVolatile())
98         continue;
99
100     MemDepResult InstDep = MD.getDependency(Inst);
101     
102     // Ignore non-local stores.
103     // FIXME: cross-block DSE would be fun. :)
104     if (InstDep.isNonLocal()) continue;
105   
106     // Handle frees whose dependencies are non-trivial.
107     if (FreeInst *FI = dyn_cast<FreeInst>(Inst)) {
108       MadeChange |= handleFreeWithNonTrivialDependency(FI, InstDep);
109       continue;
110     }
111     
112     StoreInst *SI = cast<StoreInst>(Inst);
113     
114     // If not a definite must-alias dependency, ignore it.
115     if (!InstDep.isDef())
116       continue;
117     
118     // If this is a store-store dependence, then the previous store is dead so
119     // long as this store is at least as big as it.
120     if (StoreInst *DepStore = dyn_cast<StoreInst>(InstDep.getInst()))
121       if (!TD ||
122           TD->getTypeStoreSize(DepStore->getOperand(0)->getType()) <=
123           TD->getTypeStoreSize(SI->getOperand(0)->getType())) {
124         // Delete the store and now-dead instructions that feed it.
125         DeleteDeadInstruction(DepStore);
126         NumFastStores++;
127         MadeChange = true;
128         
129         if (BBI != BB.begin())
130           --BBI;
131         continue;
132       }
133     
134     // If we're storing the same value back to a pointer that we just
135     // loaded from, then the store can be removed.
136     if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
137       if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
138           SI->getOperand(0) == DepLoad) {
139         DeleteDeadInstruction(SI);
140         if (BBI != BB.begin())
141           --BBI;
142         NumFastStores++;
143         MadeChange = true;
144         continue;
145       }
146     }
147   }
148   
149   // If this block ends in a return, unwind, or unreachable, all allocas are
150   // dead at its end, which means stores to them are also dead.
151   if (BB.getTerminator()->getNumSuccessors() == 0)
152     MadeChange |= handleEndBlock(BB);
153   
154   return MadeChange;
155 }
156
157 /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
158 /// dependency is a store to a field of that structure.
159 bool DSE::handleFreeWithNonTrivialDependency(FreeInst *F, MemDepResult Dep) {
160   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
161   
162   StoreInst *Dependency = dyn_cast_or_null<StoreInst>(Dep.getInst());
163   if (!Dependency || Dependency->isVolatile())
164     return false;
165   
166   Value *DepPointer = Dependency->getPointerOperand()->getUnderlyingObject();
167
168   // Check for aliasing.
169   if (AA.alias(F->getPointerOperand(), 1, DepPointer, 1) !=
170          AliasAnalysis::MustAlias)
171     return false;
172   
173   // DCE instructions only used to calculate that store
174   DeleteDeadInstruction(Dependency);
175   NumFastStores++;
176   return true;
177 }
178
179 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
180 /// function end block.  Ex:
181 /// %A = alloca i32
182 /// ...
183 /// store i32 1, i32* %A
184 /// ret void
185 bool DSE::handleEndBlock(BasicBlock &BB) {
186   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
187   
188   bool MadeChange = false;
189   
190   // Pointers alloca'd in this function are dead in the end block
191   SmallPtrSet<Value*, 64> deadPointers;
192   
193   // Find all of the alloca'd pointers in the entry block.
194   BasicBlock *Entry = BB.getParent()->begin();
195   for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
196     if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
197       deadPointers.insert(AI);
198   
199   // Treat byval arguments the same, stores to them are dead at the end of the
200   // function.
201   for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
202        AE = BB.getParent()->arg_end(); AI != AE; ++AI)
203     if (AI->hasByValAttr())
204       deadPointers.insert(AI);
205   
206   // Scan the basic block backwards
207   for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
208     --BBI;
209     
210     // If we find a store whose pointer is dead.
211     if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
212       if (!S->isVolatile()) {
213         // See through pointer-to-pointer bitcasts
214         Value* pointerOperand = S->getPointerOperand()->getUnderlyingObject();
215
216         // Alloca'd pointers or byval arguments (which are functionally like
217         // alloca's) are valid candidates for removal.
218         if (deadPointers.count(pointerOperand)) {
219           // DCE instructions only used to calculate that store.
220           BBI++;
221           DeleteDeadInstruction(S, &deadPointers);
222           NumFastStores++;
223           MadeChange = true;
224         }
225       }
226       
227       continue;
228     }
229     
230     // We can also remove memcpy's to local variables at the end of a function.
231     if (MemCpyInst *M = dyn_cast<MemCpyInst>(BBI)) {
232       Value *dest = M->getDest()->getUnderlyingObject();
233
234       if (deadPointers.count(dest)) {
235         BBI++;
236         DeleteDeadInstruction(M, &deadPointers);
237         NumFastOther++;
238         MadeChange = true;
239         continue;
240       }
241       
242       // Because a memcpy is also a load, we can't skip it if we didn't remove
243       // it.
244     }
245     
246     Value* killPointer = 0;
247     uint64_t killPointerSize = ~0UL;
248     
249     // If we encounter a use of the pointer, it is no longer considered dead
250     if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
251       // However, if this load is unused and not volatile, we can go ahead and
252       // remove it, and not have to worry about it making our pointer undead!
253       if (L->use_empty() && !L->isVolatile()) {
254         BBI++;
255         DeleteDeadInstruction(L, &deadPointers);
256         NumFastOther++;
257         MadeChange = true;
258         continue;
259       }
260       
261       killPointer = L->getPointerOperand();
262     } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
263       killPointer = V->getOperand(0);
264     } else if (isa<MemCpyInst>(BBI) &&
265                isa<ConstantInt>(cast<MemCpyInst>(BBI)->getLength())) {
266       killPointer = cast<MemCpyInst>(BBI)->getSource();
267       killPointerSize = cast<ConstantInt>(
268                             cast<MemCpyInst>(BBI)->getLength())->getZExtValue();
269     } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
270       deadPointers.erase(A);
271       
272       // Dead alloca's can be DCE'd when we reach them
273       if (A->use_empty()) {
274         BBI++;
275         DeleteDeadInstruction(A, &deadPointers);
276         NumFastOther++;
277         MadeChange = true;
278       }
279       
280       continue;
281     } else if (CallSite::get(BBI).getInstruction() != 0) {
282       // If this call does not access memory, it can't
283       // be undeadifying any of our pointers.
284       CallSite CS = CallSite::get(BBI);
285       if (AA.doesNotAccessMemory(CS))
286         continue;
287       
288       unsigned modRef = 0;
289       unsigned other = 0;
290       
291       // Remove any pointers made undead by the call from the dead set
292       std::vector<Value*> dead;
293       for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
294            E = deadPointers.end(); I != E; ++I) {
295         // HACK: if we detect that our AA is imprecise, it's not
296         // worth it to scan the rest of the deadPointers set.  Just
297         // assume that the AA will return ModRef for everything, and
298         // go ahead and bail.
299         if (modRef >= 16 && other == 0) {
300           deadPointers.clear();
301           return MadeChange;
302         }
303
304         // Get size information for the alloca
305         unsigned pointerSize = ~0U;
306         if (TD) {
307           if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
308             if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
309               pointerSize = C->getZExtValue() *
310                             TD->getTypeAllocSize(A->getAllocatedType());
311           } else {
312             const PointerType* PT = cast<PointerType>(
313                                                    cast<Argument>(*I)->getType());
314             pointerSize = TD->getTypeAllocSize(PT->getElementType());
315           }
316         }
317
318         // See if the call site touches it
319         AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
320         
321         if (A == AliasAnalysis::ModRef)
322           modRef++;
323         else
324           other++;
325         
326         if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
327           dead.push_back(*I);
328       }
329
330       for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
331            I != E; ++I)
332         deadPointers.erase(*I);
333       
334       continue;
335     } else if (isInstructionTriviallyDead(BBI)) {
336       // For any non-memory-affecting non-terminators, DCE them as we reach them
337       Instruction *Inst = BBI;
338       BBI++;
339       DeleteDeadInstruction(Inst, &deadPointers);
340       NumFastOther++;
341       MadeChange = true;
342       continue;
343     }
344     
345     if (!killPointer)
346       continue;
347
348     killPointer = killPointer->getUnderlyingObject();
349
350     // Deal with undead pointers
351     MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
352                                        deadPointers);
353   }
354   
355   return MadeChange;
356 }
357
358 /// RemoveUndeadPointers - check for uses of a pointer that make it
359 /// undead when scanning for dead stores to alloca's.
360 bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize,
361                                BasicBlock::iterator &BBI,
362                                SmallPtrSet<Value*, 64>& deadPointers) {
363   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
364                                   
365   // If the kill pointer can be easily reduced to an alloca,
366   // don't bother doing extraneous AA queries.
367   if (deadPointers.count(killPointer)) {
368     deadPointers.erase(killPointer);
369     return false;
370   }
371   
372   // A global can't be in the dead pointer set.
373   if (isa<GlobalValue>(killPointer))
374     return false;
375   
376   bool MadeChange = false;
377   
378   SmallVector<Value*, 16> undead;
379     
380   for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
381       E = deadPointers.end(); I != E; ++I) {
382     // Get size information for the alloca.
383     unsigned pointerSize = ~0U;
384     if (TD) {
385       if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
386         if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
387           pointerSize = C->getZExtValue() *
388                         TD->getTypeAllocSize(A->getAllocatedType());
389       } else {
390         const PointerType* PT = cast<PointerType>(cast<Argument>(*I)->getType());
391         pointerSize = TD->getTypeAllocSize(PT->getElementType());
392       }
393     }
394
395     // See if this pointer could alias it
396     AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
397                                             killPointer, killPointerSize);
398
399     // If it must-alias and a store, we can delete it
400     if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
401       StoreInst* S = cast<StoreInst>(BBI);
402
403       // Remove it!
404       BBI++;
405       DeleteDeadInstruction(S, &deadPointers);
406       NumFastStores++;
407       MadeChange = true;
408
409       continue;
410
411       // Otherwise, it is undead
412     } else if (A != AliasAnalysis::NoAlias)
413       undead.push_back(*I);
414   }
415
416   for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
417        I != E; ++I)
418       deadPointers.erase(*I);
419   
420   return MadeChange;
421 }
422
423 /// DeleteDeadInstruction - Delete this instruction.  Before we do, go through
424 /// and zero out all the operands of this instruction.  If any of them become
425 /// dead, delete them and the computation tree that feeds them.
426 ///
427 /// If ValueSet is non-null, remove any deleted instructions from it as well.
428 ///
429 void DSE::DeleteDeadInstruction(Instruction *I,
430                                 SmallPtrSet<Value*, 64> *ValueSet) {
431   SmallVector<Instruction*, 32> NowDeadInsts;
432   
433   NowDeadInsts.push_back(I);
434   --NumFastOther;
435
436   // Before we touch this instruction, remove it from memdep!
437   MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
438   while (!NowDeadInsts.empty()) {
439     Instruction *DeadInst = NowDeadInsts.back();
440     NowDeadInsts.pop_back();
441     
442     ++NumFastOther;
443     
444     // This instruction is dead, zap it, in stages.  Start by removing it from
445     // MemDep, which needs to know the operands and needs it to be in the
446     // function.
447     MDA.removeInstruction(DeadInst);
448     
449     for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
450       Value *Op = DeadInst->getOperand(op);
451       DeadInst->setOperand(op, 0);
452       
453       // If this operand just became dead, add it to the NowDeadInsts list.
454       if (!Op->use_empty()) continue;
455       
456       if (Instruction *OpI = dyn_cast<Instruction>(Op))
457         if (isInstructionTriviallyDead(OpI))
458           NowDeadInsts.push_back(OpI);
459     }
460     
461     DeadInst->eraseFromParent();
462     
463     if (ValueSet) ValueSet->erase(DeadInst);
464   }
465 }