blob: 667390fd337d50a83b132ddf448ad8b5d5544607 [file] [log] [blame]
Owen Anderson666f6fe2007-08-02 18:11:11 +00001//===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
Owen Andersonb77c4572007-07-11 00:46:18 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Andersonb77c4572007-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 Andersonf6a05f92007-08-01 06:36:51 +000018#define DEBUG_TYPE "dse"
Owen Andersonb77c4572007-07-11 00:46:18 +000019#include "llvm/Transforms/Scalar.h"
Owen Anderson43b26762007-07-12 21:41:30 +000020#include "llvm/Constants.h"
Owen Andersonb77c4572007-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 Andersona96c1f62007-07-11 23:19:17 +000027#include "llvm/Analysis/AliasAnalysis.h"
Owen Andersonb77c4572007-07-11 00:46:18 +000028#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Owen Andersona96c1f62007-07-11 23:19:17 +000029#include "llvm/Target/TargetData.h"
Owen Andersonb77c4572007-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 Andersonf6a05f92007-08-01 06:36:51 +000038 struct VISIBILITY_HIDDEN DSE : public FunctionPass {
Owen Andersonb77c4572007-07-11 00:46:18 +000039 static char ID; // Pass identification, replacement for typeid
Owen Andersonf6a05f92007-08-01 06:36:51 +000040 DSE() : FunctionPass((intptr_t)&ID) {}
Owen Andersonb77c4572007-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 Anderson666f6fe2007-08-02 18:11:11 +000050 bool handleFreeWithNonTrivialDependency(FreeInst* F,
51 Instruction* dependency,
52 SetVector<Instruction*>& possiblyDead);
Owen Anderson43b26762007-07-12 21:41:30 +000053 bool handleEndBlock(BasicBlock& BB, SetVector<Instruction*>& possiblyDead);
Owen Andersonbb3abf42007-08-08 17:50:09 +000054 bool RemoveUndeadPointers(Value* pointer,
Owen Anderson43b26762007-07-12 21:41:30 +000055 BasicBlock::iterator& BBI,
Owen Andersonbb3abf42007-08-08 17:50:09 +000056 SmallPtrSet<AllocaInst*, 64>& deadPointers,
Owen Anderson43b26762007-07-12 21:41:30 +000057 SetVector<Instruction*>& possiblyDead);
Owen Andersonb77c4572007-07-11 00:46:18 +000058 void DeleteDeadInstructionChains(Instruction *I,
59 SetVector<Instruction*> &DeadInsts);
Owen Anderson6ca4cb32007-08-08 04:52:29 +000060
Owen Andersonc182f172007-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 Anderson7ebba512007-11-01 05:29:16 +000066 void TranslatePointerBitCasts(Value*& v, bool zeroGepsOnly = false) {
Owen Anderson666f6fe2007-08-02 18:11:11 +000067 assert(isa<PointerType>(v->getType()) &&
68 "Translating a non-pointer type?");
Owen Anderson6ca4cb32007-08-08 04:52:29 +000069 while (true) {
Owen Andersonce17b1c2007-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))
Owen Anderson7ebba512007-11-01 05:29:16 +000073 if (!zeroGepsOnly || G->hasAllZeroIndices()) {
74 v = G->getOperand(0);
75 } else {
76 break;
77 }
Owen Anderson6ca4cb32007-08-08 04:52:29 +000078 else
79 break;
80 }
Owen Anderson3e8d0012007-07-13 18:26:26 +000081 }
Owen Andersonb77c4572007-07-11 00:46:18 +000082
83 // getAnalysisUsage - We require post dominance frontiers (aka Control
84 // Dependence Graph)
85 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
86 AU.setPreservesCFG();
Owen Andersona96c1f62007-07-11 23:19:17 +000087 AU.addRequired<TargetData>();
88 AU.addRequired<AliasAnalysis>();
Owen Andersonb77c4572007-07-11 00:46:18 +000089 AU.addRequired<MemoryDependenceAnalysis>();
Owen Andersona96c1f62007-07-11 23:19:17 +000090 AU.addPreserved<AliasAnalysis>();
Owen Andersonb77c4572007-07-11 00:46:18 +000091 AU.addPreserved<MemoryDependenceAnalysis>();
92 }
93 };
Owen Andersonf6a05f92007-08-01 06:36:51 +000094 char DSE::ID = 0;
95 RegisterPass<DSE> X("dse", "Dead Store Elimination");
Owen Andersonb77c4572007-07-11 00:46:18 +000096}
97
Owen Andersonf6a05f92007-08-01 06:36:51 +000098FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
Owen Andersonb77c4572007-07-11 00:46:18 +000099
Owen Andersonf6a05f92007-08-01 06:36:51 +0000100bool DSE::runOnBasicBlock(BasicBlock &BB) {
Owen Andersonb77c4572007-07-11 00:46:18 +0000101 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
Owen Anderson7ebba512007-11-01 05:29:16 +0000102 TargetData &TD = getAnalysis<TargetData>();
103
Owen Andersonbfbfb3c2007-07-11 19:03:09 +0000104 // Record the last-seen store to this pointer
Owen Andersonb77c4572007-07-11 00:46:18 +0000105 DenseMap<Value*, StoreInst*> lastStore;
Owen Andersonbfbfb3c2007-07-11 19:03:09 +0000106 // Record instructions possibly made dead by deleting a store
Owen Andersonb77c4572007-07-11 00:46:18 +0000107 SetVector<Instruction*> possiblyDead;
108
109 bool MadeChange = false;
110
111 // Do a top-down walk on the BB
Owen Anderson666f6fe2007-08-02 18:11:11 +0000112 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end();
113 BBI != BBE; ++BBI) {
Owen Anderson6f46d652007-07-11 21:06:56 +0000114 // If we find a store or a free...
Owen Anderson6ca4cb32007-08-08 04:52:29 +0000115 if (!isa<StoreInst>(BBI) && !isa<FreeInst>(BBI))
116 continue;
Owen Anderson3e8d0012007-07-13 18:26:26 +0000117
Owen Anderson6ca4cb32007-08-08 04:52:29 +0000118 Value* pointer = 0;
Owen Anderson5dfcf432007-08-26 21:14:47 +0000119 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
120 if (!S->isVolatile())
121 pointer = S->getPointerOperand();
122 else
123 continue;
124 } else
Owen Andersonc182f172007-08-08 06:06:02 +0000125 pointer = cast<FreeInst>(BBI)->getPointerOperand();
Owen Andersonb77c4572007-07-11 00:46:18 +0000126
Owen Anderson7ebba512007-11-01 05:29:16 +0000127 TranslatePointerBitCasts(pointer, true);
Owen Anderson6ca4cb32007-08-08 04:52:29 +0000128 StoreInst*& last = lastStore[pointer];
129 bool deletedStore = false;
130
131 // ... to a pointer that has been stored to before...
132 if (last) {
Owen Anderson9528f112007-08-09 04:42:44 +0000133 Instruction* dep = MD.getDependency(BBI);
Owen Andersonb77c4572007-07-11 00:46:18 +0000134
Owen Anderson6ca4cb32007-08-08 04:52:29 +0000135 // ... and no other memory dependencies are between them....
136 while (dep != MemoryDependenceAnalysis::None &&
137 dep != MemoryDependenceAnalysis::NonLocal &&
138 isa<StoreInst>(dep)) {
Owen Anderson7ebba512007-11-01 05:29:16 +0000139 if (dep != last ||
Duncan Sands514ab342007-11-01 20:53:16 +0000140 TD.getTypeStoreSize(last->getOperand(0)->getType()) >
141 TD.getTypeStoreSize(BBI->getOperand(0)->getType())) {
Owen Anderson9528f112007-08-09 04:42:44 +0000142 dep = MD.getDependency(BBI, dep);
Owen Anderson6ca4cb32007-08-08 04:52:29 +0000143 continue;
Owen Andersondd61c2b2007-07-12 18:08:51 +0000144 }
Owen Anderson6ca4cb32007-08-08 04:52:29 +0000145
146 // Remove it!
147 MD.removeInstruction(last);
148
149 // DCE instructions only used to calculate that store
150 if (Instruction* D = dyn_cast<Instruction>(last->getOperand(0)))
151 possiblyDead.insert(D);
152 if (Instruction* D = dyn_cast<Instruction>(last->getOperand(1)))
153 possiblyDead.insert(D);
154
155 last->eraseFromParent();
156 NumFastStores++;
157 deletedStore = true;
158 MadeChange = true;
159
160 break;
Owen Andersonb77c4572007-07-11 00:46:18 +0000161 }
Owen Anderson6ca4cb32007-08-08 04:52:29 +0000162 }
163
164 // Handle frees whose dependencies are non-trivial.
165 if (FreeInst* F = dyn_cast<FreeInst>(BBI)) {
166 if (!deletedStore)
167 MadeChange |= handleFreeWithNonTrivialDependency(F,
Owen Anderson9528f112007-08-09 04:42:44 +0000168 MD.getDependency(F),
Owen Anderson6ca4cb32007-08-08 04:52:29 +0000169 possiblyDead);
170 // No known stores after the free
171 last = 0;
172 } else {
173 // Update our most-recent-store map.
174 last = cast<StoreInst>(BBI);
Owen Andersonb77c4572007-07-11 00:46:18 +0000175 }
176 }
177
Owen Anderson43b26762007-07-12 21:41:30 +0000178 // If this block ends in a return, unwind, unreachable, and eventually
179 // tailcall, then all allocas are dead at its end.
180 if (BB.getTerminator()->getNumSuccessors() == 0)
181 MadeChange |= handleEndBlock(BB, possiblyDead);
182
Owen Andersonb77c4572007-07-11 00:46:18 +0000183 // Do a trivial DCE
184 while (!possiblyDead.empty()) {
185 Instruction *I = possiblyDead.back();
186 possiblyDead.pop_back();
187 DeleteDeadInstructionChains(I, possiblyDead);
188 }
189
190 return MadeChange;
191}
192
Owen Andersona96c1f62007-07-11 23:19:17 +0000193/// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
194/// dependency is a store to a field of that structure
Owen Andersonf6a05f92007-08-01 06:36:51 +0000195bool DSE::handleFreeWithNonTrivialDependency(FreeInst* F, Instruction* dep,
Owen Anderson666f6fe2007-08-02 18:11:11 +0000196 SetVector<Instruction*>& possiblyDead) {
Owen Andersona96c1f62007-07-11 23:19:17 +0000197 TargetData &TD = getAnalysis<TargetData>();
198 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
199 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
200
Owen Andersondd61c2b2007-07-12 18:08:51 +0000201 if (dep == MemoryDependenceAnalysis::None ||
202 dep == MemoryDependenceAnalysis::NonLocal)
203 return false;
204
205 StoreInst* dependency = dyn_cast<StoreInst>(dep);
206 if (!dependency)
207 return false;
Owen Anderson5dfcf432007-08-26 21:14:47 +0000208 else if (dependency->isVolatile())
209 return false;
Owen Andersondd61c2b2007-07-12 18:08:51 +0000210
Owen Andersona96c1f62007-07-11 23:19:17 +0000211 Value* depPointer = dependency->getPointerOperand();
Owen Anderson666f6fe2007-08-02 18:11:11 +0000212 const Type* depType = dependency->getOperand(0)->getType();
Duncan Sands514ab342007-11-01 20:53:16 +0000213 unsigned depPointerSize = TD.getTypeStoreSize(depType);
Owen Anderson3e8d0012007-07-13 18:26:26 +0000214
Owen Andersona96c1f62007-07-11 23:19:17 +0000215 // Check for aliasing
216 AliasAnalysis::AliasResult A = AA.alias(F->getPointerOperand(), ~0UL,
217 depPointer, depPointerSize);
218
219 if (A == AliasAnalysis::MustAlias) {
220 // Remove it!
221 MD.removeInstruction(dependency);
Owen Andersona96c1f62007-07-11 23:19:17 +0000222
223 // DCE instructions only used to calculate that store
224 if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(0)))
225 possiblyDead.insert(D);
Owen Anderson3e8d0012007-07-13 18:26:26 +0000226 if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(1)))
227 possiblyDead.insert(D);
Owen Andersona96c1f62007-07-11 23:19:17 +0000228
229 dependency->eraseFromParent();
230 NumFastStores++;
231 return true;
232 }
233
234 return false;
235}
236
Owen Anderson666f6fe2007-08-02 18:11:11 +0000237/// handleEndBlock - Remove dead stores to stack-allocated locations in the
Owen Andersonbb3abf42007-08-08 17:50:09 +0000238/// function end block. Ex:
239/// %A = alloca i32
240/// ...
241/// store i32 1, i32* %A
242/// ret void
Owen Anderson666f6fe2007-08-02 18:11:11 +0000243bool DSE::handleEndBlock(BasicBlock& BB,
244 SetVector<Instruction*>& possiblyDead) {
Owen Anderson43b26762007-07-12 21:41:30 +0000245 TargetData &TD = getAnalysis<TargetData>();
246 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
247 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
248
249 bool MadeChange = false;
250
251 // Pointers alloca'd in this function are dead in the end block
Owen Andersonbb3abf42007-08-08 17:50:09 +0000252 SmallPtrSet<AllocaInst*, 64> deadPointers;
Owen Anderson43b26762007-07-12 21:41:30 +0000253
254 // Find all of the alloca'd pointers in the entry block
255 BasicBlock *Entry = BB.getParent()->begin();
256 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
257 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
258 deadPointers.insert(AI);
259
260 // Scan the basic block backwards
261 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
262 --BBI;
263
264 if (deadPointers.empty())
265 break;
266
Owen Anderson43b26762007-07-12 21:41:30 +0000267 // If we find a store whose pointer is dead...
268 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
Owen Anderson5dfcf432007-08-26 21:14:47 +0000269 if (!S->isVolatile()) {
270 Value* pointerOperand = S->getPointerOperand();
271 // See through pointer-to-pointer bitcasts
272 TranslatePointerBitCasts(pointerOperand);
Owen Anderson3e8d0012007-07-13 18:26:26 +0000273
Chris Lattner6390ae02007-11-06 22:07:22 +0000274 if (isa<AllocaInst>(pointerOperand) &&
275 deadPointers.count(cast<AllocaInst>(pointerOperand))) {
Owen Anderson5dfcf432007-08-26 21:14:47 +0000276 // Remove it!
277 MD.removeInstruction(S);
Owen Anderson43b26762007-07-12 21:41:30 +0000278
Owen Anderson5dfcf432007-08-26 21:14:47 +0000279 // DCE instructions only used to calculate that store
280 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
281 possiblyDead.insert(D);
282 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
283 possiblyDead.insert(D);
Owen Anderson43b26762007-07-12 21:41:30 +0000284
Owen Anderson5dfcf432007-08-26 21:14:47 +0000285 BBI++;
286 S->eraseFromParent();
287 NumFastStores++;
288 MadeChange = true;
289 }
Owen Anderson43b26762007-07-12 21:41:30 +0000290 }
Owen Andersonbb3abf42007-08-08 17:50:09 +0000291
292 continue;
293 }
294
295 Value* killPointer = 0;
Owen Anderson43b26762007-07-12 21:41:30 +0000296
297 // If we encounter a use of the pointer, it is no longer considered dead
Owen Andersonbb3abf42007-08-08 17:50:09 +0000298 if (LoadInst* L = dyn_cast<LoadInst>(BBI)) {
Owen Anderson43b26762007-07-12 21:41:30 +0000299 killPointer = L->getPointerOperand();
Owen Anderson43b26762007-07-12 21:41:30 +0000300 } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
301 killPointer = V->getOperand(0);
Owen Anderson43b26762007-07-12 21:41:30 +0000302 } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
303 deadPointers.erase(A);
304 continue;
305 } else if (CallSite::get(BBI).getInstruction() != 0) {
Owen Andersondf359c22007-08-08 17:58:56 +0000306 // If this call does not access memory, it can't
307 // be undeadifying any of our pointers.
308 CallSite CS = CallSite::get(BBI);
Duncan Sandsdff67102007-12-01 07:51:45 +0000309 if (AA.doesNotAccessMemory(CS))
Owen Andersondf359c22007-08-08 17:58:56 +0000310 continue;
311
Owen Anderson362bb522007-08-08 18:38:28 +0000312 unsigned modRef = 0;
313 unsigned other = 0;
314
Owen Anderson43b26762007-07-12 21:41:30 +0000315 // Remove any pointers made undead by the call from the dead set
316 std::vector<Instruction*> dead;
Owen Andersonbb3abf42007-08-08 17:50:09 +0000317 for (SmallPtrSet<AllocaInst*, 64>::iterator I = deadPointers.begin(),
Owen Anderson43b26762007-07-12 21:41:30 +0000318 E = deadPointers.end(); I != E; ++I) {
Owen Anderson362bb522007-08-08 18:38:28 +0000319 // HACK: if we detect that our AA is imprecise, it's not
320 // worth it to scan the rest of the deadPointers set. Just
321 // assume that the AA will return ModRef for everything, and
322 // go ahead and bail.
323 if (modRef >= 16 && other == 0) {
324 deadPointers.clear();
325 return MadeChange;
326 }
327
Owen Anderson43b26762007-07-12 21:41:30 +0000328 // Get size information for the alloca
329 unsigned pointerSize = ~0UL;
330 if (ConstantInt* C = dyn_cast<ConstantInt>((*I)->getArraySize()))
Owen Anderson666f6fe2007-08-02 18:11:11 +0000331 pointerSize = C->getZExtValue() * \
Duncan Sands514ab342007-11-01 20:53:16 +0000332 TD.getABITypeSize((*I)->getAllocatedType());
Owen Anderson43b26762007-07-12 21:41:30 +0000333
334 // See if the call site touches it
Owen Andersondf359c22007-08-08 17:58:56 +0000335 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
Owen Anderson362bb522007-08-08 18:38:28 +0000336
337 if (A == AliasAnalysis::ModRef)
338 modRef++;
339 else
340 other++;
341
Owen Anderson3e8d0012007-07-13 18:26:26 +0000342 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
Owen Anderson43b26762007-07-12 21:41:30 +0000343 dead.push_back(*I);
344 }
345
346 for (std::vector<Instruction*>::iterator I = dead.begin(), E = dead.end();
347 I != E; ++I)
Chris Lattner6390ae02007-11-06 22:07:22 +0000348 if (AllocaInst *AI = dyn_cast<AllocaInst>(*I))
349 deadPointers.erase(AI);
Owen Anderson43b26762007-07-12 21:41:30 +0000350
351 continue;
352 }
353
354 if (!killPointer)
355 continue;
356
Owen Anderson362bb522007-08-08 18:38:28 +0000357 TranslatePointerBitCasts(killPointer);
358
Owen Anderson43b26762007-07-12 21:41:30 +0000359 // Deal with undead pointers
Owen Andersonbb3abf42007-08-08 17:50:09 +0000360 MadeChange |= RemoveUndeadPointers(killPointer, BBI,
Owen Anderson43b26762007-07-12 21:41:30 +0000361 deadPointers, possiblyDead);
362 }
363
364 return MadeChange;
365}
366
Owen Anderson362bb522007-08-08 18:38:28 +0000367/// RemoveUndeadPointers - check for uses of a pointer that make it
368/// undead when scanning for dead stores to alloca's.
Owen Andersonbb3abf42007-08-08 17:50:09 +0000369bool DSE::RemoveUndeadPointers(Value* killPointer,
Owen Anderson43b26762007-07-12 21:41:30 +0000370 BasicBlock::iterator& BBI,
Owen Andersonbb3abf42007-08-08 17:50:09 +0000371 SmallPtrSet<AllocaInst*, 64>& deadPointers,
Owen Anderson43b26762007-07-12 21:41:30 +0000372 SetVector<Instruction*>& possiblyDead) {
373 TargetData &TD = getAnalysis<TargetData>();
374 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
375 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
376
Owen Anderson362bb522007-08-08 18:38:28 +0000377 // If the kill pointer can be easily reduced to an alloca,
378 // don't bother doing extraneous AA queries
379 if (AllocaInst* A = dyn_cast<AllocaInst>(killPointer)) {
380 if (deadPointers.count(A))
381 deadPointers.erase(A);
382 return false;
Owen Anderson838014e2007-08-08 19:12:31 +0000383 } else if (isa<GlobalValue>(killPointer)) {
384 // A global can't be in the dead pointer set
385 return false;
Owen Anderson362bb522007-08-08 18:38:28 +0000386 }
387
Owen Anderson43b26762007-07-12 21:41:30 +0000388 bool MadeChange = false;
389
390 std::vector<Instruction*> undead;
391
Owen Andersonbb3abf42007-08-08 17:50:09 +0000392 for (SmallPtrSet<AllocaInst*, 64>::iterator I = deadPointers.begin(),
Owen Anderson43b26762007-07-12 21:41:30 +0000393 E = deadPointers.end(); I != E; ++I) {
394 // Get size information for the alloca
395 unsigned pointerSize = ~0UL;
396 if (ConstantInt* C = dyn_cast<ConstantInt>((*I)->getArraySize()))
Owen Anderson666f6fe2007-08-02 18:11:11 +0000397 pointerSize = C->getZExtValue() * \
Duncan Sands514ab342007-11-01 20:53:16 +0000398 TD.getABITypeSize((*I)->getAllocatedType());
Owen Anderson43b26762007-07-12 21:41:30 +0000399
400 // See if this pointer could alias it
Owen Anderson666f6fe2007-08-02 18:11:11 +0000401 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
Owen Andersonbb3abf42007-08-08 17:50:09 +0000402 killPointer, ~0UL);
Owen Anderson43b26762007-07-12 21:41:30 +0000403
404 // If it must-alias and a store, we can delete it
405 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
406 StoreInst* S = cast<StoreInst>(BBI);
407
408 // Remove it!
409 MD.removeInstruction(S);
410
411 // DCE instructions only used to calculate that store
412 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
413 possiblyDead.insert(D);
Owen Anderson3e8d0012007-07-13 18:26:26 +0000414 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
415 possiblyDead.insert(D);
Owen Anderson43b26762007-07-12 21:41:30 +0000416
417 BBI++;
418 S->eraseFromParent();
419 NumFastStores++;
420 MadeChange = true;
421
422 continue;
423
424 // Otherwise, it is undead
425 } else if (A != AliasAnalysis::NoAlias)
426 undead.push_back(*I);
427 }
428
429 for (std::vector<Instruction*>::iterator I = undead.begin(), E = undead.end();
430 I != E; ++I)
Chris Lattner6390ae02007-11-06 22:07:22 +0000431 if (AllocaInst *AI = dyn_cast<AllocaInst>(*I))
432 deadPointers.erase(AI);
Owen Anderson43b26762007-07-12 21:41:30 +0000433
434 return MadeChange;
435}
436
Owen Andersonbb3abf42007-08-08 17:50:09 +0000437/// DeleteDeadInstructionChains - takes an instruction and a setvector of
438/// dead instructions. If I is dead, it is erased, and its operands are
439/// checked for deadness. If they are dead, they are added to the dead
440/// setvector.
Owen Andersonf6a05f92007-08-01 06:36:51 +0000441void DSE::DeleteDeadInstructionChains(Instruction *I,
Owen Andersonb77c4572007-07-11 00:46:18 +0000442 SetVector<Instruction*> &DeadInsts) {
443 // Instruction must be dead.
444 if (!I->use_empty() || !isInstructionTriviallyDead(I)) return;
445
446 // Let the memory dependence know
447 getAnalysis<MemoryDependenceAnalysis>().removeInstruction(I);
448
449 // See if this made any operands dead. We do it this way in case the
450 // instruction uses the same operand twice. We don't want to delete a
451 // value then reference it.
452 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Owen Andersonbfbfb3c2007-07-11 19:03:09 +0000453 if (I->getOperand(i)->hasOneUse())
454 if (Instruction* Op = dyn_cast<Instruction>(I->getOperand(i)))
455 DeadInsts.insert(Op); // Attempt to nuke it later.
456
Owen Andersonb77c4572007-07-11 00:46:18 +0000457 I->setOperand(i, 0); // Drop from the operand list.
458 }
459
460 I->eraseFromParent();
461 ++NumFastOther;
462}