blob: a7b3e7524fa2bf1a4c514cdac03910d6a36d82ba [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"
Owen Anderson5e72db32007-07-11 00:46:18 +000029#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Owen Andersonaa071722007-07-11 23:19:17 +000030#include "llvm/Target/TargetData.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000031#include "llvm/Transforms/Utils/Local.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000032using namespace llvm;
33
34STATISTIC(NumFastStores, "Number of stores deleted");
35STATISTIC(NumFastOther , "Number of other instrs removed");
36
37namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +000038 struct DSE : public FunctionPass {
Dan Gohman67243a42009-07-24 18:13:53 +000039 TargetData *TD;
40
Owen Anderson5e72db32007-07-11 00:46:18 +000041 static char ID; // Pass identification, replacement for typeid
Dan Gohmana79db302008-09-04 17:05:41 +000042 DSE() : FunctionPass(&ID) {}
Owen Anderson5e72db32007-07-11 00:46:18 +000043
44 virtual bool runOnFunction(Function &F) {
45 bool Changed = false;
46 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
47 Changed |= runOnBasicBlock(*I);
48 return Changed;
49 }
Chris Lattnerde04e112008-11-29 01:43:36 +000050
Owen Anderson5e72db32007-07-11 00:46:18 +000051 bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner7f9c8a02008-11-29 02:29:27 +000052 bool handleFreeWithNonTrivialDependency(FreeInst *F, MemDepResult Dep);
Chris Lattner1adb6752008-11-28 00:27:14 +000053 bool handleEndBlock(BasicBlock &BB);
Chris Lattner57e91ea2008-12-06 00:53:22 +000054 bool RemoveUndeadPointers(Value* Ptr, uint64_t killPointerSize,
Owen Anderson32c4a052007-07-12 21:41:30 +000055 BasicBlock::iterator& BBI,
Chris Lattner1adb6752008-11-28 00:27:14 +000056 SmallPtrSet<Value*, 64>& deadPointers);
57 void DeleteDeadInstruction(Instruction *I,
58 SmallPtrSet<Value*, 64> *deadPointers = 0);
59
Owen Anderson5e72db32007-07-11 00:46:18 +000060
61 // getAnalysisUsage - We require post dominance frontiers (aka Control
62 // Dependence Graph)
63 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.setPreservesCFG();
Owen Anderson3f338972008-07-28 16:14:26 +000065 AU.addRequired<DominatorTree>();
Owen Andersonaa071722007-07-11 23:19:17 +000066 AU.addRequired<AliasAnalysis>();
Owen Anderson5e72db32007-07-11 00:46:18 +000067 AU.addRequired<MemoryDependenceAnalysis>();
Owen Anderson3f338972008-07-28 16:14:26 +000068 AU.addPreserved<DominatorTree>();
Owen Andersonaa071722007-07-11 23:19:17 +000069 AU.addPreserved<AliasAnalysis>();
Owen Anderson5e72db32007-07-11 00:46:18 +000070 AU.addPreserved<MemoryDependenceAnalysis>();
71 }
72 };
Owen Anderson5e72db32007-07-11 00:46:18 +000073}
74
Dan Gohmand78c4002008-05-13 00:00:25 +000075char DSE::ID = 0;
76static RegisterPass<DSE> X("dse", "Dead Store Elimination");
77
Owen Anderson10e52ed2007-08-01 06:36:51 +000078FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
Owen Anderson5e72db32007-07-11 00:46:18 +000079
Owen Anderson10e52ed2007-08-01 06:36:51 +000080bool DSE::runOnBasicBlock(BasicBlock &BB) {
Owen Anderson5e72db32007-07-11 00:46:18 +000081 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
Dan Gohman67243a42009-07-24 18:13:53 +000082 TD = getAnalysisIfAvailable<TargetData>();
Owen Anderson2ed651a2007-11-01 05:29:16 +000083
Owen Anderson5e72db32007-07-11 00:46:18 +000084 bool MadeChange = false;
85
Chris Lattner49162672009-09-02 06:31:02 +000086 // Do a top-down walk on the BB.
Chris Lattnerf2a8ba42008-11-28 21:29:52 +000087 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
88 Instruction *Inst = BBI++;
89
Dan Gohman67243a42009-07-24 18:13:53 +000090 // If we find a store or a free, get its memory dependence.
Chris Lattnerf2a8ba42008-11-28 21:29:52 +000091 if (!isa<StoreInst>(Inst) && !isa<FreeInst>(Inst))
Owen Anderson0aecf0e2007-08-08 04:52:29 +000092 continue;
Chris Lattner5df5b4c2008-12-07 00:25:15 +000093
94 // Don't molest volatile stores or do queries that will return "clobber".
95 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
96 if (SI->isVolatile())
97 continue;
Duncan Sandsd65a4da2008-10-01 15:25:41 +000098
Chris Lattner57e91ea2008-12-06 00:53:22 +000099 MemDepResult InstDep = MD.getDependency(Inst);
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000100
Chris Lattner57e91ea2008-12-06 00:53:22 +0000101 // Ignore non-local stores.
102 // FIXME: cross-block DSE would be fun. :)
103 if (InstDep.isNonLocal()) continue;
104
105 // Handle frees whose dependencies are non-trivial.
106 if (FreeInst *FI = dyn_cast<FreeInst>(Inst)) {
107 MadeChange |= handleFreeWithNonTrivialDependency(FI, InstDep);
108 continue;
109 }
110
111 StoreInst *SI = cast<StoreInst>(Inst);
112
113 // If not a definite must-alias dependency, ignore it.
114 if (!InstDep.isDef())
115 continue;
116
117 // If this is a store-store dependence, then the previous store is dead so
118 // long as this store is at least as big as it.
119 if (StoreInst *DepStore = dyn_cast<StoreInst>(InstDep.getInst()))
Dan Gohman0b5be942009-07-24 18:31:07 +0000120 if (TD &&
Dan Gohman67243a42009-07-24 18:13:53 +0000121 TD->getTypeStoreSize(DepStore->getOperand(0)->getType()) <=
122 TD->getTypeStoreSize(SI->getOperand(0)->getType())) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000123 // Delete the store and now-dead instructions that feed it.
Chris Lattner57e91ea2008-12-06 00:53:22 +0000124 DeleteDeadInstruction(DepStore);
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000125 NumFastStores++;
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000126 MadeChange = true;
Chris Lattner49162672009-09-02 06:31:02 +0000127
128 // DeleteDeadInstruction can delete the current instruction in loop
129 // cases, reset BBI.
130 BBI = Inst;
Chris Lattner8c5ff512008-11-29 20:29:04 +0000131 if (BBI != BB.begin())
Chris Lattnerf3f6a802008-11-28 22:50:08 +0000132 --BBI;
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000133 continue;
134 }
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000135
Chris Lattner57e91ea2008-12-06 00:53:22 +0000136 // If we're storing the same value back to a pointer that we just
137 // loaded from, then the store can be removed.
138 if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
139 if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
140 SI->getOperand(0) == DepLoad) {
Chris Lattner49162672009-09-02 06:31:02 +0000141 // DeleteDeadInstruction can delete the current instruction. Save BBI
142 // in case we need it.
143 WeakVH NextInst(BBI);
144
Chris Lattner57e91ea2008-12-06 00:53:22 +0000145 DeleteDeadInstruction(SI);
Chris Lattner49162672009-09-02 06:31:02 +0000146
147 if (NextInst == 0) // Next instruction deleted.
148 BBI = BB.begin();
149 else if (BBI != BB.begin()) // Revisit this instruction if possible.
Chris Lattner57e91ea2008-12-06 00:53:22 +0000150 --BBI;
151 NumFastStores++;
152 MadeChange = true;
153 continue;
Chris Lattner0e3d6332008-12-05 21:04:20 +0000154 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000155 }
156 }
157
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000158 // If this block ends in a return, unwind, or unreachable, all allocas are
159 // dead at its end, which means stores to them are also dead.
Owen Anderson32c4a052007-07-12 21:41:30 +0000160 if (BB.getTerminator()->getNumSuccessors() == 0)
Chris Lattner1adb6752008-11-28 00:27:14 +0000161 MadeChange |= handleEndBlock(BB);
Owen Anderson5e72db32007-07-11 00:46:18 +0000162
163 return MadeChange;
164}
165
Owen Andersonaa071722007-07-11 23:19:17 +0000166/// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
Chris Lattner1adb6752008-11-28 00:27:14 +0000167/// dependency is a store to a field of that structure.
Chris Lattner57e91ea2008-12-06 00:53:22 +0000168bool DSE::handleFreeWithNonTrivialDependency(FreeInst *F, MemDepResult Dep) {
Owen Andersonaa071722007-07-11 23:19:17 +0000169 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Andersonaa071722007-07-11 23:19:17 +0000170
Chris Lattner57e91ea2008-12-06 00:53:22 +0000171 StoreInst *Dependency = dyn_cast_or_null<StoreInst>(Dep.getInst());
172 if (!Dependency || Dependency->isVolatile())
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000173 return false;
Owen Andersond4451de2007-07-12 18:08:51 +0000174
Chris Lattner57e91ea2008-12-06 00:53:22 +0000175 Value *DepPointer = Dependency->getPointerOperand()->getUnderlyingObject();
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000176
Chris Lattner57e91ea2008-12-06 00:53:22 +0000177 // Check for aliasing.
178 if (AA.alias(F->getPointerOperand(), 1, DepPointer, 1) !=
179 AliasAnalysis::MustAlias)
Chris Lattner1adb6752008-11-28 00:27:14 +0000180 return false;
Owen Andersonaa071722007-07-11 23:19:17 +0000181
Chris Lattner1adb6752008-11-28 00:27:14 +0000182 // DCE instructions only used to calculate that store
Chris Lattner57e91ea2008-12-06 00:53:22 +0000183 DeleteDeadInstruction(Dependency);
Chris Lattner1adb6752008-11-28 00:27:14 +0000184 NumFastStores++;
185 return true;
Owen Andersonaa071722007-07-11 23:19:17 +0000186}
187
Owen Andersone3590582007-08-02 18:11:11 +0000188/// handleEndBlock - Remove dead stores to stack-allocated locations in the
Owen Anderson52aaabf2007-08-08 17:50:09 +0000189/// function end block. Ex:
190/// %A = alloca i32
191/// ...
192/// store i32 1, i32* %A
193/// ret void
Chris Lattner1adb6752008-11-28 00:27:14 +0000194bool DSE::handleEndBlock(BasicBlock &BB) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000195 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000196
197 bool MadeChange = false;
198
199 // Pointers alloca'd in this function are dead in the end block
Owen Anderson48d37802008-01-29 06:18:36 +0000200 SmallPtrSet<Value*, 64> deadPointers;
Owen Anderson32c4a052007-07-12 21:41:30 +0000201
Chris Lattner1adb6752008-11-28 00:27:14 +0000202 // Find all of the alloca'd pointers in the entry block.
Owen Anderson32c4a052007-07-12 21:41:30 +0000203 BasicBlock *Entry = BB.getParent()->begin();
204 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
205 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
206 deadPointers.insert(AI);
Chris Lattner1adb6752008-11-28 00:27:14 +0000207
208 // Treat byval arguments the same, stores to them are dead at the end of the
209 // function.
Owen Anderson48d37802008-01-29 06:18:36 +0000210 for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
211 AE = BB.getParent()->arg_end(); AI != AE; ++AI)
212 if (AI->hasByValAttr())
213 deadPointers.insert(AI);
Owen Anderson32c4a052007-07-12 21:41:30 +0000214
215 // Scan the basic block backwards
216 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
217 --BBI;
218
Chris Lattner1adb6752008-11-28 00:27:14 +0000219 // If we find a store whose pointer is dead.
Owen Anderson32c4a052007-07-12 21:41:30 +0000220 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000221 if (!S->isVolatile()) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000222 // See through pointer-to-pointer bitcasts
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000223 Value* pointerOperand = S->getPointerOperand()->getUnderlyingObject();
224
Owen Anderson6af19fd2008-01-25 10:10:33 +0000225 // Alloca'd pointers or byval arguments (which are functionally like
226 // alloca's) are valid candidates for removal.
Owen Anderson48d37802008-01-29 06:18:36 +0000227 if (deadPointers.count(pointerOperand)) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000228 // DCE instructions only used to calculate that store.
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000229 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000230 DeleteDeadInstruction(S, &deadPointers);
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000231 NumFastStores++;
232 MadeChange = true;
233 }
Owen Anderson32c4a052007-07-12 21:41:30 +0000234 }
Owen Anderson52aaabf2007-08-08 17:50:09 +0000235
236 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000237 }
Owen Anderson48d37802008-01-29 06:18:36 +0000238
Chris Lattner1adb6752008-11-28 00:27:14 +0000239 // We can also remove memcpy's to local variables at the end of a function.
240 if (MemCpyInst *M = dyn_cast<MemCpyInst>(BBI)) {
241 Value *dest = M->getDest()->getUnderlyingObject();
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000242
Owen Anderson48d37802008-01-29 06:18:36 +0000243 if (deadPointers.count(dest)) {
Owen Anderson48d37802008-01-29 06:18:36 +0000244 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000245 DeleteDeadInstruction(M, &deadPointers);
Owen Anderson48d37802008-01-29 06:18:36 +0000246 NumFastOther++;
247 MadeChange = true;
Owen Anderson48d37802008-01-29 06:18:36 +0000248 continue;
249 }
250
Chris Lattner1adb6752008-11-28 00:27:14 +0000251 // Because a memcpy is also a load, we can't skip it if we didn't remove
252 // it.
Owen Anderson52aaabf2007-08-08 17:50:09 +0000253 }
254
255 Value* killPointer = 0;
Owen Andersona82c9932008-02-04 04:53:00 +0000256 uint64_t killPointerSize = ~0UL;
Owen Anderson32c4a052007-07-12 21:41:30 +0000257
258 // If we encounter a use of the pointer, it is no longer considered dead
Chris Lattner1adb6752008-11-28 00:27:14 +0000259 if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
Nate Begeman53c5c622008-05-13 01:48:26 +0000260 // However, if this load is unused and not volatile, we can go ahead and
261 // remove it, and not have to worry about it making our pointer undead!
Dan Gohman8cb19d92008-04-28 19:51:27 +0000262 if (L->use_empty() && !L->isVolatile()) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000263 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000264 DeleteDeadInstruction(L, &deadPointers);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000265 NumFastOther++;
266 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000267 continue;
268 }
269
Owen Anderson32c4a052007-07-12 21:41:30 +0000270 killPointer = L->getPointerOperand();
Owen Anderson32c4a052007-07-12 21:41:30 +0000271 } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
272 killPointer = V->getOperand(0);
Owen Andersona82c9932008-02-04 04:53:00 +0000273 } else if (isa<MemCpyInst>(BBI) &&
274 isa<ConstantInt>(cast<MemCpyInst>(BBI)->getLength())) {
275 killPointer = cast<MemCpyInst>(BBI)->getSource();
276 killPointerSize = cast<ConstantInt>(
277 cast<MemCpyInst>(BBI)->getLength())->getZExtValue();
Owen Anderson32c4a052007-07-12 21:41:30 +0000278 } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
279 deadPointers.erase(A);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000280
281 // Dead alloca's can be DCE'd when we reach them
Nick Lewycky6b016702008-01-30 08:01:28 +0000282 if (A->use_empty()) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000283 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000284 DeleteDeadInstruction(A, &deadPointers);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000285 NumFastOther++;
286 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000287 }
288
Owen Anderson32c4a052007-07-12 21:41:30 +0000289 continue;
290 } else if (CallSite::get(BBI).getInstruction() != 0) {
Owen Anderson50df9682007-08-08 17:58:56 +0000291 // If this call does not access memory, it can't
292 // be undeadifying any of our pointers.
293 CallSite CS = CallSite::get(BBI);
Duncan Sands68b6f502007-12-01 07:51:45 +0000294 if (AA.doesNotAccessMemory(CS))
Owen Anderson50df9682007-08-08 17:58:56 +0000295 continue;
296
Owen Andersonddf4aee2007-08-08 18:38:28 +0000297 unsigned modRef = 0;
298 unsigned other = 0;
299
Owen Anderson32c4a052007-07-12 21:41:30 +0000300 // Remove any pointers made undead by the call from the dead set
Owen Anderson48d37802008-01-29 06:18:36 +0000301 std::vector<Value*> dead;
302 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000303 E = deadPointers.end(); I != E; ++I) {
Owen Andersonddf4aee2007-08-08 18:38:28 +0000304 // HACK: if we detect that our AA is imprecise, it's not
305 // worth it to scan the rest of the deadPointers set. Just
306 // assume that the AA will return ModRef for everything, and
307 // go ahead and bail.
308 if (modRef >= 16 && other == 0) {
309 deadPointers.clear();
310 return MadeChange;
311 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000312
Owen Anderson32c4a052007-07-12 21:41:30 +0000313 // Get size information for the alloca
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000314 unsigned pointerSize = ~0U;
Dan Gohman67243a42009-07-24 18:13:53 +0000315 if (TD) {
316 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
317 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
318 pointerSize = C->getZExtValue() *
319 TD->getTypeAllocSize(A->getAllocatedType());
320 } else {
321 const PointerType* PT = cast<PointerType>(
322 cast<Argument>(*I)->getType());
323 pointerSize = TD->getTypeAllocSize(PT->getElementType());
324 }
Owen Anderson48d37802008-01-29 06:18:36 +0000325 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000326
Owen Anderson32c4a052007-07-12 21:41:30 +0000327 // See if the call site touches it
Owen Anderson50df9682007-08-08 17:58:56 +0000328 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000329
330 if (A == AliasAnalysis::ModRef)
331 modRef++;
332 else
333 other++;
334
Owen Anderson9c9ef212007-07-13 18:26:26 +0000335 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
Owen Anderson32c4a052007-07-12 21:41:30 +0000336 dead.push_back(*I);
337 }
338
Owen Anderson48d37802008-01-29 06:18:36 +0000339 for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000340 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000341 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000342
343 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000344 } else if (isInstructionTriviallyDead(BBI)) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000345 // For any non-memory-affecting non-terminators, DCE them as we reach them
Chris Lattner1adb6752008-11-28 00:27:14 +0000346 Instruction *Inst = BBI;
347 BBI++;
348 DeleteDeadInstruction(Inst, &deadPointers);
349 NumFastOther++;
350 MadeChange = true;
351 continue;
Owen Anderson32c4a052007-07-12 21:41:30 +0000352 }
353
354 if (!killPointer)
355 continue;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000356
357 killPointer = killPointer->getUnderlyingObject();
358
Owen Anderson32c4a052007-07-12 21:41:30 +0000359 // Deal with undead pointers
Owen Andersona82c9932008-02-04 04:53:00 +0000360 MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
Chris Lattner1adb6752008-11-28 00:27:14 +0000361 deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000362 }
363
364 return MadeChange;
365}
366
Owen Andersonddf4aee2007-08-08 18:38:28 +0000367/// RemoveUndeadPointers - check for uses of a pointer that make it
368/// undead when scanning for dead stores to alloca's.
Owen Andersona82c9932008-02-04 04:53:00 +0000369bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize,
Chris Lattner1adb6752008-11-28 00:27:14 +0000370 BasicBlock::iterator &BBI,
371 SmallPtrSet<Value*, 64>& deadPointers) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000372 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000373
Owen Andersonddf4aee2007-08-08 18:38:28 +0000374 // If the kill pointer can be easily reduced to an alloca,
Chris Lattner1adb6752008-11-28 00:27:14 +0000375 // don't bother doing extraneous AA queries.
Owen Anderson48d37802008-01-29 06:18:36 +0000376 if (deadPointers.count(killPointer)) {
377 deadPointers.erase(killPointer);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000378 return false;
379 }
380
Chris Lattner1adb6752008-11-28 00:27:14 +0000381 // A global can't be in the dead pointer set.
382 if (isa<GlobalValue>(killPointer))
383 return false;
384
Owen Anderson32c4a052007-07-12 21:41:30 +0000385 bool MadeChange = false;
386
Chris Lattner1adb6752008-11-28 00:27:14 +0000387 SmallVector<Value*, 16> undead;
Owen Anderson32c4a052007-07-12 21:41:30 +0000388
Owen Anderson48d37802008-01-29 06:18:36 +0000389 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000390 E = deadPointers.end(); I != E; ++I) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000391 // Get size information for the alloca.
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000392 unsigned pointerSize = ~0U;
Dan Gohman67243a42009-07-24 18:13:53 +0000393 if (TD) {
394 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
395 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
396 pointerSize = C->getZExtValue() *
397 TD->getTypeAllocSize(A->getAllocatedType());
398 } else {
399 const PointerType* PT = cast<PointerType>(cast<Argument>(*I)->getType());
400 pointerSize = TD->getTypeAllocSize(PT->getElementType());
401 }
Owen Anderson48d37802008-01-29 06:18:36 +0000402 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000403
Owen Anderson32c4a052007-07-12 21:41:30 +0000404 // See if this pointer could alias it
Owen Andersone3590582007-08-02 18:11:11 +0000405 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
Owen Andersona82c9932008-02-04 04:53:00 +0000406 killPointer, killPointerSize);
Owen Anderson32c4a052007-07-12 21:41:30 +0000407
408 // If it must-alias and a store, we can delete it
409 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
410 StoreInst* S = cast<StoreInst>(BBI);
411
412 // Remove it!
Owen Anderson32c4a052007-07-12 21:41:30 +0000413 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000414 DeleteDeadInstruction(S, &deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000415 NumFastStores++;
416 MadeChange = true;
417
418 continue;
419
420 // Otherwise, it is undead
Chris Lattner1adb6752008-11-28 00:27:14 +0000421 } else if (A != AliasAnalysis::NoAlias)
422 undead.push_back(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000423 }
424
Chris Lattner1adb6752008-11-28 00:27:14 +0000425 for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000426 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000427 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000428
429 return MadeChange;
430}
431
Chris Lattner1adb6752008-11-28 00:27:14 +0000432/// DeleteDeadInstruction - Delete this instruction. Before we do, go through
433/// and zero out all the operands of this instruction. If any of them become
434/// dead, delete them and the computation tree that feeds them.
435///
436/// If ValueSet is non-null, remove any deleted instructions from it as well.
437///
438void DSE::DeleteDeadInstruction(Instruction *I,
439 SmallPtrSet<Value*, 64> *ValueSet) {
440 SmallVector<Instruction*, 32> NowDeadInsts;
441
442 NowDeadInsts.push_back(I);
443 --NumFastOther;
Owen Anderson5e72db32007-07-11 00:46:18 +0000444
Chris Lattner1adb6752008-11-28 00:27:14 +0000445 // Before we touch this instruction, remove it from memdep!
446 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
447 while (!NowDeadInsts.empty()) {
448 Instruction *DeadInst = NowDeadInsts.back();
449 NowDeadInsts.pop_back();
Owen Andersonbf971aa2007-07-11 19:03:09 +0000450
Chris Lattner1adb6752008-11-28 00:27:14 +0000451 ++NumFastOther;
452
453 // This instruction is dead, zap it, in stages. Start by removing it from
454 // MemDep, which needs to know the operands and needs it to be in the
455 // function.
456 MDA.removeInstruction(DeadInst);
457
458 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
459 Value *Op = DeadInst->getOperand(op);
460 DeadInst->setOperand(op, 0);
461
462 // If this operand just became dead, add it to the NowDeadInsts list.
463 if (!Op->use_empty()) continue;
464
465 if (Instruction *OpI = dyn_cast<Instruction>(Op))
466 if (isInstructionTriviallyDead(OpI))
467 NowDeadInsts.push_back(OpI);
468 }
469
470 DeadInst->eraseFromParent();
471
472 if (ValueSet) ValueSet->erase(DeadInst);
Owen Anderson5e72db32007-07-11 00:46:18 +0000473 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000474}