blob: 60b12fd8679ed92a2b172a188f5a398ae08f044e [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
Dan Gohmana79db302008-09-04 17:05:41 +000043 DSE() : FunctionPass(&ID) {}
Owen Anderson5e72db32007-07-11 00:46:18 +000044
45 virtual bool runOnFunction(Function &F) {
46 bool Changed = false;
47 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
48 Changed |= runOnBasicBlock(*I);
49 return Changed;
50 }
Chris Lattnerde04e112008-11-29 01:43:36 +000051
Owen Anderson5e72db32007-07-11 00:46:18 +000052 bool runOnBasicBlock(BasicBlock &BB);
Victor Hernandeze2971492009-10-24 04:23:03 +000053 bool handleFreeWithNonTrivialDependency(Instruction *F, MemDepResult Dep);
Chris Lattner1adb6752008-11-28 00:27:14 +000054 bool handleEndBlock(BasicBlock &BB);
Chris Lattner57e91ea2008-12-06 00:53:22 +000055 bool RemoveUndeadPointers(Value* Ptr, uint64_t killPointerSize,
Owen Anderson32c4a052007-07-12 21:41:30 +000056 BasicBlock::iterator& BBI,
Chris Lattner1adb6752008-11-28 00:27:14 +000057 SmallPtrSet<Value*, 64>& deadPointers);
58 void DeleteDeadInstruction(Instruction *I,
59 SmallPtrSet<Value*, 64> *deadPointers = 0);
60
Owen Anderson5e72db32007-07-11 00:46:18 +000061
62 // getAnalysisUsage - We require post dominance frontiers (aka Control
63 // Dependence Graph)
64 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
65 AU.setPreservesCFG();
Owen Anderson3f338972008-07-28 16:14:26 +000066 AU.addRequired<DominatorTree>();
Owen Andersonaa071722007-07-11 23:19:17 +000067 AU.addRequired<AliasAnalysis>();
Owen Anderson5e72db32007-07-11 00:46:18 +000068 AU.addRequired<MemoryDependenceAnalysis>();
Owen Anderson3f338972008-07-28 16:14:26 +000069 AU.addPreserved<DominatorTree>();
Owen Andersonaa071722007-07-11 23:19:17 +000070 AU.addPreserved<AliasAnalysis>();
Owen Anderson5e72db32007-07-11 00:46:18 +000071 AU.addPreserved<MemoryDependenceAnalysis>();
72 }
73 };
Owen Anderson5e72db32007-07-11 00:46:18 +000074}
75
Dan Gohmand78c4002008-05-13 00:00:25 +000076char DSE::ID = 0;
77static RegisterPass<DSE> X("dse", "Dead Store Elimination");
78
Owen Anderson10e52ed2007-08-01 06:36:51 +000079FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
Owen Anderson5e72db32007-07-11 00:46:18 +000080
Owen Anderson10e52ed2007-08-01 06:36:51 +000081bool DSE::runOnBasicBlock(BasicBlock &BB) {
Owen Anderson5e72db32007-07-11 00:46:18 +000082 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
Dan Gohman67243a42009-07-24 18:13:53 +000083 TD = getAnalysisIfAvailable<TargetData>();
Owen Anderson2ed651a2007-11-01 05:29:16 +000084
Owen Anderson5e72db32007-07-11 00:46:18 +000085 bool MadeChange = false;
86
Chris Lattner49162672009-09-02 06:31:02 +000087 // Do a top-down walk on the BB.
Chris Lattnerf2a8ba42008-11-28 21:29:52 +000088 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
89 Instruction *Inst = BBI++;
90
Dan Gohman67243a42009-07-24 18:13:53 +000091 // If we find a store or a free, get its memory dependence.
Victor Hernandezde5ad422009-10-26 23:43:48 +000092 if (!isa<StoreInst>(Inst) && !isFreeCall(Inst))
Owen Anderson0aecf0e2007-08-08 04:52:29 +000093 continue;
Chris Lattner5df5b4c2008-12-07 00:25:15 +000094
95 // Don't molest volatile stores or do queries that will return "clobber".
96 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
97 if (SI->isVolatile())
98 continue;
Duncan Sandsd65a4da2008-10-01 15:25:41 +000099
Chris Lattner57e91ea2008-12-06 00:53:22 +0000100 MemDepResult InstDep = MD.getDependency(Inst);
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000101
Chris Lattner57e91ea2008-12-06 00:53:22 +0000102 // Ignore non-local stores.
103 // FIXME: cross-block DSE would be fun. :)
104 if (InstDep.isNonLocal()) continue;
105
106 // Handle frees whose dependencies are non-trivial.
Victor Hernandezde5ad422009-10-26 23:43:48 +0000107 if (isFreeCall(Inst)) {
Victor Hernandeze2971492009-10-24 04:23:03 +0000108 MadeChange |= handleFreeWithNonTrivialDependency(Inst, InstDep);
Chris Lattner57e91ea2008-12-06 00:53:22 +0000109 continue;
110 }
111
112 StoreInst *SI = cast<StoreInst>(Inst);
113
114 // If not a definite must-alias dependency, ignore it.
115 if (!InstDep.isDef())
116 continue;
117
118 // If this is a store-store dependence, then the previous store is dead so
119 // long as this store is at least as big as it.
120 if (StoreInst *DepStore = dyn_cast<StoreInst>(InstDep.getInst()))
Dan Gohman0b5be942009-07-24 18:31:07 +0000121 if (TD &&
Dan Gohman67243a42009-07-24 18:13:53 +0000122 TD->getTypeStoreSize(DepStore->getOperand(0)->getType()) <=
123 TD->getTypeStoreSize(SI->getOperand(0)->getType())) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000124 // Delete the store and now-dead instructions that feed it.
Chris Lattner57e91ea2008-12-06 00:53:22 +0000125 DeleteDeadInstruction(DepStore);
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000126 NumFastStores++;
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000127 MadeChange = true;
Chris Lattner49162672009-09-02 06:31:02 +0000128
129 // DeleteDeadInstruction can delete the current instruction in loop
130 // cases, reset BBI.
131 BBI = Inst;
Chris Lattner8c5ff512008-11-29 20:29:04 +0000132 if (BBI != BB.begin())
Chris Lattnerf3f6a802008-11-28 22:50:08 +0000133 --BBI;
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000134 continue;
135 }
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000136
Chris Lattner57e91ea2008-12-06 00:53:22 +0000137 // If we're storing the same value back to a pointer that we just
138 // loaded from, then the store can be removed.
139 if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
140 if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
141 SI->getOperand(0) == DepLoad) {
Chris Lattner49162672009-09-02 06:31:02 +0000142 // DeleteDeadInstruction can delete the current instruction. Save BBI
143 // in case we need it.
144 WeakVH NextInst(BBI);
145
Chris Lattner57e91ea2008-12-06 00:53:22 +0000146 DeleteDeadInstruction(SI);
Chris Lattner49162672009-09-02 06:31:02 +0000147
148 if (NextInst == 0) // Next instruction deleted.
149 BBI = BB.begin();
150 else if (BBI != BB.begin()) // Revisit this instruction if possible.
Chris Lattner57e91ea2008-12-06 00:53:22 +0000151 --BBI;
152 NumFastStores++;
153 MadeChange = true;
154 continue;
Chris Lattner0e3d6332008-12-05 21:04:20 +0000155 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000156 }
Owen Anderson2b2bd282009-10-28 07:05:35 +0000157
158 // If this is a lifetime end marker, we can throw away the store.
159 if (IntrinsicInst* II = dyn_cast<IntrinsicInst>(InstDep.getInst())) {
160 if (II->getIntrinsicID() == Intrinsic::lifetime_end) {
161 // Delete the store and now-dead instructions that feed it.
162 // DeleteDeadInstruction can delete the current instruction. Save BBI
163 // in case we need it.
164 WeakVH NextInst(BBI);
165
166 DeleteDeadInstruction(SI);
167
168 if (NextInst == 0) // Next instruction deleted.
169 BBI = BB.begin();
170 else if (BBI != BB.begin()) // Revisit this instruction if possible.
171 --BBI;
172 NumFastStores++;
173 MadeChange = true;
174 continue;
175 }
176 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000177 }
178
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000179 // If this block ends in a return, unwind, or unreachable, all allocas are
180 // dead at its end, which means stores to them are also dead.
Owen Anderson32c4a052007-07-12 21:41:30 +0000181 if (BB.getTerminator()->getNumSuccessors() == 0)
Chris Lattner1adb6752008-11-28 00:27:14 +0000182 MadeChange |= handleEndBlock(BB);
Owen Anderson5e72db32007-07-11 00:46:18 +0000183
184 return MadeChange;
185}
186
Owen Andersonaa071722007-07-11 23:19:17 +0000187/// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
Chris Lattner1adb6752008-11-28 00:27:14 +0000188/// dependency is a store to a field of that structure.
Victor Hernandeze2971492009-10-24 04:23:03 +0000189bool DSE::handleFreeWithNonTrivialDependency(Instruction *F, MemDepResult Dep) {
Owen Andersonaa071722007-07-11 23:19:17 +0000190 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Andersonaa071722007-07-11 23:19:17 +0000191
Chris Lattner57e91ea2008-12-06 00:53:22 +0000192 StoreInst *Dependency = dyn_cast_or_null<StoreInst>(Dep.getInst());
193 if (!Dependency || Dependency->isVolatile())
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000194 return false;
Owen Andersond4451de2007-07-12 18:08:51 +0000195
Chris Lattner57e91ea2008-12-06 00:53:22 +0000196 Value *DepPointer = Dependency->getPointerOperand()->getUnderlyingObject();
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000197
Chris Lattner57e91ea2008-12-06 00:53:22 +0000198 // Check for aliasing.
Victor Hernandezde5ad422009-10-26 23:43:48 +0000199 if (AA.alias(F->getOperand(1), 1, DepPointer, 1) !=
Chris Lattner57e91ea2008-12-06 00:53:22 +0000200 AliasAnalysis::MustAlias)
Chris Lattner1adb6752008-11-28 00:27:14 +0000201 return false;
Owen Andersonaa071722007-07-11 23:19:17 +0000202
Chris Lattner1adb6752008-11-28 00:27:14 +0000203 // DCE instructions only used to calculate that store
Chris Lattner57e91ea2008-12-06 00:53:22 +0000204 DeleteDeadInstruction(Dependency);
Chris Lattner1adb6752008-11-28 00:27:14 +0000205 NumFastStores++;
206 return true;
Owen Andersonaa071722007-07-11 23:19:17 +0000207}
208
Owen Andersone3590582007-08-02 18:11:11 +0000209/// handleEndBlock - Remove dead stores to stack-allocated locations in the
Owen Anderson52aaabf2007-08-08 17:50:09 +0000210/// function end block. Ex:
211/// %A = alloca i32
212/// ...
213/// store i32 1, i32* %A
214/// ret void
Chris Lattner1adb6752008-11-28 00:27:14 +0000215bool DSE::handleEndBlock(BasicBlock &BB) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000216 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000217
218 bool MadeChange = false;
219
220 // Pointers alloca'd in this function are dead in the end block
Owen Anderson48d37802008-01-29 06:18:36 +0000221 SmallPtrSet<Value*, 64> deadPointers;
Owen Anderson32c4a052007-07-12 21:41:30 +0000222
Chris Lattner1adb6752008-11-28 00:27:14 +0000223 // Find all of the alloca'd pointers in the entry block.
Owen Anderson32c4a052007-07-12 21:41:30 +0000224 BasicBlock *Entry = BB.getParent()->begin();
225 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
226 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
227 deadPointers.insert(AI);
Chris Lattner1adb6752008-11-28 00:27:14 +0000228
229 // Treat byval arguments the same, stores to them are dead at the end of the
230 // function.
Owen Anderson48d37802008-01-29 06:18:36 +0000231 for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
232 AE = BB.getParent()->arg_end(); AI != AE; ++AI)
233 if (AI->hasByValAttr())
234 deadPointers.insert(AI);
Owen Anderson32c4a052007-07-12 21:41:30 +0000235
236 // Scan the basic block backwards
237 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
238 --BBI;
239
Chris Lattner1adb6752008-11-28 00:27:14 +0000240 // If we find a store whose pointer is dead.
Owen Anderson32c4a052007-07-12 21:41:30 +0000241 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000242 if (!S->isVolatile()) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000243 // See through pointer-to-pointer bitcasts
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000244 Value* pointerOperand = S->getPointerOperand()->getUnderlyingObject();
245
Owen Anderson6af19fd2008-01-25 10:10:33 +0000246 // Alloca'd pointers or byval arguments (which are functionally like
247 // alloca's) are valid candidates for removal.
Owen Anderson48d37802008-01-29 06:18:36 +0000248 if (deadPointers.count(pointerOperand)) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000249 // DCE instructions only used to calculate that store.
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000250 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000251 DeleteDeadInstruction(S, &deadPointers);
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000252 NumFastStores++;
253 MadeChange = true;
254 }
Owen Anderson32c4a052007-07-12 21:41:30 +0000255 }
Owen Anderson52aaabf2007-08-08 17:50:09 +0000256
257 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000258 }
Owen Anderson48d37802008-01-29 06:18:36 +0000259
Chris Lattner1adb6752008-11-28 00:27:14 +0000260 // We can also remove memcpy's to local variables at the end of a function.
261 if (MemCpyInst *M = dyn_cast<MemCpyInst>(BBI)) {
262 Value *dest = M->getDest()->getUnderlyingObject();
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000263
Owen Anderson48d37802008-01-29 06:18:36 +0000264 if (deadPointers.count(dest)) {
Owen Anderson48d37802008-01-29 06:18:36 +0000265 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000266 DeleteDeadInstruction(M, &deadPointers);
Owen Anderson48d37802008-01-29 06:18:36 +0000267 NumFastOther++;
268 MadeChange = true;
Owen Anderson48d37802008-01-29 06:18:36 +0000269 continue;
270 }
271
Chris Lattner1adb6752008-11-28 00:27:14 +0000272 // Because a memcpy is also a load, we can't skip it if we didn't remove
273 // it.
Owen Anderson52aaabf2007-08-08 17:50:09 +0000274 }
275
276 Value* killPointer = 0;
Owen Andersona82c9932008-02-04 04:53:00 +0000277 uint64_t killPointerSize = ~0UL;
Owen Anderson32c4a052007-07-12 21:41:30 +0000278
279 // If we encounter a use of the pointer, it is no longer considered dead
Chris Lattner1adb6752008-11-28 00:27:14 +0000280 if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
Nate Begeman53c5c622008-05-13 01:48:26 +0000281 // However, if this load is unused and not volatile, we can go ahead and
282 // remove it, and not have to worry about it making our pointer undead!
Dan Gohman8cb19d92008-04-28 19:51:27 +0000283 if (L->use_empty() && !L->isVolatile()) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000284 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000285 DeleteDeadInstruction(L, &deadPointers);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000286 NumFastOther++;
287 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000288 continue;
289 }
290
Owen Anderson32c4a052007-07-12 21:41:30 +0000291 killPointer = L->getPointerOperand();
Owen Anderson32c4a052007-07-12 21:41:30 +0000292 } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
293 killPointer = V->getOperand(0);
Owen Andersona82c9932008-02-04 04:53:00 +0000294 } else if (isa<MemCpyInst>(BBI) &&
295 isa<ConstantInt>(cast<MemCpyInst>(BBI)->getLength())) {
296 killPointer = cast<MemCpyInst>(BBI)->getSource();
297 killPointerSize = cast<ConstantInt>(
298 cast<MemCpyInst>(BBI)->getLength())->getZExtValue();
Owen Anderson32c4a052007-07-12 21:41:30 +0000299 } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
300 deadPointers.erase(A);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000301
302 // Dead alloca's can be DCE'd when we reach them
Nick Lewycky6b016702008-01-30 08:01:28 +0000303 if (A->use_empty()) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000304 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000305 DeleteDeadInstruction(A, &deadPointers);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000306 NumFastOther++;
307 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000308 }
309
Owen Anderson32c4a052007-07-12 21:41:30 +0000310 continue;
311 } else if (CallSite::get(BBI).getInstruction() != 0) {
Owen Anderson50df9682007-08-08 17:58:56 +0000312 // If this call does not access memory, it can't
313 // be undeadifying any of our pointers.
314 CallSite CS = CallSite::get(BBI);
Duncan Sands68b6f502007-12-01 07:51:45 +0000315 if (AA.doesNotAccessMemory(CS))
Owen Anderson50df9682007-08-08 17:58:56 +0000316 continue;
317
Owen Andersonddf4aee2007-08-08 18:38:28 +0000318 unsigned modRef = 0;
319 unsigned other = 0;
320
Owen Anderson32c4a052007-07-12 21:41:30 +0000321 // Remove any pointers made undead by the call from the dead set
Owen Anderson48d37802008-01-29 06:18:36 +0000322 std::vector<Value*> dead;
323 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000324 E = deadPointers.end(); I != E; ++I) {
Owen Andersonddf4aee2007-08-08 18:38:28 +0000325 // HACK: if we detect that our AA is imprecise, it's not
326 // worth it to scan the rest of the deadPointers set. Just
327 // assume that the AA will return ModRef for everything, and
328 // go ahead and bail.
329 if (modRef >= 16 && other == 0) {
330 deadPointers.clear();
331 return MadeChange;
332 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000333
Owen Anderson32c4a052007-07-12 21:41:30 +0000334 // Get size information for the alloca
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000335 unsigned pointerSize = ~0U;
Dan Gohman67243a42009-07-24 18:13:53 +0000336 if (TD) {
337 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
338 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
339 pointerSize = C->getZExtValue() *
340 TD->getTypeAllocSize(A->getAllocatedType());
341 } else {
342 const PointerType* PT = cast<PointerType>(
343 cast<Argument>(*I)->getType());
344 pointerSize = TD->getTypeAllocSize(PT->getElementType());
345 }
Owen Anderson48d37802008-01-29 06:18:36 +0000346 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000347
Owen Anderson32c4a052007-07-12 21:41:30 +0000348 // See if the call site touches it
Owen Anderson50df9682007-08-08 17:58:56 +0000349 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000350
351 if (A == AliasAnalysis::ModRef)
352 modRef++;
353 else
354 other++;
355
Owen Anderson9c9ef212007-07-13 18:26:26 +0000356 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
Owen Anderson32c4a052007-07-12 21:41:30 +0000357 dead.push_back(*I);
358 }
359
Owen Anderson48d37802008-01-29 06:18:36 +0000360 for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000361 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000362 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000363
364 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000365 } else if (isInstructionTriviallyDead(BBI)) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000366 // For any non-memory-affecting non-terminators, DCE them as we reach them
Chris Lattner1adb6752008-11-28 00:27:14 +0000367 Instruction *Inst = BBI;
368 BBI++;
369 DeleteDeadInstruction(Inst, &deadPointers);
370 NumFastOther++;
371 MadeChange = true;
372 continue;
Owen Anderson32c4a052007-07-12 21:41:30 +0000373 }
374
375 if (!killPointer)
376 continue;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000377
378 killPointer = killPointer->getUnderlyingObject();
379
Owen Anderson32c4a052007-07-12 21:41:30 +0000380 // Deal with undead pointers
Owen Andersona82c9932008-02-04 04:53:00 +0000381 MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
Chris Lattner1adb6752008-11-28 00:27:14 +0000382 deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000383 }
384
385 return MadeChange;
386}
387
Owen Andersonddf4aee2007-08-08 18:38:28 +0000388/// RemoveUndeadPointers - check for uses of a pointer that make it
389/// undead when scanning for dead stores to alloca's.
Owen Andersona82c9932008-02-04 04:53:00 +0000390bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize,
Chris Lattner1adb6752008-11-28 00:27:14 +0000391 BasicBlock::iterator &BBI,
392 SmallPtrSet<Value*, 64>& deadPointers) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000393 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000394
Owen Andersonddf4aee2007-08-08 18:38:28 +0000395 // If the kill pointer can be easily reduced to an alloca,
Chris Lattner1adb6752008-11-28 00:27:14 +0000396 // don't bother doing extraneous AA queries.
Owen Anderson48d37802008-01-29 06:18:36 +0000397 if (deadPointers.count(killPointer)) {
398 deadPointers.erase(killPointer);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000399 return false;
400 }
401
Chris Lattner1adb6752008-11-28 00:27:14 +0000402 // A global can't be in the dead pointer set.
403 if (isa<GlobalValue>(killPointer))
404 return false;
405
Owen Anderson32c4a052007-07-12 21:41:30 +0000406 bool MadeChange = false;
407
Chris Lattner1adb6752008-11-28 00:27:14 +0000408 SmallVector<Value*, 16> undead;
Owen Anderson32c4a052007-07-12 21:41:30 +0000409
Owen Anderson48d37802008-01-29 06:18:36 +0000410 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000411 E = deadPointers.end(); I != E; ++I) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000412 // Get size information for the alloca.
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000413 unsigned pointerSize = ~0U;
Dan Gohman67243a42009-07-24 18:13:53 +0000414 if (TD) {
415 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
416 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
417 pointerSize = C->getZExtValue() *
418 TD->getTypeAllocSize(A->getAllocatedType());
419 } else {
420 const PointerType* PT = cast<PointerType>(cast<Argument>(*I)->getType());
421 pointerSize = TD->getTypeAllocSize(PT->getElementType());
422 }
Owen Anderson48d37802008-01-29 06:18:36 +0000423 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000424
Owen Anderson32c4a052007-07-12 21:41:30 +0000425 // See if this pointer could alias it
Owen Andersone3590582007-08-02 18:11:11 +0000426 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
Owen Andersona82c9932008-02-04 04:53:00 +0000427 killPointer, killPointerSize);
Owen Anderson32c4a052007-07-12 21:41:30 +0000428
429 // If it must-alias and a store, we can delete it
430 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
431 StoreInst* S = cast<StoreInst>(BBI);
432
433 // Remove it!
Owen Anderson32c4a052007-07-12 21:41:30 +0000434 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000435 DeleteDeadInstruction(S, &deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000436 NumFastStores++;
437 MadeChange = true;
438
439 continue;
440
441 // Otherwise, it is undead
Chris Lattner1adb6752008-11-28 00:27:14 +0000442 } else if (A != AliasAnalysis::NoAlias)
443 undead.push_back(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000444 }
445
Chris Lattner1adb6752008-11-28 00:27:14 +0000446 for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000447 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000448 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000449
450 return MadeChange;
451}
452
Chris Lattner1adb6752008-11-28 00:27:14 +0000453/// DeleteDeadInstruction - Delete this instruction. Before we do, go through
454/// and zero out all the operands of this instruction. If any of them become
455/// dead, delete them and the computation tree that feeds them.
456///
457/// If ValueSet is non-null, remove any deleted instructions from it as well.
458///
459void DSE::DeleteDeadInstruction(Instruction *I,
460 SmallPtrSet<Value*, 64> *ValueSet) {
461 SmallVector<Instruction*, 32> NowDeadInsts;
462
463 NowDeadInsts.push_back(I);
464 --NumFastOther;
Owen Anderson5e72db32007-07-11 00:46:18 +0000465
Chris Lattner1adb6752008-11-28 00:27:14 +0000466 // Before we touch this instruction, remove it from memdep!
467 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
468 while (!NowDeadInsts.empty()) {
469 Instruction *DeadInst = NowDeadInsts.back();
470 NowDeadInsts.pop_back();
Owen Andersonbf971aa2007-07-11 19:03:09 +0000471
Chris Lattner1adb6752008-11-28 00:27:14 +0000472 ++NumFastOther;
473
474 // This instruction is dead, zap it, in stages. Start by removing it from
475 // MemDep, which needs to know the operands and needs it to be in the
476 // function.
477 MDA.removeInstruction(DeadInst);
478
479 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
480 Value *Op = DeadInst->getOperand(op);
481 DeadInst->setOperand(op, 0);
482
483 // If this operand just became dead, add it to the NowDeadInsts list.
484 if (!Op->use_empty()) continue;
485
486 if (Instruction *OpI = dyn_cast<Instruction>(Op))
487 if (isInstructionTriviallyDead(OpI))
488 NowDeadInsts.push_back(OpI);
489 }
490
491 DeadInst->eraseFromParent();
492
493 if (ValueSet) ValueSet->erase(DeadInst);
Owen Anderson5e72db32007-07-11 00:46:18 +0000494 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000495}