blob: e3f43372ec1ab9b144f25c94974be396162917d0 [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 }
157 }
158
Chris Lattnerf2a8ba42008-11-28 21:29:52 +0000159 // If this block ends in a return, unwind, or unreachable, all allocas are
160 // dead at its end, which means stores to them are also dead.
Owen Anderson32c4a052007-07-12 21:41:30 +0000161 if (BB.getTerminator()->getNumSuccessors() == 0)
Chris Lattner1adb6752008-11-28 00:27:14 +0000162 MadeChange |= handleEndBlock(BB);
Owen Anderson5e72db32007-07-11 00:46:18 +0000163
164 return MadeChange;
165}
166
Owen Andersonaa071722007-07-11 23:19:17 +0000167/// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
Chris Lattner1adb6752008-11-28 00:27:14 +0000168/// dependency is a store to a field of that structure.
Victor Hernandeze2971492009-10-24 04:23:03 +0000169bool DSE::handleFreeWithNonTrivialDependency(Instruction *F, MemDepResult Dep) {
Owen Andersonaa071722007-07-11 23:19:17 +0000170 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Andersonaa071722007-07-11 23:19:17 +0000171
Chris Lattner57e91ea2008-12-06 00:53:22 +0000172 StoreInst *Dependency = dyn_cast_or_null<StoreInst>(Dep.getInst());
173 if (!Dependency || Dependency->isVolatile())
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000174 return false;
Owen Andersond4451de2007-07-12 18:08:51 +0000175
Chris Lattner57e91ea2008-12-06 00:53:22 +0000176 Value *DepPointer = Dependency->getPointerOperand()->getUnderlyingObject();
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000177
Chris Lattner57e91ea2008-12-06 00:53:22 +0000178 // Check for aliasing.
Victor Hernandezde5ad422009-10-26 23:43:48 +0000179 if (AA.alias(F->getOperand(1), 1, DepPointer, 1) !=
Chris Lattner57e91ea2008-12-06 00:53:22 +0000180 AliasAnalysis::MustAlias)
Chris Lattner1adb6752008-11-28 00:27:14 +0000181 return false;
Owen Andersonaa071722007-07-11 23:19:17 +0000182
Chris Lattner1adb6752008-11-28 00:27:14 +0000183 // DCE instructions only used to calculate that store
Chris Lattner57e91ea2008-12-06 00:53:22 +0000184 DeleteDeadInstruction(Dependency);
Chris Lattner1adb6752008-11-28 00:27:14 +0000185 NumFastStores++;
186 return true;
Owen Andersonaa071722007-07-11 23:19:17 +0000187}
188
Owen Andersone3590582007-08-02 18:11:11 +0000189/// handleEndBlock - Remove dead stores to stack-allocated locations in the
Owen Anderson52aaabf2007-08-08 17:50:09 +0000190/// function end block. Ex:
191/// %A = alloca i32
192/// ...
193/// store i32 1, i32* %A
194/// ret void
Chris Lattner1adb6752008-11-28 00:27:14 +0000195bool DSE::handleEndBlock(BasicBlock &BB) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000196 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000197
198 bool MadeChange = false;
199
200 // Pointers alloca'd in this function are dead in the end block
Owen Anderson48d37802008-01-29 06:18:36 +0000201 SmallPtrSet<Value*, 64> deadPointers;
Owen Anderson32c4a052007-07-12 21:41:30 +0000202
Chris Lattner1adb6752008-11-28 00:27:14 +0000203 // Find all of the alloca'd pointers in the entry block.
Owen Anderson32c4a052007-07-12 21:41:30 +0000204 BasicBlock *Entry = BB.getParent()->begin();
205 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
206 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
207 deadPointers.insert(AI);
Chris Lattner1adb6752008-11-28 00:27:14 +0000208
209 // Treat byval arguments the same, stores to them are dead at the end of the
210 // function.
Owen Anderson48d37802008-01-29 06:18:36 +0000211 for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
212 AE = BB.getParent()->arg_end(); AI != AE; ++AI)
213 if (AI->hasByValAttr())
214 deadPointers.insert(AI);
Owen Anderson32c4a052007-07-12 21:41:30 +0000215
216 // Scan the basic block backwards
217 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
218 --BBI;
219
Chris Lattner1adb6752008-11-28 00:27:14 +0000220 // If we find a store whose pointer is dead.
Owen Anderson32c4a052007-07-12 21:41:30 +0000221 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000222 if (!S->isVolatile()) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000223 // See through pointer-to-pointer bitcasts
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000224 Value* pointerOperand = S->getPointerOperand()->getUnderlyingObject();
225
Owen Anderson6af19fd2008-01-25 10:10:33 +0000226 // Alloca'd pointers or byval arguments (which are functionally like
227 // alloca's) are valid candidates for removal.
Owen Anderson48d37802008-01-29 06:18:36 +0000228 if (deadPointers.count(pointerOperand)) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000229 // DCE instructions only used to calculate that store.
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000230 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000231 DeleteDeadInstruction(S, &deadPointers);
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000232 NumFastStores++;
233 MadeChange = true;
234 }
Owen Anderson32c4a052007-07-12 21:41:30 +0000235 }
Owen Anderson52aaabf2007-08-08 17:50:09 +0000236
237 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000238 }
Owen Anderson48d37802008-01-29 06:18:36 +0000239
Chris Lattner1adb6752008-11-28 00:27:14 +0000240 // We can also remove memcpy's to local variables at the end of a function.
241 if (MemCpyInst *M = dyn_cast<MemCpyInst>(BBI)) {
242 Value *dest = M->getDest()->getUnderlyingObject();
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000243
Owen Anderson48d37802008-01-29 06:18:36 +0000244 if (deadPointers.count(dest)) {
Owen Anderson48d37802008-01-29 06:18:36 +0000245 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000246 DeleteDeadInstruction(M, &deadPointers);
Owen Anderson48d37802008-01-29 06:18:36 +0000247 NumFastOther++;
248 MadeChange = true;
Owen Anderson48d37802008-01-29 06:18:36 +0000249 continue;
250 }
251
Chris Lattner1adb6752008-11-28 00:27:14 +0000252 // Because a memcpy is also a load, we can't skip it if we didn't remove
253 // it.
Owen Anderson52aaabf2007-08-08 17:50:09 +0000254 }
255
256 Value* killPointer = 0;
Owen Andersona82c9932008-02-04 04:53:00 +0000257 uint64_t killPointerSize = ~0UL;
Owen Anderson32c4a052007-07-12 21:41:30 +0000258
259 // If we encounter a use of the pointer, it is no longer considered dead
Chris Lattner1adb6752008-11-28 00:27:14 +0000260 if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
Nate Begeman53c5c622008-05-13 01:48:26 +0000261 // However, if this load is unused and not volatile, we can go ahead and
262 // remove it, and not have to worry about it making our pointer undead!
Dan Gohman8cb19d92008-04-28 19:51:27 +0000263 if (L->use_empty() && !L->isVolatile()) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000264 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000265 DeleteDeadInstruction(L, &deadPointers);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000266 NumFastOther++;
267 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000268 continue;
269 }
270
Owen Anderson32c4a052007-07-12 21:41:30 +0000271 killPointer = L->getPointerOperand();
Owen Anderson32c4a052007-07-12 21:41:30 +0000272 } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
273 killPointer = V->getOperand(0);
Owen Andersona82c9932008-02-04 04:53:00 +0000274 } else if (isa<MemCpyInst>(BBI) &&
275 isa<ConstantInt>(cast<MemCpyInst>(BBI)->getLength())) {
276 killPointer = cast<MemCpyInst>(BBI)->getSource();
277 killPointerSize = cast<ConstantInt>(
278 cast<MemCpyInst>(BBI)->getLength())->getZExtValue();
Owen Anderson32c4a052007-07-12 21:41:30 +0000279 } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
280 deadPointers.erase(A);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000281
282 // Dead alloca's can be DCE'd when we reach them
Nick Lewycky6b016702008-01-30 08:01:28 +0000283 if (A->use_empty()) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000284 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000285 DeleteDeadInstruction(A, &deadPointers);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000286 NumFastOther++;
287 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000288 }
289
Owen Anderson32c4a052007-07-12 21:41:30 +0000290 continue;
291 } else if (CallSite::get(BBI).getInstruction() != 0) {
Owen Anderson50df9682007-08-08 17:58:56 +0000292 // If this call does not access memory, it can't
293 // be undeadifying any of our pointers.
294 CallSite CS = CallSite::get(BBI);
Duncan Sands68b6f502007-12-01 07:51:45 +0000295 if (AA.doesNotAccessMemory(CS))
Owen Anderson50df9682007-08-08 17:58:56 +0000296 continue;
297
Owen Andersonddf4aee2007-08-08 18:38:28 +0000298 unsigned modRef = 0;
299 unsigned other = 0;
300
Owen Anderson32c4a052007-07-12 21:41:30 +0000301 // Remove any pointers made undead by the call from the dead set
Owen Anderson48d37802008-01-29 06:18:36 +0000302 std::vector<Value*> dead;
303 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000304 E = deadPointers.end(); I != E; ++I) {
Owen Andersonddf4aee2007-08-08 18:38:28 +0000305 // HACK: if we detect that our AA is imprecise, it's not
306 // worth it to scan the rest of the deadPointers set. Just
307 // assume that the AA will return ModRef for everything, and
308 // go ahead and bail.
309 if (modRef >= 16 && other == 0) {
310 deadPointers.clear();
311 return MadeChange;
312 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000313
Owen Anderson32c4a052007-07-12 21:41:30 +0000314 // Get size information for the alloca
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000315 unsigned pointerSize = ~0U;
Dan Gohman67243a42009-07-24 18:13:53 +0000316 if (TD) {
317 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
318 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
319 pointerSize = C->getZExtValue() *
320 TD->getTypeAllocSize(A->getAllocatedType());
321 } else {
322 const PointerType* PT = cast<PointerType>(
323 cast<Argument>(*I)->getType());
324 pointerSize = TD->getTypeAllocSize(PT->getElementType());
325 }
Owen Anderson48d37802008-01-29 06:18:36 +0000326 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000327
Owen Anderson32c4a052007-07-12 21:41:30 +0000328 // See if the call site touches it
Owen Anderson50df9682007-08-08 17:58:56 +0000329 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000330
331 if (A == AliasAnalysis::ModRef)
332 modRef++;
333 else
334 other++;
335
Owen Anderson9c9ef212007-07-13 18:26:26 +0000336 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
Owen Anderson32c4a052007-07-12 21:41:30 +0000337 dead.push_back(*I);
338 }
339
Owen Anderson48d37802008-01-29 06:18:36 +0000340 for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000341 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000342 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000343
344 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000345 } else if (isInstructionTriviallyDead(BBI)) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000346 // For any non-memory-affecting non-terminators, DCE them as we reach them
Chris Lattner1adb6752008-11-28 00:27:14 +0000347 Instruction *Inst = BBI;
348 BBI++;
349 DeleteDeadInstruction(Inst, &deadPointers);
350 NumFastOther++;
351 MadeChange = true;
352 continue;
Owen Anderson32c4a052007-07-12 21:41:30 +0000353 }
354
355 if (!killPointer)
356 continue;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000357
358 killPointer = killPointer->getUnderlyingObject();
359
Owen Anderson32c4a052007-07-12 21:41:30 +0000360 // Deal with undead pointers
Owen Andersona82c9932008-02-04 04:53:00 +0000361 MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
Chris Lattner1adb6752008-11-28 00:27:14 +0000362 deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000363 }
364
365 return MadeChange;
366}
367
Owen Andersonddf4aee2007-08-08 18:38:28 +0000368/// RemoveUndeadPointers - check for uses of a pointer that make it
369/// undead when scanning for dead stores to alloca's.
Owen Andersona82c9932008-02-04 04:53:00 +0000370bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize,
Chris Lattner1adb6752008-11-28 00:27:14 +0000371 BasicBlock::iterator &BBI,
372 SmallPtrSet<Value*, 64>& deadPointers) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000373 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000374
Owen Andersonddf4aee2007-08-08 18:38:28 +0000375 // If the kill pointer can be easily reduced to an alloca,
Chris Lattner1adb6752008-11-28 00:27:14 +0000376 // don't bother doing extraneous AA queries.
Owen Anderson48d37802008-01-29 06:18:36 +0000377 if (deadPointers.count(killPointer)) {
378 deadPointers.erase(killPointer);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000379 return false;
380 }
381
Chris Lattner1adb6752008-11-28 00:27:14 +0000382 // A global can't be in the dead pointer set.
383 if (isa<GlobalValue>(killPointer))
384 return false;
385
Owen Anderson32c4a052007-07-12 21:41:30 +0000386 bool MadeChange = false;
387
Chris Lattner1adb6752008-11-28 00:27:14 +0000388 SmallVector<Value*, 16> undead;
Owen Anderson32c4a052007-07-12 21:41:30 +0000389
Owen Anderson48d37802008-01-29 06:18:36 +0000390 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000391 E = deadPointers.end(); I != E; ++I) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000392 // Get size information for the alloca.
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000393 unsigned pointerSize = ~0U;
Dan Gohman67243a42009-07-24 18:13:53 +0000394 if (TD) {
395 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
396 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
397 pointerSize = C->getZExtValue() *
398 TD->getTypeAllocSize(A->getAllocatedType());
399 } else {
400 const PointerType* PT = cast<PointerType>(cast<Argument>(*I)->getType());
401 pointerSize = TD->getTypeAllocSize(PT->getElementType());
402 }
Owen Anderson48d37802008-01-29 06:18:36 +0000403 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000404
Owen Anderson32c4a052007-07-12 21:41:30 +0000405 // See if this pointer could alias it
Owen Andersone3590582007-08-02 18:11:11 +0000406 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
Owen Andersona82c9932008-02-04 04:53:00 +0000407 killPointer, killPointerSize);
Owen Anderson32c4a052007-07-12 21:41:30 +0000408
409 // If it must-alias and a store, we can delete it
410 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
411 StoreInst* S = cast<StoreInst>(BBI);
412
413 // Remove it!
Owen Anderson32c4a052007-07-12 21:41:30 +0000414 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000415 DeleteDeadInstruction(S, &deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000416 NumFastStores++;
417 MadeChange = true;
418
419 continue;
420
421 // Otherwise, it is undead
Chris Lattner1adb6752008-11-28 00:27:14 +0000422 } else if (A != AliasAnalysis::NoAlias)
423 undead.push_back(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000424 }
425
Chris Lattner1adb6752008-11-28 00:27:14 +0000426 for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000427 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000428 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000429
430 return MadeChange;
431}
432
Chris Lattner1adb6752008-11-28 00:27:14 +0000433/// DeleteDeadInstruction - Delete this instruction. Before we do, go through
434/// and zero out all the operands of this instruction. If any of them become
435/// dead, delete them and the computation tree that feeds them.
436///
437/// If ValueSet is non-null, remove any deleted instructions from it as well.
438///
439void DSE::DeleteDeadInstruction(Instruction *I,
440 SmallPtrSet<Value*, 64> *ValueSet) {
441 SmallVector<Instruction*, 32> NowDeadInsts;
442
443 NowDeadInsts.push_back(I);
444 --NumFastOther;
Owen Anderson5e72db32007-07-11 00:46:18 +0000445
Chris Lattner1adb6752008-11-28 00:27:14 +0000446 // Before we touch this instruction, remove it from memdep!
447 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
448 while (!NowDeadInsts.empty()) {
449 Instruction *DeadInst = NowDeadInsts.back();
450 NowDeadInsts.pop_back();
Owen Andersonbf971aa2007-07-11 19:03:09 +0000451
Chris Lattner1adb6752008-11-28 00:27:14 +0000452 ++NumFastOther;
453
454 // This instruction is dead, zap it, in stages. Start by removing it from
455 // MemDep, which needs to know the operands and needs it to be in the
456 // function.
457 MDA.removeInstruction(DeadInst);
458
459 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
460 Value *Op = DeadInst->getOperand(op);
461 DeadInst->setOperand(op, 0);
462
463 // If this operand just became dead, add it to the NowDeadInsts list.
464 if (!Op->use_empty()) continue;
465
466 if (Instruction *OpI = dyn_cast<Instruction>(Op))
467 if (isInstructionTriviallyDead(OpI))
468 NowDeadInsts.push_back(OpI);
469 }
470
471 DeadInst->eraseFromParent();
472
473 if (ValueSet) ValueSet->erase(DeadInst);
Owen Anderson5e72db32007-07-11 00:46:18 +0000474 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000475}