80 col / tabs fixes
[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/SetVector.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Analysis/AliasAnalysis.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     static char ID; // Pass identification, replacement for typeid
41     DSE() : FunctionPass((intptr_t)&ID) {}
42
43     virtual bool runOnFunction(Function &F) {
44       bool Changed = false;
45       for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
46         Changed |= runOnBasicBlock(*I);
47       return Changed;
48     }
49
50     bool runOnBasicBlock(BasicBlock &BB);
51     bool handleFreeWithNonTrivialDependency(FreeInst* F,
52                                             Instruction* dependency,
53                                         SetVector<Instruction*>& possiblyDead);
54     bool handleEndBlock(BasicBlock& BB, SetVector<Instruction*>& possiblyDead);
55     bool RemoveUndeadPointers(Value* pointer, uint64_t killPointerSize,
56                               BasicBlock::iterator& BBI,
57                               SmallPtrSet<Value*, 64>& deadPointers, 
58                               SetVector<Instruction*>& possiblyDead);
59     void DeleteDeadInstructionChains(Instruction *I,
60                                      SetVector<Instruction*> &DeadInsts);
61     
62     /// Find the base pointer that a pointer came from
63     /// Because this is used to find pointers that originate
64     /// from allocas, it is safe to ignore GEP indices, since
65     /// either the store will be in the alloca, and thus dead,
66     /// or beyond the end of the alloca, and thus undefined.
67     void TranslatePointerBitCasts(Value*& v, bool zeroGepsOnly = false) {
68       assert(isa<PointerType>(v->getType()) &&
69              "Translating a non-pointer type?");
70       while (true) {
71         if (BitCastInst* C = dyn_cast<BitCastInst>(v))
72           v = C->getOperand(0);
73         else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(v))
74           if (!zeroGepsOnly || G->hasAllZeroIndices()) {
75             v = G->getOperand(0);
76           } else {
77             break;
78           }
79         else
80           break;
81       }
82     }
83
84     // getAnalysisUsage - We require post dominance frontiers (aka Control
85     // Dependence Graph)
86     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
87       AU.setPreservesCFG();
88       AU.addRequired<TargetData>();
89       AU.addRequired<AliasAnalysis>();
90       AU.addRequired<MemoryDependenceAnalysis>();
91       AU.addPreserved<AliasAnalysis>();
92       AU.addPreserved<MemoryDependenceAnalysis>();
93     }
94   };
95 }
96
97 char DSE::ID = 0;
98 static RegisterPass<DSE> X("dse", "Dead Store Elimination");
99
100 FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
101
102 bool DSE::runOnBasicBlock(BasicBlock &BB) {
103   MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
104   TargetData &TD = getAnalysis<TargetData>();  
105
106   // Record the last-seen store to this pointer
107   DenseMap<Value*, StoreInst*> lastStore;
108   // Record instructions possibly made dead by deleting a store
109   SetVector<Instruction*> possiblyDead;
110   
111   bool MadeChange = false;
112   
113   // Do a top-down walk on the BB
114   for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end();
115        BBI != BBE; ++BBI) {
116     // If we find a store or a free...
117     if (!isa<StoreInst>(BBI) && !isa<FreeInst>(BBI))
118       continue;
119       
120     Value* pointer = 0;
121     if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
122       if (!S->isVolatile())
123         pointer = S->getPointerOperand();
124       else
125         continue;
126     } else
127       pointer = cast<FreeInst>(BBI)->getPointerOperand();
128       
129     TranslatePointerBitCasts(pointer, true);
130     StoreInst*& last = lastStore[pointer];
131     bool deletedStore = false;
132       
133     // ... to a pointer that has been stored to before...
134     if (last) {
135       Instruction* dep = MD.getDependency(BBI);
136         
137       // ... and no other memory dependencies are between them....
138       while (dep != MemoryDependenceAnalysis::None &&
139              dep != MemoryDependenceAnalysis::NonLocal &&
140              isa<StoreInst>(dep)) {
141         if (dep != last ||
142              TD.getTypeStoreSize(last->getOperand(0)->getType()) >
143              TD.getTypeStoreSize(BBI->getOperand(0)->getType())) {
144           dep = MD.getDependency(BBI, dep);
145           continue;
146         }
147         
148         // Remove it!
149         MD.removeInstruction(last);
150           
151         // DCE instructions only used to calculate that store
152         if (Instruction* D = dyn_cast<Instruction>(last->getOperand(0)))
153           possiblyDead.insert(D);
154         if (Instruction* D = dyn_cast<Instruction>(last->getOperand(1)))
155           possiblyDead.insert(D);
156         
157         last->eraseFromParent();
158         NumFastStores++;
159         deletedStore = true;
160         MadeChange = true;
161           
162         break;
163       }
164     }
165     
166     // Handle frees whose dependencies are non-trivial.
167     if (FreeInst* F = dyn_cast<FreeInst>(BBI)) {
168       if (!deletedStore)
169         MadeChange |= handleFreeWithNonTrivialDependency(F,
170                                                          MD.getDependency(F),
171                                                          possiblyDead);
172       // No known stores after the free
173       last = 0;
174     } else {
175       // Update our most-recent-store map.
176       last = cast<StoreInst>(BBI);
177     }
178   }
179   
180   // If this block ends in a return, unwind, unreachable, and eventually
181   // tailcall, then all allocas are dead at its end.
182   if (BB.getTerminator()->getNumSuccessors() == 0)
183     MadeChange |= handleEndBlock(BB, possiblyDead);
184   
185   // Do a trivial DCE
186   while (!possiblyDead.empty()) {
187     Instruction *I = possiblyDead.back();
188     possiblyDead.pop_back();
189     DeleteDeadInstructionChains(I, possiblyDead);
190   }
191   
192   return MadeChange;
193 }
194
195 /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
196 /// dependency is a store to a field of that structure
197 bool DSE::handleFreeWithNonTrivialDependency(FreeInst* F, Instruction* dep,
198                                        SetVector<Instruction*>& possiblyDead) {
199   TargetData &TD = getAnalysis<TargetData>();
200   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
201   MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
202   
203   if (dep == MemoryDependenceAnalysis::None ||
204       dep == MemoryDependenceAnalysis::NonLocal)
205     return false;
206   
207   StoreInst* dependency = dyn_cast<StoreInst>(dep);
208   if (!dependency)
209     return false;
210   else if (dependency->isVolatile())
211     return false;
212   
213   Value* depPointer = dependency->getPointerOperand();
214   const Type* depType = dependency->getOperand(0)->getType();
215   unsigned depPointerSize = TD.getTypeStoreSize(depType);
216
217   // Check for aliasing
218   AliasAnalysis::AliasResult A = AA.alias(F->getPointerOperand(), ~0U,
219                                           depPointer, depPointerSize);
220
221   if (A == AliasAnalysis::MustAlias) {
222     // Remove it!
223     MD.removeInstruction(dependency);
224
225     // DCE instructions only used to calculate that store
226     if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(0)))
227       possiblyDead.insert(D);
228     if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(1)))
229       possiblyDead.insert(D);
230
231     dependency->eraseFromParent();
232     NumFastStores++;
233     return true;
234   }
235   
236   return false;
237 }
238
239 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
240 /// function end block.  Ex:
241 /// %A = alloca i32
242 /// ...
243 /// store i32 1, i32* %A
244 /// ret void
245 bool DSE::handleEndBlock(BasicBlock& BB,
246                          SetVector<Instruction*>& possiblyDead) {
247   TargetData &TD = getAnalysis<TargetData>();
248   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
249   MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
250   
251   bool MadeChange = false;
252   
253   // Pointers alloca'd in this function are dead in the end block
254   SmallPtrSet<Value*, 64> deadPointers;
255   
256   // Find all of the alloca'd pointers in the entry block
257   BasicBlock *Entry = BB.getParent()->begin();
258   for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
259     if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
260       deadPointers.insert(AI);
261   for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
262        AE = BB.getParent()->arg_end(); AI != AE; ++AI)
263     if (AI->hasByValAttr())
264       deadPointers.insert(AI);
265   
266   // Scan the basic block backwards
267   for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
268     --BBI;
269     
270     // If we find a store whose pointer is dead...
271     if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
272       if (!S->isVolatile()) {
273         Value* pointerOperand = S->getPointerOperand();
274         // See through pointer-to-pointer bitcasts
275         TranslatePointerBitCasts(pointerOperand);
276       
277         // Alloca'd pointers or byval arguments (which are functionally like
278         // alloca's) are valid candidates for removal.
279         if (deadPointers.count(pointerOperand)) {
280           // Remove it!
281           MD.removeInstruction(S);
282         
283           // DCE instructions only used to calculate that store
284           if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
285             possiblyDead.insert(D);
286           if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
287             possiblyDead.insert(D);
288         
289           BBI++;
290           S->eraseFromParent();
291           NumFastStores++;
292           MadeChange = true;
293         }
294       }
295       
296       continue;
297     
298     // We can also remove memcpy's to local variables at the end of a function
299     } else if (MemCpyInst* M = dyn_cast<MemCpyInst>(BBI)) {
300       Value* dest = M->getDest();
301       TranslatePointerBitCasts(dest);
302       
303       if (deadPointers.count(dest)) {
304         MD.removeInstruction(M);
305         
306         // DCE instructions only used to calculate that memcpy
307         if (Instruction* D = dyn_cast<Instruction>(M->getRawSource()))
308           possiblyDead.insert(D);
309         if (Instruction* D = dyn_cast<Instruction>(M->getLength()))
310           possiblyDead.insert(D);
311         if (Instruction* D = dyn_cast<Instruction>(M->getRawDest()))
312           possiblyDead.insert(D);
313         
314         BBI++;
315         M->eraseFromParent();
316         NumFastOther++;
317         MadeChange = true;
318         
319         continue;
320       }
321       
322       // Because a memcpy is also a load, we can't skip it if we didn't remove it
323     }
324     
325     Value* killPointer = 0;
326     uint64_t killPointerSize = ~0UL;
327     
328     // If we encounter a use of the pointer, it is no longer considered dead
329     if (LoadInst* L = dyn_cast<LoadInst>(BBI)) {
330       // However, if this load is unused and not volatile, we can go ahead and
331       // remove it, and not have to worry about it making our pointer undead!
332       if (L->use_empty() && !L->isVolatile()) {
333         MD.removeInstruction(L);
334         
335         // DCE instructions only used to calculate that load
336         if (Instruction* D = dyn_cast<Instruction>(L->getPointerOperand()))
337           possiblyDead.insert(D);
338         
339         BBI++;
340         L->eraseFromParent();
341         NumFastOther++;
342         MadeChange = true;
343         possiblyDead.remove(L);
344         
345         continue;
346       }
347       
348       killPointer = L->getPointerOperand();
349     } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
350       killPointer = V->getOperand(0);
351     } else if (isa<MemCpyInst>(BBI) &&
352                isa<ConstantInt>(cast<MemCpyInst>(BBI)->getLength())) {
353       killPointer = cast<MemCpyInst>(BBI)->getSource();
354       killPointerSize = cast<ConstantInt>(
355                             cast<MemCpyInst>(BBI)->getLength())->getZExtValue();
356     } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
357       deadPointers.erase(A);
358       
359       // Dead alloca's can be DCE'd when we reach them
360       if (A->use_empty()) {
361         MD.removeInstruction(A);
362         
363         // DCE instructions only used to calculate that load
364         if (Instruction* D = dyn_cast<Instruction>(A->getArraySize()))
365           possiblyDead.insert(D);
366         
367         BBI++;
368         A->eraseFromParent();
369         NumFastOther++;
370         MadeChange = true;
371         possiblyDead.remove(A);
372       }
373       
374       continue;
375     } else if (CallSite::get(BBI).getInstruction() != 0) {
376       // If this call does not access memory, it can't
377       // be undeadifying any of our pointers.
378       CallSite CS = CallSite::get(BBI);
379       if (AA.doesNotAccessMemory(CS))
380         continue;
381       
382       unsigned modRef = 0;
383       unsigned other = 0;
384       
385       // Remove any pointers made undead by the call from the dead set
386       std::vector<Value*> dead;
387       for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
388            E = deadPointers.end(); I != E; ++I) {
389         // HACK: if we detect that our AA is imprecise, it's not
390         // worth it to scan the rest of the deadPointers set.  Just
391         // assume that the AA will return ModRef for everything, and
392         // go ahead and bail.
393         if (modRef >= 16 && other == 0) {
394           deadPointers.clear();
395           return MadeChange;
396         }
397
398         // Get size information for the alloca
399         unsigned pointerSize = ~0U;
400         if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
401           if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
402             pointerSize = C->getZExtValue() * \
403                           TD.getABITypeSize(A->getAllocatedType());
404         } else {
405           const PointerType* PT = cast<PointerType>(
406                                                  cast<Argument>(*I)->getType());
407           pointerSize = TD.getABITypeSize(PT->getElementType());
408         }
409
410         // See if the call site touches it
411         AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
412         
413         if (A == AliasAnalysis::ModRef)
414           modRef++;
415         else
416           other++;
417         
418         if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
419           dead.push_back(*I);
420       }
421
422       for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
423            I != E; ++I)
424         deadPointers.erase(*I);
425       
426       continue;
427     } else {
428       // For any non-memory-affecting non-terminators, DCE them as we reach them
429       Instruction *CI = BBI;
430       if (!CI->isTerminator() && CI->use_empty() && !isa<FreeInst>(CI)) {
431         
432         // DCE instructions only used to calculate that load
433         for (Instruction::op_iterator OI = CI->op_begin(), OE = CI->op_end();
434              OI != OE; ++OI)
435           if (Instruction* D = dyn_cast<Instruction>(OI))
436             possiblyDead.insert(D);
437         
438         BBI++;
439         CI->eraseFromParent();
440         NumFastOther++;
441         MadeChange = true;
442         possiblyDead.remove(CI);
443         
444         continue;
445       }
446     }
447     
448     if (!killPointer)
449       continue;
450     
451     TranslatePointerBitCasts(killPointer);
452     
453     // Deal with undead pointers
454     MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
455                                        deadPointers, possiblyDead);
456   }
457   
458   return MadeChange;
459 }
460
461 /// RemoveUndeadPointers - check for uses of a pointer that make it
462 /// undead when scanning for dead stores to alloca's.
463 bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize,
464                                 BasicBlock::iterator& BBI,
465                                 SmallPtrSet<Value*, 64>& deadPointers, 
466                                 SetVector<Instruction*>& possiblyDead) {
467   TargetData &TD = getAnalysis<TargetData>();
468   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
469   MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
470                                   
471   // If the kill pointer can be easily reduced to an alloca,
472   // don't bother doing extraneous AA queries
473   if (deadPointers.count(killPointer)) {
474     deadPointers.erase(killPointer);
475     return false;
476   } else if (isa<GlobalValue>(killPointer)) {
477     // A global can't be in the dead pointer set
478     return false;
479   }
480   
481   bool MadeChange = false;
482   
483   std::vector<Value*> undead;
484     
485   for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
486       E = deadPointers.end(); I != E; ++I) {
487     // Get size information for the alloca
488     unsigned pointerSize = ~0U;
489     if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
490       if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
491         pointerSize = C->getZExtValue() * \
492                       TD.getABITypeSize(A->getAllocatedType());
493     } else {
494       const PointerType* PT = cast<PointerType>(
495                                                 cast<Argument>(*I)->getType());
496       pointerSize = TD.getABITypeSize(PT->getElementType());
497     }
498
499     // See if this pointer could alias it
500     AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
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       MD.removeInstruction(S);
509
510       // DCE instructions only used to calculate that store
511       if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
512         possiblyDead.insert(D);
513       if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
514         possiblyDead.insert(D);
515
516       BBI++;
517       S->eraseFromParent();
518       NumFastStores++;
519       MadeChange = true;
520
521       continue;
522
523       // Otherwise, it is undead
524       } else if (A != AliasAnalysis::NoAlias)
525         undead.push_back(*I);
526   }
527
528   for (std::vector<Value*>::iterator I = undead.begin(), E = undead.end();
529        I != E; ++I)
530       deadPointers.erase(*I);
531   
532   return MadeChange;
533 }
534
535 /// DeleteDeadInstructionChains - takes an instruction and a setvector of
536 /// dead instructions.  If I is dead, it is erased, and its operands are
537 /// checked for deadness.  If they are dead, they are added to the dead
538 /// setvector.
539 void DSE::DeleteDeadInstructionChains(Instruction *I,
540                                       SetVector<Instruction*> &DeadInsts) {
541   // Instruction must be dead.
542   if (!I->use_empty() || !isInstructionTriviallyDead(I)) return;
543
544   // Let the memory dependence know
545   getAnalysis<MemoryDependenceAnalysis>().removeInstruction(I);
546
547   // See if this made any operands dead.  We do it this way in case the
548   // instruction uses the same operand twice.  We don't want to delete a
549   // value then reference it.
550   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
551     if (I->getOperand(i)->hasOneUse())
552       if (Instruction* Op = dyn_cast<Instruction>(I->getOperand(i)))
553         DeadInsts.insert(Op);      // Attempt to nuke it later.
554     
555     I->setOperand(i, 0);         // Drop from the operand list.
556   }
557
558   I->eraseFromParent();
559   ++NumFastOther;
560 }