improve DSE when TargetData is not around, based on work by
[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/MemoryBuiltins.h"
30 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
31 #include "llvm/Target/TargetData.h"
32 #include "llvm/Transforms/Utils/Local.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 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(Instruction *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 /// isValueAtLeastAsBigAs - Return true if V1 is greater than or equal to the
82 /// stored size of V2.  This returns false if we don't know.
83 ///
84 static bool isValueAtLeastAsBigAs(Value *V1, Value *V2, const TargetData *TD) {
85   const Type *V1Ty = V1->getType(), *V2Ty = V2->getType();
86   
87   // Exactly the same type, must have exactly the same size.
88   if (V1Ty == V2Ty) return true;
89   
90   // If we don't have target data, we don't know.
91   if (TD == 0) return false;
92   
93   return TD->getTypeStoreSize(V1Ty) >= TD->getTypeStoreSize(V2Ty);
94 }
95
96 bool DSE::runOnBasicBlock(BasicBlock &BB) {
97   MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
98   TD = getAnalysisIfAvailable<TargetData>();
99
100   bool MadeChange = false;
101   
102   // Do a top-down walk on the BB.
103   for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
104     Instruction *Inst = BBI++;
105     
106     // If we find a store or a free, get its memory dependence.
107     if (!isa<StoreInst>(Inst) && !isFreeCall(Inst))
108       continue;
109     
110     // Don't molest volatile stores or do queries that will return "clobber".
111     if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
112       if (SI->isVolatile())
113         continue;
114
115     MemDepResult InstDep = MD.getDependency(Inst);
116     
117     // Ignore non-local stores.
118     // FIXME: cross-block DSE would be fun. :)
119     if (InstDep.isNonLocal()) continue;
120   
121     // Handle frees whose dependencies are non-trivial.
122     if (isFreeCall(Inst)) {
123       MadeChange |= handleFreeWithNonTrivialDependency(Inst, InstDep);
124       continue;
125     }
126     
127     StoreInst *SI = cast<StoreInst>(Inst);
128     
129     // If not a definite must-alias dependency, ignore it.
130     if (!InstDep.isDef())
131       continue;
132     
133     // If this is a store-store dependence, then the previous store is dead so
134     // long as this store is at least as big as it.
135     if (StoreInst *DepStore = dyn_cast<StoreInst>(InstDep.getInst()))
136       if (isValueAtLeastAsBigAs(SI->getOperand(0), DepStore->getOperand(0),TD)){
137         // Delete the store and now-dead instructions that feed it.
138         DeleteDeadInstruction(DepStore);
139         NumFastStores++;
140         MadeChange = true;
141
142         // DeleteDeadInstruction can delete the current instruction in loop
143         // cases, reset BBI.
144         BBI = Inst;
145         if (BBI != BB.begin())
146           --BBI;
147         continue;
148       }
149     
150     // If we're storing the same value back to a pointer that we just
151     // loaded from, then the store can be removed.
152     if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
153       if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
154           SI->getOperand(0) == DepLoad) {
155         // DeleteDeadInstruction can delete the current instruction.  Save BBI
156         // in case we need it.
157         WeakVH NextInst(BBI);
158         
159         DeleteDeadInstruction(SI);
160         
161         if (NextInst == 0)  // Next instruction deleted.
162           BBI = BB.begin();
163         else if (BBI != BB.begin())  // Revisit this instruction if possible.
164           --BBI;
165         NumFastStores++;
166         MadeChange = true;
167         continue;
168       }
169     }
170     
171     // If this is a lifetime end marker, we can throw away the store.
172     if (IntrinsicInst* II = dyn_cast<IntrinsicInst>(InstDep.getInst())) {
173       if (II->getIntrinsicID() == Intrinsic::lifetime_end) {
174         // Delete the store and now-dead instructions that feed it.
175         // DeleteDeadInstruction can delete the current instruction.  Save BBI
176         // in case we need it.
177         WeakVH NextInst(BBI);
178         
179         DeleteDeadInstruction(SI);
180         
181         if (NextInst == 0)  // Next instruction deleted.
182           BBI = BB.begin();
183         else if (BBI != BB.begin())  // Revisit this instruction if possible.
184           --BBI;
185         NumFastStores++;
186         MadeChange = true;
187         continue;
188       }
189     }
190   }
191   
192   // If this block ends in a return, unwind, or unreachable, all allocas are
193   // dead at its end, which means stores to them are also dead.
194   if (BB.getTerminator()->getNumSuccessors() == 0)
195     MadeChange |= handleEndBlock(BB);
196   
197   return MadeChange;
198 }
199
200 /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
201 /// dependency is a store to a field of that structure.
202 bool DSE::handleFreeWithNonTrivialDependency(Instruction *F, MemDepResult Dep) {
203   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
204   
205   StoreInst *Dependency = dyn_cast_or_null<StoreInst>(Dep.getInst());
206   if (!Dependency || Dependency->isVolatile())
207     return false;
208   
209   Value *DepPointer = Dependency->getPointerOperand()->getUnderlyingObject();
210
211   // Check for aliasing.
212   if (AA.alias(F->getOperand(1), 1, DepPointer, 1) !=
213          AliasAnalysis::MustAlias)
214     return false;
215   
216   // DCE instructions only used to calculate that store
217   DeleteDeadInstruction(Dependency);
218   NumFastStores++;
219   return true;
220 }
221
222 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
223 /// function end block.  Ex:
224 /// %A = alloca i32
225 /// ...
226 /// store i32 1, i32* %A
227 /// ret void
228 bool DSE::handleEndBlock(BasicBlock &BB) {
229   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
230   
231   bool MadeChange = false;
232   
233   // Pointers alloca'd in this function are dead in the end block
234   SmallPtrSet<Value*, 64> deadPointers;
235   
236   // Find all of the alloca'd pointers in the entry block.
237   BasicBlock *Entry = BB.getParent()->begin();
238   for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
239     if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
240       deadPointers.insert(AI);
241   
242   // Treat byval arguments the same, stores to them are dead at the end of the
243   // function.
244   for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
245        AE = BB.getParent()->arg_end(); AI != AE; ++AI)
246     if (AI->hasByValAttr())
247       deadPointers.insert(AI);
248   
249   // Scan the basic block backwards
250   for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
251     --BBI;
252     
253     // If we find a store whose pointer is dead.
254     if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
255       if (!S->isVolatile()) {
256         // See through pointer-to-pointer bitcasts
257         Value* pointerOperand = S->getPointerOperand()->getUnderlyingObject();
258
259         // Alloca'd pointers or byval arguments (which are functionally like
260         // alloca's) are valid candidates for removal.
261         if (deadPointers.count(pointerOperand)) {
262           // DCE instructions only used to calculate that store.
263           BBI++;
264           DeleteDeadInstruction(S, &deadPointers);
265           NumFastStores++;
266           MadeChange = true;
267         }
268       }
269       
270       continue;
271     }
272     
273     // We can also remove memcpy's to local variables at the end of a function.
274     if (MemCpyInst *M = dyn_cast<MemCpyInst>(BBI)) {
275       Value *dest = M->getDest()->getUnderlyingObject();
276
277       if (deadPointers.count(dest)) {
278         BBI++;
279         DeleteDeadInstruction(M, &deadPointers);
280         NumFastOther++;
281         MadeChange = true;
282         continue;
283       }
284       
285       // Because a memcpy is also a load, we can't skip it if we didn't remove
286       // it.
287     }
288     
289     Value* killPointer = 0;
290     uint64_t killPointerSize = ~0UL;
291     
292     // If we encounter a use of the pointer, it is no longer considered dead
293     if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
294       // However, if this load is unused and not volatile, we can go ahead and
295       // remove it, and not have to worry about it making our pointer undead!
296       if (L->use_empty() && !L->isVolatile()) {
297         BBI++;
298         DeleteDeadInstruction(L, &deadPointers);
299         NumFastOther++;
300         MadeChange = true;
301         continue;
302       }
303       
304       killPointer = L->getPointerOperand();
305     } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
306       killPointer = V->getOperand(0);
307     } else if (isa<MemCpyInst>(BBI) &&
308                isa<ConstantInt>(cast<MemCpyInst>(BBI)->getLength())) {
309       killPointer = cast<MemCpyInst>(BBI)->getSource();
310       killPointerSize = cast<ConstantInt>(
311                             cast<MemCpyInst>(BBI)->getLength())->getZExtValue();
312     } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
313       deadPointers.erase(A);
314       
315       // Dead alloca's can be DCE'd when we reach them
316       if (A->use_empty()) {
317         BBI++;
318         DeleteDeadInstruction(A, &deadPointers);
319         NumFastOther++;
320         MadeChange = true;
321       }
322       
323       continue;
324     } else if (CallSite::get(BBI).getInstruction() != 0) {
325       // If this call does not access memory, it can't
326       // be undeadifying any of our pointers.
327       CallSite CS = CallSite::get(BBI);
328       if (AA.doesNotAccessMemory(CS))
329         continue;
330       
331       unsigned modRef = 0;
332       unsigned other = 0;
333       
334       // Remove any pointers made undead by the call from the dead set
335       std::vector<Value*> dead;
336       for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
337            E = deadPointers.end(); I != E; ++I) {
338         // HACK: if we detect that our AA is imprecise, it's not
339         // worth it to scan the rest of the deadPointers set.  Just
340         // assume that the AA will return ModRef for everything, and
341         // go ahead and bail.
342         if (modRef >= 16 && other == 0) {
343           deadPointers.clear();
344           return MadeChange;
345         }
346
347         // Get size information for the alloca
348         unsigned pointerSize = ~0U;
349         if (TD) {
350           if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
351             if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
352               pointerSize = C->getZExtValue() *
353                             TD->getTypeAllocSize(A->getAllocatedType());
354           } else {
355             const PointerType* PT = cast<PointerType>(
356                                                    cast<Argument>(*I)->getType());
357             pointerSize = TD->getTypeAllocSize(PT->getElementType());
358           }
359         }
360
361         // See if the call site touches it
362         AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
363         
364         if (A == AliasAnalysis::ModRef)
365           modRef++;
366         else
367           other++;
368         
369         if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
370           dead.push_back(*I);
371       }
372
373       for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
374            I != E; ++I)
375         deadPointers.erase(*I);
376       
377       continue;
378     } else if (isInstructionTriviallyDead(BBI)) {
379       // For any non-memory-affecting non-terminators, DCE them as we reach them
380       Instruction *Inst = BBI;
381       BBI++;
382       DeleteDeadInstruction(Inst, &deadPointers);
383       NumFastOther++;
384       MadeChange = true;
385       continue;
386     }
387     
388     if (!killPointer)
389       continue;
390
391     killPointer = killPointer->getUnderlyingObject();
392
393     // Deal with undead pointers
394     MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
395                                        deadPointers);
396   }
397   
398   return MadeChange;
399 }
400
401 /// RemoveUndeadPointers - check for uses of a pointer that make it
402 /// undead when scanning for dead stores to alloca's.
403 bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize,
404                                BasicBlock::iterator &BBI,
405                                SmallPtrSet<Value*, 64>& deadPointers) {
406   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
407                                   
408   // If the kill pointer can be easily reduced to an alloca,
409   // don't bother doing extraneous AA queries.
410   if (deadPointers.count(killPointer)) {
411     deadPointers.erase(killPointer);
412     return false;
413   }
414   
415   // A global can't be in the dead pointer set.
416   if (isa<GlobalValue>(killPointer))
417     return false;
418   
419   bool MadeChange = false;
420   
421   SmallVector<Value*, 16> undead;
422     
423   for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
424       E = deadPointers.end(); I != E; ++I) {
425     // Get size information for the alloca.
426     unsigned pointerSize = ~0U;
427     if (TD) {
428       if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
429         if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
430           pointerSize = C->getZExtValue() *
431                         TD->getTypeAllocSize(A->getAllocatedType());
432       } else {
433         const PointerType* PT = cast<PointerType>(cast<Argument>(*I)->getType());
434         pointerSize = TD->getTypeAllocSize(PT->getElementType());
435       }
436     }
437
438     // See if this pointer could alias it
439     AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
440                                             killPointer, killPointerSize);
441
442     // If it must-alias and a store, we can delete it
443     if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
444       StoreInst* S = cast<StoreInst>(BBI);
445
446       // Remove it!
447       BBI++;
448       DeleteDeadInstruction(S, &deadPointers);
449       NumFastStores++;
450       MadeChange = true;
451
452       continue;
453
454       // Otherwise, it is undead
455     } else if (A != AliasAnalysis::NoAlias)
456       undead.push_back(*I);
457   }
458
459   for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
460        I != E; ++I)
461       deadPointers.erase(*I);
462   
463   return MadeChange;
464 }
465
466 /// DeleteDeadInstruction - Delete this instruction.  Before we do, go through
467 /// and zero out all the operands of this instruction.  If any of them become
468 /// dead, delete them and the computation tree that feeds them.
469 ///
470 /// If ValueSet is non-null, remove any deleted instructions from it as well.
471 ///
472 void DSE::DeleteDeadInstruction(Instruction *I,
473                                 SmallPtrSet<Value*, 64> *ValueSet) {
474   SmallVector<Instruction*, 32> NowDeadInsts;
475   
476   NowDeadInsts.push_back(I);
477   --NumFastOther;
478
479   // Before we touch this instruction, remove it from memdep!
480   MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
481   while (!NowDeadInsts.empty()) {
482     Instruction *DeadInst = NowDeadInsts.back();
483     NowDeadInsts.pop_back();
484     
485     ++NumFastOther;
486     
487     // This instruction is dead, zap it, in stages.  Start by removing it from
488     // MemDep, which needs to know the operands and needs it to be in the
489     // function.
490     MDA.removeInstruction(DeadInst);
491     
492     for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
493       Value *Op = DeadInst->getOperand(op);
494       DeadInst->setOperand(op, 0);
495       
496       // If this operand just became dead, add it to the NowDeadInsts list.
497       if (!Op->use_empty()) continue;
498       
499       if (Instruction *OpI = dyn_cast<Instruction>(Op))
500         if (isInstructionTriviallyDead(OpI))
501           NowDeadInsts.push_back(OpI);
502     }
503     
504     DeadInst->eraseFromParent();
505     
506     if (ValueSet) ValueSet->erase(DeadInst);
507   }
508 }