xref: /freebsd-src/contrib/llvm-project/llvm/lib/Analysis/MemorySSA.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1 //===- MemorySSA.cpp - Memory SSA Builder ---------------------------------===//
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 the MemorySSA class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Analysis/MemorySSA.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseMapInfo.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/DepthFirstIterator.h"
18 #include "llvm/ADT/Hashing.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/iterator.h"
25 #include "llvm/ADT/iterator_range.h"
26 #include "llvm/Analysis/AliasAnalysis.h"
27 #include "llvm/Analysis/CFGPrinter.h"
28 #include "llvm/Analysis/IteratedDominanceFrontier.h"
29 #include "llvm/Analysis/MemoryLocation.h"
30 #include "llvm/Config/llvm-config.h"
31 #include "llvm/IR/AssemblyAnnotationWriter.h"
32 #include "llvm/IR/BasicBlock.h"
33 #include "llvm/IR/Dominators.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/Instruction.h"
36 #include "llvm/IR/Instructions.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/Intrinsics.h"
39 #include "llvm/IR/LLVMContext.h"
40 #include "llvm/IR/PassManager.h"
41 #include "llvm/IR/Use.h"
42 #include "llvm/InitializePasses.h"
43 #include "llvm/Pass.h"
44 #include "llvm/Support/AtomicOrdering.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/CommandLine.h"
47 #include "llvm/Support/Compiler.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/ErrorHandling.h"
50 #include "llvm/Support/FormattedStream.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include <algorithm>
53 #include <cassert>
54 #include <cstdlib>
55 #include <iterator>
56 #include <memory>
57 #include <utility>
58 
59 using namespace llvm;
60 
61 #define DEBUG_TYPE "memoryssa"
62 
63 static cl::opt<std::string>
64     DotCFGMSSA("dot-cfg-mssa",
65                cl::value_desc("file name for generated dot file"),
66                cl::desc("file name for generated dot file"), cl::init(""));
67 
68 INITIALIZE_PASS_BEGIN(MemorySSAWrapperPass, "memoryssa", "Memory SSA", false,
69                       true)
70 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
71 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
72 INITIALIZE_PASS_END(MemorySSAWrapperPass, "memoryssa", "Memory SSA", false,
73                     true)
74 
75 INITIALIZE_PASS_BEGIN(MemorySSAPrinterLegacyPass, "print-memoryssa",
76                       "Memory SSA Printer", false, false)
77 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
78 INITIALIZE_PASS_END(MemorySSAPrinterLegacyPass, "print-memoryssa",
79                     "Memory SSA Printer", false, false)
80 
81 static cl::opt<unsigned> MaxCheckLimit(
82     "memssa-check-limit", cl::Hidden, cl::init(100),
83     cl::desc("The maximum number of stores/phis MemorySSA"
84              "will consider trying to walk past (default = 100)"));
85 
86 // Always verify MemorySSA if expensive checking is enabled.
87 #ifdef EXPENSIVE_CHECKS
88 bool llvm::VerifyMemorySSA = true;
89 #else
90 bool llvm::VerifyMemorySSA = false;
91 #endif
92 /// Enables memory ssa as a dependency for loop passes in legacy pass manager.
93 cl::opt<bool> llvm::EnableMSSALoopDependency(
94     "enable-mssa-loop-dependency", cl::Hidden, cl::init(true),
95     cl::desc("Enable MemorySSA dependency for loop pass manager"));
96 
97 static cl::opt<bool, true>
98     VerifyMemorySSAX("verify-memoryssa", cl::location(VerifyMemorySSA),
99                      cl::Hidden, cl::desc("Enable verification of MemorySSA."));
100 
101 namespace llvm {
102 
103 /// An assembly annotator class to print Memory SSA information in
104 /// comments.
105 class MemorySSAAnnotatedWriter : public AssemblyAnnotationWriter {
106   friend class MemorySSA;
107 
108   const MemorySSA *MSSA;
109 
110 public:
111   MemorySSAAnnotatedWriter(const MemorySSA *M) : MSSA(M) {}
112 
113   void emitBasicBlockStartAnnot(const BasicBlock *BB,
114                                 formatted_raw_ostream &OS) override {
115     if (MemoryAccess *MA = MSSA->getMemoryAccess(BB))
116       OS << "; " << *MA << "\n";
117   }
118 
119   void emitInstructionAnnot(const Instruction *I,
120                             formatted_raw_ostream &OS) override {
121     if (MemoryAccess *MA = MSSA->getMemoryAccess(I))
122       OS << "; " << *MA << "\n";
123   }
124 };
125 
126 } // end namespace llvm
127 
128 namespace {
129 
130 /// Our current alias analysis API differentiates heavily between calls and
131 /// non-calls, and functions called on one usually assert on the other.
132 /// This class encapsulates the distinction to simplify other code that wants
133 /// "Memory affecting instructions and related data" to use as a key.
134 /// For example, this class is used as a densemap key in the use optimizer.
135 class MemoryLocOrCall {
136 public:
137   bool IsCall = false;
138 
139   MemoryLocOrCall(MemoryUseOrDef *MUD)
140       : MemoryLocOrCall(MUD->getMemoryInst()) {}
141   MemoryLocOrCall(const MemoryUseOrDef *MUD)
142       : MemoryLocOrCall(MUD->getMemoryInst()) {}
143 
144   MemoryLocOrCall(Instruction *Inst) {
145     if (auto *C = dyn_cast<CallBase>(Inst)) {
146       IsCall = true;
147       Call = C;
148     } else {
149       IsCall = false;
150       // There is no such thing as a memorylocation for a fence inst, and it is
151       // unique in that regard.
152       if (!isa<FenceInst>(Inst))
153         Loc = MemoryLocation::get(Inst);
154     }
155   }
156 
157   explicit MemoryLocOrCall(const MemoryLocation &Loc) : Loc(Loc) {}
158 
159   const CallBase *getCall() const {
160     assert(IsCall);
161     return Call;
162   }
163 
164   MemoryLocation getLoc() const {
165     assert(!IsCall);
166     return Loc;
167   }
168 
169   bool operator==(const MemoryLocOrCall &Other) const {
170     if (IsCall != Other.IsCall)
171       return false;
172 
173     if (!IsCall)
174       return Loc == Other.Loc;
175 
176     if (Call->getCalledOperand() != Other.Call->getCalledOperand())
177       return false;
178 
179     return Call->arg_size() == Other.Call->arg_size() &&
180            std::equal(Call->arg_begin(), Call->arg_end(),
181                       Other.Call->arg_begin());
182   }
183 
184 private:
185   union {
186     const CallBase *Call;
187     MemoryLocation Loc;
188   };
189 };
190 
191 } // end anonymous namespace
192 
193 namespace llvm {
194 
195 template <> struct DenseMapInfo<MemoryLocOrCall> {
196   static inline MemoryLocOrCall getEmptyKey() {
197     return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getEmptyKey());
198   }
199 
200   static inline MemoryLocOrCall getTombstoneKey() {
201     return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getTombstoneKey());
202   }
203 
204   static unsigned getHashValue(const MemoryLocOrCall &MLOC) {
205     if (!MLOC.IsCall)
206       return hash_combine(
207           MLOC.IsCall,
208           DenseMapInfo<MemoryLocation>::getHashValue(MLOC.getLoc()));
209 
210     hash_code hash =
211         hash_combine(MLOC.IsCall, DenseMapInfo<const Value *>::getHashValue(
212                                       MLOC.getCall()->getCalledOperand()));
213 
214     for (const Value *Arg : MLOC.getCall()->args())
215       hash = hash_combine(hash, DenseMapInfo<const Value *>::getHashValue(Arg));
216     return hash;
217   }
218 
219   static bool isEqual(const MemoryLocOrCall &LHS, const MemoryLocOrCall &RHS) {
220     return LHS == RHS;
221   }
222 };
223 
224 } // end namespace llvm
225 
226 /// This does one-way checks to see if Use could theoretically be hoisted above
227 /// MayClobber. This will not check the other way around.
228 ///
229 /// This assumes that, for the purposes of MemorySSA, Use comes directly after
230 /// MayClobber, with no potentially clobbering operations in between them.
231 /// (Where potentially clobbering ops are memory barriers, aliased stores, etc.)
232 static bool areLoadsReorderable(const LoadInst *Use,
233                                 const LoadInst *MayClobber) {
234   bool VolatileUse = Use->isVolatile();
235   bool VolatileClobber = MayClobber->isVolatile();
236   // Volatile operations may never be reordered with other volatile operations.
237   if (VolatileUse && VolatileClobber)
238     return false;
239   // Otherwise, volatile doesn't matter here. From the language reference:
240   // 'optimizers may change the order of volatile operations relative to
241   // non-volatile operations.'"
242 
243   // If a load is seq_cst, it cannot be moved above other loads. If its ordering
244   // is weaker, it can be moved above other loads. We just need to be sure that
245   // MayClobber isn't an acquire load, because loads can't be moved above
246   // acquire loads.
247   //
248   // Note that this explicitly *does* allow the free reordering of monotonic (or
249   // weaker) loads of the same address.
250   bool SeqCstUse = Use->getOrdering() == AtomicOrdering::SequentiallyConsistent;
251   bool MayClobberIsAcquire = isAtLeastOrStrongerThan(MayClobber->getOrdering(),
252                                                      AtomicOrdering::Acquire);
253   return !(SeqCstUse || MayClobberIsAcquire);
254 }
255 
256 namespace {
257 
258 struct ClobberAlias {
259   bool IsClobber;
260   Optional<AliasResult> AR;
261 };
262 
263 } // end anonymous namespace
264 
265 // Return a pair of {IsClobber (bool), AR (AliasResult)}. It relies on AR being
266 // ignored if IsClobber = false.
267 template <typename AliasAnalysisType>
268 static ClobberAlias
269 instructionClobbersQuery(const MemoryDef *MD, const MemoryLocation &UseLoc,
270                          const Instruction *UseInst, AliasAnalysisType &AA) {
271   Instruction *DefInst = MD->getMemoryInst();
272   assert(DefInst && "Defining instruction not actually an instruction");
273   Optional<AliasResult> AR;
274 
275   if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(DefInst)) {
276     // These intrinsics will show up as affecting memory, but they are just
277     // markers, mostly.
278     //
279     // FIXME: We probably don't actually want MemorySSA to model these at all
280     // (including creating MemoryAccesses for them): we just end up inventing
281     // clobbers where they don't really exist at all. Please see D43269 for
282     // context.
283     switch (II->getIntrinsicID()) {
284     case Intrinsic::lifetime_end:
285     case Intrinsic::invariant_start:
286     case Intrinsic::invariant_end:
287     case Intrinsic::assume:
288     case Intrinsic::experimental_noalias_scope_decl:
289       return {false, NoAlias};
290     case Intrinsic::dbg_addr:
291     case Intrinsic::dbg_declare:
292     case Intrinsic::dbg_label:
293     case Intrinsic::dbg_value:
294       llvm_unreachable("debuginfo shouldn't have associated defs!");
295     default:
296       break;
297     }
298   }
299 
300   if (auto *CB = dyn_cast_or_null<CallBase>(UseInst)) {
301     ModRefInfo I = AA.getModRefInfo(DefInst, CB);
302     AR = isMustSet(I) ? MustAlias : MayAlias;
303     return {isModOrRefSet(I), AR};
304   }
305 
306   if (auto *DefLoad = dyn_cast<LoadInst>(DefInst))
307     if (auto *UseLoad = dyn_cast_or_null<LoadInst>(UseInst))
308       return {!areLoadsReorderable(UseLoad, DefLoad), MayAlias};
309 
310   ModRefInfo I = AA.getModRefInfo(DefInst, UseLoc);
311   AR = isMustSet(I) ? MustAlias : MayAlias;
312   return {isModSet(I), AR};
313 }
314 
315 template <typename AliasAnalysisType>
316 static ClobberAlias instructionClobbersQuery(MemoryDef *MD,
317                                              const MemoryUseOrDef *MU,
318                                              const MemoryLocOrCall &UseMLOC,
319                                              AliasAnalysisType &AA) {
320   // FIXME: This is a temporary hack to allow a single instructionClobbersQuery
321   // to exist while MemoryLocOrCall is pushed through places.
322   if (UseMLOC.IsCall)
323     return instructionClobbersQuery(MD, MemoryLocation(), MU->getMemoryInst(),
324                                     AA);
325   return instructionClobbersQuery(MD, UseMLOC.getLoc(), MU->getMemoryInst(),
326                                   AA);
327 }
328 
329 // Return true when MD may alias MU, return false otherwise.
330 bool MemorySSAUtil::defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU,
331                                         AliasAnalysis &AA) {
332   return instructionClobbersQuery(MD, MU, MemoryLocOrCall(MU), AA).IsClobber;
333 }
334 
335 namespace {
336 
337 struct UpwardsMemoryQuery {
338   // True if our original query started off as a call
339   bool IsCall = false;
340   // The pointer location we started the query with. This will be empty if
341   // IsCall is true.
342   MemoryLocation StartingLoc;
343   // This is the instruction we were querying about.
344   const Instruction *Inst = nullptr;
345   // The MemoryAccess we actually got called with, used to test local domination
346   const MemoryAccess *OriginalAccess = nullptr;
347   Optional<AliasResult> AR = MayAlias;
348   bool SkipSelfAccess = false;
349 
350   UpwardsMemoryQuery() = default;
351 
352   UpwardsMemoryQuery(const Instruction *Inst, const MemoryAccess *Access)
353       : IsCall(isa<CallBase>(Inst)), Inst(Inst), OriginalAccess(Access) {
354     if (!IsCall)
355       StartingLoc = MemoryLocation::get(Inst);
356   }
357 };
358 
359 } // end anonymous namespace
360 
361 static bool lifetimeEndsAt(MemoryDef *MD, const MemoryLocation &Loc,
362                            BatchAAResults &AA) {
363   Instruction *Inst = MD->getMemoryInst();
364   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
365     switch (II->getIntrinsicID()) {
366     case Intrinsic::lifetime_end: {
367       MemoryLocation ArgLoc = MemoryLocation::getAfter(II->getArgOperand(1));
368       return AA.alias(ArgLoc, Loc) == MustAlias;
369     }
370     default:
371       return false;
372     }
373   }
374   return false;
375 }
376 
377 template <typename AliasAnalysisType>
378 static bool isUseTriviallyOptimizableToLiveOnEntry(AliasAnalysisType &AA,
379                                                    const Instruction *I) {
380   // If the memory can't be changed, then loads of the memory can't be
381   // clobbered.
382   if (auto *LI = dyn_cast<LoadInst>(I))
383     return I->hasMetadata(LLVMContext::MD_invariant_load) ||
384            AA.pointsToConstantMemory(MemoryLocation::get(LI));
385   return false;
386 }
387 
388 /// Verifies that `Start` is clobbered by `ClobberAt`, and that nothing
389 /// inbetween `Start` and `ClobberAt` can clobbers `Start`.
390 ///
391 /// This is meant to be as simple and self-contained as possible. Because it
392 /// uses no cache, etc., it can be relatively expensive.
393 ///
394 /// \param Start     The MemoryAccess that we want to walk from.
395 /// \param ClobberAt A clobber for Start.
396 /// \param StartLoc  The MemoryLocation for Start.
397 /// \param MSSA      The MemorySSA instance that Start and ClobberAt belong to.
398 /// \param Query     The UpwardsMemoryQuery we used for our search.
399 /// \param AA        The AliasAnalysis we used for our search.
400 /// \param AllowImpreciseClobber Always false, unless we do relaxed verify.
401 
402 template <typename AliasAnalysisType>
403 LLVM_ATTRIBUTE_UNUSED static void
404 checkClobberSanity(const MemoryAccess *Start, MemoryAccess *ClobberAt,
405                    const MemoryLocation &StartLoc, const MemorySSA &MSSA,
406                    const UpwardsMemoryQuery &Query, AliasAnalysisType &AA,
407                    bool AllowImpreciseClobber = false) {
408   assert(MSSA.dominates(ClobberAt, Start) && "Clobber doesn't dominate start?");
409 
410   if (MSSA.isLiveOnEntryDef(Start)) {
411     assert(MSSA.isLiveOnEntryDef(ClobberAt) &&
412            "liveOnEntry must clobber itself");
413     return;
414   }
415 
416   bool FoundClobber = false;
417   DenseSet<ConstMemoryAccessPair> VisitedPhis;
418   SmallVector<ConstMemoryAccessPair, 8> Worklist;
419   Worklist.emplace_back(Start, StartLoc);
420   // Walk all paths from Start to ClobberAt, while looking for clobbers. If one
421   // is found, complain.
422   while (!Worklist.empty()) {
423     auto MAP = Worklist.pop_back_val();
424     // All we care about is that nothing from Start to ClobberAt clobbers Start.
425     // We learn nothing from revisiting nodes.
426     if (!VisitedPhis.insert(MAP).second)
427       continue;
428 
429     for (const auto *MA : def_chain(MAP.first)) {
430       if (MA == ClobberAt) {
431         if (const auto *MD = dyn_cast<MemoryDef>(MA)) {
432           // instructionClobbersQuery isn't essentially free, so don't use `|=`,
433           // since it won't let us short-circuit.
434           //
435           // Also, note that this can't be hoisted out of the `Worklist` loop,
436           // since MD may only act as a clobber for 1 of N MemoryLocations.
437           FoundClobber = FoundClobber || MSSA.isLiveOnEntryDef(MD);
438           if (!FoundClobber) {
439             ClobberAlias CA =
440                 instructionClobbersQuery(MD, MAP.second, Query.Inst, AA);
441             if (CA.IsClobber) {
442               FoundClobber = true;
443               // Not used: CA.AR;
444             }
445           }
446         }
447         break;
448       }
449 
450       // We should never hit liveOnEntry, unless it's the clobber.
451       assert(!MSSA.isLiveOnEntryDef(MA) && "Hit liveOnEntry before clobber?");
452 
453       if (const auto *MD = dyn_cast<MemoryDef>(MA)) {
454         // If Start is a Def, skip self.
455         if (MD == Start)
456           continue;
457 
458         assert(!instructionClobbersQuery(MD, MAP.second, Query.Inst, AA)
459                     .IsClobber &&
460                "Found clobber before reaching ClobberAt!");
461         continue;
462       }
463 
464       if (const auto *MU = dyn_cast<MemoryUse>(MA)) {
465         (void)MU;
466         assert (MU == Start &&
467                 "Can only find use in def chain if Start is a use");
468         continue;
469       }
470 
471       assert(isa<MemoryPhi>(MA));
472 
473       // Add reachable phi predecessors
474       for (auto ItB = upward_defs_begin(
475                     {const_cast<MemoryAccess *>(MA), MAP.second},
476                     MSSA.getDomTree()),
477                 ItE = upward_defs_end();
478            ItB != ItE; ++ItB)
479         if (MSSA.getDomTree().isReachableFromEntry(ItB.getPhiArgBlock()))
480           Worklist.emplace_back(*ItB);
481     }
482   }
483 
484   // If the verify is done following an optimization, it's possible that
485   // ClobberAt was a conservative clobbering, that we can now infer is not a
486   // true clobbering access. Don't fail the verify if that's the case.
487   // We do have accesses that claim they're optimized, but could be optimized
488   // further. Updating all these can be expensive, so allow it for now (FIXME).
489   if (AllowImpreciseClobber)
490     return;
491 
492   // If ClobberAt is a MemoryPhi, we can assume something above it acted as a
493   // clobber. Otherwise, `ClobberAt` should've acted as a clobber at some point.
494   assert((isa<MemoryPhi>(ClobberAt) || FoundClobber) &&
495          "ClobberAt never acted as a clobber");
496 }
497 
498 namespace {
499 
500 /// Our algorithm for walking (and trying to optimize) clobbers, all wrapped up
501 /// in one class.
502 template <class AliasAnalysisType> class ClobberWalker {
503   /// Save a few bytes by using unsigned instead of size_t.
504   using ListIndex = unsigned;
505 
506   /// Represents a span of contiguous MemoryDefs, potentially ending in a
507   /// MemoryPhi.
508   struct DefPath {
509     MemoryLocation Loc;
510     // Note that, because we always walk in reverse, Last will always dominate
511     // First. Also note that First and Last are inclusive.
512     MemoryAccess *First;
513     MemoryAccess *Last;
514     Optional<ListIndex> Previous;
515 
516     DefPath(const MemoryLocation &Loc, MemoryAccess *First, MemoryAccess *Last,
517             Optional<ListIndex> Previous)
518         : Loc(Loc), First(First), Last(Last), Previous(Previous) {}
519 
520     DefPath(const MemoryLocation &Loc, MemoryAccess *Init,
521             Optional<ListIndex> Previous)
522         : DefPath(Loc, Init, Init, Previous) {}
523   };
524 
525   const MemorySSA &MSSA;
526   AliasAnalysisType &AA;
527   DominatorTree &DT;
528   UpwardsMemoryQuery *Query;
529   unsigned *UpwardWalkLimit;
530 
531   // Phi optimization bookkeeping:
532   // List of DefPath to process during the current phi optimization walk.
533   SmallVector<DefPath, 32> Paths;
534   // List of visited <Access, Location> pairs; we can skip paths already
535   // visited with the same memory location.
536   DenseSet<ConstMemoryAccessPair> VisitedPhis;
537   // Record if phi translation has been performed during the current phi
538   // optimization walk, as merging alias results after phi translation can
539   // yield incorrect results. Context in PR46156.
540   bool PerformedPhiTranslation = false;
541 
542   /// Find the nearest def or phi that `From` can legally be optimized to.
543   const MemoryAccess *getWalkTarget(const MemoryPhi *From) const {
544     assert(From->getNumOperands() && "Phi with no operands?");
545 
546     BasicBlock *BB = From->getBlock();
547     MemoryAccess *Result = MSSA.getLiveOnEntryDef();
548     DomTreeNode *Node = DT.getNode(BB);
549     while ((Node = Node->getIDom())) {
550       auto *Defs = MSSA.getBlockDefs(Node->getBlock());
551       if (Defs)
552         return &*Defs->rbegin();
553     }
554     return Result;
555   }
556 
557   /// Result of calling walkToPhiOrClobber.
558   struct UpwardsWalkResult {
559     /// The "Result" of the walk. Either a clobber, the last thing we walked, or
560     /// both. Include alias info when clobber found.
561     MemoryAccess *Result;
562     bool IsKnownClobber;
563     Optional<AliasResult> AR;
564   };
565 
566   /// Walk to the next Phi or Clobber in the def chain starting at Desc.Last.
567   /// This will update Desc.Last as it walks. It will (optionally) also stop at
568   /// StopAt.
569   ///
570   /// This does not test for whether StopAt is a clobber
571   UpwardsWalkResult
572   walkToPhiOrClobber(DefPath &Desc, const MemoryAccess *StopAt = nullptr,
573                      const MemoryAccess *SkipStopAt = nullptr) const {
574     assert(!isa<MemoryUse>(Desc.Last) && "Uses don't exist in my world");
575     assert(UpwardWalkLimit && "Need a valid walk limit");
576     bool LimitAlreadyReached = false;
577     // (*UpwardWalkLimit) may be 0 here, due to the loop in tryOptimizePhi. Set
578     // it to 1. This will not do any alias() calls. It either returns in the
579     // first iteration in the loop below, or is set back to 0 if all def chains
580     // are free of MemoryDefs.
581     if (!*UpwardWalkLimit) {
582       *UpwardWalkLimit = 1;
583       LimitAlreadyReached = true;
584     }
585 
586     for (MemoryAccess *Current : def_chain(Desc.Last)) {
587       Desc.Last = Current;
588       if (Current == StopAt || Current == SkipStopAt)
589         return {Current, false, MayAlias};
590 
591       if (auto *MD = dyn_cast<MemoryDef>(Current)) {
592         if (MSSA.isLiveOnEntryDef(MD))
593           return {MD, true, MustAlias};
594 
595         if (!--*UpwardWalkLimit)
596           return {Current, true, MayAlias};
597 
598         ClobberAlias CA =
599             instructionClobbersQuery(MD, Desc.Loc, Query->Inst, AA);
600         if (CA.IsClobber)
601           return {MD, true, CA.AR};
602       }
603     }
604 
605     if (LimitAlreadyReached)
606       *UpwardWalkLimit = 0;
607 
608     assert(isa<MemoryPhi>(Desc.Last) &&
609            "Ended at a non-clobber that's not a phi?");
610     return {Desc.Last, false, MayAlias};
611   }
612 
613   void addSearches(MemoryPhi *Phi, SmallVectorImpl<ListIndex> &PausedSearches,
614                    ListIndex PriorNode) {
615     auto UpwardDefsBegin = upward_defs_begin({Phi, Paths[PriorNode].Loc}, DT,
616                                              &PerformedPhiTranslation);
617     auto UpwardDefs = make_range(UpwardDefsBegin, upward_defs_end());
618     for (const MemoryAccessPair &P : UpwardDefs) {
619       PausedSearches.push_back(Paths.size());
620       Paths.emplace_back(P.second, P.first, PriorNode);
621     }
622   }
623 
624   /// Represents a search that terminated after finding a clobber. This clobber
625   /// may or may not be present in the path of defs from LastNode..SearchStart,
626   /// since it may have been retrieved from cache.
627   struct TerminatedPath {
628     MemoryAccess *Clobber;
629     ListIndex LastNode;
630   };
631 
632   /// Get an access that keeps us from optimizing to the given phi.
633   ///
634   /// PausedSearches is an array of indices into the Paths array. Its incoming
635   /// value is the indices of searches that stopped at the last phi optimization
636   /// target. It's left in an unspecified state.
637   ///
638   /// If this returns None, NewPaused is a vector of searches that terminated
639   /// at StopWhere. Otherwise, NewPaused is left in an unspecified state.
640   Optional<TerminatedPath>
641   getBlockingAccess(const MemoryAccess *StopWhere,
642                     SmallVectorImpl<ListIndex> &PausedSearches,
643                     SmallVectorImpl<ListIndex> &NewPaused,
644                     SmallVectorImpl<TerminatedPath> &Terminated) {
645     assert(!PausedSearches.empty() && "No searches to continue?");
646 
647     // BFS vs DFS really doesn't make a difference here, so just do a DFS with
648     // PausedSearches as our stack.
649     while (!PausedSearches.empty()) {
650       ListIndex PathIndex = PausedSearches.pop_back_val();
651       DefPath &Node = Paths[PathIndex];
652 
653       // If we've already visited this path with this MemoryLocation, we don't
654       // need to do so again.
655       //
656       // NOTE: That we just drop these paths on the ground makes caching
657       // behavior sporadic. e.g. given a diamond:
658       //  A
659       // B C
660       //  D
661       //
662       // ...If we walk D, B, A, C, we'll only cache the result of phi
663       // optimization for A, B, and D; C will be skipped because it dies here.
664       // This arguably isn't the worst thing ever, since:
665       //   - We generally query things in a top-down order, so if we got below D
666       //     without needing cache entries for {C, MemLoc}, then chances are
667       //     that those cache entries would end up ultimately unused.
668       //   - We still cache things for A, so C only needs to walk up a bit.
669       // If this behavior becomes problematic, we can fix without a ton of extra
670       // work.
671       if (!VisitedPhis.insert({Node.Last, Node.Loc}).second) {
672         if (PerformedPhiTranslation) {
673           // If visiting this path performed Phi translation, don't continue,
674           // since it may not be correct to merge results from two paths if one
675           // relies on the phi translation.
676           TerminatedPath Term{Node.Last, PathIndex};
677           return Term;
678         }
679         continue;
680       }
681 
682       const MemoryAccess *SkipStopWhere = nullptr;
683       if (Query->SkipSelfAccess && Node.Loc == Query->StartingLoc) {
684         assert(isa<MemoryDef>(Query->OriginalAccess));
685         SkipStopWhere = Query->OriginalAccess;
686       }
687 
688       UpwardsWalkResult Res = walkToPhiOrClobber(Node,
689                                                  /*StopAt=*/StopWhere,
690                                                  /*SkipStopAt=*/SkipStopWhere);
691       if (Res.IsKnownClobber) {
692         assert(Res.Result != StopWhere && Res.Result != SkipStopWhere);
693 
694         // If this wasn't a cache hit, we hit a clobber when walking. That's a
695         // failure.
696         TerminatedPath Term{Res.Result, PathIndex};
697         if (!MSSA.dominates(Res.Result, StopWhere))
698           return Term;
699 
700         // Otherwise, it's a valid thing to potentially optimize to.
701         Terminated.push_back(Term);
702         continue;
703       }
704 
705       if (Res.Result == StopWhere || Res.Result == SkipStopWhere) {
706         // We've hit our target. Save this path off for if we want to continue
707         // walking. If we are in the mode of skipping the OriginalAccess, and
708         // we've reached back to the OriginalAccess, do not save path, we've
709         // just looped back to self.
710         if (Res.Result != SkipStopWhere)
711           NewPaused.push_back(PathIndex);
712         continue;
713       }
714 
715       assert(!MSSA.isLiveOnEntryDef(Res.Result) && "liveOnEntry is a clobber");
716       addSearches(cast<MemoryPhi>(Res.Result), PausedSearches, PathIndex);
717     }
718 
719     return None;
720   }
721 
722   template <typename T, typename Walker>
723   struct generic_def_path_iterator
724       : public iterator_facade_base<generic_def_path_iterator<T, Walker>,
725                                     std::forward_iterator_tag, T *> {
726     generic_def_path_iterator() {}
727     generic_def_path_iterator(Walker *W, ListIndex N) : W(W), N(N) {}
728 
729     T &operator*() const { return curNode(); }
730 
731     generic_def_path_iterator &operator++() {
732       N = curNode().Previous;
733       return *this;
734     }
735 
736     bool operator==(const generic_def_path_iterator &O) const {
737       if (N.hasValue() != O.N.hasValue())
738         return false;
739       return !N.hasValue() || *N == *O.N;
740     }
741 
742   private:
743     T &curNode() const { return W->Paths[*N]; }
744 
745     Walker *W = nullptr;
746     Optional<ListIndex> N = None;
747   };
748 
749   using def_path_iterator = generic_def_path_iterator<DefPath, ClobberWalker>;
750   using const_def_path_iterator =
751       generic_def_path_iterator<const DefPath, const ClobberWalker>;
752 
753   iterator_range<def_path_iterator> def_path(ListIndex From) {
754     return make_range(def_path_iterator(this, From), def_path_iterator());
755   }
756 
757   iterator_range<const_def_path_iterator> const_def_path(ListIndex From) const {
758     return make_range(const_def_path_iterator(this, From),
759                       const_def_path_iterator());
760   }
761 
762   struct OptznResult {
763     /// The path that contains our result.
764     TerminatedPath PrimaryClobber;
765     /// The paths that we can legally cache back from, but that aren't
766     /// necessarily the result of the Phi optimization.
767     SmallVector<TerminatedPath, 4> OtherClobbers;
768   };
769 
770   ListIndex defPathIndex(const DefPath &N) const {
771     // The assert looks nicer if we don't need to do &N
772     const DefPath *NP = &N;
773     assert(!Paths.empty() && NP >= &Paths.front() && NP <= &Paths.back() &&
774            "Out of bounds DefPath!");
775     return NP - &Paths.front();
776   }
777 
778   /// Try to optimize a phi as best as we can. Returns a SmallVector of Paths
779   /// that act as legal clobbers. Note that this won't return *all* clobbers.
780   ///
781   /// Phi optimization algorithm tl;dr:
782   ///   - Find the earliest def/phi, A, we can optimize to
783   ///   - Find if all paths from the starting memory access ultimately reach A
784   ///     - If not, optimization isn't possible.
785   ///     - Otherwise, walk from A to another clobber or phi, A'.
786   ///       - If A' is a def, we're done.
787   ///       - If A' is a phi, try to optimize it.
788   ///
789   /// A path is a series of {MemoryAccess, MemoryLocation} pairs. A path
790   /// terminates when a MemoryAccess that clobbers said MemoryLocation is found.
791   OptznResult tryOptimizePhi(MemoryPhi *Phi, MemoryAccess *Start,
792                              const MemoryLocation &Loc) {
793     assert(Paths.empty() && VisitedPhis.empty() && !PerformedPhiTranslation &&
794            "Reset the optimization state.");
795 
796     Paths.emplace_back(Loc, Start, Phi, None);
797     // Stores how many "valid" optimization nodes we had prior to calling
798     // addSearches/getBlockingAccess. Necessary for caching if we had a blocker.
799     auto PriorPathsSize = Paths.size();
800 
801     SmallVector<ListIndex, 16> PausedSearches;
802     SmallVector<ListIndex, 8> NewPaused;
803     SmallVector<TerminatedPath, 4> TerminatedPaths;
804 
805     addSearches(Phi, PausedSearches, 0);
806 
807     // Moves the TerminatedPath with the "most dominated" Clobber to the end of
808     // Paths.
809     auto MoveDominatedPathToEnd = [&](SmallVectorImpl<TerminatedPath> &Paths) {
810       assert(!Paths.empty() && "Need a path to move");
811       auto Dom = Paths.begin();
812       for (auto I = std::next(Dom), E = Paths.end(); I != E; ++I)
813         if (!MSSA.dominates(I->Clobber, Dom->Clobber))
814           Dom = I;
815       auto Last = Paths.end() - 1;
816       if (Last != Dom)
817         std::iter_swap(Last, Dom);
818     };
819 
820     MemoryPhi *Current = Phi;
821     while (true) {
822       assert(!MSSA.isLiveOnEntryDef(Current) &&
823              "liveOnEntry wasn't treated as a clobber?");
824 
825       const auto *Target = getWalkTarget(Current);
826       // If a TerminatedPath doesn't dominate Target, then it wasn't a legal
827       // optimization for the prior phi.
828       assert(all_of(TerminatedPaths, [&](const TerminatedPath &P) {
829         return MSSA.dominates(P.Clobber, Target);
830       }));
831 
832       // FIXME: This is broken, because the Blocker may be reported to be
833       // liveOnEntry, and we'll happily wait for that to disappear (read: never)
834       // For the moment, this is fine, since we do nothing with blocker info.
835       if (Optional<TerminatedPath> Blocker = getBlockingAccess(
836               Target, PausedSearches, NewPaused, TerminatedPaths)) {
837 
838         // Find the node we started at. We can't search based on N->Last, since
839         // we may have gone around a loop with a different MemoryLocation.
840         auto Iter = find_if(def_path(Blocker->LastNode), [&](const DefPath &N) {
841           return defPathIndex(N) < PriorPathsSize;
842         });
843         assert(Iter != def_path_iterator());
844 
845         DefPath &CurNode = *Iter;
846         assert(CurNode.Last == Current);
847 
848         // Two things:
849         // A. We can't reliably cache all of NewPaused back. Consider a case
850         //    where we have two paths in NewPaused; one of which can't optimize
851         //    above this phi, whereas the other can. If we cache the second path
852         //    back, we'll end up with suboptimal cache entries. We can handle
853         //    cases like this a bit better when we either try to find all
854         //    clobbers that block phi optimization, or when our cache starts
855         //    supporting unfinished searches.
856         // B. We can't reliably cache TerminatedPaths back here without doing
857         //    extra checks; consider a case like:
858         //       T
859         //      / \
860         //     D   C
861         //      \ /
862         //       S
863         //    Where T is our target, C is a node with a clobber on it, D is a
864         //    diamond (with a clobber *only* on the left or right node, N), and
865         //    S is our start. Say we walk to D, through the node opposite N
866         //    (read: ignoring the clobber), and see a cache entry in the top
867         //    node of D. That cache entry gets put into TerminatedPaths. We then
868         //    walk up to C (N is later in our worklist), find the clobber, and
869         //    quit. If we append TerminatedPaths to OtherClobbers, we'll cache
870         //    the bottom part of D to the cached clobber, ignoring the clobber
871         //    in N. Again, this problem goes away if we start tracking all
872         //    blockers for a given phi optimization.
873         TerminatedPath Result{CurNode.Last, defPathIndex(CurNode)};
874         return {Result, {}};
875       }
876 
877       // If there's nothing left to search, then all paths led to valid clobbers
878       // that we got from our cache; pick the nearest to the start, and allow
879       // the rest to be cached back.
880       if (NewPaused.empty()) {
881         MoveDominatedPathToEnd(TerminatedPaths);
882         TerminatedPath Result = TerminatedPaths.pop_back_val();
883         return {Result, std::move(TerminatedPaths)};
884       }
885 
886       MemoryAccess *DefChainEnd = nullptr;
887       SmallVector<TerminatedPath, 4> Clobbers;
888       for (ListIndex Paused : NewPaused) {
889         UpwardsWalkResult WR = walkToPhiOrClobber(Paths[Paused]);
890         if (WR.IsKnownClobber)
891           Clobbers.push_back({WR.Result, Paused});
892         else
893           // Micro-opt: If we hit the end of the chain, save it.
894           DefChainEnd = WR.Result;
895       }
896 
897       if (!TerminatedPaths.empty()) {
898         // If we couldn't find the dominating phi/liveOnEntry in the above loop,
899         // do it now.
900         if (!DefChainEnd)
901           for (auto *MA : def_chain(const_cast<MemoryAccess *>(Target)))
902             DefChainEnd = MA;
903         assert(DefChainEnd && "Failed to find dominating phi/liveOnEntry");
904 
905         // If any of the terminated paths don't dominate the phi we'll try to
906         // optimize, we need to figure out what they are and quit.
907         const BasicBlock *ChainBB = DefChainEnd->getBlock();
908         for (const TerminatedPath &TP : TerminatedPaths) {
909           // Because we know that DefChainEnd is as "high" as we can go, we
910           // don't need local dominance checks; BB dominance is sufficient.
911           if (DT.dominates(ChainBB, TP.Clobber->getBlock()))
912             Clobbers.push_back(TP);
913         }
914       }
915 
916       // If we have clobbers in the def chain, find the one closest to Current
917       // and quit.
918       if (!Clobbers.empty()) {
919         MoveDominatedPathToEnd(Clobbers);
920         TerminatedPath Result = Clobbers.pop_back_val();
921         return {Result, std::move(Clobbers)};
922       }
923 
924       assert(all_of(NewPaused,
925                     [&](ListIndex I) { return Paths[I].Last == DefChainEnd; }));
926 
927       // Because liveOnEntry is a clobber, this must be a phi.
928       auto *DefChainPhi = cast<MemoryPhi>(DefChainEnd);
929 
930       PriorPathsSize = Paths.size();
931       PausedSearches.clear();
932       for (ListIndex I : NewPaused)
933         addSearches(DefChainPhi, PausedSearches, I);
934       NewPaused.clear();
935 
936       Current = DefChainPhi;
937     }
938   }
939 
940   void verifyOptResult(const OptznResult &R) const {
941     assert(all_of(R.OtherClobbers, [&](const TerminatedPath &P) {
942       return MSSA.dominates(P.Clobber, R.PrimaryClobber.Clobber);
943     }));
944   }
945 
946   void resetPhiOptznState() {
947     Paths.clear();
948     VisitedPhis.clear();
949     PerformedPhiTranslation = false;
950   }
951 
952 public:
953   ClobberWalker(const MemorySSA &MSSA, AliasAnalysisType &AA, DominatorTree &DT)
954       : MSSA(MSSA), AA(AA), DT(DT) {}
955 
956   AliasAnalysisType *getAA() { return &AA; }
957   /// Finds the nearest clobber for the given query, optimizing phis if
958   /// possible.
959   MemoryAccess *findClobber(MemoryAccess *Start, UpwardsMemoryQuery &Q,
960                             unsigned &UpWalkLimit) {
961     Query = &Q;
962     UpwardWalkLimit = &UpWalkLimit;
963     // Starting limit must be > 0.
964     if (!UpWalkLimit)
965       UpWalkLimit++;
966 
967     MemoryAccess *Current = Start;
968     // This walker pretends uses don't exist. If we're handed one, silently grab
969     // its def. (This has the nice side-effect of ensuring we never cache uses)
970     if (auto *MU = dyn_cast<MemoryUse>(Start))
971       Current = MU->getDefiningAccess();
972 
973     DefPath FirstDesc(Q.StartingLoc, Current, Current, None);
974     // Fast path for the overly-common case (no crazy phi optimization
975     // necessary)
976     UpwardsWalkResult WalkResult = walkToPhiOrClobber(FirstDesc);
977     MemoryAccess *Result;
978     if (WalkResult.IsKnownClobber) {
979       Result = WalkResult.Result;
980       Q.AR = WalkResult.AR;
981     } else {
982       OptznResult OptRes = tryOptimizePhi(cast<MemoryPhi>(FirstDesc.Last),
983                                           Current, Q.StartingLoc);
984       verifyOptResult(OptRes);
985       resetPhiOptznState();
986       Result = OptRes.PrimaryClobber.Clobber;
987     }
988 
989 #ifdef EXPENSIVE_CHECKS
990     if (!Q.SkipSelfAccess && *UpwardWalkLimit > 0)
991       checkClobberSanity(Current, Result, Q.StartingLoc, MSSA, Q, AA);
992 #endif
993     return Result;
994   }
995 };
996 
997 struct RenamePassData {
998   DomTreeNode *DTN;
999   DomTreeNode::const_iterator ChildIt;
1000   MemoryAccess *IncomingVal;
1001 
1002   RenamePassData(DomTreeNode *D, DomTreeNode::const_iterator It,
1003                  MemoryAccess *M)
1004       : DTN(D), ChildIt(It), IncomingVal(M) {}
1005 
1006   void swap(RenamePassData &RHS) {
1007     std::swap(DTN, RHS.DTN);
1008     std::swap(ChildIt, RHS.ChildIt);
1009     std::swap(IncomingVal, RHS.IncomingVal);
1010   }
1011 };
1012 
1013 } // end anonymous namespace
1014 
1015 namespace llvm {
1016 
1017 template <class AliasAnalysisType> class MemorySSA::ClobberWalkerBase {
1018   ClobberWalker<AliasAnalysisType> Walker;
1019   MemorySSA *MSSA;
1020 
1021 public:
1022   ClobberWalkerBase(MemorySSA *M, AliasAnalysisType *A, DominatorTree *D)
1023       : Walker(*M, *A, *D), MSSA(M) {}
1024 
1025   MemoryAccess *getClobberingMemoryAccessBase(MemoryAccess *,
1026                                               const MemoryLocation &,
1027                                               unsigned &);
1028   // Third argument (bool), defines whether the clobber search should skip the
1029   // original queried access. If true, there will be a follow-up query searching
1030   // for a clobber access past "self". Note that the Optimized access is not
1031   // updated if a new clobber is found by this SkipSelf search. If this
1032   // additional query becomes heavily used we may decide to cache the result.
1033   // Walker instantiations will decide how to set the SkipSelf bool.
1034   MemoryAccess *getClobberingMemoryAccessBase(MemoryAccess *, unsigned &, bool);
1035 };
1036 
1037 /// A MemorySSAWalker that does AA walks to disambiguate accesses. It no
1038 /// longer does caching on its own, but the name has been retained for the
1039 /// moment.
1040 template <class AliasAnalysisType>
1041 class MemorySSA::CachingWalker final : public MemorySSAWalker {
1042   ClobberWalkerBase<AliasAnalysisType> *Walker;
1043 
1044 public:
1045   CachingWalker(MemorySSA *M, ClobberWalkerBase<AliasAnalysisType> *W)
1046       : MemorySSAWalker(M), Walker(W) {}
1047   ~CachingWalker() override = default;
1048 
1049   using MemorySSAWalker::getClobberingMemoryAccess;
1050 
1051   MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA, unsigned &UWL) {
1052     return Walker->getClobberingMemoryAccessBase(MA, UWL, false);
1053   }
1054   MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA,
1055                                           const MemoryLocation &Loc,
1056                                           unsigned &UWL) {
1057     return Walker->getClobberingMemoryAccessBase(MA, Loc, UWL);
1058   }
1059 
1060   MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA) override {
1061     unsigned UpwardWalkLimit = MaxCheckLimit;
1062     return getClobberingMemoryAccess(MA, UpwardWalkLimit);
1063   }
1064   MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA,
1065                                           const MemoryLocation &Loc) override {
1066     unsigned UpwardWalkLimit = MaxCheckLimit;
1067     return getClobberingMemoryAccess(MA, Loc, UpwardWalkLimit);
1068   }
1069 
1070   void invalidateInfo(MemoryAccess *MA) override {
1071     if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1072       MUD->resetOptimized();
1073   }
1074 };
1075 
1076 template <class AliasAnalysisType>
1077 class MemorySSA::SkipSelfWalker final : public MemorySSAWalker {
1078   ClobberWalkerBase<AliasAnalysisType> *Walker;
1079 
1080 public:
1081   SkipSelfWalker(MemorySSA *M, ClobberWalkerBase<AliasAnalysisType> *W)
1082       : MemorySSAWalker(M), Walker(W) {}
1083   ~SkipSelfWalker() override = default;
1084 
1085   using MemorySSAWalker::getClobberingMemoryAccess;
1086 
1087   MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA, unsigned &UWL) {
1088     return Walker->getClobberingMemoryAccessBase(MA, UWL, true);
1089   }
1090   MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA,
1091                                           const MemoryLocation &Loc,
1092                                           unsigned &UWL) {
1093     return Walker->getClobberingMemoryAccessBase(MA, Loc, UWL);
1094   }
1095 
1096   MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA) override {
1097     unsigned UpwardWalkLimit = MaxCheckLimit;
1098     return getClobberingMemoryAccess(MA, UpwardWalkLimit);
1099   }
1100   MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA,
1101                                           const MemoryLocation &Loc) override {
1102     unsigned UpwardWalkLimit = MaxCheckLimit;
1103     return getClobberingMemoryAccess(MA, Loc, UpwardWalkLimit);
1104   }
1105 
1106   void invalidateInfo(MemoryAccess *MA) override {
1107     if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1108       MUD->resetOptimized();
1109   }
1110 };
1111 
1112 } // end namespace llvm
1113 
1114 void MemorySSA::renameSuccessorPhis(BasicBlock *BB, MemoryAccess *IncomingVal,
1115                                     bool RenameAllUses) {
1116   // Pass through values to our successors
1117   for (const BasicBlock *S : successors(BB)) {
1118     auto It = PerBlockAccesses.find(S);
1119     // Rename the phi nodes in our successor block
1120     if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front()))
1121       continue;
1122     AccessList *Accesses = It->second.get();
1123     auto *Phi = cast<MemoryPhi>(&Accesses->front());
1124     if (RenameAllUses) {
1125       bool ReplacementDone = false;
1126       for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I)
1127         if (Phi->getIncomingBlock(I) == BB) {
1128           Phi->setIncomingValue(I, IncomingVal);
1129           ReplacementDone = true;
1130         }
1131       (void) ReplacementDone;
1132       assert(ReplacementDone && "Incomplete phi during partial rename");
1133     } else
1134       Phi->addIncoming(IncomingVal, BB);
1135   }
1136 }
1137 
1138 /// Rename a single basic block into MemorySSA form.
1139 /// Uses the standard SSA renaming algorithm.
1140 /// \returns The new incoming value.
1141 MemoryAccess *MemorySSA::renameBlock(BasicBlock *BB, MemoryAccess *IncomingVal,
1142                                      bool RenameAllUses) {
1143   auto It = PerBlockAccesses.find(BB);
1144   // Skip most processing if the list is empty.
1145   if (It != PerBlockAccesses.end()) {
1146     AccessList *Accesses = It->second.get();
1147     for (MemoryAccess &L : *Accesses) {
1148       if (MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(&L)) {
1149         if (MUD->getDefiningAccess() == nullptr || RenameAllUses)
1150           MUD->setDefiningAccess(IncomingVal);
1151         if (isa<MemoryDef>(&L))
1152           IncomingVal = &L;
1153       } else {
1154         IncomingVal = &L;
1155       }
1156     }
1157   }
1158   return IncomingVal;
1159 }
1160 
1161 /// This is the standard SSA renaming algorithm.
1162 ///
1163 /// We walk the dominator tree in preorder, renaming accesses, and then filling
1164 /// in phi nodes in our successors.
1165 void MemorySSA::renamePass(DomTreeNode *Root, MemoryAccess *IncomingVal,
1166                            SmallPtrSetImpl<BasicBlock *> &Visited,
1167                            bool SkipVisited, bool RenameAllUses) {
1168   assert(Root && "Trying to rename accesses in an unreachable block");
1169 
1170   SmallVector<RenamePassData, 32> WorkStack;
1171   // Skip everything if we already renamed this block and we are skipping.
1172   // Note: You can't sink this into the if, because we need it to occur
1173   // regardless of whether we skip blocks or not.
1174   bool AlreadyVisited = !Visited.insert(Root->getBlock()).second;
1175   if (SkipVisited && AlreadyVisited)
1176     return;
1177 
1178   IncomingVal = renameBlock(Root->getBlock(), IncomingVal, RenameAllUses);
1179   renameSuccessorPhis(Root->getBlock(), IncomingVal, RenameAllUses);
1180   WorkStack.push_back({Root, Root->begin(), IncomingVal});
1181 
1182   while (!WorkStack.empty()) {
1183     DomTreeNode *Node = WorkStack.back().DTN;
1184     DomTreeNode::const_iterator ChildIt = WorkStack.back().ChildIt;
1185     IncomingVal = WorkStack.back().IncomingVal;
1186 
1187     if (ChildIt == Node->end()) {
1188       WorkStack.pop_back();
1189     } else {
1190       DomTreeNode *Child = *ChildIt;
1191       ++WorkStack.back().ChildIt;
1192       BasicBlock *BB = Child->getBlock();
1193       // Note: You can't sink this into the if, because we need it to occur
1194       // regardless of whether we skip blocks or not.
1195       AlreadyVisited = !Visited.insert(BB).second;
1196       if (SkipVisited && AlreadyVisited) {
1197         // We already visited this during our renaming, which can happen when
1198         // being asked to rename multiple blocks. Figure out the incoming val,
1199         // which is the last def.
1200         // Incoming value can only change if there is a block def, and in that
1201         // case, it's the last block def in the list.
1202         if (auto *BlockDefs = getWritableBlockDefs(BB))
1203           IncomingVal = &*BlockDefs->rbegin();
1204       } else
1205         IncomingVal = renameBlock(BB, IncomingVal, RenameAllUses);
1206       renameSuccessorPhis(BB, IncomingVal, RenameAllUses);
1207       WorkStack.push_back({Child, Child->begin(), IncomingVal});
1208     }
1209   }
1210 }
1211 
1212 /// This handles unreachable block accesses by deleting phi nodes in
1213 /// unreachable blocks, and marking all other unreachable MemoryAccess's as
1214 /// being uses of the live on entry definition.
1215 void MemorySSA::markUnreachableAsLiveOnEntry(BasicBlock *BB) {
1216   assert(!DT->isReachableFromEntry(BB) &&
1217          "Reachable block found while handling unreachable blocks");
1218 
1219   // Make sure phi nodes in our reachable successors end up with a
1220   // LiveOnEntryDef for our incoming edge, even though our block is forward
1221   // unreachable.  We could just disconnect these blocks from the CFG fully,
1222   // but we do not right now.
1223   for (const BasicBlock *S : successors(BB)) {
1224     if (!DT->isReachableFromEntry(S))
1225       continue;
1226     auto It = PerBlockAccesses.find(S);
1227     // Rename the phi nodes in our successor block
1228     if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front()))
1229       continue;
1230     AccessList *Accesses = It->second.get();
1231     auto *Phi = cast<MemoryPhi>(&Accesses->front());
1232     Phi->addIncoming(LiveOnEntryDef.get(), BB);
1233   }
1234 
1235   auto It = PerBlockAccesses.find(BB);
1236   if (It == PerBlockAccesses.end())
1237     return;
1238 
1239   auto &Accesses = It->second;
1240   for (auto AI = Accesses->begin(), AE = Accesses->end(); AI != AE;) {
1241     auto Next = std::next(AI);
1242     // If we have a phi, just remove it. We are going to replace all
1243     // users with live on entry.
1244     if (auto *UseOrDef = dyn_cast<MemoryUseOrDef>(AI))
1245       UseOrDef->setDefiningAccess(LiveOnEntryDef.get());
1246     else
1247       Accesses->erase(AI);
1248     AI = Next;
1249   }
1250 }
1251 
1252 MemorySSA::MemorySSA(Function &Func, AliasAnalysis *AA, DominatorTree *DT)
1253     : AA(nullptr), DT(DT), F(Func), LiveOnEntryDef(nullptr), Walker(nullptr),
1254       SkipWalker(nullptr), NextID(0) {
1255   // Build MemorySSA using a batch alias analysis. This reuses the internal
1256   // state that AA collects during an alias()/getModRefInfo() call. This is
1257   // safe because there are no CFG changes while building MemorySSA and can
1258   // significantly reduce the time spent by the compiler in AA, because we will
1259   // make queries about all the instructions in the Function.
1260   assert(AA && "No alias analysis?");
1261   BatchAAResults BatchAA(*AA);
1262   buildMemorySSA(BatchAA);
1263   // Intentionally leave AA to nullptr while building so we don't accidently
1264   // use non-batch AliasAnalysis.
1265   this->AA = AA;
1266   // Also create the walker here.
1267   getWalker();
1268 }
1269 
1270 MemorySSA::~MemorySSA() {
1271   // Drop all our references
1272   for (const auto &Pair : PerBlockAccesses)
1273     for (MemoryAccess &MA : *Pair.second)
1274       MA.dropAllReferences();
1275 }
1276 
1277 MemorySSA::AccessList *MemorySSA::getOrCreateAccessList(const BasicBlock *BB) {
1278   auto Res = PerBlockAccesses.insert(std::make_pair(BB, nullptr));
1279 
1280   if (Res.second)
1281     Res.first->second = std::make_unique<AccessList>();
1282   return Res.first->second.get();
1283 }
1284 
1285 MemorySSA::DefsList *MemorySSA::getOrCreateDefsList(const BasicBlock *BB) {
1286   auto Res = PerBlockDefs.insert(std::make_pair(BB, nullptr));
1287 
1288   if (Res.second)
1289     Res.first->second = std::make_unique<DefsList>();
1290   return Res.first->second.get();
1291 }
1292 
1293 namespace llvm {
1294 
1295 /// This class is a batch walker of all MemoryUse's in the program, and points
1296 /// their defining access at the thing that actually clobbers them.  Because it
1297 /// is a batch walker that touches everything, it does not operate like the
1298 /// other walkers.  This walker is basically performing a top-down SSA renaming
1299 /// pass, where the version stack is used as the cache.  This enables it to be
1300 /// significantly more time and memory efficient than using the regular walker,
1301 /// which is walking bottom-up.
1302 class MemorySSA::OptimizeUses {
1303 public:
1304   OptimizeUses(MemorySSA *MSSA, CachingWalker<BatchAAResults> *Walker,
1305                BatchAAResults *BAA, DominatorTree *DT)
1306       : MSSA(MSSA), Walker(Walker), AA(BAA), DT(DT) {}
1307 
1308   void optimizeUses();
1309 
1310 private:
1311   /// This represents where a given memorylocation is in the stack.
1312   struct MemlocStackInfo {
1313     // This essentially is keeping track of versions of the stack. Whenever
1314     // the stack changes due to pushes or pops, these versions increase.
1315     unsigned long StackEpoch;
1316     unsigned long PopEpoch;
1317     // This is the lower bound of places on the stack to check. It is equal to
1318     // the place the last stack walk ended.
1319     // Note: Correctness depends on this being initialized to 0, which densemap
1320     // does
1321     unsigned long LowerBound;
1322     const BasicBlock *LowerBoundBlock;
1323     // This is where the last walk for this memory location ended.
1324     unsigned long LastKill;
1325     bool LastKillValid;
1326     Optional<AliasResult> AR;
1327   };
1328 
1329   void optimizeUsesInBlock(const BasicBlock *, unsigned long &, unsigned long &,
1330                            SmallVectorImpl<MemoryAccess *> &,
1331                            DenseMap<MemoryLocOrCall, MemlocStackInfo> &);
1332 
1333   MemorySSA *MSSA;
1334   CachingWalker<BatchAAResults> *Walker;
1335   BatchAAResults *AA;
1336   DominatorTree *DT;
1337 };
1338 
1339 } // end namespace llvm
1340 
1341 /// Optimize the uses in a given block This is basically the SSA renaming
1342 /// algorithm, with one caveat: We are able to use a single stack for all
1343 /// MemoryUses.  This is because the set of *possible* reaching MemoryDefs is
1344 /// the same for every MemoryUse.  The *actual* clobbering MemoryDef is just
1345 /// going to be some position in that stack of possible ones.
1346 ///
1347 /// We track the stack positions that each MemoryLocation needs
1348 /// to check, and last ended at.  This is because we only want to check the
1349 /// things that changed since last time.  The same MemoryLocation should
1350 /// get clobbered by the same store (getModRefInfo does not use invariantness or
1351 /// things like this, and if they start, we can modify MemoryLocOrCall to
1352 /// include relevant data)
1353 void MemorySSA::OptimizeUses::optimizeUsesInBlock(
1354     const BasicBlock *BB, unsigned long &StackEpoch, unsigned long &PopEpoch,
1355     SmallVectorImpl<MemoryAccess *> &VersionStack,
1356     DenseMap<MemoryLocOrCall, MemlocStackInfo> &LocStackInfo) {
1357 
1358   /// If no accesses, nothing to do.
1359   MemorySSA::AccessList *Accesses = MSSA->getWritableBlockAccesses(BB);
1360   if (Accesses == nullptr)
1361     return;
1362 
1363   // Pop everything that doesn't dominate the current block off the stack,
1364   // increment the PopEpoch to account for this.
1365   while (true) {
1366     assert(
1367         !VersionStack.empty() &&
1368         "Version stack should have liveOnEntry sentinel dominating everything");
1369     BasicBlock *BackBlock = VersionStack.back()->getBlock();
1370     if (DT->dominates(BackBlock, BB))
1371       break;
1372     while (VersionStack.back()->getBlock() == BackBlock)
1373       VersionStack.pop_back();
1374     ++PopEpoch;
1375   }
1376 
1377   for (MemoryAccess &MA : *Accesses) {
1378     auto *MU = dyn_cast<MemoryUse>(&MA);
1379     if (!MU) {
1380       VersionStack.push_back(&MA);
1381       ++StackEpoch;
1382       continue;
1383     }
1384 
1385     if (isUseTriviallyOptimizableToLiveOnEntry(*AA, MU->getMemoryInst())) {
1386       MU->setDefiningAccess(MSSA->getLiveOnEntryDef(), true, None);
1387       continue;
1388     }
1389 
1390     MemoryLocOrCall UseMLOC(MU);
1391     auto &LocInfo = LocStackInfo[UseMLOC];
1392     // If the pop epoch changed, it means we've removed stuff from top of
1393     // stack due to changing blocks. We may have to reset the lower bound or
1394     // last kill info.
1395     if (LocInfo.PopEpoch != PopEpoch) {
1396       LocInfo.PopEpoch = PopEpoch;
1397       LocInfo.StackEpoch = StackEpoch;
1398       // If the lower bound was in something that no longer dominates us, we
1399       // have to reset it.
1400       // We can't simply track stack size, because the stack may have had
1401       // pushes/pops in the meantime.
1402       // XXX: This is non-optimal, but only is slower cases with heavily
1403       // branching dominator trees.  To get the optimal number of queries would
1404       // be to make lowerbound and lastkill a per-loc stack, and pop it until
1405       // the top of that stack dominates us.  This does not seem worth it ATM.
1406       // A much cheaper optimization would be to always explore the deepest
1407       // branch of the dominator tree first. This will guarantee this resets on
1408       // the smallest set of blocks.
1409       if (LocInfo.LowerBoundBlock && LocInfo.LowerBoundBlock != BB &&
1410           !DT->dominates(LocInfo.LowerBoundBlock, BB)) {
1411         // Reset the lower bound of things to check.
1412         // TODO: Some day we should be able to reset to last kill, rather than
1413         // 0.
1414         LocInfo.LowerBound = 0;
1415         LocInfo.LowerBoundBlock = VersionStack[0]->getBlock();
1416         LocInfo.LastKillValid = false;
1417       }
1418     } else if (LocInfo.StackEpoch != StackEpoch) {
1419       // If all that has changed is the StackEpoch, we only have to check the
1420       // new things on the stack, because we've checked everything before.  In
1421       // this case, the lower bound of things to check remains the same.
1422       LocInfo.PopEpoch = PopEpoch;
1423       LocInfo.StackEpoch = StackEpoch;
1424     }
1425     if (!LocInfo.LastKillValid) {
1426       LocInfo.LastKill = VersionStack.size() - 1;
1427       LocInfo.LastKillValid = true;
1428       LocInfo.AR = MayAlias;
1429     }
1430 
1431     // At this point, we should have corrected last kill and LowerBound to be
1432     // in bounds.
1433     assert(LocInfo.LowerBound < VersionStack.size() &&
1434            "Lower bound out of range");
1435     assert(LocInfo.LastKill < VersionStack.size() &&
1436            "Last kill info out of range");
1437     // In any case, the new upper bound is the top of the stack.
1438     unsigned long UpperBound = VersionStack.size() - 1;
1439 
1440     if (UpperBound - LocInfo.LowerBound > MaxCheckLimit) {
1441       LLVM_DEBUG(dbgs() << "MemorySSA skipping optimization of " << *MU << " ("
1442                         << *(MU->getMemoryInst()) << ")"
1443                         << " because there are "
1444                         << UpperBound - LocInfo.LowerBound
1445                         << " stores to disambiguate\n");
1446       // Because we did not walk, LastKill is no longer valid, as this may
1447       // have been a kill.
1448       LocInfo.LastKillValid = false;
1449       continue;
1450     }
1451     bool FoundClobberResult = false;
1452     unsigned UpwardWalkLimit = MaxCheckLimit;
1453     while (UpperBound > LocInfo.LowerBound) {
1454       if (isa<MemoryPhi>(VersionStack[UpperBound])) {
1455         // For phis, use the walker, see where we ended up, go there
1456         MemoryAccess *Result =
1457             Walker->getClobberingMemoryAccess(MU, UpwardWalkLimit);
1458         // We are guaranteed to find it or something is wrong
1459         while (VersionStack[UpperBound] != Result) {
1460           assert(UpperBound != 0);
1461           --UpperBound;
1462         }
1463         FoundClobberResult = true;
1464         break;
1465       }
1466 
1467       MemoryDef *MD = cast<MemoryDef>(VersionStack[UpperBound]);
1468       // If the lifetime of the pointer ends at this instruction, it's live on
1469       // entry.
1470       if (!UseMLOC.IsCall && lifetimeEndsAt(MD, UseMLOC.getLoc(), *AA)) {
1471         // Reset UpperBound to liveOnEntryDef's place in the stack
1472         UpperBound = 0;
1473         FoundClobberResult = true;
1474         LocInfo.AR = MustAlias;
1475         break;
1476       }
1477       ClobberAlias CA = instructionClobbersQuery(MD, MU, UseMLOC, *AA);
1478       if (CA.IsClobber) {
1479         FoundClobberResult = true;
1480         LocInfo.AR = CA.AR;
1481         break;
1482       }
1483       --UpperBound;
1484     }
1485 
1486     // Note: Phis always have AliasResult AR set to MayAlias ATM.
1487 
1488     // At the end of this loop, UpperBound is either a clobber, or lower bound
1489     // PHI walking may cause it to be < LowerBound, and in fact, < LastKill.
1490     if (FoundClobberResult || UpperBound < LocInfo.LastKill) {
1491       // We were last killed now by where we got to
1492       if (MSSA->isLiveOnEntryDef(VersionStack[UpperBound]))
1493         LocInfo.AR = None;
1494       MU->setDefiningAccess(VersionStack[UpperBound], true, LocInfo.AR);
1495       LocInfo.LastKill = UpperBound;
1496     } else {
1497       // Otherwise, we checked all the new ones, and now we know we can get to
1498       // LastKill.
1499       MU->setDefiningAccess(VersionStack[LocInfo.LastKill], true, LocInfo.AR);
1500     }
1501     LocInfo.LowerBound = VersionStack.size() - 1;
1502     LocInfo.LowerBoundBlock = BB;
1503   }
1504 }
1505 
1506 /// Optimize uses to point to their actual clobbering definitions.
1507 void MemorySSA::OptimizeUses::optimizeUses() {
1508   SmallVector<MemoryAccess *, 16> VersionStack;
1509   DenseMap<MemoryLocOrCall, MemlocStackInfo> LocStackInfo;
1510   VersionStack.push_back(MSSA->getLiveOnEntryDef());
1511 
1512   unsigned long StackEpoch = 1;
1513   unsigned long PopEpoch = 1;
1514   // We perform a non-recursive top-down dominator tree walk.
1515   for (const auto *DomNode : depth_first(DT->getRootNode()))
1516     optimizeUsesInBlock(DomNode->getBlock(), StackEpoch, PopEpoch, VersionStack,
1517                         LocStackInfo);
1518 }
1519 
1520 void MemorySSA::placePHINodes(
1521     const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks) {
1522   // Determine where our MemoryPhi's should go
1523   ForwardIDFCalculator IDFs(*DT);
1524   IDFs.setDefiningBlocks(DefiningBlocks);
1525   SmallVector<BasicBlock *, 32> IDFBlocks;
1526   IDFs.calculate(IDFBlocks);
1527 
1528   // Now place MemoryPhi nodes.
1529   for (auto &BB : IDFBlocks)
1530     createMemoryPhi(BB);
1531 }
1532 
1533 void MemorySSA::buildMemorySSA(BatchAAResults &BAA) {
1534   // We create an access to represent "live on entry", for things like
1535   // arguments or users of globals, where the memory they use is defined before
1536   // the beginning of the function. We do not actually insert it into the IR.
1537   // We do not define a live on exit for the immediate uses, and thus our
1538   // semantics do *not* imply that something with no immediate uses can simply
1539   // be removed.
1540   BasicBlock &StartingPoint = F.getEntryBlock();
1541   LiveOnEntryDef.reset(new MemoryDef(F.getContext(), nullptr, nullptr,
1542                                      &StartingPoint, NextID++));
1543 
1544   // We maintain lists of memory accesses per-block, trading memory for time. We
1545   // could just look up the memory access for every possible instruction in the
1546   // stream.
1547   SmallPtrSet<BasicBlock *, 32> DefiningBlocks;
1548   // Go through each block, figure out where defs occur, and chain together all
1549   // the accesses.
1550   for (BasicBlock &B : F) {
1551     bool InsertIntoDef = false;
1552     AccessList *Accesses = nullptr;
1553     DefsList *Defs = nullptr;
1554     for (Instruction &I : B) {
1555       MemoryUseOrDef *MUD = createNewAccess(&I, &BAA);
1556       if (!MUD)
1557         continue;
1558 
1559       if (!Accesses)
1560         Accesses = getOrCreateAccessList(&B);
1561       Accesses->push_back(MUD);
1562       if (isa<MemoryDef>(MUD)) {
1563         InsertIntoDef = true;
1564         if (!Defs)
1565           Defs = getOrCreateDefsList(&B);
1566         Defs->push_back(*MUD);
1567       }
1568     }
1569     if (InsertIntoDef)
1570       DefiningBlocks.insert(&B);
1571   }
1572   placePHINodes(DefiningBlocks);
1573 
1574   // Now do regular SSA renaming on the MemoryDef/MemoryUse. Visited will get
1575   // filled in with all blocks.
1576   SmallPtrSet<BasicBlock *, 16> Visited;
1577   renamePass(DT->getRootNode(), LiveOnEntryDef.get(), Visited);
1578 
1579   ClobberWalkerBase<BatchAAResults> WalkerBase(this, &BAA, DT);
1580   CachingWalker<BatchAAResults> WalkerLocal(this, &WalkerBase);
1581   OptimizeUses(this, &WalkerLocal, &BAA, DT).optimizeUses();
1582 
1583   // Mark the uses in unreachable blocks as live on entry, so that they go
1584   // somewhere.
1585   for (auto &BB : F)
1586     if (!Visited.count(&BB))
1587       markUnreachableAsLiveOnEntry(&BB);
1588 }
1589 
1590 MemorySSAWalker *MemorySSA::getWalker() { return getWalkerImpl(); }
1591 
1592 MemorySSA::CachingWalker<AliasAnalysis> *MemorySSA::getWalkerImpl() {
1593   if (Walker)
1594     return Walker.get();
1595 
1596   if (!WalkerBase)
1597     WalkerBase =
1598         std::make_unique<ClobberWalkerBase<AliasAnalysis>>(this, AA, DT);
1599 
1600   Walker =
1601       std::make_unique<CachingWalker<AliasAnalysis>>(this, WalkerBase.get());
1602   return Walker.get();
1603 }
1604 
1605 MemorySSAWalker *MemorySSA::getSkipSelfWalker() {
1606   if (SkipWalker)
1607     return SkipWalker.get();
1608 
1609   if (!WalkerBase)
1610     WalkerBase =
1611         std::make_unique<ClobberWalkerBase<AliasAnalysis>>(this, AA, DT);
1612 
1613   SkipWalker =
1614       std::make_unique<SkipSelfWalker<AliasAnalysis>>(this, WalkerBase.get());
1615   return SkipWalker.get();
1616  }
1617 
1618 
1619 // This is a helper function used by the creation routines. It places NewAccess
1620 // into the access and defs lists for a given basic block, at the given
1621 // insertion point.
1622 void MemorySSA::insertIntoListsForBlock(MemoryAccess *NewAccess,
1623                                         const BasicBlock *BB,
1624                                         InsertionPlace Point) {
1625   auto *Accesses = getOrCreateAccessList(BB);
1626   if (Point == Beginning) {
1627     // If it's a phi node, it goes first, otherwise, it goes after any phi
1628     // nodes.
1629     if (isa<MemoryPhi>(NewAccess)) {
1630       Accesses->push_front(NewAccess);
1631       auto *Defs = getOrCreateDefsList(BB);
1632       Defs->push_front(*NewAccess);
1633     } else {
1634       auto AI = find_if_not(
1635           *Accesses, [](const MemoryAccess &MA) { return isa<MemoryPhi>(MA); });
1636       Accesses->insert(AI, NewAccess);
1637       if (!isa<MemoryUse>(NewAccess)) {
1638         auto *Defs = getOrCreateDefsList(BB);
1639         auto DI = find_if_not(
1640             *Defs, [](const MemoryAccess &MA) { return isa<MemoryPhi>(MA); });
1641         Defs->insert(DI, *NewAccess);
1642       }
1643     }
1644   } else {
1645     Accesses->push_back(NewAccess);
1646     if (!isa<MemoryUse>(NewAccess)) {
1647       auto *Defs = getOrCreateDefsList(BB);
1648       Defs->push_back(*NewAccess);
1649     }
1650   }
1651   BlockNumberingValid.erase(BB);
1652 }
1653 
1654 void MemorySSA::insertIntoListsBefore(MemoryAccess *What, const BasicBlock *BB,
1655                                       AccessList::iterator InsertPt) {
1656   auto *Accesses = getWritableBlockAccesses(BB);
1657   bool WasEnd = InsertPt == Accesses->end();
1658   Accesses->insert(AccessList::iterator(InsertPt), What);
1659   if (!isa<MemoryUse>(What)) {
1660     auto *Defs = getOrCreateDefsList(BB);
1661     // If we got asked to insert at the end, we have an easy job, just shove it
1662     // at the end. If we got asked to insert before an existing def, we also get
1663     // an iterator. If we got asked to insert before a use, we have to hunt for
1664     // the next def.
1665     if (WasEnd) {
1666       Defs->push_back(*What);
1667     } else if (isa<MemoryDef>(InsertPt)) {
1668       Defs->insert(InsertPt->getDefsIterator(), *What);
1669     } else {
1670       while (InsertPt != Accesses->end() && !isa<MemoryDef>(InsertPt))
1671         ++InsertPt;
1672       // Either we found a def, or we are inserting at the end
1673       if (InsertPt == Accesses->end())
1674         Defs->push_back(*What);
1675       else
1676         Defs->insert(InsertPt->getDefsIterator(), *What);
1677     }
1678   }
1679   BlockNumberingValid.erase(BB);
1680 }
1681 
1682 void MemorySSA::prepareForMoveTo(MemoryAccess *What, BasicBlock *BB) {
1683   // Keep it in the lookup tables, remove from the lists
1684   removeFromLists(What, false);
1685 
1686   // Note that moving should implicitly invalidate the optimized state of a
1687   // MemoryUse (and Phis can't be optimized). However, it doesn't do so for a
1688   // MemoryDef.
1689   if (auto *MD = dyn_cast<MemoryDef>(What))
1690     MD->resetOptimized();
1691   What->setBlock(BB);
1692 }
1693 
1694 // Move What before Where in the IR.  The end result is that What will belong to
1695 // the right lists and have the right Block set, but will not otherwise be
1696 // correct. It will not have the right defining access, and if it is a def,
1697 // things below it will not properly be updated.
1698 void MemorySSA::moveTo(MemoryUseOrDef *What, BasicBlock *BB,
1699                        AccessList::iterator Where) {
1700   prepareForMoveTo(What, BB);
1701   insertIntoListsBefore(What, BB, Where);
1702 }
1703 
1704 void MemorySSA::moveTo(MemoryAccess *What, BasicBlock *BB,
1705                        InsertionPlace Point) {
1706   if (isa<MemoryPhi>(What)) {
1707     assert(Point == Beginning &&
1708            "Can only move a Phi at the beginning of the block");
1709     // Update lookup table entry
1710     ValueToMemoryAccess.erase(What->getBlock());
1711     bool Inserted = ValueToMemoryAccess.insert({BB, What}).second;
1712     (void)Inserted;
1713     assert(Inserted && "Cannot move a Phi to a block that already has one");
1714   }
1715 
1716   prepareForMoveTo(What, BB);
1717   insertIntoListsForBlock(What, BB, Point);
1718 }
1719 
1720 MemoryPhi *MemorySSA::createMemoryPhi(BasicBlock *BB) {
1721   assert(!getMemoryAccess(BB) && "MemoryPhi already exists for this BB");
1722   MemoryPhi *Phi = new MemoryPhi(BB->getContext(), BB, NextID++);
1723   // Phi's always are placed at the front of the block.
1724   insertIntoListsForBlock(Phi, BB, Beginning);
1725   ValueToMemoryAccess[BB] = Phi;
1726   return Phi;
1727 }
1728 
1729 MemoryUseOrDef *MemorySSA::createDefinedAccess(Instruction *I,
1730                                                MemoryAccess *Definition,
1731                                                const MemoryUseOrDef *Template,
1732                                                bool CreationMustSucceed) {
1733   assert(!isa<PHINode>(I) && "Cannot create a defined access for a PHI");
1734   MemoryUseOrDef *NewAccess = createNewAccess(I, AA, Template);
1735   if (CreationMustSucceed)
1736     assert(NewAccess != nullptr && "Tried to create a memory access for a "
1737                                    "non-memory touching instruction");
1738   if (NewAccess) {
1739     assert((!Definition || !isa<MemoryUse>(Definition)) &&
1740            "A use cannot be a defining access");
1741     NewAccess->setDefiningAccess(Definition);
1742   }
1743   return NewAccess;
1744 }
1745 
1746 // Return true if the instruction has ordering constraints.
1747 // Note specifically that this only considers stores and loads
1748 // because others are still considered ModRef by getModRefInfo.
1749 static inline bool isOrdered(const Instruction *I) {
1750   if (auto *SI = dyn_cast<StoreInst>(I)) {
1751     if (!SI->isUnordered())
1752       return true;
1753   } else if (auto *LI = dyn_cast<LoadInst>(I)) {
1754     if (!LI->isUnordered())
1755       return true;
1756   }
1757   return false;
1758 }
1759 
1760 /// Helper function to create new memory accesses
1761 template <typename AliasAnalysisType>
1762 MemoryUseOrDef *MemorySSA::createNewAccess(Instruction *I,
1763                                            AliasAnalysisType *AAP,
1764                                            const MemoryUseOrDef *Template) {
1765   // The assume intrinsic has a control dependency which we model by claiming
1766   // that it writes arbitrarily. Debuginfo intrinsics may be considered
1767   // clobbers when we have a nonstandard AA pipeline. Ignore these fake memory
1768   // dependencies here.
1769   // FIXME: Replace this special casing with a more accurate modelling of
1770   // assume's control dependency.
1771   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1772     switch (II->getIntrinsicID()) {
1773     default:
1774       break;
1775     case Intrinsic::assume:
1776     case Intrinsic::experimental_noalias_scope_decl:
1777       return nullptr;
1778     }
1779   }
1780 
1781   // Using a nonstandard AA pipelines might leave us with unexpected modref
1782   // results for I, so add a check to not model instructions that may not read
1783   // from or write to memory. This is necessary for correctness.
1784   if (!I->mayReadFromMemory() && !I->mayWriteToMemory())
1785     return nullptr;
1786 
1787   bool Def, Use;
1788   if (Template) {
1789     Def = isa<MemoryDef>(Template);
1790     Use = isa<MemoryUse>(Template);
1791 #if !defined(NDEBUG)
1792     ModRefInfo ModRef = AAP->getModRefInfo(I, None);
1793     bool DefCheck, UseCheck;
1794     DefCheck = isModSet(ModRef) || isOrdered(I);
1795     UseCheck = isRefSet(ModRef);
1796     assert(Def == DefCheck && (Def || Use == UseCheck) && "Invalid template");
1797 #endif
1798   } else {
1799     // Find out what affect this instruction has on memory.
1800     ModRefInfo ModRef = AAP->getModRefInfo(I, None);
1801     // The isOrdered check is used to ensure that volatiles end up as defs
1802     // (atomics end up as ModRef right now anyway).  Until we separate the
1803     // ordering chain from the memory chain, this enables people to see at least
1804     // some relative ordering to volatiles.  Note that getClobberingMemoryAccess
1805     // will still give an answer that bypasses other volatile loads.  TODO:
1806     // Separate memory aliasing and ordering into two different chains so that
1807     // we can precisely represent both "what memory will this read/write/is
1808     // clobbered by" and "what instructions can I move this past".
1809     Def = isModSet(ModRef) || isOrdered(I);
1810     Use = isRefSet(ModRef);
1811   }
1812 
1813   // It's possible for an instruction to not modify memory at all. During
1814   // construction, we ignore them.
1815   if (!Def && !Use)
1816     return nullptr;
1817 
1818   MemoryUseOrDef *MUD;
1819   if (Def)
1820     MUD = new MemoryDef(I->getContext(), nullptr, I, I->getParent(), NextID++);
1821   else
1822     MUD = new MemoryUse(I->getContext(), nullptr, I, I->getParent());
1823   ValueToMemoryAccess[I] = MUD;
1824   return MUD;
1825 }
1826 
1827 /// Properly remove \p MA from all of MemorySSA's lookup tables.
1828 void MemorySSA::removeFromLookups(MemoryAccess *MA) {
1829   assert(MA->use_empty() &&
1830          "Trying to remove memory access that still has uses");
1831   BlockNumbering.erase(MA);
1832   if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1833     MUD->setDefiningAccess(nullptr);
1834   // Invalidate our walker's cache if necessary
1835   if (!isa<MemoryUse>(MA))
1836     getWalker()->invalidateInfo(MA);
1837 
1838   Value *MemoryInst;
1839   if (const auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1840     MemoryInst = MUD->getMemoryInst();
1841   else
1842     MemoryInst = MA->getBlock();
1843 
1844   auto VMA = ValueToMemoryAccess.find(MemoryInst);
1845   if (VMA->second == MA)
1846     ValueToMemoryAccess.erase(VMA);
1847 }
1848 
1849 /// Properly remove \p MA from all of MemorySSA's lists.
1850 ///
1851 /// Because of the way the intrusive list and use lists work, it is important to
1852 /// do removal in the right order.
1853 /// ShouldDelete defaults to true, and will cause the memory access to also be
1854 /// deleted, not just removed.
1855 void MemorySSA::removeFromLists(MemoryAccess *MA, bool ShouldDelete) {
1856   BasicBlock *BB = MA->getBlock();
1857   // The access list owns the reference, so we erase it from the non-owning list
1858   // first.
1859   if (!isa<MemoryUse>(MA)) {
1860     auto DefsIt = PerBlockDefs.find(BB);
1861     std::unique_ptr<DefsList> &Defs = DefsIt->second;
1862     Defs->remove(*MA);
1863     if (Defs->empty())
1864       PerBlockDefs.erase(DefsIt);
1865   }
1866 
1867   // The erase call here will delete it. If we don't want it deleted, we call
1868   // remove instead.
1869   auto AccessIt = PerBlockAccesses.find(BB);
1870   std::unique_ptr<AccessList> &Accesses = AccessIt->second;
1871   if (ShouldDelete)
1872     Accesses->erase(MA);
1873   else
1874     Accesses->remove(MA);
1875 
1876   if (Accesses->empty()) {
1877     PerBlockAccesses.erase(AccessIt);
1878     BlockNumberingValid.erase(BB);
1879   }
1880 }
1881 
1882 void MemorySSA::print(raw_ostream &OS) const {
1883   MemorySSAAnnotatedWriter Writer(this);
1884   F.print(OS, &Writer);
1885 }
1886 
1887 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1888 LLVM_DUMP_METHOD void MemorySSA::dump() const { print(dbgs()); }
1889 #endif
1890 
1891 void MemorySSA::verifyMemorySSA() const {
1892   verifyOrderingDominationAndDefUses(F);
1893   verifyDominationNumbers(F);
1894   verifyPrevDefInPhis(F);
1895   // Previously, the verification used to also verify that the clobberingAccess
1896   // cached by MemorySSA is the same as the clobberingAccess found at a later
1897   // query to AA. This does not hold true in general due to the current fragility
1898   // of BasicAA which has arbitrary caps on the things it analyzes before giving
1899   // up. As a result, transformations that are correct, will lead to BasicAA
1900   // returning different Alias answers before and after that transformation.
1901   // Invalidating MemorySSA is not an option, as the results in BasicAA can be so
1902   // random, in the worst case we'd need to rebuild MemorySSA from scratch after
1903   // every transformation, which defeats the purpose of using it. For such an
1904   // example, see test4 added in D51960.
1905 }
1906 
1907 void MemorySSA::verifyPrevDefInPhis(Function &F) const {
1908 #if !defined(NDEBUG) && defined(EXPENSIVE_CHECKS)
1909   for (const BasicBlock &BB : F) {
1910     if (MemoryPhi *Phi = getMemoryAccess(&BB)) {
1911       for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
1912         auto *Pred = Phi->getIncomingBlock(I);
1913         auto *IncAcc = Phi->getIncomingValue(I);
1914         // If Pred has no unreachable predecessors, get last def looking at
1915         // IDoms. If, while walkings IDoms, any of these has an unreachable
1916         // predecessor, then the incoming def can be any access.
1917         if (auto *DTNode = DT->getNode(Pred)) {
1918           while (DTNode) {
1919             if (auto *DefList = getBlockDefs(DTNode->getBlock())) {
1920               auto *LastAcc = &*(--DefList->end());
1921               assert(LastAcc == IncAcc &&
1922                      "Incorrect incoming access into phi.");
1923               break;
1924             }
1925             DTNode = DTNode->getIDom();
1926           }
1927         } else {
1928           // If Pred has unreachable predecessors, but has at least a Def, the
1929           // incoming access can be the last Def in Pred, or it could have been
1930           // optimized to LoE. After an update, though, the LoE may have been
1931           // replaced by another access, so IncAcc may be any access.
1932           // If Pred has unreachable predecessors and no Defs, incoming access
1933           // should be LoE; However, after an update, it may be any access.
1934         }
1935       }
1936     }
1937   }
1938 #endif
1939 }
1940 
1941 /// Verify that all of the blocks we believe to have valid domination numbers
1942 /// actually have valid domination numbers.
1943 void MemorySSA::verifyDominationNumbers(const Function &F) const {
1944 #ifndef NDEBUG
1945   if (BlockNumberingValid.empty())
1946     return;
1947 
1948   SmallPtrSet<const BasicBlock *, 16> ValidBlocks = BlockNumberingValid;
1949   for (const BasicBlock &BB : F) {
1950     if (!ValidBlocks.count(&BB))
1951       continue;
1952 
1953     ValidBlocks.erase(&BB);
1954 
1955     const AccessList *Accesses = getBlockAccesses(&BB);
1956     // It's correct to say an empty block has valid numbering.
1957     if (!Accesses)
1958       continue;
1959 
1960     // Block numbering starts at 1.
1961     unsigned long LastNumber = 0;
1962     for (const MemoryAccess &MA : *Accesses) {
1963       auto ThisNumberIter = BlockNumbering.find(&MA);
1964       assert(ThisNumberIter != BlockNumbering.end() &&
1965              "MemoryAccess has no domination number in a valid block!");
1966 
1967       unsigned long ThisNumber = ThisNumberIter->second;
1968       assert(ThisNumber > LastNumber &&
1969              "Domination numbers should be strictly increasing!");
1970       LastNumber = ThisNumber;
1971     }
1972   }
1973 
1974   assert(ValidBlocks.empty() &&
1975          "All valid BasicBlocks should exist in F -- dangling pointers?");
1976 #endif
1977 }
1978 
1979 /// Verify ordering: the order and existence of MemoryAccesses matches the
1980 /// order and existence of memory affecting instructions.
1981 /// Verify domination: each definition dominates all of its uses.
1982 /// Verify def-uses: the immediate use information - walk all the memory
1983 /// accesses and verifying that, for each use, it appears in the appropriate
1984 /// def's use list
1985 void MemorySSA::verifyOrderingDominationAndDefUses(Function &F) const {
1986 #if !defined(NDEBUG)
1987   // Walk all the blocks, comparing what the lookups think and what the access
1988   // lists think, as well as the order in the blocks vs the order in the access
1989   // lists.
1990   SmallVector<MemoryAccess *, 32> ActualAccesses;
1991   SmallVector<MemoryAccess *, 32> ActualDefs;
1992   for (BasicBlock &B : F) {
1993     const AccessList *AL = getBlockAccesses(&B);
1994     const auto *DL = getBlockDefs(&B);
1995     MemoryPhi *Phi = getMemoryAccess(&B);
1996     if (Phi) {
1997       // Verify ordering.
1998       ActualAccesses.push_back(Phi);
1999       ActualDefs.push_back(Phi);
2000       // Verify domination
2001       for (const Use &U : Phi->uses())
2002         assert(dominates(Phi, U) && "Memory PHI does not dominate it's uses");
2003 #if defined(EXPENSIVE_CHECKS)
2004       // Verify def-uses.
2005       assert(Phi->getNumOperands() == static_cast<unsigned>(std::distance(
2006                                           pred_begin(&B), pred_end(&B))) &&
2007              "Incomplete MemoryPhi Node");
2008       for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
2009         verifyUseInDefs(Phi->getIncomingValue(I), Phi);
2010         assert(is_contained(predecessors(&B), Phi->getIncomingBlock(I)) &&
2011                "Incoming phi block not a block predecessor");
2012       }
2013 #endif
2014     }
2015 
2016     for (Instruction &I : B) {
2017       MemoryUseOrDef *MA = getMemoryAccess(&I);
2018       assert((!MA || (AL && (isa<MemoryUse>(MA) || DL))) &&
2019              "We have memory affecting instructions "
2020              "in this block but they are not in the "
2021              "access list or defs list");
2022       if (MA) {
2023         // Verify ordering.
2024         ActualAccesses.push_back(MA);
2025         if (MemoryAccess *MD = dyn_cast<MemoryDef>(MA)) {
2026           // Verify ordering.
2027           ActualDefs.push_back(MA);
2028           // Verify domination.
2029           for (const Use &U : MD->uses())
2030             assert(dominates(MD, U) &&
2031                    "Memory Def does not dominate it's uses");
2032         }
2033 #if defined(EXPENSIVE_CHECKS)
2034         // Verify def-uses.
2035         verifyUseInDefs(MA->getDefiningAccess(), MA);
2036 #endif
2037       }
2038     }
2039     // Either we hit the assert, really have no accesses, or we have both
2040     // accesses and an access list. Same with defs.
2041     if (!AL && !DL)
2042       continue;
2043     // Verify ordering.
2044     assert(AL->size() == ActualAccesses.size() &&
2045            "We don't have the same number of accesses in the block as on the "
2046            "access list");
2047     assert((DL || ActualDefs.size() == 0) &&
2048            "Either we should have a defs list, or we should have no defs");
2049     assert((!DL || DL->size() == ActualDefs.size()) &&
2050            "We don't have the same number of defs in the block as on the "
2051            "def list");
2052     auto ALI = AL->begin();
2053     auto AAI = ActualAccesses.begin();
2054     while (ALI != AL->end() && AAI != ActualAccesses.end()) {
2055       assert(&*ALI == *AAI && "Not the same accesses in the same order");
2056       ++ALI;
2057       ++AAI;
2058     }
2059     ActualAccesses.clear();
2060     if (DL) {
2061       auto DLI = DL->begin();
2062       auto ADI = ActualDefs.begin();
2063       while (DLI != DL->end() && ADI != ActualDefs.end()) {
2064         assert(&*DLI == *ADI && "Not the same defs in the same order");
2065         ++DLI;
2066         ++ADI;
2067       }
2068     }
2069     ActualDefs.clear();
2070   }
2071 #endif
2072 }
2073 
2074 /// Verify the def-use lists in MemorySSA, by verifying that \p Use
2075 /// appears in the use list of \p Def.
2076 void MemorySSA::verifyUseInDefs(MemoryAccess *Def, MemoryAccess *Use) const {
2077 #ifndef NDEBUG
2078   // The live on entry use may cause us to get a NULL def here
2079   if (!Def)
2080     assert(isLiveOnEntryDef(Use) &&
2081            "Null def but use not point to live on entry def");
2082   else
2083     assert(is_contained(Def->users(), Use) &&
2084            "Did not find use in def's use list");
2085 #endif
2086 }
2087 
2088 /// Perform a local numbering on blocks so that instruction ordering can be
2089 /// determined in constant time.
2090 /// TODO: We currently just number in order.  If we numbered by N, we could
2091 /// allow at least N-1 sequences of insertBefore or insertAfter (and at least
2092 /// log2(N) sequences of mixed before and after) without needing to invalidate
2093 /// the numbering.
2094 void MemorySSA::renumberBlock(const BasicBlock *B) const {
2095   // The pre-increment ensures the numbers really start at 1.
2096   unsigned long CurrentNumber = 0;
2097   const AccessList *AL = getBlockAccesses(B);
2098   assert(AL != nullptr && "Asking to renumber an empty block");
2099   for (const auto &I : *AL)
2100     BlockNumbering[&I] = ++CurrentNumber;
2101   BlockNumberingValid.insert(B);
2102 }
2103 
2104 /// Determine, for two memory accesses in the same block,
2105 /// whether \p Dominator dominates \p Dominatee.
2106 /// \returns True if \p Dominator dominates \p Dominatee.
2107 bool MemorySSA::locallyDominates(const MemoryAccess *Dominator,
2108                                  const MemoryAccess *Dominatee) const {
2109   const BasicBlock *DominatorBlock = Dominator->getBlock();
2110 
2111   assert((DominatorBlock == Dominatee->getBlock()) &&
2112          "Asking for local domination when accesses are in different blocks!");
2113   // A node dominates itself.
2114   if (Dominatee == Dominator)
2115     return true;
2116 
2117   // When Dominatee is defined on function entry, it is not dominated by another
2118   // memory access.
2119   if (isLiveOnEntryDef(Dominatee))
2120     return false;
2121 
2122   // When Dominator is defined on function entry, it dominates the other memory
2123   // access.
2124   if (isLiveOnEntryDef(Dominator))
2125     return true;
2126 
2127   if (!BlockNumberingValid.count(DominatorBlock))
2128     renumberBlock(DominatorBlock);
2129 
2130   unsigned long DominatorNum = BlockNumbering.lookup(Dominator);
2131   // All numbers start with 1
2132   assert(DominatorNum != 0 && "Block was not numbered properly");
2133   unsigned long DominateeNum = BlockNumbering.lookup(Dominatee);
2134   assert(DominateeNum != 0 && "Block was not numbered properly");
2135   return DominatorNum < DominateeNum;
2136 }
2137 
2138 bool MemorySSA::dominates(const MemoryAccess *Dominator,
2139                           const MemoryAccess *Dominatee) const {
2140   if (Dominator == Dominatee)
2141     return true;
2142 
2143   if (isLiveOnEntryDef(Dominatee))
2144     return false;
2145 
2146   if (Dominator->getBlock() != Dominatee->getBlock())
2147     return DT->dominates(Dominator->getBlock(), Dominatee->getBlock());
2148   return locallyDominates(Dominator, Dominatee);
2149 }
2150 
2151 bool MemorySSA::dominates(const MemoryAccess *Dominator,
2152                           const Use &Dominatee) const {
2153   if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Dominatee.getUser())) {
2154     BasicBlock *UseBB = MP->getIncomingBlock(Dominatee);
2155     // The def must dominate the incoming block of the phi.
2156     if (UseBB != Dominator->getBlock())
2157       return DT->dominates(Dominator->getBlock(), UseBB);
2158     // If the UseBB and the DefBB are the same, compare locally.
2159     return locallyDominates(Dominator, cast<MemoryAccess>(Dominatee));
2160   }
2161   // If it's not a PHI node use, the normal dominates can already handle it.
2162   return dominates(Dominator, cast<MemoryAccess>(Dominatee.getUser()));
2163 }
2164 
2165 const static char LiveOnEntryStr[] = "liveOnEntry";
2166 
2167 void MemoryAccess::print(raw_ostream &OS) const {
2168   switch (getValueID()) {
2169   case MemoryPhiVal: return static_cast<const MemoryPhi *>(this)->print(OS);
2170   case MemoryDefVal: return static_cast<const MemoryDef *>(this)->print(OS);
2171   case MemoryUseVal: return static_cast<const MemoryUse *>(this)->print(OS);
2172   }
2173   llvm_unreachable("invalid value id");
2174 }
2175 
2176 void MemoryDef::print(raw_ostream &OS) const {
2177   MemoryAccess *UO = getDefiningAccess();
2178 
2179   auto printID = [&OS](MemoryAccess *A) {
2180     if (A && A->getID())
2181       OS << A->getID();
2182     else
2183       OS << LiveOnEntryStr;
2184   };
2185 
2186   OS << getID() << " = MemoryDef(";
2187   printID(UO);
2188   OS << ")";
2189 
2190   if (isOptimized()) {
2191     OS << "->";
2192     printID(getOptimized());
2193 
2194     if (Optional<AliasResult> AR = getOptimizedAccessType())
2195       OS << " " << *AR;
2196   }
2197 }
2198 
2199 void MemoryPhi::print(raw_ostream &OS) const {
2200   bool First = true;
2201   OS << getID() << " = MemoryPhi(";
2202   for (const auto &Op : operands()) {
2203     BasicBlock *BB = getIncomingBlock(Op);
2204     MemoryAccess *MA = cast<MemoryAccess>(Op);
2205     if (!First)
2206       OS << ',';
2207     else
2208       First = false;
2209 
2210     OS << '{';
2211     if (BB->hasName())
2212       OS << BB->getName();
2213     else
2214       BB->printAsOperand(OS, false);
2215     OS << ',';
2216     if (unsigned ID = MA->getID())
2217       OS << ID;
2218     else
2219       OS << LiveOnEntryStr;
2220     OS << '}';
2221   }
2222   OS << ')';
2223 }
2224 
2225 void MemoryUse::print(raw_ostream &OS) const {
2226   MemoryAccess *UO = getDefiningAccess();
2227   OS << "MemoryUse(";
2228   if (UO && UO->getID())
2229     OS << UO->getID();
2230   else
2231     OS << LiveOnEntryStr;
2232   OS << ')';
2233 
2234   if (Optional<AliasResult> AR = getOptimizedAccessType())
2235     OS << " " << *AR;
2236 }
2237 
2238 void MemoryAccess::dump() const {
2239 // Cannot completely remove virtual function even in release mode.
2240 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2241   print(dbgs());
2242   dbgs() << "\n";
2243 #endif
2244 }
2245 
2246 char MemorySSAPrinterLegacyPass::ID = 0;
2247 
2248 MemorySSAPrinterLegacyPass::MemorySSAPrinterLegacyPass() : FunctionPass(ID) {
2249   initializeMemorySSAPrinterLegacyPassPass(*PassRegistry::getPassRegistry());
2250 }
2251 
2252 void MemorySSAPrinterLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
2253   AU.setPreservesAll();
2254   AU.addRequired<MemorySSAWrapperPass>();
2255 }
2256 
2257 class DOTFuncMSSAInfo {
2258 private:
2259   const Function &F;
2260   MemorySSAAnnotatedWriter MSSAWriter;
2261 
2262 public:
2263   DOTFuncMSSAInfo(const Function &F, MemorySSA &MSSA)
2264       : F(F), MSSAWriter(&MSSA) {}
2265 
2266   const Function *getFunction() { return &F; }
2267   MemorySSAAnnotatedWriter &getWriter() { return MSSAWriter; }
2268 };
2269 
2270 namespace llvm {
2271 
2272 template <>
2273 struct GraphTraits<DOTFuncMSSAInfo *> : public GraphTraits<const BasicBlock *> {
2274   static NodeRef getEntryNode(DOTFuncMSSAInfo *CFGInfo) {
2275     return &(CFGInfo->getFunction()->getEntryBlock());
2276   }
2277 
2278   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
2279   using nodes_iterator = pointer_iterator<Function::const_iterator>;
2280 
2281   static nodes_iterator nodes_begin(DOTFuncMSSAInfo *CFGInfo) {
2282     return nodes_iterator(CFGInfo->getFunction()->begin());
2283   }
2284 
2285   static nodes_iterator nodes_end(DOTFuncMSSAInfo *CFGInfo) {
2286     return nodes_iterator(CFGInfo->getFunction()->end());
2287   }
2288 
2289   static size_t size(DOTFuncMSSAInfo *CFGInfo) {
2290     return CFGInfo->getFunction()->size();
2291   }
2292 };
2293 
2294 template <>
2295 struct DOTGraphTraits<DOTFuncMSSAInfo *> : public DefaultDOTGraphTraits {
2296 
2297   DOTGraphTraits(bool IsSimple = false) : DefaultDOTGraphTraits(IsSimple) {}
2298 
2299   static std::string getGraphName(DOTFuncMSSAInfo *CFGInfo) {
2300     return "MSSA CFG for '" + CFGInfo->getFunction()->getName().str() +
2301            "' function";
2302   }
2303 
2304   std::string getNodeLabel(const BasicBlock *Node, DOTFuncMSSAInfo *CFGInfo) {
2305     return DOTGraphTraits<DOTFuncInfo *>::getCompleteNodeLabel(
2306         Node, nullptr,
2307         [CFGInfo](raw_string_ostream &OS, const BasicBlock &BB) -> void {
2308           BB.print(OS, &CFGInfo->getWriter(), true, true);
2309         },
2310         [](std::string &S, unsigned &I, unsigned Idx) -> void {
2311           std::string Str = S.substr(I, Idx - I);
2312           StringRef SR = Str;
2313           if (SR.count(" = MemoryDef(") || SR.count(" = MemoryPhi(") ||
2314               SR.count("MemoryUse("))
2315             return;
2316           DOTGraphTraits<DOTFuncInfo *>::eraseComment(S, I, Idx);
2317         });
2318   }
2319 
2320   static std::string getEdgeSourceLabel(const BasicBlock *Node,
2321                                         const_succ_iterator I) {
2322     return DOTGraphTraits<DOTFuncInfo *>::getEdgeSourceLabel(Node, I);
2323   }
2324 
2325   /// Display the raw branch weights from PGO.
2326   std::string getEdgeAttributes(const BasicBlock *Node, const_succ_iterator I,
2327                                 DOTFuncMSSAInfo *CFGInfo) {
2328     return "";
2329   }
2330 
2331   std::string getNodeAttributes(const BasicBlock *Node,
2332                                 DOTFuncMSSAInfo *CFGInfo) {
2333     return getNodeLabel(Node, CFGInfo).find(';') != std::string::npos
2334                ? "style=filled, fillcolor=lightpink"
2335                : "";
2336   }
2337 };
2338 
2339 } // namespace llvm
2340 
2341 bool MemorySSAPrinterLegacyPass::runOnFunction(Function &F) {
2342   auto &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA();
2343   if (DotCFGMSSA != "") {
2344     DOTFuncMSSAInfo CFGInfo(F, MSSA);
2345     WriteGraph(&CFGInfo, "", false, "MSSA", DotCFGMSSA);
2346   } else
2347     MSSA.print(dbgs());
2348 
2349   if (VerifyMemorySSA)
2350     MSSA.verifyMemorySSA();
2351   return false;
2352 }
2353 
2354 AnalysisKey MemorySSAAnalysis::Key;
2355 
2356 MemorySSAAnalysis::Result MemorySSAAnalysis::run(Function &F,
2357                                                  FunctionAnalysisManager &AM) {
2358   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
2359   auto &AA = AM.getResult<AAManager>(F);
2360   return MemorySSAAnalysis::Result(std::make_unique<MemorySSA>(F, &AA, &DT));
2361 }
2362 
2363 bool MemorySSAAnalysis::Result::invalidate(
2364     Function &F, const PreservedAnalyses &PA,
2365     FunctionAnalysisManager::Invalidator &Inv) {
2366   auto PAC = PA.getChecker<MemorySSAAnalysis>();
2367   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
2368          Inv.invalidate<AAManager>(F, PA) ||
2369          Inv.invalidate<DominatorTreeAnalysis>(F, PA);
2370 }
2371 
2372 PreservedAnalyses MemorySSAPrinterPass::run(Function &F,
2373                                             FunctionAnalysisManager &AM) {
2374   auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
2375   if (DotCFGMSSA != "") {
2376     DOTFuncMSSAInfo CFGInfo(F, MSSA);
2377     WriteGraph(&CFGInfo, "", false, "MSSA", DotCFGMSSA);
2378   } else {
2379     OS << "MemorySSA for function: " << F.getName() << "\n";
2380     MSSA.print(OS);
2381   }
2382 
2383   return PreservedAnalyses::all();
2384 }
2385 
2386 PreservedAnalyses MemorySSAVerifierPass::run(Function &F,
2387                                              FunctionAnalysisManager &AM) {
2388   AM.getResult<MemorySSAAnalysis>(F).getMSSA().verifyMemorySSA();
2389 
2390   return PreservedAnalyses::all();
2391 }
2392 
2393 char MemorySSAWrapperPass::ID = 0;
2394 
2395 MemorySSAWrapperPass::MemorySSAWrapperPass() : FunctionPass(ID) {
2396   initializeMemorySSAWrapperPassPass(*PassRegistry::getPassRegistry());
2397 }
2398 
2399 void MemorySSAWrapperPass::releaseMemory() { MSSA.reset(); }
2400 
2401 void MemorySSAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
2402   AU.setPreservesAll();
2403   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
2404   AU.addRequiredTransitive<AAResultsWrapperPass>();
2405 }
2406 
2407 bool MemorySSAWrapperPass::runOnFunction(Function &F) {
2408   auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2409   auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
2410   MSSA.reset(new MemorySSA(F, &AA, &DT));
2411   return false;
2412 }
2413 
2414 void MemorySSAWrapperPass::verifyAnalysis() const {
2415   if (VerifyMemorySSA)
2416     MSSA->verifyMemorySSA();
2417 }
2418 
2419 void MemorySSAWrapperPass::print(raw_ostream &OS, const Module *M) const {
2420   MSSA->print(OS);
2421 }
2422 
2423 MemorySSAWalker::MemorySSAWalker(MemorySSA *M) : MSSA(M) {}
2424 
2425 /// Walk the use-def chains starting at \p StartingAccess and find
2426 /// the MemoryAccess that actually clobbers Loc.
2427 ///
2428 /// \returns our clobbering memory access
2429 template <typename AliasAnalysisType>
2430 MemoryAccess *
2431 MemorySSA::ClobberWalkerBase<AliasAnalysisType>::getClobberingMemoryAccessBase(
2432     MemoryAccess *StartingAccess, const MemoryLocation &Loc,
2433     unsigned &UpwardWalkLimit) {
2434   if (isa<MemoryPhi>(StartingAccess))
2435     return StartingAccess;
2436 
2437   auto *StartingUseOrDef = cast<MemoryUseOrDef>(StartingAccess);
2438   if (MSSA->isLiveOnEntryDef(StartingUseOrDef))
2439     return StartingUseOrDef;
2440 
2441   Instruction *I = StartingUseOrDef->getMemoryInst();
2442 
2443   // Conservatively, fences are always clobbers, so don't perform the walk if we
2444   // hit a fence.
2445   if (!isa<CallBase>(I) && I->isFenceLike())
2446     return StartingUseOrDef;
2447 
2448   UpwardsMemoryQuery Q;
2449   Q.OriginalAccess = StartingUseOrDef;
2450   Q.StartingLoc = Loc;
2451   Q.Inst = nullptr;
2452   Q.IsCall = false;
2453 
2454   // Unlike the other function, do not walk to the def of a def, because we are
2455   // handed something we already believe is the clobbering access.
2456   // We never set SkipSelf to true in Q in this method.
2457   MemoryAccess *DefiningAccess = isa<MemoryUse>(StartingUseOrDef)
2458                                      ? StartingUseOrDef->getDefiningAccess()
2459                                      : StartingUseOrDef;
2460 
2461   MemoryAccess *Clobber =
2462       Walker.findClobber(DefiningAccess, Q, UpwardWalkLimit);
2463   LLVM_DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is ");
2464   LLVM_DEBUG(dbgs() << *StartingUseOrDef << "\n");
2465   LLVM_DEBUG(dbgs() << "Final Memory SSA clobber for " << *I << " is ");
2466   LLVM_DEBUG(dbgs() << *Clobber << "\n");
2467   return Clobber;
2468 }
2469 
2470 template <typename AliasAnalysisType>
2471 MemoryAccess *
2472 MemorySSA::ClobberWalkerBase<AliasAnalysisType>::getClobberingMemoryAccessBase(
2473     MemoryAccess *MA, unsigned &UpwardWalkLimit, bool SkipSelf) {
2474   auto *StartingAccess = dyn_cast<MemoryUseOrDef>(MA);
2475   // If this is a MemoryPhi, we can't do anything.
2476   if (!StartingAccess)
2477     return MA;
2478 
2479   bool IsOptimized = false;
2480 
2481   // If this is an already optimized use or def, return the optimized result.
2482   // Note: Currently, we store the optimized def result in a separate field,
2483   // since we can't use the defining access.
2484   if (StartingAccess->isOptimized()) {
2485     if (!SkipSelf || !isa<MemoryDef>(StartingAccess))
2486       return StartingAccess->getOptimized();
2487     IsOptimized = true;
2488   }
2489 
2490   const Instruction *I = StartingAccess->getMemoryInst();
2491   // We can't sanely do anything with a fence, since they conservatively clobber
2492   // all memory, and have no locations to get pointers from to try to
2493   // disambiguate.
2494   if (!isa<CallBase>(I) && I->isFenceLike())
2495     return StartingAccess;
2496 
2497   UpwardsMemoryQuery Q(I, StartingAccess);
2498 
2499   if (isUseTriviallyOptimizableToLiveOnEntry(*Walker.getAA(), I)) {
2500     MemoryAccess *LiveOnEntry = MSSA->getLiveOnEntryDef();
2501     StartingAccess->setOptimized(LiveOnEntry);
2502     StartingAccess->setOptimizedAccessType(None);
2503     return LiveOnEntry;
2504   }
2505 
2506   MemoryAccess *OptimizedAccess;
2507   if (!IsOptimized) {
2508     // Start with the thing we already think clobbers this location
2509     MemoryAccess *DefiningAccess = StartingAccess->getDefiningAccess();
2510 
2511     // At this point, DefiningAccess may be the live on entry def.
2512     // If it is, we will not get a better result.
2513     if (MSSA->isLiveOnEntryDef(DefiningAccess)) {
2514       StartingAccess->setOptimized(DefiningAccess);
2515       StartingAccess->setOptimizedAccessType(None);
2516       return DefiningAccess;
2517     }
2518 
2519     OptimizedAccess = Walker.findClobber(DefiningAccess, Q, UpwardWalkLimit);
2520     StartingAccess->setOptimized(OptimizedAccess);
2521     if (MSSA->isLiveOnEntryDef(OptimizedAccess))
2522       StartingAccess->setOptimizedAccessType(None);
2523     else if (Q.AR == MustAlias)
2524       StartingAccess->setOptimizedAccessType(MustAlias);
2525   } else
2526     OptimizedAccess = StartingAccess->getOptimized();
2527 
2528   LLVM_DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is ");
2529   LLVM_DEBUG(dbgs() << *StartingAccess << "\n");
2530   LLVM_DEBUG(dbgs() << "Optimized Memory SSA clobber for " << *I << " is ");
2531   LLVM_DEBUG(dbgs() << *OptimizedAccess << "\n");
2532 
2533   MemoryAccess *Result;
2534   if (SkipSelf && isa<MemoryPhi>(OptimizedAccess) &&
2535       isa<MemoryDef>(StartingAccess) && UpwardWalkLimit) {
2536     assert(isa<MemoryDef>(Q.OriginalAccess));
2537     Q.SkipSelfAccess = true;
2538     Result = Walker.findClobber(OptimizedAccess, Q, UpwardWalkLimit);
2539   } else
2540     Result = OptimizedAccess;
2541 
2542   LLVM_DEBUG(dbgs() << "Result Memory SSA clobber [SkipSelf = " << SkipSelf);
2543   LLVM_DEBUG(dbgs() << "] for " << *I << " is " << *Result << "\n");
2544 
2545   return Result;
2546 }
2547 
2548 MemoryAccess *
2549 DoNothingMemorySSAWalker::getClobberingMemoryAccess(MemoryAccess *MA) {
2550   if (auto *Use = dyn_cast<MemoryUseOrDef>(MA))
2551     return Use->getDefiningAccess();
2552   return MA;
2553 }
2554 
2555 MemoryAccess *DoNothingMemorySSAWalker::getClobberingMemoryAccess(
2556     MemoryAccess *StartingAccess, const MemoryLocation &) {
2557   if (auto *Use = dyn_cast<MemoryUseOrDef>(StartingAccess))
2558     return Use->getDefiningAccess();
2559   return StartingAccess;
2560 }
2561 
2562 void MemoryPhi::deleteMe(DerivedUser *Self) {
2563   delete static_cast<MemoryPhi *>(Self);
2564 }
2565 
2566 void MemoryDef::deleteMe(DerivedUser *Self) {
2567   delete static_cast<MemoryDef *>(Self);
2568 }
2569 
2570 void MemoryUse::deleteMe(DerivedUser *Self) {
2571   delete static_cast<MemoryUse *>(Self);
2572 }
2573 
2574 bool upward_defs_iterator::IsGuaranteedLoopInvariant(Value *Ptr) const {
2575   auto IsGuaranteedLoopInvariantBase = [](Value *Ptr) {
2576     Ptr = Ptr->stripPointerCasts();
2577     if (!isa<Instruction>(Ptr))
2578       return true;
2579     return isa<AllocaInst>(Ptr);
2580   };
2581 
2582   Ptr = Ptr->stripPointerCasts();
2583   if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
2584     return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) &&
2585            GEP->hasAllConstantIndices();
2586   }
2587   return IsGuaranteedLoopInvariantBase(Ptr);
2588 }
2589