blob: a1a9f812043fd176e38fb37daa0e6a449e534a0b [file] [log] [blame]
Owen Andersone3590582007-08-02 18:11:11 +00001//===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
Owen Anderson5e72db32007-07-11 00:46:18 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Owen Anderson5e72db32007-07-11 00:46:18 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a trivial dead store elimination that only considers
11// basic-block local redundant stores.
12//
13// FIXME: This should eventually be extended to be a post-dominator tree
14// traversal. Doing so would be pretty trivial.
15//
16//===----------------------------------------------------------------------===//
17
Owen Anderson10e52ed2007-08-01 06:36:51 +000018#define DEBUG_TYPE "dse"
Owen Anderson5e72db32007-07-11 00:46:18 +000019#include "llvm/Transforms/Scalar.h"
Owen Anderson32c4a052007-07-12 21:41:30 +000020#include "llvm/Constants.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000021#include "llvm/Function.h"
22#include "llvm/Instructions.h"
Owen Anderson48d37802008-01-29 06:18:36 +000023#include "llvm/IntrinsicInst.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000024#include "llvm/Pass.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Owen Andersonaa071722007-07-11 23:19:17 +000027#include "llvm/Analysis/AliasAnalysis.h"
Owen Anderson3f338972008-07-28 16:14:26 +000028#include "llvm/Analysis/Dominators.h"
Victor Hernandezf390e042009-10-27 20:05:49 +000029#include "llvm/Analysis/MemoryBuiltins.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000030#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Owen Andersonaa071722007-07-11 23:19:17 +000031#include "llvm/Target/TargetData.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000032#include "llvm/Transforms/Utils/Local.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000033using namespace llvm;
34
35STATISTIC(NumFastStores, "Number of stores deleted");
36STATISTIC(NumFastOther , "Number of other instrs removed");
37
38namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +000039 struct DSE : public FunctionPass {
Dan Gohman67243a42009-07-24 18:13:53 +000040 TargetData *TD;
41
Owen Anderson5e72db32007-07-11 00:46:18 +000042 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000043 DSE() : FunctionPass(ID) {}
Owen Anderson5e72db32007-07-11 00:46:18 +000044
45 virtual bool runOnFunction(Function &F) {
46 bool Changed = false;
Chris Lattnerc053cbb2010-02-11 05:11:54 +000047
48 DominatorTree &DT = getAnalysis<DominatorTree>();
49
Owen Anderson5e72db32007-07-11 00:46:18 +000050 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnerc053cbb2010-02-11 05:11:54 +000051 // Only check non-dead blocks. Dead blocks may have strange pointer
52 // cycles that will confuse alias analysis.
53 if (DT.isReachableFromEntry(I))
54 Changed |= runOnBasicBlock(*I);
Owen Anderson5e72db32007-07-11 00:46:18 +000055 return Changed;
56 }
Chris Lattnerde04e112008-11-29 01:43:36 +000057
Owen Anderson5e72db32007-07-11 00:46:18 +000058 bool runOnBasicBlock(BasicBlock &BB);
Gabor Greif07e92842010-06-25 07:40:32 +000059 bool handleFreeWithNonTrivialDependency(const CallInst *F,
60 MemDepResult Dep);
Chris Lattner1adb6752008-11-28 00:27:14 +000061 bool handleEndBlock(BasicBlock &BB);
Nick Lewycky475d3d12010-01-03 04:39:07 +000062 bool RemoveUndeadPointers(Value *Ptr, uint64_t killPointerSize,
63 BasicBlock::iterator &BBI,
64 SmallPtrSet<Value*, 64> &deadPointers);
Chris Lattner1adb6752008-11-28 00:27:14 +000065 void DeleteDeadInstruction(Instruction *I,
66 SmallPtrSet<Value*, 64> *deadPointers = 0);
67
Owen Anderson5e72db32007-07-11 00:46:18 +000068
69 // getAnalysisUsage - We require post dominance frontiers (aka Control
70 // Dependence Graph)
71 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.setPreservesCFG();
Owen Anderson3f338972008-07-28 16:14:26 +000073 AU.addRequired<DominatorTree>();
Owen Andersonaa071722007-07-11 23:19:17 +000074 AU.addRequired<AliasAnalysis>();
Owen Anderson5e72db32007-07-11 00:46:18 +000075 AU.addRequired<MemoryDependenceAnalysis>();
Owen Anderson3f338972008-07-28 16:14:26 +000076 AU.addPreserved<DominatorTree>();
Owen Anderson5e72db32007-07-11 00:46:18 +000077 AU.addPreserved<MemoryDependenceAnalysis>();
78 }
Nick Lewycky475d3d12010-01-03 04:39:07 +000079
80 unsigned getPointerSize(Value *V) const;
Owen Anderson5e72db32007-07-11 00:46:18 +000081 };
Owen Anderson5e72db32007-07-11 00:46:18 +000082}
83
Dan Gohmand78c4002008-05-13 00:00:25 +000084char DSE::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000085INITIALIZE_PASS_BEGIN(DSE, "dse", "Dead Store Elimination", false, false)
86INITIALIZE_PASS_DEPENDENCY(DominatorTree)
87INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
88INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
89INITIALIZE_PASS_END(DSE, "dse", "Dead Store Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000090
Owen Anderson10e52ed2007-08-01 06:36:51 +000091FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
Owen Anderson5e72db32007-07-11 00:46:18 +000092
Nick Lewycky90271472009-11-10 06:46:40 +000093/// doesClobberMemory - Does this instruction clobber (write without reading)
94/// some memory?
95static bool doesClobberMemory(Instruction *I) {
96 if (isa<StoreInst>(I))
97 return true;
98 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
99 switch (II->getIntrinsicID()) {
Chris Lattner2764b4d2009-12-02 06:35:55 +0000100 default:
101 return false;
102 case Intrinsic::memset:
103 case Intrinsic::memmove:
104 case Intrinsic::memcpy:
105 case Intrinsic::init_trampoline:
106 case Intrinsic::lifetime_end:
107 return true;
Nick Lewycky90271472009-11-10 06:46:40 +0000108 }
109 }
110 return false;
111}
112
Duncan Sands1925d3a2009-11-10 13:49:50 +0000113/// isElidable - If the value of this instruction and the memory it writes to is
Nick Lewycky90271472009-11-10 06:46:40 +0000114/// unused, may we delete this instrtction?
115static bool isElidable(Instruction *I) {
116 assert(doesClobberMemory(I));
117 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
118 return II->getIntrinsicID() != Intrinsic::lifetime_end;
119 if (StoreInst *SI = dyn_cast<StoreInst>(I))
120 return !SI->isVolatile();
121 return true;
122}
123
124/// getPointerOperand - Return the pointer that is being clobbered.
125static Value *getPointerOperand(Instruction *I) {
126 assert(doesClobberMemory(I));
127 if (StoreInst *SI = dyn_cast<StoreInst>(I))
128 return SI->getPointerOperand();
129 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
Gabor Greif91f95892010-06-24 12:03:56 +0000130 return MI->getArgOperand(0);
131
132 IntrinsicInst *II = cast<IntrinsicInst>(I);
133 switch (II->getIntrinsicID()) {
Chris Lattner2764b4d2009-12-02 06:35:55 +0000134 default: assert(false && "Unexpected intrinsic!");
135 case Intrinsic::init_trampoline:
Gabor Greif91f95892010-06-24 12:03:56 +0000136 return II->getArgOperand(0);
Eric Christopher7258dcd2010-04-16 23:37:20 +0000137 case Intrinsic::lifetime_end:
Gabor Greif91f95892010-06-24 12:03:56 +0000138 return II->getArgOperand(1);
Duncan Sands1925d3a2009-11-10 13:49:50 +0000139 }
Nick Lewycky90271472009-11-10 06:46:40 +0000140}
141
142/// getStoreSize - Return the length in bytes of the write by the clobbering
143/// instruction. If variable or unknown, returns -1.
144static unsigned getStoreSize(Instruction *I, const TargetData *TD) {
145 assert(doesClobberMemory(I));
146 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
147 if (!TD) return -1u;
Nick Lewycky5b3def92009-11-10 07:00:43 +0000148 return TD->getTypeStoreSize(SI->getOperand(0)->getType());
Nick Lewycky90271472009-11-10 06:46:40 +0000149 }
150
151 Value *Len;
152 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
153 Len = MI->getLength();
154 } else {
Gabor Greif91f95892010-06-24 12:03:56 +0000155 IntrinsicInst *II = cast<IntrinsicInst>(I);
156 switch (II->getIntrinsicID()) {
Chris Lattner2764b4d2009-12-02 06:35:55 +0000157 default: assert(false && "Unexpected intrinsic!");
158 case Intrinsic::init_trampoline:
159 return -1u;
160 case Intrinsic::lifetime_end:
Gabor Greif91f95892010-06-24 12:03:56 +0000161 Len = II->getArgOperand(0);
Chris Lattner2764b4d2009-12-02 06:35:55 +0000162 break;
Duncan Sands1925d3a2009-11-10 13:49:50 +0000163 }
Nick Lewycky90271472009-11-10 06:46:40 +0000164 }
165 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(Len))
166 if (!LenCI->isAllOnesValue())
167 return LenCI->getZExtValue();
168 return -1u;
169}
170
171/// isStoreAtLeastAsWideAs - Return true if the size of the store in I1 is
172/// greater than or equal to the store in I2. This returns false if we don't
173/// know.
Chris Lattnera0906272009-11-04 23:20:12 +0000174///
Nick Lewycky90271472009-11-10 06:46:40 +0000175static bool isStoreAtLeastAsWideAs(Instruction *I1, Instruction *I2,
176 const TargetData *TD) {
177 const Type *I1Ty = getPointerOperand(I1)->getType();
178 const Type *I2Ty = getPointerOperand(I2)->getType();
Chris Lattnera0906272009-11-04 23:20:12 +0000179
180 // Exactly the same type, must have exactly the same size.
Nick Lewycky90271472009-11-10 06:46:40 +0000181 if (I1Ty == I2Ty) return true;
Chris Lattnera0906272009-11-04 23:20:12 +0000182
Nick Lewycky90271472009-11-10 06:46:40 +0000183 int I1Size = getStoreSize(I1, TD);
184 int I2Size = getStoreSize(I2, TD);
Chris Lattnera0906272009-11-04 23:20:12 +0000185
Nick Lewycky90271472009-11-10 06:46:40 +0000186 return I1Size != -1 && I2Size != -1 && I1Size >= I2Size;
Chris Lattnera0906272009-11-04 23:20:12 +0000187}
188
Owen Anderson10e52ed2007-08-01 06:36:51 +0000189bool DSE::runOnBasicBlock(BasicBlock &BB) {
Nick Lewycky475d3d12010-01-03 04:39:07 +0000190 MemoryDependenceAnalysis &MD = getAnalysis<MemoryDependenceAnalysis>();
Dan Gohman67243a42009-07-24 18:13:53 +0000191 TD = getAnalysisIfAvailable<TargetData>();
Owen Anderson2ed651a2007-11-01 05:29:16 +0000192
Owen Anderson5e72db32007-07-11 00:46:18 +0000193 bool MadeChange = false;
194
Chris Lattner49162672009-09-02 06:31:02 +0000195 // Do a top-down walk on the BB.
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000196 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
197 Instruction *Inst = BBI++;
198
Dan Gohman67243a42009-07-24 18:13:53 +0000199 // If we find a store or a free, get its memory dependence.
Nick Lewycky90271472009-11-10 06:46:40 +0000200 if (!doesClobberMemory(Inst) && !isFreeCall(Inst))
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000201 continue;
Chris Lattner5df5b4c2008-12-07 00:25:15 +0000202
Chris Lattner57e91ea2008-12-06 00:53:22 +0000203 MemDepResult InstDep = MD.getDependency(Inst);
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000204
Chris Lattner57e91ea2008-12-06 00:53:22 +0000205 // Ignore non-local stores.
206 // FIXME: cross-block DSE would be fun. :)
207 if (InstDep.isNonLocal()) continue;
208
209 // Handle frees whose dependencies are non-trivial.
Gabor Greif07e92842010-06-25 07:40:32 +0000210 if (const CallInst *F = isFreeCall(Inst)) {
211 MadeChange |= handleFreeWithNonTrivialDependency(F, InstDep);
Chris Lattner57e91ea2008-12-06 00:53:22 +0000212 continue;
213 }
214
Chris Lattner57e91ea2008-12-06 00:53:22 +0000215 // If not a definite must-alias dependency, ignore it.
216 if (!InstDep.isDef())
217 continue;
218
219 // If this is a store-store dependence, then the previous store is dead so
220 // long as this store is at least as big as it.
Nick Lewycky90271472009-11-10 06:46:40 +0000221 if (doesClobberMemory(InstDep.getInst())) {
222 Instruction *DepStore = InstDep.getInst();
223 if (isStoreAtLeastAsWideAs(Inst, DepStore, TD) &&
224 isElidable(DepStore)) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000225 // Delete the store and now-dead instructions that feed it.
Chris Lattner57e91ea2008-12-06 00:53:22 +0000226 DeleteDeadInstruction(DepStore);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000227 ++NumFastStores;
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000228 MadeChange = true;
Chris Lattner49162672009-09-02 06:31:02 +0000229
230 // DeleteDeadInstruction can delete the current instruction in loop
231 // cases, reset BBI.
232 BBI = Inst;
Chris Lattner8c5ff512008-11-29 20:29:04 +0000233 if (BBI != BB.begin())
Chris Lattnerf3f6a802008-11-28 22:50:08 +0000234 --BBI;
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000235 continue;
236 }
Nick Lewycky90271472009-11-10 06:46:40 +0000237 }
238
239 if (!isElidable(Inst))
240 continue;
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000241
Chris Lattner57e91ea2008-12-06 00:53:22 +0000242 // If we're storing the same value back to a pointer that we just
243 // loaded from, then the store can be removed.
Nick Lewycky90271472009-11-10 06:46:40 +0000244 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
245 if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
246 if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
247 SI->getOperand(0) == DepLoad) {
248 // DeleteDeadInstruction can delete the current instruction. Save BBI
249 // in case we need it.
250 WeakVH NextInst(BBI);
251
252 DeleteDeadInstruction(SI);
253
254 if (NextInst == 0) // Next instruction deleted.
255 BBI = BB.begin();
256 else if (BBI != BB.begin()) // Revisit this instruction if possible.
257 --BBI;
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000258 ++NumFastStores;
Nick Lewycky90271472009-11-10 06:46:40 +0000259 MadeChange = true;
260 continue;
261 }
Chris Lattner0e3d6332008-12-05 21:04:20 +0000262 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000263 }
Owen Anderson2b2bd282009-10-28 07:05:35 +0000264
265 // If this is a lifetime end marker, we can throw away the store.
Nick Lewycky90271472009-11-10 06:46:40 +0000266 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(InstDep.getInst())) {
Owen Anderson2b2bd282009-10-28 07:05:35 +0000267 if (II->getIntrinsicID() == Intrinsic::lifetime_end) {
268 // Delete the store and now-dead instructions that feed it.
269 // DeleteDeadInstruction can delete the current instruction. Save BBI
270 // in case we need it.
271 WeakVH NextInst(BBI);
272
Nick Lewycky90271472009-11-10 06:46:40 +0000273 DeleteDeadInstruction(Inst);
Owen Anderson2b2bd282009-10-28 07:05:35 +0000274
275 if (NextInst == 0) // Next instruction deleted.
276 BBI = BB.begin();
277 else if (BBI != BB.begin()) // Revisit this instruction if possible.
278 --BBI;
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000279 ++NumFastStores;
Owen Anderson2b2bd282009-10-28 07:05:35 +0000280 MadeChange = true;
281 continue;
282 }
283 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000284 }
285
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000286 // If this block ends in a return, unwind, or unreachable, all allocas are
287 // dead at its end, which means stores to them are also dead.
Owen Anderson32c4a052007-07-12 21:41:30 +0000288 if (BB.getTerminator()->getNumSuccessors() == 0)
Chris Lattner1adb6752008-11-28 00:27:14 +0000289 MadeChange |= handleEndBlock(BB);
Owen Anderson5e72db32007-07-11 00:46:18 +0000290
291 return MadeChange;
292}
293
Owen Andersonaa071722007-07-11 23:19:17 +0000294/// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
Chris Lattner1adb6752008-11-28 00:27:14 +0000295/// dependency is a store to a field of that structure.
Gabor Greif07e92842010-06-25 07:40:32 +0000296bool DSE::handleFreeWithNonTrivialDependency(const CallInst *F,
297 MemDepResult Dep) {
Owen Andersonaa071722007-07-11 23:19:17 +0000298 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Andersonaa071722007-07-11 23:19:17 +0000299
Nick Lewycky90271472009-11-10 06:46:40 +0000300 Instruction *Dependency = Dep.getInst();
301 if (!Dependency || !doesClobberMemory(Dependency) || !isElidable(Dependency))
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000302 return false;
Owen Andersond4451de2007-07-12 18:08:51 +0000303
Nick Lewycky90271472009-11-10 06:46:40 +0000304 Value *DepPointer = getPointerOperand(Dependency)->getUnderlyingObject();
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000305
Chris Lattner57e91ea2008-12-06 00:53:22 +0000306 // Check for aliasing.
Gabor Greif07e92842010-06-25 07:40:32 +0000307 if (AA.alias(F->getArgOperand(0), 1, DepPointer, 1) !=
Chris Lattner57e91ea2008-12-06 00:53:22 +0000308 AliasAnalysis::MustAlias)
Chris Lattner1adb6752008-11-28 00:27:14 +0000309 return false;
Owen Andersonaa071722007-07-11 23:19:17 +0000310
Chris Lattner1adb6752008-11-28 00:27:14 +0000311 // DCE instructions only used to calculate that store
Chris Lattner57e91ea2008-12-06 00:53:22 +0000312 DeleteDeadInstruction(Dependency);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000313 ++NumFastStores;
Chris Lattner1adb6752008-11-28 00:27:14 +0000314 return true;
Owen Andersonaa071722007-07-11 23:19:17 +0000315}
316
Owen Andersone3590582007-08-02 18:11:11 +0000317/// handleEndBlock - Remove dead stores to stack-allocated locations in the
Owen Anderson52aaabf2007-08-08 17:50:09 +0000318/// function end block. Ex:
319/// %A = alloca i32
320/// ...
321/// store i32 1, i32* %A
322/// ret void
Chris Lattner1adb6752008-11-28 00:27:14 +0000323bool DSE::handleEndBlock(BasicBlock &BB) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000324 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000325
326 bool MadeChange = false;
327
328 // Pointers alloca'd in this function are dead in the end block
Owen Anderson48d37802008-01-29 06:18:36 +0000329 SmallPtrSet<Value*, 64> deadPointers;
Owen Anderson32c4a052007-07-12 21:41:30 +0000330
Chris Lattner1adb6752008-11-28 00:27:14 +0000331 // Find all of the alloca'd pointers in the entry block.
Owen Anderson32c4a052007-07-12 21:41:30 +0000332 BasicBlock *Entry = BB.getParent()->begin();
333 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
334 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
335 deadPointers.insert(AI);
Chris Lattner1adb6752008-11-28 00:27:14 +0000336
337 // Treat byval arguments the same, stores to them are dead at the end of the
338 // function.
Owen Anderson48d37802008-01-29 06:18:36 +0000339 for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
340 AE = BB.getParent()->arg_end(); AI != AE; ++AI)
341 if (AI->hasByValAttr())
342 deadPointers.insert(AI);
Owen Anderson32c4a052007-07-12 21:41:30 +0000343
344 // Scan the basic block backwards
345 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
346 --BBI;
347
Chris Lattner1adb6752008-11-28 00:27:14 +0000348 // If we find a store whose pointer is dead.
Nick Lewycky90271472009-11-10 06:46:40 +0000349 if (doesClobberMemory(BBI)) {
350 if (isElidable(BBI)) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000351 // See through pointer-to-pointer bitcasts
Nick Lewycky90271472009-11-10 06:46:40 +0000352 Value *pointerOperand = getPointerOperand(BBI)->getUnderlyingObject();
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000353
Owen Anderson6af19fd2008-01-25 10:10:33 +0000354 // Alloca'd pointers or byval arguments (which are functionally like
355 // alloca's) are valid candidates for removal.
Owen Anderson48d37802008-01-29 06:18:36 +0000356 if (deadPointers.count(pointerOperand)) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000357 // DCE instructions only used to calculate that store.
Nick Lewycky90271472009-11-10 06:46:40 +0000358 Instruction *Dead = BBI;
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000359 ++BBI;
Nick Lewycky90271472009-11-10 06:46:40 +0000360 DeleteDeadInstruction(Dead, &deadPointers);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000361 ++NumFastStores;
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000362 MadeChange = true;
Nick Lewycky90271472009-11-10 06:46:40 +0000363 continue;
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000364 }
Owen Anderson32c4a052007-07-12 21:41:30 +0000365 }
Owen Anderson52aaabf2007-08-08 17:50:09 +0000366
Nick Lewycky90271472009-11-10 06:46:40 +0000367 // Because a memcpy or memmove is also a load, we can't skip it if we
368 // didn't remove it.
369 if (!isa<MemTransferInst>(BBI))
Owen Anderson48d37802008-01-29 06:18:36 +0000370 continue;
Owen Anderson52aaabf2007-08-08 17:50:09 +0000371 }
372
Nick Lewycky475d3d12010-01-03 04:39:07 +0000373 Value *killPointer = 0;
Owen Andersona82c9932008-02-04 04:53:00 +0000374 uint64_t killPointerSize = ~0UL;
Owen Anderson32c4a052007-07-12 21:41:30 +0000375
376 // If we encounter a use of the pointer, it is no longer considered dead
Chris Lattner1adb6752008-11-28 00:27:14 +0000377 if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
Nate Begeman53c5c622008-05-13 01:48:26 +0000378 // However, if this load is unused and not volatile, we can go ahead and
379 // remove it, and not have to worry about it making our pointer undead!
Dan Gohman8cb19d92008-04-28 19:51:27 +0000380 if (L->use_empty() && !L->isVolatile()) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000381 ++BBI;
Chris Lattner1adb6752008-11-28 00:27:14 +0000382 DeleteDeadInstruction(L, &deadPointers);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000383 ++NumFastOther;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000384 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000385 continue;
386 }
387
Owen Anderson32c4a052007-07-12 21:41:30 +0000388 killPointer = L->getPointerOperand();
Nick Lewycky475d3d12010-01-03 04:39:07 +0000389 } else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000390 killPointer = V->getOperand(0);
Nick Lewycky90271472009-11-10 06:46:40 +0000391 } else if (isa<MemTransferInst>(BBI) &&
392 isa<ConstantInt>(cast<MemTransferInst>(BBI)->getLength())) {
393 killPointer = cast<MemTransferInst>(BBI)->getSource();
Owen Andersona82c9932008-02-04 04:53:00 +0000394 killPointerSize = cast<ConstantInt>(
Nick Lewycky90271472009-11-10 06:46:40 +0000395 cast<MemTransferInst>(BBI)->getLength())->getZExtValue();
Nick Lewycky475d3d12010-01-03 04:39:07 +0000396 } else if (AllocaInst *A = dyn_cast<AllocaInst>(BBI)) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000397 deadPointers.erase(A);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000398
399 // Dead alloca's can be DCE'd when we reach them
Nick Lewycky6b016702008-01-30 08:01:28 +0000400 if (A->use_empty()) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000401 ++BBI;
Chris Lattner1adb6752008-11-28 00:27:14 +0000402 DeleteDeadInstruction(A, &deadPointers);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000403 ++NumFastOther;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000404 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000405 }
406
Owen Anderson32c4a052007-07-12 21:41:30 +0000407 continue;
Gabor Greif0a970692010-07-28 14:28:18 +0000408 } else if (CallSite CS = cast<Value>(BBI)) {
Owen Anderson50df9682007-08-08 17:58:56 +0000409 // If this call does not access memory, it can't
410 // be undeadifying any of our pointers.
Duncan Sands68b6f502007-12-01 07:51:45 +0000411 if (AA.doesNotAccessMemory(CS))
Owen Anderson50df9682007-08-08 17:58:56 +0000412 continue;
413
Owen Andersonddf4aee2007-08-08 18:38:28 +0000414 unsigned modRef = 0;
415 unsigned other = 0;
416
Owen Anderson32c4a052007-07-12 21:41:30 +0000417 // Remove any pointers made undead by the call from the dead set
Owen Anderson48d37802008-01-29 06:18:36 +0000418 std::vector<Value*> dead;
419 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000420 E = deadPointers.end(); I != E; ++I) {
Owen Andersonddf4aee2007-08-08 18:38:28 +0000421 // HACK: if we detect that our AA is imprecise, it's not
422 // worth it to scan the rest of the deadPointers set. Just
423 // assume that the AA will return ModRef for everything, and
424 // go ahead and bail.
425 if (modRef >= 16 && other == 0) {
426 deadPointers.clear();
427 return MadeChange;
428 }
Nick Lewycky475d3d12010-01-03 04:39:07 +0000429
Owen Anderson32c4a052007-07-12 21:41:30 +0000430 // See if the call site touches it
Nick Lewycky475d3d12010-01-03 04:39:07 +0000431 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I,
432 getPointerSize(*I));
Owen Andersonddf4aee2007-08-08 18:38:28 +0000433
434 if (A == AliasAnalysis::ModRef)
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000435 ++modRef;
Owen Andersonddf4aee2007-08-08 18:38:28 +0000436 else
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000437 ++other;
Owen Andersonddf4aee2007-08-08 18:38:28 +0000438
Owen Anderson9c9ef212007-07-13 18:26:26 +0000439 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
Owen Anderson32c4a052007-07-12 21:41:30 +0000440 dead.push_back(*I);
441 }
442
Owen Anderson48d37802008-01-29 06:18:36 +0000443 for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000444 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000445 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000446
447 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000448 } else if (isInstructionTriviallyDead(BBI)) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000449 // For any non-memory-affecting non-terminators, DCE them as we reach them
Chris Lattner1adb6752008-11-28 00:27:14 +0000450 Instruction *Inst = BBI;
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000451 ++BBI;
Chris Lattner1adb6752008-11-28 00:27:14 +0000452 DeleteDeadInstruction(Inst, &deadPointers);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000453 ++NumFastOther;
Chris Lattner1adb6752008-11-28 00:27:14 +0000454 MadeChange = true;
455 continue;
Owen Anderson32c4a052007-07-12 21:41:30 +0000456 }
457
458 if (!killPointer)
459 continue;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000460
461 killPointer = killPointer->getUnderlyingObject();
462
Owen Anderson32c4a052007-07-12 21:41:30 +0000463 // Deal with undead pointers
Owen Andersona82c9932008-02-04 04:53:00 +0000464 MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
Chris Lattner1adb6752008-11-28 00:27:14 +0000465 deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000466 }
467
468 return MadeChange;
469}
470
Owen Andersonddf4aee2007-08-08 18:38:28 +0000471/// RemoveUndeadPointers - check for uses of a pointer that make it
472/// undead when scanning for dead stores to alloca's.
Nick Lewycky475d3d12010-01-03 04:39:07 +0000473bool DSE::RemoveUndeadPointers(Value *killPointer, uint64_t killPointerSize,
Chris Lattner1adb6752008-11-28 00:27:14 +0000474 BasicBlock::iterator &BBI,
Nick Lewycky475d3d12010-01-03 04:39:07 +0000475 SmallPtrSet<Value*, 64> &deadPointers) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000476 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Nick Lewycky475d3d12010-01-03 04:39:07 +0000477
Owen Andersonddf4aee2007-08-08 18:38:28 +0000478 // If the kill pointer can be easily reduced to an alloca,
Chris Lattner1adb6752008-11-28 00:27:14 +0000479 // don't bother doing extraneous AA queries.
Owen Anderson48d37802008-01-29 06:18:36 +0000480 if (deadPointers.count(killPointer)) {
481 deadPointers.erase(killPointer);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000482 return false;
483 }
484
Chris Lattner1adb6752008-11-28 00:27:14 +0000485 // A global can't be in the dead pointer set.
486 if (isa<GlobalValue>(killPointer))
487 return false;
488
Owen Anderson32c4a052007-07-12 21:41:30 +0000489 bool MadeChange = false;
490
Chris Lattner1adb6752008-11-28 00:27:14 +0000491 SmallVector<Value*, 16> undead;
Nick Lewycky475d3d12010-01-03 04:39:07 +0000492
Owen Anderson48d37802008-01-29 06:18:36 +0000493 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Nick Lewycky475d3d12010-01-03 04:39:07 +0000494 E = deadPointers.end(); I != E; ++I) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000495 // See if this pointer could alias it
Nick Lewycky475d3d12010-01-03 04:39:07 +0000496 AliasAnalysis::AliasResult A = AA.alias(*I, getPointerSize(*I),
Owen Andersona82c9932008-02-04 04:53:00 +0000497 killPointer, killPointerSize);
Owen Anderson32c4a052007-07-12 21:41:30 +0000498
499 // If it must-alias and a store, we can delete it
500 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
Nick Lewycky475d3d12010-01-03 04:39:07 +0000501 StoreInst *S = cast<StoreInst>(BBI);
Owen Anderson32c4a052007-07-12 21:41:30 +0000502
503 // Remove it!
Nick Lewycky475d3d12010-01-03 04:39:07 +0000504 ++BBI;
Chris Lattner1adb6752008-11-28 00:27:14 +0000505 DeleteDeadInstruction(S, &deadPointers);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000506 ++NumFastStores;
Owen Anderson32c4a052007-07-12 21:41:30 +0000507 MadeChange = true;
508
509 continue;
510
511 // Otherwise, it is undead
Chris Lattner1adb6752008-11-28 00:27:14 +0000512 } else if (A != AliasAnalysis::NoAlias)
513 undead.push_back(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000514 }
515
Chris Lattner1adb6752008-11-28 00:27:14 +0000516 for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000517 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000518 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000519
520 return MadeChange;
521}
522
Chris Lattner1adb6752008-11-28 00:27:14 +0000523/// DeleteDeadInstruction - Delete this instruction. Before we do, go through
524/// and zero out all the operands of this instruction. If any of them become
525/// dead, delete them and the computation tree that feeds them.
526///
527/// If ValueSet is non-null, remove any deleted instructions from it as well.
528///
529void DSE::DeleteDeadInstruction(Instruction *I,
530 SmallPtrSet<Value*, 64> *ValueSet) {
531 SmallVector<Instruction*, 32> NowDeadInsts;
532
533 NowDeadInsts.push_back(I);
534 --NumFastOther;
Owen Anderson5e72db32007-07-11 00:46:18 +0000535
Chris Lattner1adb6752008-11-28 00:27:14 +0000536 // Before we touch this instruction, remove it from memdep!
537 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
Dan Gohman28943872010-01-05 16:27:25 +0000538 do {
539 Instruction *DeadInst = NowDeadInsts.pop_back_val();
Owen Andersonbf971aa2007-07-11 19:03:09 +0000540
Chris Lattner1adb6752008-11-28 00:27:14 +0000541 ++NumFastOther;
542
543 // This instruction is dead, zap it, in stages. Start by removing it from
544 // MemDep, which needs to know the operands and needs it to be in the
545 // function.
546 MDA.removeInstruction(DeadInst);
547
548 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
549 Value *Op = DeadInst->getOperand(op);
550 DeadInst->setOperand(op, 0);
551
552 // If this operand just became dead, add it to the NowDeadInsts list.
553 if (!Op->use_empty()) continue;
554
555 if (Instruction *OpI = dyn_cast<Instruction>(Op))
556 if (isInstructionTriviallyDead(OpI))
557 NowDeadInsts.push_back(OpI);
558 }
559
560 DeadInst->eraseFromParent();
561
562 if (ValueSet) ValueSet->erase(DeadInst);
Dan Gohman28943872010-01-05 16:27:25 +0000563 } while (!NowDeadInsts.empty());
Owen Anderson5e72db32007-07-11 00:46:18 +0000564}
Nick Lewycky475d3d12010-01-03 04:39:07 +0000565
566unsigned DSE::getPointerSize(Value *V) const {
567 if (TD) {
568 if (AllocaInst *A = dyn_cast<AllocaInst>(V)) {
569 // Get size information for the alloca
570 if (ConstantInt *C = dyn_cast<ConstantInt>(A->getArraySize()))
571 return C->getZExtValue() * TD->getTypeAllocSize(A->getAllocatedType());
572 } else {
573 assert(isa<Argument>(V) && "Expected AllocaInst or Argument!");
574 const PointerType *PT = cast<PointerType>(V->getType());
575 return TD->getTypeAllocSize(PT->getElementType());
576 }
577 }
578 return ~0U;
579}