blob: 376a098689d32bcb0c7370aef9327da236a3c434 [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
86 // 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 Lattner57e91ea2008-12-06 00:53:22 +0000127
Chris Lattner8c5ff512008-11-29 20:29:04 +0000128 if (BBI != BB.begin())
Chris Lattnerf3f6a802008-11-28 22:50:08 +0000129 --BBI;
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000130 continue;
131 }
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000132
Chris Lattner57e91ea2008-12-06 00:53:22 +0000133 // If we're storing the same value back to a pointer that we just
134 // loaded from, then the store can be removed.
135 if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
136 if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
137 SI->getOperand(0) == DepLoad) {
138 DeleteDeadInstruction(SI);
139 if (BBI != BB.begin())
140 --BBI;
141 NumFastStores++;
142 MadeChange = true;
143 continue;
Chris Lattner0e3d6332008-12-05 21:04:20 +0000144 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000145 }
146 }
147
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000148 // If this block ends in a return, unwind, or unreachable, all allocas are
149 // dead at its end, which means stores to them are also dead.
Owen Anderson32c4a052007-07-12 21:41:30 +0000150 if (BB.getTerminator()->getNumSuccessors() == 0)
Chris Lattner1adb6752008-11-28 00:27:14 +0000151 MadeChange |= handleEndBlock(BB);
Owen Anderson5e72db32007-07-11 00:46:18 +0000152
153 return MadeChange;
154}
155
Owen Andersonaa071722007-07-11 23:19:17 +0000156/// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
Chris Lattner1adb6752008-11-28 00:27:14 +0000157/// dependency is a store to a field of that structure.
Chris Lattner57e91ea2008-12-06 00:53:22 +0000158bool DSE::handleFreeWithNonTrivialDependency(FreeInst *F, MemDepResult Dep) {
Owen Andersonaa071722007-07-11 23:19:17 +0000159 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Andersonaa071722007-07-11 23:19:17 +0000160
Chris Lattner57e91ea2008-12-06 00:53:22 +0000161 StoreInst *Dependency = dyn_cast_or_null<StoreInst>(Dep.getInst());
162 if (!Dependency || Dependency->isVolatile())
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000163 return false;
Owen Andersond4451de2007-07-12 18:08:51 +0000164
Chris Lattner57e91ea2008-12-06 00:53:22 +0000165 Value *DepPointer = Dependency->getPointerOperand()->getUnderlyingObject();
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000166
Chris Lattner57e91ea2008-12-06 00:53:22 +0000167 // Check for aliasing.
168 if (AA.alias(F->getPointerOperand(), 1, DepPointer, 1) !=
169 AliasAnalysis::MustAlias)
Chris Lattner1adb6752008-11-28 00:27:14 +0000170 return false;
Owen Andersonaa071722007-07-11 23:19:17 +0000171
Chris Lattner1adb6752008-11-28 00:27:14 +0000172 // DCE instructions only used to calculate that store
Chris Lattner57e91ea2008-12-06 00:53:22 +0000173 DeleteDeadInstruction(Dependency);
Chris Lattner1adb6752008-11-28 00:27:14 +0000174 NumFastStores++;
175 return true;
Owen Andersonaa071722007-07-11 23:19:17 +0000176}
177
Owen Andersone3590582007-08-02 18:11:11 +0000178/// handleEndBlock - Remove dead stores to stack-allocated locations in the
Owen Anderson52aaabf2007-08-08 17:50:09 +0000179/// function end block. Ex:
180/// %A = alloca i32
181/// ...
182/// store i32 1, i32* %A
183/// ret void
Chris Lattner1adb6752008-11-28 00:27:14 +0000184bool DSE::handleEndBlock(BasicBlock &BB) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000185 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000186
187 bool MadeChange = false;
188
189 // Pointers alloca'd in this function are dead in the end block
Owen Anderson48d37802008-01-29 06:18:36 +0000190 SmallPtrSet<Value*, 64> deadPointers;
Owen Anderson32c4a052007-07-12 21:41:30 +0000191
Chris Lattner1adb6752008-11-28 00:27:14 +0000192 // Find all of the alloca'd pointers in the entry block.
Owen Anderson32c4a052007-07-12 21:41:30 +0000193 BasicBlock *Entry = BB.getParent()->begin();
194 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
195 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
196 deadPointers.insert(AI);
Chris Lattner1adb6752008-11-28 00:27:14 +0000197
198 // Treat byval arguments the same, stores to them are dead at the end of the
199 // function.
Owen Anderson48d37802008-01-29 06:18:36 +0000200 for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
201 AE = BB.getParent()->arg_end(); AI != AE; ++AI)
202 if (AI->hasByValAttr())
203 deadPointers.insert(AI);
Owen Anderson32c4a052007-07-12 21:41:30 +0000204
205 // Scan the basic block backwards
206 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
207 --BBI;
208
Chris Lattner1adb6752008-11-28 00:27:14 +0000209 // If we find a store whose pointer is dead.
Owen Anderson32c4a052007-07-12 21:41:30 +0000210 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000211 if (!S->isVolatile()) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000212 // See through pointer-to-pointer bitcasts
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000213 Value* pointerOperand = S->getPointerOperand()->getUnderlyingObject();
214
Owen Anderson6af19fd2008-01-25 10:10:33 +0000215 // Alloca'd pointers or byval arguments (which are functionally like
216 // alloca's) are valid candidates for removal.
Owen Anderson48d37802008-01-29 06:18:36 +0000217 if (deadPointers.count(pointerOperand)) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000218 // DCE instructions only used to calculate that store.
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000219 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000220 DeleteDeadInstruction(S, &deadPointers);
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000221 NumFastStores++;
222 MadeChange = true;
223 }
Owen Anderson32c4a052007-07-12 21:41:30 +0000224 }
Owen Anderson52aaabf2007-08-08 17:50:09 +0000225
226 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000227 }
Owen Anderson48d37802008-01-29 06:18:36 +0000228
Chris Lattner1adb6752008-11-28 00:27:14 +0000229 // We can also remove memcpy's to local variables at the end of a function.
230 if (MemCpyInst *M = dyn_cast<MemCpyInst>(BBI)) {
231 Value *dest = M->getDest()->getUnderlyingObject();
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000232
Owen Anderson48d37802008-01-29 06:18:36 +0000233 if (deadPointers.count(dest)) {
Owen Anderson48d37802008-01-29 06:18:36 +0000234 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000235 DeleteDeadInstruction(M, &deadPointers);
Owen Anderson48d37802008-01-29 06:18:36 +0000236 NumFastOther++;
237 MadeChange = true;
Owen Anderson48d37802008-01-29 06:18:36 +0000238 continue;
239 }
240
Chris Lattner1adb6752008-11-28 00:27:14 +0000241 // Because a memcpy is also a load, we can't skip it if we didn't remove
242 // it.
Owen Anderson52aaabf2007-08-08 17:50:09 +0000243 }
244
245 Value* killPointer = 0;
Owen Andersona82c9932008-02-04 04:53:00 +0000246 uint64_t killPointerSize = ~0UL;
Owen Anderson32c4a052007-07-12 21:41:30 +0000247
248 // If we encounter a use of the pointer, it is no longer considered dead
Chris Lattner1adb6752008-11-28 00:27:14 +0000249 if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
Nate Begeman53c5c622008-05-13 01:48:26 +0000250 // However, if this load is unused and not volatile, we can go ahead and
251 // remove it, and not have to worry about it making our pointer undead!
Dan Gohman8cb19d92008-04-28 19:51:27 +0000252 if (L->use_empty() && !L->isVolatile()) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000253 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000254 DeleteDeadInstruction(L, &deadPointers);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000255 NumFastOther++;
256 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000257 continue;
258 }
259
Owen Anderson32c4a052007-07-12 21:41:30 +0000260 killPointer = L->getPointerOperand();
Owen Anderson32c4a052007-07-12 21:41:30 +0000261 } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
262 killPointer = V->getOperand(0);
Owen Andersona82c9932008-02-04 04:53:00 +0000263 } else if (isa<MemCpyInst>(BBI) &&
264 isa<ConstantInt>(cast<MemCpyInst>(BBI)->getLength())) {
265 killPointer = cast<MemCpyInst>(BBI)->getSource();
266 killPointerSize = cast<ConstantInt>(
267 cast<MemCpyInst>(BBI)->getLength())->getZExtValue();
Owen Anderson32c4a052007-07-12 21:41:30 +0000268 } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
269 deadPointers.erase(A);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000270
271 // Dead alloca's can be DCE'd when we reach them
Nick Lewycky6b016702008-01-30 08:01:28 +0000272 if (A->use_empty()) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000273 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000274 DeleteDeadInstruction(A, &deadPointers);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000275 NumFastOther++;
276 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000277 }
278
Owen Anderson32c4a052007-07-12 21:41:30 +0000279 continue;
280 } else if (CallSite::get(BBI).getInstruction() != 0) {
Owen Anderson50df9682007-08-08 17:58:56 +0000281 // If this call does not access memory, it can't
282 // be undeadifying any of our pointers.
283 CallSite CS = CallSite::get(BBI);
Duncan Sands68b6f502007-12-01 07:51:45 +0000284 if (AA.doesNotAccessMemory(CS))
Owen Anderson50df9682007-08-08 17:58:56 +0000285 continue;
286
Owen Andersonddf4aee2007-08-08 18:38:28 +0000287 unsigned modRef = 0;
288 unsigned other = 0;
289
Owen Anderson32c4a052007-07-12 21:41:30 +0000290 // Remove any pointers made undead by the call from the dead set
Owen Anderson48d37802008-01-29 06:18:36 +0000291 std::vector<Value*> dead;
292 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000293 E = deadPointers.end(); I != E; ++I) {
Owen Andersonddf4aee2007-08-08 18:38:28 +0000294 // HACK: if we detect that our AA is imprecise, it's not
295 // worth it to scan the rest of the deadPointers set. Just
296 // assume that the AA will return ModRef for everything, and
297 // go ahead and bail.
298 if (modRef >= 16 && other == 0) {
299 deadPointers.clear();
300 return MadeChange;
301 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000302
Owen Anderson32c4a052007-07-12 21:41:30 +0000303 // Get size information for the alloca
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000304 unsigned pointerSize = ~0U;
Dan Gohman67243a42009-07-24 18:13:53 +0000305 if (TD) {
306 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
307 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
308 pointerSize = C->getZExtValue() *
309 TD->getTypeAllocSize(A->getAllocatedType());
310 } else {
311 const PointerType* PT = cast<PointerType>(
312 cast<Argument>(*I)->getType());
313 pointerSize = TD->getTypeAllocSize(PT->getElementType());
314 }
Owen Anderson48d37802008-01-29 06:18:36 +0000315 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000316
Owen Anderson32c4a052007-07-12 21:41:30 +0000317 // See if the call site touches it
Owen Anderson50df9682007-08-08 17:58:56 +0000318 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000319
320 if (A == AliasAnalysis::ModRef)
321 modRef++;
322 else
323 other++;
324
Owen Anderson9c9ef212007-07-13 18:26:26 +0000325 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
Owen Anderson32c4a052007-07-12 21:41:30 +0000326 dead.push_back(*I);
327 }
328
Owen Anderson48d37802008-01-29 06:18:36 +0000329 for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000330 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000331 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000332
333 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000334 } else if (isInstructionTriviallyDead(BBI)) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000335 // For any non-memory-affecting non-terminators, DCE them as we reach them
Chris Lattner1adb6752008-11-28 00:27:14 +0000336 Instruction *Inst = BBI;
337 BBI++;
338 DeleteDeadInstruction(Inst, &deadPointers);
339 NumFastOther++;
340 MadeChange = true;
341 continue;
Owen Anderson32c4a052007-07-12 21:41:30 +0000342 }
343
344 if (!killPointer)
345 continue;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000346
347 killPointer = killPointer->getUnderlyingObject();
348
Owen Anderson32c4a052007-07-12 21:41:30 +0000349 // Deal with undead pointers
Owen Andersona82c9932008-02-04 04:53:00 +0000350 MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
Chris Lattner1adb6752008-11-28 00:27:14 +0000351 deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000352 }
353
354 return MadeChange;
355}
356
Owen Andersonddf4aee2007-08-08 18:38:28 +0000357/// RemoveUndeadPointers - check for uses of a pointer that make it
358/// undead when scanning for dead stores to alloca's.
Owen Andersona82c9932008-02-04 04:53:00 +0000359bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize,
Chris Lattner1adb6752008-11-28 00:27:14 +0000360 BasicBlock::iterator &BBI,
361 SmallPtrSet<Value*, 64>& deadPointers) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000362 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000363
Owen Andersonddf4aee2007-08-08 18:38:28 +0000364 // If the kill pointer can be easily reduced to an alloca,
Chris Lattner1adb6752008-11-28 00:27:14 +0000365 // don't bother doing extraneous AA queries.
Owen Anderson48d37802008-01-29 06:18:36 +0000366 if (deadPointers.count(killPointer)) {
367 deadPointers.erase(killPointer);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000368 return false;
369 }
370
Chris Lattner1adb6752008-11-28 00:27:14 +0000371 // A global can't be in the dead pointer set.
372 if (isa<GlobalValue>(killPointer))
373 return false;
374
Owen Anderson32c4a052007-07-12 21:41:30 +0000375 bool MadeChange = false;
376
Chris Lattner1adb6752008-11-28 00:27:14 +0000377 SmallVector<Value*, 16> undead;
Owen Anderson32c4a052007-07-12 21:41:30 +0000378
Owen Anderson48d37802008-01-29 06:18:36 +0000379 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000380 E = deadPointers.end(); I != E; ++I) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000381 // Get size information for the alloca.
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000382 unsigned pointerSize = ~0U;
Dan Gohman67243a42009-07-24 18:13:53 +0000383 if (TD) {
384 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
385 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
386 pointerSize = C->getZExtValue() *
387 TD->getTypeAllocSize(A->getAllocatedType());
388 } else {
389 const PointerType* PT = cast<PointerType>(cast<Argument>(*I)->getType());
390 pointerSize = TD->getTypeAllocSize(PT->getElementType());
391 }
Owen Anderson48d37802008-01-29 06:18:36 +0000392 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000393
Owen Anderson32c4a052007-07-12 21:41:30 +0000394 // See if this pointer could alias it
Owen Andersone3590582007-08-02 18:11:11 +0000395 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
Owen Andersona82c9932008-02-04 04:53:00 +0000396 killPointer, killPointerSize);
Owen Anderson32c4a052007-07-12 21:41:30 +0000397
398 // If it must-alias and a store, we can delete it
399 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
400 StoreInst* S = cast<StoreInst>(BBI);
401
402 // Remove it!
Owen Anderson32c4a052007-07-12 21:41:30 +0000403 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000404 DeleteDeadInstruction(S, &deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000405 NumFastStores++;
406 MadeChange = true;
407
408 continue;
409
410 // Otherwise, it is undead
Chris Lattner1adb6752008-11-28 00:27:14 +0000411 } else if (A != AliasAnalysis::NoAlias)
412 undead.push_back(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000413 }
414
Chris Lattner1adb6752008-11-28 00:27:14 +0000415 for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000416 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000417 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000418
419 return MadeChange;
420}
421
Chris Lattner1adb6752008-11-28 00:27:14 +0000422/// DeleteDeadInstruction - Delete this instruction. Before we do, go through
423/// and zero out all the operands of this instruction. If any of them become
424/// dead, delete them and the computation tree that feeds them.
425///
426/// If ValueSet is non-null, remove any deleted instructions from it as well.
427///
428void DSE::DeleteDeadInstruction(Instruction *I,
429 SmallPtrSet<Value*, 64> *ValueSet) {
430 SmallVector<Instruction*, 32> NowDeadInsts;
431
432 NowDeadInsts.push_back(I);
433 --NumFastOther;
Owen Anderson5e72db32007-07-11 00:46:18 +0000434
Chris Lattner1adb6752008-11-28 00:27:14 +0000435 // Before we touch this instruction, remove it from memdep!
436 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
437 while (!NowDeadInsts.empty()) {
438 Instruction *DeadInst = NowDeadInsts.back();
439 NowDeadInsts.pop_back();
Owen Andersonbf971aa2007-07-11 19:03:09 +0000440
Chris Lattner1adb6752008-11-28 00:27:14 +0000441 ++NumFastOther;
442
443 // This instruction is dead, zap it, in stages. Start by removing it from
444 // MemDep, which needs to know the operands and needs it to be in the
445 // function.
446 MDA.removeInstruction(DeadInst);
447
448 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
449 Value *Op = DeadInst->getOperand(op);
450 DeadInst->setOperand(op, 0);
451
452 // If this operand just became dead, add it to the NowDeadInsts list.
453 if (!Op->use_empty()) continue;
454
455 if (Instruction *OpI = dyn_cast<Instruction>(Op))
456 if (isInstructionTriviallyDead(OpI))
457 NowDeadInsts.push_back(OpI);
458 }
459
460 DeadInst->eraseFromParent();
461
462 if (ValueSet) ValueSet->erase(DeadInst);
Owen Anderson5e72db32007-07-11 00:46:18 +0000463 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000464}