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