Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 1 | //===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===// |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Owen Anderson | bf971aa | 2007-07-11 19:03:09 +0000 | [diff] [blame] | 5 | // This file was developed by Owen Anderson and is distributed under |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 6 | // 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 Anderson | 10e52ed | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "dse" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Scalar.h" |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 21 | #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 Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/AliasAnalysis.h" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/Local.h" |
| 31 | #include "llvm/Support/Compiler.h" |
| 32 | using namespace llvm; |
| 33 | |
| 34 | STATISTIC(NumFastStores, "Number of stores deleted"); |
| 35 | STATISTIC(NumFastOther , "Number of other instrs removed"); |
| 36 | |
| 37 | namespace { |
Owen Anderson | 10e52ed | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 38 | struct VISIBILITY_HIDDEN DSE : public FunctionPass { |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 39 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 10e52ed | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 40 | DSE() : FunctionPass((intptr_t)&ID) {} |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 41 | |
| 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 Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 50 | bool handleFreeWithNonTrivialDependency(FreeInst* F, |
| 51 | Instruction* dependency, |
| 52 | SetVector<Instruction*>& possiblyDead); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 53 | bool handleEndBlock(BasicBlock& BB, SetVector<Instruction*>& possiblyDead); |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 54 | bool RemoveUndeadPointers(Value* pointer, |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 55 | BasicBlock::iterator& BBI, |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 56 | SmallPtrSet<AllocaInst*, 64>& deadPointers, |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 57 | SetVector<Instruction*>& possiblyDead); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 58 | void DeleteDeadInstructionChains(Instruction *I, |
| 59 | SetVector<Instruction*> &DeadInsts); |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 60 | |
Owen Anderson | b17ab03 | 2007-08-08 06:06:02 +0000 | [diff] [blame] | 61 | /// 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 Anderson | 9c9ef21 | 2007-07-13 18:26:26 +0000 | [diff] [blame] | 66 | void TranslatePointerBitCasts(Value*& v) { |
Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 67 | assert(isa<PointerType>(v->getType()) && |
| 68 | "Translating a non-pointer type?"); |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 69 | while (true) { |
Owen Anderson | 09f8699 | 2007-07-16 23:34:39 +0000 | [diff] [blame] | 70 | 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 Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 74 | else |
| 75 | break; |
| 76 | } |
Owen Anderson | 9c9ef21 | 2007-07-13 18:26:26 +0000 | [diff] [blame] | 77 | } |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 78 | |
| 79 | // getAnalysisUsage - We require post dominance frontiers (aka Control |
| 80 | // Dependence Graph) |
| 81 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 82 | AU.setPreservesCFG(); |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 83 | AU.addRequired<TargetData>(); |
| 84 | AU.addRequired<AliasAnalysis>(); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 85 | AU.addRequired<MemoryDependenceAnalysis>(); |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 86 | AU.addPreserved<AliasAnalysis>(); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 87 | AU.addPreserved<MemoryDependenceAnalysis>(); |
| 88 | } |
| 89 | }; |
Owen Anderson | 10e52ed | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 90 | char DSE::ID = 0; |
| 91 | RegisterPass<DSE> X("dse", "Dead Store Elimination"); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Owen Anderson | 10e52ed | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 94 | FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); } |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 95 | |
Owen Anderson | 10e52ed | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 96 | bool DSE::runOnBasicBlock(BasicBlock &BB) { |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 97 | MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); |
| 98 | |
Owen Anderson | bf971aa | 2007-07-11 19:03:09 +0000 | [diff] [blame] | 99 | // Record the last-seen store to this pointer |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 100 | DenseMap<Value*, StoreInst*> lastStore; |
Owen Anderson | bf971aa | 2007-07-11 19:03:09 +0000 | [diff] [blame] | 101 | // Record instructions possibly made dead by deleting a store |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 102 | SetVector<Instruction*> possiblyDead; |
| 103 | |
| 104 | bool MadeChange = false; |
| 105 | |
| 106 | // Do a top-down walk on the BB |
Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 107 | for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); |
| 108 | BBI != BBE; ++BBI) { |
Owen Anderson | 1441470 | 2007-07-11 21:06:56 +0000 | [diff] [blame] | 109 | // If we find a store or a free... |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 110 | if (!isa<StoreInst>(BBI) && !isa<FreeInst>(BBI)) |
| 111 | continue; |
Owen Anderson | 9c9ef21 | 2007-07-13 18:26:26 +0000 | [diff] [blame] | 112 | |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 113 | Value* pointer = 0; |
Owen Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame^] | 114 | if (StoreInst* S = dyn_cast<StoreInst>(BBI)) { |
| 115 | if (!S->isVolatile()) |
| 116 | pointer = S->getPointerOperand(); |
| 117 | else |
| 118 | continue; |
| 119 | } else |
Owen Anderson | b17ab03 | 2007-08-08 06:06:02 +0000 | [diff] [blame] | 120 | pointer = cast<FreeInst>(BBI)->getPointerOperand(); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 121 | |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 122 | StoreInst*& last = lastStore[pointer]; |
| 123 | bool deletedStore = false; |
| 124 | |
| 125 | // ... to a pointer that has been stored to before... |
| 126 | if (last) { |
Owen Anderson | 9b1cc8c | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 127 | Instruction* dep = MD.getDependency(BBI); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 128 | |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 129 | // ... 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 Anderson | 9b1cc8c | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 134 | dep = MD.getDependency(BBI, dep); |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 135 | continue; |
Owen Anderson | d4451de | 2007-07-12 18:08:51 +0000 | [diff] [blame] | 136 | } |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 137 | |
| 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 Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 153 | } |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 154 | } |
| 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 Anderson | 9b1cc8c | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 160 | MD.getDependency(F), |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 161 | 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 Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 170 | // 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 Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 175 | // 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 Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 185 | /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose |
| 186 | /// dependency is a store to a field of that structure |
Owen Anderson | 10e52ed | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 187 | bool DSE::handleFreeWithNonTrivialDependency(FreeInst* F, Instruction* dep, |
Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 188 | SetVector<Instruction*>& possiblyDead) { |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 189 | TargetData &TD = getAnalysis<TargetData>(); |
| 190 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 191 | MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); |
| 192 | |
Owen Anderson | d4451de | 2007-07-12 18:08:51 +0000 | [diff] [blame] | 193 | 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 Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame^] | 200 | else if (dependency->isVolatile()) |
| 201 | return false; |
Owen Anderson | d4451de | 2007-07-12 18:08:51 +0000 | [diff] [blame] | 202 | |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 203 | Value* depPointer = dependency->getPointerOperand(); |
Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 204 | const Type* depType = dependency->getOperand(0)->getType(); |
| 205 | unsigned depPointerSize = TD.getTypeSize(depType); |
Owen Anderson | 9c9ef21 | 2007-07-13 18:26:26 +0000 | [diff] [blame] | 206 | |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 207 | // 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 Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 214 | |
| 215 | // DCE instructions only used to calculate that store |
| 216 | if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(0))) |
| 217 | possiblyDead.insert(D); |
Owen Anderson | 9c9ef21 | 2007-07-13 18:26:26 +0000 | [diff] [blame] | 218 | if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(1))) |
| 219 | possiblyDead.insert(D); |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 220 | |
| 221 | dependency->eraseFromParent(); |
| 222 | NumFastStores++; |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | return false; |
| 227 | } |
| 228 | |
Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 229 | /// handleEndBlock - Remove dead stores to stack-allocated locations in the |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 230 | /// function end block. Ex: |
| 231 | /// %A = alloca i32 |
| 232 | /// ... |
| 233 | /// store i32 1, i32* %A |
| 234 | /// ret void |
Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 235 | bool DSE::handleEndBlock(BasicBlock& BB, |
| 236 | SetVector<Instruction*>& possiblyDead) { |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 237 | 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 Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 244 | SmallPtrSet<AllocaInst*, 64> deadPointers; |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 245 | |
| 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 Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 259 | // If we find a store whose pointer is dead... |
| 260 | if (StoreInst* S = dyn_cast<StoreInst>(BBI)) { |
Owen Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame^] | 261 | if (!S->isVolatile()) { |
| 262 | Value* pointerOperand = S->getPointerOperand(); |
| 263 | // See through pointer-to-pointer bitcasts |
| 264 | TranslatePointerBitCasts(pointerOperand); |
Owen Anderson | 9c9ef21 | 2007-07-13 18:26:26 +0000 | [diff] [blame] | 265 | |
Owen Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame^] | 266 | if (deadPointers.count(pointerOperand)){ |
| 267 | // Remove it! |
| 268 | MD.removeInstruction(S); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 269 | |
Owen Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame^] | 270 | // 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 Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 275 | |
Owen Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame^] | 276 | BBI++; |
| 277 | S->eraseFromParent(); |
| 278 | NumFastStores++; |
| 279 | MadeChange = true; |
| 280 | } |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 281 | } |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 282 | |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | Value* killPointer = 0; |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 287 | |
| 288 | // If we encounter a use of the pointer, it is no longer considered dead |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 289 | if (LoadInst* L = dyn_cast<LoadInst>(BBI)) { |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 290 | killPointer = L->getPointerOperand(); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 291 | } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) { |
| 292 | killPointer = V->getOperand(0); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 293 | } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) { |
| 294 | deadPointers.erase(A); |
| 295 | continue; |
| 296 | } else if (CallSite::get(BBI).getInstruction() != 0) { |
Owen Anderson | 50df968 | 2007-08-08 17:58:56 +0000 | [diff] [blame] | 297 | // 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 Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 304 | unsigned modRef = 0; |
| 305 | unsigned other = 0; |
| 306 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 307 | // Remove any pointers made undead by the call from the dead set |
| 308 | std::vector<Instruction*> dead; |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 309 | for (SmallPtrSet<AllocaInst*, 64>::iterator I = deadPointers.begin(), |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 310 | E = deadPointers.end(); I != E; ++I) { |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 311 | // 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 Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 320 | // Get size information for the alloca |
| 321 | unsigned pointerSize = ~0UL; |
| 322 | if (ConstantInt* C = dyn_cast<ConstantInt>((*I)->getArraySize())) |
Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 323 | pointerSize = C->getZExtValue() * \ |
| 324 | TD.getTypeSize((*I)->getAllocatedType()); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 325 | |
| 326 | // See if the call site touches it |
Owen Anderson | 50df968 | 2007-08-08 17:58:56 +0000 | [diff] [blame] | 327 | AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize); |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 328 | |
| 329 | if (A == AliasAnalysis::ModRef) |
| 330 | modRef++; |
| 331 | else |
| 332 | other++; |
| 333 | |
Owen Anderson | 9c9ef21 | 2007-07-13 18:26:26 +0000 | [diff] [blame] | 334 | if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref) |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 335 | 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 Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 348 | TranslatePointerBitCasts(killPointer); |
| 349 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 350 | // Deal with undead pointers |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 351 | MadeChange |= RemoveUndeadPointers(killPointer, BBI, |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 352 | deadPointers, possiblyDead); |
| 353 | } |
| 354 | |
| 355 | return MadeChange; |
| 356 | } |
| 357 | |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 358 | /// RemoveUndeadPointers - check for uses of a pointer that make it |
| 359 | /// undead when scanning for dead stores to alloca's. |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 360 | bool DSE::RemoveUndeadPointers(Value* killPointer, |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 361 | BasicBlock::iterator& BBI, |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 362 | SmallPtrSet<AllocaInst*, 64>& deadPointers, |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 363 | SetVector<Instruction*>& possiblyDead) { |
| 364 | TargetData &TD = getAnalysis<TargetData>(); |
| 365 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 366 | MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); |
| 367 | |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 368 | // 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 Anderson | 6808628 | 2007-08-08 19:12:31 +0000 | [diff] [blame] | 374 | } else if (isa<GlobalValue>(killPointer)) { |
| 375 | // A global can't be in the dead pointer set |
| 376 | return false; |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 379 | bool MadeChange = false; |
| 380 | |
| 381 | std::vector<Instruction*> undead; |
| 382 | |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 383 | for (SmallPtrSet<AllocaInst*, 64>::iterator I = deadPointers.begin(), |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 384 | 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 Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 388 | pointerSize = C->getZExtValue() * \ |
| 389 | TD.getTypeSize((*I)->getAllocatedType()); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 390 | |
| 391 | // See if this pointer could alias it |
Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 392 | AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize, |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 393 | killPointer, ~0UL); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 394 | |
| 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 Anderson | 9c9ef21 | 2007-07-13 18:26:26 +0000 | [diff] [blame] | 405 | if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1))) |
| 406 | possiblyDead.insert(D); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 407 | |
| 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 Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 427 | /// 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 Anderson | 10e52ed | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 431 | void DSE::DeleteDeadInstructionChains(Instruction *I, |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 432 | 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 Anderson | bf971aa | 2007-07-11 19:03:09 +0000 | [diff] [blame] | 443 | 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 Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 447 | I->setOperand(i, 0); // Drop from the operand list. |
| 448 | } |
| 449 | |
| 450 | I->eraseFromParent(); |
| 451 | ++NumFastOther; |
| 452 | } |