blob: 1e5381db9af2d4b86f3c0516331d0d697b39a43e [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//
Owen Andersonbf971aa2007-07-11 19:03:09 +00005// This file was developed by Owen Anderson and is distributed under
Owen Anderson5e72db32007-07-11 00:46:18 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
23#include "llvm/Pass.h"
24#include "llvm/ADT/SetVector.h"
25#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 Anderson5e72db32007-07-11 00:46:18 +000028#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Owen Andersonaa071722007-07-11 23:19:17 +000029#include "llvm/Target/TargetData.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000030#include "llvm/Transforms/Utils/Local.h"
31#include "llvm/Support/Compiler.h"
32using namespace llvm;
33
34STATISTIC(NumFastStores, "Number of stores deleted");
35STATISTIC(NumFastOther , "Number of other instrs removed");
36
37namespace {
Owen Anderson10e52ed2007-08-01 06:36:51 +000038 struct VISIBILITY_HIDDEN DSE : public FunctionPass {
Owen Anderson5e72db32007-07-11 00:46:18 +000039 static char ID; // Pass identification, replacement for typeid
Owen Anderson10e52ed2007-08-01 06:36:51 +000040 DSE() : FunctionPass((intptr_t)&ID) {}
Owen Anderson5e72db32007-07-11 00:46:18 +000041
42 virtual bool runOnFunction(Function &F) {
43 bool Changed = false;
44 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
45 Changed |= runOnBasicBlock(*I);
46 return Changed;
47 }
48
49 bool runOnBasicBlock(BasicBlock &BB);
Owen Andersone3590582007-08-02 18:11:11 +000050 bool handleFreeWithNonTrivialDependency(FreeInst* F,
51 Instruction* dependency,
52 SetVector<Instruction*>& possiblyDead);
Owen Anderson32c4a052007-07-12 21:41:30 +000053 bool handleEndBlock(BasicBlock& BB, SetVector<Instruction*>& possiblyDead);
Owen Anderson52aaabf2007-08-08 17:50:09 +000054 bool RemoveUndeadPointers(Value* pointer,
Owen Anderson32c4a052007-07-12 21:41:30 +000055 BasicBlock::iterator& BBI,
Owen Anderson52aaabf2007-08-08 17:50:09 +000056 SmallPtrSet<AllocaInst*, 64>& deadPointers,
Owen Anderson32c4a052007-07-12 21:41:30 +000057 SetVector<Instruction*>& possiblyDead);
Owen Anderson5e72db32007-07-11 00:46:18 +000058 void DeleteDeadInstructionChains(Instruction *I,
59 SetVector<Instruction*> &DeadInsts);
Owen Anderson0aecf0e2007-08-08 04:52:29 +000060
Owen Andersonb17ab032007-08-08 06:06:02 +000061 /// Find the base pointer that a pointer came from
62 /// Because this is used to find pointers that originate
63 /// from allocas, it is safe to ignore GEP indices, since
64 /// either the store will be in the alloca, and thus dead,
65 /// or beyond the end of the alloca, and thus undefined.
Owen Anderson9c9ef212007-07-13 18:26:26 +000066 void TranslatePointerBitCasts(Value*& v) {
Owen Andersone3590582007-08-02 18:11:11 +000067 assert(isa<PointerType>(v->getType()) &&
68 "Translating a non-pointer type?");
Owen Anderson0aecf0e2007-08-08 04:52:29 +000069 while (true) {
Owen Anderson09f86992007-07-16 23:34:39 +000070 if (BitCastInst* C = dyn_cast<BitCastInst>(v))
71 v = C->getOperand(0);
72 else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(v))
73 v = G->getOperand(0);
Owen Anderson0aecf0e2007-08-08 04:52:29 +000074 else
75 break;
76 }
Owen Anderson9c9ef212007-07-13 18:26:26 +000077 }
Owen Anderson5e72db32007-07-11 00:46:18 +000078
79 // getAnalysisUsage - We require post dominance frontiers (aka Control
80 // Dependence Graph)
81 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
82 AU.setPreservesCFG();
Owen Andersonaa071722007-07-11 23:19:17 +000083 AU.addRequired<TargetData>();
84 AU.addRequired<AliasAnalysis>();
Owen Anderson5e72db32007-07-11 00:46:18 +000085 AU.addRequired<MemoryDependenceAnalysis>();
Owen Andersonaa071722007-07-11 23:19:17 +000086 AU.addPreserved<AliasAnalysis>();
Owen Anderson5e72db32007-07-11 00:46:18 +000087 AU.addPreserved<MemoryDependenceAnalysis>();
88 }
89 };
Owen Anderson10e52ed2007-08-01 06:36:51 +000090 char DSE::ID = 0;
91 RegisterPass<DSE> X("dse", "Dead Store Elimination");
Owen Anderson5e72db32007-07-11 00:46:18 +000092}
93
Owen Anderson10e52ed2007-08-01 06:36:51 +000094FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
Owen Anderson5e72db32007-07-11 00:46:18 +000095
Owen Anderson10e52ed2007-08-01 06:36:51 +000096bool DSE::runOnBasicBlock(BasicBlock &BB) {
Owen Anderson5e72db32007-07-11 00:46:18 +000097 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
98
Owen Andersonbf971aa2007-07-11 19:03:09 +000099 // Record the last-seen store to this pointer
Owen Anderson5e72db32007-07-11 00:46:18 +0000100 DenseMap<Value*, StoreInst*> lastStore;
Owen Andersonbf971aa2007-07-11 19:03:09 +0000101 // Record instructions possibly made dead by deleting a store
Owen Anderson5e72db32007-07-11 00:46:18 +0000102 SetVector<Instruction*> possiblyDead;
103
104 bool MadeChange = false;
105
106 // Do a top-down walk on the BB
Owen Andersone3590582007-08-02 18:11:11 +0000107 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end();
108 BBI != BBE; ++BBI) {
Owen Anderson14414702007-07-11 21:06:56 +0000109 // If we find a store or a free...
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000110 if (!isa<StoreInst>(BBI) && !isa<FreeInst>(BBI))
111 continue;
Owen Anderson9c9ef212007-07-13 18:26:26 +0000112
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000113 Value* pointer = 0;
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000114 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
115 if (!S->isVolatile())
116 pointer = S->getPointerOperand();
117 else
118 continue;
119 } else
Owen Andersonb17ab032007-08-08 06:06:02 +0000120 pointer = cast<FreeInst>(BBI)->getPointerOperand();
Owen Anderson5e72db32007-07-11 00:46:18 +0000121
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000122 StoreInst*& last = lastStore[pointer];
123 bool deletedStore = false;
124
125 // ... to a pointer that has been stored to before...
126 if (last) {
Owen Anderson9b1cc8c2007-08-09 04:42:44 +0000127 Instruction* dep = MD.getDependency(BBI);
Owen Anderson5e72db32007-07-11 00:46:18 +0000128
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000129 // ... and no other memory dependencies are between them....
130 while (dep != MemoryDependenceAnalysis::None &&
131 dep != MemoryDependenceAnalysis::NonLocal &&
132 isa<StoreInst>(dep)) {
133 if (dep != last) {
Owen Anderson9b1cc8c2007-08-09 04:42:44 +0000134 dep = MD.getDependency(BBI, dep);
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000135 continue;
Owen Andersond4451de2007-07-12 18:08:51 +0000136 }
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000137
138 // Remove it!
139 MD.removeInstruction(last);
140
141 // DCE instructions only used to calculate that store
142 if (Instruction* D = dyn_cast<Instruction>(last->getOperand(0)))
143 possiblyDead.insert(D);
144 if (Instruction* D = dyn_cast<Instruction>(last->getOperand(1)))
145 possiblyDead.insert(D);
146
147 last->eraseFromParent();
148 NumFastStores++;
149 deletedStore = true;
150 MadeChange = true;
151
152 break;
Owen Anderson5e72db32007-07-11 00:46:18 +0000153 }
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000154 }
155
156 // Handle frees whose dependencies are non-trivial.
157 if (FreeInst* F = dyn_cast<FreeInst>(BBI)) {
158 if (!deletedStore)
159 MadeChange |= handleFreeWithNonTrivialDependency(F,
Owen Anderson9b1cc8c2007-08-09 04:42:44 +0000160 MD.getDependency(F),
Owen Anderson0aecf0e2007-08-08 04:52:29 +0000161 possiblyDead);
162 // No known stores after the free
163 last = 0;
164 } else {
165 // Update our most-recent-store map.
166 last = cast<StoreInst>(BBI);
Owen Anderson5e72db32007-07-11 00:46:18 +0000167 }
168 }
169
Owen Anderson32c4a052007-07-12 21:41:30 +0000170 // If this block ends in a return, unwind, unreachable, and eventually
171 // tailcall, then all allocas are dead at its end.
172 if (BB.getTerminator()->getNumSuccessors() == 0)
173 MadeChange |= handleEndBlock(BB, possiblyDead);
174
Owen Anderson5e72db32007-07-11 00:46:18 +0000175 // Do a trivial DCE
176 while (!possiblyDead.empty()) {
177 Instruction *I = possiblyDead.back();
178 possiblyDead.pop_back();
179 DeleteDeadInstructionChains(I, possiblyDead);
180 }
181
182 return MadeChange;
183}
184
Owen Andersonaa071722007-07-11 23:19:17 +0000185/// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
186/// dependency is a store to a field of that structure
Owen Anderson10e52ed2007-08-01 06:36:51 +0000187bool DSE::handleFreeWithNonTrivialDependency(FreeInst* F, Instruction* dep,
Owen Andersone3590582007-08-02 18:11:11 +0000188 SetVector<Instruction*>& possiblyDead) {
Owen Andersonaa071722007-07-11 23:19:17 +0000189 TargetData &TD = getAnalysis<TargetData>();
190 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
191 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
192
Owen Andersond4451de2007-07-12 18:08:51 +0000193 if (dep == MemoryDependenceAnalysis::None ||
194 dep == MemoryDependenceAnalysis::NonLocal)
195 return false;
196
197 StoreInst* dependency = dyn_cast<StoreInst>(dep);
198 if (!dependency)
199 return false;
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000200 else if (dependency->isVolatile())
201 return false;
Owen Andersond4451de2007-07-12 18:08:51 +0000202
Owen Andersonaa071722007-07-11 23:19:17 +0000203 Value* depPointer = dependency->getPointerOperand();
Owen Andersone3590582007-08-02 18:11:11 +0000204 const Type* depType = dependency->getOperand(0)->getType();
205 unsigned depPointerSize = TD.getTypeSize(depType);
Owen Anderson9c9ef212007-07-13 18:26:26 +0000206
Owen Andersonaa071722007-07-11 23:19:17 +0000207 // Check for aliasing
208 AliasAnalysis::AliasResult A = AA.alias(F->getPointerOperand(), ~0UL,
209 depPointer, depPointerSize);
210
211 if (A == AliasAnalysis::MustAlias) {
212 // Remove it!
213 MD.removeInstruction(dependency);
Owen Andersonaa071722007-07-11 23:19:17 +0000214
215 // DCE instructions only used to calculate that store
216 if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(0)))
217 possiblyDead.insert(D);
Owen Anderson9c9ef212007-07-13 18:26:26 +0000218 if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(1)))
219 possiblyDead.insert(D);
Owen Andersonaa071722007-07-11 23:19:17 +0000220
221 dependency->eraseFromParent();
222 NumFastStores++;
223 return true;
224 }
225
226 return false;
227}
228
Owen Andersone3590582007-08-02 18:11:11 +0000229/// handleEndBlock - Remove dead stores to stack-allocated locations in the
Owen Anderson52aaabf2007-08-08 17:50:09 +0000230/// function end block. Ex:
231/// %A = alloca i32
232/// ...
233/// store i32 1, i32* %A
234/// ret void
Owen Andersone3590582007-08-02 18:11:11 +0000235bool DSE::handleEndBlock(BasicBlock& BB,
236 SetVector<Instruction*>& possiblyDead) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000237 TargetData &TD = getAnalysis<TargetData>();
238 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
239 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
240
241 bool MadeChange = false;
242
243 // Pointers alloca'd in this function are dead in the end block
Owen Anderson52aaabf2007-08-08 17:50:09 +0000244 SmallPtrSet<AllocaInst*, 64> deadPointers;
Owen Anderson32c4a052007-07-12 21:41:30 +0000245
246 // Find all of the alloca'd pointers in the entry block
247 BasicBlock *Entry = BB.getParent()->begin();
248 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
249 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
250 deadPointers.insert(AI);
251
252 // Scan the basic block backwards
253 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
254 --BBI;
255
256 if (deadPointers.empty())
257 break;
258
Owen Anderson32c4a052007-07-12 21:41:30 +0000259 // If we find a store whose pointer is dead...
260 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000261 if (!S->isVolatile()) {
262 Value* pointerOperand = S->getPointerOperand();
263 // See through pointer-to-pointer bitcasts
264 TranslatePointerBitCasts(pointerOperand);
Owen Anderson9c9ef212007-07-13 18:26:26 +0000265
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000266 if (deadPointers.count(pointerOperand)){
267 // Remove it!
268 MD.removeInstruction(S);
Owen Anderson32c4a052007-07-12 21:41:30 +0000269
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000270 // DCE instructions only used to calculate that store
271 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
272 possiblyDead.insert(D);
273 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
274 possiblyDead.insert(D);
Owen Anderson32c4a052007-07-12 21:41:30 +0000275
Owen Anderson2b9ec7f2007-08-26 21:14:47 +0000276 BBI++;
277 S->eraseFromParent();
278 NumFastStores++;
279 MadeChange = true;
280 }
Owen Anderson32c4a052007-07-12 21:41:30 +0000281 }
Owen Anderson52aaabf2007-08-08 17:50:09 +0000282
283 continue;
284 }
285
286 Value* killPointer = 0;
Owen Anderson32c4a052007-07-12 21:41:30 +0000287
288 // If we encounter a use of the pointer, it is no longer considered dead
Owen Anderson52aaabf2007-08-08 17:50:09 +0000289 if (LoadInst* L = dyn_cast<LoadInst>(BBI)) {
Owen Anderson32c4a052007-07-12 21:41:30 +0000290 killPointer = L->getPointerOperand();
Owen Anderson32c4a052007-07-12 21:41:30 +0000291 } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
292 killPointer = V->getOperand(0);
Owen Anderson32c4a052007-07-12 21:41:30 +0000293 } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
294 deadPointers.erase(A);
295 continue;
296 } else if (CallSite::get(BBI).getInstruction() != 0) {
Owen Anderson50df9682007-08-08 17:58:56 +0000297 // If this call does not access memory, it can't
298 // be undeadifying any of our pointers.
299 CallSite CS = CallSite::get(BBI);
300 if (CS.getCalledFunction() &&
301 AA.doesNotAccessMemory(CS.getCalledFunction()))
302 continue;
303
Owen Andersonddf4aee2007-08-08 18:38:28 +0000304 unsigned modRef = 0;
305 unsigned other = 0;
306
Owen Anderson32c4a052007-07-12 21:41:30 +0000307 // Remove any pointers made undead by the call from the dead set
308 std::vector<Instruction*> dead;
Owen Anderson52aaabf2007-08-08 17:50:09 +0000309 for (SmallPtrSet<AllocaInst*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000310 E = deadPointers.end(); I != E; ++I) {
Owen Andersonddf4aee2007-08-08 18:38:28 +0000311 // HACK: if we detect that our AA is imprecise, it's not
312 // worth it to scan the rest of the deadPointers set. Just
313 // assume that the AA will return ModRef for everything, and
314 // go ahead and bail.
315 if (modRef >= 16 && other == 0) {
316 deadPointers.clear();
317 return MadeChange;
318 }
319
Owen Anderson32c4a052007-07-12 21:41:30 +0000320 // Get size information for the alloca
321 unsigned pointerSize = ~0UL;
322 if (ConstantInt* C = dyn_cast<ConstantInt>((*I)->getArraySize()))
Owen Andersone3590582007-08-02 18:11:11 +0000323 pointerSize = C->getZExtValue() * \
324 TD.getTypeSize((*I)->getAllocatedType());
Owen Anderson32c4a052007-07-12 21:41:30 +0000325
326 // See if the call site touches it
Owen Anderson50df9682007-08-08 17:58:56 +0000327 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
Owen Andersonddf4aee2007-08-08 18:38:28 +0000328
329 if (A == AliasAnalysis::ModRef)
330 modRef++;
331 else
332 other++;
333
Owen Anderson9c9ef212007-07-13 18:26:26 +0000334 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
Owen Anderson32c4a052007-07-12 21:41:30 +0000335 dead.push_back(*I);
336 }
337
338 for (std::vector<Instruction*>::iterator I = dead.begin(), E = dead.end();
339 I != E; ++I)
340 deadPointers.erase(*I);
341
342 continue;
343 }
344
345 if (!killPointer)
346 continue;
347
Owen Andersonddf4aee2007-08-08 18:38:28 +0000348 TranslatePointerBitCasts(killPointer);
349
Owen Anderson32c4a052007-07-12 21:41:30 +0000350 // Deal with undead pointers
Owen Anderson52aaabf2007-08-08 17:50:09 +0000351 MadeChange |= RemoveUndeadPointers(killPointer, BBI,
Owen Anderson32c4a052007-07-12 21:41:30 +0000352 deadPointers, possiblyDead);
353 }
354
355 return MadeChange;
356}
357
Owen Andersonddf4aee2007-08-08 18:38:28 +0000358/// RemoveUndeadPointers - check for uses of a pointer that make it
359/// undead when scanning for dead stores to alloca's.
Owen Anderson52aaabf2007-08-08 17:50:09 +0000360bool DSE::RemoveUndeadPointers(Value* killPointer,
Owen Anderson32c4a052007-07-12 21:41:30 +0000361 BasicBlock::iterator& BBI,
Owen Anderson52aaabf2007-08-08 17:50:09 +0000362 SmallPtrSet<AllocaInst*, 64>& deadPointers,
Owen Anderson32c4a052007-07-12 21:41:30 +0000363 SetVector<Instruction*>& possiblyDead) {
364 TargetData &TD = getAnalysis<TargetData>();
365 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
366 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
367
Owen Andersonddf4aee2007-08-08 18:38:28 +0000368 // If the kill pointer can be easily reduced to an alloca,
369 // don't bother doing extraneous AA queries
370 if (AllocaInst* A = dyn_cast<AllocaInst>(killPointer)) {
371 if (deadPointers.count(A))
372 deadPointers.erase(A);
373 return false;
Owen Anderson68086282007-08-08 19:12:31 +0000374 } else if (isa<GlobalValue>(killPointer)) {
375 // A global can't be in the dead pointer set
376 return false;
Owen Andersonddf4aee2007-08-08 18:38:28 +0000377 }
378
Owen Anderson32c4a052007-07-12 21:41:30 +0000379 bool MadeChange = false;
380
381 std::vector<Instruction*> undead;
382
Owen Anderson52aaabf2007-08-08 17:50:09 +0000383 for (SmallPtrSet<AllocaInst*, 64>::iterator I = deadPointers.begin(),
Owen Anderson32c4a052007-07-12 21:41:30 +0000384 E = deadPointers.end(); I != E; ++I) {
385 // Get size information for the alloca
386 unsigned pointerSize = ~0UL;
387 if (ConstantInt* C = dyn_cast<ConstantInt>((*I)->getArraySize()))
Owen Andersone3590582007-08-02 18:11:11 +0000388 pointerSize = C->getZExtValue() * \
389 TD.getTypeSize((*I)->getAllocatedType());
Owen Anderson32c4a052007-07-12 21:41:30 +0000390
391 // See if this pointer could alias it
Owen Andersone3590582007-08-02 18:11:11 +0000392 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
Owen Anderson52aaabf2007-08-08 17:50:09 +0000393 killPointer, ~0UL);
Owen Anderson32c4a052007-07-12 21:41:30 +0000394
395 // If it must-alias and a store, we can delete it
396 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
397 StoreInst* S = cast<StoreInst>(BBI);
398
399 // Remove it!
400 MD.removeInstruction(S);
401
402 // DCE instructions only used to calculate that store
403 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
404 possiblyDead.insert(D);
Owen Anderson9c9ef212007-07-13 18:26:26 +0000405 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
406 possiblyDead.insert(D);
Owen Anderson32c4a052007-07-12 21:41:30 +0000407
408 BBI++;
409 S->eraseFromParent();
410 NumFastStores++;
411 MadeChange = true;
412
413 continue;
414
415 // Otherwise, it is undead
416 } else if (A != AliasAnalysis::NoAlias)
417 undead.push_back(*I);
418 }
419
420 for (std::vector<Instruction*>::iterator I = undead.begin(), E = undead.end();
421 I != E; ++I)
422 deadPointers.erase(*I);
423
424 return MadeChange;
425}
426
Owen Anderson52aaabf2007-08-08 17:50:09 +0000427/// DeleteDeadInstructionChains - takes an instruction and a setvector of
428/// dead instructions. If I is dead, it is erased, and its operands are
429/// checked for deadness. If they are dead, they are added to the dead
430/// setvector.
Owen Anderson10e52ed2007-08-01 06:36:51 +0000431void DSE::DeleteDeadInstructionChains(Instruction *I,
Owen Anderson5e72db32007-07-11 00:46:18 +0000432 SetVector<Instruction*> &DeadInsts) {
433 // Instruction must be dead.
434 if (!I->use_empty() || !isInstructionTriviallyDead(I)) return;
435
436 // Let the memory dependence know
437 getAnalysis<MemoryDependenceAnalysis>().removeInstruction(I);
438
439 // See if this made any operands dead. We do it this way in case the
440 // instruction uses the same operand twice. We don't want to delete a
441 // value then reference it.
442 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Owen Andersonbf971aa2007-07-11 19:03:09 +0000443 if (I->getOperand(i)->hasOneUse())
444 if (Instruction* Op = dyn_cast<Instruction>(I->getOperand(i)))
445 DeadInsts.insert(Op); // Attempt to nuke it later.
446
Owen Anderson5e72db32007-07-11 00:46:18 +0000447 I->setOperand(i, 0); // Drop from the operand list.
448 }
449
450 I->eraseFromParent();
451 ++NumFastOther;
452}