1 //===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a trivial dead store elimination that only considers
11 // basic-block local redundant stores.
13 // FIXME: This should eventually be extended to be a post-dominator tree
14 // traversal. Doing so would be pretty trivial.
16 //===----------------------------------------------------------------------===//
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"
35 STATISTIC(NumFastStores, "Number of stores deleted");
36 STATISTIC(NumFastOther , "Number of other instrs removed");
39 struct VISIBILITY_HIDDEN DSE : public FunctionPass {
40 static char ID; // Pass identification, replacement for typeid
41 DSE() : FunctionPass((intptr_t)&ID) {}
43 virtual bool runOnFunction(Function &F) {
45 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
46 Changed |= runOnBasicBlock(*I);
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,
56 BasicBlock::iterator& BBI,
57 SmallPtrSet<Value*, 64>& deadPointers,
58 SetVector<Instruction*>& possiblyDead);
59 void DeleteDeadInstructionChains(Instruction *I,
60 SetVector<Instruction*> &DeadInsts);
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?");
71 if (BitCastInst* C = dyn_cast<BitCastInst>(v))
73 else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(v))
74 if (!zeroGepsOnly || G->hasAllZeroIndices()) {
84 // getAnalysisUsage - We require post dominance frontiers (aka Control
86 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
88 AU.addRequired<TargetData>();
89 AU.addRequired<AliasAnalysis>();
90 AU.addRequired<MemoryDependenceAnalysis>();
91 AU.addPreserved<AliasAnalysis>();
92 AU.addPreserved<MemoryDependenceAnalysis>();
96 RegisterPass<DSE> X("dse", "Dead Store Elimination");
99 FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
101 bool DSE::runOnBasicBlock(BasicBlock &BB) {
102 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
103 TargetData &TD = getAnalysis<TargetData>();
105 // Record the last-seen store to this pointer
106 DenseMap<Value*, StoreInst*> lastStore;
107 // Record instructions possibly made dead by deleting a store
108 SetVector<Instruction*> possiblyDead;
110 bool MadeChange = false;
112 // Do a top-down walk on the BB
113 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end();
115 // If we find a store or a free...
116 if (!isa<StoreInst>(BBI) && !isa<FreeInst>(BBI))
120 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
121 if (!S->isVolatile())
122 pointer = S->getPointerOperand();
126 pointer = cast<FreeInst>(BBI)->getPointerOperand();
128 TranslatePointerBitCasts(pointer, true);
129 StoreInst*& last = lastStore[pointer];
130 bool deletedStore = false;
132 // ... to a pointer that has been stored to before...
134 Instruction* dep = MD.getDependency(BBI);
136 // ... and no other memory dependencies are between them....
137 while (dep != MemoryDependenceAnalysis::None &&
138 dep != MemoryDependenceAnalysis::NonLocal &&
139 isa<StoreInst>(dep)) {
141 TD.getTypeStoreSize(last->getOperand(0)->getType()) >
142 TD.getTypeStoreSize(BBI->getOperand(0)->getType())) {
143 dep = MD.getDependency(BBI, dep);
148 MD.removeInstruction(last);
150 // DCE instructions only used to calculate that store
151 if (Instruction* D = dyn_cast<Instruction>(last->getOperand(0)))
152 possiblyDead.insert(D);
153 if (Instruction* D = dyn_cast<Instruction>(last->getOperand(1)))
154 possiblyDead.insert(D);
156 last->eraseFromParent();
165 // Handle frees whose dependencies are non-trivial.
166 if (FreeInst* F = dyn_cast<FreeInst>(BBI)) {
168 MadeChange |= handleFreeWithNonTrivialDependency(F,
171 // No known stores after the free
174 // Update our most-recent-store map.
175 last = cast<StoreInst>(BBI);
179 // If this block ends in a return, unwind, unreachable, and eventually
180 // tailcall, then all allocas are dead at its end.
181 if (BB.getTerminator()->getNumSuccessors() == 0)
182 MadeChange |= handleEndBlock(BB, possiblyDead);
185 while (!possiblyDead.empty()) {
186 Instruction *I = possiblyDead.back();
187 possiblyDead.pop_back();
188 DeleteDeadInstructionChains(I, possiblyDead);
194 /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
195 /// dependency is a store to a field of that structure
196 bool DSE::handleFreeWithNonTrivialDependency(FreeInst* F, Instruction* dep,
197 SetVector<Instruction*>& possiblyDead) {
198 TargetData &TD = getAnalysis<TargetData>();
199 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
200 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
202 if (dep == MemoryDependenceAnalysis::None ||
203 dep == MemoryDependenceAnalysis::NonLocal)
206 StoreInst* dependency = dyn_cast<StoreInst>(dep);
209 else if (dependency->isVolatile())
212 Value* depPointer = dependency->getPointerOperand();
213 const Type* depType = dependency->getOperand(0)->getType();
214 unsigned depPointerSize = TD.getTypeStoreSize(depType);
216 // Check for aliasing
217 AliasAnalysis::AliasResult A = AA.alias(F->getPointerOperand(), ~0U,
218 depPointer, depPointerSize);
220 if (A == AliasAnalysis::MustAlias) {
222 MD.removeInstruction(dependency);
224 // DCE instructions only used to calculate that store
225 if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(0)))
226 possiblyDead.insert(D);
227 if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(1)))
228 possiblyDead.insert(D);
230 dependency->eraseFromParent();
238 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
239 /// function end block. Ex:
242 /// store i32 1, i32* %A
244 bool DSE::handleEndBlock(BasicBlock& BB,
245 SetVector<Instruction*>& possiblyDead) {
246 TargetData &TD = getAnalysis<TargetData>();
247 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
248 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
250 bool MadeChange = false;
252 // Pointers alloca'd in this function are dead in the end block
253 SmallPtrSet<Value*, 64> deadPointers;
255 // Find all of the alloca'd pointers in the entry block
256 BasicBlock *Entry = BB.getParent()->begin();
257 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
258 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
259 deadPointers.insert(AI);
260 for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
261 AE = BB.getParent()->arg_end(); AI != AE; ++AI)
262 if (AI->hasByValAttr())
263 deadPointers.insert(AI);
265 // Scan the basic block backwards
266 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
269 // If we find a store whose pointer is dead...
270 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
271 if (!S->isVolatile()) {
272 Value* pointerOperand = S->getPointerOperand();
273 // See through pointer-to-pointer bitcasts
274 TranslatePointerBitCasts(pointerOperand);
276 // Alloca'd pointers or byval arguments (which are functionally like
277 // alloca's) are valid candidates for removal.
278 if (deadPointers.count(pointerOperand)) {
280 MD.removeInstruction(S);
282 // DCE instructions only used to calculate that store
283 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
284 possiblyDead.insert(D);
285 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
286 possiblyDead.insert(D);
289 S->eraseFromParent();
297 // We can also remove memcpy's to local variables at the end of a function
298 } else if (MemCpyInst* M = dyn_cast<MemCpyInst>(BBI)) {
299 Value* dest = M->getDest();
300 TranslatePointerBitCasts(dest);
302 if (deadPointers.count(dest)) {
303 MD.removeInstruction(M);
305 // DCE instructions only used to calculate that memcpy
306 if (Instruction* D = dyn_cast<Instruction>(M->getRawSource()))
307 possiblyDead.insert(D);
308 if (Instruction* D = dyn_cast<Instruction>(M->getLength()))
309 possiblyDead.insert(D);
310 if (Instruction* D = dyn_cast<Instruction>(M->getRawDest()))
311 possiblyDead.insert(D);
314 M->eraseFromParent();
321 // Because a memcpy is also a load, we can't skip it if we didn't remove it
324 Value* killPointer = 0;
326 // If we encounter a use of the pointer, it is no longer considered dead
327 if (LoadInst* L = dyn_cast<LoadInst>(BBI)) {
328 // However, if this load is unused, we can go ahead and remove it, and
329 // not have to worry about it making our pointer undead!
330 if (L->use_empty()) {
331 MD.removeInstruction(L);
333 // DCE instructions only used to calculate that load
334 if (Instruction* D = dyn_cast<Instruction>(L->getPointerOperand()))
335 possiblyDead.insert(D);
338 L->eraseFromParent();
341 possiblyDead.remove(L);
346 killPointer = L->getPointerOperand();
347 } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
348 killPointer = V->getOperand(0);
349 } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
350 deadPointers.erase(A);
352 // Dead alloca's can be DCE'd when we reach them
353 if (A->use_empty()) {
354 MD.removeInstruction(A);
356 // DCE instructions only used to calculate that load
357 if (Instruction* D = dyn_cast<Instruction>(A->getArraySize()))
358 possiblyDead.insert(D);
361 A->eraseFromParent();
364 possiblyDead.remove(A);
368 } else if (CallSite::get(BBI).getInstruction() != 0) {
369 // If this call does not access memory, it can't
370 // be undeadifying any of our pointers.
371 CallSite CS = CallSite::get(BBI);
372 if (AA.doesNotAccessMemory(CS))
378 // Remove any pointers made undead by the call from the dead set
379 std::vector<Value*> dead;
380 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
381 E = deadPointers.end(); I != E; ++I) {
382 // HACK: if we detect that our AA is imprecise, it's not
383 // worth it to scan the rest of the deadPointers set. Just
384 // assume that the AA will return ModRef for everything, and
385 // go ahead and bail.
386 if (modRef >= 16 && other == 0) {
387 deadPointers.clear();
391 // Get size information for the alloca
392 unsigned pointerSize = ~0U;
393 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
394 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
395 pointerSize = C->getZExtValue() * \
396 TD.getABITypeSize(A->getAllocatedType());
398 const PointerType* PT = cast<PointerType>(
399 cast<Argument>(*I)->getType());
400 pointerSize = TD.getABITypeSize(PT->getElementType());
403 // See if the call site touches it
404 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
406 if (A == AliasAnalysis::ModRef)
411 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
415 for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
417 deadPointers.erase(*I);
421 // For any non-memory-affecting non-terminators, DCE them as we reach them
422 Instruction *CI = BBI;
423 if (!CI->isTerminator() && CI->use_empty() && !isa<FreeInst>(CI)) {
425 // DCE instructions only used to calculate that load
426 for (Instruction::op_iterator OI = CI->op_begin(), OE = CI->op_end();
428 if (Instruction* D = dyn_cast<Instruction>(OI))
429 possiblyDead.insert(D);
432 CI->eraseFromParent();
435 possiblyDead.remove(CI);
444 TranslatePointerBitCasts(killPointer);
446 // Deal with undead pointers
447 MadeChange |= RemoveUndeadPointers(killPointer, BBI,
448 deadPointers, possiblyDead);
454 /// RemoveUndeadPointers - check for uses of a pointer that make it
455 /// undead when scanning for dead stores to alloca's.
456 bool DSE::RemoveUndeadPointers(Value* killPointer,
457 BasicBlock::iterator& BBI,
458 SmallPtrSet<Value*, 64>& deadPointers,
459 SetVector<Instruction*>& possiblyDead) {
460 TargetData &TD = getAnalysis<TargetData>();
461 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
462 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
464 // If the kill pointer can be easily reduced to an alloca,
465 // don't bother doing extraneous AA queries
466 if (deadPointers.count(killPointer)) {
467 deadPointers.erase(killPointer);
469 } else if (isa<GlobalValue>(killPointer)) {
470 // A global can't be in the dead pointer set
474 bool MadeChange = false;
476 std::vector<Value*> undead;
478 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
479 E = deadPointers.end(); I != E; ++I) {
480 // Get size information for the alloca
481 unsigned pointerSize = ~0U;
482 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
483 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
484 pointerSize = C->getZExtValue() * \
485 TD.getABITypeSize(A->getAllocatedType());
487 const PointerType* PT = cast<PointerType>(
488 cast<Argument>(*I)->getType());
489 pointerSize = TD.getABITypeSize(PT->getElementType());
492 // See if this pointer could alias it
493 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
496 // If it must-alias and a store, we can delete it
497 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
498 StoreInst* S = cast<StoreInst>(BBI);
501 MD.removeInstruction(S);
503 // DCE instructions only used to calculate that store
504 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
505 possiblyDead.insert(D);
506 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
507 possiblyDead.insert(D);
510 S->eraseFromParent();
516 // Otherwise, it is undead
517 } else if (A != AliasAnalysis::NoAlias)
518 undead.push_back(*I);
521 for (std::vector<Value*>::iterator I = undead.begin(), E = undead.end();
523 deadPointers.erase(*I);
528 /// DeleteDeadInstructionChains - takes an instruction and a setvector of
529 /// dead instructions. If I is dead, it is erased, and its operands are
530 /// checked for deadness. If they are dead, they are added to the dead
532 void DSE::DeleteDeadInstructionChains(Instruction *I,
533 SetVector<Instruction*> &DeadInsts) {
534 // Instruction must be dead.
535 if (!I->use_empty() || !isInstructionTriviallyDead(I)) return;
537 // Let the memory dependence know
538 getAnalysis<MemoryDependenceAnalysis>().removeInstruction(I);
540 // See if this made any operands dead. We do it this way in case the
541 // instruction uses the same operand twice. We don't want to delete a
542 // value then reference it.
543 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
544 if (I->getOperand(i)->hasOneUse())
545 if (Instruction* Op = dyn_cast<Instruction>(I->getOperand(i)))
546 DeadInsts.insert(Op); // Attempt to nuke it later.
548 I->setOperand(i, 0); // Drop from the operand list.
551 I->eraseFromParent();