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 {
41 MemoryDependenceAnalysis *MD;
43 static char ID; // Pass identification, replacement for typeid
44 DSE() : FunctionPass(ID), AA(0), MD(0) {
45 initializeDSEPass(*PassRegistry::getPassRegistry());
48 virtual bool runOnFunction(Function &F) {
49 AA = &getAnalysis<AliasAnalysis>();
50 MD = &getAnalysis<MemoryDependenceAnalysis>();
51 DominatorTree &DT = getAnalysis<DominatorTree>();
54 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
55 // Only check non-dead blocks. Dead blocks may have strange pointer
56 // cycles that will confuse alias analysis.
57 if (DT.isReachableFromEntry(I))
58 Changed |= runOnBasicBlock(*I);
64 bool runOnBasicBlock(BasicBlock &BB);
65 bool HandleFree(CallInst *F);
66 bool handleEndBlock(BasicBlock &BB);
67 void RemoveAccessedObjects(const AliasAnalysis::Location &LoadedLoc,
68 SmallPtrSet<Value*, 16> &DeadStackObjects);
69 void DeleteDeadInstruction(Instruction *I,
70 SmallPtrSet<Value*, 16> *deadPointers = 0);
73 // getAnalysisUsage - We require post dominance frontiers (aka Control
75 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
77 AU.addRequired<DominatorTree>();
78 AU.addRequired<AliasAnalysis>();
79 AU.addRequired<MemoryDependenceAnalysis>();
80 AU.addPreserved<AliasAnalysis>();
81 AU.addPreserved<DominatorTree>();
82 AU.addPreserved<MemoryDependenceAnalysis>();
88 INITIALIZE_PASS_BEGIN(DSE, "dse", "Dead Store Elimination", false, false)
89 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
90 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
91 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
92 INITIALIZE_PASS_END(DSE, "dse", "Dead Store Elimination", false, false)
94 FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
96 /// hasMemoryWrite - Does this instruction write some memory? This only returns
97 /// true for things that we can analyze with other helpers below.
98 static bool hasMemoryWrite(Instruction *I) {
99 if (isa<StoreInst>(I))
101 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
102 switch (II->getIntrinsicID()) {
105 case Intrinsic::memset:
106 case Intrinsic::memmove:
107 case Intrinsic::memcpy:
108 case Intrinsic::init_trampoline:
109 case Intrinsic::lifetime_end:
116 /// getLocForWrite - Return a Location stored to by the specified instruction.
117 static AliasAnalysis::Location
118 getLocForWrite(Instruction *Inst, AliasAnalysis &AA) {
119 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
120 return AA.getLocation(SI);
122 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(Inst)) {
123 // memcpy/memmove/memset.
124 AliasAnalysis::Location Loc = AA.getLocationForDest(MI);
125 // If we don't have target data around, an unknown size in Location means
126 // that we should use the size of the pointee type. This isn't valid for
127 // memset/memcpy, which writes more than an i8.
128 if (Loc.Size == AliasAnalysis::UnknownSize && AA.getTargetData() == 0)
129 return AliasAnalysis::Location();
133 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst);
134 if (II == 0) return AliasAnalysis::Location();
136 switch (II->getIntrinsicID()) {
137 default: return AliasAnalysis::Location(); // Unhandled intrinsic.
138 case Intrinsic::init_trampoline:
139 // If we don't have target data around, an unknown size in Location means
140 // that we should use the size of the pointee type. This isn't valid for
141 // init.trampoline, which writes more than an i8.
142 if (AA.getTargetData() == 0) return AliasAnalysis::Location();
144 // FIXME: We don't know the size of the trampoline, so we can't really
146 return AliasAnalysis::Location(II->getArgOperand(0));
147 case Intrinsic::lifetime_end: {
148 uint64_t Len = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue();
149 return AliasAnalysis::Location(II->getArgOperand(1), Len);
154 /// isRemovable - If the value of this instruction and the memory it writes to
155 /// is unused, may we delete this instruction?
156 static bool isRemovable(Instruction *I) {
157 // Don't remove volatile stores.
158 if (StoreInst *SI = dyn_cast<StoreInst>(I))
159 return !SI->isVolatile();
161 IntrinsicInst *II = cast<IntrinsicInst>(I);
162 switch (II->getIntrinsicID()) {
163 default: assert(0 && "doesn't pass 'hasMemoryWrite' predicate");
164 case Intrinsic::lifetime_end:
165 // Never remove dead lifetime_end's, e.g. because it is followed by a
168 case Intrinsic::init_trampoline:
169 // Always safe to remove init_trampoline.
172 case Intrinsic::memset:
173 case Intrinsic::memmove:
174 case Intrinsic::memcpy:
175 // Don't remove volatile memory intrinsics.
176 return !cast<MemIntrinsic>(II)->isVolatile();
180 /// getPointerOperand - Return the pointer that is being written to.
181 static Value *getPointerOperand(Instruction *I) {
182 assert(hasMemoryWrite(I));
183 if (StoreInst *SI = dyn_cast<StoreInst>(I))
184 return SI->getPointerOperand();
185 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
186 return MI->getArgOperand(0);
188 IntrinsicInst *II = cast<IntrinsicInst>(I);
189 switch (II->getIntrinsicID()) {
190 default: assert(false && "Unexpected intrinsic!");
191 case Intrinsic::init_trampoline:
192 return II->getArgOperand(0);
193 case Intrinsic::lifetime_end:
194 return II->getArgOperand(1);
198 static uint64_t getPointerSize(Value *V, AliasAnalysis &AA) {
199 const TargetData *TD = AA.getTargetData();
201 return AliasAnalysis::UnknownSize;
203 if (AllocaInst *A = dyn_cast<AllocaInst>(V)) {
204 // Get size information for the alloca
205 if (ConstantInt *C = dyn_cast<ConstantInt>(A->getArraySize()))
206 return C->getZExtValue() * TD->getTypeAllocSize(A->getAllocatedType());
207 return AliasAnalysis::UnknownSize;
210 assert(isa<Argument>(V) && "Expected AllocaInst or Argument!");
211 const PointerType *PT = cast<PointerType>(V->getType());
212 return TD->getTypeAllocSize(PT->getElementType());
216 /// isCompleteOverwrite - Return true if a store to the 'Later' location
217 /// completely overwrites a store to the 'Earlier' location.
218 static bool isCompleteOverwrite(const AliasAnalysis::Location &Later,
219 const AliasAnalysis::Location &Earlier,
221 const Value *P1 = Later.Ptr->stripPointerCasts();
222 const Value *P2 = Earlier.Ptr->stripPointerCasts();
224 // Make sure that the start pointers are the same.
228 // If we don't know the sizes of either access, then we can't do a comparison.
229 if (Later.Size == AliasAnalysis::UnknownSize ||
230 Earlier.Size == AliasAnalysis::UnknownSize) {
231 // If we have no TargetData information around, then the size of the store
232 // is inferrable from the pointee type. If they are the same type, then we
233 // know that the store is safe.
234 if (AA.getTargetData() == 0)
235 return Later.Ptr->getType() == Earlier.Ptr->getType();
239 // Make sure that the Later size is >= the Earlier size.
240 if (Later.Size < Earlier.Size)
246 bool DSE::runOnBasicBlock(BasicBlock &BB) {
247 bool MadeChange = false;
249 // Do a top-down walk on the BB.
250 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
251 Instruction *Inst = BBI++;
253 // Handle 'free' calls specially.
254 if (CallInst *F = isFreeCall(Inst)) {
255 MadeChange |= HandleFree(F);
259 // If we find something that writes memory, get its memory dependence.
260 if (!hasMemoryWrite(Inst))
263 MemDepResult InstDep = MD->getDependency(Inst);
265 // Ignore non-local store liveness.
266 // FIXME: cross-block DSE would be fun. :)
267 if (InstDep.isNonLocal() ||
268 // Ignore self dependence, which happens in the entry block of the
270 InstDep.getInst() == Inst)
273 // If we're storing the same value back to a pointer that we just
274 // loaded from, then the store can be removed.
275 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
276 if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
277 if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
278 SI->getOperand(0) == DepLoad && !SI->isVolatile()) {
279 // DeleteDeadInstruction can delete the current instruction. Save BBI
280 // in case we need it.
281 WeakVH NextInst(BBI);
283 DeleteDeadInstruction(SI);
285 if (NextInst == 0) // Next instruction deleted.
287 else if (BBI != BB.begin()) // Revisit this instruction if possible.
296 // Figure out what location is being stored to.
297 AliasAnalysis::Location Loc = getLocForWrite(Inst, *AA);
299 // If we didn't get a useful location, fail.
303 while (!InstDep.isNonLocal()) {
304 // Get the memory clobbered by the instruction we depend on. MemDep will
305 // skip any instructions that 'Loc' clearly doesn't interact with. If we
306 // end up depending on a may- or must-aliased load, then we can't optimize
307 // away the store and we bail out. However, if we depend on on something
308 // that overwrites the memory location we *can* potentially optimize it.
310 // Find out what memory location the dependant instruction stores.
311 Instruction *DepWrite = InstDep.getInst();
312 AliasAnalysis::Location DepLoc = getLocForWrite(DepWrite, *AA);
313 // If we didn't get a useful location, or if it isn't a size, bail out.
317 // If we find a removable write that is completely obliterated by the
318 // store to 'Loc' then we can remove it.
319 if (isRemovable(DepWrite) && isCompleteOverwrite(Loc, DepLoc, *AA)) {
320 // Delete the store and now-dead instructions that feed it.
321 DeleteDeadInstruction(DepWrite);
325 // DeleteDeadInstruction can delete the current instruction in loop
328 if (BBI != BB.begin())
333 // If this is a may-aliased store that is clobbering the store value, we
334 // can keep searching past it for another must-aliased pointer that stores
335 // to the same location. For example, in:
339 // we can remove the first store to P even though we don't know if P and Q
341 if (DepWrite == &BB.front()) break;
343 // Can't look past this instruction if it might read 'Loc'.
344 if (AA->getModRefInfo(DepWrite, Loc) & AliasAnalysis::Ref)
347 InstDep = MD->getPointerDependencyFrom(Loc, false, DepWrite, &BB);
351 // If this block ends in a return, unwind, or unreachable, all allocas are
352 // dead at its end, which means stores to them are also dead.
353 if (BB.getTerminator()->getNumSuccessors() == 0)
354 MadeChange |= handleEndBlock(BB);
359 /// HandleFree - Handle frees of entire structures whose dependency is a store
360 /// to a field of that structure.
361 bool DSE::HandleFree(CallInst *F) {
362 MemDepResult Dep = MD->getDependency(F);
364 if (Dep.isNonLocal()) return false;
366 Instruction *Dependency = Dep.getInst();
367 if (!hasMemoryWrite(Dependency) || !isRemovable(Dependency))
370 Value *DepPointer = getPointerOperand(Dependency)->getUnderlyingObject();
372 // Check for aliasing.
373 if (AA->alias(F->getArgOperand(0), 1, DepPointer, 1) !=
374 AliasAnalysis::MustAlias)
377 // DCE instructions only used to calculate that store
378 DeleteDeadInstruction(Dependency);
381 // Inst's old Dependency is now deleted. Compute the next dependency,
382 // which may also be dead, as in
384 // s[1] = 0; // This has just been deleted.
386 Dep = MD->getDependency(F);
387 } while (!Dep.isNonLocal());
392 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
393 /// function end block. Ex:
396 /// store i32 1, i32* %A
398 bool DSE::handleEndBlock(BasicBlock &BB) {
399 bool MadeChange = false;
401 // Keep track of all of the stack objects that are dead at the end of the
403 SmallPtrSet<Value*, 16> DeadStackObjects;
405 // Find all of the alloca'd pointers in the entry block.
406 BasicBlock *Entry = BB.getParent()->begin();
407 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
408 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
409 DeadStackObjects.insert(AI);
411 // Treat byval arguments the same, stores to them are dead at the end of the
413 for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
414 AE = BB.getParent()->arg_end(); AI != AE; ++AI)
415 if (AI->hasByValAttr())
416 DeadStackObjects.insert(AI);
418 // Scan the basic block backwards
419 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
422 // If we find a store, check to see if it points into a dead stack value.
423 if (hasMemoryWrite(BBI) && isRemovable(BBI)) {
424 // See through pointer-to-pointer bitcasts
425 Value *Pointer = getPointerOperand(BBI)->getUnderlyingObject();
427 // Alloca'd pointers or byval arguments (which are functionally like
428 // alloca's) are valid candidates for removal.
429 if (DeadStackObjects.count(Pointer)) {
430 // DCE instructions only used to calculate that store.
431 Instruction *Dead = BBI++;
432 DeleteDeadInstruction(Dead, &DeadStackObjects);
439 // Remove any dead non-memory-mutating instructions.
440 if (isInstructionTriviallyDead(BBI)) {
441 Instruction *Inst = BBI++;
442 DeleteDeadInstruction(Inst, &DeadStackObjects);
448 if (AllocaInst *A = dyn_cast<AllocaInst>(BBI)) {
449 DeadStackObjects.erase(A);
453 if (CallSite CS = cast<Value>(BBI)) {
454 // If this call does not access memory, it can't be loading any of our
456 if (AA->doesNotAccessMemory(CS))
459 unsigned NumModRef = 0, NumOther = 0;
461 // If the call might load from any of our allocas, then any store above
463 SmallVector<Value*, 8> LiveAllocas;
464 for (SmallPtrSet<Value*, 16>::iterator I = DeadStackObjects.begin(),
465 E = DeadStackObjects.end(); I != E; ++I) {
466 // If we detect that our AA is imprecise, it's not worth it to scan the
467 // rest of the DeadPointers set. Just assume that the AA will return
468 // ModRef for everything, and go ahead and bail out.
469 if (NumModRef >= 16 && NumOther == 0)
472 // See if the call site touches it.
473 AliasAnalysis::ModRefResult A =
474 AA->getModRefInfo(CS, *I, getPointerSize(*I, *AA));
476 if (A == AliasAnalysis::ModRef)
481 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
482 LiveAllocas.push_back(*I);
485 for (SmallVector<Value*, 8>::iterator I = LiveAllocas.begin(),
486 E = LiveAllocas.end(); I != E; ++I)
487 DeadStackObjects.erase(*I);
489 // If all of the allocas were clobbered by the call then we're not going
490 // to find anything else to process.
491 if (DeadStackObjects.empty())
497 AliasAnalysis::Location LoadedLoc;
499 // If we encounter a use of the pointer, it is no longer considered dead
500 if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
501 LoadedLoc = AA->getLocation(L);
502 } else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
503 LoadedLoc = AA->getLocation(V);
504 } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(BBI)) {
505 LoadedLoc = AA->getLocationForSource(MTI);
507 // Not a loading instruction.
511 // Remove any allocas from the DeadPointer set that are loaded, as this
512 // makes any stores above the access live.
513 RemoveAccessedObjects(LoadedLoc, DeadStackObjects);
515 // If all of the allocas were clobbered by the access then we're not going
516 // to find anything else to process.
517 if (DeadStackObjects.empty())
524 /// RemoveAccessedObjects - Check to see if the specified location may alias any
525 /// of the stack objects in the DeadStackObjects set. If so, they become live
526 /// because the location is being loaded.
527 void DSE::RemoveAccessedObjects(const AliasAnalysis::Location &LoadedLoc,
528 SmallPtrSet<Value*, 16> &DeadStackObjects) {
529 const Value *UnderlyingPointer = LoadedLoc.Ptr->getUnderlyingObject();
531 // A constant can't be in the dead pointer set.
532 if (isa<Constant>(UnderlyingPointer))
535 // If the kill pointer can be easily reduced to an alloca, don't bother doing
536 // extraneous AA queries.
537 if (isa<AllocaInst>(UnderlyingPointer) || isa<Argument>(UnderlyingPointer)) {
538 DeadStackObjects.erase(const_cast<Value*>(UnderlyingPointer));
542 SmallVector<Value*, 16> NowLive;
543 for (SmallPtrSet<Value*, 16>::iterator I = DeadStackObjects.begin(),
544 E = DeadStackObjects.end(); I != E; ++I) {
545 // See if the loaded location could alias the stack location.
546 AliasAnalysis::Location StackLoc(*I, getPointerSize(*I, *AA));
547 if (!AA->isNoAlias(StackLoc, LoadedLoc))
548 NowLive.push_back(*I);
551 for (SmallVector<Value*, 16>::iterator I = NowLive.begin(), E = NowLive.end();
553 DeadStackObjects.erase(*I);
556 /// DeleteDeadInstruction - Delete this instruction. Before we do, go through
557 /// and zero out all the operands of this instruction. If any of them become
558 /// dead, delete them and the computation tree that feeds them.
560 /// If ValueSet is non-null, remove any deleted instructions from it as well.
562 void DSE::DeleteDeadInstruction(Instruction *I,
563 SmallPtrSet<Value*, 16> *ValueSet) {
564 SmallVector<Instruction*, 32> NowDeadInsts;
566 NowDeadInsts.push_back(I);
569 // Before we touch this instruction, remove it from memdep!
571 Instruction *DeadInst = NowDeadInsts.pop_back_val();
575 // This instruction is dead, zap it, in stages. Start by removing it from
576 // MemDep, which needs to know the operands and needs it to be in the
578 MD->removeInstruction(DeadInst);
580 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
581 Value *Op = DeadInst->getOperand(op);
582 DeadInst->setOperand(op, 0);
584 // If this operand just became dead, add it to the NowDeadInsts list.
585 if (!Op->use_empty()) continue;
587 if (Instruction *OpI = dyn_cast<Instruction>(Op))
588 if (isInstructionTriviallyDead(OpI))
589 NowDeadInsts.push_back(OpI);
592 DeadInst->eraseFromParent();
594 if (ValueSet) ValueSet->erase(DeadInst);
595 } while (!NowDeadInsts.empty());