blob: f55d8b2accbb59598b7bd9e25c13724fd4897de2 [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 Hernandeze2971492009-10-24 04:23:03 +000029#include "llvm/Analysis/MallocHelper.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 Hernandeze2971492009-10-24 04:23:03 +000092 if (!isa<StoreInst>(Inst) && !isa<FreeInst>(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 Hernandeze2971492009-10-24 04:23:03 +0000107 if (isa<FreeInst>(Inst) || isFreeCall(Inst)) {
108 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 Hernandeze2971492009-10-24 04:23:03 +0000179 Value* FreeVal = isa<FreeInst>(F) ? F->getOperand(0) : F->getOperand(1);
180 if (AA.alias(FreeVal, 1, DepPointer, 1) !=
Chris Lattner57e91ea2008-12-06 00:53:22 +0000181 AliasAnalysis::MustAlias)
Chris Lattner1adb6752008-11-28 00:27:14 +0000182 return false;
Owen Andersonaa071722007-07-11 23:19:17 +0000183
Chris Lattner1adb6752008-11-28 00:27:14 +0000184 // DCE instructions only used to calculate that store
Chris Lattner57e91ea2008-12-06 00:53:22 +0000185 DeleteDeadInstruction(Dependency);
Chris Lattner1adb6752008-11-28 00:27:14 +0000186 NumFastStores++;
187 return true;
Owen Andersonaa071722007-07-11 23:19:17 +0000188}
189
Owen Andersone3590582007-08-02 18:11:11 +0000190/// handleEndBlock - Remove dead stores to stack-allocated locations in the
Owen Anderson52aaabf2007-08-08 17:50:09 +0000191/// function end block. Ex:
192/// %A = alloca i32
193/// ...
194/// store i32 1, i32* %A
195/// ret void
Chris Lattner1adb6752008-11-28 00:27:14 +0000196bool DSE::handleEndBlock(BasicBlock &BB) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000197 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000198
199 bool MadeChange = false;
200
201 // Pointers alloca'd in this function are dead in the end block
Owen Anderson48d37802008-01-29 06:18:36 +0000202 SmallPtrSet<Value*, 64> deadPointers;
Owen Anderson32c4a052007-07-12 21:41:30 +0000203
Chris Lattner1adb6752008-11-28 00:27:14 +0000204 // Find all of the alloca'd pointers in the entry block.
Owen Anderson32c4a052007-07-12 21:41:30 +0000205 BasicBlock *Entry = BB.getParent()->begin();
206 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
207 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
208 deadPointers.insert(AI);
Chris Lattner1adb6752008-11-28 00:27:14 +0000209
210 // Treat byval arguments the same, stores to them are dead at the end of the
211 // function.
Owen Anderson48d37802008-01-29 06:18:36 +0000212 for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
213 AE = BB.getParent()->arg_end(); AI != AE; ++AI)
214 if (AI->hasByValAttr())
215 deadPointers.insert(AI);
Owen Anderson32c4a052007-07-12 21:41:30 +0000216
217 // Scan the basic block backwards
218 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
219 --BBI;
220
Chris Lattner1adb6752008-11-28 00:27:14 +0000221 // If we find a store whose pointer is dead.
Owen Anderson32c4a052007-07-12 21:41:30 +0000222 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000223 if (!S->isVolatile()) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000224 // See through pointer-to-pointer bitcasts
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000225 Value* pointerOperand = S->getPointerOperand()->getUnderlyingObject();
226
Owen Anderson6af19fd2008-01-25 10:10:33 +0000227 // Alloca'd pointers or byval arguments (which are functionally like
228 // alloca's) are valid candidates for removal.
Owen Anderson48d37802008-01-29 06:18:36 +0000229 if (deadPointers.count(pointerOperand)) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000230 // DCE instructions only used to calculate that store.
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000231 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000232 DeleteDeadInstruction(S, &deadPointers);
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000233 NumFastStores++;
234 MadeChange = true;
235 }
Owen Anderson32c4a052007-07-12 21:41:30 +0000236 }
Owen Anderson52aaabf2007-08-08 17:50:09 +0000237
238 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000239 }
Owen Anderson48d37802008-01-29 06:18:36 +0000240
Chris Lattner1adb6752008-11-28 00:27:14 +0000241 // We can also remove memcpy's to local variables at the end of a function.
242 if (MemCpyInst *M = dyn_cast<MemCpyInst>(BBI)) {
243 Value *dest = M->getDest()->getUnderlyingObject();
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000244
Owen Anderson48d37802008-01-29 06:18:36 +0000245 if (deadPointers.count(dest)) {
Owen Anderson48d37802008-01-29 06:18:36 +0000246 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000247 DeleteDeadInstruction(M, &deadPointers);
Owen Anderson48d37802008-01-29 06:18:36 +0000248 NumFastOther++;
249 MadeChange = true;
Owen Anderson48d37802008-01-29 06:18:36 +0000250 continue;
251 }
252
Chris Lattner1adb6752008-11-28 00:27:14 +0000253 // Because a memcpy is also a load, we can't skip it if we didn't remove
254 // it.
Owen Anderson52aaabf2007-08-08 17:50:09 +0000255 }
256
257 Value* killPointer = 0;
Owen Andersona82c9932008-02-04 04:53:00 +0000258 uint64_t killPointerSize = ~0UL;
Owen Anderson32c4a052007-07-12 21:41:30 +0000259
260 // If we encounter a use of the pointer, it is no longer considered dead
Chris Lattner1adb6752008-11-28 00:27:14 +0000261 if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
Nate Begeman53c5c622008-05-13 01:48:26 +0000262 // However, if this load is unused and not volatile, we can go ahead and
263 // remove it, and not have to worry about it making our pointer undead!
Dan Gohman8cb19d92008-04-28 19:51:27 +0000264 if (L->use_empty() && !L->isVolatile()) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000265 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000266 DeleteDeadInstruction(L, &deadPointers);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000267 NumFastOther++;
268 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000269 continue;
270 }
271
Owen Anderson32c4a052007-07-12 21:41:30 +0000272 killPointer = L->getPointerOperand();
Owen Anderson32c4a052007-07-12 21:41:30 +0000273 } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
274 killPointer = V->getOperand(0);
Owen Andersona82c9932008-02-04 04:53:00 +0000275 } else if (isa<MemCpyInst>(BBI) &&
276 isa<ConstantInt>(cast<MemCpyInst>(BBI)->getLength())) {
277 killPointer = cast<MemCpyInst>(BBI)->getSource();
278 killPointerSize = cast<ConstantInt>(
279 cast<MemCpyInst>(BBI)->getLength())->getZExtValue();
Owen Anderson32c4a052007-07-12 21:41:30 +0000280 } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
281 deadPointers.erase(A);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000282
283 // Dead alloca's can be DCE'd when we reach them
Nick Lewycky6b016702008-01-30 08:01:28 +0000284 if (A->use_empty()) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000285 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000286 DeleteDeadInstruction(A, &deadPointers);
Owen Anderson4e4b1162008-01-30 01:24:47 +0000287 NumFastOther++;
288 MadeChange = true;
Owen Anderson4e4b1162008-01-30 01:24:47 +0000289 }
290
Owen Anderson32c4a052007-07-12 21:41:30 +0000291 continue;
292 } else if (CallSite::get(BBI).getInstruction() != 0) {
Owen Anderson50df9682007-08-08 17:58:56 +0000293 // If this call does not access memory, it can't
294 // be undeadifying any of our pointers.
295 CallSite CS = CallSite::get(BBI);
Duncan Sands68b6f502007-12-01 07:51:45 +0000296 if (AA.doesNotAccessMemory(CS))
Owen Anderson50df9682007-08-08 17:58:56 +0000297 continue;
298
Owen Andersonddf4aee2007-08-08 18:38:28 +0000299 unsigned modRef = 0;
300 unsigned other = 0;
301
Owen Anderson32c4a052007-07-12 21:41:30 +0000302 // Remove any pointers made undead by the call from the dead set
Owen Anderson48d37802008-01-29 06:18:36 +0000303 std::vector<Value*> dead;
304 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000305 E = deadPointers.end(); I != E; ++I) {
Owen Andersonddf4aee2007-08-08 18:38:28 +0000306 // HACK: if we detect that our AA is imprecise, it's not
307 // worth it to scan the rest of the deadPointers set. Just
308 // assume that the AA will return ModRef for everything, and
309 // go ahead and bail.
310 if (modRef >= 16 && other == 0) {
311 deadPointers.clear();
312 return MadeChange;
313 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000314
Owen Anderson32c4a052007-07-12 21:41:30 +0000315 // Get size information for the alloca
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000316 unsigned pointerSize = ~0U;
Dan Gohman67243a42009-07-24 18:13:53 +0000317 if (TD) {
318 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
319 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
320 pointerSize = C->getZExtValue() *
321 TD->getTypeAllocSize(A->getAllocatedType());
322 } else {
323 const PointerType* PT = cast<PointerType>(
324 cast<Argument>(*I)->getType());
325 pointerSize = TD->getTypeAllocSize(PT->getElementType());
326 }
Owen Anderson48d37802008-01-29 06:18:36 +0000327 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000328
Owen Anderson32c4a052007-07-12 21:41:30 +0000329 // See if the call site touches it
Owen Anderson50df9682007-08-08 17:58:56 +0000330 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000331
332 if (A == AliasAnalysis::ModRef)
333 modRef++;
334 else
335 other++;
336
Owen Anderson9c9ef212007-07-13 18:26:26 +0000337 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
Owen Anderson32c4a052007-07-12 21:41:30 +0000338 dead.push_back(*I);
339 }
340
Owen Anderson48d37802008-01-29 06:18:36 +0000341 for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000342 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000343 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000344
345 continue;
Chris Lattner1adb6752008-11-28 00:27:14 +0000346 } else if (isInstructionTriviallyDead(BBI)) {
Owen Anderson4e4b1162008-01-30 01:24:47 +0000347 // For any non-memory-affecting non-terminators, DCE them as we reach them
Chris Lattner1adb6752008-11-28 00:27:14 +0000348 Instruction *Inst = BBI;
349 BBI++;
350 DeleteDeadInstruction(Inst, &deadPointers);
351 NumFastOther++;
352 MadeChange = true;
353 continue;
Owen Anderson32c4a052007-07-12 21:41:30 +0000354 }
355
356 if (!killPointer)
357 continue;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000358
359 killPointer = killPointer->getUnderlyingObject();
360
Owen Anderson32c4a052007-07-12 21:41:30 +0000361 // Deal with undead pointers
Owen Andersona82c9932008-02-04 04:53:00 +0000362 MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
Chris Lattner1adb6752008-11-28 00:27:14 +0000363 deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000364 }
365
366 return MadeChange;
367}
368
Owen Andersonddf4aee2007-08-08 18:38:28 +0000369/// RemoveUndeadPointers - check for uses of a pointer that make it
370/// undead when scanning for dead stores to alloca's.
Owen Andersona82c9932008-02-04 04:53:00 +0000371bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize,
Chris Lattner1adb6752008-11-28 00:27:14 +0000372 BasicBlock::iterator &BBI,
373 SmallPtrSet<Value*, 64>& deadPointers) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000374 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Anderson32c4a052007-07-12 21:41:30 +0000375
Owen Andersonddf4aee2007-08-08 18:38:28 +0000376 // If the kill pointer can be easily reduced to an alloca,
Chris Lattner1adb6752008-11-28 00:27:14 +0000377 // don't bother doing extraneous AA queries.
Owen Anderson48d37802008-01-29 06:18:36 +0000378 if (deadPointers.count(killPointer)) {
379 deadPointers.erase(killPointer);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000380 return false;
381 }
382
Chris Lattner1adb6752008-11-28 00:27:14 +0000383 // A global can't be in the dead pointer set.
384 if (isa<GlobalValue>(killPointer))
385 return false;
386
Owen Anderson32c4a052007-07-12 21:41:30 +0000387 bool MadeChange = false;
388
Chris Lattner1adb6752008-11-28 00:27:14 +0000389 SmallVector<Value*, 16> undead;
Owen Anderson32c4a052007-07-12 21:41:30 +0000390
Owen Anderson48d37802008-01-29 06:18:36 +0000391 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000392 E = deadPointers.end(); I != E; ++I) {
Chris Lattner1adb6752008-11-28 00:27:14 +0000393 // Get size information for the alloca.
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000394 unsigned pointerSize = ~0U;
Dan Gohman67243a42009-07-24 18:13:53 +0000395 if (TD) {
396 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) {
397 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize()))
398 pointerSize = C->getZExtValue() *
399 TD->getTypeAllocSize(A->getAllocatedType());
400 } else {
401 const PointerType* PT = cast<PointerType>(cast<Argument>(*I)->getType());
402 pointerSize = TD->getTypeAllocSize(PT->getElementType());
403 }
Owen Anderson48d37802008-01-29 06:18:36 +0000404 }
Duncan Sandsfe3bef02008-01-20 10:49:23 +0000405
Owen Anderson32c4a052007-07-12 21:41:30 +0000406 // See if this pointer could alias it
Owen Andersone3590582007-08-02 18:11:11 +0000407 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
Owen Andersona82c9932008-02-04 04:53:00 +0000408 killPointer, killPointerSize);
Owen Anderson32c4a052007-07-12 21:41:30 +0000409
410 // If it must-alias and a store, we can delete it
411 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
412 StoreInst* S = cast<StoreInst>(BBI);
413
414 // Remove it!
Owen Anderson32c4a052007-07-12 21:41:30 +0000415 BBI++;
Chris Lattner1adb6752008-11-28 00:27:14 +0000416 DeleteDeadInstruction(S, &deadPointers);
Owen Anderson32c4a052007-07-12 21:41:30 +0000417 NumFastStores++;
418 MadeChange = true;
419
420 continue;
421
422 // Otherwise, it is undead
Chris Lattner1adb6752008-11-28 00:27:14 +0000423 } else if (A != AliasAnalysis::NoAlias)
424 undead.push_back(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000425 }
426
Chris Lattner1adb6752008-11-28 00:27:14 +0000427 for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
Owen Anderson32c4a052007-07-12 21:41:30 +0000428 I != E; ++I)
Owen Anderson48d37802008-01-29 06:18:36 +0000429 deadPointers.erase(*I);
Owen Anderson32c4a052007-07-12 21:41:30 +0000430
431 return MadeChange;
432}
433
Chris Lattner1adb6752008-11-28 00:27:14 +0000434/// DeleteDeadInstruction - Delete this instruction. Before we do, go through
435/// and zero out all the operands of this instruction. If any of them become
436/// dead, delete them and the computation tree that feeds them.
437///
438/// If ValueSet is non-null, remove any deleted instructions from it as well.
439///
440void DSE::DeleteDeadInstruction(Instruction *I,
441 SmallPtrSet<Value*, 64> *ValueSet) {
442 SmallVector<Instruction*, 32> NowDeadInsts;
443
444 NowDeadInsts.push_back(I);
445 --NumFastOther;
Owen Anderson5e72db32007-07-11 00:46:18 +0000446
Chris Lattner1adb6752008-11-28 00:27:14 +0000447 // Before we touch this instruction, remove it from memdep!
448 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
449 while (!NowDeadInsts.empty()) {
450 Instruction *DeadInst = NowDeadInsts.back();
451 NowDeadInsts.pop_back();
Owen Andersonbf971aa2007-07-11 19:03:09 +0000452
Chris Lattner1adb6752008-11-28 00:27:14 +0000453 ++NumFastOther;
454
455 // This instruction is dead, zap it, in stages. Start by removing it from
456 // MemDep, which needs to know the operands and needs it to be in the
457 // function.
458 MDA.removeInstruction(DeadInst);
459
460 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
461 Value *Op = DeadInst->getOperand(op);
462 DeadInst->setOperand(op, 0);
463
464 // If this operand just became dead, add it to the NowDeadInsts list.
465 if (!Op->use_empty()) continue;
466
467 if (Instruction *OpI = dyn_cast<Instruction>(Op))
468 if (isInstructionTriviallyDead(OpI))
469 NowDeadInsts.push_back(OpI);
470 }
471
472 DeadInst->eraseFromParent();
473
474 if (ValueSet) ValueSet->erase(DeadInst);
Owen Anderson5e72db32007-07-11 00:46:18 +0000475 }
Owen Anderson5e72db32007-07-11 00:46:18 +0000476}