d81d302ebaf3ae42d63db4004aee173ef9263aad
[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       initializeDSEPass(*PassRegistry::getPassRegistry());
45     }
46
47     virtual bool runOnFunction(Function &F) {
48       bool Changed = false;
49       
50       DominatorTree &DT = getAnalysis<DominatorTree>();
51       
52       for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
53         // Only check non-dead blocks.  Dead blocks may have strange pointer
54         // cycles that will confuse alias analysis.
55         if (DT.isReachableFromEntry(I))
56           Changed |= runOnBasicBlock(*I);
57       return Changed;
58     }
59     
60     bool runOnBasicBlock(BasicBlock &BB);
61     bool handleFreeWithNonTrivialDependency(const CallInst *F,
62                                             MemDepResult Dep);
63     bool handleEndBlock(BasicBlock &BB);
64     bool RemoveUndeadPointers(Value *Ptr, uint64_t killPointerSize,
65                               BasicBlock::iterator &BBI,
66                               SmallPtrSet<Value*, 64> &deadPointers);
67     void DeleteDeadInstruction(Instruction *I,
68                                SmallPtrSet<Value*, 64> *deadPointers = 0);
69     
70
71     // getAnalysisUsage - We require post dominance frontiers (aka Control
72     // Dependence Graph)
73     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74       AU.setPreservesCFG();
75       AU.addRequired<DominatorTree>();
76       AU.addRequired<AliasAnalysis>();
77       AU.addRequired<MemoryDependenceAnalysis>();
78       AU.addPreserved<DominatorTree>();
79       AU.addPreserved<MemoryDependenceAnalysis>();
80     }
81
82     uint64_t getPointerSize(Value *V) const;
83   };
84 }
85
86 char DSE::ID = 0;
87 INITIALIZE_PASS_BEGIN(DSE, "dse", "Dead Store Elimination", false, false)
88 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
89 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
90 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
91 INITIALIZE_PASS_END(DSE, "dse", "Dead Store Elimination", false, false)
92
93 FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
94
95 /// doesClobberMemory - Does this instruction clobber (write without reading)
96 /// some memory?
97 static bool doesClobberMemory(Instruction *I) {
98   if (isa<StoreInst>(I))
99     return true;
100   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
101     switch (II->getIntrinsicID()) {
102     default:
103       return false;
104     case Intrinsic::memset:
105     case Intrinsic::memmove:
106     case Intrinsic::memcpy:
107     case Intrinsic::init_trampoline:
108     case Intrinsic::lifetime_end:
109       return true;
110     }
111   }
112   return false;
113 }
114
115 /// isElidable - If the value of this instruction and the memory it writes to is
116 /// unused, may we delete this instrtction?
117 static bool isElidable(Instruction *I) {
118   assert(doesClobberMemory(I));
119   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
120     return II->getIntrinsicID() != Intrinsic::lifetime_end;
121   if (StoreInst *SI = dyn_cast<StoreInst>(I))
122     return !SI->isVolatile();
123   return true;
124 }
125
126 /// getPointerOperand - Return the pointer that is being clobbered.
127 static Value *getPointerOperand(Instruction *I) {
128   assert(doesClobberMemory(I));
129   if (StoreInst *SI = dyn_cast<StoreInst>(I))
130     return SI->getPointerOperand();
131   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
132     return MI->getArgOperand(0);
133
134   IntrinsicInst *II = cast<IntrinsicInst>(I);
135   switch (II->getIntrinsicID()) {
136   default: assert(false && "Unexpected intrinsic!");
137   case Intrinsic::init_trampoline:
138     return II->getArgOperand(0);
139   case Intrinsic::lifetime_end:
140     return II->getArgOperand(1);
141   }
142 }
143
144 /// getStoreSize - Return the length in bytes of the write by the clobbering
145 /// instruction. If variable or unknown, returns AliasAnalysis::UnknownSize.
146 static uint64_t getStoreSize(Instruction *I, const TargetData *TD) {
147   assert(doesClobberMemory(I));
148   if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
149     if (!TD) return AliasAnalysis::UnknownSize;
150     return TD->getTypeStoreSize(SI->getOperand(0)->getType());
151   }
152
153   Value *Len;
154   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
155     Len = MI->getLength();
156   } else {
157     IntrinsicInst *II = cast<IntrinsicInst>(I);
158     switch (II->getIntrinsicID()) {
159     default: assert(false && "Unexpected intrinsic!");
160     case Intrinsic::init_trampoline:
161       return AliasAnalysis::UnknownSize;
162     case Intrinsic::lifetime_end:
163       Len = II->getArgOperand(0);
164       break;
165     }
166   }
167   if (ConstantInt *LenCI = dyn_cast<ConstantInt>(Len))
168     if (!LenCI->isAllOnesValue())
169       return LenCI->getZExtValue();
170   return AliasAnalysis::UnknownSize;
171 }
172
173 /// isStoreAtLeastAsWideAs - Return true if the size of the store in I1 is
174 /// greater than or equal to the store in I2.  This returns false if we don't
175 /// know.
176 ///
177 static bool isStoreAtLeastAsWideAs(Instruction *I1, Instruction *I2,
178                                    const TargetData *TD) {
179   const Type *I1Ty = getPointerOperand(I1)->getType();
180   const Type *I2Ty = getPointerOperand(I2)->getType();
181   
182   // Exactly the same type, must have exactly the same size.
183   if (I1Ty == I2Ty) return true;
184   
185   uint64_t I1Size = getStoreSize(I1, TD);
186   uint64_t I2Size = getStoreSize(I2, TD);
187   
188   return I1Size != AliasAnalysis::UnknownSize &&
189          I2Size != AliasAnalysis::UnknownSize &&
190          I1Size >= I2Size;
191 }
192
193 bool DSE::runOnBasicBlock(BasicBlock &BB) {
194   MemoryDependenceAnalysis &MD = getAnalysis<MemoryDependenceAnalysis>();
195   TD = getAnalysisIfAvailable<TargetData>();
196
197   bool MadeChange = false;
198   
199   // Do a top-down walk on the BB.
200   for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
201     Instruction *Inst = BBI++;
202     
203     // If we find a store or a free, get its memory dependence.
204     if (!doesClobberMemory(Inst) && !isFreeCall(Inst))
205       continue;
206     
207     MemDepResult InstDep = MD.getDependency(Inst);
208     
209     // Ignore non-local stores.
210     // FIXME: cross-block DSE would be fun. :)
211     if (InstDep.isNonLocal()) continue;
212   
213     // Handle frees whose dependencies are non-trivial.
214     if (const CallInst *F = isFreeCall(Inst)) {
215       MadeChange |= handleFreeWithNonTrivialDependency(F, InstDep);
216       continue;
217     }
218     
219     // If not a definite must-alias dependency, ignore it.
220     if (!InstDep.isDef())
221       continue;
222     
223     // If this is a store-store dependence, then the previous store is dead so
224     // long as this store is at least as big as it.
225     if (doesClobberMemory(InstDep.getInst())) {
226       Instruction *DepStore = InstDep.getInst();
227       if (isStoreAtLeastAsWideAs(Inst, DepStore, TD) &&
228           isElidable(DepStore)) {
229         // Delete the store and now-dead instructions that feed it.
230         DeleteDeadInstruction(DepStore);
231         ++NumFastStores;
232         MadeChange = true;
233
234         // DeleteDeadInstruction can delete the current instruction in loop
235         // cases, reset BBI.
236         BBI = Inst;
237         if (BBI != BB.begin())
238           --BBI;
239         continue;
240       }
241     }
242     
243     if (!isElidable(Inst))
244       continue;
245     
246     // If we're storing the same value back to a pointer that we just
247     // loaded from, then the store can be removed.
248     if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
249       if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
250         if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
251             SI->getOperand(0) == DepLoad) {
252           // DeleteDeadInstruction can delete the current instruction.  Save BBI
253           // in case we need it.
254           WeakVH NextInst(BBI);
255           
256           DeleteDeadInstruction(SI);
257           
258           if (NextInst == 0)  // Next instruction deleted.
259             BBI = BB.begin();
260           else if (BBI != BB.begin())  // Revisit this instruction if possible.
261             --BBI;
262           ++NumFastStores;
263           MadeChange = true;
264           continue;
265         }
266       }
267     }
268     
269     // If this is a lifetime end marker, we can throw away the store.
270     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(InstDep.getInst())) {
271       if (II->getIntrinsicID() == Intrinsic::lifetime_end) {
272         // Delete the store and now-dead instructions that feed it.
273         // DeleteDeadInstruction can delete the current instruction.  Save BBI
274         // in case we need it.
275         WeakVH NextInst(BBI);
276         
277         DeleteDeadInstruction(Inst);
278         
279         if (NextInst == 0)  // Next instruction deleted.
280           BBI = BB.begin();
281         else if (BBI != BB.begin())  // Revisit this instruction if possible.
282           --BBI;
283         ++NumFastStores;
284         MadeChange = true;
285         continue;
286       }
287     }
288   }
289   
290   // If this block ends in a return, unwind, or unreachable, all allocas are
291   // dead at its end, which means stores to them are also dead.
292   if (BB.getTerminator()->getNumSuccessors() == 0)
293     MadeChange |= handleEndBlock(BB);
294   
295   return MadeChange;
296 }
297
298 /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
299 /// dependency is a store to a field of that structure.
300 bool DSE::handleFreeWithNonTrivialDependency(const CallInst *F,
301                                              MemDepResult Dep) {
302   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
303   
304   Instruction *Dependency = Dep.getInst();
305   if (!Dependency || !doesClobberMemory(Dependency) || !isElidable(Dependency))
306     return false;
307   
308   Value *DepPointer = getPointerOperand(Dependency)->getUnderlyingObject();
309
310   // Check for aliasing.
311   if (AA.alias(F->getArgOperand(0), 1, DepPointer, 1) !=
312          AliasAnalysis::MustAlias)
313     return false;
314   
315   // DCE instructions only used to calculate that store
316   DeleteDeadInstruction(Dependency);
317   ++NumFastStores;
318   return true;
319 }
320
321 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
322 /// function end block.  Ex:
323 /// %A = alloca i32
324 /// ...
325 /// store i32 1, i32* %A
326 /// ret void
327 bool DSE::handleEndBlock(BasicBlock &BB) {
328   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
329   
330   bool MadeChange = false;
331   
332   // Pointers alloca'd in this function are dead in the end block
333   SmallPtrSet<Value*, 64> deadPointers;
334   
335   // Find all of the alloca'd pointers in the entry block.
336   BasicBlock *Entry = BB.getParent()->begin();
337   for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
338     if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
339       deadPointers.insert(AI);
340   
341   // Treat byval arguments the same, stores to them are dead at the end of the
342   // function.
343   for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
344        AE = BB.getParent()->arg_end(); AI != AE; ++AI)
345     if (AI->hasByValAttr())
346       deadPointers.insert(AI);
347   
348   // Scan the basic block backwards
349   for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
350     --BBI;
351     
352     // If we find a store whose pointer is dead.
353     if (doesClobberMemory(BBI)) {
354       if (isElidable(BBI)) {
355         // See through pointer-to-pointer bitcasts
356         Value *pointerOperand = getPointerOperand(BBI)->getUnderlyingObject();
357
358         // Alloca'd pointers or byval arguments (which are functionally like
359         // alloca's) are valid candidates for removal.
360         if (deadPointers.count(pointerOperand)) {
361           // DCE instructions only used to calculate that store.
362           Instruction *Dead = BBI;
363           ++BBI;
364           DeleteDeadInstruction(Dead, &deadPointers);
365           ++NumFastStores;
366           MadeChange = true;
367           continue;
368         }
369       }
370       
371       // Because a memcpy or memmove is also a load, we can't skip it if we
372       // didn't remove it.
373       if (!isa<MemTransferInst>(BBI))
374         continue;
375     }
376     
377     Value *killPointer = 0;
378     uint64_t killPointerSize = AliasAnalysis::UnknownSize;
379     
380     // If we encounter a use of the pointer, it is no longer considered dead
381     if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
382       // However, if this load is unused and not volatile, we can go ahead and
383       // remove it, and not have to worry about it making our pointer undead!
384       if (L->use_empty() && !L->isVolatile()) {
385         ++BBI;
386         DeleteDeadInstruction(L, &deadPointers);
387         ++NumFastOther;
388         MadeChange = true;
389         continue;
390       }
391       
392       killPointer = L->getPointerOperand();
393     } else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
394       killPointer = V->getOperand(0);
395     } else if (isa<MemTransferInst>(BBI) &&
396                isa<ConstantInt>(cast<MemTransferInst>(BBI)->getLength())) {
397       killPointer = cast<MemTransferInst>(BBI)->getSource();
398       killPointerSize = cast<ConstantInt>(
399                        cast<MemTransferInst>(BBI)->getLength())->getZExtValue();
400     } else if (AllocaInst *A = dyn_cast<AllocaInst>(BBI)) {
401       deadPointers.erase(A);
402       
403       // Dead alloca's can be DCE'd when we reach them
404       if (A->use_empty()) {
405         ++BBI;
406         DeleteDeadInstruction(A, &deadPointers);
407         ++NumFastOther;
408         MadeChange = true;
409       }
410       
411       continue;
412     } else if (CallSite CS = cast<Value>(BBI)) {
413       // If this call does not access memory, it can't
414       // be undeadifying any of our pointers.
415       if (AA.doesNotAccessMemory(CS))
416         continue;
417       
418       unsigned modRef = 0;
419       unsigned other = 0;
420       
421       // Remove any pointers made undead by the call from the dead set
422       std::vector<Value*> dead;
423       for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
424            E = deadPointers.end(); I != E; ++I) {
425         // HACK: if we detect that our AA is imprecise, it's not
426         // worth it to scan the rest of the deadPointers set.  Just
427         // assume that the AA will return ModRef for everything, and
428         // go ahead and bail.
429         if (modRef >= 16 && other == 0) {
430           deadPointers.clear();
431           return MadeChange;
432         }
433         
434         // See if the call site touches it
435         AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I,
436                                                          getPointerSize(*I));
437         
438         if (A == AliasAnalysis::ModRef)
439           ++modRef;
440         else
441           ++other;
442         
443         if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
444           dead.push_back(*I);
445       }
446
447       for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
448            I != E; ++I)
449         deadPointers.erase(*I);
450       
451       continue;
452     } else if (isInstructionTriviallyDead(BBI)) {
453       // For any non-memory-affecting non-terminators, DCE them as we reach them
454       Instruction *Inst = BBI;
455       ++BBI;
456       DeleteDeadInstruction(Inst, &deadPointers);
457       ++NumFastOther;
458       MadeChange = true;
459       continue;
460     }
461     
462     if (!killPointer)
463       continue;
464
465     killPointer = killPointer->getUnderlyingObject();
466
467     // Deal with undead pointers
468     MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
469                                        deadPointers);
470   }
471   
472   return MadeChange;
473 }
474
475 /// RemoveUndeadPointers - check for uses of a pointer that make it
476 /// undead when scanning for dead stores to alloca's.
477 bool DSE::RemoveUndeadPointers(Value *killPointer, uint64_t killPointerSize,
478                                BasicBlock::iterator &BBI,
479                                SmallPtrSet<Value*, 64> &deadPointers) {
480   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
481
482   // If the kill pointer can be easily reduced to an alloca,
483   // don't bother doing extraneous AA queries.
484   if (deadPointers.count(killPointer)) {
485     deadPointers.erase(killPointer);
486     return false;
487   }
488   
489   // A global can't be in the dead pointer set.
490   if (isa<GlobalValue>(killPointer))
491     return false;
492   
493   bool MadeChange = false;
494   
495   SmallVector<Value*, 16> undead;
496   
497   for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
498        E = deadPointers.end(); I != E; ++I) {
499     // See if this pointer could alias it
500     AliasAnalysis::AliasResult A = AA.alias(*I, getPointerSize(*I),
501                                             killPointer, killPointerSize);
502
503     // If it must-alias and a store, we can delete it
504     if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
505       StoreInst *S = cast<StoreInst>(BBI);
506
507       // Remove it!
508       ++BBI;
509       DeleteDeadInstruction(S, &deadPointers);
510       ++NumFastStores;
511       MadeChange = true;
512
513       continue;
514
515       // Otherwise, it is undead
516     } else if (A != AliasAnalysis::NoAlias)
517       undead.push_back(*I);
518   }
519
520   for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
521        I != E; ++I)
522       deadPointers.erase(*I);
523   
524   return MadeChange;
525 }
526
527 /// DeleteDeadInstruction - Delete this instruction.  Before we do, go through
528 /// and zero out all the operands of this instruction.  If any of them become
529 /// dead, delete them and the computation tree that feeds them.
530 ///
531 /// If ValueSet is non-null, remove any deleted instructions from it as well.
532 ///
533 void DSE::DeleteDeadInstruction(Instruction *I,
534                                 SmallPtrSet<Value*, 64> *ValueSet) {
535   SmallVector<Instruction*, 32> NowDeadInsts;
536   
537   NowDeadInsts.push_back(I);
538   --NumFastOther;
539
540   // Before we touch this instruction, remove it from memdep!
541   MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
542   do {
543     Instruction *DeadInst = NowDeadInsts.pop_back_val();
544     
545     ++NumFastOther;
546     
547     // This instruction is dead, zap it, in stages.  Start by removing it from
548     // MemDep, which needs to know the operands and needs it to be in the
549     // function.
550     MDA.removeInstruction(DeadInst);
551     
552     for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
553       Value *Op = DeadInst->getOperand(op);
554       DeadInst->setOperand(op, 0);
555       
556       // If this operand just became dead, add it to the NowDeadInsts list.
557       if (!Op->use_empty()) continue;
558       
559       if (Instruction *OpI = dyn_cast<Instruction>(Op))
560         if (isInstructionTriviallyDead(OpI))
561           NowDeadInsts.push_back(OpI);
562     }
563     
564     DeadInst->eraseFromParent();
565     
566     if (ValueSet) ValueSet->erase(DeadInst);
567   } while (!NowDeadInsts.empty());
568 }
569
570 uint64_t DSE::getPointerSize(Value *V) const {
571   if (TD) {
572     if (AllocaInst *A = dyn_cast<AllocaInst>(V)) {
573       // Get size information for the alloca
574       if (ConstantInt *C = dyn_cast<ConstantInt>(A->getArraySize()))
575         return C->getZExtValue() * TD->getTypeAllocSize(A->getAllocatedType());
576     } else {
577       assert(isa<Argument>(V) && "Expected AllocaInst or Argument!");
578       const PointerType *PT = cast<PointerType>(V->getType());
579       return TD->getTypeAllocSize(PT->getElementType());
580     }
581   }
582   return AliasAnalysis::UnknownSize;
583 }