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/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"
35 STATISTIC(NumFastStores, "Number of stores deleted");
36 STATISTIC(NumFastOther , "Number of other instrs removed");
39 struct DSE : public FunctionPass {
42 static char ID; // Pass identification, replacement for typeid
43 DSE() : FunctionPass(&ID) {}
45 virtual bool runOnFunction(Function &F) {
47 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
48 Changed |= runOnBasicBlock(*I);
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);
62 // getAnalysisUsage - We require post dominance frontiers (aka Control
64 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
66 AU.addRequired<DominatorTree>();
67 AU.addRequired<AliasAnalysis>();
68 AU.addRequired<MemoryDependenceAnalysis>();
69 AU.addPreserved<DominatorTree>();
70 AU.addPreserved<AliasAnalysis>();
71 AU.addPreserved<MemoryDependenceAnalysis>();
77 static RegisterPass<DSE> X("dse", "Dead Store Elimination");
79 FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
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.
84 static bool isValueAtLeastAsBigAs(Value *V1, Value *V2, const TargetData *TD) {
85 const Type *V1Ty = V1->getType(), *V2Ty = V2->getType();
87 // Exactly the same type, must have exactly the same size.
88 if (V1Ty == V2Ty) return true;
90 // If we don't have target data, we don't know.
91 if (TD == 0) return false;
93 return TD->getTypeStoreSize(V1Ty) >= TD->getTypeStoreSize(V2Ty);
96 bool DSE::runOnBasicBlock(BasicBlock &BB) {
97 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
98 TD = getAnalysisIfAvailable<TargetData>();
100 bool MadeChange = false;
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++;
106 // If we find a store or a free, get its memory dependence.
107 if (!isa<StoreInst>(Inst) && !isFreeCall(Inst))
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())
115 MemDepResult InstDep = MD.getDependency(Inst);
117 // Ignore non-local stores.
118 // FIXME: cross-block DSE would be fun. :)
119 if (InstDep.isNonLocal()) continue;
121 // Handle frees whose dependencies are non-trivial.
122 if (isFreeCall(Inst)) {
123 MadeChange |= handleFreeWithNonTrivialDependency(Inst, InstDep);
127 StoreInst *SI = cast<StoreInst>(Inst);
129 // If not a definite must-alias dependency, ignore it.
130 if (!InstDep.isDef())
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);
142 // DeleteDeadInstruction can delete the current instruction in loop
145 if (BBI != BB.begin())
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);
159 DeleteDeadInstruction(SI);
161 if (NextInst == 0) // Next instruction deleted.
163 else if (BBI != BB.begin()) // Revisit this instruction if possible.
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);
179 DeleteDeadInstruction(SI);
181 if (NextInst == 0) // Next instruction deleted.
183 else if (BBI != BB.begin()) // Revisit this instruction if possible.
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);
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>();
205 StoreInst *Dependency = dyn_cast_or_null<StoreInst>(Dep.getInst());
206 if (!Dependency || Dependency->isVolatile())
209 Value *DepPointer = Dependency->getPointerOperand()->getUnderlyingObject();
211 // Check for aliasing.
212 if (AA.alias(F->getOperand(1), 1, DepPointer, 1) !=
213 AliasAnalysis::MustAlias)
216 // DCE instructions only used to calculate that store
217 DeleteDeadInstruction(Dependency);
222 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
223 /// function end block. Ex:
226 /// store i32 1, i32* %A
228 bool DSE::handleEndBlock(BasicBlock &BB) {
229 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
231 bool MadeChange = false;
233 // Pointers alloca'd in this function are dead in the end block
234 SmallPtrSet<Value*, 64> deadPointers;
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);
242 // Treat byval arguments the same, stores to them are dead at the end of the
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);
249 // Scan the basic block backwards
250 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
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();
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.
264 DeleteDeadInstruction(S, &deadPointers);
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();
277 if (deadPointers.count(dest)) {
279 DeleteDeadInstruction(M, &deadPointers);
285 // Because a memcpy is also a load, we can't skip it if we didn't remove
289 Value* killPointer = 0;
290 uint64_t killPointerSize = ~0UL;
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()) {
298 DeleteDeadInstruction(L, &deadPointers);
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);
315 // Dead alloca's can be DCE'd when we reach them
316 if (A->use_empty()) {
318 DeleteDeadInstruction(A, &deadPointers);
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))
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();
347 // Get size information for the alloca
348 unsigned pointerSize = ~0U;
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());
355 const PointerType* PT = cast<PointerType>(
356 cast<Argument>(*I)->getType());
357 pointerSize = TD->getTypeAllocSize(PT->getElementType());
361 // See if the call site touches it
362 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
364 if (A == AliasAnalysis::ModRef)
369 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
373 for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
375 deadPointers.erase(*I);
378 } else if (isInstructionTriviallyDead(BBI)) {
379 // For any non-memory-affecting non-terminators, DCE them as we reach them
380 Instruction *Inst = BBI;
382 DeleteDeadInstruction(Inst, &deadPointers);
391 killPointer = killPointer->getUnderlyingObject();
393 // Deal with undead pointers
394 MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
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>();
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);
415 // A global can't be in the dead pointer set.
416 if (isa<GlobalValue>(killPointer))
419 bool MadeChange = false;
421 SmallVector<Value*, 16> undead;
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;
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());
433 const PointerType* PT = cast<PointerType>(cast<Argument>(*I)->getType());
434 pointerSize = TD->getTypeAllocSize(PT->getElementType());
438 // See if this pointer could alias it
439 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
440 killPointer, killPointerSize);
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);
448 DeleteDeadInstruction(S, &deadPointers);
454 // Otherwise, it is undead
455 } else if (A != AliasAnalysis::NoAlias)
456 undead.push_back(*I);
459 for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
461 deadPointers.erase(*I);
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.
470 /// If ValueSet is non-null, remove any deleted instructions from it as well.
472 void DSE::DeleteDeadInstruction(Instruction *I,
473 SmallPtrSet<Value*, 64> *ValueSet) {
474 SmallVector<Instruction*, 32> NowDeadInsts;
476 NowDeadInsts.push_back(I);
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();
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
490 MDA.removeInstruction(DeadInst);
492 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
493 Value *Op = DeadInst->getOperand(op);
494 DeadInst->setOperand(op, 0);
496 // If this operand just became dead, add it to the NowDeadInsts list.
497 if (!Op->use_empty()) continue;
499 if (Instruction *OpI = dyn_cast<Instruction>(Op))
500 if (isInstructionTriviallyDead(OpI))
501 NowDeadInsts.push_back(OpI);
504 DeadInst->eraseFromParent();
506 if (ValueSet) ValueSet->erase(DeadInst);