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 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 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" |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 23 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 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 | 3f33897 | 2008-07-28 16:14:26 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/Dominators.h" |
Victor Hernandez | f390e04 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/MemoryBuiltins.h" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetData.h" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/Local.h" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
| 35 | STATISTIC(NumFastStores, "Number of stores deleted"); |
| 36 | STATISTIC(NumFastOther , "Number of other instrs removed"); |
| 37 | |
| 38 | namespace { |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 39 | struct DSE : public FunctionPass { |
Dan Gohman | 67243a4 | 2009-07-24 18:13:53 +0000 | [diff] [blame] | 40 | TargetData *TD; |
| 41 | |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 42 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | a79db30 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 43 | DSE() : FunctionPass(&ID) {} |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 44 | |
| 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 Lattner | de04e11 | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 51 | |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 52 | bool runOnBasicBlock(BasicBlock &BB); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 53 | bool handleFreeWithNonTrivialDependency(Instruction *F, MemDepResult Dep); |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 54 | bool handleEndBlock(BasicBlock &BB); |
Chris Lattner | 57e91ea | 2008-12-06 00:53:22 +0000 | [diff] [blame] | 55 | bool RemoveUndeadPointers(Value* Ptr, uint64_t killPointerSize, |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 56 | BasicBlock::iterator& BBI, |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 57 | SmallPtrSet<Value*, 64>& deadPointers); |
| 58 | void DeleteDeadInstruction(Instruction *I, |
| 59 | SmallPtrSet<Value*, 64> *deadPointers = 0); |
| 60 | |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 61 | |
| 62 | // getAnalysisUsage - We require post dominance frontiers (aka Control |
| 63 | // Dependence Graph) |
| 64 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 65 | AU.setPreservesCFG(); |
Owen Anderson | 3f33897 | 2008-07-28 16:14:26 +0000 | [diff] [blame] | 66 | AU.addRequired<DominatorTree>(); |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 67 | AU.addRequired<AliasAnalysis>(); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 68 | AU.addRequired<MemoryDependenceAnalysis>(); |
Owen Anderson | 3f33897 | 2008-07-28 16:14:26 +0000 | [diff] [blame] | 69 | AU.addPreserved<DominatorTree>(); |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 70 | AU.addPreserved<AliasAnalysis>(); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 71 | AU.addPreserved<MemoryDependenceAnalysis>(); |
| 72 | } |
| 73 | }; |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 76 | char DSE::ID = 0; |
| 77 | static RegisterPass<DSE> X("dse", "Dead Store Elimination"); |
| 78 | |
Owen Anderson | 10e52ed | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 79 | FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); } |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 80 | |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 81 | /// doesClobberMemory - Does this instruction clobber (write without reading) |
| 82 | /// some memory? |
| 83 | static bool doesClobberMemory(Instruction *I) { |
| 84 | if (isa<StoreInst>(I)) |
| 85 | return true; |
| 86 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 87 | switch (II->getIntrinsicID()) { |
| 88 | default: return false; |
| 89 | case Intrinsic::memset: case Intrinsic::memmove: case Intrinsic::memcpy: |
| 90 | case Intrinsic::lifetime_end: return true; |
| 91 | } |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | /// isElidable - If the memory this instruction and the memory it writes to is |
| 97 | /// unused, may we delete this instrtction? |
| 98 | static bool isElidable(Instruction *I) { |
| 99 | assert(doesClobberMemory(I)); |
| 100 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 101 | return II->getIntrinsicID() != Intrinsic::lifetime_end; |
| 102 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 103 | return !SI->isVolatile(); |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | /// getPointerOperand - Return the pointer that is being clobbered. |
| 108 | static Value *getPointerOperand(Instruction *I) { |
| 109 | assert(doesClobberMemory(I)); |
| 110 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 111 | return SI->getPointerOperand(); |
| 112 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) |
| 113 | return MI->getOperand(1); |
| 114 | assert(cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::lifetime_end); |
| 115 | return cast<IntrinsicInst>(I)->getOperand(2); |
| 116 | } |
| 117 | |
| 118 | /// getStoreSize - Return the length in bytes of the write by the clobbering |
| 119 | /// instruction. If variable or unknown, returns -1. |
| 120 | static unsigned getStoreSize(Instruction *I, const TargetData *TD) { |
| 121 | assert(doesClobberMemory(I)); |
| 122 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 123 | if (!TD) return -1u; |
| 124 | const PointerType *PTy = |
| 125 | cast<PointerType>(SI->getPointerOperand()->getType()); |
| 126 | return TD->getTypeStoreSize(PTy->getElementType()); |
| 127 | } |
| 128 | |
| 129 | Value *Len; |
| 130 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { |
| 131 | Len = MI->getLength(); |
| 132 | } else { |
| 133 | IntrinsicInst *II = cast<IntrinsicInst>(I); |
| 134 | assert(II->getIntrinsicID() == Intrinsic::lifetime_end); |
| 135 | Len = II->getOperand(0); |
| 136 | } |
| 137 | if (ConstantInt *LenCI = dyn_cast<ConstantInt>(Len)) |
| 138 | if (!LenCI->isAllOnesValue()) |
| 139 | return LenCI->getZExtValue(); |
| 140 | return -1u; |
| 141 | } |
| 142 | |
| 143 | /// isStoreAtLeastAsWideAs - Return true if the size of the store in I1 is |
| 144 | /// greater than or equal to the store in I2. This returns false if we don't |
| 145 | /// know. |
Chris Lattner | a090627 | 2009-11-04 23:20:12 +0000 | [diff] [blame] | 146 | /// |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 147 | static bool isStoreAtLeastAsWideAs(Instruction *I1, Instruction *I2, |
| 148 | const TargetData *TD) { |
| 149 | const Type *I1Ty = getPointerOperand(I1)->getType(); |
| 150 | const Type *I2Ty = getPointerOperand(I2)->getType(); |
Chris Lattner | a090627 | 2009-11-04 23:20:12 +0000 | [diff] [blame] | 151 | |
| 152 | // Exactly the same type, must have exactly the same size. |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 153 | if (I1Ty == I2Ty) return true; |
Chris Lattner | a090627 | 2009-11-04 23:20:12 +0000 | [diff] [blame] | 154 | |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 155 | int I1Size = getStoreSize(I1, TD); |
| 156 | int I2Size = getStoreSize(I2, TD); |
Chris Lattner | a090627 | 2009-11-04 23:20:12 +0000 | [diff] [blame] | 157 | |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 158 | return I1Size != -1 && I2Size != -1 && I1Size >= I2Size; |
Chris Lattner | a090627 | 2009-11-04 23:20:12 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Owen Anderson | 10e52ed | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 161 | bool DSE::runOnBasicBlock(BasicBlock &BB) { |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 162 | MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); |
Dan Gohman | 67243a4 | 2009-07-24 18:13:53 +0000 | [diff] [blame] | 163 | TD = getAnalysisIfAvailable<TargetData>(); |
Owen Anderson | 2ed651a | 2007-11-01 05:29:16 +0000 | [diff] [blame] | 164 | |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 165 | bool MadeChange = false; |
| 166 | |
Chris Lattner | 4916267 | 2009-09-02 06:31:02 +0000 | [diff] [blame] | 167 | // Do a top-down walk on the BB. |
Chris Lattner | f2a8ba4 | 2008-11-28 21:29:52 +0000 | [diff] [blame] | 168 | for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) { |
| 169 | Instruction *Inst = BBI++; |
| 170 | |
Dan Gohman | 67243a4 | 2009-07-24 18:13:53 +0000 | [diff] [blame] | 171 | // If we find a store or a free, get its memory dependence. |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 172 | if (!doesClobberMemory(Inst) && !isFreeCall(Inst)) |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 173 | continue; |
Chris Lattner | 5df5b4c | 2008-12-07 00:25:15 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 57e91ea | 2008-12-06 00:53:22 +0000 | [diff] [blame] | 175 | MemDepResult InstDep = MD.getDependency(Inst); |
Chris Lattner | f2a8ba4 | 2008-11-28 21:29:52 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 57e91ea | 2008-12-06 00:53:22 +0000 | [diff] [blame] | 177 | // Ignore non-local stores. |
| 178 | // FIXME: cross-block DSE would be fun. :) |
| 179 | if (InstDep.isNonLocal()) continue; |
| 180 | |
| 181 | // Handle frees whose dependencies are non-trivial. |
Victor Hernandez | de5ad42 | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 182 | if (isFreeCall(Inst)) { |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 183 | MadeChange |= handleFreeWithNonTrivialDependency(Inst, InstDep); |
Chris Lattner | 57e91ea | 2008-12-06 00:53:22 +0000 | [diff] [blame] | 184 | continue; |
| 185 | } |
| 186 | |
Chris Lattner | 57e91ea | 2008-12-06 00:53:22 +0000 | [diff] [blame] | 187 | // If not a definite must-alias dependency, ignore it. |
| 188 | if (!InstDep.isDef()) |
| 189 | continue; |
| 190 | |
| 191 | // If this is a store-store dependence, then the previous store is dead so |
| 192 | // long as this store is at least as big as it. |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 193 | if (doesClobberMemory(InstDep.getInst())) { |
| 194 | Instruction *DepStore = InstDep.getInst(); |
| 195 | if (isStoreAtLeastAsWideAs(Inst, DepStore, TD) && |
| 196 | isElidable(DepStore)) { |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 197 | // Delete the store and now-dead instructions that feed it. |
Chris Lattner | 57e91ea | 2008-12-06 00:53:22 +0000 | [diff] [blame] | 198 | DeleteDeadInstruction(DepStore); |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 199 | NumFastStores++; |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 200 | MadeChange = true; |
Chris Lattner | 4916267 | 2009-09-02 06:31:02 +0000 | [diff] [blame] | 201 | |
| 202 | // DeleteDeadInstruction can delete the current instruction in loop |
| 203 | // cases, reset BBI. |
| 204 | BBI = Inst; |
Chris Lattner | 8c5ff51 | 2008-11-29 20:29:04 +0000 | [diff] [blame] | 205 | if (BBI != BB.begin()) |
Chris Lattner | f3f6a80 | 2008-11-28 22:50:08 +0000 | [diff] [blame] | 206 | --BBI; |
Chris Lattner | f2a8ba4 | 2008-11-28 21:29:52 +0000 | [diff] [blame] | 207 | continue; |
| 208 | } |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 209 | } |
| 210 | |
| 211 | if (!isElidable(Inst)) |
| 212 | continue; |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 57e91ea | 2008-12-06 00:53:22 +0000 | [diff] [blame] | 214 | // If we're storing the same value back to a pointer that we just |
| 215 | // loaded from, then the store can be removed. |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 216 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 217 | if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) { |
| 218 | if (SI->getPointerOperand() == DepLoad->getPointerOperand() && |
| 219 | SI->getOperand(0) == DepLoad) { |
| 220 | // DeleteDeadInstruction can delete the current instruction. Save BBI |
| 221 | // in case we need it. |
| 222 | WeakVH NextInst(BBI); |
| 223 | |
| 224 | DeleteDeadInstruction(SI); |
| 225 | |
| 226 | if (NextInst == 0) // Next instruction deleted. |
| 227 | BBI = BB.begin(); |
| 228 | else if (BBI != BB.begin()) // Revisit this instruction if possible. |
| 229 | --BBI; |
| 230 | NumFastStores++; |
| 231 | MadeChange = true; |
| 232 | continue; |
| 233 | } |
Chris Lattner | 0e3d633 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 234 | } |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 235 | } |
Owen Anderson | 2b2bd28 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 236 | |
| 237 | // If this is a lifetime end marker, we can throw away the store. |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 238 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(InstDep.getInst())) { |
Owen Anderson | 2b2bd28 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 239 | if (II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 240 | // Delete the store and now-dead instructions that feed it. |
| 241 | // DeleteDeadInstruction can delete the current instruction. Save BBI |
| 242 | // in case we need it. |
| 243 | WeakVH NextInst(BBI); |
| 244 | |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 245 | DeleteDeadInstruction(Inst); |
Owen Anderson | 2b2bd28 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 246 | |
| 247 | if (NextInst == 0) // Next instruction deleted. |
| 248 | BBI = BB.begin(); |
| 249 | else if (BBI != BB.begin()) // Revisit this instruction if possible. |
| 250 | --BBI; |
| 251 | NumFastStores++; |
| 252 | MadeChange = true; |
| 253 | continue; |
| 254 | } |
| 255 | } |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | f2a8ba4 | 2008-11-28 21:29:52 +0000 | [diff] [blame] | 258 | // If this block ends in a return, unwind, or unreachable, all allocas are |
| 259 | // dead at its end, which means stores to them are also dead. |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 260 | if (BB.getTerminator()->getNumSuccessors() == 0) |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 261 | MadeChange |= handleEndBlock(BB); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 262 | |
| 263 | return MadeChange; |
| 264 | } |
| 265 | |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 266 | /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 267 | /// dependency is a store to a field of that structure. |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 268 | bool DSE::handleFreeWithNonTrivialDependency(Instruction *F, MemDepResult Dep) { |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 269 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 270 | |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 271 | Instruction *Dependency = Dep.getInst(); |
| 272 | if (!Dependency || !doesClobberMemory(Dependency) || !isElidable(Dependency)) |
Owen Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame] | 273 | return false; |
Owen Anderson | d4451de | 2007-07-12 18:08:51 +0000 | [diff] [blame] | 274 | |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 275 | Value *DepPointer = getPointerOperand(Dependency)->getUnderlyingObject(); |
Duncan Sands | fe3bef0 | 2008-01-20 10:49:23 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 57e91ea | 2008-12-06 00:53:22 +0000 | [diff] [blame] | 277 | // Check for aliasing. |
Victor Hernandez | de5ad42 | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 278 | if (AA.alias(F->getOperand(1), 1, DepPointer, 1) != |
Chris Lattner | 57e91ea | 2008-12-06 00:53:22 +0000 | [diff] [blame] | 279 | AliasAnalysis::MustAlias) |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 280 | return false; |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 282 | // DCE instructions only used to calculate that store |
Chris Lattner | 57e91ea | 2008-12-06 00:53:22 +0000 | [diff] [blame] | 283 | DeleteDeadInstruction(Dependency); |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 284 | NumFastStores++; |
| 285 | return true; |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 288 | /// handleEndBlock - Remove dead stores to stack-allocated locations in the |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 289 | /// function end block. Ex: |
| 290 | /// %A = alloca i32 |
| 291 | /// ... |
| 292 | /// store i32 1, i32* %A |
| 293 | /// ret void |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 294 | bool DSE::handleEndBlock(BasicBlock &BB) { |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 295 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 296 | |
| 297 | bool MadeChange = false; |
| 298 | |
| 299 | // Pointers alloca'd in this function are dead in the end block |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 300 | SmallPtrSet<Value*, 64> deadPointers; |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 301 | |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 302 | // Find all of the alloca'd pointers in the entry block. |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 303 | BasicBlock *Entry = BB.getParent()->begin(); |
| 304 | for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I) |
| 305 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 306 | deadPointers.insert(AI); |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 307 | |
| 308 | // Treat byval arguments the same, stores to them are dead at the end of the |
| 309 | // function. |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 310 | for (Function::arg_iterator AI = BB.getParent()->arg_begin(), |
| 311 | AE = BB.getParent()->arg_end(); AI != AE; ++AI) |
| 312 | if (AI->hasByValAttr()) |
| 313 | deadPointers.insert(AI); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 314 | |
| 315 | // Scan the basic block backwards |
| 316 | for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){ |
| 317 | --BBI; |
| 318 | |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 319 | // If we find a store whose pointer is dead. |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 320 | if (doesClobberMemory(BBI)) { |
| 321 | if (isElidable(BBI)) { |
Owen Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame] | 322 | // See through pointer-to-pointer bitcasts |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 323 | Value *pointerOperand = getPointerOperand(BBI)->getUnderlyingObject(); |
Duncan Sands | d65a4da | 2008-10-01 15:25:41 +0000 | [diff] [blame] | 324 | |
Owen Anderson | 6af19fd | 2008-01-25 10:10:33 +0000 | [diff] [blame] | 325 | // Alloca'd pointers or byval arguments (which are functionally like |
| 326 | // alloca's) are valid candidates for removal. |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 327 | if (deadPointers.count(pointerOperand)) { |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 328 | // DCE instructions only used to calculate that store. |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 329 | Instruction *Dead = BBI; |
Owen Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame] | 330 | BBI++; |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 331 | DeleteDeadInstruction(Dead, &deadPointers); |
Owen Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame] | 332 | NumFastStores++; |
| 333 | MadeChange = true; |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 334 | continue; |
Owen Anderson | 2b9ec7f | 2007-08-26 21:14:47 +0000 | [diff] [blame] | 335 | } |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 336 | } |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 337 | |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 338 | // Because a memcpy or memmove is also a load, we can't skip it if we |
| 339 | // didn't remove it. |
| 340 | if (!isa<MemTransferInst>(BBI)) |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 341 | continue; |
Owen Anderson | 52aaabf | 2007-08-08 17:50:09 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | Value* killPointer = 0; |
Owen Anderson | a82c993 | 2008-02-04 04:53:00 +0000 | [diff] [blame] | 345 | uint64_t killPointerSize = ~0UL; |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 346 | |
| 347 | // If we encounter a use of the pointer, it is no longer considered dead |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 348 | if (LoadInst *L = dyn_cast<LoadInst>(BBI)) { |
Nate Begeman | 53c5c62 | 2008-05-13 01:48:26 +0000 | [diff] [blame] | 349 | // However, if this load is unused and not volatile, we can go ahead and |
| 350 | // remove it, and not have to worry about it making our pointer undead! |
Dan Gohman | 8cb19d9 | 2008-04-28 19:51:27 +0000 | [diff] [blame] | 351 | if (L->use_empty() && !L->isVolatile()) { |
Owen Anderson | 4e4b116 | 2008-01-30 01:24:47 +0000 | [diff] [blame] | 352 | BBI++; |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 353 | DeleteDeadInstruction(L, &deadPointers); |
Owen Anderson | 4e4b116 | 2008-01-30 01:24:47 +0000 | [diff] [blame] | 354 | NumFastOther++; |
| 355 | MadeChange = true; |
Owen Anderson | 4e4b116 | 2008-01-30 01:24:47 +0000 | [diff] [blame] | 356 | continue; |
| 357 | } |
| 358 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 359 | killPointer = L->getPointerOperand(); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 360 | } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) { |
| 361 | killPointer = V->getOperand(0); |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 362 | } else if (isa<MemTransferInst>(BBI) && |
| 363 | isa<ConstantInt>(cast<MemTransferInst>(BBI)->getLength())) { |
| 364 | killPointer = cast<MemTransferInst>(BBI)->getSource(); |
Owen Anderson | a82c993 | 2008-02-04 04:53:00 +0000 | [diff] [blame] | 365 | killPointerSize = cast<ConstantInt>( |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame^] | 366 | cast<MemTransferInst>(BBI)->getLength())->getZExtValue(); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 367 | } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) { |
| 368 | deadPointers.erase(A); |
Owen Anderson | 4e4b116 | 2008-01-30 01:24:47 +0000 | [diff] [blame] | 369 | |
| 370 | // Dead alloca's can be DCE'd when we reach them |
Nick Lewycky | 6b01670 | 2008-01-30 08:01:28 +0000 | [diff] [blame] | 371 | if (A->use_empty()) { |
Owen Anderson | 4e4b116 | 2008-01-30 01:24:47 +0000 | [diff] [blame] | 372 | BBI++; |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 373 | DeleteDeadInstruction(A, &deadPointers); |
Owen Anderson | 4e4b116 | 2008-01-30 01:24:47 +0000 | [diff] [blame] | 374 | NumFastOther++; |
| 375 | MadeChange = true; |
Owen Anderson | 4e4b116 | 2008-01-30 01:24:47 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 378 | continue; |
| 379 | } else if (CallSite::get(BBI).getInstruction() != 0) { |
Owen Anderson | 50df968 | 2007-08-08 17:58:56 +0000 | [diff] [blame] | 380 | // If this call does not access memory, it can't |
| 381 | // be undeadifying any of our pointers. |
| 382 | CallSite CS = CallSite::get(BBI); |
Duncan Sands | 68b6f50 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 383 | if (AA.doesNotAccessMemory(CS)) |
Owen Anderson | 50df968 | 2007-08-08 17:58:56 +0000 | [diff] [blame] | 384 | continue; |
| 385 | |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 386 | unsigned modRef = 0; |
| 387 | unsigned other = 0; |
| 388 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 389 | // Remove any pointers made undead by the call from the dead set |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 390 | std::vector<Value*> dead; |
| 391 | for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(), |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 392 | E = deadPointers.end(); I != E; ++I) { |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 393 | // HACK: if we detect that our AA is imprecise, it's not |
| 394 | // worth it to scan the rest of the deadPointers set. Just |
| 395 | // assume that the AA will return ModRef for everything, and |
| 396 | // go ahead and bail. |
| 397 | if (modRef >= 16 && other == 0) { |
| 398 | deadPointers.clear(); |
| 399 | return MadeChange; |
| 400 | } |
Duncan Sands | fe3bef0 | 2008-01-20 10:49:23 +0000 | [diff] [blame] | 401 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 402 | // Get size information for the alloca |
Duncan Sands | fe3bef0 | 2008-01-20 10:49:23 +0000 | [diff] [blame] | 403 | unsigned pointerSize = ~0U; |
Dan Gohman | 67243a4 | 2009-07-24 18:13:53 +0000 | [diff] [blame] | 404 | if (TD) { |
| 405 | if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) { |
| 406 | if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize())) |
| 407 | pointerSize = C->getZExtValue() * |
| 408 | TD->getTypeAllocSize(A->getAllocatedType()); |
| 409 | } else { |
| 410 | const PointerType* PT = cast<PointerType>( |
| 411 | cast<Argument>(*I)->getType()); |
| 412 | pointerSize = TD->getTypeAllocSize(PT->getElementType()); |
| 413 | } |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 414 | } |
Duncan Sands | fe3bef0 | 2008-01-20 10:49:23 +0000 | [diff] [blame] | 415 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 416 | // See if the call site touches it |
Owen Anderson | 50df968 | 2007-08-08 17:58:56 +0000 | [diff] [blame] | 417 | AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize); |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 418 | |
| 419 | if (A == AliasAnalysis::ModRef) |
| 420 | modRef++; |
| 421 | else |
| 422 | other++; |
| 423 | |
Owen Anderson | 9c9ef21 | 2007-07-13 18:26:26 +0000 | [diff] [blame] | 424 | if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref) |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 425 | dead.push_back(*I); |
| 426 | } |
| 427 | |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 428 | for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end(); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 429 | I != E; ++I) |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 430 | deadPointers.erase(*I); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 431 | |
| 432 | continue; |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 433 | } else if (isInstructionTriviallyDead(BBI)) { |
Owen Anderson | 4e4b116 | 2008-01-30 01:24:47 +0000 | [diff] [blame] | 434 | // For any non-memory-affecting non-terminators, DCE them as we reach them |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 435 | Instruction *Inst = BBI; |
| 436 | BBI++; |
| 437 | DeleteDeadInstruction(Inst, &deadPointers); |
| 438 | NumFastOther++; |
| 439 | MadeChange = true; |
| 440 | continue; |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | if (!killPointer) |
| 444 | continue; |
Duncan Sands | d65a4da | 2008-10-01 15:25:41 +0000 | [diff] [blame] | 445 | |
| 446 | killPointer = killPointer->getUnderlyingObject(); |
| 447 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 448 | // Deal with undead pointers |
Owen Anderson | a82c993 | 2008-02-04 04:53:00 +0000 | [diff] [blame] | 449 | MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI, |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 450 | deadPointers); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | return MadeChange; |
| 454 | } |
| 455 | |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 456 | /// RemoveUndeadPointers - check for uses of a pointer that make it |
| 457 | /// undead when scanning for dead stores to alloca's. |
Owen Anderson | a82c993 | 2008-02-04 04:53:00 +0000 | [diff] [blame] | 458 | bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize, |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 459 | BasicBlock::iterator &BBI, |
| 460 | SmallPtrSet<Value*, 64>& deadPointers) { |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 461 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 462 | |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 463 | // If the kill pointer can be easily reduced to an alloca, |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 464 | // don't bother doing extraneous AA queries. |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 465 | if (deadPointers.count(killPointer)) { |
| 466 | deadPointers.erase(killPointer); |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 467 | return false; |
| 468 | } |
| 469 | |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 470 | // A global can't be in the dead pointer set. |
| 471 | if (isa<GlobalValue>(killPointer)) |
| 472 | return false; |
| 473 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 474 | bool MadeChange = false; |
| 475 | |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 476 | SmallVector<Value*, 16> undead; |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 477 | |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 478 | for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(), |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 479 | E = deadPointers.end(); I != E; ++I) { |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 480 | // Get size information for the alloca. |
Duncan Sands | fe3bef0 | 2008-01-20 10:49:23 +0000 | [diff] [blame] | 481 | unsigned pointerSize = ~0U; |
Dan Gohman | 67243a4 | 2009-07-24 18:13:53 +0000 | [diff] [blame] | 482 | if (TD) { |
| 483 | if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) { |
| 484 | if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize())) |
| 485 | pointerSize = C->getZExtValue() * |
| 486 | TD->getTypeAllocSize(A->getAllocatedType()); |
| 487 | } else { |
| 488 | const PointerType* PT = cast<PointerType>(cast<Argument>(*I)->getType()); |
| 489 | pointerSize = TD->getTypeAllocSize(PT->getElementType()); |
| 490 | } |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 491 | } |
Duncan Sands | fe3bef0 | 2008-01-20 10:49:23 +0000 | [diff] [blame] | 492 | |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 493 | // See if this pointer could alias it |
Owen Anderson | e359058 | 2007-08-02 18:11:11 +0000 | [diff] [blame] | 494 | AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize, |
Owen Anderson | a82c993 | 2008-02-04 04:53:00 +0000 | [diff] [blame] | 495 | killPointer, killPointerSize); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 496 | |
| 497 | // If it must-alias and a store, we can delete it |
| 498 | if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) { |
| 499 | StoreInst* S = cast<StoreInst>(BBI); |
| 500 | |
| 501 | // Remove it! |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 502 | BBI++; |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 503 | DeleteDeadInstruction(S, &deadPointers); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 504 | NumFastStores++; |
| 505 | MadeChange = true; |
| 506 | |
| 507 | continue; |
| 508 | |
| 509 | // Otherwise, it is undead |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 510 | } else if (A != AliasAnalysis::NoAlias) |
| 511 | undead.push_back(*I); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 514 | for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end(); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 515 | I != E; ++I) |
Owen Anderson | 48d3780 | 2008-01-29 06:18:36 +0000 | [diff] [blame] | 516 | deadPointers.erase(*I); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 517 | |
| 518 | return MadeChange; |
| 519 | } |
| 520 | |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 521 | /// DeleteDeadInstruction - Delete this instruction. Before we do, go through |
| 522 | /// and zero out all the operands of this instruction. If any of them become |
| 523 | /// dead, delete them and the computation tree that feeds them. |
| 524 | /// |
| 525 | /// If ValueSet is non-null, remove any deleted instructions from it as well. |
| 526 | /// |
| 527 | void DSE::DeleteDeadInstruction(Instruction *I, |
| 528 | SmallPtrSet<Value*, 64> *ValueSet) { |
| 529 | SmallVector<Instruction*, 32> NowDeadInsts; |
| 530 | |
| 531 | NowDeadInsts.push_back(I); |
| 532 | --NumFastOther; |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 534 | // Before we touch this instruction, remove it from memdep! |
| 535 | MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>(); |
| 536 | while (!NowDeadInsts.empty()) { |
| 537 | Instruction *DeadInst = NowDeadInsts.back(); |
| 538 | NowDeadInsts.pop_back(); |
Owen Anderson | bf971aa | 2007-07-11 19:03:09 +0000 | [diff] [blame] | 539 | |
Chris Lattner | 1adb675 | 2008-11-28 00:27:14 +0000 | [diff] [blame] | 540 | ++NumFastOther; |
| 541 | |
| 542 | // This instruction is dead, zap it, in stages. Start by removing it from |
| 543 | // MemDep, which needs to know the operands and needs it to be in the |
| 544 | // function. |
| 545 | MDA.removeInstruction(DeadInst); |
| 546 | |
| 547 | for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) { |
| 548 | Value *Op = DeadInst->getOperand(op); |
| 549 | DeadInst->setOperand(op, 0); |
| 550 | |
| 551 | // If this operand just became dead, add it to the NowDeadInsts list. |
| 552 | if (!Op->use_empty()) continue; |
| 553 | |
| 554 | if (Instruction *OpI = dyn_cast<Instruction>(Op)) |
| 555 | if (isInstructionTriviallyDead(OpI)) |
| 556 | NowDeadInsts.push_back(OpI); |
| 557 | } |
| 558 | |
| 559 | DeadInst->eraseFromParent(); |
| 560 | |
| 561 | if (ValueSet) ValueSet->erase(DeadInst); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 562 | } |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 563 | } |