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