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