blob: d6427b2e955f83bbd01466cce9b324b7e464df98 [file] [log] [blame]
Owen Andersone3590582007-08-02 18:11:11 +00001//===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
Owen Anderson5e72db32007-07-11 00:46:18 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Owen Anderson5e72db32007-07-11 00:46:18 +00007//
8//===----------------------------------------------------------------------===//
9//
Chad Rosierd7634fc2015-12-11 18:39:41 +000010// 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 Anderson5e72db32007-07-11 00:46:18 +000015//
16//===----------------------------------------------------------------------===//
17
Justin Bogner594e07b2016-05-17 21:38:13 +000018#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Hal Finkela1271032016-06-23 13:46:39 +000019#include "llvm/ADT/DenseMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/Statistic.h"
Owen Andersonaa071722007-07-11 23:19:17 +000023#include "llvm/Analysis/AliasAnalysis.h"
Nick Lewycky32f80512011-10-22 21:59:35 +000024#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000025#include "llvm/Analysis/GlobalsModRef.h"
Victor Hernandezf390e042009-10-27 20:05:49 +000026#include "llvm/Analysis/MemoryBuiltins.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000027#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000028#include "llvm/Analysis/TargetLibraryInfo.h"
Chris Lattnerc0f33792010-11-30 23:05:20 +000029#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Constants.h"
31#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000032#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalVariable.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/IntrinsicInst.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/Pass.h"
Hal Finkela1271032016-06-23 13:46:39 +000038#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000040#include "llvm/Support/raw_ostream.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000041#include "llvm/Transforms/Scalar.h"
Owen Anderson5e72db32007-07-11 00:46:18 +000042#include "llvm/Transforms/Utils/Local.h"
Hal Finkela1271032016-06-23 13:46:39 +000043#include <map>
Owen Anderson5e72db32007-07-11 00:46:18 +000044using namespace llvm;
45
Chandler Carruth964daaa2014-04-22 02:55:47 +000046#define DEBUG_TYPE "dse"
47
Erik Eckstein11fc8172015-08-13 15:36:11 +000048STATISTIC(NumRedundantStores, "Number of redundant stores deleted");
Owen Anderson5e72db32007-07-11 00:46:18 +000049STATISTIC(NumFastStores, "Number of stores deleted");
50STATISTIC(NumFastOther , "Number of other instrs removed");
Hal Finkela1271032016-06-23 13:46:39 +000051STATISTIC(NumCompletePartials, "Number of stores dead by later partials");
52
53static cl::opt<bool>
54EnablePartialOverwriteTracking("enable-dse-partial-overwrite-tracking",
55 cl::init(true), cl::Hidden,
56 cl::desc("Enable partial-overwrite tracking in DSE"));
Owen Anderson5e72db32007-07-11 00:46:18 +000057
Owen Anderson5e72db32007-07-11 00:46:18 +000058
Chris Lattner67122512010-11-30 21:58:14 +000059//===----------------------------------------------------------------------===//
60// Helper functions
61//===----------------------------------------------------------------------===//
Jun Bum Lim6a7dc5c2016-07-22 18:27:24 +000062typedef std::map<int64_t, int64_t> OverlapIntervalsTy;
63typedef DenseMap<Instruction *, OverlapIntervalsTy> InstOverlapIntervalsTy;
Chris Lattner67122512010-11-30 21:58:14 +000064
Chad Rosiera8bc5122016-06-10 17:58:01 +000065/// Delete this instruction. Before we do, go through and zero out all the
Justin Bogner594e07b2016-05-17 21:38:13 +000066/// operands of this instruction. If any of them become dead, delete them and
67/// the computation tree that feeds them.
Eric Christopher0efe9f62015-08-19 02:15:13 +000068/// If ValueSet is non-null, remove any deleted instructions from it as well.
Justin Bogner594e07b2016-05-17 21:38:13 +000069static void
Chad Rosierdcfce2d2016-07-06 19:48:52 +000070deleteDeadInstruction(Instruction *I, BasicBlock::iterator *BBI,
71 MemoryDependenceResults &MD, const TargetLibraryInfo &TLI,
Jun Bum Lim6a7dc5c2016-07-22 18:27:24 +000072 InstOverlapIntervalsTy &IOL,
Eli Friedmana6707f52016-08-12 01:09:53 +000073 DenseMap<Instruction*, size_t> *InstrOrdering,
Justin Bogner594e07b2016-05-17 21:38:13 +000074 SmallSetVector<Value *, 16> *ValueSet = nullptr) {
Eric Christopher0efe9f62015-08-19 02:15:13 +000075 SmallVector<Instruction*, 32> NowDeadInsts;
76
77 NowDeadInsts.push_back(I);
78 --NumFastOther;
79
Chad Rosierdcfce2d2016-07-06 19:48:52 +000080 // 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 Christopher0efe9f62015-08-19 02:15:13 +000084 // 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 Friedmana6707f52016-08-12 01:09:53 +0000106 if (ValueSet) ValueSet->remove(DeadInst);
107 InstrOrdering->erase(DeadInst);
108 IOL.erase(DeadInst);
Chad Rosierdcfce2d2016-07-06 19:48:52 +0000109
110 if (NewIter == DeadInst->getIterator())
111 NewIter = DeadInst->eraseFromParent();
112 else
113 DeadInst->eraseFromParent();
Eric Christopher0efe9f62015-08-19 02:15:13 +0000114 } while (!NowDeadInsts.empty());
Chad Rosierdcfce2d2016-07-06 19:48:52 +0000115 *BBI = NewIter;
Eric Christopher0efe9f62015-08-19 02:15:13 +0000116}
117
Justin Bogner594e07b2016-05-17 21:38:13 +0000118/// Does this instruction write some memory? This only returns true for things
119/// that we can analyze with other helpers below.
Chandler Carruthdbe40fb2015-08-12 18:01:44 +0000120static bool hasMemoryWrite(Instruction *I, const TargetLibraryInfo &TLI) {
Nick Lewycky90271472009-11-10 06:46:40 +0000121 if (isa<StoreInst>(I))
122 return true;
123 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
124 switch (II->getIntrinsicID()) {
Chris Lattner2764b4d2009-12-02 06:35:55 +0000125 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 Lewycky90271472009-11-10 06:46:40 +0000133 }
134 }
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000135 if (auto CS = CallSite(I)) {
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000136 if (Function *F = CS.getCalledFunction()) {
Chad Rosier624fee52016-06-16 17:06:04 +0000137 StringRef FnName = F->getName();
138 if (TLI.has(LibFunc::strcpy) && FnName == TLI.getName(LibFunc::strcpy))
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000139 return true;
Chad Rosier624fee52016-06-16 17:06:04 +0000140 if (TLI.has(LibFunc::strncpy) && FnName == TLI.getName(LibFunc::strncpy))
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000141 return true;
Chad Rosier624fee52016-06-16 17:06:04 +0000142 if (TLI.has(LibFunc::strcat) && FnName == TLI.getName(LibFunc::strcat))
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000143 return true;
Chad Rosier624fee52016-06-16 17:06:04 +0000144 if (TLI.has(LibFunc::strncat) && FnName == TLI.getName(LibFunc::strncat))
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000145 return true;
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000146 }
147 }
Nick Lewycky90271472009-11-10 06:46:40 +0000148 return false;
149}
150
Justin Bogner594e07b2016-05-17 21:38:13 +0000151/// 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 Carruthac80dc72015-06-17 07:18:54 +0000154static MemoryLocation getLocForWrite(Instruction *Inst, AliasAnalysis &AA) {
Chris Lattner58b779e2010-11-30 07:23:21 +0000155 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
Chandler Carruth70c61c12015-06-04 02:03:15 +0000156 return MemoryLocation::get(SI);
Owen Anderson58704ee2011-09-06 18:14:09 +0000157
Chris Lattner58b779e2010-11-30 07:23:21 +0000158 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(Inst)) {
159 // memcpy/memmove/memset.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000160 MemoryLocation Loc = MemoryLocation::getForDest(MI);
Chris Lattner58b779e2010-11-30 07:23:21 +0000161 return Loc;
162 }
Owen Anderson58704ee2011-09-06 18:14:09 +0000163
Chris Lattner58b779e2010-11-30 07:23:21 +0000164 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst);
Chandler Carruthac80dc72015-06-17 07:18:54 +0000165 if (!II)
166 return MemoryLocation();
Owen Anderson58704ee2011-09-06 18:14:09 +0000167
Chris Lattner58b779e2010-11-30 07:23:21 +0000168 switch (II->getIntrinsicID()) {
Chandler Carruthac80dc72015-06-17 07:18:54 +0000169 default:
170 return MemoryLocation(); // Unhandled intrinsic.
Chris Lattner58b779e2010-11-30 07:23:21 +0000171 case Intrinsic::init_trampoline:
Chris Lattner58b779e2010-11-30 07:23:21 +0000172 // FIXME: We don't know the size of the trampoline, so we can't really
173 // handle it here.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000174 return MemoryLocation(II->getArgOperand(0));
Chris Lattner58b779e2010-11-30 07:23:21 +0000175 case Intrinsic::lifetime_end: {
176 uint64_t Len = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue();
Chandler Carruthac80dc72015-06-17 07:18:54 +0000177 return MemoryLocation(II->getArgOperand(1), Len);
Chris Lattner58b779e2010-11-30 07:23:21 +0000178 }
179 }
180}
181
Justin Bogner594e07b2016-05-17 21:38:13 +0000182/// Return the location read by the specified "hasMemoryWrite" instruction if
183/// any.
Chandler Carruthdbe40fb2015-08-12 18:01:44 +0000184static MemoryLocation getLocForRead(Instruction *Inst,
185 const TargetLibraryInfo &TLI) {
186 assert(hasMemoryWrite(Inst, TLI) && "Unknown instruction case");
Owen Anderson58704ee2011-09-06 18:14:09 +0000187
Chris Lattner94fbdf32010-12-06 01:48:06 +0000188 // 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 Carruth70c61c12015-06-04 02:03:15 +0000191 return MemoryLocation::getForSource(MTI);
Chandler Carruthac80dc72015-06-17 07:18:54 +0000192 return MemoryLocation();
Chris Lattner94fbdf32010-12-06 01:48:06 +0000193}
194
Justin Bogner594e07b2016-05-17 21:38:13 +0000195/// If the value of this instruction and the memory it writes to is unused, may
196/// we delete this instruction?
Chris Lattner3590ef82010-11-30 05:30:45 +0000197static bool isRemovable(Instruction *I) {
Eli Friedman9a468152011-08-17 22:22:24 +0000198 // Don't remove volatile/atomic stores.
Nick Lewycky90271472009-11-10 06:46:40 +0000199 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Eli Friedman9a468152011-08-17 22:22:24 +0000200 return SI->isUnordered();
Owen Anderson58704ee2011-09-06 18:14:09 +0000201
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000202 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 Anderson58704ee2011-09-06 18:14:09 +0000212
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000213 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 Lattnerb63ba732010-11-30 19:12:10 +0000219 }
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000220
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000221 if (auto CS = CallSite(I))
Nick Lewycky42bca052012-09-25 01:55:59 +0000222 return CS.getInstruction()->use_empty();
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000223
224 return false;
Nick Lewycky90271472009-11-10 06:46:40 +0000225}
226
Pete Cooper856977c2011-11-09 23:07:35 +0000227
Jun Bum Limd29a24e2016-04-22 19:51:29 +0000228/// Returns true if the end of this instruction can be safely shortened in
Pete Cooper856977c2011-11-09 23:07:35 +0000229/// length.
Jun Bum Limd29a24e2016-04-22 19:51:29 +0000230static bool isShortenableAtTheEnd(Instruction *I) {
Pete Cooper856977c2011-11-09 23:07:35 +0000231 // Don't shorten stores for now
232 if (isa<StoreInst>(I))
233 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000234
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000235 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 Limd29a24e2016-04-22 19:51:29 +0000241 // FIXME: Add memmove if it's also safe to transform.
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000242 return true;
243 }
Pete Cooper856977c2011-11-09 23:07:35 +0000244 }
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000245
246 // Don't shorten libcalls calls for now.
247
248 return false;
Pete Cooper856977c2011-11-09 23:07:35 +0000249}
250
Jun Bum Limd29a24e2016-04-22 19:51:29 +0000251/// Returns true if the beginning of this instruction can be safely shortened
252/// in length.
253static 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 Bogner594e07b2016-05-17 21:38:13 +0000260/// Return the pointer that is being written to.
Chris Lattner67122512010-11-30 21:58:14 +0000261static Value *getStoredPointerOperand(Instruction *I) {
Nick Lewycky90271472009-11-10 06:46:40 +0000262 if (StoreInst *SI = dyn_cast<StoreInst>(I))
263 return SI->getPointerOperand();
264 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
Chris Lattner67122512010-11-30 21:58:14 +0000265 return MI->getDest();
Gabor Greif91f95892010-06-24 12:03:56 +0000266
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000267 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 Sands1925d3a2009-11-10 13:49:50 +0000273 }
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000274
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000275 CallSite CS(I);
Nick Lewycky9f4729d2012-09-24 22:09:10 +0000276 // All the supported functions so far happen to have dest as their first
277 // argument.
278 return CS.getArgument(0);
Nick Lewycky90271472009-11-10 06:46:40 +0000279}
280
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000281static uint64_t getPointerSize(const Value *V, const DataLayout &DL,
Chandler Carruthdbe40fb2015-08-12 18:01:44 +0000282 const TargetLibraryInfo &TLI) {
Nuno Lopes55fff832012-06-21 15:45:28 +0000283 uint64_t Size;
Chandler Carruthdbe40fb2015-08-12 18:01:44 +0000284 if (getObjectSize(V, Size, DL, &TLI))
Nuno Lopes55fff832012-06-21 15:45:28 +0000285 return Size;
Chandler Carruthecbd1682015-06-17 07:21:38 +0000286 return MemoryLocation::UnknownSize;
Chris Lattner903add82010-11-30 23:43:23 +0000287}
Chris Lattner51c28a92010-11-30 19:34:42 +0000288
Pete Cooper856977c2011-11-09 23:07:35 +0000289namespace {
Jun Bum Limd29a24e2016-04-22 19:51:29 +0000290enum OverwriteResult {
291 OverwriteBegin,
292 OverwriteComplete,
293 OverwriteEnd,
294 OverwriteUnknown
295};
Pete Cooper856977c2011-11-09 23:07:35 +0000296}
297
Jun Bum Limd29a24e2016-04-22 19:51:29 +0000298/// 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 Carruthac80dc72015-06-17 07:18:54 +0000303static OverwriteResult isOverwrite(const MemoryLocation &Later,
304 const MemoryLocation &Earlier,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000305 const DataLayout &DL,
Chandler Carruthdbe40fb2015-08-12 18:01:44 +0000306 const TargetLibraryInfo &TLI,
Hal Finkela1271032016-06-23 13:46:39 +0000307 int64_t &EarlierOff, int64_t &LaterOff,
308 Instruction *DepWrite,
309 InstOverlapIntervalsTy &IOL) {
Chad Rosier72a793c2016-06-15 22:17:38 +0000310 // 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 Lattnerc0f33792010-11-30 23:05:20 +0000315 const Value *P1 = Earlier.Ptr->stripPointerCasts();
316 const Value *P2 = Later.Ptr->stripPointerCasts();
Owen Anderson58704ee2011-09-06 18:14:09 +0000317
Chris Lattnerc0f33792010-11-30 23:05:20 +0000318 // 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 Lattnerc0f33792010-11-30 23:05:20 +0000321 // Make sure that the Later size is >= the Earlier size.
Pete Cooper856977c2011-11-09 23:07:35 +0000322 if (Later.Size >= Earlier.Size)
323 return OverwriteComplete;
Chris Lattner77d79fa2010-11-30 19:28:23 +0000324 }
Owen Anderson58704ee2011-09-06 18:14:09 +0000325
Chris Lattner903add82010-11-30 23:43:23 +0000326 // Check to see if the later store is to the entire object (either a global,
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000327 // an alloca, or a byval/inalloca argument). If so, then it clearly
328 // overwrites any other store to the same object.
Rafael Espindola5f57f462014-02-21 18:34:28 +0000329 const Value *UO1 = GetUnderlyingObject(P1, DL),
330 *UO2 = GetUnderlyingObject(P2, DL);
Owen Anderson58704ee2011-09-06 18:14:09 +0000331
Chris Lattner903add82010-11-30 23:43:23 +0000332 // 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 Cooper856977c2011-11-09 23:07:35 +0000335 return OverwriteUnknown;
Owen Anderson58704ee2011-09-06 18:14:09 +0000336
Chris Lattner903add82010-11-30 23:43:23 +0000337 // If the "Later" store is to a recognizable object, get its size.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000338 uint64_t ObjectSize = getPointerSize(UO2, DL, TLI);
Chandler Carruthecbd1682015-06-17 07:21:38 +0000339 if (ObjectSize != MemoryLocation::UnknownSize)
Pete Coopera4237c32011-11-10 20:22:08 +0000340 if (ObjectSize == Later.Size && ObjectSize >= Earlier.Size)
Pete Cooper856977c2011-11-09 23:07:35 +0000341 return OverwriteComplete;
Owen Anderson58704ee2011-09-06 18:14:09 +0000342
Chris Lattnerc0f33792010-11-30 23:05:20 +0000343 // 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 Cooper856977c2011-11-09 23:07:35 +0000346 EarlierOff = 0;
347 LaterOff = 0;
Rafael Espindola5f57f462014-02-21 18:34:28 +0000348 const Value *BP1 = GetPointerBaseWithConstantOffset(P1, EarlierOff, DL);
349 const Value *BP2 = GetPointerBaseWithConstantOffset(P2, LaterOff, DL);
Owen Anderson58704ee2011-09-06 18:14:09 +0000350
Chris Lattnerc0f33792010-11-30 23:05:20 +0000351 // If the base pointers still differ, we have two completely different stores.
352 if (BP1 != BP2)
Pete Cooper856977c2011-11-09 23:07:35 +0000353 return OverwriteUnknown;
Bill Wendlingdb40b5c2011-03-26 01:20:37 +0000354
Bill Wendling19f33b92011-03-26 08:02:59 +0000355 // The later store completely overlaps the earlier store if:
Owen Anderson58704ee2011-09-06 18:14:09 +0000356 //
Bill Wendling19f33b92011-03-26 08:02:59 +0000357 // 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 Anderson58704ee2011-09-06 18:14:09 +0000362 //
Bill Wendling19f33b92011-03-26 08:02:59 +0000363 // 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 Wendling50341592011-03-30 21:37:19 +0000368 //
369 // We have to be careful here as *Off is signed while *.Size is unsigned.
Bill Wendlingb5139922011-03-26 09:32:07 +0000370 if (EarlierOff >= LaterOff &&
Craig Topper2a404182012-08-14 07:32:05 +0000371 Later.Size >= Earlier.Size &&
Bill Wendling50341592011-03-30 21:37:19 +0000372 uint64_t(EarlierOff - LaterOff) + Earlier.Size <= Later.Size)
Pete Cooper856977c2011-11-09 23:07:35 +0000373 return OverwriteComplete;
Nadav Rotem465834c2012-07-24 10:51:42 +0000374
Hal Finkela1271032016-06-23 13:46:39 +0000375 // 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 Lim596a3bd2016-06-30 15:32:20 +0000399 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 Finkela1271032016-06-23 13:46:39 +0000403 LaterIntStart = std::min(LaterIntStart, ILI->second);
404 LaterIntEnd = std::max(LaterIntEnd, ILI->first);
405 ILI = IM.erase(ILI);
406
Jun Bum Lim596a3bd2016-06-30 15:32:20 +0000407 // 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 Finkela1271032016-06-23 13:46:39 +0000415 LaterIntEnd = std::max(LaterIntEnd, ILI->first);
Jun Bum Lim596a3bd2016-06-30 15:32:20 +0000416 ILI = IM.erase(ILI);
417 }
Hal Finkela1271032016-06-23 13:46:39 +0000418 }
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 Limd29a24e2016-04-22 19:51:29 +0000435 // Another interesting case is if the later store overwrites the end of the
436 // earlier store.
Pete Cooper856977c2011-11-09 23:07:35 +0000437 //
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 Lim6a7dc5c2016-07-22 18:27:24 +0000443 if (!EnablePartialOverwriteTracking &&
444 (LaterOff > EarlierOff && LaterOff < int64_t(EarlierOff + Earlier.Size) &&
445 int64_t(LaterOff + Later.Size) >= int64_t(EarlierOff + Earlier.Size)))
Pete Cooper856977c2011-11-09 23:07:35 +0000446 return OverwriteEnd;
Bill Wendling19f33b92011-03-26 08:02:59 +0000447
Jun Bum Limd29a24e2016-04-22 19:51:29 +0000448 // 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 Lim6a7dc5c2016-07-22 18:27:24 +0000457 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 Limd29a24e2016-04-22 19:51:29 +0000462 return OverwriteBegin;
463 }
Bill Wendling19f33b92011-03-26 08:02:59 +0000464 // Otherwise, they don't completely overlap.
Pete Cooper856977c2011-11-09 23:07:35 +0000465 return OverwriteUnknown;
Nick Lewycky90271472009-11-10 06:46:40 +0000466}
467
Justin Bogner594e07b2016-05-17 21:38:13 +0000468/// If 'Inst' might be a self read (i.e. a noop copy of a
Chris Lattner94fbdf32010-12-06 01:48:06 +0000469/// memory region into an identical pointer) then it doesn't actually make its
Owen Anderson58704ee2011-09-06 18:14:09 +0000470/// input dead in the traditional sense. Consider this case:
Chris Lattner94fbdf32010-12-06 01:48:06 +0000471///
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.
481static bool isPossibleSelfRead(Instruction *Inst,
Chandler Carruthac80dc72015-06-17 07:18:54 +0000482 const MemoryLocation &InstStoreLoc,
Chandler Carruthdbe40fb2015-08-12 18:01:44 +0000483 Instruction *DepWrite,
484 const TargetLibraryInfo &TLI,
485 AliasAnalysis &AA) {
Chris Lattner94fbdf32010-12-06 01:48:06 +0000486 // Self reads can only happen for instructions that read memory. Get the
487 // location read.
Chandler Carruthdbe40fb2015-08-12 18:01:44 +0000488 MemoryLocation InstReadLoc = getLocForRead(Inst, TLI);
Craig Topperf40110f2014-04-25 05:29:35 +0000489 if (!InstReadLoc.Ptr) return false; // Not a reading instruction.
Owen Anderson58704ee2011-09-06 18:14:09 +0000490
Chris Lattner94fbdf32010-12-06 01:48:06 +0000491 // If the read and written loc obviously don't alias, it isn't a read.
492 if (AA.isNoAlias(InstReadLoc, InstStoreLoc)) return false;
Owen Anderson58704ee2011-09-06 18:14:09 +0000493
Chris Lattner94fbdf32010-12-06 01:48:06 +0000494 // 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 Carruthdbe40fb2015-08-12 18:01:44 +0000502 MemoryLocation DepReadLoc = getLocForRead(DepWrite, TLI);
Owen Anderson58704ee2011-09-06 18:14:09 +0000503
Chris Lattner94fbdf32010-12-06 01:48:06 +0000504 if (DepReadLoc.Ptr && AA.isMustAlias(InstReadLoc.Ptr, DepReadLoc.Ptr))
505 return false;
Owen Anderson58704ee2011-09-06 18:14:09 +0000506
Chris Lattner94fbdf32010-12-06 01:48:06 +0000507 // 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
Chris Lattner67122512010-11-30 21:58:14 +0000512
Justin Bogner594e07b2016-05-17 21:38:13 +0000513/// Returns true if the memory which is accessed by the second instruction is not
514/// modified between the first and the second instruction.
515/// Precondition: Second instruction must be dominated by the first
516/// instruction.
517static bool memoryIsNotModifiedBetween(Instruction *FirstI,
518 Instruction *SecondI,
519 AliasAnalysis *AA) {
520 SmallVector<BasicBlock *, 16> WorkList;
521 SmallPtrSet<BasicBlock *, 8> Visited;
522 BasicBlock::iterator FirstBBI(FirstI);
523 ++FirstBBI;
524 BasicBlock::iterator SecondBBI(SecondI);
525 BasicBlock *FirstBB = FirstI->getParent();
526 BasicBlock *SecondBB = SecondI->getParent();
527 MemoryLocation MemLoc = MemoryLocation::get(SecondI);
Chris Lattner67122512010-11-30 21:58:14 +0000528
Justin Bogner594e07b2016-05-17 21:38:13 +0000529 // Start checking the store-block.
530 WorkList.push_back(SecondBB);
531 bool isFirstBlock = true;
532
533 // Check all blocks going backward until we reach the load-block.
534 while (!WorkList.empty()) {
535 BasicBlock *B = WorkList.pop_back_val();
536
537 // Ignore instructions before LI if this is the FirstBB.
538 BasicBlock::iterator BI = (B == FirstBB ? FirstBBI : B->begin());
539
540 BasicBlock::iterator EI;
541 if (isFirstBlock) {
542 // Ignore instructions after SI if this is the first visit of SecondBB.
543 assert(B == SecondBB && "first block is not the store block");
544 EI = SecondBBI;
545 isFirstBlock = false;
546 } else {
547 // It's not SecondBB or (in case of a loop) the second visit of SecondBB.
548 // In this case we also have to look at instructions after SI.
549 EI = B->end();
550 }
551 for (; BI != EI; ++BI) {
552 Instruction *I = &*BI;
553 if (I->mayWriteToMemory() && I != SecondI) {
554 auto Res = AA->getModRefInfo(I, MemLoc);
555 if (Res != MRI_NoModRef)
556 return false;
557 }
558 }
559 if (B != FirstBB) {
560 assert(B != &FirstBB->getParent()->getEntryBlock() &&
561 "Should not hit the entry block because SI must be dominated by LI");
562 for (auto PredI = pred_begin(B), PE = pred_end(B); PredI != PE; ++PredI) {
563 if (!Visited.insert(*PredI).second)
564 continue;
565 WorkList.push_back(*PredI);
566 }
567 }
568 }
569 return true;
570}
571
572/// Find all blocks that will unconditionally lead to the block BB and append
573/// them to F.
574static void findUnconditionalPreds(SmallVectorImpl<BasicBlock *> &Blocks,
575 BasicBlock *BB, DominatorTree *DT) {
576 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
577 BasicBlock *Pred = *I;
578 if (Pred == BB) continue;
579 TerminatorInst *PredTI = Pred->getTerminator();
580 if (PredTI->getNumSuccessors() != 1)
581 continue;
582
583 if (DT->isReachableFromEntry(Pred))
584 Blocks.push_back(Pred);
585 }
586}
587
588/// Handle frees of entire structures whose dependency is a store
589/// to a field of that structure.
590static bool handleFree(CallInst *F, AliasAnalysis *AA,
591 MemoryDependenceResults *MD, DominatorTree *DT,
Jun Bum Lim6a7dc5c2016-07-22 18:27:24 +0000592 const TargetLibraryInfo *TLI,
Eli Friedmana6707f52016-08-12 01:09:53 +0000593 InstOverlapIntervalsTy &IOL,
594 DenseMap<Instruction*, size_t> *InstrOrdering) {
Justin Bogner594e07b2016-05-17 21:38:13 +0000595 bool MadeChange = false;
596
597 MemoryLocation Loc = MemoryLocation(F->getOperand(0));
598 SmallVector<BasicBlock *, 16> Blocks;
599 Blocks.push_back(F->getParent());
600 const DataLayout &DL = F->getModule()->getDataLayout();
601
602 while (!Blocks.empty()) {
603 BasicBlock *BB = Blocks.pop_back_val();
604 Instruction *InstPt = BB->getTerminator();
605 if (BB == F->getParent()) InstPt = F;
606
607 MemDepResult Dep =
608 MD->getPointerDependencyFrom(Loc, false, InstPt->getIterator(), BB);
609 while (Dep.isDef() || Dep.isClobber()) {
610 Instruction *Dependency = Dep.getInst();
611 if (!hasMemoryWrite(Dependency, *TLI) || !isRemovable(Dependency))
612 break;
613
614 Value *DepPointer =
615 GetUnderlyingObject(getStoredPointerOperand(Dependency), DL);
616
617 // Check for aliasing.
618 if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
619 break;
620
Chad Rosier667b1ca2016-07-19 16:50:57 +0000621 DEBUG(dbgs() << "DSE: Dead Store to soon to be freed memory:\n DEAD: "
622 << *Dependency << '\n');
623
Chad Rosier840b3ef2016-06-10 17:59:22 +0000624 // DCE instructions only used to calculate that store.
Chad Rosierdcfce2d2016-07-06 19:48:52 +0000625 BasicBlock::iterator BBI(Dependency);
Eli Friedmana6707f52016-08-12 01:09:53 +0000626 deleteDeadInstruction(Dependency, &BBI, *MD, *TLI, IOL, InstrOrdering);
Justin Bogner594e07b2016-05-17 21:38:13 +0000627 ++NumFastStores;
628 MadeChange = true;
629
630 // Inst's old Dependency is now deleted. Compute the next dependency,
631 // which may also be dead, as in
632 // s[0] = 0;
633 // s[1] = 0; // This has just been deleted.
634 // free(s);
Chad Rosierdcfce2d2016-07-06 19:48:52 +0000635 Dep = MD->getPointerDependencyFrom(Loc, false, BBI, BB);
Justin Bogner594e07b2016-05-17 21:38:13 +0000636 }
637
638 if (Dep.isNonLocal())
639 findUnconditionalPreds(Blocks, BB, DT);
640 }
641
642 return MadeChange;
643}
644
645/// Check to see if the specified location may alias any of the stack objects in
646/// the DeadStackObjects set. If so, they become live because the location is
647/// being loaded.
648static void removeAccessedObjects(const MemoryLocation &LoadedLoc,
649 SmallSetVector<Value *, 16> &DeadStackObjects,
650 const DataLayout &DL, AliasAnalysis *AA,
651 const TargetLibraryInfo *TLI) {
652 const Value *UnderlyingPointer = GetUnderlyingObject(LoadedLoc.Ptr, DL);
653
654 // A constant can't be in the dead pointer set.
655 if (isa<Constant>(UnderlyingPointer))
656 return;
657
658 // If the kill pointer can be easily reduced to an alloca, don't bother doing
659 // extraneous AA queries.
660 if (isa<AllocaInst>(UnderlyingPointer) || isa<Argument>(UnderlyingPointer)) {
661 DeadStackObjects.remove(const_cast<Value*>(UnderlyingPointer));
662 return;
663 }
664
665 // Remove objects that could alias LoadedLoc.
666 DeadStackObjects.remove_if([&](Value *I) {
667 // See if the loaded location could alias the stack location.
668 MemoryLocation StackLoc(I, getPointerSize(I, DL, *TLI));
669 return !AA->isNoAlias(StackLoc, LoadedLoc);
670 });
671}
672
673/// Remove dead stores to stack-allocated locations in the function end block.
674/// Ex:
675/// %A = alloca i32
676/// ...
677/// store i32 1, i32* %A
678/// ret void
679static bool handleEndBlock(BasicBlock &BB, AliasAnalysis *AA,
680 MemoryDependenceResults *MD,
Jun Bum Lim6a7dc5c2016-07-22 18:27:24 +0000681 const TargetLibraryInfo *TLI,
Eli Friedmana6707f52016-08-12 01:09:53 +0000682 InstOverlapIntervalsTy &IOL,
683 DenseMap<Instruction*, size_t> *InstrOrdering) {
Justin Bogner594e07b2016-05-17 21:38:13 +0000684 bool MadeChange = false;
685
686 // Keep track of all of the stack objects that are dead at the end of the
687 // function.
688 SmallSetVector<Value*, 16> DeadStackObjects;
689
690 // Find all of the alloca'd pointers in the entry block.
691 BasicBlock &Entry = BB.getParent()->front();
692 for (Instruction &I : Entry) {
693 if (isa<AllocaInst>(&I))
694 DeadStackObjects.insert(&I);
695
696 // Okay, so these are dead heap objects, but if the pointer never escapes
697 // then it's leaked by this function anyways.
698 else if (isAllocLikeFn(&I, TLI) && !PointerMayBeCaptured(&I, true, true))
699 DeadStackObjects.insert(&I);
700 }
701
702 // Treat byval or inalloca arguments the same, stores to them are dead at the
703 // end of the function.
704 for (Argument &AI : BB.getParent()->args())
705 if (AI.hasByValOrInAllocaAttr())
706 DeadStackObjects.insert(&AI);
707
708 const DataLayout &DL = BB.getModule()->getDataLayout();
709
710 // Scan the basic block backwards
711 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
712 --BBI;
713
714 // If we find a store, check to see if it points into a dead stack value.
715 if (hasMemoryWrite(&*BBI, *TLI) && isRemovable(&*BBI)) {
716 // See through pointer-to-pointer bitcasts
717 SmallVector<Value *, 4> Pointers;
718 GetUnderlyingObjects(getStoredPointerOperand(&*BBI), Pointers, DL);
719
720 // Stores to stack values are valid candidates for removal.
721 bool AllDead = true;
Benjamin Kramer135f7352016-06-26 12:28:59 +0000722 for (Value *Pointer : Pointers)
723 if (!DeadStackObjects.count(Pointer)) {
Justin Bogner594e07b2016-05-17 21:38:13 +0000724 AllDead = false;
725 break;
726 }
727
728 if (AllDead) {
Chad Rosierdcfce2d2016-07-06 19:48:52 +0000729 Instruction *Dead = &*BBI;
Justin Bogner594e07b2016-05-17 21:38:13 +0000730
731 DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n DEAD: "
732 << *Dead << "\n Objects: ";
733 for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(),
734 E = Pointers.end(); I != E; ++I) {
735 dbgs() << **I;
736 if (std::next(I) != E)
737 dbgs() << ", ";
738 }
739 dbgs() << '\n');
740
741 // DCE instructions only used to calculate that store.
Eli Friedmana6707f52016-08-12 01:09:53 +0000742 deleteDeadInstruction(Dead, &BBI, *MD, *TLI, IOL, InstrOrdering, &DeadStackObjects);
Justin Bogner594e07b2016-05-17 21:38:13 +0000743 ++NumFastStores;
744 MadeChange = true;
745 continue;
746 }
747 }
748
749 // Remove any dead non-memory-mutating instructions.
750 if (isInstructionTriviallyDead(&*BBI, TLI)) {
Chad Rosier8b5fa7a2016-07-19 18:11:11 +0000751 DEBUG(dbgs() << "DSE: Removing trivially dead instruction:\n DEAD: "
752 << *&*BBI << '\n');
Eli Friedmana6707f52016-08-12 01:09:53 +0000753 deleteDeadInstruction(&*BBI, &BBI, *MD, *TLI, IOL, InstrOrdering, &DeadStackObjects);
Justin Bogner594e07b2016-05-17 21:38:13 +0000754 ++NumFastOther;
755 MadeChange = true;
756 continue;
757 }
758
759 if (isa<AllocaInst>(BBI)) {
760 // Remove allocas from the list of dead stack objects; there can't be
761 // any references before the definition.
762 DeadStackObjects.remove(&*BBI);
763 continue;
764 }
765
766 if (auto CS = CallSite(&*BBI)) {
767 // Remove allocation function calls from the list of dead stack objects;
768 // there can't be any references before the definition.
769 if (isAllocLikeFn(&*BBI, TLI))
770 DeadStackObjects.remove(&*BBI);
771
772 // If this call does not access memory, it can't be loading any of our
773 // pointers.
774 if (AA->doesNotAccessMemory(CS))
775 continue;
776
777 // If the call might load from any of our allocas, then any store above
778 // the call is live.
779 DeadStackObjects.remove_if([&](Value *I) {
780 // See if the call site touches the value.
781 ModRefInfo A = AA->getModRefInfo(CS, I, getPointerSize(I, DL, *TLI));
782
783 return A == MRI_ModRef || A == MRI_Ref;
784 });
785
786 // If all of the allocas were clobbered by the call then we're not going
787 // to find anything else to process.
788 if (DeadStackObjects.empty())
789 break;
790
791 continue;
792 }
793
Anna Thomas6a78c782016-07-07 20:51:42 +0000794 // We can remove the dead stores, irrespective of the fence and its ordering
795 // (release/acquire/seq_cst). Fences only constraints the ordering of
796 // already visible stores, it does not make a store visible to other
797 // threads. So, skipping over a fence does not change a store from being
798 // dead.
799 if (isa<FenceInst>(*BBI))
800 continue;
801
Justin Bogner594e07b2016-05-17 21:38:13 +0000802 MemoryLocation LoadedLoc;
803
804 // If we encounter a use of the pointer, it is no longer considered dead
805 if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
806 if (!L->isUnordered()) // Be conservative with atomic/volatile load
807 break;
808 LoadedLoc = MemoryLocation::get(L);
809 } else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
810 LoadedLoc = MemoryLocation::get(V);
811 } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(BBI)) {
812 LoadedLoc = MemoryLocation::getForSource(MTI);
813 } else if (!BBI->mayReadFromMemory()) {
814 // Instruction doesn't read memory. Note that stores that weren't removed
815 // above will hit this case.
816 continue;
817 } else {
818 // Unknown inst; assume it clobbers everything.
819 break;
820 }
821
822 // Remove any allocas from the DeadPointer set that are loaded, as this
823 // makes any stores above the access live.
824 removeAccessedObjects(LoadedLoc, DeadStackObjects, DL, AA, TLI);
825
826 // If all of the allocas were clobbered by the access then we're not going
827 // to find anything else to process.
828 if (DeadStackObjects.empty())
829 break;
830 }
831
832 return MadeChange;
833}
834
Jun Bum Lim6a7dc5c2016-07-22 18:27:24 +0000835static bool tryToShorten(Instruction *EarlierWrite, int64_t &EarlierOffset,
836 int64_t &EarlierSize, int64_t LaterOffset,
837 int64_t LaterSize, bool IsOverwriteEnd) {
838 // TODO: base this on the target vector size so that if the earlier
839 // store was too small to get vector writes anyway then its likely
840 // a good idea to shorten it
841 // Power of 2 vector writes are probably always a bad idea to optimize
842 // as any store/memset/memcpy is likely using vector instructions so
843 // shortening it to not vector size is likely to be slower
844 MemIntrinsic *EarlierIntrinsic = cast<MemIntrinsic>(EarlierWrite);
845 unsigned EarlierWriteAlign = EarlierIntrinsic->getAlignment();
846 if (!IsOverwriteEnd)
847 LaterOffset = int64_t(LaterOffset + LaterSize);
848
849 if (!(llvm::isPowerOf2_64(LaterOffset) && EarlierWriteAlign <= LaterOffset) &&
850 !((EarlierWriteAlign != 0) && LaterOffset % EarlierWriteAlign == 0))
851 return false;
852
853 DEBUG(dbgs() << "DSE: Remove Dead Store:\n OW "
854 << (IsOverwriteEnd ? "END" : "BEGIN") << ": " << *EarlierWrite
855 << "\n KILLER (offset " << LaterOffset << ", " << EarlierSize
856 << ")\n");
857
858 int64_t NewLength = IsOverwriteEnd
859 ? LaterOffset - EarlierOffset
860 : EarlierSize - (LaterOffset - EarlierOffset);
861
862 Value *EarlierWriteLength = EarlierIntrinsic->getLength();
863 Value *TrimmedLength =
864 ConstantInt::get(EarlierWriteLength->getType(), NewLength);
865 EarlierIntrinsic->setLength(TrimmedLength);
866
867 EarlierSize = NewLength;
868 if (!IsOverwriteEnd) {
869 int64_t OffsetMoved = (LaterOffset - EarlierOffset);
870 Value *Indices[1] = {
871 ConstantInt::get(EarlierWriteLength->getType(), OffsetMoved)};
872 GetElementPtrInst *NewDestGEP = GetElementPtrInst::CreateInBounds(
873 EarlierIntrinsic->getRawDest(), Indices, "", EarlierWrite);
874 EarlierIntrinsic->setDest(NewDestGEP);
875 EarlierOffset = EarlierOffset + OffsetMoved;
876 }
877 return true;
878}
879
880static bool tryToShortenEnd(Instruction *EarlierWrite,
881 OverlapIntervalsTy &IntervalMap,
882 int64_t &EarlierStart, int64_t &EarlierSize) {
883 if (IntervalMap.empty() || !isShortenableAtTheEnd(EarlierWrite))
884 return false;
885
886 OverlapIntervalsTy::iterator OII = --IntervalMap.end();
887 int64_t LaterStart = OII->second;
888 int64_t LaterSize = OII->first - LaterStart;
889
890 if (LaterStart > EarlierStart && LaterStart < EarlierStart + EarlierSize &&
891 LaterStart + LaterSize >= EarlierStart + EarlierSize) {
892 if (tryToShorten(EarlierWrite, EarlierStart, EarlierSize, LaterStart,
893 LaterSize, true)) {
894 IntervalMap.erase(OII);
895 return true;
896 }
897 }
898 return false;
899}
900
901static bool tryToShortenBegin(Instruction *EarlierWrite,
902 OverlapIntervalsTy &IntervalMap,
903 int64_t &EarlierStart, int64_t &EarlierSize) {
904 if (IntervalMap.empty() || !isShortenableAtTheBeginning(EarlierWrite))
905 return false;
906
907 OverlapIntervalsTy::iterator OII = IntervalMap.begin();
908 int64_t LaterStart = OII->second;
909 int64_t LaterSize = OII->first - LaterStart;
910
911 if (LaterStart <= EarlierStart && LaterStart + LaterSize > EarlierStart) {
912 assert(LaterStart + LaterSize < EarlierStart + EarlierSize &&
913 "Should have been handled as OverwriteComplete");
914 if (tryToShorten(EarlierWrite, EarlierStart, EarlierSize, LaterStart,
915 LaterSize, false)) {
916 IntervalMap.erase(OII);
917 return true;
918 }
919 }
920 return false;
921}
922
923static bool removePartiallyOverlappedStores(AliasAnalysis *AA,
924 const DataLayout &DL,
925 InstOverlapIntervalsTy &IOL) {
926 bool Changed = false;
927 for (auto OI : IOL) {
928 Instruction *EarlierWrite = OI.first;
929 MemoryLocation Loc = getLocForWrite(EarlierWrite, *AA);
930 assert(isRemovable(EarlierWrite) && "Expect only removable instruction");
931 assert(Loc.Size != MemoryLocation::UnknownSize && "Unexpected mem loc");
932
933 const Value *Ptr = Loc.Ptr->stripPointerCasts();
934 int64_t EarlierStart = 0;
935 int64_t EarlierSize = int64_t(Loc.Size);
936 GetPointerBaseWithConstantOffset(Ptr, EarlierStart, DL);
937 OverlapIntervalsTy &IntervalMap = OI.second;
Jun Bum Lima0331392016-07-27 17:25:20 +0000938 Changed |=
Jun Bum Lim6a7dc5c2016-07-22 18:27:24 +0000939 tryToShortenEnd(EarlierWrite, IntervalMap, EarlierStart, EarlierSize);
940 if (IntervalMap.empty())
941 continue;
942 Changed |=
943 tryToShortenBegin(EarlierWrite, IntervalMap, EarlierStart, EarlierSize);
944 }
945 return Changed;
946}
947
Chad Rosier89c32a92016-07-08 16:48:40 +0000948static bool eliminateNoopStore(Instruction *Inst, BasicBlock::iterator &BBI,
949 AliasAnalysis *AA, MemoryDependenceResults *MD,
950 const DataLayout &DL,
Jun Bum Lim6a7dc5c2016-07-22 18:27:24 +0000951 const TargetLibraryInfo *TLI,
Eli Friedmana6707f52016-08-12 01:09:53 +0000952 InstOverlapIntervalsTy &IOL,
953 DenseMap<Instruction*, size_t> *InstrOrdering) {
Chad Rosier89c32a92016-07-08 16:48:40 +0000954 // Must be a store instruction.
955 StoreInst *SI = dyn_cast<StoreInst>(Inst);
956 if (!SI)
957 return false;
958
959 // If we're storing the same value back to a pointer that we just loaded from,
960 // then the store can be removed.
961 if (LoadInst *DepLoad = dyn_cast<LoadInst>(SI->getValueOperand())) {
962 if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
963 isRemovable(SI) && memoryIsNotModifiedBetween(DepLoad, SI, AA)) {
964
965 DEBUG(dbgs() << "DSE: Remove Store Of Load from same pointer:\n LOAD: "
966 << *DepLoad << "\n STORE: " << *SI << '\n');
967
Eli Friedmana6707f52016-08-12 01:09:53 +0000968 deleteDeadInstruction(SI, &BBI, *MD, *TLI, IOL, InstrOrdering);
Chad Rosier89c32a92016-07-08 16:48:40 +0000969 ++NumRedundantStores;
970 return true;
971 }
972 }
973
974 // Remove null stores into the calloc'ed objects
975 Constant *StoredConstant = dyn_cast<Constant>(SI->getValueOperand());
976 if (StoredConstant && StoredConstant->isNullValue() && isRemovable(SI)) {
977 Instruction *UnderlyingPointer =
978 dyn_cast<Instruction>(GetUnderlyingObject(SI->getPointerOperand(), DL));
979
980 if (UnderlyingPointer && isCallocLikeFn(UnderlyingPointer, TLI) &&
981 memoryIsNotModifiedBetween(UnderlyingPointer, SI, AA)) {
982 DEBUG(
983 dbgs() << "DSE: Remove null store to the calloc'ed object:\n DEAD: "
984 << *Inst << "\n OBJECT: " << *UnderlyingPointer << '\n');
985
Eli Friedmana6707f52016-08-12 01:09:53 +0000986 deleteDeadInstruction(SI, &BBI, *MD, *TLI, IOL, InstrOrdering);
Chad Rosier89c32a92016-07-08 16:48:40 +0000987 ++NumRedundantStores;
988 return true;
989 }
990 }
991 return false;
992}
993
Justin Bogner594e07b2016-05-17 21:38:13 +0000994static bool eliminateDeadStores(BasicBlock &BB, AliasAnalysis *AA,
995 MemoryDependenceResults *MD, DominatorTree *DT,
996 const TargetLibraryInfo *TLI) {
Igor Laevsky029bd932015-09-23 11:38:44 +0000997 const DataLayout &DL = BB.getModule()->getDataLayout();
Owen Anderson5e72db32007-07-11 00:46:18 +0000998 bool MadeChange = false;
Owen Anderson58704ee2011-09-06 18:14:09 +0000999
Eli Friedmana6707f52016-08-12 01:09:53 +00001000 // FIXME: Maybe change this to use some abstraction like OrderedBasicBlock?
1001 // The current OrderedBasicBlock can't deal with mutation at the moment.
1002 size_t LastThrowingInstIndex = 0;
1003 DenseMap<Instruction*, size_t> InstrOrdering;
1004 size_t InstrIndex = 1;
1005
Hal Finkela1271032016-06-23 13:46:39 +00001006 // A map of interval maps representing partially-overwritten value parts.
1007 InstOverlapIntervalsTy IOL;
1008
Chris Lattner49162672009-09-02 06:31:02 +00001009 // Do a top-down walk on the BB.
Chris Lattnerf2a8ba42008-11-28 21:29:52 +00001010 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
Chris Lattner9d179d92010-11-30 01:28:33 +00001011 // Handle 'free' calls specially.
Chad Rosierdcfce2d2016-07-06 19:48:52 +00001012 if (CallInst *F = isFreeCall(&*BBI, TLI)) {
Eli Friedmana6707f52016-08-12 01:09:53 +00001013 MadeChange |= handleFree(F, AA, MD, DT, TLI, IOL, &InstrOrdering);
Chad Rosierdcfce2d2016-07-06 19:48:52 +00001014 // Increment BBI after handleFree has potentially deleted instructions.
1015 // This ensures we maintain a valid iterator.
1016 ++BBI;
Chris Lattner9d179d92010-11-30 01:28:33 +00001017 continue;
1018 }
Owen Anderson58704ee2011-09-06 18:14:09 +00001019
Chad Rosierdcfce2d2016-07-06 19:48:52 +00001020 Instruction *Inst = &*BBI++;
1021
Eli Friedmana6707f52016-08-12 01:09:53 +00001022 size_t CurInstNumber = InstrIndex++;
1023 InstrOrdering.insert(std::make_pair(Inst, CurInstNumber));
1024 if (Inst->mayThrow()) {
1025 LastThrowingInstIndex = CurInstNumber;
1026 continue;
1027 }
1028
Chad Rosier89c32a92016-07-08 16:48:40 +00001029 // Check to see if Inst writes to memory. If not, continue.
Chandler Carruthdbe40fb2015-08-12 18:01:44 +00001030 if (!hasMemoryWrite(Inst, *TLI))
Owen Anderson0aecf0e2007-08-08 04:52:29 +00001031 continue;
Chris Lattnerd4f10902010-11-30 00:01:19 +00001032
Chad Rosier89c32a92016-07-08 16:48:40 +00001033 // eliminateNoopStore will update in iterator, if necessary.
Eli Friedmana6707f52016-08-12 01:09:53 +00001034 if (eliminateNoopStore(Inst, BBI, AA, MD, DL, TLI, IOL, &InstrOrdering)) {
Chad Rosier89c32a92016-07-08 16:48:40 +00001035 MadeChange = true;
1036 continue;
Owen Anderson5e72db32007-07-11 00:46:18 +00001037 }
Owen Anderson58704ee2011-09-06 18:14:09 +00001038
Chad Rosier89c32a92016-07-08 16:48:40 +00001039 // If we find something that writes memory, get its memory dependence.
Erik Eckstein11fc8172015-08-13 15:36:11 +00001040 MemDepResult InstDep = MD->getDependency(Inst);
1041
Chad Rosierd7634fc2015-12-11 18:39:41 +00001042 // Ignore any store where we can't find a local dependence.
1043 // FIXME: cross-block DSE would be fun. :)
1044 if (!InstDep.isDef() && !InstDep.isClobber())
1045 continue;
Erik Eckstein11fc8172015-08-13 15:36:11 +00001046
Chad Rosierd7634fc2015-12-11 18:39:41 +00001047 // Figure out what location is being stored to.
1048 MemoryLocation Loc = getLocForWrite(Inst, *AA);
Chris Lattner58b779e2010-11-30 07:23:21 +00001049
Chad Rosierd7634fc2015-12-11 18:39:41 +00001050 // If we didn't get a useful location, fail.
1051 if (!Loc.Ptr)
1052 continue;
1053
1054 while (InstDep.isDef() || InstDep.isClobber()) {
1055 // Get the memory clobbered by the instruction we depend on. MemDep will
1056 // skip any instructions that 'Loc' clearly doesn't interact with. If we
1057 // end up depending on a may- or must-aliased load, then we can't optimize
Chad Rosier844e2df2016-06-15 21:41:22 +00001058 // away the store and we bail out. However, if we depend on something
Chad Rosierd7634fc2015-12-11 18:39:41 +00001059 // that overwrites the memory location we *can* potentially optimize it.
1060 //
1061 // Find out what memory location the dependent instruction stores.
1062 Instruction *DepWrite = InstDep.getInst();
1063 MemoryLocation DepLoc = getLocForWrite(DepWrite, *AA);
1064 // If we didn't get a useful location, or if it isn't a size, bail out.
1065 if (!DepLoc.Ptr)
1066 break;
1067
Eli Friedmana6707f52016-08-12 01:09:53 +00001068 // Make sure we don't look past a call which might throw. This is an
1069 // issue because MemoryDependenceAnalysis works in the wrong direction:
1070 // it finds instructions which dominate the current instruction, rather than
1071 // instructions which are post-dominated by the current instruction.
1072 //
1073 // If the underlying object is a non-escaping memory allocation, any store
1074 // to it is dead along the unwind edge. Otherwise, we need to preserve
1075 // the store.
1076 size_t DepIndex = InstrOrdering.lookup(DepWrite);
1077 assert(DepIndex && "Unexpected instruction");
1078 if (DepIndex <= LastThrowingInstIndex) {
1079 const Value* Underlying = GetUnderlyingObject(DepLoc.Ptr, DL);
1080 bool IsStoreDeadOnUnwind = isa<AllocaInst>(Underlying);
1081 if (!IsStoreDeadOnUnwind) {
1082 // We're looking for a call to an allocation function
1083 // where the allocation doesn't escape before the last
1084 // throwing instruction; PointerMayBeCaptured
1085 // reasonably fast approximation.
1086 IsStoreDeadOnUnwind = isAllocLikeFn(Underlying, TLI) &&
1087 !PointerMayBeCaptured(Underlying, false, true);
1088 }
1089 if (!IsStoreDeadOnUnwind)
1090 break;
1091 }
1092
Chad Rosierd7634fc2015-12-11 18:39:41 +00001093 // If we find a write that is a) removable (i.e., non-volatile), b) is
1094 // completely obliterated by the store to 'Loc', and c) which we know that
1095 // 'Inst' doesn't load from, then we can remove it.
1096 if (isRemovable(DepWrite) &&
1097 !isPossibleSelfRead(Inst, Loc, DepWrite, *TLI, *AA)) {
1098 int64_t InstWriteOffset, DepWriteOffset;
1099 OverwriteResult OR =
Hal Finkela1271032016-06-23 13:46:39 +00001100 isOverwrite(Loc, DepLoc, DL, *TLI, DepWriteOffset, InstWriteOffset,
1101 DepWrite, IOL);
Chad Rosierd7634fc2015-12-11 18:39:41 +00001102 if (OR == OverwriteComplete) {
1103 DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: "
1104 << *DepWrite << "\n KILLER: " << *Inst << '\n');
Alexander Kornienko63dd36f2016-07-18 15:51:31 +00001105
Chad Rosierd7634fc2015-12-11 18:39:41 +00001106 // Delete the store and now-dead instructions that feed it.
Eli Friedmana6707f52016-08-12 01:09:53 +00001107 deleteDeadInstruction(DepWrite, &BBI, *MD, *TLI, IOL, &InstrOrdering);
Chad Rosierd7634fc2015-12-11 18:39:41 +00001108 ++NumFastStores;
1109 MadeChange = true;
1110
Chad Rosierdcfce2d2016-07-06 19:48:52 +00001111 // We erased DepWrite; start over.
1112 InstDep = MD->getDependency(Inst);
1113 continue;
Jun Bum Limd29a24e2016-04-22 19:51:29 +00001114 } else if ((OR == OverwriteEnd && isShortenableAtTheEnd(DepWrite)) ||
1115 ((OR == OverwriteBegin &&
1116 isShortenableAtTheBeginning(DepWrite)))) {
Jun Bum Lim6a7dc5c2016-07-22 18:27:24 +00001117 assert(!EnablePartialOverwriteTracking && "Do not expect to perform "
1118 "when partial-overwrite "
1119 "tracking is enabled");
1120 int64_t EarlierSize = DepLoc.Size;
1121 int64_t LaterSize = Loc.Size;
Jun Bum Limd29a24e2016-04-22 19:51:29 +00001122 bool IsOverwriteEnd = (OR == OverwriteEnd);
Jun Bum Lima0331392016-07-27 17:25:20 +00001123 MadeChange |= tryToShorten(DepWrite, DepWriteOffset, EarlierSize,
Jun Bum Lim6a7dc5c2016-07-22 18:27:24 +00001124 InstWriteOffset, LaterSize, IsOverwriteEnd);
Pete Cooper856977c2011-11-09 23:07:35 +00001125 }
Chris Lattner58b779e2010-11-30 07:23:21 +00001126 }
Chad Rosierd7634fc2015-12-11 18:39:41 +00001127
1128 // If this is a may-aliased store that is clobbering the store value, we
1129 // can keep searching past it for another must-aliased pointer that stores
1130 // to the same location. For example, in:
1131 // store -> P
1132 // store -> Q
1133 // store -> P
1134 // we can remove the first store to P even though we don't know if P and Q
1135 // alias.
1136 if (DepWrite == &BB.front()) break;
1137
1138 // Can't look past this instruction if it might read 'Loc'.
1139 if (AA->getModRefInfo(DepWrite, Loc) & MRI_Ref)
1140 break;
1141
1142 InstDep = MD->getPointerDependencyFrom(Loc, false,
1143 DepWrite->getIterator(), &BB);
Owen Anderson2b2bd282009-10-28 07:05:35 +00001144 }
Owen Anderson5e72db32007-07-11 00:46:18 +00001145 }
Owen Anderson58704ee2011-09-06 18:14:09 +00001146
Jun Bum Lim6a7dc5c2016-07-22 18:27:24 +00001147 if (EnablePartialOverwriteTracking)
1148 MadeChange |= removePartiallyOverlappedStores(AA, DL, IOL);
1149
Chris Lattnerf2a8ba42008-11-28 21:29:52 +00001150 // If this block ends in a return, unwind, or unreachable, all allocas are
1151 // dead at its end, which means stores to them are also dead.
Owen Anderson32c4a052007-07-12 21:41:30 +00001152 if (BB.getTerminator()->getNumSuccessors() == 0)
Eli Friedmana6707f52016-08-12 01:09:53 +00001153 MadeChange |= handleEndBlock(BB, AA, MD, TLI, IOL, &InstrOrdering);
Owen Anderson58704ee2011-09-06 18:14:09 +00001154
Owen Anderson5e72db32007-07-11 00:46:18 +00001155 return MadeChange;
1156}
1157
Justin Bogner594e07b2016-05-17 21:38:13 +00001158static bool eliminateDeadStores(Function &F, AliasAnalysis *AA,
1159 MemoryDependenceResults *MD, DominatorTree *DT,
1160 const TargetLibraryInfo *TLI) {
Eli Friedman7d58bc72011-06-15 00:47:34 +00001161 bool MadeChange = false;
Justin Bogner594e07b2016-05-17 21:38:13 +00001162 for (BasicBlock &BB : F)
1163 // Only check non-dead blocks. Dead blocks may have strange pointer
1164 // cycles that will confuse alias analysis.
1165 if (DT->isReachableFromEntry(&BB))
1166 MadeChange |= eliminateDeadStores(BB, AA, MD, DT, TLI);
Eli Friedmana6707f52016-08-12 01:09:53 +00001167
Eli Friedman7d58bc72011-06-15 00:47:34 +00001168 return MadeChange;
Owen Andersonaa071722007-07-11 23:19:17 +00001169}
1170
Justin Bogner594e07b2016-05-17 21:38:13 +00001171//===----------------------------------------------------------------------===//
1172// DSE Pass
1173//===----------------------------------------------------------------------===//
1174PreservedAnalyses DSEPass::run(Function &F, FunctionAnalysisManager &AM) {
1175 AliasAnalysis *AA = &AM.getResult<AAManager>(F);
1176 DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F);
1177 MemoryDependenceResults *MD = &AM.getResult<MemoryDependenceAnalysis>(F);
1178 const TargetLibraryInfo *TLI = &AM.getResult<TargetLibraryAnalysis>(F);
Owen Anderson58704ee2011-09-06 18:14:09 +00001179
Justin Bogner594e07b2016-05-17 21:38:13 +00001180 if (!eliminateDeadStores(F, AA, MD, DT, TLI))
1181 return PreservedAnalyses::all();
1182 PreservedAnalyses PA;
1183 PA.preserve<DominatorTreeAnalysis>();
1184 PA.preserve<GlobalsAA>();
1185 PA.preserve<MemoryDependenceAnalysis>();
1186 return PA;
Owen Anderson32c4a052007-07-12 21:41:30 +00001187}
1188
Benjamin Kramer4d098922016-07-10 11:28:51 +00001189namespace {
Justin Bogner594e07b2016-05-17 21:38:13 +00001190/// A legacy pass for the legacy pass manager that wraps \c DSEPass.
1191class DSELegacyPass : public FunctionPass {
1192public:
1193 DSELegacyPass() : FunctionPass(ID) {
1194 initializeDSELegacyPassPass(*PassRegistry::getPassRegistry());
Owen Andersonddf4aee2007-08-08 18:38:28 +00001195 }
Owen Anderson58704ee2011-09-06 18:14:09 +00001196
Justin Bogner594e07b2016-05-17 21:38:13 +00001197 bool runOnFunction(Function &F) override {
1198 if (skipFunction(F))
1199 return false;
1200
1201 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1202 AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
1203 MemoryDependenceResults *MD =
1204 &getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
1205 const TargetLibraryInfo *TLI =
1206 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1207
1208 return eliminateDeadStores(F, AA, MD, DT, TLI);
1209 }
1210
1211 void getAnalysisUsage(AnalysisUsage &AU) const override {
1212 AU.setPreservesCFG();
1213 AU.addRequired<DominatorTreeWrapperPass>();
1214 AU.addRequired<AAResultsWrapperPass>();
1215 AU.addRequired<MemoryDependenceWrapperPass>();
1216 AU.addRequired<TargetLibraryInfoWrapperPass>();
1217 AU.addPreserved<DominatorTreeWrapperPass>();
1218 AU.addPreserved<GlobalsAAWrapperPass>();
1219 AU.addPreserved<MemoryDependenceWrapperPass>();
1220 }
1221
1222 static char ID; // Pass identification, replacement for typeid
1223};
Benjamin Kramer4d098922016-07-10 11:28:51 +00001224} // end anonymous namespace
Justin Bogner594e07b2016-05-17 21:38:13 +00001225
1226char DSELegacyPass::ID = 0;
1227INITIALIZE_PASS_BEGIN(DSELegacyPass, "dse", "Dead Store Elimination", false,
1228 false)
1229INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1230INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
1231INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
1232INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
1233INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1234INITIALIZE_PASS_END(DSELegacyPass, "dse", "Dead Store Elimination", false,
1235 false)
1236
1237FunctionPass *llvm::createDeadStoreEliminationPass() {
1238 return new DSELegacyPass();
Owen Anderson32c4a052007-07-12 21:41:30 +00001239}