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, 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);
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>();
98 static RegisterPass<DSE> X("dse", "Dead Store Elimination");
100 FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
102 bool DSE::runOnBasicBlock(BasicBlock &BB) {
103 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
104 TargetData &TD = getAnalysis<TargetData>();
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;
111 bool MadeChange = false;
113 // Do a top-down walk on the BB
114 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end();
116 // If we find a store or a free...
117 if (!isa<StoreInst>(BBI) && !isa<FreeInst>(BBI))
121 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
122 if (!S->isVolatile())
123 pointer = S->getPointerOperand();
127 pointer = cast<FreeInst>(BBI)->getPointerOperand();
129 TranslatePointerBitCasts(pointer, true);
130 StoreInst*& last = lastStore[pointer];
131 bool deletedStore = false;
133 // ... to a pointer that has been stored to before...
135 Instruction* dep = MD.getDependency(BBI);
137 // ... and no other memory dependencies are between them....
138 while (dep != MemoryDependenceAnalysis::None &&
139 dep != MemoryDependenceAnalysis::NonLocal &&
140 isa<StoreInst>(dep)) {
142 TD.getTypeStoreSize(last->getOperand(0)->getType()) >
143 TD.getTypeStoreSize(BBI->getOperand(0)->getType())) {
144 dep = MD.getDependency(BBI, dep);
149 MD.removeInstruction(last);
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);
157 last->eraseFromParent();
166 // Handle frees whose dependencies are non-trivial.
167 if (FreeInst* F = dyn_cast<FreeInst>(BBI)) {
169 MadeChange |= handleFreeWithNonTrivialDependency(F,
172 // No known stores after the free
175 // Update our most-recent-store map.
176 last = cast<StoreInst>(BBI);
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);
186 while (!possiblyDead.empty()) {
187 Instruction *I = possiblyDead.back();
188 possiblyDead.pop_back();
189 DeleteDeadInstructionChains(I, possiblyDead);
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>();
203 if (dep == MemoryDependenceAnalysis::None ||
204 dep == MemoryDependenceAnalysis::NonLocal)
207 StoreInst* dependency = dyn_cast<StoreInst>(dep);
210 else if (dependency->isVolatile())
213 Value* depPointer = dependency->getPointerOperand();
214 const Type* depType = dependency->getOperand(0)->getType();
215 unsigned depPointerSize = TD.getTypeStoreSize(depType);
217 // Check for aliasing
218 AliasAnalysis::AliasResult A = AA.alias(F->getPointerOperand(), ~0U,
219 depPointer, depPointerSize);
221 if (A == AliasAnalysis::MustAlias) {
223 MD.removeInstruction(dependency);
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);
231 dependency->eraseFromParent();
239 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
240 /// function end block. Ex:
243 /// store i32 1, i32* %A
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>();
251 bool MadeChange = false;
253 // Pointers alloca'd in this function are dead in the end block
254 SmallPtrSet<Value*, 64> deadPointers;
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);
266 // Scan the basic block backwards
267 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
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);
277 // Alloca'd pointers or byval arguments (which are functionally like
278 // alloca's) are valid candidates for removal.
279 if (deadPointers.count(pointerOperand)) {
281 MD.removeInstruction(S);
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);
290 S->eraseFromParent();
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);
303 if (deadPointers.count(dest)) {
304 MD.removeInstruction(M);
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);
315 M->eraseFromParent();
322 // Because a memcpy is also a load, we can't skip it if we didn't remove it
325 Value* killPointer = 0;
326 uint64_t killPointerSize = ~0UL;
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);
335 // DCE instructions only used to calculate that load
336 if (Instruction* D = dyn_cast<Instruction>(L->getPointerOperand()))
337 possiblyDead.insert(D);
340 L->eraseFromParent();
343 possiblyDead.remove(L);
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);
359 // Dead alloca's can be DCE'd when we reach them
360 if (A->use_empty()) {
361 MD.removeInstruction(A);
363 // DCE instructions only used to calculate that load
364 if (Instruction* D = dyn_cast<Instruction>(A->getArraySize()))
365 possiblyDead.insert(D);
368 A->eraseFromParent();
371 possiblyDead.remove(A);
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))
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();
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());
405 const PointerType* PT = cast<PointerType>(
406 cast<Argument>(*I)->getType());
407 pointerSize = TD.getABITypeSize(PT->getElementType());
410 // See if the call site touches it
411 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
413 if (A == AliasAnalysis::ModRef)
418 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
422 for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
424 deadPointers.erase(*I);
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)) {
432 // DCE instructions only used to calculate that load
433 for (Instruction::op_iterator OI = CI->op_begin(), OE = CI->op_end();
435 if (Instruction* D = dyn_cast<Instruction>(OI))
436 possiblyDead.insert(D);
439 CI->eraseFromParent();
442 possiblyDead.remove(CI);
451 TranslatePointerBitCasts(killPointer);
453 // Deal with undead pointers
454 MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
455 deadPointers, possiblyDead);
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>();
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);
476 } else if (isa<GlobalValue>(killPointer)) {
477 // A global can't be in the dead pointer set
481 bool MadeChange = false;
483 std::vector<Value*> undead;
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());
494 const PointerType* PT = cast<PointerType>(
495 cast<Argument>(*I)->getType());
496 pointerSize = TD.getABITypeSize(PT->getElementType());
499 // See if this pointer could alias it
500 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
501 killPointer, killPointerSize);
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);
508 MD.removeInstruction(S);
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);
517 S->eraseFromParent();
523 // Otherwise, it is undead
524 } else if (A != AliasAnalysis::NoAlias)
525 undead.push_back(*I);
528 for (std::vector<Value*>::iterator I = undead.begin(), E = undead.end();
530 deadPointers.erase(*I);
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
539 void DSE::DeleteDeadInstructionChains(Instruction *I,
540 SetVector<Instruction*> &DeadInsts) {
541 // Instruction must be dead.
542 if (!I->use_empty() || !isInstructionTriviallyDead(I)) return;
544 // Let the memory dependence know
545 getAnalysis<MemoryDependenceAnalysis>().removeInstruction(I);
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.
555 I->setOperand(i, 0); // Drop from the operand list.
558 I->eraseFromParent();