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
) {
44 initializeDSEPass(*PassRegistry::getPassRegistry());
47 virtual bool runOnFunction(Function
&F
) {
50 DominatorTree
&DT
= getAnalysis
<DominatorTree
>();
52 for (Function::iterator I
= F
.begin(), E
= F
.end(); I
!= E
; ++I
)
53 // Only check non-dead blocks. Dead blocks may have strange pointer
54 // cycles that will confuse alias analysis.
55 if (DT
.isReachableFromEntry(I
))
56 Changed
|= runOnBasicBlock(*I
);
60 bool runOnBasicBlock(BasicBlock
&BB
);
61 bool handleFreeWithNonTrivialDependency(const CallInst
*F
,
63 bool handleEndBlock(BasicBlock
&BB
);
64 bool RemoveUndeadPointers(Value
*Ptr
, uint64_t killPointerSize
,
65 BasicBlock::iterator
&BBI
,
66 SmallPtrSet
<Value
*, 64> &deadPointers
);
67 void DeleteDeadInstruction(Instruction
*I
,
68 SmallPtrSet
<Value
*, 64> *deadPointers
= 0);
71 // getAnalysisUsage - We require post dominance frontiers (aka Control
73 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
75 AU
.addRequired
<DominatorTree
>();
76 AU
.addRequired
<AliasAnalysis
>();
77 AU
.addRequired
<MemoryDependenceAnalysis
>();
78 AU
.addPreserved
<DominatorTree
>();
79 AU
.addPreserved
<MemoryDependenceAnalysis
>();
82 uint64_t getPointerSize(Value
*V
) const;
87 INITIALIZE_PASS_BEGIN(DSE
, "dse", "Dead Store Elimination", false, false)
88 INITIALIZE_PASS_DEPENDENCY(DominatorTree
)
89 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis
)
90 INITIALIZE_AG_DEPENDENCY(AliasAnalysis
)
91 INITIALIZE_PASS_END(DSE
, "dse", "Dead Store Elimination", false, false)
93 FunctionPass
*llvm::createDeadStoreEliminationPass() { return new DSE(); }
95 /// doesClobberMemory - Does this instruction clobber (write without reading)
97 static bool doesClobberMemory(Instruction
*I
) {
98 if (isa
<StoreInst
>(I
))
100 if (IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(I
)) {
101 switch (II
->getIntrinsicID()) {
104 case Intrinsic::memset
:
105 case Intrinsic::memmove
:
106 case Intrinsic::memcpy
:
107 case Intrinsic::init_trampoline
:
108 case Intrinsic::lifetime_end
:
115 /// isElidable - If the value of this instruction and the memory it writes to is
116 /// unused, may we delete this instrtction?
117 static bool isElidable(Instruction
*I
) {
118 assert(doesClobberMemory(I
));
119 if (IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(I
))
120 return II
->getIntrinsicID() != Intrinsic::lifetime_end
;
121 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(I
))
122 return !SI
->isVolatile();
126 /// getPointerOperand - Return the pointer that is being clobbered.
127 static Value
*getPointerOperand(Instruction
*I
) {
128 assert(doesClobberMemory(I
));
129 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(I
))
130 return SI
->getPointerOperand();
131 if (MemIntrinsic
*MI
= dyn_cast
<MemIntrinsic
>(I
))
132 return MI
->getArgOperand(0);
134 IntrinsicInst
*II
= cast
<IntrinsicInst
>(I
);
135 switch (II
->getIntrinsicID()) {
136 default: assert(false && "Unexpected intrinsic!");
137 case Intrinsic::init_trampoline
:
138 return II
->getArgOperand(0);
139 case Intrinsic::lifetime_end
:
140 return II
->getArgOperand(1);
144 /// getStoreSize - Return the length in bytes of the write by the clobbering
145 /// instruction. If variable or unknown, returns AliasAnalysis::UnknownSize.
146 static uint64_t getStoreSize(Instruction
*I
, const TargetData
*TD
) {
147 assert(doesClobberMemory(I
));
148 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(I
)) {
149 if (!TD
) return AliasAnalysis::UnknownSize
;
150 return TD
->getTypeStoreSize(SI
->getOperand(0)->getType());
154 if (MemIntrinsic
*MI
= dyn_cast
<MemIntrinsic
>(I
)) {
155 Len
= MI
->getLength();
157 IntrinsicInst
*II
= cast
<IntrinsicInst
>(I
);
158 switch (II
->getIntrinsicID()) {
159 default: assert(false && "Unexpected intrinsic!");
160 case Intrinsic::init_trampoline
:
161 return AliasAnalysis::UnknownSize
;
162 case Intrinsic::lifetime_end
:
163 Len
= II
->getArgOperand(0);
167 if (ConstantInt
*LenCI
= dyn_cast
<ConstantInt
>(Len
))
168 if (!LenCI
->isAllOnesValue())
169 return LenCI
->getZExtValue();
170 return AliasAnalysis::UnknownSize
;
173 /// isStoreAtLeastAsWideAs - Return true if the size of the store in I1 is
174 /// greater than or equal to the store in I2. This returns false if we don't
177 static bool isStoreAtLeastAsWideAs(Instruction
*I1
, Instruction
*I2
,
178 const TargetData
*TD
) {
179 const Type
*I1Ty
= getPointerOperand(I1
)->getType();
180 const Type
*I2Ty
= getPointerOperand(I2
)->getType();
182 // Exactly the same type, must have exactly the same size.
183 if (I1Ty
== I2Ty
) return true;
185 uint64_t I1Size
= getStoreSize(I1
, TD
);
186 uint64_t I2Size
= getStoreSize(I2
, TD
);
188 return I1Size
!= AliasAnalysis::UnknownSize
&&
189 I2Size
!= AliasAnalysis::UnknownSize
&&
193 bool DSE::runOnBasicBlock(BasicBlock
&BB
) {
194 MemoryDependenceAnalysis
&MD
= getAnalysis
<MemoryDependenceAnalysis
>();
195 TD
= getAnalysisIfAvailable
<TargetData
>();
197 bool MadeChange
= false;
199 // Do a top-down walk on the BB.
200 for (BasicBlock::iterator BBI
= BB
.begin(), BBE
= BB
.end(); BBI
!= BBE
; ) {
201 Instruction
*Inst
= BBI
++;
203 // If we find a store or a free, get its memory dependence.
204 if (!doesClobberMemory(Inst
) && !isFreeCall(Inst
))
207 MemDepResult InstDep
= MD
.getDependency(Inst
);
209 // Ignore non-local stores.
210 // FIXME: cross-block DSE would be fun. :)
211 if (InstDep
.isNonLocal()) continue;
213 // Handle frees whose dependencies are non-trivial.
214 if (const CallInst
*F
= isFreeCall(Inst
)) {
215 MadeChange
|= handleFreeWithNonTrivialDependency(F
, InstDep
);
219 // If not a definite must-alias dependency, ignore it.
220 if (!InstDep
.isDef())
223 // If this is a store-store dependence, then the previous store is dead so
224 // long as this store is at least as big as it.
225 if (doesClobberMemory(InstDep
.getInst())) {
226 Instruction
*DepStore
= InstDep
.getInst();
227 if (isStoreAtLeastAsWideAs(Inst
, DepStore
, TD
) &&
228 isElidable(DepStore
)) {
229 // Delete the store and now-dead instructions that feed it.
230 DeleteDeadInstruction(DepStore
);
234 // DeleteDeadInstruction can delete the current instruction in loop
237 if (BBI
!= BB
.begin())
243 if (!isElidable(Inst
))
246 // If we're storing the same value back to a pointer that we just
247 // loaded from, then the store can be removed.
248 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(Inst
)) {
249 if (LoadInst
*DepLoad
= dyn_cast
<LoadInst
>(InstDep
.getInst())) {
250 if (SI
->getPointerOperand() == DepLoad
->getPointerOperand() &&
251 SI
->getOperand(0) == DepLoad
) {
252 // DeleteDeadInstruction can delete the current instruction. Save BBI
253 // in case we need it.
254 WeakVH
NextInst(BBI
);
256 DeleteDeadInstruction(SI
);
258 if (NextInst
== 0) // Next instruction deleted.
260 else if (BBI
!= BB
.begin()) // Revisit this instruction if possible.
269 // If this is a lifetime end marker, we can throw away the store.
270 if (IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(InstDep
.getInst())) {
271 if (II
->getIntrinsicID() == Intrinsic::lifetime_end
) {
272 // Delete the store and now-dead instructions that feed it.
273 // DeleteDeadInstruction can delete the current instruction. Save BBI
274 // in case we need it.
275 WeakVH
NextInst(BBI
);
277 DeleteDeadInstruction(Inst
);
279 if (NextInst
== 0) // Next instruction deleted.
281 else if (BBI
!= BB
.begin()) // Revisit this instruction if possible.
290 // If this block ends in a return, unwind, or unreachable, all allocas are
291 // dead at its end, which means stores to them are also dead.
292 if (BB
.getTerminator()->getNumSuccessors() == 0)
293 MadeChange
|= handleEndBlock(BB
);
298 /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
299 /// dependency is a store to a field of that structure.
300 bool DSE::handleFreeWithNonTrivialDependency(const CallInst
*F
,
302 AliasAnalysis
&AA
= getAnalysis
<AliasAnalysis
>();
304 Instruction
*Dependency
= Dep
.getInst();
305 if (!Dependency
|| !doesClobberMemory(Dependency
) || !isElidable(Dependency
))
308 Value
*DepPointer
= getPointerOperand(Dependency
)->getUnderlyingObject();
310 // Check for aliasing.
311 if (AA
.alias(F
->getArgOperand(0), 1, DepPointer
, 1) !=
312 AliasAnalysis::MustAlias
)
315 // DCE instructions only used to calculate that store
316 DeleteDeadInstruction(Dependency
);
321 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
322 /// function end block. Ex:
325 /// store i32 1, i32* %A
327 bool DSE::handleEndBlock(BasicBlock
&BB
) {
328 AliasAnalysis
&AA
= getAnalysis
<AliasAnalysis
>();
330 bool MadeChange
= false;
332 // Pointers alloca'd in this function are dead in the end block
333 SmallPtrSet
<Value
*, 64> deadPointers
;
335 // Find all of the alloca'd pointers in the entry block.
336 BasicBlock
*Entry
= BB
.getParent()->begin();
337 for (BasicBlock::iterator I
= Entry
->begin(), E
= Entry
->end(); I
!= E
; ++I
)
338 if (AllocaInst
*AI
= dyn_cast
<AllocaInst
>(I
))
339 deadPointers
.insert(AI
);
341 // Treat byval arguments the same, stores to them are dead at the end of the
343 for (Function::arg_iterator AI
= BB
.getParent()->arg_begin(),
344 AE
= BB
.getParent()->arg_end(); AI
!= AE
; ++AI
)
345 if (AI
->hasByValAttr())
346 deadPointers
.insert(AI
);
348 // Scan the basic block backwards
349 for (BasicBlock::iterator BBI
= BB
.end(); BBI
!= BB
.begin(); ){
352 // If we find a store whose pointer is dead.
353 if (doesClobberMemory(BBI
)) {
354 if (isElidable(BBI
)) {
355 // See through pointer-to-pointer bitcasts
356 Value
*pointerOperand
= getPointerOperand(BBI
)->getUnderlyingObject();
358 // Alloca'd pointers or byval arguments (which are functionally like
359 // alloca's) are valid candidates for removal.
360 if (deadPointers
.count(pointerOperand
)) {
361 // DCE instructions only used to calculate that store.
362 Instruction
*Dead
= BBI
;
364 DeleteDeadInstruction(Dead
, &deadPointers
);
371 // Because a memcpy or memmove is also a load, we can't skip it if we
373 if (!isa
<MemTransferInst
>(BBI
))
377 Value
*killPointer
= 0;
378 uint64_t killPointerSize
= AliasAnalysis::UnknownSize
;
380 // If we encounter a use of the pointer, it is no longer considered dead
381 if (LoadInst
*L
= dyn_cast
<LoadInst
>(BBI
)) {
382 // However, if this load is unused and not volatile, we can go ahead and
383 // remove it, and not have to worry about it making our pointer undead!
384 if (L
->use_empty() && !L
->isVolatile()) {
386 DeleteDeadInstruction(L
, &deadPointers
);
392 killPointer
= L
->getPointerOperand();
393 } else if (VAArgInst
*V
= dyn_cast
<VAArgInst
>(BBI
)) {
394 killPointer
= V
->getOperand(0);
395 } else if (isa
<MemTransferInst
>(BBI
) &&
396 isa
<ConstantInt
>(cast
<MemTransferInst
>(BBI
)->getLength())) {
397 killPointer
= cast
<MemTransferInst
>(BBI
)->getSource();
398 killPointerSize
= cast
<ConstantInt
>(
399 cast
<MemTransferInst
>(BBI
)->getLength())->getZExtValue();
400 } else if (AllocaInst
*A
= dyn_cast
<AllocaInst
>(BBI
)) {
401 deadPointers
.erase(A
);
403 // Dead alloca's can be DCE'd when we reach them
404 if (A
->use_empty()) {
406 DeleteDeadInstruction(A
, &deadPointers
);
412 } else if (CallSite CS
= cast
<Value
>(BBI
)) {
413 // If this call does not access memory, it can't
414 // be undeadifying any of our pointers.
415 if (AA
.doesNotAccessMemory(CS
))
421 // Remove any pointers made undead by the call from the dead set
422 std::vector
<Value
*> dead
;
423 for (SmallPtrSet
<Value
*, 64>::iterator I
= deadPointers
.begin(),
424 E
= deadPointers
.end(); I
!= E
; ++I
) {
425 // HACK: if we detect that our AA is imprecise, it's not
426 // worth it to scan the rest of the deadPointers set. Just
427 // assume that the AA will return ModRef for everything, and
428 // go ahead and bail.
429 if (modRef
>= 16 && other
== 0) {
430 deadPointers
.clear();
434 // See if the call site touches it
435 AliasAnalysis::ModRefResult A
= AA
.getModRefInfo(CS
, *I
,
438 if (A
== AliasAnalysis::ModRef
)
443 if (A
== AliasAnalysis::ModRef
|| A
== AliasAnalysis::Ref
)
447 for (std::vector
<Value
*>::iterator I
= dead
.begin(), E
= dead
.end();
449 deadPointers
.erase(*I
);
452 } else if (isInstructionTriviallyDead(BBI
)) {
453 // For any non-memory-affecting non-terminators, DCE them as we reach them
454 Instruction
*Inst
= BBI
;
456 DeleteDeadInstruction(Inst
, &deadPointers
);
465 killPointer
= killPointer
->getUnderlyingObject();
467 // Deal with undead pointers
468 MadeChange
|= RemoveUndeadPointers(killPointer
, killPointerSize
, BBI
,
475 /// RemoveUndeadPointers - check for uses of a pointer that make it
476 /// undead when scanning for dead stores to alloca's.
477 bool DSE::RemoveUndeadPointers(Value
*killPointer
, uint64_t killPointerSize
,
478 BasicBlock::iterator
&BBI
,
479 SmallPtrSet
<Value
*, 64> &deadPointers
) {
480 AliasAnalysis
&AA
= getAnalysis
<AliasAnalysis
>();
482 // If the kill pointer can be easily reduced to an alloca,
483 // don't bother doing extraneous AA queries.
484 if (deadPointers
.count(killPointer
)) {
485 deadPointers
.erase(killPointer
);
489 // A global can't be in the dead pointer set.
490 if (isa
<GlobalValue
>(killPointer
))
493 bool MadeChange
= false;
495 SmallVector
<Value
*, 16> undead
;
497 for (SmallPtrSet
<Value
*, 64>::iterator I
= deadPointers
.begin(),
498 E
= deadPointers
.end(); I
!= E
; ++I
) {
499 // See if this pointer could alias it
500 AliasAnalysis::AliasResult A
= AA
.alias(*I
, getPointerSize(*I
),
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
);
509 DeleteDeadInstruction(S
, &deadPointers
);
515 // Otherwise, it is undead
516 } else if (A
!= AliasAnalysis::NoAlias
)
517 undead
.push_back(*I
);
520 for (SmallVector
<Value
*, 16>::iterator I
= undead
.begin(), E
= undead
.end();
522 deadPointers
.erase(*I
);
527 /// DeleteDeadInstruction - Delete this instruction. Before we do, go through
528 /// and zero out all the operands of this instruction. If any of them become
529 /// dead, delete them and the computation tree that feeds them.
531 /// If ValueSet is non-null, remove any deleted instructions from it as well.
533 void DSE::DeleteDeadInstruction(Instruction
*I
,
534 SmallPtrSet
<Value
*, 64> *ValueSet
) {
535 SmallVector
<Instruction
*, 32> NowDeadInsts
;
537 NowDeadInsts
.push_back(I
);
540 // Before we touch this instruction, remove it from memdep!
541 MemoryDependenceAnalysis
&MDA
= getAnalysis
<MemoryDependenceAnalysis
>();
543 Instruction
*DeadInst
= NowDeadInsts
.pop_back_val();
547 // This instruction is dead, zap it, in stages. Start by removing it from
548 // MemDep, which needs to know the operands and needs it to be in the
550 MDA
.removeInstruction(DeadInst
);
552 for (unsigned op
= 0, e
= DeadInst
->getNumOperands(); op
!= e
; ++op
) {
553 Value
*Op
= DeadInst
->getOperand(op
);
554 DeadInst
->setOperand(op
, 0);
556 // If this operand just became dead, add it to the NowDeadInsts list.
557 if (!Op
->use_empty()) continue;
559 if (Instruction
*OpI
= dyn_cast
<Instruction
>(Op
))
560 if (isInstructionTriviallyDead(OpI
))
561 NowDeadInsts
.push_back(OpI
);
564 DeadInst
->eraseFromParent();
566 if (ValueSet
) ValueSet
->erase(DeadInst
);
567 } while (!NowDeadInsts
.empty());
570 uint64_t DSE::getPointerSize(Value
*V
) const {
572 if (AllocaInst
*A
= dyn_cast
<AllocaInst
>(V
)) {
573 // Get size information for the alloca
574 if (ConstantInt
*C
= dyn_cast
<ConstantInt
>(A
->getArraySize()))
575 return C
->getZExtValue() * TD
->getTypeAllocSize(A
->getAllocatedType());
577 assert(isa
<Argument
>(V
) && "Expected AllocaInst or Argument!");
578 const PointerType
*PT
= cast
<PointerType
>(V
->getType());
579 return TD
->getTypeAllocSize(PT
->getElementType());
582 return AliasAnalysis::UnknownSize
;