1 //===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a trivial dead store elimination that only considers
10 // basic-block local redundant stores.
11 //
12 // FIXME: This should eventually be extended to be a post-dominator tree
13 // traversal.  Doing so would be pretty trivial.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/Transforms/Scalar/DeadStoreElimination.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/PostOrderIterator.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/CaptureTracking.h"
29 #include "llvm/Analysis/GlobalsModRef.h"
30 #include "llvm/Analysis/MemoryBuiltins.h"
31 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
32 #include "llvm/Analysis/MemoryLocation.h"
33 #include "llvm/Analysis/MemorySSA.h"
34 #include "llvm/Analysis/MemorySSAUpdater.h"
35 #include "llvm/Analysis/PostDominators.h"
36 #include "llvm/Analysis/TargetLibraryInfo.h"
37 #include "llvm/Analysis/ValueTracking.h"
38 #include "llvm/IR/Argument.h"
39 #include "llvm/IR/BasicBlock.h"
40 #include "llvm/IR/Constant.h"
41 #include "llvm/IR/Constants.h"
42 #include "llvm/IR/DataLayout.h"
43 #include "llvm/IR/Dominators.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/InstIterator.h"
46 #include "llvm/IR/InstrTypes.h"
47 #include "llvm/IR/Instruction.h"
48 #include "llvm/IR/Instructions.h"
49 #include "llvm/IR/IntrinsicInst.h"
50 #include "llvm/IR/Intrinsics.h"
51 #include "llvm/IR/LLVMContext.h"
52 #include "llvm/IR/Module.h"
53 #include "llvm/IR/PassManager.h"
54 #include "llvm/IR/PatternMatch.h"
55 #include "llvm/IR/Value.h"
56 #include "llvm/InitializePasses.h"
57 #include "llvm/Pass.h"
58 #include "llvm/Support/Casting.h"
59 #include "llvm/Support/CommandLine.h"
60 #include "llvm/Support/Debug.h"
61 #include "llvm/Support/DebugCounter.h"
62 #include "llvm/Support/ErrorHandling.h"
63 #include "llvm/Support/MathExtras.h"
64 #include "llvm/Support/raw_ostream.h"
65 #include "llvm/Transforms/Scalar.h"
66 #include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
67 #include "llvm/Transforms/Utils/Local.h"
68 #include <algorithm>
69 #include <cassert>
70 #include <cstddef>
71 #include <cstdint>
72 #include <iterator>
73 #include <map>
74 #include <utility>
75 
76 using namespace llvm;
77 using namespace PatternMatch;
78 
79 #define DEBUG_TYPE "dse"
80 
81 STATISTIC(NumRemainingStores, "Number of stores remaining after DSE");
82 STATISTIC(NumRedundantStores, "Number of redundant stores deleted");
83 STATISTIC(NumFastStores, "Number of stores deleted");
84 STATISTIC(NumFastOther, "Number of other instrs removed");
85 STATISTIC(NumCompletePartials, "Number of stores dead by later partials");
86 STATISTIC(NumModifiedStores, "Number of stores modified");
87 STATISTIC(NumCFGChecks, "Number of stores modified");
88 STATISTIC(NumCFGTries, "Number of stores modified");
89 STATISTIC(NumCFGSuccess, "Number of stores modified");
90 STATISTIC(NumGetDomMemoryDefPassed,
91           "Number of times a valid candidate is returned from getDomMemoryDef");
92 STATISTIC(NumDomMemDefChecks,
93           "Number iterations check for reads in getDomMemoryDef");
94 
95 DEBUG_COUNTER(MemorySSACounter, "dse-memoryssa",
96               "Controls which MemoryDefs are eliminated.");
97 
98 static cl::opt<bool>
99 EnablePartialOverwriteTracking("enable-dse-partial-overwrite-tracking",
100   cl::init(true), cl::Hidden,
101   cl::desc("Enable partial-overwrite tracking in DSE"));
102 
103 static cl::opt<bool>
104 EnablePartialStoreMerging("enable-dse-partial-store-merging",
105   cl::init(true), cl::Hidden,
106   cl::desc("Enable partial store merging in DSE"));
107 
108 static cl::opt<bool>
109     EnableMemorySSA("enable-dse-memoryssa", cl::init(true), cl::Hidden,
110                     cl::desc("Use the new MemorySSA-backed DSE."));
111 
112 static cl::opt<unsigned>
113     MemorySSAScanLimit("dse-memoryssa-scanlimit", cl::init(150), cl::Hidden,
114                        cl::desc("The number of memory instructions to scan for "
115                                 "dead store elimination (default = 100)"));
116 static cl::opt<unsigned> MemorySSAUpwardsStepLimit(
117     "dse-memoryssa-walklimit", cl::init(90), cl::Hidden,
118     cl::desc("The maximum number of steps while walking upwards to find "
119              "MemoryDefs that may be killed (default = 90)"));
120 
121 static cl::opt<unsigned> MemorySSAPartialStoreLimit(
122     "dse-memoryssa-partial-store-limit", cl::init(5), cl::Hidden,
123     cl::desc("The maximum number candidates that only partially overwrite the "
124              "killing MemoryDef to consider"
125              " (default = 5)"));
126 
127 static cl::opt<unsigned> MemorySSADefsPerBlockLimit(
128     "dse-memoryssa-defs-per-block-limit", cl::init(5000), cl::Hidden,
129     cl::desc("The number of MemoryDefs we consider as candidates to eliminated "
130              "other stores per basic block (default = 5000)"));
131 
132 static cl::opt<unsigned> MemorySSASameBBStepCost(
133     "dse-memoryssa-samebb-cost", cl::init(1), cl::Hidden,
134     cl::desc(
135         "The cost of a step in the same basic block as the killing MemoryDef"
136         "(default = 1)"));
137 
138 static cl::opt<unsigned>
139     MemorySSAOtherBBStepCost("dse-memoryssa-otherbb-cost", cl::init(5),
140                              cl::Hidden,
141                              cl::desc("The cost of a step in a different basic "
142                                       "block than the killing MemoryDef"
143                                       "(default = 5)"));
144 
145 static cl::opt<unsigned> MemorySSAPathCheckLimit(
146     "dse-memoryssa-path-check-limit", cl::init(50), cl::Hidden,
147     cl::desc("The maximum number of blocks to check when trying to prove that "
148              "all paths to an exit go through a killing block (default = 50)"));
149 
150 //===----------------------------------------------------------------------===//
151 // Helper functions
152 //===----------------------------------------------------------------------===//
153 using OverlapIntervalsTy = std::map<int64_t, int64_t>;
154 using InstOverlapIntervalsTy = DenseMap<Instruction *, OverlapIntervalsTy>;
155 
156 /// Delete this instruction.  Before we do, go through and zero out all the
157 /// operands of this instruction.  If any of them become dead, delete them and
158 /// the computation tree that feeds them.
159 /// If ValueSet is non-null, remove any deleted instructions from it as well.
160 static void
161 deleteDeadInstruction(Instruction *I, BasicBlock::iterator *BBI,
162                       MemoryDependenceResults &MD, const TargetLibraryInfo &TLI,
163                       InstOverlapIntervalsTy &IOL,
164                       MapVector<Instruction *, bool> &ThrowableInst,
165                       SmallSetVector<const Value *, 16> *ValueSet = nullptr) {
166   SmallVector<Instruction*, 32> NowDeadInsts;
167 
168   NowDeadInsts.push_back(I);
169   --NumFastOther;
170 
171   // Keeping the iterator straight is a pain, so we let this routine tell the
172   // caller what the next instruction is after we're done mucking about.
173   BasicBlock::iterator NewIter = *BBI;
174 
175   // Before we touch this instruction, remove it from memdep!
176   do {
177     Instruction *DeadInst = NowDeadInsts.pop_back_val();
178     // Mark the DeadInst as dead in the list of throwable instructions.
179     auto It = ThrowableInst.find(DeadInst);
180     if (It != ThrowableInst.end())
181       ThrowableInst[It->first] = false;
182     ++NumFastOther;
183 
184     // Try to preserve debug information attached to the dead instruction.
185     salvageDebugInfo(*DeadInst);
186     salvageKnowledge(DeadInst);
187 
188     // This instruction is dead, zap it, in stages.  Start by removing it from
189     // MemDep, which needs to know the operands and needs it to be in the
190     // function.
191     MD.removeInstruction(DeadInst);
192 
193     for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
194       Value *Op = DeadInst->getOperand(op);
195       DeadInst->setOperand(op, nullptr);
196 
197       // If this operand just became dead, add it to the NowDeadInsts list.
198       if (!Op->use_empty()) continue;
199 
200       if (Instruction *OpI = dyn_cast<Instruction>(Op))
201         if (isInstructionTriviallyDead(OpI, &TLI))
202           NowDeadInsts.push_back(OpI);
203     }
204 
205     if (ValueSet) ValueSet->remove(DeadInst);
206     IOL.erase(DeadInst);
207 
208     if (NewIter == DeadInst->getIterator())
209       NewIter = DeadInst->eraseFromParent();
210     else
211       DeadInst->eraseFromParent();
212   } while (!NowDeadInsts.empty());
213   *BBI = NewIter;
214   // Pop dead entries from back of ThrowableInst till we find an alive entry.
215   while (!ThrowableInst.empty() && !ThrowableInst.back().second)
216     ThrowableInst.pop_back();
217 }
218 
219 /// Does this instruction write some memory?  This only returns true for things
220 /// that we can analyze with other helpers below.
221 static bool hasAnalyzableMemoryWrite(Instruction *I,
222                                      const TargetLibraryInfo &TLI) {
223   if (isa<StoreInst>(I))
224     return true;
225   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
226     switch (II->getIntrinsicID()) {
227     default:
228       return false;
229     case Intrinsic::memset:
230     case Intrinsic::memmove:
231     case Intrinsic::memcpy:
232     case Intrinsic::memcpy_inline:
233     case Intrinsic::memcpy_element_unordered_atomic:
234     case Intrinsic::memmove_element_unordered_atomic:
235     case Intrinsic::memset_element_unordered_atomic:
236     case Intrinsic::init_trampoline:
237     case Intrinsic::lifetime_end:
238     case Intrinsic::masked_store:
239       return true;
240     }
241   }
242   if (auto *CB = dyn_cast<CallBase>(I)) {
243     LibFunc LF;
244     if (TLI.getLibFunc(*CB, LF) && TLI.has(LF)) {
245       switch (LF) {
246       case LibFunc_strcpy:
247       case LibFunc_strncpy:
248       case LibFunc_strcat:
249       case LibFunc_strncat:
250         return true;
251       default:
252         return false;
253       }
254     }
255   }
256   return false;
257 }
258 
259 /// Return a Location stored to by the specified instruction. If isRemovable
260 /// returns true, this function and getLocForRead completely describe the memory
261 /// operations for this instruction.
262 static MemoryLocation getLocForWrite(Instruction *Inst,
263                                      const TargetLibraryInfo &TLI) {
264   if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
265     return MemoryLocation::get(SI);
266 
267   // memcpy/memmove/memset.
268   if (auto *MI = dyn_cast<AnyMemIntrinsic>(Inst))
269     return MemoryLocation::getForDest(MI);
270 
271   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
272     switch (II->getIntrinsicID()) {
273     default:
274       return MemoryLocation(); // Unhandled intrinsic.
275     case Intrinsic::init_trampoline:
276       return MemoryLocation::getAfter(II->getArgOperand(0));
277     case Intrinsic::masked_store:
278       return MemoryLocation::getForArgument(II, 1, TLI);
279     case Intrinsic::lifetime_end: {
280       uint64_t Len = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue();
281       return MemoryLocation(II->getArgOperand(1), Len);
282     }
283     }
284   }
285   if (auto *CB = dyn_cast<CallBase>(Inst))
286     // All the supported TLI functions so far happen to have dest as their
287     // first argument.
288     return MemoryLocation::getAfter(CB->getArgOperand(0));
289   return MemoryLocation();
290 }
291 
292 /// Return the location read by the specified "hasAnalyzableMemoryWrite"
293 /// instruction if any.
294 static MemoryLocation getLocForRead(Instruction *Inst,
295                                     const TargetLibraryInfo &TLI) {
296   assert(hasAnalyzableMemoryWrite(Inst, TLI) && "Unknown instruction case");
297 
298   // The only instructions that both read and write are the mem transfer
299   // instructions (memcpy/memmove).
300   if (auto *MTI = dyn_cast<AnyMemTransferInst>(Inst))
301     return MemoryLocation::getForSource(MTI);
302   return MemoryLocation();
303 }
304 
305 /// If the value of this instruction and the memory it writes to is unused, may
306 /// we delete this instruction?
307 static bool isRemovable(Instruction *I) {
308   // Don't remove volatile/atomic stores.
309   if (StoreInst *SI = dyn_cast<StoreInst>(I))
310     return SI->isUnordered();
311 
312   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
313     switch (II->getIntrinsicID()) {
314     default: llvm_unreachable("doesn't pass 'hasAnalyzableMemoryWrite' predicate");
315     case Intrinsic::lifetime_end:
316       // Never remove dead lifetime_end's, e.g. because it is followed by a
317       // free.
318       return false;
319     case Intrinsic::init_trampoline:
320       // Always safe to remove init_trampoline.
321       return true;
322     case Intrinsic::memset:
323     case Intrinsic::memmove:
324     case Intrinsic::memcpy:
325     case Intrinsic::memcpy_inline:
326       // Don't remove volatile memory intrinsics.
327       return !cast<MemIntrinsic>(II)->isVolatile();
328     case Intrinsic::memcpy_element_unordered_atomic:
329     case Intrinsic::memmove_element_unordered_atomic:
330     case Intrinsic::memset_element_unordered_atomic:
331     case Intrinsic::masked_store:
332       return true;
333     }
334   }
335 
336   // note: only get here for calls with analyzable writes - i.e. libcalls
337   if (auto *CB = dyn_cast<CallBase>(I))
338     return CB->use_empty();
339 
340   return false;
341 }
342 
343 /// Returns true if the end of this instruction can be safely shortened in
344 /// length.
345 static bool isShortenableAtTheEnd(Instruction *I) {
346   // Don't shorten stores for now
347   if (isa<StoreInst>(I))
348     return false;
349 
350   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
351     switch (II->getIntrinsicID()) {
352       default: return false;
353       case Intrinsic::memset:
354       case Intrinsic::memcpy:
355       case Intrinsic::memcpy_element_unordered_atomic:
356       case Intrinsic::memset_element_unordered_atomic:
357         // Do shorten memory intrinsics.
358         // FIXME: Add memmove if it's also safe to transform.
359         return true;
360     }
361   }
362 
363   // Don't shorten libcalls calls for now.
364 
365   return false;
366 }
367 
368 /// Returns true if the beginning of this instruction can be safely shortened
369 /// in length.
370 static bool isShortenableAtTheBeginning(Instruction *I) {
371   // FIXME: Handle only memset for now. Supporting memcpy/memmove should be
372   // easily done by offsetting the source address.
373   return isa<AnyMemSetInst>(I);
374 }
375 
376 /// Return the pointer that is being written to.
377 static Value *getStoredPointerOperand(Instruction *I,
378                                       const TargetLibraryInfo &TLI) {
379   //TODO: factor this to reuse getLocForWrite
380   MemoryLocation Loc = getLocForWrite(I, TLI);
381   assert(Loc.Ptr &&
382          "unable to find pointer written for analyzable instruction?");
383   // TODO: most APIs don't expect const Value *
384   return const_cast<Value*>(Loc.Ptr);
385 }
386 
387 static uint64_t getPointerSize(const Value *V, const DataLayout &DL,
388                                const TargetLibraryInfo &TLI,
389                                const Function *F) {
390   uint64_t Size;
391   ObjectSizeOpts Opts;
392   Opts.NullIsUnknownSize = NullPointerIsDefined(F);
393 
394   if (getObjectSize(V, Size, DL, &TLI, Opts))
395     return Size;
396   return MemoryLocation::UnknownSize;
397 }
398 
399 namespace {
400 
401 enum OverwriteResult {
402   OW_Begin,
403   OW_Complete,
404   OW_End,
405   OW_PartialEarlierWithFullLater,
406   OW_MaybePartial,
407   OW_Unknown
408 };
409 
410 } // end anonymous namespace
411 
412 /// Check if two instruction are masked stores that completely
413 /// overwrite one another. More specifically, \p Later has to
414 /// overwrite \p Earlier.
415 template <typename AATy>
416 static OverwriteResult isMaskedStoreOverwrite(const Instruction *Later,
417                                               const Instruction *Earlier,
418                                               AATy &AA) {
419   const auto *IIL = dyn_cast<IntrinsicInst>(Later);
420   const auto *IIE = dyn_cast<IntrinsicInst>(Earlier);
421   if (IIL == nullptr || IIE == nullptr)
422     return OW_Unknown;
423   if (IIL->getIntrinsicID() != Intrinsic::masked_store ||
424       IIE->getIntrinsicID() != Intrinsic::masked_store)
425     return OW_Unknown;
426   // Pointers.
427   Value *LP = IIL->getArgOperand(1)->stripPointerCasts();
428   Value *EP = IIE->getArgOperand(1)->stripPointerCasts();
429   if (LP != EP && !AA.isMustAlias(LP, EP))
430     return OW_Unknown;
431   // Masks.
432   // TODO: check that Later's mask is a superset of the Earlier's mask.
433   if (IIL->getArgOperand(3) != IIE->getArgOperand(3))
434     return OW_Unknown;
435   return OW_Complete;
436 }
437 
438 /// Return 'OW_Complete' if a store to the 'Later' location (by \p LaterI
439 /// instruction) completely overwrites a store to the 'Earlier' location.
440 /// (by \p EarlierI instruction).
441 /// Return OW_MaybePartial if \p Later does not completely overwrite
442 /// \p Earlier, but they both write to the same underlying object. In that
443 /// case, use isPartialOverwrite to check if \p Later partially overwrites
444 /// \p Earlier. Returns 'OW_Unknown' if nothing can be determined.
445 template <typename AATy>
446 static OverwriteResult
447 isOverwrite(const Instruction *LaterI, const Instruction *EarlierI,
448             const MemoryLocation &Later, const MemoryLocation &Earlier,
449             const DataLayout &DL, const TargetLibraryInfo &TLI,
450             int64_t &EarlierOff, int64_t &LaterOff, AATy &AA,
451             const Function *F) {
452   // FIXME: Vet that this works for size upper-bounds. Seems unlikely that we'll
453   // get imprecise values here, though (except for unknown sizes).
454   if (!Later.Size.isPrecise() || !Earlier.Size.isPrecise()) {
455     // Masked stores have imprecise locations, but we can reason about them
456     // to some extent.
457     return isMaskedStoreOverwrite(LaterI, EarlierI, AA);
458   }
459 
460   const uint64_t LaterSize = Later.Size.getValue();
461   const uint64_t EarlierSize = Earlier.Size.getValue();
462 
463   const Value *P1 = Earlier.Ptr->stripPointerCasts();
464   const Value *P2 = Later.Ptr->stripPointerCasts();
465 
466   // If the start pointers are the same, we just have to compare sizes to see if
467   // the later store was larger than the earlier store.
468   if (P1 == P2 || AA.isMustAlias(P1, P2)) {
469     // Make sure that the Later size is >= the Earlier size.
470     if (LaterSize >= EarlierSize)
471       return OW_Complete;
472   }
473 
474   // Check to see if the later store is to the entire object (either a global,
475   // an alloca, or a byval/inalloca argument).  If so, then it clearly
476   // overwrites any other store to the same object.
477   const Value *UO1 = getUnderlyingObject(P1), *UO2 = getUnderlyingObject(P2);
478 
479   // If we can't resolve the same pointers to the same object, then we can't
480   // analyze them at all.
481   if (UO1 != UO2)
482     return OW_Unknown;
483 
484   // If the "Later" store is to a recognizable object, get its size.
485   uint64_t ObjectSize = getPointerSize(UO2, DL, TLI, F);
486   if (ObjectSize != MemoryLocation::UnknownSize)
487     if (ObjectSize == LaterSize && ObjectSize >= EarlierSize)
488       return OW_Complete;
489 
490   // Okay, we have stores to two completely different pointers.  Try to
491   // decompose the pointer into a "base + constant_offset" form.  If the base
492   // pointers are equal, then we can reason about the two stores.
493   EarlierOff = 0;
494   LaterOff = 0;
495   const Value *BP1 = GetPointerBaseWithConstantOffset(P1, EarlierOff, DL);
496   const Value *BP2 = GetPointerBaseWithConstantOffset(P2, LaterOff, DL);
497 
498   // If the base pointers still differ, we have two completely different stores.
499   if (BP1 != BP2)
500     return OW_Unknown;
501 
502   // The later access completely overlaps the earlier store if and only if
503   // both start and end of the earlier one is "inside" the later one:
504   //    |<->|--earlier--|<->|
505   //    |-------later-------|
506   // Accesses may overlap if and only if start of one of them is "inside"
507   // another one:
508   //    |<->|--earlier--|<----->|
509   //    |-------later-------|
510   //           OR
511   //    |----- earlier -----|
512   //    |<->|---later---|<----->|
513   //
514   // We have to be careful here as *Off is signed while *.Size is unsigned.
515 
516   // Check if the earlier access starts "not before" the later one.
517   if (EarlierOff >= LaterOff) {
518     // If the earlier access ends "not after" the later access then the earlier
519     // one is completely overwritten by the later one.
520     if (uint64_t(EarlierOff - LaterOff) + EarlierSize <= LaterSize)
521       return OW_Complete;
522     // If start of the earlier access is "before" end of the later access then
523     // accesses overlap.
524     else if ((uint64_t)(EarlierOff - LaterOff) < LaterSize)
525       return OW_MaybePartial;
526   }
527   // If start of the later access is "before" end of the earlier access then
528   // accesses overlap.
529   else if ((uint64_t)(LaterOff - EarlierOff) < EarlierSize) {
530     return OW_MaybePartial;
531   }
532 
533   // Can reach here only if accesses are known not to overlap. There is no
534   // dedicated code to indicate no overlap so signal "unknown".
535   return OW_Unknown;
536 }
537 
538 /// Return 'OW_Complete' if a store to the 'Later' location completely
539 /// overwrites a store to the 'Earlier' location, 'OW_End' if the end of the
540 /// 'Earlier' location is completely overwritten by 'Later', 'OW_Begin' if the
541 /// beginning of the 'Earlier' location is overwritten by 'Later'.
542 /// 'OW_PartialEarlierWithFullLater' means that an earlier (big) store was
543 /// overwritten by a latter (smaller) store which doesn't write outside the big
544 /// store's memory locations. Returns 'OW_Unknown' if nothing can be determined.
545 /// NOTE: This function must only be called if both \p Later and \p Earlier
546 /// write to the same underlying object with valid \p EarlierOff and \p
547 /// LaterOff.
548 static OverwriteResult isPartialOverwrite(const MemoryLocation &Later,
549                                           const MemoryLocation &Earlier,
550                                           int64_t EarlierOff, int64_t LaterOff,
551                                           Instruction *DepWrite,
552                                           InstOverlapIntervalsTy &IOL) {
553   const uint64_t LaterSize = Later.Size.getValue();
554   const uint64_t EarlierSize = Earlier.Size.getValue();
555   // We may now overlap, although the overlap is not complete. There might also
556   // be other incomplete overlaps, and together, they might cover the complete
557   // earlier write.
558   // Note: The correctness of this logic depends on the fact that this function
559   // is not even called providing DepWrite when there are any intervening reads.
560   if (EnablePartialOverwriteTracking &&
561       LaterOff < int64_t(EarlierOff + EarlierSize) &&
562       int64_t(LaterOff + LaterSize) >= EarlierOff) {
563 
564     // Insert our part of the overlap into the map.
565     auto &IM = IOL[DepWrite];
566     LLVM_DEBUG(dbgs() << "DSE: Partial overwrite: Earlier [" << EarlierOff
567                       << ", " << int64_t(EarlierOff + EarlierSize)
568                       << ") Later [" << LaterOff << ", "
569                       << int64_t(LaterOff + LaterSize) << ")\n");
570 
571     // Make sure that we only insert non-overlapping intervals and combine
572     // adjacent intervals. The intervals are stored in the map with the ending
573     // offset as the key (in the half-open sense) and the starting offset as
574     // the value.
575     int64_t LaterIntStart = LaterOff, LaterIntEnd = LaterOff + LaterSize;
576 
577     // Find any intervals ending at, or after, LaterIntStart which start
578     // before LaterIntEnd.
579     auto ILI = IM.lower_bound(LaterIntStart);
580     if (ILI != IM.end() && ILI->second <= LaterIntEnd) {
581       // This existing interval is overlapped with the current store somewhere
582       // in [LaterIntStart, LaterIntEnd]. Merge them by erasing the existing
583       // intervals and adjusting our start and end.
584       LaterIntStart = std::min(LaterIntStart, ILI->second);
585       LaterIntEnd = std::max(LaterIntEnd, ILI->first);
586       ILI = IM.erase(ILI);
587 
588       // Continue erasing and adjusting our end in case other previous
589       // intervals are also overlapped with the current store.
590       //
591       // |--- ealier 1 ---|  |--- ealier 2 ---|
592       //     |------- later---------|
593       //
594       while (ILI != IM.end() && ILI->second <= LaterIntEnd) {
595         assert(ILI->second > LaterIntStart && "Unexpected interval");
596         LaterIntEnd = std::max(LaterIntEnd, ILI->first);
597         ILI = IM.erase(ILI);
598       }
599     }
600 
601     IM[LaterIntEnd] = LaterIntStart;
602 
603     ILI = IM.begin();
604     if (ILI->second <= EarlierOff &&
605         ILI->first >= int64_t(EarlierOff + EarlierSize)) {
606       LLVM_DEBUG(dbgs() << "DSE: Full overwrite from partials: Earlier ["
607                         << EarlierOff << ", "
608                         << int64_t(EarlierOff + EarlierSize)
609                         << ") Composite Later [" << ILI->second << ", "
610                         << ILI->first << ")\n");
611       ++NumCompletePartials;
612       return OW_Complete;
613     }
614   }
615 
616   // Check for an earlier store which writes to all the memory locations that
617   // the later store writes to.
618   if (EnablePartialStoreMerging && LaterOff >= EarlierOff &&
619       int64_t(EarlierOff + EarlierSize) > LaterOff &&
620       uint64_t(LaterOff - EarlierOff) + LaterSize <= EarlierSize) {
621     LLVM_DEBUG(dbgs() << "DSE: Partial overwrite an earlier load ["
622                       << EarlierOff << ", "
623                       << int64_t(EarlierOff + EarlierSize)
624                       << ") by a later store [" << LaterOff << ", "
625                       << int64_t(LaterOff + LaterSize) << ")\n");
626     // TODO: Maybe come up with a better name?
627     return OW_PartialEarlierWithFullLater;
628   }
629 
630   // Another interesting case is if the later store overwrites the end of the
631   // earlier store.
632   //
633   //      |--earlier--|
634   //                |--   later   --|
635   //
636   // In this case we may want to trim the size of earlier to avoid generating
637   // writes to addresses which will definitely be overwritten later
638   if (!EnablePartialOverwriteTracking &&
639       (LaterOff > EarlierOff && LaterOff < int64_t(EarlierOff + EarlierSize) &&
640        int64_t(LaterOff + LaterSize) >= int64_t(EarlierOff + EarlierSize)))
641     return OW_End;
642 
643   // Finally, we also need to check if the later store overwrites the beginning
644   // of the earlier store.
645   //
646   //                |--earlier--|
647   //      |--   later   --|
648   //
649   // In this case we may want to move the destination address and trim the size
650   // of earlier to avoid generating writes to addresses which will definitely
651   // be overwritten later.
652   if (!EnablePartialOverwriteTracking &&
653       (LaterOff <= EarlierOff && int64_t(LaterOff + LaterSize) > EarlierOff)) {
654     assert(int64_t(LaterOff + LaterSize) < int64_t(EarlierOff + EarlierSize) &&
655            "Expect to be handled as OW_Complete");
656     return OW_Begin;
657   }
658   // Otherwise, they don't completely overlap.
659   return OW_Unknown;
660 }
661 
662 /// If 'Inst' might be a self read (i.e. a noop copy of a
663 /// memory region into an identical pointer) then it doesn't actually make its
664 /// input dead in the traditional sense.  Consider this case:
665 ///
666 ///   memmove(A <- B)
667 ///   memmove(A <- A)
668 ///
669 /// In this case, the second store to A does not make the first store to A dead.
670 /// The usual situation isn't an explicit A<-A store like this (which can be
671 /// trivially removed) but a case where two pointers may alias.
672 ///
673 /// This function detects when it is unsafe to remove a dependent instruction
674 /// because the DSE inducing instruction may be a self-read.
675 static bool isPossibleSelfRead(Instruction *Inst,
676                                const MemoryLocation &InstStoreLoc,
677                                Instruction *DepWrite,
678                                const TargetLibraryInfo &TLI,
679                                AliasAnalysis &AA) {
680   // Self reads can only happen for instructions that read memory.  Get the
681   // location read.
682   MemoryLocation InstReadLoc = getLocForRead(Inst, TLI);
683   if (!InstReadLoc.Ptr)
684     return false; // Not a reading instruction.
685 
686   // If the read and written loc obviously don't alias, it isn't a read.
687   if (AA.isNoAlias(InstReadLoc, InstStoreLoc))
688     return false;
689 
690   if (isa<AnyMemCpyInst>(Inst)) {
691     // LLVM's memcpy overlap semantics are not fully fleshed out (see PR11763)
692     // but in practice memcpy(A <- B) either means that A and B are disjoint or
693     // are equal (i.e. there are not partial overlaps).  Given that, if we have:
694     //
695     //   memcpy/memmove(A <- B)  // DepWrite
696     //   memcpy(A <- B)  // Inst
697     //
698     // with Inst reading/writing a >= size than DepWrite, we can reason as
699     // follows:
700     //
701     //   - If A == B then both the copies are no-ops, so the DepWrite can be
702     //     removed.
703     //   - If A != B then A and B are disjoint locations in Inst.  Since
704     //     Inst.size >= DepWrite.size A and B are disjoint in DepWrite too.
705     //     Therefore DepWrite can be removed.
706     MemoryLocation DepReadLoc = getLocForRead(DepWrite, TLI);
707 
708     if (DepReadLoc.Ptr && AA.isMustAlias(InstReadLoc.Ptr, DepReadLoc.Ptr))
709       return false;
710   }
711 
712   // If DepWrite doesn't read memory or if we can't prove it is a must alias,
713   // then it can't be considered dead.
714   return true;
715 }
716 
717 /// Returns true if the memory which is accessed by the second instruction is not
718 /// modified between the first and the second instruction.
719 /// Precondition: Second instruction must be dominated by the first
720 /// instruction.
721 template <typename AATy>
722 static bool
723 memoryIsNotModifiedBetween(Instruction *FirstI, Instruction *SecondI, AATy &AA,
724                            const DataLayout &DL, DominatorTree *DT) {
725   // Do a backwards scan through the CFG from SecondI to FirstI. Look for
726   // instructions which can modify the memory location accessed by SecondI.
727   //
728   // While doing the walk keep track of the address to check. It might be
729   // different in different basic blocks due to PHI translation.
730   using BlockAddressPair = std::pair<BasicBlock *, PHITransAddr>;
731   SmallVector<BlockAddressPair, 16> WorkList;
732   // Keep track of the address we visited each block with. Bail out if we
733   // visit a block with different addresses.
734   DenseMap<BasicBlock *, Value *> Visited;
735 
736   BasicBlock::iterator FirstBBI(FirstI);
737   ++FirstBBI;
738   BasicBlock::iterator SecondBBI(SecondI);
739   BasicBlock *FirstBB = FirstI->getParent();
740   BasicBlock *SecondBB = SecondI->getParent();
741   MemoryLocation MemLoc = MemoryLocation::get(SecondI);
742   auto *MemLocPtr = const_cast<Value *>(MemLoc.Ptr);
743 
744   // Start checking the SecondBB.
745   WorkList.push_back(
746       std::make_pair(SecondBB, PHITransAddr(MemLocPtr, DL, nullptr)));
747   bool isFirstBlock = true;
748 
749   // Check all blocks going backward until we reach the FirstBB.
750   while (!WorkList.empty()) {
751     BlockAddressPair Current = WorkList.pop_back_val();
752     BasicBlock *B = Current.first;
753     PHITransAddr &Addr = Current.second;
754     Value *Ptr = Addr.getAddr();
755 
756     // Ignore instructions before FirstI if this is the FirstBB.
757     BasicBlock::iterator BI = (B == FirstBB ? FirstBBI : B->begin());
758 
759     BasicBlock::iterator EI;
760     if (isFirstBlock) {
761       // Ignore instructions after SecondI if this is the first visit of SecondBB.
762       assert(B == SecondBB && "first block is not the store block");
763       EI = SecondBBI;
764       isFirstBlock = false;
765     } else {
766       // It's not SecondBB or (in case of a loop) the second visit of SecondBB.
767       // In this case we also have to look at instructions after SecondI.
768       EI = B->end();
769     }
770     for (; BI != EI; ++BI) {
771       Instruction *I = &*BI;
772       if (I->mayWriteToMemory() && I != SecondI)
773         if (isModSet(AA.getModRefInfo(I, MemLoc.getWithNewPtr(Ptr))))
774           return false;
775     }
776     if (B != FirstBB) {
777       assert(B != &FirstBB->getParent()->getEntryBlock() &&
778           "Should not hit the entry block because SI must be dominated by LI");
779       for (BasicBlock *Pred : predecessors(B)) {
780         PHITransAddr PredAddr = Addr;
781         if (PredAddr.NeedsPHITranslationFromBlock(B)) {
782           if (!PredAddr.IsPotentiallyPHITranslatable())
783             return false;
784           if (PredAddr.PHITranslateValue(B, Pred, DT, false))
785             return false;
786         }
787         Value *TranslatedPtr = PredAddr.getAddr();
788         auto Inserted = Visited.insert(std::make_pair(Pred, TranslatedPtr));
789         if (!Inserted.second) {
790           // We already visited this block before. If it was with a different
791           // address - bail out!
792           if (TranslatedPtr != Inserted.first->second)
793             return false;
794           // ... otherwise just skip it.
795           continue;
796         }
797         WorkList.push_back(std::make_pair(Pred, PredAddr));
798       }
799     }
800   }
801   return true;
802 }
803 
804 /// Find all blocks that will unconditionally lead to the block BB and append
805 /// them to F.
806 static void findUnconditionalPreds(SmallVectorImpl<BasicBlock *> &Blocks,
807                                    BasicBlock *BB, DominatorTree *DT) {
808   for (BasicBlock *Pred : predecessors(BB)) {
809     if (Pred == BB) continue;
810     Instruction *PredTI = Pred->getTerminator();
811     if (PredTI->getNumSuccessors() != 1)
812       continue;
813 
814     if (DT->isReachableFromEntry(Pred))
815       Blocks.push_back(Pred);
816   }
817 }
818 
819 /// Handle frees of entire structures whose dependency is a store
820 /// to a field of that structure.
821 static bool handleFree(CallInst *F, AliasAnalysis *AA,
822                        MemoryDependenceResults *MD, DominatorTree *DT,
823                        const TargetLibraryInfo *TLI,
824                        InstOverlapIntervalsTy &IOL,
825                        MapVector<Instruction *, bool> &ThrowableInst) {
826   bool MadeChange = false;
827 
828   MemoryLocation Loc = MemoryLocation::getAfter(F->getOperand(0));
829   SmallVector<BasicBlock *, 16> Blocks;
830   Blocks.push_back(F->getParent());
831 
832   while (!Blocks.empty()) {
833     BasicBlock *BB = Blocks.pop_back_val();
834     Instruction *InstPt = BB->getTerminator();
835     if (BB == F->getParent()) InstPt = F;
836 
837     MemDepResult Dep =
838         MD->getPointerDependencyFrom(Loc, false, InstPt->getIterator(), BB);
839     while (Dep.isDef() || Dep.isClobber()) {
840       Instruction *Dependency = Dep.getInst();
841       if (!hasAnalyzableMemoryWrite(Dependency, *TLI) ||
842           !isRemovable(Dependency))
843         break;
844 
845       Value *DepPointer =
846           getUnderlyingObject(getStoredPointerOperand(Dependency, *TLI));
847 
848       // Check for aliasing.
849       if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
850         break;
851 
852       LLVM_DEBUG(
853           dbgs() << "DSE: Dead Store to soon to be freed memory:\n  DEAD: "
854                  << *Dependency << '\n');
855 
856       // DCE instructions only used to calculate that store.
857       BasicBlock::iterator BBI(Dependency);
858       deleteDeadInstruction(Dependency, &BBI, *MD, *TLI, IOL,
859                             ThrowableInst);
860       ++NumFastStores;
861       MadeChange = true;
862 
863       // Inst's old Dependency is now deleted. Compute the next dependency,
864       // which may also be dead, as in
865       //    s[0] = 0;
866       //    s[1] = 0; // This has just been deleted.
867       //    free(s);
868       Dep = MD->getPointerDependencyFrom(Loc, false, BBI, BB);
869     }
870 
871     if (Dep.isNonLocal())
872       findUnconditionalPreds(Blocks, BB, DT);
873   }
874 
875   return MadeChange;
876 }
877 
878 /// Check to see if the specified location may alias any of the stack objects in
879 /// the DeadStackObjects set. If so, they become live because the location is
880 /// being loaded.
881 static void removeAccessedObjects(const MemoryLocation &LoadedLoc,
882                                   SmallSetVector<const Value *, 16> &DeadStackObjects,
883                                   const DataLayout &DL, AliasAnalysis *AA,
884                                   const TargetLibraryInfo *TLI,
885                                   const Function *F) {
886   const Value *UnderlyingPointer = getUnderlyingObject(LoadedLoc.Ptr);
887 
888   // A constant can't be in the dead pointer set.
889   if (isa<Constant>(UnderlyingPointer))
890     return;
891 
892   // If the kill pointer can be easily reduced to an alloca, don't bother doing
893   // extraneous AA queries.
894   if (isa<AllocaInst>(UnderlyingPointer) || isa<Argument>(UnderlyingPointer)) {
895     DeadStackObjects.remove(UnderlyingPointer);
896     return;
897   }
898 
899   // Remove objects that could alias LoadedLoc.
900   DeadStackObjects.remove_if([&](const Value *I) {
901     // See if the loaded location could alias the stack location.
902     MemoryLocation StackLoc(I, getPointerSize(I, DL, *TLI, F));
903     return !AA->isNoAlias(StackLoc, LoadedLoc);
904   });
905 }
906 
907 /// Remove dead stores to stack-allocated locations in the function end block.
908 /// Ex:
909 /// %A = alloca i32
910 /// ...
911 /// store i32 1, i32* %A
912 /// ret void
913 static bool handleEndBlock(BasicBlock &BB, AliasAnalysis *AA,
914                            MemoryDependenceResults *MD,
915                            const TargetLibraryInfo *TLI,
916                            InstOverlapIntervalsTy &IOL,
917                            MapVector<Instruction *, bool> &ThrowableInst) {
918   bool MadeChange = false;
919 
920   // Keep track of all of the stack objects that are dead at the end of the
921   // function.
922   SmallSetVector<const Value*, 16> DeadStackObjects;
923 
924   // Find all of the alloca'd pointers in the entry block.
925   BasicBlock &Entry = BB.getParent()->front();
926   for (Instruction &I : Entry) {
927     if (isa<AllocaInst>(&I))
928       DeadStackObjects.insert(&I);
929 
930     // Okay, so these are dead heap objects, but if the pointer never escapes
931     // then it's leaked by this function anyways.
932     else if (isAllocLikeFn(&I, TLI) && !PointerMayBeCaptured(&I, true, true))
933       DeadStackObjects.insert(&I);
934   }
935 
936   // Treat byval or inalloca arguments the same, stores to them are dead at the
937   // end of the function.
938   for (Argument &AI : BB.getParent()->args())
939     if (AI.hasPassPointeeByValueCopyAttr())
940       DeadStackObjects.insert(&AI);
941 
942   const DataLayout &DL = BB.getModule()->getDataLayout();
943 
944   // Scan the basic block backwards
945   for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
946     --BBI;
947 
948     // If we find a store, check to see if it points into a dead stack value.
949     if (hasAnalyzableMemoryWrite(&*BBI, *TLI) && isRemovable(&*BBI)) {
950       // See through pointer-to-pointer bitcasts
951       SmallVector<const Value *, 4> Pointers;
952       getUnderlyingObjects(getStoredPointerOperand(&*BBI, *TLI), Pointers);
953 
954       // Stores to stack values are valid candidates for removal.
955       bool AllDead = true;
956       for (const Value *Pointer : Pointers)
957         if (!DeadStackObjects.count(Pointer)) {
958           AllDead = false;
959           break;
960         }
961 
962       if (AllDead) {
963         Instruction *Dead = &*BBI;
964 
965         LLVM_DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n  DEAD: "
966                           << *Dead << "\n  Objects: ";
967                    for (SmallVectorImpl<const Value *>::iterator I =
968                             Pointers.begin(),
969                         E = Pointers.end();
970                         I != E; ++I) {
971                      dbgs() << **I;
972                      if (std::next(I) != E)
973                        dbgs() << ", ";
974                    } dbgs()
975                    << '\n');
976 
977         // DCE instructions only used to calculate that store.
978         deleteDeadInstruction(Dead, &BBI, *MD, *TLI, IOL, ThrowableInst,
979                               &DeadStackObjects);
980         ++NumFastStores;
981         MadeChange = true;
982         continue;
983       }
984     }
985 
986     // Remove any dead non-memory-mutating instructions.
987     if (isInstructionTriviallyDead(&*BBI, TLI)) {
988       LLVM_DEBUG(dbgs() << "DSE: Removing trivially dead instruction:\n  DEAD: "
989                         << *&*BBI << '\n');
990       deleteDeadInstruction(&*BBI, &BBI, *MD, *TLI, IOL, ThrowableInst,
991                             &DeadStackObjects);
992       ++NumFastOther;
993       MadeChange = true;
994       continue;
995     }
996 
997     if (isa<AllocaInst>(BBI)) {
998       // Remove allocas from the list of dead stack objects; there can't be
999       // any references before the definition.
1000       DeadStackObjects.remove(&*BBI);
1001       continue;
1002     }
1003 
1004     if (auto *Call = dyn_cast<CallBase>(&*BBI)) {
1005       // Remove allocation function calls from the list of dead stack objects;
1006       // there can't be any references before the definition.
1007       if (isAllocLikeFn(&*BBI, TLI))
1008         DeadStackObjects.remove(&*BBI);
1009 
1010       // If this call does not access memory, it can't be loading any of our
1011       // pointers.
1012       if (AA->doesNotAccessMemory(Call))
1013         continue;
1014 
1015       // If the call might load from any of our allocas, then any store above
1016       // the call is live.
1017       DeadStackObjects.remove_if([&](const Value *I) {
1018         // See if the call site touches the value.
1019         return isRefSet(AA->getModRefInfo(
1020             Call, I, getPointerSize(I, DL, *TLI, BB.getParent())));
1021       });
1022 
1023       // If all of the allocas were clobbered by the call then we're not going
1024       // to find anything else to process.
1025       if (DeadStackObjects.empty())
1026         break;
1027 
1028       continue;
1029     }
1030 
1031     // We can remove the dead stores, irrespective of the fence and its ordering
1032     // (release/acquire/seq_cst). Fences only constraints the ordering of
1033     // already visible stores, it does not make a store visible to other
1034     // threads. So, skipping over a fence does not change a store from being
1035     // dead.
1036     if (isa<FenceInst>(*BBI))
1037       continue;
1038 
1039     MemoryLocation LoadedLoc;
1040 
1041     // If we encounter a use of the pointer, it is no longer considered dead
1042     if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
1043       if (!L->isUnordered()) // Be conservative with atomic/volatile load
1044         break;
1045       LoadedLoc = MemoryLocation::get(L);
1046     } else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
1047       LoadedLoc = MemoryLocation::get(V);
1048     } else if (!BBI->mayReadFromMemory()) {
1049       // Instruction doesn't read memory.  Note that stores that weren't removed
1050       // above will hit this case.
1051       continue;
1052     } else {
1053       // Unknown inst; assume it clobbers everything.
1054       break;
1055     }
1056 
1057     // Remove any allocas from the DeadPointer set that are loaded, as this
1058     // makes any stores above the access live.
1059     removeAccessedObjects(LoadedLoc, DeadStackObjects, DL, AA, TLI, BB.getParent());
1060 
1061     // If all of the allocas were clobbered by the access then we're not going
1062     // to find anything else to process.
1063     if (DeadStackObjects.empty())
1064       break;
1065   }
1066 
1067   return MadeChange;
1068 }
1069 
1070 static bool tryToShorten(Instruction *EarlierWrite, int64_t &EarlierOffset,
1071                          uint64_t &EarlierSize, int64_t LaterOffset,
1072                          uint64_t LaterSize, bool IsOverwriteEnd) {
1073   // TODO: base this on the target vector size so that if the earlier
1074   // store was too small to get vector writes anyway then its likely
1075   // a good idea to shorten it
1076   // Power of 2 vector writes are probably always a bad idea to optimize
1077   // as any store/memset/memcpy is likely using vector instructions so
1078   // shortening it to not vector size is likely to be slower
1079   auto *EarlierIntrinsic = cast<AnyMemIntrinsic>(EarlierWrite);
1080   unsigned EarlierWriteAlign = EarlierIntrinsic->getDestAlignment();
1081   if (!IsOverwriteEnd)
1082     LaterOffset = int64_t(LaterOffset + LaterSize);
1083 
1084   if (!(isPowerOf2_64(LaterOffset) && EarlierWriteAlign <= LaterOffset) &&
1085       !((EarlierWriteAlign != 0) && LaterOffset % EarlierWriteAlign == 0))
1086     return false;
1087 
1088   int64_t NewLength = IsOverwriteEnd
1089                           ? LaterOffset - EarlierOffset
1090                           : EarlierSize - (LaterOffset - EarlierOffset);
1091 
1092   if (auto *AMI = dyn_cast<AtomicMemIntrinsic>(EarlierWrite)) {
1093     // When shortening an atomic memory intrinsic, the newly shortened
1094     // length must remain an integer multiple of the element size.
1095     const uint32_t ElementSize = AMI->getElementSizeInBytes();
1096     if (0 != NewLength % ElementSize)
1097       return false;
1098   }
1099 
1100   LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n  OW "
1101                     << (IsOverwriteEnd ? "END" : "BEGIN") << ": "
1102                     << *EarlierWrite << "\n  KILLER (offset " << LaterOffset
1103                     << ", " << EarlierSize << ")\n");
1104 
1105   Value *EarlierWriteLength = EarlierIntrinsic->getLength();
1106   Value *TrimmedLength =
1107       ConstantInt::get(EarlierWriteLength->getType(), NewLength);
1108   EarlierIntrinsic->setLength(TrimmedLength);
1109 
1110   EarlierSize = NewLength;
1111   if (!IsOverwriteEnd) {
1112     int64_t OffsetMoved = (LaterOffset - EarlierOffset);
1113     Value *Indices[1] = {
1114         ConstantInt::get(EarlierWriteLength->getType(), OffsetMoved)};
1115     GetElementPtrInst *NewDestGEP = GetElementPtrInst::CreateInBounds(
1116         EarlierIntrinsic->getRawDest()->getType()->getPointerElementType(),
1117         EarlierIntrinsic->getRawDest(), Indices, "", EarlierWrite);
1118     NewDestGEP->setDebugLoc(EarlierIntrinsic->getDebugLoc());
1119     EarlierIntrinsic->setDest(NewDestGEP);
1120     EarlierOffset = EarlierOffset + OffsetMoved;
1121   }
1122   return true;
1123 }
1124 
1125 static bool tryToShortenEnd(Instruction *EarlierWrite,
1126                             OverlapIntervalsTy &IntervalMap,
1127                             int64_t &EarlierStart, uint64_t &EarlierSize) {
1128   if (IntervalMap.empty() || !isShortenableAtTheEnd(EarlierWrite))
1129     return false;
1130 
1131   OverlapIntervalsTy::iterator OII = --IntervalMap.end();
1132   int64_t LaterStart = OII->second;
1133   uint64_t LaterSize = OII->first - LaterStart;
1134 
1135   assert(OII->first - LaterStart >= 0 && "Size expected to be positive");
1136 
1137   if (LaterStart > EarlierStart &&
1138       // Note: "LaterStart - EarlierStart" is known to be positive due to
1139       // preceding check.
1140       (uint64_t)(LaterStart - EarlierStart) < EarlierSize &&
1141       // Note: "EarlierSize - (uint64_t)(LaterStart - EarlierStart)" is known to
1142       // be non negative due to preceding checks.
1143       LaterSize >= EarlierSize - (uint64_t)(LaterStart - EarlierStart)) {
1144     if (tryToShorten(EarlierWrite, EarlierStart, EarlierSize, LaterStart,
1145                      LaterSize, true)) {
1146       IntervalMap.erase(OII);
1147       return true;
1148     }
1149   }
1150   return false;
1151 }
1152 
1153 static bool tryToShortenBegin(Instruction *EarlierWrite,
1154                               OverlapIntervalsTy &IntervalMap,
1155                               int64_t &EarlierStart, uint64_t &EarlierSize) {
1156   if (IntervalMap.empty() || !isShortenableAtTheBeginning(EarlierWrite))
1157     return false;
1158 
1159   OverlapIntervalsTy::iterator OII = IntervalMap.begin();
1160   int64_t LaterStart = OII->second;
1161   uint64_t LaterSize = OII->first - LaterStart;
1162 
1163   assert(OII->first - LaterStart >= 0 && "Size expected to be positive");
1164 
1165   if (LaterStart <= EarlierStart &&
1166       // Note: "EarlierStart - LaterStart" is known to be non negative due to
1167       // preceding check.
1168       LaterSize > (uint64_t)(EarlierStart - LaterStart)) {
1169     // Note: "LaterSize - (uint64_t)(EarlierStart - LaterStart)" is known to be
1170     // positive due to preceding checks.
1171     assert(LaterSize - (uint64_t)(EarlierStart - LaterStart) < EarlierSize &&
1172            "Should have been handled as OW_Complete");
1173     if (tryToShorten(EarlierWrite, EarlierStart, EarlierSize, LaterStart,
1174                      LaterSize, false)) {
1175       IntervalMap.erase(OII);
1176       return true;
1177     }
1178   }
1179   return false;
1180 }
1181 
1182 static bool removePartiallyOverlappedStores(const DataLayout &DL,
1183                                             InstOverlapIntervalsTy &IOL,
1184                                             const TargetLibraryInfo &TLI) {
1185   bool Changed = false;
1186   for (auto OI : IOL) {
1187     Instruction *EarlierWrite = OI.first;
1188     MemoryLocation Loc = getLocForWrite(EarlierWrite, TLI);
1189     assert(isRemovable(EarlierWrite) && "Expect only removable instruction");
1190 
1191     const Value *Ptr = Loc.Ptr->stripPointerCasts();
1192     int64_t EarlierStart = 0;
1193     uint64_t EarlierSize = Loc.Size.getValue();
1194     GetPointerBaseWithConstantOffset(Ptr, EarlierStart, DL);
1195     OverlapIntervalsTy &IntervalMap = OI.second;
1196     Changed |=
1197         tryToShortenEnd(EarlierWrite, IntervalMap, EarlierStart, EarlierSize);
1198     if (IntervalMap.empty())
1199       continue;
1200     Changed |=
1201         tryToShortenBegin(EarlierWrite, IntervalMap, EarlierStart, EarlierSize);
1202   }
1203   return Changed;
1204 }
1205 
1206 static bool eliminateNoopStore(Instruction *Inst, BasicBlock::iterator &BBI,
1207                                AliasAnalysis *AA, MemoryDependenceResults *MD,
1208                                const DataLayout &DL,
1209                                const TargetLibraryInfo *TLI,
1210                                InstOverlapIntervalsTy &IOL,
1211                                MapVector<Instruction *, bool> &ThrowableInst,
1212                                DominatorTree *DT) {
1213   // Must be a store instruction.
1214   StoreInst *SI = dyn_cast<StoreInst>(Inst);
1215   if (!SI)
1216     return false;
1217 
1218   // If we're storing the same value back to a pointer that we just loaded from,
1219   // then the store can be removed.
1220   if (LoadInst *DepLoad = dyn_cast<LoadInst>(SI->getValueOperand())) {
1221     if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
1222         isRemovable(SI) &&
1223         memoryIsNotModifiedBetween(DepLoad, SI, *AA, DL, DT)) {
1224 
1225       LLVM_DEBUG(
1226           dbgs() << "DSE: Remove Store Of Load from same pointer:\n  LOAD: "
1227                  << *DepLoad << "\n  STORE: " << *SI << '\n');
1228 
1229       deleteDeadInstruction(SI, &BBI, *MD, *TLI, IOL, ThrowableInst);
1230       ++NumRedundantStores;
1231       return true;
1232     }
1233   }
1234 
1235   // Remove null stores into the calloc'ed objects
1236   Constant *StoredConstant = dyn_cast<Constant>(SI->getValueOperand());
1237   if (StoredConstant && StoredConstant->isNullValue() && isRemovable(SI)) {
1238     Instruction *UnderlyingPointer =
1239         dyn_cast<Instruction>(getUnderlyingObject(SI->getPointerOperand()));
1240 
1241     if (UnderlyingPointer && isCallocLikeFn(UnderlyingPointer, TLI) &&
1242         memoryIsNotModifiedBetween(UnderlyingPointer, SI, *AA, DL, DT)) {
1243       LLVM_DEBUG(
1244           dbgs() << "DSE: Remove null store to the calloc'ed object:\n  DEAD: "
1245                  << *Inst << "\n  OBJECT: " << *UnderlyingPointer << '\n');
1246 
1247       deleteDeadInstruction(SI, &BBI, *MD, *TLI, IOL, ThrowableInst);
1248       ++NumRedundantStores;
1249       return true;
1250     }
1251   }
1252   return false;
1253 }
1254 
1255 template <typename AATy>
1256 static Constant *tryToMergePartialOverlappingStores(
1257     StoreInst *Earlier, StoreInst *Later, int64_t InstWriteOffset,
1258     int64_t DepWriteOffset, const DataLayout &DL, AATy &AA, DominatorTree *DT) {
1259 
1260   if (Earlier && isa<ConstantInt>(Earlier->getValueOperand()) &&
1261       DL.typeSizeEqualsStoreSize(Earlier->getValueOperand()->getType()) &&
1262       Later && isa<ConstantInt>(Later->getValueOperand()) &&
1263       DL.typeSizeEqualsStoreSize(Later->getValueOperand()->getType()) &&
1264       memoryIsNotModifiedBetween(Earlier, Later, AA, DL, DT)) {
1265     // If the store we find is:
1266     //   a) partially overwritten by the store to 'Loc'
1267     //   b) the later store is fully contained in the earlier one and
1268     //   c) they both have a constant value
1269     //   d) none of the two stores need padding
1270     // Merge the two stores, replacing the earlier store's value with a
1271     // merge of both values.
1272     // TODO: Deal with other constant types (vectors, etc), and probably
1273     // some mem intrinsics (if needed)
1274 
1275     APInt EarlierValue =
1276         cast<ConstantInt>(Earlier->getValueOperand())->getValue();
1277     APInt LaterValue = cast<ConstantInt>(Later->getValueOperand())->getValue();
1278     unsigned LaterBits = LaterValue.getBitWidth();
1279     assert(EarlierValue.getBitWidth() > LaterValue.getBitWidth());
1280     LaterValue = LaterValue.zext(EarlierValue.getBitWidth());
1281 
1282     // Offset of the smaller store inside the larger store
1283     unsigned BitOffsetDiff = (InstWriteOffset - DepWriteOffset) * 8;
1284     unsigned LShiftAmount = DL.isBigEndian() ? EarlierValue.getBitWidth() -
1285                                                    BitOffsetDiff - LaterBits
1286                                              : BitOffsetDiff;
1287     APInt Mask = APInt::getBitsSet(EarlierValue.getBitWidth(), LShiftAmount,
1288                                    LShiftAmount + LaterBits);
1289     // Clear the bits we'll be replacing, then OR with the smaller
1290     // store, shifted appropriately.
1291     APInt Merged = (EarlierValue & ~Mask) | (LaterValue << LShiftAmount);
1292     LLVM_DEBUG(dbgs() << "DSE: Merge Stores:\n  Earlier: " << *Earlier
1293                       << "\n  Later: " << *Later
1294                       << "\n  Merged Value: " << Merged << '\n');
1295     return ConstantInt::get(Earlier->getValueOperand()->getType(), Merged);
1296   }
1297   return nullptr;
1298 }
1299 
1300 static bool eliminateDeadStores(BasicBlock &BB, AliasAnalysis *AA,
1301                                 MemoryDependenceResults *MD, DominatorTree *DT,
1302                                 const TargetLibraryInfo *TLI) {
1303   const DataLayout &DL = BB.getModule()->getDataLayout();
1304   bool MadeChange = false;
1305 
1306   MapVector<Instruction *, bool> ThrowableInst;
1307 
1308   // A map of interval maps representing partially-overwritten value parts.
1309   InstOverlapIntervalsTy IOL;
1310 
1311   // Do a top-down walk on the BB.
1312   for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
1313     // Handle 'free' calls specially.
1314     if (CallInst *F = isFreeCall(&*BBI, TLI)) {
1315       MadeChange |= handleFree(F, AA, MD, DT, TLI, IOL, ThrowableInst);
1316       // Increment BBI after handleFree has potentially deleted instructions.
1317       // This ensures we maintain a valid iterator.
1318       ++BBI;
1319       continue;
1320     }
1321 
1322     Instruction *Inst = &*BBI++;
1323 
1324     if (Inst->mayThrow()) {
1325       ThrowableInst[Inst] = true;
1326       continue;
1327     }
1328 
1329     // Check to see if Inst writes to memory.  If not, continue.
1330     if (!hasAnalyzableMemoryWrite(Inst, *TLI))
1331       continue;
1332 
1333     // eliminateNoopStore will update in iterator, if necessary.
1334     if (eliminateNoopStore(Inst, BBI, AA, MD, DL, TLI, IOL,
1335                            ThrowableInst, DT)) {
1336       MadeChange = true;
1337       continue;
1338     }
1339 
1340     // If we find something that writes memory, get its memory dependence.
1341     MemDepResult InstDep = MD->getDependency(Inst);
1342 
1343     // Ignore any store where we can't find a local dependence.
1344     // FIXME: cross-block DSE would be fun. :)
1345     if (!InstDep.isDef() && !InstDep.isClobber())
1346       continue;
1347 
1348     // Figure out what location is being stored to.
1349     MemoryLocation Loc = getLocForWrite(Inst, *TLI);
1350 
1351     // If we didn't get a useful location, fail.
1352     if (!Loc.Ptr)
1353       continue;
1354 
1355     // Loop until we find a store we can eliminate or a load that
1356     // invalidates the analysis. Without an upper bound on the number of
1357     // instructions examined, this analysis can become very time-consuming.
1358     // However, the potential gain diminishes as we process more instructions
1359     // without eliminating any of them. Therefore, we limit the number of
1360     // instructions we look at.
1361     auto Limit = MD->getDefaultBlockScanLimit();
1362     while (InstDep.isDef() || InstDep.isClobber()) {
1363       // Get the memory clobbered by the instruction we depend on.  MemDep will
1364       // skip any instructions that 'Loc' clearly doesn't interact with.  If we
1365       // end up depending on a may- or must-aliased load, then we can't optimize
1366       // away the store and we bail out.  However, if we depend on something
1367       // that overwrites the memory location we *can* potentially optimize it.
1368       //
1369       // Find out what memory location the dependent instruction stores.
1370       Instruction *DepWrite = InstDep.getInst();
1371       if (!hasAnalyzableMemoryWrite(DepWrite, *TLI))
1372         break;
1373       MemoryLocation DepLoc = getLocForWrite(DepWrite, *TLI);
1374       // If we didn't get a useful location, or if it isn't a size, bail out.
1375       if (!DepLoc.Ptr)
1376         break;
1377 
1378       // Find the last throwable instruction not removed by call to
1379       // deleteDeadInstruction.
1380       Instruction *LastThrowing = nullptr;
1381       if (!ThrowableInst.empty())
1382         LastThrowing = ThrowableInst.back().first;
1383 
1384       // Make sure we don't look past a call which might throw. This is an
1385       // issue because MemoryDependenceAnalysis works in the wrong direction:
1386       // it finds instructions which dominate the current instruction, rather than
1387       // instructions which are post-dominated by the current instruction.
1388       //
1389       // If the underlying object is a non-escaping memory allocation, any store
1390       // to it is dead along the unwind edge. Otherwise, we need to preserve
1391       // the store.
1392       if (LastThrowing && DepWrite->comesBefore(LastThrowing)) {
1393         const Value *Underlying = getUnderlyingObject(DepLoc.Ptr);
1394         bool IsStoreDeadOnUnwind = isa<AllocaInst>(Underlying);
1395         if (!IsStoreDeadOnUnwind) {
1396             // We're looking for a call to an allocation function
1397             // where the allocation doesn't escape before the last
1398             // throwing instruction; PointerMayBeCaptured
1399             // reasonably fast approximation.
1400             IsStoreDeadOnUnwind = isAllocLikeFn(Underlying, TLI) &&
1401                 !PointerMayBeCaptured(Underlying, false, true);
1402         }
1403         if (!IsStoreDeadOnUnwind)
1404           break;
1405       }
1406 
1407       // If we find a write that is a) removable (i.e., non-volatile), b) is
1408       // completely obliterated by the store to 'Loc', and c) which we know that
1409       // 'Inst' doesn't load from, then we can remove it.
1410       // Also try to merge two stores if a later one only touches memory written
1411       // to by the earlier one.
1412       if (isRemovable(DepWrite) &&
1413           !isPossibleSelfRead(Inst, Loc, DepWrite, *TLI, *AA)) {
1414         int64_t InstWriteOffset, DepWriteOffset;
1415         OverwriteResult OR = isOverwrite(Inst, DepWrite, Loc, DepLoc, DL, *TLI,
1416                                          DepWriteOffset, InstWriteOffset, *AA,
1417                                          BB.getParent());
1418         if (OR == OW_MaybePartial)
1419           OR = isPartialOverwrite(Loc, DepLoc, DepWriteOffset, InstWriteOffset,
1420                                   DepWrite, IOL);
1421 
1422         if (OR == OW_Complete) {
1423           LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n  DEAD: " << *DepWrite
1424                             << "\n  KILLER: " << *Inst << '\n');
1425 
1426           // Delete the store and now-dead instructions that feed it.
1427           deleteDeadInstruction(DepWrite, &BBI, *MD, *TLI, IOL,
1428                                 ThrowableInst);
1429           ++NumFastStores;
1430           MadeChange = true;
1431 
1432           // We erased DepWrite; start over.
1433           InstDep = MD->getDependency(Inst);
1434           continue;
1435         } else if ((OR == OW_End && isShortenableAtTheEnd(DepWrite)) ||
1436                    ((OR == OW_Begin &&
1437                      isShortenableAtTheBeginning(DepWrite)))) {
1438           assert(!EnablePartialOverwriteTracking && "Do not expect to perform "
1439                                                     "when partial-overwrite "
1440                                                     "tracking is enabled");
1441           // The overwrite result is known, so these must be known, too.
1442           uint64_t EarlierSize = DepLoc.Size.getValue();
1443           uint64_t LaterSize = Loc.Size.getValue();
1444           bool IsOverwriteEnd = (OR == OW_End);
1445           MadeChange |= tryToShorten(DepWrite, DepWriteOffset, EarlierSize,
1446                                     InstWriteOffset, LaterSize, IsOverwriteEnd);
1447         } else if (EnablePartialStoreMerging &&
1448                    OR == OW_PartialEarlierWithFullLater) {
1449           auto *Earlier = dyn_cast<StoreInst>(DepWrite);
1450           auto *Later = dyn_cast<StoreInst>(Inst);
1451           if (Constant *C = tryToMergePartialOverlappingStores(
1452                   Earlier, Later, InstWriteOffset, DepWriteOffset, DL, *AA,
1453                   DT)) {
1454             auto *SI = new StoreInst(
1455                 C, Earlier->getPointerOperand(), false, Earlier->getAlign(),
1456                 Earlier->getOrdering(), Earlier->getSyncScopeID(), DepWrite);
1457 
1458             unsigned MDToKeep[] = {LLVMContext::MD_dbg, LLVMContext::MD_tbaa,
1459                                    LLVMContext::MD_alias_scope,
1460                                    LLVMContext::MD_noalias,
1461                                    LLVMContext::MD_nontemporal};
1462             SI->copyMetadata(*DepWrite, MDToKeep);
1463             ++NumModifiedStores;
1464 
1465             // Delete the old stores and now-dead instructions that feed them.
1466             deleteDeadInstruction(Inst, &BBI, *MD, *TLI, IOL,
1467                                   ThrowableInst);
1468             deleteDeadInstruction(DepWrite, &BBI, *MD, *TLI, IOL,
1469                                   ThrowableInst);
1470             MadeChange = true;
1471 
1472             // We erased DepWrite and Inst (Loc); start over.
1473             break;
1474           }
1475         }
1476       }
1477 
1478       // If this is a may-aliased store that is clobbering the store value, we
1479       // can keep searching past it for another must-aliased pointer that stores
1480       // to the same location.  For example, in:
1481       //   store -> P
1482       //   store -> Q
1483       //   store -> P
1484       // we can remove the first store to P even though we don't know if P and Q
1485       // alias.
1486       if (DepWrite == &BB.front()) break;
1487 
1488       // Can't look past this instruction if it might read 'Loc'.
1489       if (isRefSet(AA->getModRefInfo(DepWrite, Loc)))
1490         break;
1491 
1492       InstDep = MD->getPointerDependencyFrom(Loc, /*isLoad=*/ false,
1493                                              DepWrite->getIterator(), &BB,
1494                                              /*QueryInst=*/ nullptr, &Limit);
1495     }
1496   }
1497 
1498   if (EnablePartialOverwriteTracking)
1499     MadeChange |= removePartiallyOverlappedStores(DL, IOL, *TLI);
1500 
1501   // If this block ends in a return, unwind, or unreachable, all allocas are
1502   // dead at its end, which means stores to them are also dead.
1503   if (BB.getTerminator()->getNumSuccessors() == 0)
1504     MadeChange |= handleEndBlock(BB, AA, MD, TLI, IOL, ThrowableInst);
1505 
1506   return MadeChange;
1507 }
1508 
1509 static bool eliminateDeadStores(Function &F, AliasAnalysis *AA,
1510                                 MemoryDependenceResults *MD, DominatorTree *DT,
1511                                 const TargetLibraryInfo *TLI) {
1512   bool MadeChange = false;
1513   for (BasicBlock &BB : F)
1514     // Only check non-dead blocks.  Dead blocks may have strange pointer
1515     // cycles that will confuse alias analysis.
1516     if (DT->isReachableFromEntry(&BB))
1517       MadeChange |= eliminateDeadStores(BB, AA, MD, DT, TLI);
1518 
1519   return MadeChange;
1520 }
1521 
1522 namespace {
1523 //=============================================================================
1524 // MemorySSA backed dead store elimination.
1525 //
1526 // The code below implements dead store elimination using MemorySSA. It uses
1527 // the following general approach: given a MemoryDef, walk upwards to find
1528 // clobbering MemoryDefs that may be killed by the starting def. Then check
1529 // that there are no uses that may read the location of the original MemoryDef
1530 // in between both MemoryDefs. A bit more concretely:
1531 //
1532 // For all MemoryDefs StartDef:
1533 // 1. Get the next dominating clobbering MemoryDef (EarlierAccess) by walking
1534 //    upwards.
1535 // 2. Check that there are no reads between EarlierAccess and the StartDef by
1536 //    checking all uses starting at EarlierAccess and walking until we see
1537 //    StartDef.
1538 // 3. For each found CurrentDef, check that:
1539 //   1. There are no barrier instructions between CurrentDef and StartDef (like
1540 //       throws or stores with ordering constraints).
1541 //   2. StartDef is executed whenever CurrentDef is executed.
1542 //   3. StartDef completely overwrites CurrentDef.
1543 // 4. Erase CurrentDef from the function and MemorySSA.
1544 
1545 // Returns true if \p I is an intrisnic that does not read or write memory.
1546 bool isNoopIntrinsic(Instruction *I) {
1547   if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1548     switch (II->getIntrinsicID()) {
1549     case Intrinsic::lifetime_start:
1550     case Intrinsic::lifetime_end:
1551     case Intrinsic::invariant_end:
1552     case Intrinsic::launder_invariant_group:
1553     case Intrinsic::assume:
1554       return true;
1555     case Intrinsic::dbg_addr:
1556     case Intrinsic::dbg_declare:
1557     case Intrinsic::dbg_label:
1558     case Intrinsic::dbg_value:
1559       llvm_unreachable("Intrinsic should not be modeled in MemorySSA");
1560     default:
1561       return false;
1562     }
1563   }
1564   return false;
1565 }
1566 
1567 // Check if we can ignore \p D for DSE.
1568 bool canSkipDef(MemoryDef *D, bool DefVisibleToCaller) {
1569   Instruction *DI = D->getMemoryInst();
1570   // Calls that only access inaccessible memory cannot read or write any memory
1571   // locations we consider for elimination.
1572   if (auto *CB = dyn_cast<CallBase>(DI))
1573     if (CB->onlyAccessesInaccessibleMemory())
1574       return true;
1575 
1576   // We can eliminate stores to locations not visible to the caller across
1577   // throwing instructions.
1578   if (DI->mayThrow() && !DefVisibleToCaller)
1579     return true;
1580 
1581   // We can remove the dead stores, irrespective of the fence and its ordering
1582   // (release/acquire/seq_cst). Fences only constraints the ordering of
1583   // already visible stores, it does not make a store visible to other
1584   // threads. So, skipping over a fence does not change a store from being
1585   // dead.
1586   if (isa<FenceInst>(DI))
1587     return true;
1588 
1589   // Skip intrinsics that do not really read or modify memory.
1590   if (isNoopIntrinsic(D->getMemoryInst()))
1591     return true;
1592 
1593   return false;
1594 }
1595 
1596 struct DSEState {
1597   Function &F;
1598   AliasAnalysis &AA;
1599 
1600   /// The single BatchAA instance that is used to cache AA queries. It will
1601   /// not be invalidated over the whole run. This is safe, because:
1602   /// 1. Only memory writes are removed, so the alias cache for memory
1603   ///    locations remains valid.
1604   /// 2. No new instructions are added (only instructions removed), so cached
1605   ///    information for a deleted value cannot be accessed by a re-used new
1606   ///    value pointer.
1607   BatchAAResults BatchAA;
1608 
1609   MemorySSA &MSSA;
1610   DominatorTree &DT;
1611   PostDominatorTree &PDT;
1612   const TargetLibraryInfo &TLI;
1613   const DataLayout &DL;
1614 
1615   // All MemoryDefs that potentially could kill other MemDefs.
1616   SmallVector<MemoryDef *, 64> MemDefs;
1617   // Any that should be skipped as they are already deleted
1618   SmallPtrSet<MemoryAccess *, 4> SkipStores;
1619   // Keep track of all of the objects that are invisible to the caller before
1620   // the function returns.
1621   // SmallPtrSet<const Value *, 16> InvisibleToCallerBeforeRet;
1622   DenseMap<const Value *, bool> InvisibleToCallerBeforeRet;
1623   // Keep track of all of the objects that are invisible to the caller after
1624   // the function returns.
1625   DenseMap<const Value *, bool> InvisibleToCallerAfterRet;
1626   // Keep track of blocks with throwing instructions not modeled in MemorySSA.
1627   SmallPtrSet<BasicBlock *, 16> ThrowingBlocks;
1628   // Post-order numbers for each basic block. Used to figure out if memory
1629   // accesses are executed before another access.
1630   DenseMap<BasicBlock *, unsigned> PostOrderNumbers;
1631 
1632   /// Keep track of instructions (partly) overlapping with killing MemoryDefs per
1633   /// basic block.
1634   DenseMap<BasicBlock *, InstOverlapIntervalsTy> IOLs;
1635 
1636   DSEState(Function &F, AliasAnalysis &AA, MemorySSA &MSSA, DominatorTree &DT,
1637            PostDominatorTree &PDT, const TargetLibraryInfo &TLI)
1638       : F(F), AA(AA), BatchAA(AA), MSSA(MSSA), DT(DT), PDT(PDT), TLI(TLI),
1639         DL(F.getParent()->getDataLayout()) {}
1640 
1641   static DSEState get(Function &F, AliasAnalysis &AA, MemorySSA &MSSA,
1642                       DominatorTree &DT, PostDominatorTree &PDT,
1643                       const TargetLibraryInfo &TLI) {
1644     DSEState State(F, AA, MSSA, DT, PDT, TLI);
1645     // Collect blocks with throwing instructions not modeled in MemorySSA and
1646     // alloc-like objects.
1647     unsigned PO = 0;
1648     for (BasicBlock *BB : post_order(&F)) {
1649       State.PostOrderNumbers[BB] = PO++;
1650       for (Instruction &I : *BB) {
1651         MemoryAccess *MA = MSSA.getMemoryAccess(&I);
1652         if (I.mayThrow() && !MA)
1653           State.ThrowingBlocks.insert(I.getParent());
1654 
1655         auto *MD = dyn_cast_or_null<MemoryDef>(MA);
1656         if (MD && State.MemDefs.size() < MemorySSADefsPerBlockLimit &&
1657             (State.getLocForWriteEx(&I) || State.isMemTerminatorInst(&I)))
1658           State.MemDefs.push_back(MD);
1659       }
1660     }
1661 
1662     // Treat byval or inalloca arguments the same as Allocas, stores to them are
1663     // dead at the end of the function.
1664     for (Argument &AI : F.args())
1665       if (AI.hasPassPointeeByValueCopyAttr()) {
1666         // For byval, the caller doesn't know the address of the allocation.
1667         if (AI.hasByValAttr())
1668           State.InvisibleToCallerBeforeRet.insert({&AI, true});
1669         State.InvisibleToCallerAfterRet.insert({&AI, true});
1670       }
1671 
1672     return State;
1673   }
1674 
1675   bool isInvisibleToCallerAfterRet(const Value *V) {
1676     if (isa<AllocaInst>(V))
1677       return true;
1678     auto I = InvisibleToCallerAfterRet.insert({V, false});
1679     if (I.second) {
1680       if (!isInvisibleToCallerBeforeRet(V)) {
1681         I.first->second = false;
1682       } else {
1683         auto *Inst = dyn_cast<Instruction>(V);
1684         if (Inst && isAllocLikeFn(Inst, &TLI))
1685           I.first->second = !PointerMayBeCaptured(V, true, false);
1686       }
1687     }
1688     return I.first->second;
1689   }
1690 
1691   bool isInvisibleToCallerBeforeRet(const Value *V) {
1692     if (isa<AllocaInst>(V))
1693       return true;
1694     auto I = InvisibleToCallerBeforeRet.insert({V, false});
1695     if (I.second) {
1696       auto *Inst = dyn_cast<Instruction>(V);
1697       if (Inst && isAllocLikeFn(Inst, &TLI))
1698         // NOTE: This could be made more precise by PointerMayBeCapturedBefore
1699         // with the killing MemoryDef. But we refrain from doing so for now to
1700         // limit compile-time and this does not cause any changes to the number
1701         // of stores removed on a large test set in practice.
1702         I.first->second = !PointerMayBeCaptured(V, false, true);
1703     }
1704     return I.first->second;
1705   }
1706 
1707   Optional<MemoryLocation> getLocForWriteEx(Instruction *I) const {
1708     if (!I->mayWriteToMemory())
1709       return None;
1710 
1711     if (auto *MTI = dyn_cast<AnyMemIntrinsic>(I))
1712       return {MemoryLocation::getForDest(MTI)};
1713 
1714     if (auto *CB = dyn_cast<CallBase>(I)) {
1715       // If the functions may write to memory we do not know about, bail out.
1716       if (!CB->onlyAccessesArgMemory() &&
1717           !CB->onlyAccessesInaccessibleMemOrArgMem())
1718         return None;
1719 
1720       LibFunc LF;
1721       if (TLI.getLibFunc(*CB, LF) && TLI.has(LF)) {
1722         switch (LF) {
1723         case LibFunc_strcpy:
1724         case LibFunc_strncpy:
1725         case LibFunc_strcat:
1726         case LibFunc_strncat:
1727           return {MemoryLocation::getAfter(CB->getArgOperand(0))};
1728         default:
1729           break;
1730         }
1731       }
1732       switch (CB->getIntrinsicID()) {
1733       case Intrinsic::init_trampoline:
1734         return {MemoryLocation::getAfter(CB->getArgOperand(0))};
1735       case Intrinsic::masked_store:
1736         return {MemoryLocation::getForArgument(CB, 1, TLI)};
1737       default:
1738         break;
1739       }
1740       return None;
1741     }
1742 
1743     return MemoryLocation::getOrNone(I);
1744   }
1745 
1746   /// Returns true if \p UseInst completely overwrites \p DefLoc
1747   /// (stored by \p DefInst).
1748   bool isCompleteOverwrite(const MemoryLocation &DefLoc, Instruction *DefInst,
1749                            Instruction *UseInst) {
1750     // UseInst has a MemoryDef associated in MemorySSA. It's possible for a
1751     // MemoryDef to not write to memory, e.g. a volatile load is modeled as a
1752     // MemoryDef.
1753     if (!UseInst->mayWriteToMemory())
1754       return false;
1755 
1756     if (auto *CB = dyn_cast<CallBase>(UseInst))
1757       if (CB->onlyAccessesInaccessibleMemory())
1758         return false;
1759 
1760     int64_t InstWriteOffset, DepWriteOffset;
1761     if (auto CC = getLocForWriteEx(UseInst))
1762       return isOverwrite(UseInst, DefInst, *CC, DefLoc, DL, TLI, DepWriteOffset,
1763                          InstWriteOffset, BatchAA, &F) == OW_Complete;
1764     return false;
1765   }
1766 
1767   /// Returns true if \p Def is not read before returning from the function.
1768   bool isWriteAtEndOfFunction(MemoryDef *Def) {
1769     LLVM_DEBUG(dbgs() << "  Check if def " << *Def << " ("
1770                       << *Def->getMemoryInst()
1771                       << ") is at the end the function \n");
1772 
1773     auto MaybeLoc = getLocForWriteEx(Def->getMemoryInst());
1774     if (!MaybeLoc) {
1775       LLVM_DEBUG(dbgs() << "  ... could not get location for write.\n");
1776       return false;
1777     }
1778 
1779     SmallVector<MemoryAccess *, 4> WorkList;
1780     SmallPtrSet<MemoryAccess *, 8> Visited;
1781     auto PushMemUses = [&WorkList, &Visited](MemoryAccess *Acc) {
1782       if (!Visited.insert(Acc).second)
1783         return;
1784       for (Use &U : Acc->uses())
1785         WorkList.push_back(cast<MemoryAccess>(U.getUser()));
1786     };
1787     PushMemUses(Def);
1788     for (unsigned I = 0; I < WorkList.size(); I++) {
1789       if (WorkList.size() >= MemorySSAScanLimit) {
1790         LLVM_DEBUG(dbgs() << "  ... hit exploration limit.\n");
1791         return false;
1792       }
1793 
1794       MemoryAccess *UseAccess = WorkList[I];
1795       // Simply adding the users of MemoryPhi to the worklist is not enough,
1796       // because we might miss read clobbers in different iterations of a loop,
1797       // for example.
1798       // TODO: Add support for phi translation to handle the loop case.
1799       if (isa<MemoryPhi>(UseAccess))
1800         return false;
1801 
1802       // TODO: Checking for aliasing is expensive. Consider reducing the amount
1803       // of times this is called and/or caching it.
1804       Instruction *UseInst = cast<MemoryUseOrDef>(UseAccess)->getMemoryInst();
1805       if (isReadClobber(*MaybeLoc, UseInst)) {
1806         LLVM_DEBUG(dbgs() << "  ... hit read clobber " << *UseInst << ".\n");
1807         return false;
1808       }
1809 
1810       if (MemoryDef *UseDef = dyn_cast<MemoryDef>(UseAccess))
1811         PushMemUses(UseDef);
1812     }
1813     return true;
1814   }
1815 
1816   /// If \p I is a memory  terminator like llvm.lifetime.end or free, return a
1817   /// pair with the MemoryLocation terminated by \p I and a boolean flag
1818   /// indicating whether \p I is a free-like call.
1819   Optional<std::pair<MemoryLocation, bool>>
1820   getLocForTerminator(Instruction *I) const {
1821     uint64_t Len;
1822     Value *Ptr;
1823     if (match(I, m_Intrinsic<Intrinsic::lifetime_end>(m_ConstantInt(Len),
1824                                                       m_Value(Ptr))))
1825       return {std::make_pair(MemoryLocation(Ptr, Len), false)};
1826 
1827     if (auto *CB = dyn_cast<CallBase>(I)) {
1828       if (isFreeCall(I, &TLI))
1829         return {std::make_pair(MemoryLocation::getAfter(CB->getArgOperand(0)),
1830                                true)};
1831     }
1832 
1833     return None;
1834   }
1835 
1836   /// Returns true if \p I is a memory terminator instruction like
1837   /// llvm.lifetime.end or free.
1838   bool isMemTerminatorInst(Instruction *I) const {
1839     IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1840     return (II && II->getIntrinsicID() == Intrinsic::lifetime_end) ||
1841            isFreeCall(I, &TLI);
1842   }
1843 
1844   /// Returns true if \p MaybeTerm is a memory terminator for \p Loc from
1845   /// instruction \p AccessI.
1846   bool isMemTerminator(const MemoryLocation &Loc, Instruction *AccessI,
1847                        Instruction *MaybeTerm) {
1848     Optional<std::pair<MemoryLocation, bool>> MaybeTermLoc =
1849         getLocForTerminator(MaybeTerm);
1850 
1851     if (!MaybeTermLoc)
1852       return false;
1853 
1854     // If the terminator is a free-like call, all accesses to the underlying
1855     // object can be considered terminated.
1856     if (getUnderlyingObject(Loc.Ptr) !=
1857         getUnderlyingObject(MaybeTermLoc->first.Ptr))
1858       return false;
1859 
1860     auto TermLoc = MaybeTermLoc->first;
1861     if (MaybeTermLoc->second) {
1862       const Value *LocUO = getUnderlyingObject(Loc.Ptr);
1863       return BatchAA.isMustAlias(TermLoc.Ptr, LocUO);
1864     }
1865     int64_t InstWriteOffset, DepWriteOffset;
1866     return isOverwrite(MaybeTerm, AccessI, TermLoc, Loc, DL, TLI,
1867                        DepWriteOffset, InstWriteOffset, BatchAA,
1868                        &F) == OW_Complete;
1869   }
1870 
1871   // Returns true if \p Use may read from \p DefLoc.
1872   bool isReadClobber(const MemoryLocation &DefLoc, Instruction *UseInst) {
1873     if (isNoopIntrinsic(UseInst))
1874       return false;
1875 
1876     // Monotonic or weaker atomic stores can be re-ordered and do not need to be
1877     // treated as read clobber.
1878     if (auto SI = dyn_cast<StoreInst>(UseInst))
1879       return isStrongerThan(SI->getOrdering(), AtomicOrdering::Monotonic);
1880 
1881     if (!UseInst->mayReadFromMemory())
1882       return false;
1883 
1884     if (auto *CB = dyn_cast<CallBase>(UseInst))
1885       if (CB->onlyAccessesInaccessibleMemory())
1886         return false;
1887 
1888     // NOTE: For calls, the number of stores removed could be slightly improved
1889     // by using AA.callCapturesBefore(UseInst, DefLoc, &DT), but that showed to
1890     // be expensive compared to the benefits in practice. For now, avoid more
1891     // expensive analysis to limit compile-time.
1892     return isRefSet(BatchAA.getModRefInfo(UseInst, DefLoc));
1893   }
1894 
1895   /// Returns true if \p Ptr is guaranteed to be loop invariant for any possible
1896   /// loop. In particular, this guarantees that it only references a single
1897   /// MemoryLocation during execution of the containing function.
1898   bool IsGuaranteedLoopInvariant(Value *Ptr) {
1899     auto IsGuaranteedLoopInvariantBase = [this](Value *Ptr) {
1900       Ptr = Ptr->stripPointerCasts();
1901       if (auto *I = dyn_cast<Instruction>(Ptr)) {
1902         if (isa<AllocaInst>(Ptr))
1903           return true;
1904 
1905         if (isAllocLikeFn(I, &TLI))
1906           return true;
1907 
1908         return false;
1909       }
1910       return true;
1911     };
1912 
1913     Ptr = Ptr->stripPointerCasts();
1914     if (auto *I = dyn_cast<Instruction>(Ptr)) {
1915       if (I->getParent() == &I->getFunction()->getEntryBlock()) {
1916         return true;
1917       }
1918     }
1919     if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
1920       return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) &&
1921              GEP->hasAllConstantIndices();
1922     }
1923     return IsGuaranteedLoopInvariantBase(Ptr);
1924   }
1925 
1926   // Find a MemoryDef writing to \p DefLoc and dominating \p StartAccess, with
1927   // no read access between them or on any other path to a function exit block
1928   // if \p DefLoc is not accessible after the function returns. If there is no
1929   // such MemoryDef, return None. The returned value may not (completely)
1930   // overwrite \p DefLoc. Currently we bail out when we encounter an aliasing
1931   // MemoryUse (read).
1932   Optional<MemoryAccess *>
1933   getDomMemoryDef(MemoryDef *KillingDef, MemoryAccess *StartAccess,
1934                   const MemoryLocation &DefLoc, const Value *DefUO,
1935                   unsigned &ScanLimit, unsigned &WalkerStepLimit,
1936                   bool IsMemTerm, unsigned &PartialLimit) {
1937     if (ScanLimit == 0 || WalkerStepLimit == 0) {
1938       LLVM_DEBUG(dbgs() << "\n    ...  hit scan limit\n");
1939       return None;
1940     }
1941 
1942     MemoryAccess *Current = StartAccess;
1943     Instruction *KillingI = KillingDef->getMemoryInst();
1944     bool StepAgain;
1945     LLVM_DEBUG(dbgs() << "  trying to get dominating access\n");
1946 
1947     // Find the next clobbering Mod access for DefLoc, starting at StartAccess.
1948     Optional<MemoryLocation> CurrentLoc;
1949     do {
1950       StepAgain = false;
1951       LLVM_DEBUG({
1952         dbgs() << "   visiting " << *Current;
1953         if (!MSSA.isLiveOnEntryDef(Current) && isa<MemoryUseOrDef>(Current))
1954           dbgs() << " (" << *cast<MemoryUseOrDef>(Current)->getMemoryInst()
1955                  << ")";
1956         dbgs() << "\n";
1957       });
1958 
1959       // Reached TOP.
1960       if (MSSA.isLiveOnEntryDef(Current)) {
1961         LLVM_DEBUG(dbgs() << "   ...  found LiveOnEntryDef\n");
1962         return None;
1963       }
1964 
1965       // Cost of a step. Accesses in the same block are more likely to be valid
1966       // candidates for elimination, hence consider them cheaper.
1967       unsigned StepCost = KillingDef->getBlock() == Current->getBlock()
1968                               ? MemorySSASameBBStepCost
1969                               : MemorySSAOtherBBStepCost;
1970       if (WalkerStepLimit <= StepCost) {
1971         LLVM_DEBUG(dbgs() << "   ...  hit walker step limit\n");
1972         return None;
1973       }
1974       WalkerStepLimit -= StepCost;
1975 
1976       // Return for MemoryPhis. They cannot be eliminated directly and the
1977       // caller is responsible for traversing them.
1978       if (isa<MemoryPhi>(Current)) {
1979         LLVM_DEBUG(dbgs() << "   ...  found MemoryPhi\n");
1980         return Current;
1981       }
1982 
1983       // Below, check if CurrentDef is a valid candidate to be eliminated by
1984       // KillingDef. If it is not, check the next candidate.
1985       MemoryDef *CurrentDef = cast<MemoryDef>(Current);
1986       Instruction *CurrentI = CurrentDef->getMemoryInst();
1987 
1988       if (canSkipDef(CurrentDef, !isInvisibleToCallerBeforeRet(DefUO))) {
1989         StepAgain = true;
1990         Current = CurrentDef->getDefiningAccess();
1991         continue;
1992       }
1993 
1994       // Before we try to remove anything, check for any extra throwing
1995       // instructions that block us from DSEing
1996       if (mayThrowBetween(KillingI, CurrentI, DefUO)) {
1997         LLVM_DEBUG(dbgs() << "  ... skip, may throw!\n");
1998         return None;
1999       }
2000 
2001       // Check for anything that looks like it will be a barrier to further
2002       // removal
2003       if (isDSEBarrier(DefUO, CurrentI)) {
2004         LLVM_DEBUG(dbgs() << "  ... skip, barrier\n");
2005         return None;
2006       }
2007 
2008       // If Current is known to be on path that reads DefLoc or is a read
2009       // clobber, bail out, as the path is not profitable. We skip this check
2010       // for intrinsic calls, because the code knows how to handle memcpy
2011       // intrinsics.
2012       if (!isa<IntrinsicInst>(CurrentI) && isReadClobber(DefLoc, CurrentI))
2013         return None;
2014 
2015       // Quick check if there are direct uses that are read-clobbers.
2016       if (any_of(Current->uses(), [this, &DefLoc, StartAccess](Use &U) {
2017             if (auto *UseOrDef = dyn_cast<MemoryUseOrDef>(U.getUser()))
2018               return !MSSA.dominates(StartAccess, UseOrDef) &&
2019                      isReadClobber(DefLoc, UseOrDef->getMemoryInst());
2020             return false;
2021           })) {
2022         LLVM_DEBUG(dbgs() << "   ...  found a read clobber\n");
2023         return None;
2024       }
2025 
2026       // If Current cannot be analyzed or is not removable, check the next
2027       // candidate.
2028       if (!hasAnalyzableMemoryWrite(CurrentI, TLI) || !isRemovable(CurrentI)) {
2029         StepAgain = true;
2030         Current = CurrentDef->getDefiningAccess();
2031         continue;
2032       }
2033 
2034       // If Current does not have an analyzable write location, skip it
2035       CurrentLoc = getLocForWriteEx(CurrentI);
2036       if (!CurrentLoc) {
2037         StepAgain = true;
2038         Current = CurrentDef->getDefiningAccess();
2039         continue;
2040       }
2041 
2042       // AliasAnalysis does not account for loops. Limit elimination to
2043       // candidates for which we can guarantee they always store to the same
2044       // memory location and not multiple locations in a loop.
2045       if (Current->getBlock() != KillingDef->getBlock() &&
2046           !IsGuaranteedLoopInvariant(const_cast<Value *>(CurrentLoc->Ptr))) {
2047         StepAgain = true;
2048         Current = CurrentDef->getDefiningAccess();
2049         WalkerStepLimit -= 1;
2050         continue;
2051       }
2052 
2053       if (IsMemTerm) {
2054         // If the killing def is a memory terminator (e.g. lifetime.end), check
2055         // the next candidate if the current Current does not write the same
2056         // underlying object as the terminator.
2057         if (!isMemTerminator(*CurrentLoc, CurrentI, KillingI)) {
2058           StepAgain = true;
2059           Current = CurrentDef->getDefiningAccess();
2060         }
2061         continue;
2062       } else {
2063         int64_t InstWriteOffset, DepWriteOffset;
2064         auto OR = isOverwrite(KillingI, CurrentI, DefLoc, *CurrentLoc, DL, TLI,
2065                               DepWriteOffset, InstWriteOffset, BatchAA, &F);
2066         // If Current does not write to the same object as KillingDef, check
2067         // the next candidate.
2068         if (OR == OW_Unknown) {
2069           StepAgain = true;
2070           Current = CurrentDef->getDefiningAccess();
2071         } else if (OR == OW_MaybePartial) {
2072           // If KillingDef only partially overwrites Current, check the next
2073           // candidate if the partial step limit is exceeded. This aggressively
2074           // limits the number of candidates for partial store elimination,
2075           // which are less likely to be removable in the end.
2076           if (PartialLimit <= 1) {
2077             StepAgain = true;
2078             Current = CurrentDef->getDefiningAccess();
2079             WalkerStepLimit -= 1;
2080             continue;
2081           }
2082           PartialLimit -= 1;
2083         }
2084       }
2085     } while (StepAgain);
2086 
2087     // Accesses to objects accessible after the function returns can only be
2088     // eliminated if the access is killed along all paths to the exit. Collect
2089     // the blocks with killing (=completely overwriting MemoryDefs) and check if
2090     // they cover all paths from EarlierAccess to any function exit.
2091     SmallPtrSet<Instruction *, 16> KillingDefs;
2092     KillingDefs.insert(KillingDef->getMemoryInst());
2093     MemoryAccess *EarlierAccess = Current;
2094     Instruction *EarlierMemInst =
2095         cast<MemoryDef>(EarlierAccess)->getMemoryInst();
2096     LLVM_DEBUG(dbgs() << "  Checking for reads of " << *EarlierAccess << " ("
2097                       << *EarlierMemInst << ")\n");
2098 
2099     SmallSetVector<MemoryAccess *, 32> WorkList;
2100     auto PushMemUses = [&WorkList](MemoryAccess *Acc) {
2101       for (Use &U : Acc->uses())
2102         WorkList.insert(cast<MemoryAccess>(U.getUser()));
2103     };
2104     PushMemUses(EarlierAccess);
2105 
2106     // Optimistically collect all accesses for reads. If we do not find any
2107     // read clobbers, add them to the cache.
2108     SmallPtrSet<MemoryAccess *, 16> KnownNoReads;
2109     if (!EarlierMemInst->mayReadFromMemory())
2110       KnownNoReads.insert(EarlierAccess);
2111     // Check if EarlierDef may be read.
2112     for (unsigned I = 0; I < WorkList.size(); I++) {
2113       MemoryAccess *UseAccess = WorkList[I];
2114 
2115       LLVM_DEBUG(dbgs() << "   " << *UseAccess);
2116       // Bail out if the number of accesses to check exceeds the scan limit.
2117       if (ScanLimit < (WorkList.size() - I)) {
2118         LLVM_DEBUG(dbgs() << "\n    ...  hit scan limit\n");
2119         return None;
2120       }
2121       --ScanLimit;
2122       NumDomMemDefChecks++;
2123       KnownNoReads.insert(UseAccess);
2124 
2125       if (isa<MemoryPhi>(UseAccess)) {
2126         if (any_of(KillingDefs, [this, UseAccess](Instruction *KI) {
2127               return DT.properlyDominates(KI->getParent(),
2128                                           UseAccess->getBlock());
2129             })) {
2130           LLVM_DEBUG(dbgs() << " ... skipping, dominated by killing block\n");
2131           continue;
2132         }
2133         LLVM_DEBUG(dbgs() << "\n    ... adding PHI uses\n");
2134         PushMemUses(UseAccess);
2135         continue;
2136       }
2137 
2138       Instruction *UseInst = cast<MemoryUseOrDef>(UseAccess)->getMemoryInst();
2139       LLVM_DEBUG(dbgs() << " (" << *UseInst << ")\n");
2140 
2141       if (any_of(KillingDefs, [this, UseInst](Instruction *KI) {
2142             return DT.dominates(KI, UseInst);
2143           })) {
2144         LLVM_DEBUG(dbgs() << " ... skipping, dominated by killing def\n");
2145         continue;
2146       }
2147 
2148       // A memory terminator kills all preceeding MemoryDefs and all succeeding
2149       // MemoryAccesses. We do not have to check it's users.
2150       if (isMemTerminator(*CurrentLoc, EarlierMemInst, UseInst)) {
2151         LLVM_DEBUG(
2152             dbgs()
2153             << " ... skipping, memterminator invalidates following accesses\n");
2154         continue;
2155       }
2156 
2157       if (isNoopIntrinsic(cast<MemoryUseOrDef>(UseAccess)->getMemoryInst())) {
2158         LLVM_DEBUG(dbgs() << "    ... adding uses of intrinsic\n");
2159         PushMemUses(UseAccess);
2160         continue;
2161       }
2162 
2163       if (UseInst->mayThrow() && !isInvisibleToCallerBeforeRet(DefUO)) {
2164         LLVM_DEBUG(dbgs() << "  ... found throwing instruction\n");
2165         return None;
2166       }
2167 
2168       // Uses which may read the original MemoryDef mean we cannot eliminate the
2169       // original MD. Stop walk.
2170       if (isReadClobber(*CurrentLoc, UseInst)) {
2171         LLVM_DEBUG(dbgs() << "    ... found read clobber\n");
2172         return None;
2173       }
2174 
2175       // For the KillingDef and EarlierAccess we only have to check if it reads
2176       // the memory location.
2177       // TODO: It would probably be better to check for self-reads before
2178       // calling the function.
2179       if (KillingDef == UseAccess || EarlierAccess == UseAccess) {
2180         LLVM_DEBUG(dbgs() << "    ... skipping killing def/dom access\n");
2181         continue;
2182       }
2183 
2184       // Check all uses for MemoryDefs, except for defs completely overwriting
2185       // the original location. Otherwise we have to check uses of *all*
2186       // MemoryDefs we discover, including non-aliasing ones. Otherwise we might
2187       // miss cases like the following
2188       //   1 = Def(LoE) ; <----- EarlierDef stores [0,1]
2189       //   2 = Def(1)   ; (2, 1) = NoAlias,   stores [2,3]
2190       //   Use(2)       ; MayAlias 2 *and* 1, loads [0, 3].
2191       //                  (The Use points to the *first* Def it may alias)
2192       //   3 = Def(1)   ; <---- Current  (3, 2) = NoAlias, (3,1) = MayAlias,
2193       //                  stores [0,1]
2194       if (MemoryDef *UseDef = dyn_cast<MemoryDef>(UseAccess)) {
2195         if (isCompleteOverwrite(*CurrentLoc, EarlierMemInst, UseInst)) {
2196           if (!isInvisibleToCallerAfterRet(DefUO) &&
2197               UseAccess != EarlierAccess) {
2198             BasicBlock *MaybeKillingBlock = UseInst->getParent();
2199             if (PostOrderNumbers.find(MaybeKillingBlock)->second <
2200                 PostOrderNumbers.find(EarlierAccess->getBlock())->second) {
2201 
2202               LLVM_DEBUG(dbgs()
2203                          << "    ... found killing def " << *UseInst << "\n");
2204               KillingDefs.insert(UseInst);
2205             }
2206           }
2207         } else
2208           PushMemUses(UseDef);
2209       }
2210     }
2211 
2212     // For accesses to locations visible after the function returns, make sure
2213     // that the location is killed (=overwritten) along all paths from
2214     // EarlierAccess to the exit.
2215     if (!isInvisibleToCallerAfterRet(DefUO)) {
2216       SmallPtrSet<BasicBlock *, 16> KillingBlocks;
2217       for (Instruction *KD : KillingDefs)
2218         KillingBlocks.insert(KD->getParent());
2219       assert(!KillingBlocks.empty() &&
2220              "Expected at least a single killing block");
2221 
2222       // Find the common post-dominator of all killing blocks.
2223       BasicBlock *CommonPred = *KillingBlocks.begin();
2224       for (auto I = std::next(KillingBlocks.begin()), E = KillingBlocks.end();
2225            I != E; I++) {
2226         if (!CommonPred)
2227           break;
2228         CommonPred = PDT.findNearestCommonDominator(CommonPred, *I);
2229       }
2230 
2231       // If CommonPred is in the set of killing blocks, just check if it
2232       // post-dominates EarlierAccess.
2233       if (KillingBlocks.count(CommonPred)) {
2234         if (PDT.dominates(CommonPred, EarlierAccess->getBlock()))
2235           return {EarlierAccess};
2236         return None;
2237       }
2238 
2239       // If the common post-dominator does not post-dominate EarlierAccess,
2240       // there is a path from EarlierAccess to an exit not going through a
2241       // killing block.
2242       if (PDT.dominates(CommonPred, EarlierAccess->getBlock())) {
2243         SetVector<BasicBlock *> WorkList;
2244 
2245         // If CommonPred is null, there are multiple exits from the function.
2246         // They all have to be added to the worklist.
2247         if (CommonPred)
2248           WorkList.insert(CommonPred);
2249         else
2250           for (BasicBlock *R : PDT.roots())
2251             WorkList.insert(R);
2252 
2253         NumCFGTries++;
2254         // Check if all paths starting from an exit node go through one of the
2255         // killing blocks before reaching EarlierAccess.
2256         for (unsigned I = 0; I < WorkList.size(); I++) {
2257           NumCFGChecks++;
2258           BasicBlock *Current = WorkList[I];
2259           if (KillingBlocks.count(Current))
2260             continue;
2261           if (Current == EarlierAccess->getBlock())
2262             return None;
2263 
2264           // EarlierAccess is reachable from the entry, so we don't have to
2265           // explore unreachable blocks further.
2266           if (!DT.isReachableFromEntry(Current))
2267             continue;
2268 
2269           for (BasicBlock *Pred : predecessors(Current))
2270             WorkList.insert(Pred);
2271 
2272           if (WorkList.size() >= MemorySSAPathCheckLimit)
2273             return None;
2274         }
2275         NumCFGSuccess++;
2276         return {EarlierAccess};
2277       }
2278       return None;
2279     }
2280 
2281     // No aliasing MemoryUses of EarlierAccess found, EarlierAccess is
2282     // potentially dead.
2283     return {EarlierAccess};
2284   }
2285 
2286   // Delete dead memory defs
2287   void deleteDeadInstruction(Instruction *SI) {
2288     MemorySSAUpdater Updater(&MSSA);
2289     SmallVector<Instruction *, 32> NowDeadInsts;
2290     NowDeadInsts.push_back(SI);
2291     --NumFastOther;
2292 
2293     while (!NowDeadInsts.empty()) {
2294       Instruction *DeadInst = NowDeadInsts.pop_back_val();
2295       ++NumFastOther;
2296 
2297       // Try to preserve debug information attached to the dead instruction.
2298       salvageDebugInfo(*DeadInst);
2299       salvageKnowledge(DeadInst);
2300 
2301       // Remove the Instruction from MSSA.
2302       if (MemoryAccess *MA = MSSA.getMemoryAccess(DeadInst)) {
2303         if (MemoryDef *MD = dyn_cast<MemoryDef>(MA)) {
2304           SkipStores.insert(MD);
2305         }
2306         Updater.removeMemoryAccess(MA);
2307       }
2308 
2309       auto I = IOLs.find(DeadInst->getParent());
2310       if (I != IOLs.end())
2311         I->second.erase(DeadInst);
2312       // Remove its operands
2313       for (Use &O : DeadInst->operands())
2314         if (Instruction *OpI = dyn_cast<Instruction>(O)) {
2315           O = nullptr;
2316           if (isInstructionTriviallyDead(OpI, &TLI))
2317             NowDeadInsts.push_back(OpI);
2318         }
2319 
2320       DeadInst->eraseFromParent();
2321     }
2322   }
2323 
2324   // Check for any extra throws between SI and NI that block DSE.  This only
2325   // checks extra maythrows (those that aren't MemoryDef's). MemoryDef that may
2326   // throw are handled during the walk from one def to the next.
2327   bool mayThrowBetween(Instruction *SI, Instruction *NI,
2328                        const Value *SILocUnd) {
2329     // First see if we can ignore it by using the fact that SI is an
2330     // alloca/alloca like object that is not visible to the caller during
2331     // execution of the function.
2332     if (SILocUnd && isInvisibleToCallerBeforeRet(SILocUnd))
2333       return false;
2334 
2335     if (SI->getParent() == NI->getParent())
2336       return ThrowingBlocks.count(SI->getParent());
2337     return !ThrowingBlocks.empty();
2338   }
2339 
2340   // Check if \p NI acts as a DSE barrier for \p SI. The following instructions
2341   // act as barriers:
2342   //  * A memory instruction that may throw and \p SI accesses a non-stack
2343   //  object.
2344   //  * Atomic stores stronger that monotonic.
2345   bool isDSEBarrier(const Value *SILocUnd, Instruction *NI) {
2346     // If NI may throw it acts as a barrier, unless we are to an alloca/alloca
2347     // like object that does not escape.
2348     if (NI->mayThrow() && !isInvisibleToCallerBeforeRet(SILocUnd))
2349       return true;
2350 
2351     // If NI is an atomic load/store stronger than monotonic, do not try to
2352     // eliminate/reorder it.
2353     if (NI->isAtomic()) {
2354       if (auto *LI = dyn_cast<LoadInst>(NI))
2355         return isStrongerThanMonotonic(LI->getOrdering());
2356       if (auto *SI = dyn_cast<StoreInst>(NI))
2357         return isStrongerThanMonotonic(SI->getOrdering());
2358       if (auto *ARMW = dyn_cast<AtomicRMWInst>(NI))
2359         return isStrongerThanMonotonic(ARMW->getOrdering());
2360       if (auto *CmpXchg = dyn_cast<AtomicCmpXchgInst>(NI))
2361         return isStrongerThanMonotonic(CmpXchg->getSuccessOrdering()) ||
2362                isStrongerThanMonotonic(CmpXchg->getFailureOrdering());
2363       llvm_unreachable("other instructions should be skipped in MemorySSA");
2364     }
2365     return false;
2366   }
2367 
2368   /// Eliminate writes to objects that are not visible in the caller and are not
2369   /// accessed before returning from the function.
2370   bool eliminateDeadWritesAtEndOfFunction() {
2371     bool MadeChange = false;
2372     LLVM_DEBUG(
2373         dbgs()
2374         << "Trying to eliminate MemoryDefs at the end of the function\n");
2375     for (int I = MemDefs.size() - 1; I >= 0; I--) {
2376       MemoryDef *Def = MemDefs[I];
2377       if (SkipStores.contains(Def) || !isRemovable(Def->getMemoryInst()))
2378         continue;
2379 
2380       Instruction *DefI = Def->getMemoryInst();
2381       SmallVector<const Value *, 4> Pointers;
2382       auto DefLoc = getLocForWriteEx(DefI);
2383       if (!DefLoc)
2384         continue;
2385 
2386       // NOTE: Currently eliminating writes at the end of a function is limited
2387       // to MemoryDefs with a single underlying object, to save compile-time. In
2388       // practice it appears the case with multiple underlying objects is very
2389       // uncommon. If it turns out to be important, we can use
2390       // getUnderlyingObjects here instead.
2391       const Value *UO = getUnderlyingObject(DefLoc->Ptr);
2392       if (!UO || !isInvisibleToCallerAfterRet(UO))
2393         continue;
2394 
2395       if (isWriteAtEndOfFunction(Def)) {
2396         // See through pointer-to-pointer bitcasts
2397         LLVM_DEBUG(dbgs() << "   ... MemoryDef is not accessed until the end "
2398                              "of the function\n");
2399         deleteDeadInstruction(DefI);
2400         ++NumFastStores;
2401         MadeChange = true;
2402       }
2403     }
2404     return MadeChange;
2405   }
2406 
2407   /// \returns true if \p Def is a no-op store, either because it
2408   /// directly stores back a loaded value or stores zero to a calloced object.
2409   bool storeIsNoop(MemoryDef *Def, const MemoryLocation &DefLoc,
2410                    const Value *DefUO) {
2411     StoreInst *Store = dyn_cast<StoreInst>(Def->getMemoryInst());
2412     if (!Store)
2413       return false;
2414 
2415     if (auto *LoadI = dyn_cast<LoadInst>(Store->getOperand(0))) {
2416       if (LoadI->getPointerOperand() == Store->getOperand(1)) {
2417         // Get the defining access for the load.
2418         auto *LoadAccess = MSSA.getMemoryAccess(LoadI)->getDefiningAccess();
2419         // Fast path: the defining accesses are the same.
2420         if (LoadAccess == Def->getDefiningAccess())
2421           return true;
2422 
2423         // Look through phi accesses. Recursively scan all phi accesses by
2424         // adding them to a worklist. Bail when we run into a memory def that
2425         // does not match LoadAccess.
2426         SetVector<MemoryAccess *> ToCheck;
2427         MemoryAccess *Current =
2428             MSSA.getWalker()->getClobberingMemoryAccess(Def);
2429         // We don't want to bail when we run into the store memory def. But,
2430         // the phi access may point to it. So, pretend like we've already
2431         // checked it.
2432         ToCheck.insert(Def);
2433         ToCheck.insert(Current);
2434         // Start at current (1) to simulate already having checked Def.
2435         for (unsigned I = 1; I < ToCheck.size(); ++I) {
2436           Current = ToCheck[I];
2437           if (auto PhiAccess = dyn_cast<MemoryPhi>(Current)) {
2438             // Check all the operands.
2439             for (auto &Use : PhiAccess->incoming_values())
2440               ToCheck.insert(cast<MemoryAccess>(&Use));
2441             continue;
2442           }
2443 
2444           // If we found a memory def, bail. This happens when we have an
2445           // unrelated write in between an otherwise noop store.
2446           assert(isa<MemoryDef>(Current) &&
2447                  "Only MemoryDefs should reach here.");
2448           // TODO: Skip no alias MemoryDefs that have no aliasing reads.
2449           // We are searching for the definition of the store's destination.
2450           // So, if that is the same definition as the load, then this is a
2451           // noop. Otherwise, fail.
2452           if (LoadAccess != Current)
2453             return false;
2454         }
2455         return true;
2456       }
2457     }
2458 
2459     Constant *StoredConstant = dyn_cast<Constant>(Store->getOperand(0));
2460     if (StoredConstant && StoredConstant->isNullValue()) {
2461       auto *DefUOInst = dyn_cast<Instruction>(DefUO);
2462       if (DefUOInst && isCallocLikeFn(DefUOInst, &TLI)) {
2463         auto *UnderlyingDef = cast<MemoryDef>(MSSA.getMemoryAccess(DefUOInst));
2464         // If UnderlyingDef is the clobbering access of Def, no instructions
2465         // between them can modify the memory location.
2466         auto *ClobberDef =
2467             MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def);
2468         return UnderlyingDef == ClobberDef;
2469       }
2470     }
2471     return false;
2472   }
2473 };
2474 
2475 bool eliminateDeadStoresMemorySSA(Function &F, AliasAnalysis &AA,
2476                                   MemorySSA &MSSA, DominatorTree &DT,
2477                                   PostDominatorTree &PDT,
2478                                   const TargetLibraryInfo &TLI) {
2479   bool MadeChange = false;
2480 
2481   DSEState State = DSEState::get(F, AA, MSSA, DT, PDT, TLI);
2482   // For each store:
2483   for (unsigned I = 0; I < State.MemDefs.size(); I++) {
2484     MemoryDef *KillingDef = State.MemDefs[I];
2485     if (State.SkipStores.count(KillingDef))
2486       continue;
2487     Instruction *SI = KillingDef->getMemoryInst();
2488 
2489     Optional<MemoryLocation> MaybeSILoc;
2490     if (State.isMemTerminatorInst(SI))
2491       MaybeSILoc = State.getLocForTerminator(SI).map(
2492           [](const std::pair<MemoryLocation, bool> &P) { return P.first; });
2493     else
2494       MaybeSILoc = State.getLocForWriteEx(SI);
2495 
2496     if (!MaybeSILoc) {
2497       LLVM_DEBUG(dbgs() << "Failed to find analyzable write location for "
2498                         << *SI << "\n");
2499       continue;
2500     }
2501     MemoryLocation SILoc = *MaybeSILoc;
2502     assert(SILoc.Ptr && "SILoc should not be null");
2503     const Value *SILocUnd = getUnderlyingObject(SILoc.Ptr);
2504 
2505     MemoryAccess *Current = KillingDef;
2506     LLVM_DEBUG(dbgs() << "Trying to eliminate MemoryDefs killed by "
2507                       << *Current << " (" << *SI << ")\n");
2508 
2509     unsigned ScanLimit = MemorySSAScanLimit;
2510     unsigned WalkerStepLimit = MemorySSAUpwardsStepLimit;
2511     unsigned PartialLimit = MemorySSAPartialStoreLimit;
2512     // Worklist of MemoryAccesses that may be killed by KillingDef.
2513     SetVector<MemoryAccess *> ToCheck;
2514 
2515     if (SILocUnd)
2516       ToCheck.insert(KillingDef->getDefiningAccess());
2517 
2518     bool Shortend = false;
2519     bool IsMemTerm = State.isMemTerminatorInst(SI);
2520     // Check if MemoryAccesses in the worklist are killed by KillingDef.
2521     for (unsigned I = 0; I < ToCheck.size(); I++) {
2522       Current = ToCheck[I];
2523       if (State.SkipStores.count(Current))
2524         continue;
2525 
2526       Optional<MemoryAccess *> Next = State.getDomMemoryDef(
2527           KillingDef, Current, SILoc, SILocUnd, ScanLimit, WalkerStepLimit,
2528           IsMemTerm, PartialLimit);
2529 
2530       if (!Next) {
2531         LLVM_DEBUG(dbgs() << "  finished walk\n");
2532         continue;
2533       }
2534 
2535       MemoryAccess *EarlierAccess = *Next;
2536       LLVM_DEBUG(dbgs() << " Checking if we can kill " << *EarlierAccess);
2537       if (isa<MemoryPhi>(EarlierAccess)) {
2538         LLVM_DEBUG(dbgs() << "\n  ... adding incoming values to worklist\n");
2539         for (Value *V : cast<MemoryPhi>(EarlierAccess)->incoming_values()) {
2540           MemoryAccess *IncomingAccess = cast<MemoryAccess>(V);
2541           BasicBlock *IncomingBlock = IncomingAccess->getBlock();
2542           BasicBlock *PhiBlock = EarlierAccess->getBlock();
2543 
2544           // We only consider incoming MemoryAccesses that come before the
2545           // MemoryPhi. Otherwise we could discover candidates that do not
2546           // strictly dominate our starting def.
2547           if (State.PostOrderNumbers[IncomingBlock] >
2548               State.PostOrderNumbers[PhiBlock])
2549             ToCheck.insert(IncomingAccess);
2550         }
2551         continue;
2552       }
2553       auto *NextDef = cast<MemoryDef>(EarlierAccess);
2554       Instruction *NI = NextDef->getMemoryInst();
2555       LLVM_DEBUG(dbgs() << " (" << *NI << ")\n");
2556       ToCheck.insert(NextDef->getDefiningAccess());
2557       NumGetDomMemoryDefPassed++;
2558 
2559       if (!DebugCounter::shouldExecute(MemorySSACounter))
2560         continue;
2561 
2562       MemoryLocation NILoc = *State.getLocForWriteEx(NI);
2563 
2564       if (IsMemTerm) {
2565         const Value *NIUnd = getUnderlyingObject(NILoc.Ptr);
2566         if (SILocUnd != NIUnd)
2567           continue;
2568         LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n  DEAD: " << *NI
2569                           << "\n  KILLER: " << *SI << '\n');
2570         State.deleteDeadInstruction(NI);
2571         ++NumFastStores;
2572         MadeChange = true;
2573       } else {
2574         // Check if NI overwrites SI.
2575         int64_t InstWriteOffset, DepWriteOffset;
2576         OverwriteResult OR =
2577             isOverwrite(SI, NI, SILoc, NILoc, State.DL, TLI, DepWriteOffset,
2578                         InstWriteOffset, State.BatchAA, &F);
2579         if (OR == OW_MaybePartial) {
2580           auto Iter = State.IOLs.insert(
2581               std::make_pair<BasicBlock *, InstOverlapIntervalsTy>(
2582                   NI->getParent(), InstOverlapIntervalsTy()));
2583           auto &IOL = Iter.first->second;
2584           OR = isPartialOverwrite(SILoc, NILoc, DepWriteOffset, InstWriteOffset,
2585                                   NI, IOL);
2586         }
2587 
2588         if (EnablePartialStoreMerging && OR == OW_PartialEarlierWithFullLater) {
2589           auto *Earlier = dyn_cast<StoreInst>(NI);
2590           auto *Later = dyn_cast<StoreInst>(SI);
2591           // We are re-using tryToMergePartialOverlappingStores, which requires
2592           // Earlier to domiante Later.
2593           // TODO: implement tryToMergeParialOverlappingStores using MemorySSA.
2594           if (Earlier && Later && DT.dominates(Earlier, Later)) {
2595             if (Constant *Merged = tryToMergePartialOverlappingStores(
2596                     Earlier, Later, InstWriteOffset, DepWriteOffset, State.DL,
2597                     State.BatchAA, &DT)) {
2598 
2599               // Update stored value of earlier store to merged constant.
2600               Earlier->setOperand(0, Merged);
2601               ++NumModifiedStores;
2602               MadeChange = true;
2603 
2604               Shortend = true;
2605               // Remove later store and remove any outstanding overlap intervals
2606               // for the updated store.
2607               State.deleteDeadInstruction(Later);
2608               auto I = State.IOLs.find(Earlier->getParent());
2609               if (I != State.IOLs.end())
2610                 I->second.erase(Earlier);
2611               break;
2612             }
2613           }
2614         }
2615 
2616         if (OR == OW_Complete) {
2617           LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n  DEAD: " << *NI
2618                             << "\n  KILLER: " << *SI << '\n');
2619           State.deleteDeadInstruction(NI);
2620           ++NumFastStores;
2621           MadeChange = true;
2622         }
2623       }
2624     }
2625 
2626     // Check if the store is a no-op.
2627     if (!Shortend && isRemovable(SI) &&
2628         State.storeIsNoop(KillingDef, SILoc, SILocUnd)) {
2629       LLVM_DEBUG(dbgs() << "DSE: Remove No-Op Store:\n  DEAD: " << *SI << '\n');
2630       State.deleteDeadInstruction(SI);
2631       NumRedundantStores++;
2632       MadeChange = true;
2633       continue;
2634     }
2635   }
2636 
2637   if (EnablePartialOverwriteTracking)
2638     for (auto &KV : State.IOLs)
2639       MadeChange |= removePartiallyOverlappedStores(State.DL, KV.second, TLI);
2640 
2641   MadeChange |= State.eliminateDeadWritesAtEndOfFunction();
2642   return MadeChange;
2643 }
2644 } // end anonymous namespace
2645 
2646 //===----------------------------------------------------------------------===//
2647 // DSE Pass
2648 //===----------------------------------------------------------------------===//
2649 PreservedAnalyses DSEPass::run(Function &F, FunctionAnalysisManager &AM) {
2650   AliasAnalysis &AA = AM.getResult<AAManager>(F);
2651   const TargetLibraryInfo &TLI = AM.getResult<TargetLibraryAnalysis>(F);
2652   DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
2653 
2654   bool Changed = false;
2655   if (EnableMemorySSA) {
2656     MemorySSA &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
2657     PostDominatorTree &PDT = AM.getResult<PostDominatorTreeAnalysis>(F);
2658 
2659     Changed = eliminateDeadStoresMemorySSA(F, AA, MSSA, DT, PDT, TLI);
2660   } else {
2661     MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F);
2662 
2663     Changed = eliminateDeadStores(F, &AA, &MD, &DT, &TLI);
2664   }
2665 
2666 #ifdef LLVM_ENABLE_STATS
2667   if (AreStatisticsEnabled())
2668     for (auto &I : instructions(F))
2669       NumRemainingStores += isa<StoreInst>(&I);
2670 #endif
2671 
2672   if (!Changed)
2673     return PreservedAnalyses::all();
2674 
2675   PreservedAnalyses PA;
2676   PA.preserveSet<CFGAnalyses>();
2677   PA.preserve<GlobalsAA>();
2678   if (EnableMemorySSA)
2679     PA.preserve<MemorySSAAnalysis>();
2680   else
2681     PA.preserve<MemoryDependenceAnalysis>();
2682   return PA;
2683 }
2684 
2685 namespace {
2686 
2687 /// A legacy pass for the legacy pass manager that wraps \c DSEPass.
2688 class DSELegacyPass : public FunctionPass {
2689 public:
2690   static char ID; // Pass identification, replacement for typeid
2691 
2692   DSELegacyPass() : FunctionPass(ID) {
2693     initializeDSELegacyPassPass(*PassRegistry::getPassRegistry());
2694   }
2695 
2696   bool runOnFunction(Function &F) override {
2697     if (skipFunction(F))
2698       return false;
2699 
2700     AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
2701     DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2702     const TargetLibraryInfo &TLI =
2703         getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
2704 
2705     bool Changed = false;
2706     if (EnableMemorySSA) {
2707       MemorySSA &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA();
2708       PostDominatorTree &PDT =
2709           getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
2710 
2711       Changed = eliminateDeadStoresMemorySSA(F, AA, MSSA, DT, PDT, TLI);
2712     } else {
2713       MemoryDependenceResults &MD =
2714           getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
2715 
2716       Changed = eliminateDeadStores(F, &AA, &MD, &DT, &TLI);
2717     }
2718 
2719 #ifdef LLVM_ENABLE_STATS
2720     if (AreStatisticsEnabled())
2721       for (auto &I : instructions(F))
2722         NumRemainingStores += isa<StoreInst>(&I);
2723 #endif
2724 
2725     return Changed;
2726   }
2727 
2728   void getAnalysisUsage(AnalysisUsage &AU) const override {
2729     AU.setPreservesCFG();
2730     AU.addRequired<AAResultsWrapperPass>();
2731     AU.addRequired<TargetLibraryInfoWrapperPass>();
2732     AU.addPreserved<GlobalsAAWrapperPass>();
2733     AU.addRequired<DominatorTreeWrapperPass>();
2734     AU.addPreserved<DominatorTreeWrapperPass>();
2735 
2736     if (EnableMemorySSA) {
2737       AU.addRequired<PostDominatorTreeWrapperPass>();
2738       AU.addRequired<MemorySSAWrapperPass>();
2739       AU.addPreserved<PostDominatorTreeWrapperPass>();
2740       AU.addPreserved<MemorySSAWrapperPass>();
2741     } else {
2742       AU.addRequired<MemoryDependenceWrapperPass>();
2743       AU.addPreserved<MemoryDependenceWrapperPass>();
2744     }
2745   }
2746 };
2747 
2748 } // end anonymous namespace
2749 
2750 char DSELegacyPass::ID = 0;
2751 
2752 INITIALIZE_PASS_BEGIN(DSELegacyPass, "dse", "Dead Store Elimination", false,
2753                       false)
2754 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
2755 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
2756 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
2757 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
2758 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
2759 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
2760 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2761 INITIALIZE_PASS_END(DSELegacyPass, "dse", "Dead Store Elimination", false,
2762                     false)
2763 
2764 FunctionPass *llvm::createDeadStoreEliminationPass() {
2765   return new DSELegacyPass();
2766 }
2767