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 | // |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 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. |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Scalar/DeadStoreElimination.h" |
Hal Finkel | a127103 | 2016-06-23 13:46:39 +0000 | [diff] [blame^] | 19 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/ADT/SetVector.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
Nick Lewycky | 32f8051 | 2011-10-22 21:59:35 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/GlobalsModRef.h" |
Victor Hernandez | f390e04 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/MemoryBuiltins.h" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chris Lattner | c0f3379 | 2010-11-30 23:05:20 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Constants.h" |
| 31 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Function.h" |
| 34 | #include "llvm/IR/GlobalVariable.h" |
| 35 | #include "llvm/IR/Instructions.h" |
| 36 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/Pass.h" |
Hal Finkel | a127103 | 2016-06-23 13:46:39 +0000 | [diff] [blame^] | 38 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Scalar.h" |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Utils/Local.h" |
Hal Finkel | a127103 | 2016-06-23 13:46:39 +0000 | [diff] [blame^] | 43 | #include <map> |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 46 | #define DEBUG_TYPE "dse" |
| 47 | |
Erik Eckstein | 11fc817 | 2015-08-13 15:36:11 +0000 | [diff] [blame] | 48 | STATISTIC(NumRedundantStores, "Number of redundant stores deleted"); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 49 | STATISTIC(NumFastStores, "Number of stores deleted"); |
| 50 | STATISTIC(NumFastOther , "Number of other instrs removed"); |
Hal Finkel | a127103 | 2016-06-23 13:46:39 +0000 | [diff] [blame^] | 51 | STATISTIC(NumCompletePartials, "Number of stores dead by later partials"); |
| 52 | |
| 53 | static cl::opt<bool> |
| 54 | EnablePartialOverwriteTracking("enable-dse-partial-overwrite-tracking", |
| 55 | cl::init(true), cl::Hidden, |
| 56 | cl::desc("Enable partial-overwrite tracking in DSE")); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 57 | |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 6712251 | 2010-11-30 21:58:14 +0000 | [diff] [blame] | 59 | //===----------------------------------------------------------------------===// |
| 60 | // Helper functions |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
Chad Rosier | a8bc512 | 2016-06-10 17:58:01 +0000 | [diff] [blame] | 63 | /// Delete this instruction. Before we do, go through and zero out all the |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 64 | /// operands of this instruction. If any of them become dead, delete them and |
| 65 | /// the computation tree that feeds them. |
Eric Christopher | 0efe9f6 | 2015-08-19 02:15:13 +0000 | [diff] [blame] | 66 | /// If ValueSet is non-null, remove any deleted instructions from it as well. |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 67 | static void |
| 68 | deleteDeadInstruction(Instruction *I, MemoryDependenceResults &MD, |
| 69 | const TargetLibraryInfo &TLI, |
| 70 | SmallSetVector<Value *, 16> *ValueSet = nullptr) { |
Eric Christopher | 0efe9f6 | 2015-08-19 02:15:13 +0000 | [diff] [blame] | 71 | SmallVector<Instruction*, 32> NowDeadInsts; |
| 72 | |
| 73 | NowDeadInsts.push_back(I); |
| 74 | --NumFastOther; |
| 75 | |
| 76 | // Before we touch this instruction, remove it from memdep! |
| 77 | do { |
| 78 | Instruction *DeadInst = NowDeadInsts.pop_back_val(); |
| 79 | ++NumFastOther; |
| 80 | |
| 81 | // This instruction is dead, zap it, in stages. Start by removing it from |
| 82 | // MemDep, which needs to know the operands and needs it to be in the |
| 83 | // function. |
| 84 | MD.removeInstruction(DeadInst); |
| 85 | |
| 86 | for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) { |
| 87 | Value *Op = DeadInst->getOperand(op); |
| 88 | DeadInst->setOperand(op, nullptr); |
| 89 | |
| 90 | // If this operand just became dead, add it to the NowDeadInsts list. |
| 91 | if (!Op->use_empty()) continue; |
| 92 | |
| 93 | if (Instruction *OpI = dyn_cast<Instruction>(Op)) |
| 94 | if (isInstructionTriviallyDead(OpI, &TLI)) |
| 95 | NowDeadInsts.push_back(OpI); |
| 96 | } |
| 97 | |
| 98 | DeadInst->eraseFromParent(); |
| 99 | |
| 100 | if (ValueSet) ValueSet->remove(DeadInst); |
| 101 | } while (!NowDeadInsts.empty()); |
| 102 | } |
| 103 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 104 | /// Does this instruction write some memory? This only returns true for things |
| 105 | /// that we can analyze with other helpers below. |
Chandler Carruth | dbe40fb | 2015-08-12 18:01:44 +0000 | [diff] [blame] | 106 | static bool hasMemoryWrite(Instruction *I, const TargetLibraryInfo &TLI) { |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame] | 107 | if (isa<StoreInst>(I)) |
| 108 | return true; |
| 109 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 110 | switch (II->getIntrinsicID()) { |
Chris Lattner | 2764b4d | 2009-12-02 06:35:55 +0000 | [diff] [blame] | 111 | default: |
| 112 | return false; |
| 113 | case Intrinsic::memset: |
| 114 | case Intrinsic::memmove: |
| 115 | case Intrinsic::memcpy: |
| 116 | case Intrinsic::init_trampoline: |
| 117 | case Intrinsic::lifetime_end: |
| 118 | return true; |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 121 | if (auto CS = CallSite(I)) { |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 122 | if (Function *F = CS.getCalledFunction()) { |
Chad Rosier | 624fee5 | 2016-06-16 17:06:04 +0000 | [diff] [blame] | 123 | StringRef FnName = F->getName(); |
| 124 | if (TLI.has(LibFunc::strcpy) && FnName == TLI.getName(LibFunc::strcpy)) |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 125 | return true; |
Chad Rosier | 624fee5 | 2016-06-16 17:06:04 +0000 | [diff] [blame] | 126 | if (TLI.has(LibFunc::strncpy) && FnName == TLI.getName(LibFunc::strncpy)) |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 127 | return true; |
Chad Rosier | 624fee5 | 2016-06-16 17:06:04 +0000 | [diff] [blame] | 128 | if (TLI.has(LibFunc::strcat) && FnName == TLI.getName(LibFunc::strcat)) |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 129 | return true; |
Chad Rosier | 624fee5 | 2016-06-16 17:06:04 +0000 | [diff] [blame] | 130 | if (TLI.has(LibFunc::strncat) && FnName == TLI.getName(LibFunc::strncat)) |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 131 | return true; |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 137 | /// Return a Location stored to by the specified instruction. If isRemovable |
| 138 | /// returns true, this function and getLocForRead completely describe the memory |
| 139 | /// operations for this instruction. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 140 | static MemoryLocation getLocForWrite(Instruction *Inst, AliasAnalysis &AA) { |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 141 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 142 | return MemoryLocation::get(SI); |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 144 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(Inst)) { |
| 145 | // memcpy/memmove/memset. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 146 | MemoryLocation Loc = MemoryLocation::getForDest(MI); |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 147 | return Loc; |
| 148 | } |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 150 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst); |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 151 | if (!II) |
| 152 | return MemoryLocation(); |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 154 | switch (II->getIntrinsicID()) { |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 155 | default: |
| 156 | return MemoryLocation(); // Unhandled intrinsic. |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 157 | case Intrinsic::init_trampoline: |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 158 | // FIXME: We don't know the size of the trampoline, so we can't really |
| 159 | // handle it here. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 160 | return MemoryLocation(II->getArgOperand(0)); |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 161 | case Intrinsic::lifetime_end: { |
| 162 | uint64_t Len = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(); |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 163 | return MemoryLocation(II->getArgOperand(1), Len); |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 168 | /// Return the location read by the specified "hasMemoryWrite" instruction if |
| 169 | /// any. |
Chandler Carruth | dbe40fb | 2015-08-12 18:01:44 +0000 | [diff] [blame] | 170 | static MemoryLocation getLocForRead(Instruction *Inst, |
| 171 | const TargetLibraryInfo &TLI) { |
| 172 | assert(hasMemoryWrite(Inst, TLI) && "Unknown instruction case"); |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 94fbdf3 | 2010-12-06 01:48:06 +0000 | [diff] [blame] | 174 | // The only instructions that both read and write are the mem transfer |
| 175 | // instructions (memcpy/memmove). |
| 176 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(Inst)) |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 177 | return MemoryLocation::getForSource(MTI); |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 178 | return MemoryLocation(); |
Chris Lattner | 94fbdf3 | 2010-12-06 01:48:06 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 181 | /// If the value of this instruction and the memory it writes to is unused, may |
| 182 | /// we delete this instruction? |
Chris Lattner | 3590ef8 | 2010-11-30 05:30:45 +0000 | [diff] [blame] | 183 | static bool isRemovable(Instruction *I) { |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 184 | // Don't remove volatile/atomic stores. |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame] | 185 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 186 | return SI->isUnordered(); |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 187 | |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 188 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 189 | switch (II->getIntrinsicID()) { |
| 190 | default: llvm_unreachable("doesn't pass 'hasMemoryWrite' predicate"); |
| 191 | case Intrinsic::lifetime_end: |
| 192 | // Never remove dead lifetime_end's, e.g. because it is followed by a |
| 193 | // free. |
| 194 | return false; |
| 195 | case Intrinsic::init_trampoline: |
| 196 | // Always safe to remove init_trampoline. |
| 197 | return true; |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 198 | |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 199 | case Intrinsic::memset: |
| 200 | case Intrinsic::memmove: |
| 201 | case Intrinsic::memcpy: |
| 202 | // Don't remove volatile memory intrinsics. |
| 203 | return !cast<MemIntrinsic>(II)->isVolatile(); |
| 204 | } |
Chris Lattner | b63ba73 | 2010-11-30 19:12:10 +0000 | [diff] [blame] | 205 | } |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 206 | |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 207 | if (auto CS = CallSite(I)) |
Nick Lewycky | 42bca05 | 2012-09-25 01:55:59 +0000 | [diff] [blame] | 208 | return CS.getInstruction()->use_empty(); |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 209 | |
| 210 | return false; |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 213 | |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 214 | /// Returns true if the end of this instruction can be safely shortened in |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 215 | /// length. |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 216 | static bool isShortenableAtTheEnd(Instruction *I) { |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 217 | // Don't shorten stores for now |
| 218 | if (isa<StoreInst>(I)) |
| 219 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 220 | |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 221 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 222 | switch (II->getIntrinsicID()) { |
| 223 | default: return false; |
| 224 | case Intrinsic::memset: |
| 225 | case Intrinsic::memcpy: |
| 226 | // Do shorten memory intrinsics. |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 227 | // FIXME: Add memmove if it's also safe to transform. |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 228 | return true; |
| 229 | } |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 230 | } |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 231 | |
| 232 | // Don't shorten libcalls calls for now. |
| 233 | |
| 234 | return false; |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 237 | /// Returns true if the beginning of this instruction can be safely shortened |
| 238 | /// in length. |
| 239 | static bool isShortenableAtTheBeginning(Instruction *I) { |
| 240 | // FIXME: Handle only memset for now. Supporting memcpy/memmove should be |
| 241 | // easily done by offsetting the source address. |
| 242 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 243 | return II && II->getIntrinsicID() == Intrinsic::memset; |
| 244 | } |
| 245 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 246 | /// Return the pointer that is being written to. |
Chris Lattner | 6712251 | 2010-11-30 21:58:14 +0000 | [diff] [blame] | 247 | static Value *getStoredPointerOperand(Instruction *I) { |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame] | 248 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 249 | return SI->getPointerOperand(); |
| 250 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) |
Chris Lattner | 6712251 | 2010-11-30 21:58:14 +0000 | [diff] [blame] | 251 | return MI->getDest(); |
Gabor Greif | 91f9589 | 2010-06-24 12:03:56 +0000 | [diff] [blame] | 252 | |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 253 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 254 | switch (II->getIntrinsicID()) { |
| 255 | default: llvm_unreachable("Unexpected intrinsic!"); |
| 256 | case Intrinsic::init_trampoline: |
| 257 | return II->getArgOperand(0); |
| 258 | } |
Duncan Sands | 1925d3a | 2009-11-10 13:49:50 +0000 | [diff] [blame] | 259 | } |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 260 | |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 261 | CallSite CS(I); |
Nick Lewycky | 9f4729d | 2012-09-24 22:09:10 +0000 | [diff] [blame] | 262 | // All the supported functions so far happen to have dest as their first |
| 263 | // argument. |
| 264 | return CS.getArgument(0); |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 267 | static uint64_t getPointerSize(const Value *V, const DataLayout &DL, |
Chandler Carruth | dbe40fb | 2015-08-12 18:01:44 +0000 | [diff] [blame] | 268 | const TargetLibraryInfo &TLI) { |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 269 | uint64_t Size; |
Chandler Carruth | dbe40fb | 2015-08-12 18:01:44 +0000 | [diff] [blame] | 270 | if (getObjectSize(V, Size, DL, &TLI)) |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 271 | return Size; |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 272 | return MemoryLocation::UnknownSize; |
Chris Lattner | 903add8 | 2010-11-30 23:43:23 +0000 | [diff] [blame] | 273 | } |
Chris Lattner | 51c28a9 | 2010-11-30 19:34:42 +0000 | [diff] [blame] | 274 | |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 275 | namespace { |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 276 | enum OverwriteResult { |
| 277 | OverwriteBegin, |
| 278 | OverwriteComplete, |
| 279 | OverwriteEnd, |
| 280 | OverwriteUnknown |
| 281 | }; |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Hal Finkel | a127103 | 2016-06-23 13:46:39 +0000 | [diff] [blame^] | 284 | typedef DenseMap<Instruction *, |
| 285 | std::map<int64_t, int64_t>> InstOverlapIntervalsTy; |
| 286 | |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 287 | /// Return 'OverwriteComplete' if a store to the 'Later' location completely |
| 288 | /// overwrites a store to the 'Earlier' location, 'OverwriteEnd' if the end of |
| 289 | /// the 'Earlier' location is completely overwritten by 'Later', |
| 290 | /// 'OverwriteBegin' if the beginning of the 'Earlier' location is overwritten |
| 291 | /// by 'Later', or 'OverwriteUnknown' if nothing can be determined. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 292 | static OverwriteResult isOverwrite(const MemoryLocation &Later, |
| 293 | const MemoryLocation &Earlier, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 294 | const DataLayout &DL, |
Chandler Carruth | dbe40fb | 2015-08-12 18:01:44 +0000 | [diff] [blame] | 295 | const TargetLibraryInfo &TLI, |
Hal Finkel | a127103 | 2016-06-23 13:46:39 +0000 | [diff] [blame^] | 296 | int64_t &EarlierOff, int64_t &LaterOff, |
| 297 | Instruction *DepWrite, |
| 298 | InstOverlapIntervalsTy &IOL) { |
Chad Rosier | 72a793c | 2016-06-15 22:17:38 +0000 | [diff] [blame] | 299 | // If we don't know the sizes of either access, then we can't do a comparison. |
| 300 | if (Later.Size == MemoryLocation::UnknownSize || |
| 301 | Earlier.Size == MemoryLocation::UnknownSize) |
| 302 | return OverwriteUnknown; |
| 303 | |
Chris Lattner | c0f3379 | 2010-11-30 23:05:20 +0000 | [diff] [blame] | 304 | const Value *P1 = Earlier.Ptr->stripPointerCasts(); |
| 305 | const Value *P2 = Later.Ptr->stripPointerCasts(); |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 306 | |
Chris Lattner | c0f3379 | 2010-11-30 23:05:20 +0000 | [diff] [blame] | 307 | // If the start pointers are the same, we just have to compare sizes to see if |
| 308 | // the later store was larger than the earlier store. |
| 309 | if (P1 == P2) { |
Chris Lattner | c0f3379 | 2010-11-30 23:05:20 +0000 | [diff] [blame] | 310 | // Make sure that the Later size is >= the Earlier size. |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 311 | if (Later.Size >= Earlier.Size) |
| 312 | return OverwriteComplete; |
Chris Lattner | 77d79fa | 2010-11-30 19:28:23 +0000 | [diff] [blame] | 313 | } |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 903add8 | 2010-11-30 23:43:23 +0000 | [diff] [blame] | 315 | // Check to see if the later store is to the entire object (either a global, |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 316 | // an alloca, or a byval/inalloca argument). If so, then it clearly |
| 317 | // overwrites any other store to the same object. |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 318 | const Value *UO1 = GetUnderlyingObject(P1, DL), |
| 319 | *UO2 = GetUnderlyingObject(P2, DL); |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 903add8 | 2010-11-30 23:43:23 +0000 | [diff] [blame] | 321 | // If we can't resolve the same pointers to the same object, then we can't |
| 322 | // analyze them at all. |
| 323 | if (UO1 != UO2) |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 324 | return OverwriteUnknown; |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 903add8 | 2010-11-30 23:43:23 +0000 | [diff] [blame] | 326 | // If the "Later" store is to a recognizable object, get its size. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 327 | uint64_t ObjectSize = getPointerSize(UO2, DL, TLI); |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 328 | if (ObjectSize != MemoryLocation::UnknownSize) |
Pete Cooper | a4237c3 | 2011-11-10 20:22:08 +0000 | [diff] [blame] | 329 | if (ObjectSize == Later.Size && ObjectSize >= Earlier.Size) |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 330 | return OverwriteComplete; |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 331 | |
Chris Lattner | c0f3379 | 2010-11-30 23:05:20 +0000 | [diff] [blame] | 332 | // Okay, we have stores to two completely different pointers. Try to |
| 333 | // decompose the pointer into a "base + constant_offset" form. If the base |
| 334 | // pointers are equal, then we can reason about the two stores. |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 335 | EarlierOff = 0; |
| 336 | LaterOff = 0; |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 337 | const Value *BP1 = GetPointerBaseWithConstantOffset(P1, EarlierOff, DL); |
| 338 | const Value *BP2 = GetPointerBaseWithConstantOffset(P2, LaterOff, DL); |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 339 | |
Chris Lattner | c0f3379 | 2010-11-30 23:05:20 +0000 | [diff] [blame] | 340 | // If the base pointers still differ, we have two completely different stores. |
| 341 | if (BP1 != BP2) |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 342 | return OverwriteUnknown; |
Bill Wendling | db40b5c | 2011-03-26 01:20:37 +0000 | [diff] [blame] | 343 | |
Bill Wendling | 19f33b9 | 2011-03-26 08:02:59 +0000 | [diff] [blame] | 344 | // The later store completely overlaps the earlier store if: |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 345 | // |
Bill Wendling | 19f33b9 | 2011-03-26 08:02:59 +0000 | [diff] [blame] | 346 | // 1. Both start at the same offset and the later one's size is greater than |
| 347 | // or equal to the earlier one's, or |
| 348 | // |
| 349 | // |--earlier--| |
| 350 | // |-- later --| |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 351 | // |
Bill Wendling | 19f33b9 | 2011-03-26 08:02:59 +0000 | [diff] [blame] | 352 | // 2. The earlier store has an offset greater than the later offset, but which |
| 353 | // still lies completely within the later store. |
| 354 | // |
| 355 | // |--earlier--| |
| 356 | // |----- later ------| |
Bill Wendling | 5034159 | 2011-03-30 21:37:19 +0000 | [diff] [blame] | 357 | // |
| 358 | // We have to be careful here as *Off is signed while *.Size is unsigned. |
Bill Wendling | b513992 | 2011-03-26 09:32:07 +0000 | [diff] [blame] | 359 | if (EarlierOff >= LaterOff && |
Craig Topper | 2a40418 | 2012-08-14 07:32:05 +0000 | [diff] [blame] | 360 | Later.Size >= Earlier.Size && |
Bill Wendling | 5034159 | 2011-03-30 21:37:19 +0000 | [diff] [blame] | 361 | uint64_t(EarlierOff - LaterOff) + Earlier.Size <= Later.Size) |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 362 | return OverwriteComplete; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 363 | |
Hal Finkel | a127103 | 2016-06-23 13:46:39 +0000 | [diff] [blame^] | 364 | // We may now overlap, although the overlap is not complete. There might also |
| 365 | // be other incomplete overlaps, and together, they might cover the complete |
| 366 | // earlier write. |
| 367 | // Note: The correctness of this logic depends on the fact that this function |
| 368 | // is not even called providing DepWrite when there are any intervening reads. |
| 369 | if (EnablePartialOverwriteTracking && |
| 370 | LaterOff < int64_t(EarlierOff + Earlier.Size) && |
| 371 | int64_t(LaterOff + Later.Size) >= EarlierOff) { |
| 372 | |
| 373 | // Insert our part of the overlap into the map. |
| 374 | auto &IM = IOL[DepWrite]; |
| 375 | DEBUG(dbgs() << "DSE: Partial overwrite: Earlier [" << EarlierOff << ", " << |
| 376 | int64_t(EarlierOff + Earlier.Size) << ") Later [" << |
| 377 | LaterOff << ", " << int64_t(LaterOff + Later.Size) << ")\n"); |
| 378 | |
| 379 | // Make sure that we only insert non-overlapping intervals and combine |
| 380 | // adjacent intervals. The intervals are stored in the map with the ending |
| 381 | // offset as the key (in the half-open sense) and the starting offset as |
| 382 | // the value. |
| 383 | int64_t LaterIntStart = LaterOff, LaterIntEnd = LaterOff + Later.Size; |
| 384 | |
| 385 | // Find any intervals ending at, or after, LaterIntStart which start |
| 386 | // before LaterIntEnd. |
| 387 | auto ILI = IM.lower_bound(LaterIntStart); |
| 388 | if (ILI != IM.end() && ILI->second < LaterIntEnd) { |
| 389 | // This existing interval ends in the middle of |
| 390 | // [LaterIntStart, LaterIntEnd), erase it adjusting our start. |
| 391 | LaterIntStart = std::min(LaterIntStart, ILI->second); |
| 392 | LaterIntEnd = std::max(LaterIntEnd, ILI->first); |
| 393 | ILI = IM.erase(ILI); |
| 394 | |
| 395 | while (ILI != IM.end() && ILI->first <= LaterIntEnd) |
| 396 | ILI = IM.erase(ILI); |
| 397 | |
| 398 | if (ILI != IM.end() && ILI->second < LaterIntEnd) |
| 399 | LaterIntEnd = std::max(LaterIntEnd, ILI->first); |
| 400 | } |
| 401 | |
| 402 | IM[LaterIntEnd] = LaterIntStart; |
| 403 | |
| 404 | ILI = IM.begin(); |
| 405 | if (ILI->second <= EarlierOff && |
| 406 | ILI->first >= int64_t(EarlierOff + Earlier.Size)) { |
| 407 | DEBUG(dbgs() << "DSE: Full overwrite from partials: Earlier [" << |
| 408 | EarlierOff << ", " << |
| 409 | int64_t(EarlierOff + Earlier.Size) << |
| 410 | ") Composite Later [" << |
| 411 | ILI->second << ", " << ILI->first << ")\n"); |
| 412 | ++NumCompletePartials; |
| 413 | return OverwriteComplete; |
| 414 | } |
| 415 | } |
| 416 | |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 417 | // Another interesting case is if the later store overwrites the end of the |
| 418 | // earlier store. |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 419 | // |
| 420 | // |--earlier--| |
| 421 | // |-- later --| |
| 422 | // |
| 423 | // In this case we may want to trim the size of earlier to avoid generating |
| 424 | // writes to addresses which will definitely be overwritten later |
| 425 | if (LaterOff > EarlierOff && |
| 426 | LaterOff < int64_t(EarlierOff + Earlier.Size) && |
Pete Cooper | e03fe83 | 2011-12-03 00:04:30 +0000 | [diff] [blame] | 427 | int64_t(LaterOff + Later.Size) >= int64_t(EarlierOff + Earlier.Size)) |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 428 | return OverwriteEnd; |
Bill Wendling | 19f33b9 | 2011-03-26 08:02:59 +0000 | [diff] [blame] | 429 | |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 430 | // Finally, we also need to check if the later store overwrites the beginning |
| 431 | // of the earlier store. |
| 432 | // |
| 433 | // |--earlier--| |
| 434 | // |-- later --| |
| 435 | // |
| 436 | // In this case we may want to move the destination address and trim the size |
| 437 | // of earlier to avoid generating writes to addresses which will definitely |
| 438 | // be overwritten later. |
| 439 | if (LaterOff <= EarlierOff && int64_t(LaterOff + Later.Size) > EarlierOff) { |
| 440 | assert (int64_t(LaterOff + Later.Size) < int64_t(EarlierOff + Earlier.Size) |
| 441 | && "Expect to be handled as OverwriteComplete" ); |
| 442 | return OverwriteBegin; |
| 443 | } |
Bill Wendling | 19f33b9 | 2011-03-26 08:02:59 +0000 | [diff] [blame] | 444 | // Otherwise, they don't completely overlap. |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 445 | return OverwriteUnknown; |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 448 | /// If 'Inst' might be a self read (i.e. a noop copy of a |
Chris Lattner | 94fbdf3 | 2010-12-06 01:48:06 +0000 | [diff] [blame] | 449 | /// memory region into an identical pointer) then it doesn't actually make its |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 450 | /// input dead in the traditional sense. Consider this case: |
Chris Lattner | 94fbdf3 | 2010-12-06 01:48:06 +0000 | [diff] [blame] | 451 | /// |
| 452 | /// memcpy(A <- B) |
| 453 | /// memcpy(A <- A) |
| 454 | /// |
| 455 | /// In this case, the second store to A does not make the first store to A dead. |
| 456 | /// The usual situation isn't an explicit A<-A store like this (which can be |
| 457 | /// trivially removed) but a case where two pointers may alias. |
| 458 | /// |
| 459 | /// This function detects when it is unsafe to remove a dependent instruction |
| 460 | /// because the DSE inducing instruction may be a self-read. |
| 461 | static bool isPossibleSelfRead(Instruction *Inst, |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 462 | const MemoryLocation &InstStoreLoc, |
Chandler Carruth | dbe40fb | 2015-08-12 18:01:44 +0000 | [diff] [blame] | 463 | Instruction *DepWrite, |
| 464 | const TargetLibraryInfo &TLI, |
| 465 | AliasAnalysis &AA) { |
Chris Lattner | 94fbdf3 | 2010-12-06 01:48:06 +0000 | [diff] [blame] | 466 | // Self reads can only happen for instructions that read memory. Get the |
| 467 | // location read. |
Chandler Carruth | dbe40fb | 2015-08-12 18:01:44 +0000 | [diff] [blame] | 468 | MemoryLocation InstReadLoc = getLocForRead(Inst, TLI); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 469 | if (!InstReadLoc.Ptr) return false; // Not a reading instruction. |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 94fbdf3 | 2010-12-06 01:48:06 +0000 | [diff] [blame] | 471 | // If the read and written loc obviously don't alias, it isn't a read. |
| 472 | if (AA.isNoAlias(InstReadLoc, InstStoreLoc)) return false; |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 94fbdf3 | 2010-12-06 01:48:06 +0000 | [diff] [blame] | 474 | // Okay, 'Inst' may copy over itself. However, we can still remove a the |
| 475 | // DepWrite instruction if we can prove that it reads from the same location |
| 476 | // as Inst. This handles useful cases like: |
| 477 | // memcpy(A <- B) |
| 478 | // memcpy(A <- B) |
| 479 | // Here we don't know if A/B may alias, but we do know that B/B are must |
| 480 | // aliases, so removing the first memcpy is safe (assuming it writes <= # |
| 481 | // bytes as the second one. |
Chandler Carruth | dbe40fb | 2015-08-12 18:01:44 +0000 | [diff] [blame] | 482 | MemoryLocation DepReadLoc = getLocForRead(DepWrite, TLI); |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 94fbdf3 | 2010-12-06 01:48:06 +0000 | [diff] [blame] | 484 | if (DepReadLoc.Ptr && AA.isMustAlias(InstReadLoc.Ptr, DepReadLoc.Ptr)) |
| 485 | return false; |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 486 | |
Chris Lattner | 94fbdf3 | 2010-12-06 01:48:06 +0000 | [diff] [blame] | 487 | // If DepWrite doesn't read memory or if we can't prove it is a must alias, |
| 488 | // then it can't be considered dead. |
| 489 | return true; |
| 490 | } |
| 491 | |
Chris Lattner | 6712251 | 2010-11-30 21:58:14 +0000 | [diff] [blame] | 492 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 493 | /// Returns true if the memory which is accessed by the second instruction is not |
| 494 | /// modified between the first and the second instruction. |
| 495 | /// Precondition: Second instruction must be dominated by the first |
| 496 | /// instruction. |
| 497 | static bool memoryIsNotModifiedBetween(Instruction *FirstI, |
| 498 | Instruction *SecondI, |
| 499 | AliasAnalysis *AA) { |
| 500 | SmallVector<BasicBlock *, 16> WorkList; |
| 501 | SmallPtrSet<BasicBlock *, 8> Visited; |
| 502 | BasicBlock::iterator FirstBBI(FirstI); |
| 503 | ++FirstBBI; |
| 504 | BasicBlock::iterator SecondBBI(SecondI); |
| 505 | BasicBlock *FirstBB = FirstI->getParent(); |
| 506 | BasicBlock *SecondBB = SecondI->getParent(); |
| 507 | MemoryLocation MemLoc = MemoryLocation::get(SecondI); |
Chris Lattner | 6712251 | 2010-11-30 21:58:14 +0000 | [diff] [blame] | 508 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 509 | // Start checking the store-block. |
| 510 | WorkList.push_back(SecondBB); |
| 511 | bool isFirstBlock = true; |
| 512 | |
| 513 | // Check all blocks going backward until we reach the load-block. |
| 514 | while (!WorkList.empty()) { |
| 515 | BasicBlock *B = WorkList.pop_back_val(); |
| 516 | |
| 517 | // Ignore instructions before LI if this is the FirstBB. |
| 518 | BasicBlock::iterator BI = (B == FirstBB ? FirstBBI : B->begin()); |
| 519 | |
| 520 | BasicBlock::iterator EI; |
| 521 | if (isFirstBlock) { |
| 522 | // Ignore instructions after SI if this is the first visit of SecondBB. |
| 523 | assert(B == SecondBB && "first block is not the store block"); |
| 524 | EI = SecondBBI; |
| 525 | isFirstBlock = false; |
| 526 | } else { |
| 527 | // It's not SecondBB or (in case of a loop) the second visit of SecondBB. |
| 528 | // In this case we also have to look at instructions after SI. |
| 529 | EI = B->end(); |
| 530 | } |
| 531 | for (; BI != EI; ++BI) { |
| 532 | Instruction *I = &*BI; |
| 533 | if (I->mayWriteToMemory() && I != SecondI) { |
| 534 | auto Res = AA->getModRefInfo(I, MemLoc); |
| 535 | if (Res != MRI_NoModRef) |
| 536 | return false; |
| 537 | } |
| 538 | } |
| 539 | if (B != FirstBB) { |
| 540 | assert(B != &FirstBB->getParent()->getEntryBlock() && |
| 541 | "Should not hit the entry block because SI must be dominated by LI"); |
| 542 | for (auto PredI = pred_begin(B), PE = pred_end(B); PredI != PE; ++PredI) { |
| 543 | if (!Visited.insert(*PredI).second) |
| 544 | continue; |
| 545 | WorkList.push_back(*PredI); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | return true; |
| 550 | } |
| 551 | |
| 552 | /// Find all blocks that will unconditionally lead to the block BB and append |
| 553 | /// them to F. |
| 554 | static void findUnconditionalPreds(SmallVectorImpl<BasicBlock *> &Blocks, |
| 555 | BasicBlock *BB, DominatorTree *DT) { |
| 556 | for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) { |
| 557 | BasicBlock *Pred = *I; |
| 558 | if (Pred == BB) continue; |
| 559 | TerminatorInst *PredTI = Pred->getTerminator(); |
| 560 | if (PredTI->getNumSuccessors() != 1) |
| 561 | continue; |
| 562 | |
| 563 | if (DT->isReachableFromEntry(Pred)) |
| 564 | Blocks.push_back(Pred); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /// Handle frees of entire structures whose dependency is a store |
| 569 | /// to a field of that structure. |
| 570 | static bool handleFree(CallInst *F, AliasAnalysis *AA, |
| 571 | MemoryDependenceResults *MD, DominatorTree *DT, |
| 572 | const TargetLibraryInfo *TLI) { |
| 573 | bool MadeChange = false; |
| 574 | |
| 575 | MemoryLocation Loc = MemoryLocation(F->getOperand(0)); |
| 576 | SmallVector<BasicBlock *, 16> Blocks; |
| 577 | Blocks.push_back(F->getParent()); |
| 578 | const DataLayout &DL = F->getModule()->getDataLayout(); |
| 579 | |
| 580 | while (!Blocks.empty()) { |
| 581 | BasicBlock *BB = Blocks.pop_back_val(); |
| 582 | Instruction *InstPt = BB->getTerminator(); |
| 583 | if (BB == F->getParent()) InstPt = F; |
| 584 | |
| 585 | MemDepResult Dep = |
| 586 | MD->getPointerDependencyFrom(Loc, false, InstPt->getIterator(), BB); |
| 587 | while (Dep.isDef() || Dep.isClobber()) { |
| 588 | Instruction *Dependency = Dep.getInst(); |
| 589 | if (!hasMemoryWrite(Dependency, *TLI) || !isRemovable(Dependency)) |
| 590 | break; |
| 591 | |
| 592 | Value *DepPointer = |
| 593 | GetUnderlyingObject(getStoredPointerOperand(Dependency), DL); |
| 594 | |
| 595 | // Check for aliasing. |
| 596 | if (!AA->isMustAlias(F->getArgOperand(0), DepPointer)) |
| 597 | break; |
| 598 | |
| 599 | auto Next = ++Dependency->getIterator(); |
| 600 | |
Chad Rosier | 840b3ef | 2016-06-10 17:59:22 +0000 | [diff] [blame] | 601 | // DCE instructions only used to calculate that store. |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 602 | deleteDeadInstruction(Dependency, *MD, *TLI); |
| 603 | ++NumFastStores; |
| 604 | MadeChange = true; |
| 605 | |
| 606 | // Inst's old Dependency is now deleted. Compute the next dependency, |
| 607 | // which may also be dead, as in |
| 608 | // s[0] = 0; |
| 609 | // s[1] = 0; // This has just been deleted. |
| 610 | // free(s); |
| 611 | Dep = MD->getPointerDependencyFrom(Loc, false, Next, BB); |
| 612 | } |
| 613 | |
| 614 | if (Dep.isNonLocal()) |
| 615 | findUnconditionalPreds(Blocks, BB, DT); |
| 616 | } |
| 617 | |
| 618 | return MadeChange; |
| 619 | } |
| 620 | |
| 621 | /// Check to see if the specified location may alias any of the stack objects in |
| 622 | /// the DeadStackObjects set. If so, they become live because the location is |
| 623 | /// being loaded. |
| 624 | static void removeAccessedObjects(const MemoryLocation &LoadedLoc, |
| 625 | SmallSetVector<Value *, 16> &DeadStackObjects, |
| 626 | const DataLayout &DL, AliasAnalysis *AA, |
| 627 | const TargetLibraryInfo *TLI) { |
| 628 | const Value *UnderlyingPointer = GetUnderlyingObject(LoadedLoc.Ptr, DL); |
| 629 | |
| 630 | // A constant can't be in the dead pointer set. |
| 631 | if (isa<Constant>(UnderlyingPointer)) |
| 632 | return; |
| 633 | |
| 634 | // If the kill pointer can be easily reduced to an alloca, don't bother doing |
| 635 | // extraneous AA queries. |
| 636 | if (isa<AllocaInst>(UnderlyingPointer) || isa<Argument>(UnderlyingPointer)) { |
| 637 | DeadStackObjects.remove(const_cast<Value*>(UnderlyingPointer)); |
| 638 | return; |
| 639 | } |
| 640 | |
| 641 | // Remove objects that could alias LoadedLoc. |
| 642 | DeadStackObjects.remove_if([&](Value *I) { |
| 643 | // See if the loaded location could alias the stack location. |
| 644 | MemoryLocation StackLoc(I, getPointerSize(I, DL, *TLI)); |
| 645 | return !AA->isNoAlias(StackLoc, LoadedLoc); |
| 646 | }); |
| 647 | } |
| 648 | |
| 649 | /// Remove dead stores to stack-allocated locations in the function end block. |
| 650 | /// Ex: |
| 651 | /// %A = alloca i32 |
| 652 | /// ... |
| 653 | /// store i32 1, i32* %A |
| 654 | /// ret void |
| 655 | static bool handleEndBlock(BasicBlock &BB, AliasAnalysis *AA, |
| 656 | MemoryDependenceResults *MD, |
| 657 | const TargetLibraryInfo *TLI) { |
| 658 | bool MadeChange = false; |
| 659 | |
| 660 | // Keep track of all of the stack objects that are dead at the end of the |
| 661 | // function. |
| 662 | SmallSetVector<Value*, 16> DeadStackObjects; |
| 663 | |
| 664 | // Find all of the alloca'd pointers in the entry block. |
| 665 | BasicBlock &Entry = BB.getParent()->front(); |
| 666 | for (Instruction &I : Entry) { |
| 667 | if (isa<AllocaInst>(&I)) |
| 668 | DeadStackObjects.insert(&I); |
| 669 | |
| 670 | // Okay, so these are dead heap objects, but if the pointer never escapes |
| 671 | // then it's leaked by this function anyways. |
| 672 | else if (isAllocLikeFn(&I, TLI) && !PointerMayBeCaptured(&I, true, true)) |
| 673 | DeadStackObjects.insert(&I); |
| 674 | } |
| 675 | |
| 676 | // Treat byval or inalloca arguments the same, stores to them are dead at the |
| 677 | // end of the function. |
| 678 | for (Argument &AI : BB.getParent()->args()) |
| 679 | if (AI.hasByValOrInAllocaAttr()) |
| 680 | DeadStackObjects.insert(&AI); |
| 681 | |
| 682 | const DataLayout &DL = BB.getModule()->getDataLayout(); |
| 683 | |
| 684 | // Scan the basic block backwards |
| 685 | for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){ |
| 686 | --BBI; |
| 687 | |
| 688 | // If we find a store, check to see if it points into a dead stack value. |
| 689 | if (hasMemoryWrite(&*BBI, *TLI) && isRemovable(&*BBI)) { |
| 690 | // See through pointer-to-pointer bitcasts |
| 691 | SmallVector<Value *, 4> Pointers; |
| 692 | GetUnderlyingObjects(getStoredPointerOperand(&*BBI), Pointers, DL); |
| 693 | |
| 694 | // Stores to stack values are valid candidates for removal. |
| 695 | bool AllDead = true; |
| 696 | for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(), |
| 697 | E = Pointers.end(); I != E; ++I) |
| 698 | if (!DeadStackObjects.count(*I)) { |
| 699 | AllDead = false; |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | if (AllDead) { |
| 704 | Instruction *Dead = &*BBI++; |
| 705 | |
| 706 | DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n DEAD: " |
| 707 | << *Dead << "\n Objects: "; |
| 708 | for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(), |
| 709 | E = Pointers.end(); I != E; ++I) { |
| 710 | dbgs() << **I; |
| 711 | if (std::next(I) != E) |
| 712 | dbgs() << ", "; |
| 713 | } |
| 714 | dbgs() << '\n'); |
| 715 | |
| 716 | // DCE instructions only used to calculate that store. |
| 717 | deleteDeadInstruction(Dead, *MD, *TLI, &DeadStackObjects); |
| 718 | ++NumFastStores; |
| 719 | MadeChange = true; |
| 720 | continue; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | // Remove any dead non-memory-mutating instructions. |
| 725 | if (isInstructionTriviallyDead(&*BBI, TLI)) { |
| 726 | Instruction *Inst = &*BBI++; |
| 727 | deleteDeadInstruction(Inst, *MD, *TLI, &DeadStackObjects); |
| 728 | ++NumFastOther; |
| 729 | MadeChange = true; |
| 730 | continue; |
| 731 | } |
| 732 | |
| 733 | if (isa<AllocaInst>(BBI)) { |
| 734 | // Remove allocas from the list of dead stack objects; there can't be |
| 735 | // any references before the definition. |
| 736 | DeadStackObjects.remove(&*BBI); |
| 737 | continue; |
| 738 | } |
| 739 | |
| 740 | if (auto CS = CallSite(&*BBI)) { |
| 741 | // Remove allocation function calls from the list of dead stack objects; |
| 742 | // there can't be any references before the definition. |
| 743 | if (isAllocLikeFn(&*BBI, TLI)) |
| 744 | DeadStackObjects.remove(&*BBI); |
| 745 | |
| 746 | // If this call does not access memory, it can't be loading any of our |
| 747 | // pointers. |
| 748 | if (AA->doesNotAccessMemory(CS)) |
| 749 | continue; |
| 750 | |
| 751 | // If the call might load from any of our allocas, then any store above |
| 752 | // the call is live. |
| 753 | DeadStackObjects.remove_if([&](Value *I) { |
| 754 | // See if the call site touches the value. |
| 755 | ModRefInfo A = AA->getModRefInfo(CS, I, getPointerSize(I, DL, *TLI)); |
| 756 | |
| 757 | return A == MRI_ModRef || A == MRI_Ref; |
| 758 | }); |
| 759 | |
| 760 | // If all of the allocas were clobbered by the call then we're not going |
| 761 | // to find anything else to process. |
| 762 | if (DeadStackObjects.empty()) |
| 763 | break; |
| 764 | |
| 765 | continue; |
| 766 | } |
| 767 | |
| 768 | MemoryLocation LoadedLoc; |
| 769 | |
| 770 | // If we encounter a use of the pointer, it is no longer considered dead |
| 771 | if (LoadInst *L = dyn_cast<LoadInst>(BBI)) { |
| 772 | if (!L->isUnordered()) // Be conservative with atomic/volatile load |
| 773 | break; |
| 774 | LoadedLoc = MemoryLocation::get(L); |
| 775 | } else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) { |
| 776 | LoadedLoc = MemoryLocation::get(V); |
| 777 | } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(BBI)) { |
| 778 | LoadedLoc = MemoryLocation::getForSource(MTI); |
| 779 | } else if (!BBI->mayReadFromMemory()) { |
| 780 | // Instruction doesn't read memory. Note that stores that weren't removed |
| 781 | // above will hit this case. |
| 782 | continue; |
| 783 | } else { |
| 784 | // Unknown inst; assume it clobbers everything. |
| 785 | break; |
| 786 | } |
| 787 | |
| 788 | // Remove any allocas from the DeadPointer set that are loaded, as this |
| 789 | // makes any stores above the access live. |
| 790 | removeAccessedObjects(LoadedLoc, DeadStackObjects, DL, AA, TLI); |
| 791 | |
| 792 | // If all of the allocas were clobbered by the access then we're not going |
| 793 | // to find anything else to process. |
| 794 | if (DeadStackObjects.empty()) |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | return MadeChange; |
| 799 | } |
| 800 | |
| 801 | static bool eliminateDeadStores(BasicBlock &BB, AliasAnalysis *AA, |
| 802 | MemoryDependenceResults *MD, DominatorTree *DT, |
| 803 | const TargetLibraryInfo *TLI) { |
Igor Laevsky | 029bd93 | 2015-09-23 11:38:44 +0000 | [diff] [blame] | 804 | const DataLayout &DL = BB.getModule()->getDataLayout(); |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 805 | bool MadeChange = false; |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 806 | |
Hal Finkel | a127103 | 2016-06-23 13:46:39 +0000 | [diff] [blame^] | 807 | // A map of interval maps representing partially-overwritten value parts. |
| 808 | InstOverlapIntervalsTy IOL; |
| 809 | |
Chris Lattner | 4916267 | 2009-09-02 06:31:02 +0000 | [diff] [blame] | 810 | // Do a top-down walk on the BB. |
Chris Lattner | f2a8ba4 | 2008-11-28 21:29:52 +0000 | [diff] [blame] | 811 | for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) { |
Duncan P. N. Exon Smith | 3a9c9e3 | 2015-10-13 18:26:00 +0000 | [diff] [blame] | 812 | Instruction *Inst = &*BBI++; |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 9d179d9 | 2010-11-30 01:28:33 +0000 | [diff] [blame] | 814 | // Handle 'free' calls specially. |
Nick Lewycky | 135ac9a | 2012-09-24 22:07:09 +0000 | [diff] [blame] | 815 | if (CallInst *F = isFreeCall(Inst, TLI)) { |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 816 | MadeChange |= handleFree(F, AA, MD, DT, TLI); |
Chris Lattner | 9d179d9 | 2010-11-30 01:28:33 +0000 | [diff] [blame] | 817 | continue; |
| 818 | } |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 819 | |
Chris Lattner | 2227a8a | 2010-11-30 01:37:52 +0000 | [diff] [blame] | 820 | // If we find something that writes memory, get its memory dependence. |
Chandler Carruth | dbe40fb | 2015-08-12 18:01:44 +0000 | [diff] [blame] | 821 | if (!hasMemoryWrite(Inst, *TLI)) |
Owen Anderson | 0aecf0e | 2007-08-08 04:52:29 +0000 | [diff] [blame] | 822 | continue; |
Chris Lattner | d4f1090 | 2010-11-30 00:01:19 +0000 | [diff] [blame] | 823 | |
Rui Ueyama | c487f77 | 2014-08-06 19:30:38 +0000 | [diff] [blame] | 824 | // If we're storing the same value back to a pointer that we just |
| 825 | // loaded from, then the store can be removed. |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame] | 826 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Igor Laevsky | 029bd93 | 2015-09-23 11:38:44 +0000 | [diff] [blame] | 827 | |
| 828 | auto RemoveDeadInstAndUpdateBBI = [&](Instruction *DeadInst) { |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 829 | // deleteDeadInstruction can delete the current instruction. Save BBI |
Igor Laevsky | 029bd93 | 2015-09-23 11:38:44 +0000 | [diff] [blame] | 830 | // in case we need it. |
Duncan P. N. Exon Smith | 3a9c9e3 | 2015-10-13 18:26:00 +0000 | [diff] [blame] | 831 | WeakVH NextInst(&*BBI); |
Igor Laevsky | 029bd93 | 2015-09-23 11:38:44 +0000 | [diff] [blame] | 832 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 833 | deleteDeadInstruction(DeadInst, *MD, *TLI); |
Igor Laevsky | 029bd93 | 2015-09-23 11:38:44 +0000 | [diff] [blame] | 834 | |
| 835 | if (!NextInst) // Next instruction deleted. |
| 836 | BBI = BB.begin(); |
| 837 | else if (BBI != BB.begin()) // Revisit this instruction if possible. |
| 838 | --BBI; |
| 839 | ++NumRedundantStores; |
| 840 | MadeChange = true; |
| 841 | }; |
| 842 | |
Erik Eckstein | 11fc817 | 2015-08-13 15:36:11 +0000 | [diff] [blame] | 843 | if (LoadInst *DepLoad = dyn_cast<LoadInst>(SI->getValueOperand())) { |
Nick Lewycky | 9027147 | 2009-11-10 06:46:40 +0000 | [diff] [blame] | 844 | if (SI->getPointerOperand() == DepLoad->getPointerOperand() && |
Erik Eckstein | 11fc817 | 2015-08-13 15:36:11 +0000 | [diff] [blame] | 845 | isRemovable(SI) && |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 846 | memoryIsNotModifiedBetween(DepLoad, SI, AA)) { |
Erik Eckstein | 11fc817 | 2015-08-13 15:36:11 +0000 | [diff] [blame] | 847 | |
Chris Lattner | ca335e3 | 2010-12-06 21:13:51 +0000 | [diff] [blame] | 848 | DEBUG(dbgs() << "DSE: Remove Store Of Load from same pointer:\n " |
| 849 | << "LOAD: " << *DepLoad << "\n STORE: " << *SI << '\n'); |
Philip Reames | 00c9b64 | 2014-08-05 17:48:20 +0000 | [diff] [blame] | 850 | |
Igor Laevsky | 029bd93 | 2015-09-23 11:38:44 +0000 | [diff] [blame] | 851 | RemoveDeadInstAndUpdateBBI(SI); |
| 852 | continue; |
| 853 | } |
| 854 | } |
Philip Reames | 00c9b64 | 2014-08-05 17:48:20 +0000 | [diff] [blame] | 855 | |
Igor Laevsky | 029bd93 | 2015-09-23 11:38:44 +0000 | [diff] [blame] | 856 | // Remove null stores into the calloc'ed objects |
| 857 | Constant *StoredConstant = dyn_cast<Constant>(SI->getValueOperand()); |
Rui Ueyama | c487f77 | 2014-08-06 19:30:38 +0000 | [diff] [blame] | 858 | |
Igor Laevsky | 029bd93 | 2015-09-23 11:38:44 +0000 | [diff] [blame] | 859 | if (StoredConstant && StoredConstant->isNullValue() && |
| 860 | isRemovable(SI)) { |
| 861 | Instruction *UnderlyingPointer = dyn_cast<Instruction>( |
| 862 | GetUnderlyingObject(SI->getPointerOperand(), DL)); |
| 863 | |
| 864 | if (UnderlyingPointer && isCallocLikeFn(UnderlyingPointer, TLI) && |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 865 | memoryIsNotModifiedBetween(UnderlyingPointer, SI, AA)) { |
Igor Laevsky | 029bd93 | 2015-09-23 11:38:44 +0000 | [diff] [blame] | 866 | DEBUG(dbgs() |
| 867 | << "DSE: Remove null store to the calloc'ed object:\n DEAD: " |
| 868 | << *Inst << "\n OBJECT: " << *UnderlyingPointer << '\n'); |
| 869 | |
| 870 | RemoveDeadInstAndUpdateBBI(SI); |
Rui Ueyama | c487f77 | 2014-08-06 19:30:38 +0000 | [diff] [blame] | 871 | continue; |
| 872 | } |
Philip Reames | 00c9b64 | 2014-08-05 17:48:20 +0000 | [diff] [blame] | 873 | } |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 874 | } |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 875 | |
Erik Eckstein | 11fc817 | 2015-08-13 15:36:11 +0000 | [diff] [blame] | 876 | MemDepResult InstDep = MD->getDependency(Inst); |
| 877 | |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 878 | // Ignore any store where we can't find a local dependence. |
| 879 | // FIXME: cross-block DSE would be fun. :) |
| 880 | if (!InstDep.isDef() && !InstDep.isClobber()) |
| 881 | continue; |
Erik Eckstein | 11fc817 | 2015-08-13 15:36:11 +0000 | [diff] [blame] | 882 | |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 883 | // Figure out what location is being stored to. |
| 884 | MemoryLocation Loc = getLocForWrite(Inst, *AA); |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 885 | |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 886 | // If we didn't get a useful location, fail. |
| 887 | if (!Loc.Ptr) |
| 888 | continue; |
| 889 | |
| 890 | while (InstDep.isDef() || InstDep.isClobber()) { |
| 891 | // Get the memory clobbered by the instruction we depend on. MemDep will |
| 892 | // skip any instructions that 'Loc' clearly doesn't interact with. If we |
| 893 | // end up depending on a may- or must-aliased load, then we can't optimize |
Chad Rosier | 844e2df | 2016-06-15 21:41:22 +0000 | [diff] [blame] | 894 | // away the store and we bail out. However, if we depend on something |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 895 | // that overwrites the memory location we *can* potentially optimize it. |
| 896 | // |
| 897 | // Find out what memory location the dependent instruction stores. |
| 898 | Instruction *DepWrite = InstDep.getInst(); |
| 899 | MemoryLocation DepLoc = getLocForWrite(DepWrite, *AA); |
| 900 | // If we didn't get a useful location, or if it isn't a size, bail out. |
| 901 | if (!DepLoc.Ptr) |
| 902 | break; |
| 903 | |
| 904 | // If we find a write that is a) removable (i.e., non-volatile), b) is |
| 905 | // completely obliterated by the store to 'Loc', and c) which we know that |
| 906 | // 'Inst' doesn't load from, then we can remove it. |
| 907 | if (isRemovable(DepWrite) && |
| 908 | !isPossibleSelfRead(Inst, Loc, DepWrite, *TLI, *AA)) { |
| 909 | int64_t InstWriteOffset, DepWriteOffset; |
| 910 | OverwriteResult OR = |
Hal Finkel | a127103 | 2016-06-23 13:46:39 +0000 | [diff] [blame^] | 911 | isOverwrite(Loc, DepLoc, DL, *TLI, DepWriteOffset, InstWriteOffset, |
| 912 | DepWrite, IOL); |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 913 | if (OR == OverwriteComplete) { |
| 914 | DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: " |
| 915 | << *DepWrite << "\n KILLER: " << *Inst << '\n'); |
| 916 | |
| 917 | // Delete the store and now-dead instructions that feed it. |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 918 | deleteDeadInstruction(DepWrite, *MD, *TLI); |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 919 | ++NumFastStores; |
| 920 | MadeChange = true; |
| 921 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 922 | // deleteDeadInstruction can delete the current instruction in loop |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 923 | // cases, reset BBI. |
| 924 | BBI = Inst->getIterator(); |
Patrik Hagglund | 7205215 | 2016-06-20 09:10:10 +0000 | [diff] [blame] | 925 | auto BBBegin = BB.begin(); |
| 926 | while (BBI != BBBegin && isa<DbgInfoIntrinsic>(*(--BBI))) |
| 927 | ; |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 928 | break; |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 929 | } else if ((OR == OverwriteEnd && isShortenableAtTheEnd(DepWrite)) || |
| 930 | ((OR == OverwriteBegin && |
| 931 | isShortenableAtTheBeginning(DepWrite)))) { |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 932 | // TODO: base this on the target vector size so that if the earlier |
| 933 | // store was too small to get vector writes anyway then its likely |
| 934 | // a good idea to shorten it |
| 935 | // Power of 2 vector writes are probably always a bad idea to optimize |
| 936 | // as any store/memset/memcpy is likely using vector instructions so |
| 937 | // shortening it to not vector size is likely to be slower |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 938 | MemIntrinsic *DepIntrinsic = cast<MemIntrinsic>(DepWrite); |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 939 | unsigned DepWriteAlign = DepIntrinsic->getAlignment(); |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 940 | bool IsOverwriteEnd = (OR == OverwriteEnd); |
| 941 | if (!IsOverwriteEnd) |
| 942 | InstWriteOffset = int64_t(InstWriteOffset + Loc.Size); |
| 943 | |
| 944 | if ((llvm::isPowerOf2_64(InstWriteOffset) && |
| 945 | DepWriteAlign <= InstWriteOffset) || |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 946 | ((DepWriteAlign != 0) && InstWriteOffset % DepWriteAlign == 0)) { |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 947 | |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 948 | DEBUG(dbgs() << "DSE: Remove Dead Store:\n OW " |
| 949 | << (IsOverwriteEnd ? "END" : "BEGIN") << ": " |
| 950 | << *DepWrite << "\n KILLER (offset " |
| 951 | << InstWriteOffset << ", " << DepLoc.Size << ")" |
| 952 | << *Inst << '\n'); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 953 | |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 954 | int64_t NewLength = |
| 955 | IsOverwriteEnd |
| 956 | ? InstWriteOffset - DepWriteOffset |
| 957 | : DepLoc.Size - (InstWriteOffset - DepWriteOffset); |
| 958 | |
| 959 | Value *DepWriteLength = DepIntrinsic->getLength(); |
| 960 | Value *TrimmedLength = |
| 961 | ConstantInt::get(DepWriteLength->getType(), NewLength); |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 962 | DepIntrinsic->setLength(TrimmedLength); |
Jun Bum Lim | d29a24e | 2016-04-22 19:51:29 +0000 | [diff] [blame] | 963 | |
| 964 | if (!IsOverwriteEnd) { |
| 965 | int64_t OffsetMoved = (InstWriteOffset - DepWriteOffset); |
| 966 | Value *Indices[1] = { |
| 967 | ConstantInt::get(DepWriteLength->getType(), OffsetMoved)}; |
| 968 | GetElementPtrInst *NewDestGEP = GetElementPtrInst::CreateInBounds( |
| 969 | DepIntrinsic->getRawDest(), Indices, "", DepWrite); |
| 970 | DepIntrinsic->setDest(NewDestGEP); |
| 971 | } |
Pete Cooper | 856977c | 2011-11-09 23:07:35 +0000 | [diff] [blame] | 972 | MadeChange = true; |
| 973 | } |
| 974 | } |
Chris Lattner | 58b779e | 2010-11-30 07:23:21 +0000 | [diff] [blame] | 975 | } |
Chad Rosier | d7634fc | 2015-12-11 18:39:41 +0000 | [diff] [blame] | 976 | |
| 977 | // If this is a may-aliased store that is clobbering the store value, we |
| 978 | // can keep searching past it for another must-aliased pointer that stores |
| 979 | // to the same location. For example, in: |
| 980 | // store -> P |
| 981 | // store -> Q |
| 982 | // store -> P |
| 983 | // we can remove the first store to P even though we don't know if P and Q |
| 984 | // alias. |
| 985 | if (DepWrite == &BB.front()) break; |
| 986 | |
| 987 | // Can't look past this instruction if it might read 'Loc'. |
| 988 | if (AA->getModRefInfo(DepWrite, Loc) & MRI_Ref) |
| 989 | break; |
| 990 | |
| 991 | InstDep = MD->getPointerDependencyFrom(Loc, false, |
| 992 | DepWrite->getIterator(), &BB); |
Owen Anderson | 2b2bd28 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 993 | } |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 994 | } |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 995 | |
Chris Lattner | f2a8ba4 | 2008-11-28 21:29:52 +0000 | [diff] [blame] | 996 | // If this block ends in a return, unwind, or unreachable, all allocas are |
| 997 | // dead at its end, which means stores to them are also dead. |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 998 | if (BB.getTerminator()->getNumSuccessors() == 0) |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 999 | MadeChange |= handleEndBlock(BB, AA, MD, TLI); |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 1000 | |
Owen Anderson | 5e72db3 | 2007-07-11 00:46:18 +0000 | [diff] [blame] | 1001 | return MadeChange; |
| 1002 | } |
| 1003 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 1004 | static bool eliminateDeadStores(Function &F, AliasAnalysis *AA, |
| 1005 | MemoryDependenceResults *MD, DominatorTree *DT, |
| 1006 | const TargetLibraryInfo *TLI) { |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 1007 | bool MadeChange = false; |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 1008 | for (BasicBlock &BB : F) |
| 1009 | // Only check non-dead blocks. Dead blocks may have strange pointer |
| 1010 | // cycles that will confuse alias analysis. |
| 1011 | if (DT->isReachableFromEntry(&BB)) |
| 1012 | MadeChange |= eliminateDeadStores(BB, AA, MD, DT, TLI); |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 1013 | return MadeChange; |
Owen Anderson | aa07172 | 2007-07-11 23:19:17 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 1016 | //===----------------------------------------------------------------------===// |
| 1017 | // DSE Pass |
| 1018 | //===----------------------------------------------------------------------===// |
| 1019 | PreservedAnalyses DSEPass::run(Function &F, FunctionAnalysisManager &AM) { |
| 1020 | AliasAnalysis *AA = &AM.getResult<AAManager>(F); |
| 1021 | DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F); |
| 1022 | MemoryDependenceResults *MD = &AM.getResult<MemoryDependenceAnalysis>(F); |
| 1023 | const TargetLibraryInfo *TLI = &AM.getResult<TargetLibraryAnalysis>(F); |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 1024 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 1025 | if (!eliminateDeadStores(F, AA, MD, DT, TLI)) |
| 1026 | return PreservedAnalyses::all(); |
| 1027 | PreservedAnalyses PA; |
| 1028 | PA.preserve<DominatorTreeAnalysis>(); |
| 1029 | PA.preserve<GlobalsAA>(); |
| 1030 | PA.preserve<MemoryDependenceAnalysis>(); |
| 1031 | return PA; |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 1034 | /// A legacy pass for the legacy pass manager that wraps \c DSEPass. |
| 1035 | class DSELegacyPass : public FunctionPass { |
| 1036 | public: |
| 1037 | DSELegacyPass() : FunctionPass(ID) { |
| 1038 | initializeDSELegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | ddf4aee | 2007-08-08 18:38:28 +0000 | [diff] [blame] | 1039 | } |
Owen Anderson | 58704ee | 2011-09-06 18:14:09 +0000 | [diff] [blame] | 1040 | |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 1041 | bool runOnFunction(Function &F) override { |
| 1042 | if (skipFunction(F)) |
| 1043 | return false; |
| 1044 | |
| 1045 | DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 1046 | AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 1047 | MemoryDependenceResults *MD = |
| 1048 | &getAnalysis<MemoryDependenceWrapperPass>().getMemDep(); |
| 1049 | const TargetLibraryInfo *TLI = |
| 1050 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| 1051 | |
| 1052 | return eliminateDeadStores(F, AA, MD, DT, TLI); |
| 1053 | } |
| 1054 | |
| 1055 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 1056 | AU.setPreservesCFG(); |
| 1057 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 1058 | AU.addRequired<AAResultsWrapperPass>(); |
| 1059 | AU.addRequired<MemoryDependenceWrapperPass>(); |
| 1060 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 1061 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 1062 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 1063 | AU.addPreserved<MemoryDependenceWrapperPass>(); |
| 1064 | } |
| 1065 | |
| 1066 | static char ID; // Pass identification, replacement for typeid |
| 1067 | }; |
| 1068 | |
| 1069 | char DSELegacyPass::ID = 0; |
| 1070 | INITIALIZE_PASS_BEGIN(DSELegacyPass, "dse", "Dead Store Elimination", false, |
| 1071 | false) |
| 1072 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 1073 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 1074 | INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) |
| 1075 | INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass) |
| 1076 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 1077 | INITIALIZE_PASS_END(DSELegacyPass, "dse", "Dead Store Elimination", false, |
| 1078 | false) |
| 1079 | |
| 1080 | FunctionPass *llvm::createDeadStoreEliminationPass() { |
| 1081 | return new DSELegacyPass(); |
Owen Anderson | 32c4a05 | 2007-07-12 21:41:30 +0000 | [diff] [blame] | 1082 | } |