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