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