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