1 //===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Owen Anderson and is distributed under
6 // the University of Illinois Open Source 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/Pass.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/Transforms/Utils/Local.h"
31 #include "llvm/Support/Compiler.h"
34 STATISTIC(NumFastStores, "Number of stores deleted");
35 STATISTIC(NumFastOther , "Number of other instrs removed");
38 struct VISIBILITY_HIDDEN DSE : public FunctionPass {
39 static char ID; // Pass identification, replacement for typeid
40 DSE() : FunctionPass((intptr_t)&ID) {}
42 virtual bool runOnFunction(Function &F) {
44 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
45 Changed |= runOnBasicBlock(*I);
49 bool runOnBasicBlock(BasicBlock &BB);
50 bool handleFreeWithNonTrivialDependency(FreeInst* F,
51 Instruction* dependency,
52 SetVector<Instruction*>& possiblyDead);
53 bool handleEndBlock(BasicBlock& BB, SetVector<Instruction*>& possiblyDead);
54 bool RemoveUndeadPointers(Value* pointer,
55 BasicBlock::iterator& BBI,
56 SmallPtrSet<AllocaInst*, 64>& deadPointers,
57 SetVector<Instruction*>& possiblyDead);
58 void DeleteDeadInstructionChains(Instruction *I,
59 SetVector<Instruction*> &DeadInsts);
61 /// Find the base pointer that a pointer came from
62 /// Because this is used to find pointers that originate
63 /// from allocas, it is safe to ignore GEP indices, since
64 /// either the store will be in the alloca, and thus dead,
65 /// or beyond the end of the alloca, and thus undefined.
66 void TranslatePointerBitCasts(Value*& v, bool zeroGepsOnly = false) {
67 assert(isa<PointerType>(v->getType()) &&
68 "Translating a non-pointer type?");
70 if (BitCastInst* C = dyn_cast<BitCastInst>(v))
72 else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(v))
73 if (!zeroGepsOnly || G->hasAllZeroIndices()) {
83 // getAnalysisUsage - We require post dominance frontiers (aka Control
85 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
87 AU.addRequired<TargetData>();
88 AU.addRequired<AliasAnalysis>();
89 AU.addRequired<MemoryDependenceAnalysis>();
90 AU.addPreserved<AliasAnalysis>();
91 AU.addPreserved<MemoryDependenceAnalysis>();
95 RegisterPass<DSE> X("dse", "Dead Store Elimination");
98 FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
100 bool DSE::runOnBasicBlock(BasicBlock &BB) {
101 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
102 TargetData &TD = getAnalysis<TargetData>();
104 // Record the last-seen store to this pointer
105 DenseMap<Value*, StoreInst*> lastStore;
106 // Record instructions possibly made dead by deleting a store
107 SetVector<Instruction*> possiblyDead;
109 bool MadeChange = false;
111 // Do a top-down walk on the BB
112 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end();
114 // If we find a store or a free...
115 if (!isa<StoreInst>(BBI) && !isa<FreeInst>(BBI))
119 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
120 if (!S->isVolatile())
121 pointer = S->getPointerOperand();
125 pointer = cast<FreeInst>(BBI)->getPointerOperand();
127 TranslatePointerBitCasts(pointer, true);
128 StoreInst*& last = lastStore[pointer];
129 bool deletedStore = false;
131 // ... to a pointer that has been stored to before...
133 Instruction* dep = MD.getDependency(BBI);
135 // ... and no other memory dependencies are between them....
136 while (dep != MemoryDependenceAnalysis::None &&
137 dep != MemoryDependenceAnalysis::NonLocal &&
138 isa<StoreInst>(dep)) {
140 TD.getTypeStoreSize(last->getOperand(0)->getType()) >
141 TD.getTypeStoreSize(BBI->getOperand(0)->getType())) {
142 dep = MD.getDependency(BBI, dep);
147 MD.removeInstruction(last);
149 // DCE instructions only used to calculate that store
150 if (Instruction* D = dyn_cast<Instruction>(last->getOperand(0)))
151 possiblyDead.insert(D);
152 if (Instruction* D = dyn_cast<Instruction>(last->getOperand(1)))
153 possiblyDead.insert(D);
155 last->eraseFromParent();
164 // Handle frees whose dependencies are non-trivial.
165 if (FreeInst* F = dyn_cast<FreeInst>(BBI)) {
167 MadeChange |= handleFreeWithNonTrivialDependency(F,
170 // No known stores after the free
173 // Update our most-recent-store map.
174 last = cast<StoreInst>(BBI);
178 // If this block ends in a return, unwind, unreachable, and eventually
179 // tailcall, then all allocas are dead at its end.
180 if (BB.getTerminator()->getNumSuccessors() == 0)
181 MadeChange |= handleEndBlock(BB, possiblyDead);
184 while (!possiblyDead.empty()) {
185 Instruction *I = possiblyDead.back();
186 possiblyDead.pop_back();
187 DeleteDeadInstructionChains(I, possiblyDead);
193 /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
194 /// dependency is a store to a field of that structure
195 bool DSE::handleFreeWithNonTrivialDependency(FreeInst* F, Instruction* dep,
196 SetVector<Instruction*>& possiblyDead) {
197 TargetData &TD = getAnalysis<TargetData>();
198 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
199 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
201 if (dep == MemoryDependenceAnalysis::None ||
202 dep == MemoryDependenceAnalysis::NonLocal)
205 StoreInst* dependency = dyn_cast<StoreInst>(dep);
208 else if (dependency->isVolatile())
211 Value* depPointer = dependency->getPointerOperand();
212 const Type* depType = dependency->getOperand(0)->getType();
213 unsigned depPointerSize = TD.getTypeStoreSize(depType);
215 // Check for aliasing
216 AliasAnalysis::AliasResult A = AA.alias(F->getPointerOperand(), ~0UL,
217 depPointer, depPointerSize);
219 if (A == AliasAnalysis::MustAlias) {
221 MD.removeInstruction(dependency);
223 // DCE instructions only used to calculate that store
224 if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(0)))
225 possiblyDead.insert(D);
226 if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(1)))
227 possiblyDead.insert(D);
229 dependency->eraseFromParent();
237 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
238 /// function end block. Ex:
241 /// store i32 1, i32* %A
243 bool DSE::handleEndBlock(BasicBlock& BB,
244 SetVector<Instruction*>& possiblyDead) {
245 TargetData &TD = getAnalysis<TargetData>();
246 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
247 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
249 bool MadeChange = false;
251 // Pointers alloca'd in this function are dead in the end block
252 SmallPtrSet<AllocaInst*, 64> deadPointers;
254 // Find all of the alloca'd pointers in the entry block
255 BasicBlock *Entry = BB.getParent()->begin();
256 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
257 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
258 deadPointers.insert(AI);
260 // Scan the basic block backwards
261 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
264 if (deadPointers.empty())
267 // If we find a store whose pointer is dead...
268 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
269 if (!S->isVolatile()) {
270 Value* pointerOperand = S->getPointerOperand();
271 // See through pointer-to-pointer bitcasts
272 TranslatePointerBitCasts(pointerOperand);
274 if (deadPointers.count(pointerOperand)){
276 MD.removeInstruction(S);
278 // DCE instructions only used to calculate that store
279 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
280 possiblyDead.insert(D);
281 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
282 possiblyDead.insert(D);
285 S->eraseFromParent();
294 Value* killPointer = 0;
296 // If we encounter a use of the pointer, it is no longer considered dead
297 if (LoadInst* L = dyn_cast<LoadInst>(BBI)) {
298 killPointer = L->getPointerOperand();
299 } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
300 killPointer = V->getOperand(0);
301 } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
302 deadPointers.erase(A);
304 } else if (CallSite::get(BBI).getInstruction() != 0) {
305 // If this call does not access memory, it can't
306 // be undeadifying any of our pointers.
307 CallSite CS = CallSite::get(BBI);
308 if (CS.getCalledFunction() &&
309 AA.doesNotAccessMemory(CS.getCalledFunction()))
315 // Remove any pointers made undead by the call from the dead set
316 std::vector<Instruction*> dead;
317 for (SmallPtrSet<AllocaInst*, 64>::iterator I = deadPointers.begin(),
318 E = deadPointers.end(); I != E; ++I) {
319 // HACK: if we detect that our AA is imprecise, it's not
320 // worth it to scan the rest of the deadPointers set. Just
321 // assume that the AA will return ModRef for everything, and
322 // go ahead and bail.
323 if (modRef >= 16 && other == 0) {
324 deadPointers.clear();
328 // Get size information for the alloca
329 unsigned pointerSize = ~0UL;
330 if (ConstantInt* C = dyn_cast<ConstantInt>((*I)->getArraySize()))
331 pointerSize = C->getZExtValue() * \
332 TD.getABITypeSize((*I)->getAllocatedType());
334 // See if the call site touches it
335 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
337 if (A == AliasAnalysis::ModRef)
342 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
346 for (std::vector<Instruction*>::iterator I = dead.begin(), E = dead.end();
348 deadPointers.erase(*I);
356 TranslatePointerBitCasts(killPointer);
358 // Deal with undead pointers
359 MadeChange |= RemoveUndeadPointers(killPointer, BBI,
360 deadPointers, possiblyDead);
366 /// RemoveUndeadPointers - check for uses of a pointer that make it
367 /// undead when scanning for dead stores to alloca's.
368 bool DSE::RemoveUndeadPointers(Value* killPointer,
369 BasicBlock::iterator& BBI,
370 SmallPtrSet<AllocaInst*, 64>& deadPointers,
371 SetVector<Instruction*>& possiblyDead) {
372 TargetData &TD = getAnalysis<TargetData>();
373 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
374 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
376 // If the kill pointer can be easily reduced to an alloca,
377 // don't bother doing extraneous AA queries
378 if (AllocaInst* A = dyn_cast<AllocaInst>(killPointer)) {
379 if (deadPointers.count(A))
380 deadPointers.erase(A);
382 } else if (isa<GlobalValue>(killPointer)) {
383 // A global can't be in the dead pointer set
387 bool MadeChange = false;
389 std::vector<Instruction*> undead;
391 for (SmallPtrSet<AllocaInst*, 64>::iterator I = deadPointers.begin(),
392 E = deadPointers.end(); I != E; ++I) {
393 // Get size information for the alloca
394 unsigned pointerSize = ~0UL;
395 if (ConstantInt* C = dyn_cast<ConstantInt>((*I)->getArraySize()))
396 pointerSize = C->getZExtValue() * \
397 TD.getABITypeSize((*I)->getAllocatedType());
399 // See if this pointer could alias it
400 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
403 // If it must-alias and a store, we can delete it
404 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
405 StoreInst* S = cast<StoreInst>(BBI);
408 MD.removeInstruction(S);
410 // DCE instructions only used to calculate that store
411 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
412 possiblyDead.insert(D);
413 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
414 possiblyDead.insert(D);
417 S->eraseFromParent();
423 // Otherwise, it is undead
424 } else if (A != AliasAnalysis::NoAlias)
425 undead.push_back(*I);
428 for (std::vector<Instruction*>::iterator I = undead.begin(), E = undead.end();
430 deadPointers.erase(*I);
435 /// DeleteDeadInstructionChains - takes an instruction and a setvector of
436 /// dead instructions. If I is dead, it is erased, and its operands are
437 /// checked for deadness. If they are dead, they are added to the dead
439 void DSE::DeleteDeadInstructionChains(Instruction *I,
440 SetVector<Instruction*> &DeadInsts) {
441 // Instruction must be dead.
442 if (!I->use_empty() || !isInstructionTriviallyDead(I)) return;
444 // Let the memory dependence know
445 getAnalysis<MemoryDependenceAnalysis>().removeInstruction(I);
447 // See if this made any operands dead. We do it this way in case the
448 // instruction uses the same operand twice. We don't want to delete a
449 // value then reference it.
450 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
451 if (I->getOperand(i)->hasOneUse())
452 if (Instruction* Op = dyn_cast<Instruction>(I->getOperand(i)))
453 DeadInsts.insert(Op); // Attempt to nuke it later.
455 I->setOperand(i, 0); // Drop from the operand list.
458 I->eraseFromParent();