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