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