1 //===- MemorySSA.cpp - Memory SSA Builder ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MemorySSA class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/MemorySSA.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseMapInfo.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/DepthFirstIterator.h"
19 #include "llvm/ADT/Hashing.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/iterator.h"
26 #include "llvm/ADT/iterator_range.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/IteratedDominanceFrontier.h"
29 #include "llvm/Analysis/MemoryLocation.h"
30 #include "llvm/Config/llvm-config.h"
31 #include "llvm/IR/AssemblyAnnotationWriter.h"
32 #include "llvm/IR/BasicBlock.h"
33 #include "llvm/IR/CallSite.h"
34 #include "llvm/IR/Dominators.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/IR/Instruction.h"
37 #include "llvm/IR/Instructions.h"
38 #include "llvm/IR/IntrinsicInst.h"
39 #include "llvm/IR/Intrinsics.h"
40 #include "llvm/IR/LLVMContext.h"
41 #include "llvm/IR/PassManager.h"
42 #include "llvm/IR/Use.h"
43 #include "llvm/Pass.h"
44 #include "llvm/Support/AtomicOrdering.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/CommandLine.h"
47 #include "llvm/Support/Compiler.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/ErrorHandling.h"
50 #include "llvm/Support/FormattedStream.h"
51 #include "llvm/Support/raw_ostream.h"
60 #define DEBUG_TYPE "memoryssa"
62 INITIALIZE_PASS_BEGIN(MemorySSAWrapperPass, "memoryssa", "Memory SSA", false,
64 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
65 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
66 INITIALIZE_PASS_END(MemorySSAWrapperPass, "memoryssa", "Memory SSA", false,
69 INITIALIZE_PASS_BEGIN(MemorySSAPrinterLegacyPass, "print-memoryssa",
70 "Memory SSA Printer", false, false)
71 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
72 INITIALIZE_PASS_END(MemorySSAPrinterLegacyPass, "print-memoryssa",
73 "Memory SSA Printer", false, false)
75 static cl::opt<unsigned> MaxCheckLimit(
76 "memssa-check-limit", cl::Hidden, cl::init(100),
77 cl::desc("The maximum number of stores/phis MemorySSA"
78 "will consider trying to walk past (default = 100)"));
81 VerifyMemorySSA("verify-memoryssa", cl::init(false), cl::Hidden,
82 cl::desc("Verify MemorySSA in legacy printer pass."));
86 /// An assembly annotator class to print Memory SSA information in
88 class MemorySSAAnnotatedWriter : public AssemblyAnnotationWriter {
89 friend class MemorySSA;
91 const MemorySSA *MSSA;
94 MemorySSAAnnotatedWriter(const MemorySSA *M) : MSSA(M) {}
96 void emitBasicBlockStartAnnot(const BasicBlock *BB,
97 formatted_raw_ostream &OS) override {
98 if (MemoryAccess *MA = MSSA->getMemoryAccess(BB))
99 OS << "; " << *MA << "\n";
102 void emitInstructionAnnot(const Instruction *I,
103 formatted_raw_ostream &OS) override {
104 if (MemoryAccess *MA = MSSA->getMemoryAccess(I))
105 OS << "; " << *MA << "\n";
109 } // end namespace llvm
113 /// Our current alias analysis API differentiates heavily between calls and
114 /// non-calls, and functions called on one usually assert on the other.
115 /// This class encapsulates the distinction to simplify other code that wants
116 /// "Memory affecting instructions and related data" to use as a key.
117 /// For example, this class is used as a densemap key in the use optimizer.
118 class MemoryLocOrCall {
122 MemoryLocOrCall(MemoryUseOrDef *MUD)
123 : MemoryLocOrCall(MUD->getMemoryInst()) {}
124 MemoryLocOrCall(const MemoryUseOrDef *MUD)
125 : MemoryLocOrCall(MUD->getMemoryInst()) {}
127 MemoryLocOrCall(Instruction *Inst) {
128 if (ImmutableCallSite(Inst)) {
130 CS = ImmutableCallSite(Inst);
133 // There is no such thing as a memorylocation for a fence inst, and it is
134 // unique in that regard.
135 if (!isa<FenceInst>(Inst))
136 Loc = MemoryLocation::get(Inst);
140 explicit MemoryLocOrCall(const MemoryLocation &Loc) : Loc(Loc) {}
142 ImmutableCallSite getCS() const {
147 MemoryLocation getLoc() const {
152 bool operator==(const MemoryLocOrCall &Other) const {
153 if (IsCall != Other.IsCall)
157 return Loc == Other.Loc;
159 if (CS.getCalledValue() != Other.CS.getCalledValue())
162 return CS.arg_size() == Other.CS.arg_size() &&
163 std::equal(CS.arg_begin(), CS.arg_end(), Other.CS.arg_begin());
168 ImmutableCallSite CS;
173 } // end anonymous namespace
177 template <> struct DenseMapInfo<MemoryLocOrCall> {
178 static inline MemoryLocOrCall getEmptyKey() {
179 return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getEmptyKey());
182 static inline MemoryLocOrCall getTombstoneKey() {
183 return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getTombstoneKey());
186 static unsigned getHashValue(const MemoryLocOrCall &MLOC) {
190 DenseMapInfo<MemoryLocation>::getHashValue(MLOC.getLoc()));
193 hash_combine(MLOC.IsCall, DenseMapInfo<const Value *>::getHashValue(
194 MLOC.getCS().getCalledValue()));
196 for (const Value *Arg : MLOC.getCS().args())
197 hash = hash_combine(hash, DenseMapInfo<const Value *>::getHashValue(Arg));
201 static bool isEqual(const MemoryLocOrCall &LHS, const MemoryLocOrCall &RHS) {
206 } // end namespace llvm
208 /// This does one-way checks to see if Use could theoretically be hoisted above
209 /// MayClobber. This will not check the other way around.
211 /// This assumes that, for the purposes of MemorySSA, Use comes directly after
212 /// MayClobber, with no potentially clobbering operations in between them.
213 /// (Where potentially clobbering ops are memory barriers, aliased stores, etc.)
214 static bool areLoadsReorderable(const LoadInst *Use,
215 const LoadInst *MayClobber) {
216 bool VolatileUse = Use->isVolatile();
217 bool VolatileClobber = MayClobber->isVolatile();
218 // Volatile operations may never be reordered with other volatile operations.
219 if (VolatileUse && VolatileClobber)
221 // Otherwise, volatile doesn't matter here. From the language reference:
222 // 'optimizers may change the order of volatile operations relative to
223 // non-volatile operations.'"
225 // If a load is seq_cst, it cannot be moved above other loads. If its ordering
226 // is weaker, it can be moved above other loads. We just need to be sure that
227 // MayClobber isn't an acquire load, because loads can't be moved above
230 // Note that this explicitly *does* allow the free reordering of monotonic (or
231 // weaker) loads of the same address.
232 bool SeqCstUse = Use->getOrdering() == AtomicOrdering::SequentiallyConsistent;
233 bool MayClobberIsAcquire = isAtLeastOrStrongerThan(MayClobber->getOrdering(),
234 AtomicOrdering::Acquire);
235 return !(SeqCstUse || MayClobberIsAcquire);
240 struct ClobberAlias {
242 Optional<AliasResult> AR;
245 } // end anonymous namespace
247 // Return a pair of {IsClobber (bool), AR (AliasResult)}. It relies on AR being
248 // ignored if IsClobber = false.
249 static ClobberAlias instructionClobbersQuery(MemoryDef *MD,
250 const MemoryLocation &UseLoc,
251 const Instruction *UseInst,
253 Instruction *DefInst = MD->getMemoryInst();
254 assert(DefInst && "Defining instruction not actually an instruction");
255 ImmutableCallSite UseCS(UseInst);
256 Optional<AliasResult> AR;
258 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(DefInst)) {
259 // These intrinsics will show up as affecting memory, but they are just
262 // FIXME: We probably don't actually want MemorySSA to model these at all
263 // (including creating MemoryAccesses for them): we just end up inventing
264 // clobbers where they don't really exist at all. Please see D43269 for
266 switch (II->getIntrinsicID()) {
267 case Intrinsic::lifetime_start:
269 return {false, NoAlias};
270 AR = AA.alias(MemoryLocation(II->getArgOperand(1)), UseLoc);
271 return {AR != NoAlias, AR};
272 case Intrinsic::lifetime_end:
273 case Intrinsic::invariant_start:
274 case Intrinsic::invariant_end:
275 case Intrinsic::assume:
276 return {false, NoAlias};
283 ModRefInfo I = AA.getModRefInfo(DefInst, UseCS);
284 AR = isMustSet(I) ? MustAlias : MayAlias;
285 return {isModOrRefSet(I), AR};
288 if (auto *DefLoad = dyn_cast<LoadInst>(DefInst))
289 if (auto *UseLoad = dyn_cast<LoadInst>(UseInst))
290 return {!areLoadsReorderable(UseLoad, DefLoad), MayAlias};
292 ModRefInfo I = AA.getModRefInfo(DefInst, UseLoc);
293 AR = isMustSet(I) ? MustAlias : MayAlias;
294 return {isModSet(I), AR};
297 static ClobberAlias instructionClobbersQuery(MemoryDef *MD,
298 const MemoryUseOrDef *MU,
299 const MemoryLocOrCall &UseMLOC,
301 // FIXME: This is a temporary hack to allow a single instructionClobbersQuery
302 // to exist while MemoryLocOrCall is pushed through places.
304 return instructionClobbersQuery(MD, MemoryLocation(), MU->getMemoryInst(),
306 return instructionClobbersQuery(MD, UseMLOC.getLoc(), MU->getMemoryInst(),
310 // Return true when MD may alias MU, return false otherwise.
311 bool MemorySSAUtil::defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU,
313 return instructionClobbersQuery(MD, MU, MemoryLocOrCall(MU), AA).IsClobber;
318 struct UpwardsMemoryQuery {
319 // True if our original query started off as a call
321 // The pointer location we started the query with. This will be empty if
323 MemoryLocation StartingLoc;
324 // This is the instruction we were querying about.
325 const Instruction *Inst = nullptr;
326 // The MemoryAccess we actually got called with, used to test local domination
327 const MemoryAccess *OriginalAccess = nullptr;
328 Optional<AliasResult> AR = MayAlias;
330 UpwardsMemoryQuery() = default;
332 UpwardsMemoryQuery(const Instruction *Inst, const MemoryAccess *Access)
333 : IsCall(ImmutableCallSite(Inst)), Inst(Inst), OriginalAccess(Access) {
335 StartingLoc = MemoryLocation::get(Inst);
339 } // end anonymous namespace
341 static bool lifetimeEndsAt(MemoryDef *MD, const MemoryLocation &Loc,
343 Instruction *Inst = MD->getMemoryInst();
344 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
345 switch (II->getIntrinsicID()) {
346 case Intrinsic::lifetime_end:
347 return AA.isMustAlias(MemoryLocation(II->getArgOperand(1)), Loc);
355 static bool isUseTriviallyOptimizableToLiveOnEntry(AliasAnalysis &AA,
356 const Instruction *I) {
357 // If the memory can't be changed, then loads of the memory can't be
359 return isa<LoadInst>(I) && (I->getMetadata(LLVMContext::MD_invariant_load) ||
360 AA.pointsToConstantMemory(cast<LoadInst>(I)->
361 getPointerOperand()));
364 /// Verifies that `Start` is clobbered by `ClobberAt`, and that nothing
365 /// inbetween `Start` and `ClobberAt` can clobbers `Start`.
367 /// This is meant to be as simple and self-contained as possible. Because it
368 /// uses no cache, etc., it can be relatively expensive.
370 /// \param Start The MemoryAccess that we want to walk from.
371 /// \param ClobberAt A clobber for Start.
372 /// \param StartLoc The MemoryLocation for Start.
373 /// \param MSSA The MemorySSA isntance that Start and ClobberAt belong to.
374 /// \param Query The UpwardsMemoryQuery we used for our search.
375 /// \param AA The AliasAnalysis we used for our search.
376 static void LLVM_ATTRIBUTE_UNUSED
377 checkClobberSanity(MemoryAccess *Start, MemoryAccess *ClobberAt,
378 const MemoryLocation &StartLoc, const MemorySSA &MSSA,
379 const UpwardsMemoryQuery &Query, AliasAnalysis &AA) {
380 assert(MSSA.dominates(ClobberAt, Start) && "Clobber doesn't dominate start?");
382 if (MSSA.isLiveOnEntryDef(Start)) {
383 assert(MSSA.isLiveOnEntryDef(ClobberAt) &&
384 "liveOnEntry must clobber itself");
388 bool FoundClobber = false;
389 DenseSet<MemoryAccessPair> VisitedPhis;
390 SmallVector<MemoryAccessPair, 8> Worklist;
391 Worklist.emplace_back(Start, StartLoc);
392 // Walk all paths from Start to ClobberAt, while looking for clobbers. If one
393 // is found, complain.
394 while (!Worklist.empty()) {
395 MemoryAccessPair MAP = Worklist.pop_back_val();
396 // All we care about is that nothing from Start to ClobberAt clobbers Start.
397 // We learn nothing from revisiting nodes.
398 if (!VisitedPhis.insert(MAP).second)
401 for (MemoryAccess *MA : def_chain(MAP.first)) {
402 if (MA == ClobberAt) {
403 if (auto *MD = dyn_cast<MemoryDef>(MA)) {
404 // instructionClobbersQuery isn't essentially free, so don't use `|=`,
405 // since it won't let us short-circuit.
407 // Also, note that this can't be hoisted out of the `Worklist` loop,
408 // since MD may only act as a clobber for 1 of N MemoryLocations.
409 FoundClobber = FoundClobber || MSSA.isLiveOnEntryDef(MD);
412 instructionClobbersQuery(MD, MAP.second, Query.Inst, AA);
422 // We should never hit liveOnEntry, unless it's the clobber.
423 assert(!MSSA.isLiveOnEntryDef(MA) && "Hit liveOnEntry before clobber?");
425 if (auto *MD = dyn_cast<MemoryDef>(MA)) {
427 assert(!instructionClobbersQuery(MD, MAP.second, Query.Inst, AA)
429 "Found clobber before reaching ClobberAt!");
433 assert(isa<MemoryPhi>(MA));
434 Worklist.append(upward_defs_begin({MA, MAP.second}), upward_defs_end());
438 // If ClobberAt is a MemoryPhi, we can assume something above it acted as a
439 // clobber. Otherwise, `ClobberAt` should've acted as a clobber at some point.
440 assert((isa<MemoryPhi>(ClobberAt) || FoundClobber) &&
441 "ClobberAt never acted as a clobber");
446 /// Our algorithm for walking (and trying to optimize) clobbers, all wrapped up
448 class ClobberWalker {
449 /// Save a few bytes by using unsigned instead of size_t.
450 using ListIndex = unsigned;
452 /// Represents a span of contiguous MemoryDefs, potentially ending in a
456 // Note that, because we always walk in reverse, Last will always dominate
457 // First. Also note that First and Last are inclusive.
460 Optional<ListIndex> Previous;
462 DefPath(const MemoryLocation &Loc, MemoryAccess *First, MemoryAccess *Last,
463 Optional<ListIndex> Previous)
464 : Loc(Loc), First(First), Last(Last), Previous(Previous) {}
466 DefPath(const MemoryLocation &Loc, MemoryAccess *Init,
467 Optional<ListIndex> Previous)
468 : DefPath(Loc, Init, Init, Previous) {}
471 const MemorySSA &MSSA;
474 UpwardsMemoryQuery *Query;
476 // Phi optimization bookkeeping
477 SmallVector<DefPath, 32> Paths;
478 DenseSet<ConstMemoryAccessPair> VisitedPhis;
480 /// Find the nearest def or phi that `From` can legally be optimized to.
481 const MemoryAccess *getWalkTarget(const MemoryPhi *From) const {
482 assert(From->getNumOperands() && "Phi with no operands?");
484 BasicBlock *BB = From->getBlock();
485 MemoryAccess *Result = MSSA.getLiveOnEntryDef();
486 DomTreeNode *Node = DT.getNode(BB);
487 while ((Node = Node->getIDom())) {
488 auto *Defs = MSSA.getBlockDefs(Node->getBlock());
490 return &*Defs->rbegin();
495 /// Result of calling walkToPhiOrClobber.
496 struct UpwardsWalkResult {
497 /// The "Result" of the walk. Either a clobber, the last thing we walked, or
498 /// both. Include alias info when clobber found.
499 MemoryAccess *Result;
501 Optional<AliasResult> AR;
504 /// Walk to the next Phi or Clobber in the def chain starting at Desc.Last.
505 /// This will update Desc.Last as it walks. It will (optionally) also stop at
508 /// This does not test for whether StopAt is a clobber
510 walkToPhiOrClobber(DefPath &Desc,
511 const MemoryAccess *StopAt = nullptr) const {
512 assert(!isa<MemoryUse>(Desc.Last) && "Uses don't exist in my world");
514 for (MemoryAccess *Current : def_chain(Desc.Last)) {
516 if (Current == StopAt)
517 return {Current, false, MayAlias};
519 if (auto *MD = dyn_cast<MemoryDef>(Current)) {
520 if (MSSA.isLiveOnEntryDef(MD))
521 return {MD, true, MustAlias};
523 instructionClobbersQuery(MD, Desc.Loc, Query->Inst, AA);
525 return {MD, true, CA.AR};
529 assert(isa<MemoryPhi>(Desc.Last) &&
530 "Ended at a non-clobber that's not a phi?");
531 return {Desc.Last, false, MayAlias};
534 void addSearches(MemoryPhi *Phi, SmallVectorImpl<ListIndex> &PausedSearches,
535 ListIndex PriorNode) {
536 auto UpwardDefs = make_range(upward_defs_begin({Phi, Paths[PriorNode].Loc}),
538 for (const MemoryAccessPair &P : UpwardDefs) {
539 PausedSearches.push_back(Paths.size());
540 Paths.emplace_back(P.second, P.first, PriorNode);
544 /// Represents a search that terminated after finding a clobber. This clobber
545 /// may or may not be present in the path of defs from LastNode..SearchStart,
546 /// since it may have been retrieved from cache.
547 struct TerminatedPath {
548 MemoryAccess *Clobber;
552 /// Get an access that keeps us from optimizing to the given phi.
554 /// PausedSearches is an array of indices into the Paths array. Its incoming
555 /// value is the indices of searches that stopped at the last phi optimization
556 /// target. It's left in an unspecified state.
558 /// If this returns None, NewPaused is a vector of searches that terminated
559 /// at StopWhere. Otherwise, NewPaused is left in an unspecified state.
560 Optional<TerminatedPath>
561 getBlockingAccess(const MemoryAccess *StopWhere,
562 SmallVectorImpl<ListIndex> &PausedSearches,
563 SmallVectorImpl<ListIndex> &NewPaused,
564 SmallVectorImpl<TerminatedPath> &Terminated) {
565 assert(!PausedSearches.empty() && "No searches to continue?");
567 // BFS vs DFS really doesn't make a difference here, so just do a DFS with
568 // PausedSearches as our stack.
569 while (!PausedSearches.empty()) {
570 ListIndex PathIndex = PausedSearches.pop_back_val();
571 DefPath &Node = Paths[PathIndex];
573 // If we've already visited this path with this MemoryLocation, we don't
574 // need to do so again.
576 // NOTE: That we just drop these paths on the ground makes caching
577 // behavior sporadic. e.g. given a diamond:
582 // ...If we walk D, B, A, C, we'll only cache the result of phi
583 // optimization for A, B, and D; C will be skipped because it dies here.
584 // This arguably isn't the worst thing ever, since:
585 // - We generally query things in a top-down order, so if we got below D
586 // without needing cache entries for {C, MemLoc}, then chances are
587 // that those cache entries would end up ultimately unused.
588 // - We still cache things for A, so C only needs to walk up a bit.
589 // If this behavior becomes problematic, we can fix without a ton of extra
591 if (!VisitedPhis.insert({Node.Last, Node.Loc}).second)
594 UpwardsWalkResult Res = walkToPhiOrClobber(Node, /*StopAt=*/StopWhere);
595 if (Res.IsKnownClobber) {
596 assert(Res.Result != StopWhere);
597 // If this wasn't a cache hit, we hit a clobber when walking. That's a
599 TerminatedPath Term{Res.Result, PathIndex};
600 if (!MSSA.dominates(Res.Result, StopWhere))
603 // Otherwise, it's a valid thing to potentially optimize to.
604 Terminated.push_back(Term);
608 if (Res.Result == StopWhere) {
609 // We've hit our target. Save this path off for if we want to continue
611 NewPaused.push_back(PathIndex);
615 assert(!MSSA.isLiveOnEntryDef(Res.Result) && "liveOnEntry is a clobber");
616 addSearches(cast<MemoryPhi>(Res.Result), PausedSearches, PathIndex);
622 template <typename T, typename Walker>
623 struct generic_def_path_iterator
624 : public iterator_facade_base<generic_def_path_iterator<T, Walker>,
625 std::forward_iterator_tag, T *> {
626 generic_def_path_iterator() = default;
627 generic_def_path_iterator(Walker *W, ListIndex N) : W(W), N(N) {}
629 T &operator*() const { return curNode(); }
631 generic_def_path_iterator &operator++() {
632 N = curNode().Previous;
636 bool operator==(const generic_def_path_iterator &O) const {
637 if (N.hasValue() != O.N.hasValue())
639 return !N.hasValue() || *N == *O.N;
643 T &curNode() const { return W->Paths[*N]; }
646 Optional<ListIndex> N = None;
649 using def_path_iterator = generic_def_path_iterator<DefPath, ClobberWalker>;
650 using const_def_path_iterator =
651 generic_def_path_iterator<const DefPath, const ClobberWalker>;
653 iterator_range<def_path_iterator> def_path(ListIndex From) {
654 return make_range(def_path_iterator(this, From), def_path_iterator());
657 iterator_range<const_def_path_iterator> const_def_path(ListIndex From) const {
658 return make_range(const_def_path_iterator(this, From),
659 const_def_path_iterator());
663 /// The path that contains our result.
664 TerminatedPath PrimaryClobber;
665 /// The paths that we can legally cache back from, but that aren't
666 /// necessarily the result of the Phi optimization.
667 SmallVector<TerminatedPath, 4> OtherClobbers;
670 ListIndex defPathIndex(const DefPath &N) const {
671 // The assert looks nicer if we don't need to do &N
672 const DefPath *NP = &N;
673 assert(!Paths.empty() && NP >= &Paths.front() && NP <= &Paths.back() &&
674 "Out of bounds DefPath!");
675 return NP - &Paths.front();
678 /// Try to optimize a phi as best as we can. Returns a SmallVector of Paths
679 /// that act as legal clobbers. Note that this won't return *all* clobbers.
681 /// Phi optimization algorithm tl;dr:
682 /// - Find the earliest def/phi, A, we can optimize to
683 /// - Find if all paths from the starting memory access ultimately reach A
684 /// - If not, optimization isn't possible.
685 /// - Otherwise, walk from A to another clobber or phi, A'.
686 /// - If A' is a def, we're done.
687 /// - If A' is a phi, try to optimize it.
689 /// A path is a series of {MemoryAccess, MemoryLocation} pairs. A path
690 /// terminates when a MemoryAccess that clobbers said MemoryLocation is found.
691 OptznResult tryOptimizePhi(MemoryPhi *Phi, MemoryAccess *Start,
692 const MemoryLocation &Loc) {
693 assert(Paths.empty() && VisitedPhis.empty() &&
694 "Reset the optimization state.");
696 Paths.emplace_back(Loc, Start, Phi, None);
697 // Stores how many "valid" optimization nodes we had prior to calling
698 // addSearches/getBlockingAccess. Necessary for caching if we had a blocker.
699 auto PriorPathsSize = Paths.size();
701 SmallVector<ListIndex, 16> PausedSearches;
702 SmallVector<ListIndex, 8> NewPaused;
703 SmallVector<TerminatedPath, 4> TerminatedPaths;
705 addSearches(Phi, PausedSearches, 0);
707 // Moves the TerminatedPath with the "most dominated" Clobber to the end of
709 auto MoveDominatedPathToEnd = [&](SmallVectorImpl<TerminatedPath> &Paths) {
710 assert(!Paths.empty() && "Need a path to move");
711 auto Dom = Paths.begin();
712 for (auto I = std::next(Dom), E = Paths.end(); I != E; ++I)
713 if (!MSSA.dominates(I->Clobber, Dom->Clobber))
715 auto Last = Paths.end() - 1;
717 std::iter_swap(Last, Dom);
720 MemoryPhi *Current = Phi;
722 assert(!MSSA.isLiveOnEntryDef(Current) &&
723 "liveOnEntry wasn't treated as a clobber?");
725 const auto *Target = getWalkTarget(Current);
726 // If a TerminatedPath doesn't dominate Target, then it wasn't a legal
727 // optimization for the prior phi.
728 assert(all_of(TerminatedPaths, [&](const TerminatedPath &P) {
729 return MSSA.dominates(P.Clobber, Target);
732 // FIXME: This is broken, because the Blocker may be reported to be
733 // liveOnEntry, and we'll happily wait for that to disappear (read: never)
734 // For the moment, this is fine, since we do nothing with blocker info.
735 if (Optional<TerminatedPath> Blocker = getBlockingAccess(
736 Target, PausedSearches, NewPaused, TerminatedPaths)) {
738 // Find the node we started at. We can't search based on N->Last, since
739 // we may have gone around a loop with a different MemoryLocation.
740 auto Iter = find_if(def_path(Blocker->LastNode), [&](const DefPath &N) {
741 return defPathIndex(N) < PriorPathsSize;
743 assert(Iter != def_path_iterator());
745 DefPath &CurNode = *Iter;
746 assert(CurNode.Last == Current);
749 // A. We can't reliably cache all of NewPaused back. Consider a case
750 // where we have two paths in NewPaused; one of which can't optimize
751 // above this phi, whereas the other can. If we cache the second path
752 // back, we'll end up with suboptimal cache entries. We can handle
753 // cases like this a bit better when we either try to find all
754 // clobbers that block phi optimization, or when our cache starts
755 // supporting unfinished searches.
756 // B. We can't reliably cache TerminatedPaths back here without doing
757 // extra checks; consider a case like:
763 // Where T is our target, C is a node with a clobber on it, D is a
764 // diamond (with a clobber *only* on the left or right node, N), and
765 // S is our start. Say we walk to D, through the node opposite N
766 // (read: ignoring the clobber), and see a cache entry in the top
767 // node of D. That cache entry gets put into TerminatedPaths. We then
768 // walk up to C (N is later in our worklist), find the clobber, and
769 // quit. If we append TerminatedPaths to OtherClobbers, we'll cache
770 // the bottom part of D to the cached clobber, ignoring the clobber
771 // in N. Again, this problem goes away if we start tracking all
772 // blockers for a given phi optimization.
773 TerminatedPath Result{CurNode.Last, defPathIndex(CurNode)};
777 // If there's nothing left to search, then all paths led to valid clobbers
778 // that we got from our cache; pick the nearest to the start, and allow
779 // the rest to be cached back.
780 if (NewPaused.empty()) {
781 MoveDominatedPathToEnd(TerminatedPaths);
782 TerminatedPath Result = TerminatedPaths.pop_back_val();
783 return {Result, std::move(TerminatedPaths)};
786 MemoryAccess *DefChainEnd = nullptr;
787 SmallVector<TerminatedPath, 4> Clobbers;
788 for (ListIndex Paused : NewPaused) {
789 UpwardsWalkResult WR = walkToPhiOrClobber(Paths[Paused]);
790 if (WR.IsKnownClobber)
791 Clobbers.push_back({WR.Result, Paused});
793 // Micro-opt: If we hit the end of the chain, save it.
794 DefChainEnd = WR.Result;
797 if (!TerminatedPaths.empty()) {
798 // If we couldn't find the dominating phi/liveOnEntry in the above loop,
801 for (auto *MA : def_chain(const_cast<MemoryAccess *>(Target)))
804 // If any of the terminated paths don't dominate the phi we'll try to
805 // optimize, we need to figure out what they are and quit.
806 const BasicBlock *ChainBB = DefChainEnd->getBlock();
807 for (const TerminatedPath &TP : TerminatedPaths) {
808 // Because we know that DefChainEnd is as "high" as we can go, we
809 // don't need local dominance checks; BB dominance is sufficient.
810 if (DT.dominates(ChainBB, TP.Clobber->getBlock()))
811 Clobbers.push_back(TP);
815 // If we have clobbers in the def chain, find the one closest to Current
817 if (!Clobbers.empty()) {
818 MoveDominatedPathToEnd(Clobbers);
819 TerminatedPath Result = Clobbers.pop_back_val();
820 return {Result, std::move(Clobbers)};
823 assert(all_of(NewPaused,
824 [&](ListIndex I) { return Paths[I].Last == DefChainEnd; }));
826 // Because liveOnEntry is a clobber, this must be a phi.
827 auto *DefChainPhi = cast<MemoryPhi>(DefChainEnd);
829 PriorPathsSize = Paths.size();
830 PausedSearches.clear();
831 for (ListIndex I : NewPaused)
832 addSearches(DefChainPhi, PausedSearches, I);
835 Current = DefChainPhi;
839 void verifyOptResult(const OptznResult &R) const {
840 assert(all_of(R.OtherClobbers, [&](const TerminatedPath &P) {
841 return MSSA.dominates(P.Clobber, R.PrimaryClobber.Clobber);
845 void resetPhiOptznState() {
851 ClobberWalker(const MemorySSA &MSSA, AliasAnalysis &AA, DominatorTree &DT)
852 : MSSA(MSSA), AA(AA), DT(DT) {}
854 /// Finds the nearest clobber for the given query, optimizing phis if
856 MemoryAccess *findClobber(MemoryAccess *Start, UpwardsMemoryQuery &Q) {
859 MemoryAccess *Current = Start;
860 // This walker pretends uses don't exist. If we're handed one, silently grab
861 // its def. (This has the nice side-effect of ensuring we never cache uses)
862 if (auto *MU = dyn_cast<MemoryUse>(Start))
863 Current = MU->getDefiningAccess();
865 DefPath FirstDesc(Q.StartingLoc, Current, Current, None);
866 // Fast path for the overly-common case (no crazy phi optimization
868 UpwardsWalkResult WalkResult = walkToPhiOrClobber(FirstDesc);
869 MemoryAccess *Result;
870 if (WalkResult.IsKnownClobber) {
871 Result = WalkResult.Result;
872 Q.AR = WalkResult.AR;
874 OptznResult OptRes = tryOptimizePhi(cast<MemoryPhi>(FirstDesc.Last),
875 Current, Q.StartingLoc);
876 verifyOptResult(OptRes);
877 resetPhiOptznState();
878 Result = OptRes.PrimaryClobber.Clobber;
881 #ifdef EXPENSIVE_CHECKS
882 checkClobberSanity(Current, Result, Q.StartingLoc, MSSA, Q, AA);
887 void verify(const MemorySSA *MSSA) { assert(MSSA == &this->MSSA); }
890 struct RenamePassData {
892 DomTreeNode::const_iterator ChildIt;
893 MemoryAccess *IncomingVal;
895 RenamePassData(DomTreeNode *D, DomTreeNode::const_iterator It,
897 : DTN(D), ChildIt(It), IncomingVal(M) {}
899 void swap(RenamePassData &RHS) {
900 std::swap(DTN, RHS.DTN);
901 std::swap(ChildIt, RHS.ChildIt);
902 std::swap(IncomingVal, RHS.IncomingVal);
906 } // end anonymous namespace
910 /// A MemorySSAWalker that does AA walks to disambiguate accesses. It no
911 /// longer does caching on its own, but the name has been retained for the
913 class MemorySSA::CachingWalker final : public MemorySSAWalker {
914 ClobberWalker Walker;
916 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *, UpwardsMemoryQuery &);
919 CachingWalker(MemorySSA *, AliasAnalysis *, DominatorTree *);
920 ~CachingWalker() override = default;
922 using MemorySSAWalker::getClobberingMemoryAccess;
924 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *) override;
925 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *,
926 const MemoryLocation &) override;
927 void invalidateInfo(MemoryAccess *) override;
929 void verify(const MemorySSA *MSSA) override {
930 MemorySSAWalker::verify(MSSA);
935 } // end namespace llvm
937 void MemorySSA::renameSuccessorPhis(BasicBlock *BB, MemoryAccess *IncomingVal,
938 bool RenameAllUses) {
939 // Pass through values to our successors
940 for (const BasicBlock *S : successors(BB)) {
941 auto It = PerBlockAccesses.find(S);
942 // Rename the phi nodes in our successor block
943 if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front()))
945 AccessList *Accesses = It->second.get();
946 auto *Phi = cast<MemoryPhi>(&Accesses->front());
948 int PhiIndex = Phi->getBasicBlockIndex(BB);
949 assert(PhiIndex != -1 && "Incomplete phi during partial rename");
950 Phi->setIncomingValue(PhiIndex, IncomingVal);
952 Phi->addIncoming(IncomingVal, BB);
956 /// Rename a single basic block into MemorySSA form.
957 /// Uses the standard SSA renaming algorithm.
958 /// \returns The new incoming value.
959 MemoryAccess *MemorySSA::renameBlock(BasicBlock *BB, MemoryAccess *IncomingVal,
960 bool RenameAllUses) {
961 auto It = PerBlockAccesses.find(BB);
962 // Skip most processing if the list is empty.
963 if (It != PerBlockAccesses.end()) {
964 AccessList *Accesses = It->second.get();
965 for (MemoryAccess &L : *Accesses) {
966 if (MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(&L)) {
967 if (MUD->getDefiningAccess() == nullptr || RenameAllUses)
968 MUD->setDefiningAccess(IncomingVal);
969 if (isa<MemoryDef>(&L))
979 /// This is the standard SSA renaming algorithm.
981 /// We walk the dominator tree in preorder, renaming accesses, and then filling
982 /// in phi nodes in our successors.
983 void MemorySSA::renamePass(DomTreeNode *Root, MemoryAccess *IncomingVal,
984 SmallPtrSetImpl<BasicBlock *> &Visited,
985 bool SkipVisited, bool RenameAllUses) {
986 SmallVector<RenamePassData, 32> WorkStack;
987 // Skip everything if we already renamed this block and we are skipping.
988 // Note: You can't sink this into the if, because we need it to occur
989 // regardless of whether we skip blocks or not.
990 bool AlreadyVisited = !Visited.insert(Root->getBlock()).second;
991 if (SkipVisited && AlreadyVisited)
994 IncomingVal = renameBlock(Root->getBlock(), IncomingVal, RenameAllUses);
995 renameSuccessorPhis(Root->getBlock(), IncomingVal, RenameAllUses);
996 WorkStack.push_back({Root, Root->begin(), IncomingVal});
998 while (!WorkStack.empty()) {
999 DomTreeNode *Node = WorkStack.back().DTN;
1000 DomTreeNode::const_iterator ChildIt = WorkStack.back().ChildIt;
1001 IncomingVal = WorkStack.back().IncomingVal;
1003 if (ChildIt == Node->end()) {
1004 WorkStack.pop_back();
1006 DomTreeNode *Child = *ChildIt;
1007 ++WorkStack.back().ChildIt;
1008 BasicBlock *BB = Child->getBlock();
1009 // Note: You can't sink this into the if, because we need it to occur
1010 // regardless of whether we skip blocks or not.
1011 AlreadyVisited = !Visited.insert(BB).second;
1012 if (SkipVisited && AlreadyVisited) {
1013 // We already visited this during our renaming, which can happen when
1014 // being asked to rename multiple blocks. Figure out the incoming val,
1015 // which is the last def.
1016 // Incoming value can only change if there is a block def, and in that
1017 // case, it's the last block def in the list.
1018 if (auto *BlockDefs = getWritableBlockDefs(BB))
1019 IncomingVal = &*BlockDefs->rbegin();
1021 IncomingVal = renameBlock(BB, IncomingVal, RenameAllUses);
1022 renameSuccessorPhis(BB, IncomingVal, RenameAllUses);
1023 WorkStack.push_back({Child, Child->begin(), IncomingVal});
1028 /// This handles unreachable block accesses by deleting phi nodes in
1029 /// unreachable blocks, and marking all other unreachable MemoryAccess's as
1030 /// being uses of the live on entry definition.
1031 void MemorySSA::markUnreachableAsLiveOnEntry(BasicBlock *BB) {
1032 assert(!DT->isReachableFromEntry(BB) &&
1033 "Reachable block found while handling unreachable blocks");
1035 // Make sure phi nodes in our reachable successors end up with a
1036 // LiveOnEntryDef for our incoming edge, even though our block is forward
1037 // unreachable. We could just disconnect these blocks from the CFG fully,
1038 // but we do not right now.
1039 for (const BasicBlock *S : successors(BB)) {
1040 if (!DT->isReachableFromEntry(S))
1042 auto It = PerBlockAccesses.find(S);
1043 // Rename the phi nodes in our successor block
1044 if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front()))
1046 AccessList *Accesses = It->second.get();
1047 auto *Phi = cast<MemoryPhi>(&Accesses->front());
1048 Phi->addIncoming(LiveOnEntryDef.get(), BB);
1051 auto It = PerBlockAccesses.find(BB);
1052 if (It == PerBlockAccesses.end())
1055 auto &Accesses = It->second;
1056 for (auto AI = Accesses->begin(), AE = Accesses->end(); AI != AE;) {
1057 auto Next = std::next(AI);
1058 // If we have a phi, just remove it. We are going to replace all
1059 // users with live on entry.
1060 if (auto *UseOrDef = dyn_cast<MemoryUseOrDef>(AI))
1061 UseOrDef->setDefiningAccess(LiveOnEntryDef.get());
1063 Accesses->erase(AI);
1068 MemorySSA::MemorySSA(Function &Func, AliasAnalysis *AA, DominatorTree *DT)
1069 : AA(AA), DT(DT), F(Func), LiveOnEntryDef(nullptr), Walker(nullptr),
1074 MemorySSA::~MemorySSA() {
1075 // Drop all our references
1076 for (const auto &Pair : PerBlockAccesses)
1077 for (MemoryAccess &MA : *Pair.second)
1078 MA.dropAllReferences();
1081 MemorySSA::AccessList *MemorySSA::getOrCreateAccessList(const BasicBlock *BB) {
1082 auto Res = PerBlockAccesses.insert(std::make_pair(BB, nullptr));
1085 Res.first->second = llvm::make_unique<AccessList>();
1086 return Res.first->second.get();
1089 MemorySSA::DefsList *MemorySSA::getOrCreateDefsList(const BasicBlock *BB) {
1090 auto Res = PerBlockDefs.insert(std::make_pair(BB, nullptr));
1093 Res.first->second = llvm::make_unique<DefsList>();
1094 return Res.first->second.get();
1099 /// This class is a batch walker of all MemoryUse's in the program, and points
1100 /// their defining access at the thing that actually clobbers them. Because it
1101 /// is a batch walker that touches everything, it does not operate like the
1102 /// other walkers. This walker is basically performing a top-down SSA renaming
1103 /// pass, where the version stack is used as the cache. This enables it to be
1104 /// significantly more time and memory efficient than using the regular walker,
1105 /// which is walking bottom-up.
1106 class MemorySSA::OptimizeUses {
1108 OptimizeUses(MemorySSA *MSSA, MemorySSAWalker *Walker, AliasAnalysis *AA,
1110 : MSSA(MSSA), Walker(Walker), AA(AA), DT(DT) {
1111 Walker = MSSA->getWalker();
1114 void optimizeUses();
1117 /// This represents where a given memorylocation is in the stack.
1118 struct MemlocStackInfo {
1119 // This essentially is keeping track of versions of the stack. Whenever
1120 // the stack changes due to pushes or pops, these versions increase.
1121 unsigned long StackEpoch;
1122 unsigned long PopEpoch;
1123 // This is the lower bound of places on the stack to check. It is equal to
1124 // the place the last stack walk ended.
1125 // Note: Correctness depends on this being initialized to 0, which densemap
1127 unsigned long LowerBound;
1128 const BasicBlock *LowerBoundBlock;
1129 // This is where the last walk for this memory location ended.
1130 unsigned long LastKill;
1132 Optional<AliasResult> AR;
1135 void optimizeUsesInBlock(const BasicBlock *, unsigned long &, unsigned long &,
1136 SmallVectorImpl<MemoryAccess *> &,
1137 DenseMap<MemoryLocOrCall, MemlocStackInfo> &);
1140 MemorySSAWalker *Walker;
1145 } // end namespace llvm
1147 /// Optimize the uses in a given block This is basically the SSA renaming
1148 /// algorithm, with one caveat: We are able to use a single stack for all
1149 /// MemoryUses. This is because the set of *possible* reaching MemoryDefs is
1150 /// the same for every MemoryUse. The *actual* clobbering MemoryDef is just
1151 /// going to be some position in that stack of possible ones.
1153 /// We track the stack positions that each MemoryLocation needs
1154 /// to check, and last ended at. This is because we only want to check the
1155 /// things that changed since last time. The same MemoryLocation should
1156 /// get clobbered by the same store (getModRefInfo does not use invariantness or
1157 /// things like this, and if they start, we can modify MemoryLocOrCall to
1158 /// include relevant data)
1159 void MemorySSA::OptimizeUses::optimizeUsesInBlock(
1160 const BasicBlock *BB, unsigned long &StackEpoch, unsigned long &PopEpoch,
1161 SmallVectorImpl<MemoryAccess *> &VersionStack,
1162 DenseMap<MemoryLocOrCall, MemlocStackInfo> &LocStackInfo) {
1164 /// If no accesses, nothing to do.
1165 MemorySSA::AccessList *Accesses = MSSA->getWritableBlockAccesses(BB);
1166 if (Accesses == nullptr)
1169 // Pop everything that doesn't dominate the current block off the stack,
1170 // increment the PopEpoch to account for this.
1173 !VersionStack.empty() &&
1174 "Version stack should have liveOnEntry sentinel dominating everything");
1175 BasicBlock *BackBlock = VersionStack.back()->getBlock();
1176 if (DT->dominates(BackBlock, BB))
1178 while (VersionStack.back()->getBlock() == BackBlock)
1179 VersionStack.pop_back();
1183 for (MemoryAccess &MA : *Accesses) {
1184 auto *MU = dyn_cast<MemoryUse>(&MA);
1186 VersionStack.push_back(&MA);
1191 if (isUseTriviallyOptimizableToLiveOnEntry(*AA, MU->getMemoryInst())) {
1192 MU->setDefiningAccess(MSSA->getLiveOnEntryDef(), true, None);
1196 MemoryLocOrCall UseMLOC(MU);
1197 auto &LocInfo = LocStackInfo[UseMLOC];
1198 // If the pop epoch changed, it means we've removed stuff from top of
1199 // stack due to changing blocks. We may have to reset the lower bound or
1201 if (LocInfo.PopEpoch != PopEpoch) {
1202 LocInfo.PopEpoch = PopEpoch;
1203 LocInfo.StackEpoch = StackEpoch;
1204 // If the lower bound was in something that no longer dominates us, we
1205 // have to reset it.
1206 // We can't simply track stack size, because the stack may have had
1207 // pushes/pops in the meantime.
1208 // XXX: This is non-optimal, but only is slower cases with heavily
1209 // branching dominator trees. To get the optimal number of queries would
1210 // be to make lowerbound and lastkill a per-loc stack, and pop it until
1211 // the top of that stack dominates us. This does not seem worth it ATM.
1212 // A much cheaper optimization would be to always explore the deepest
1213 // branch of the dominator tree first. This will guarantee this resets on
1214 // the smallest set of blocks.
1215 if (LocInfo.LowerBoundBlock && LocInfo.LowerBoundBlock != BB &&
1216 !DT->dominates(LocInfo.LowerBoundBlock, BB)) {
1217 // Reset the lower bound of things to check.
1218 // TODO: Some day we should be able to reset to last kill, rather than
1220 LocInfo.LowerBound = 0;
1221 LocInfo.LowerBoundBlock = VersionStack[0]->getBlock();
1222 LocInfo.LastKillValid = false;
1224 } else if (LocInfo.StackEpoch != StackEpoch) {
1225 // If all that has changed is the StackEpoch, we only have to check the
1226 // new things on the stack, because we've checked everything before. In
1227 // this case, the lower bound of things to check remains the same.
1228 LocInfo.PopEpoch = PopEpoch;
1229 LocInfo.StackEpoch = StackEpoch;
1231 if (!LocInfo.LastKillValid) {
1232 LocInfo.LastKill = VersionStack.size() - 1;
1233 LocInfo.LastKillValid = true;
1234 LocInfo.AR = MayAlias;
1237 // At this point, we should have corrected last kill and LowerBound to be
1239 assert(LocInfo.LowerBound < VersionStack.size() &&
1240 "Lower bound out of range");
1241 assert(LocInfo.LastKill < VersionStack.size() &&
1242 "Last kill info out of range");
1243 // In any case, the new upper bound is the top of the stack.
1244 unsigned long UpperBound = VersionStack.size() - 1;
1246 if (UpperBound - LocInfo.LowerBound > MaxCheckLimit) {
1247 LLVM_DEBUG(dbgs() << "MemorySSA skipping optimization of " << *MU << " ("
1248 << *(MU->getMemoryInst()) << ")"
1249 << " because there are "
1250 << UpperBound - LocInfo.LowerBound
1251 << " stores to disambiguate\n");
1252 // Because we did not walk, LastKill is no longer valid, as this may
1253 // have been a kill.
1254 LocInfo.LastKillValid = false;
1257 bool FoundClobberResult = false;
1258 while (UpperBound > LocInfo.LowerBound) {
1259 if (isa<MemoryPhi>(VersionStack[UpperBound])) {
1260 // For phis, use the walker, see where we ended up, go there
1261 Instruction *UseInst = MU->getMemoryInst();
1262 MemoryAccess *Result = Walker->getClobberingMemoryAccess(UseInst);
1263 // We are guaranteed to find it or something is wrong
1264 while (VersionStack[UpperBound] != Result) {
1265 assert(UpperBound != 0);
1268 FoundClobberResult = true;
1272 MemoryDef *MD = cast<MemoryDef>(VersionStack[UpperBound]);
1273 // If the lifetime of the pointer ends at this instruction, it's live on
1275 if (!UseMLOC.IsCall && lifetimeEndsAt(MD, UseMLOC.getLoc(), *AA)) {
1276 // Reset UpperBound to liveOnEntryDef's place in the stack
1278 FoundClobberResult = true;
1279 LocInfo.AR = MustAlias;
1282 ClobberAlias CA = instructionClobbersQuery(MD, MU, UseMLOC, *AA);
1284 FoundClobberResult = true;
1291 // Note: Phis always have AliasResult AR set to MayAlias ATM.
1293 // At the end of this loop, UpperBound is either a clobber, or lower bound
1294 // PHI walking may cause it to be < LowerBound, and in fact, < LastKill.
1295 if (FoundClobberResult || UpperBound < LocInfo.LastKill) {
1296 // We were last killed now by where we got to
1297 if (MSSA->isLiveOnEntryDef(VersionStack[UpperBound]))
1299 MU->setDefiningAccess(VersionStack[UpperBound], true, LocInfo.AR);
1300 LocInfo.LastKill = UpperBound;
1302 // Otherwise, we checked all the new ones, and now we know we can get to
1304 MU->setDefiningAccess(VersionStack[LocInfo.LastKill], true, LocInfo.AR);
1306 LocInfo.LowerBound = VersionStack.size() - 1;
1307 LocInfo.LowerBoundBlock = BB;
1311 /// Optimize uses to point to their actual clobbering definitions.
1312 void MemorySSA::OptimizeUses::optimizeUses() {
1313 SmallVector<MemoryAccess *, 16> VersionStack;
1314 DenseMap<MemoryLocOrCall, MemlocStackInfo> LocStackInfo;
1315 VersionStack.push_back(MSSA->getLiveOnEntryDef());
1317 unsigned long StackEpoch = 1;
1318 unsigned long PopEpoch = 1;
1319 // We perform a non-recursive top-down dominator tree walk.
1320 for (const auto *DomNode : depth_first(DT->getRootNode()))
1321 optimizeUsesInBlock(DomNode->getBlock(), StackEpoch, PopEpoch, VersionStack,
1325 void MemorySSA::placePHINodes(
1326 const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks) {
1327 // Determine where our MemoryPhi's should go
1328 ForwardIDFCalculator IDFs(*DT);
1329 IDFs.setDefiningBlocks(DefiningBlocks);
1330 SmallVector<BasicBlock *, 32> IDFBlocks;
1331 IDFs.calculate(IDFBlocks);
1333 // Now place MemoryPhi nodes.
1334 for (auto &BB : IDFBlocks)
1335 createMemoryPhi(BB);
1338 void MemorySSA::buildMemorySSA() {
1339 // We create an access to represent "live on entry", for things like
1340 // arguments or users of globals, where the memory they use is defined before
1341 // the beginning of the function. We do not actually insert it into the IR.
1342 // We do not define a live on exit for the immediate uses, and thus our
1343 // semantics do *not* imply that something with no immediate uses can simply
1345 BasicBlock &StartingPoint = F.getEntryBlock();
1346 LiveOnEntryDef.reset(new MemoryDef(F.getContext(), nullptr, nullptr,
1347 &StartingPoint, NextID++));
1349 // We maintain lists of memory accesses per-block, trading memory for time. We
1350 // could just look up the memory access for every possible instruction in the
1352 SmallPtrSet<BasicBlock *, 32> DefiningBlocks;
1353 // Go through each block, figure out where defs occur, and chain together all
1355 for (BasicBlock &B : F) {
1356 bool InsertIntoDef = false;
1357 AccessList *Accesses = nullptr;
1358 DefsList *Defs = nullptr;
1359 for (Instruction &I : B) {
1360 MemoryUseOrDef *MUD = createNewAccess(&I);
1365 Accesses = getOrCreateAccessList(&B);
1366 Accesses->push_back(MUD);
1367 if (isa<MemoryDef>(MUD)) {
1368 InsertIntoDef = true;
1370 Defs = getOrCreateDefsList(&B);
1371 Defs->push_back(*MUD);
1375 DefiningBlocks.insert(&B);
1377 placePHINodes(DefiningBlocks);
1379 // Now do regular SSA renaming on the MemoryDef/MemoryUse. Visited will get
1380 // filled in with all blocks.
1381 SmallPtrSet<BasicBlock *, 16> Visited;
1382 renamePass(DT->getRootNode(), LiveOnEntryDef.get(), Visited);
1384 CachingWalker *Walker = getWalkerImpl();
1386 OptimizeUses(this, Walker, AA, DT).optimizeUses();
1388 // Mark the uses in unreachable blocks as live on entry, so that they go
1391 if (!Visited.count(&BB))
1392 markUnreachableAsLiveOnEntry(&BB);
1395 MemorySSAWalker *MemorySSA::getWalker() { return getWalkerImpl(); }
1397 MemorySSA::CachingWalker *MemorySSA::getWalkerImpl() {
1399 return Walker.get();
1401 Walker = llvm::make_unique<CachingWalker>(this, AA, DT);
1402 return Walker.get();
1405 // This is a helper function used by the creation routines. It places NewAccess
1406 // into the access and defs lists for a given basic block, at the given
1408 void MemorySSA::insertIntoListsForBlock(MemoryAccess *NewAccess,
1409 const BasicBlock *BB,
1410 InsertionPlace Point) {
1411 auto *Accesses = getOrCreateAccessList(BB);
1412 if (Point == Beginning) {
1413 // If it's a phi node, it goes first, otherwise, it goes after any phi
1415 if (isa<MemoryPhi>(NewAccess)) {
1416 Accesses->push_front(NewAccess);
1417 auto *Defs = getOrCreateDefsList(BB);
1418 Defs->push_front(*NewAccess);
1420 auto AI = find_if_not(
1421 *Accesses, [](const MemoryAccess &MA) { return isa<MemoryPhi>(MA); });
1422 Accesses->insert(AI, NewAccess);
1423 if (!isa<MemoryUse>(NewAccess)) {
1424 auto *Defs = getOrCreateDefsList(BB);
1425 auto DI = find_if_not(
1426 *Defs, [](const MemoryAccess &MA) { return isa<MemoryPhi>(MA); });
1427 Defs->insert(DI, *NewAccess);
1431 Accesses->push_back(NewAccess);
1432 if (!isa<MemoryUse>(NewAccess)) {
1433 auto *Defs = getOrCreateDefsList(BB);
1434 Defs->push_back(*NewAccess);
1437 BlockNumberingValid.erase(BB);
1440 void MemorySSA::insertIntoListsBefore(MemoryAccess *What, const BasicBlock *BB,
1441 AccessList::iterator InsertPt) {
1442 auto *Accesses = getWritableBlockAccesses(BB);
1443 bool WasEnd = InsertPt == Accesses->end();
1444 Accesses->insert(AccessList::iterator(InsertPt), What);
1445 if (!isa<MemoryUse>(What)) {
1446 auto *Defs = getOrCreateDefsList(BB);
1447 // If we got asked to insert at the end, we have an easy job, just shove it
1448 // at the end. If we got asked to insert before an existing def, we also get
1449 // an iterator. If we got asked to insert before a use, we have to hunt for
1452 Defs->push_back(*What);
1453 } else if (isa<MemoryDef>(InsertPt)) {
1454 Defs->insert(InsertPt->getDefsIterator(), *What);
1456 while (InsertPt != Accesses->end() && !isa<MemoryDef>(InsertPt))
1458 // Either we found a def, or we are inserting at the end
1459 if (InsertPt == Accesses->end())
1460 Defs->push_back(*What);
1462 Defs->insert(InsertPt->getDefsIterator(), *What);
1465 BlockNumberingValid.erase(BB);
1468 // Move What before Where in the IR. The end result is that What will belong to
1469 // the right lists and have the right Block set, but will not otherwise be
1470 // correct. It will not have the right defining access, and if it is a def,
1471 // things below it will not properly be updated.
1472 void MemorySSA::moveTo(MemoryUseOrDef *What, BasicBlock *BB,
1473 AccessList::iterator Where) {
1474 // Keep it in the lookup tables, remove from the lists
1475 removeFromLists(What, false);
1477 insertIntoListsBefore(What, BB, Where);
1480 void MemorySSA::moveTo(MemoryAccess *What, BasicBlock *BB,
1481 InsertionPlace Point) {
1482 if (isa<MemoryPhi>(What)) {
1483 assert(Point == Beginning &&
1484 "Can only move a Phi at the beginning of the block");
1485 // Update lookup table entry
1486 ValueToMemoryAccess.erase(What->getBlock());
1487 bool Inserted = ValueToMemoryAccess.insert({BB, What}).second;
1489 assert(Inserted && "Cannot move a Phi to a block that already has one");
1492 removeFromLists(What, false);
1494 insertIntoListsForBlock(What, BB, Point);
1497 MemoryPhi *MemorySSA::createMemoryPhi(BasicBlock *BB) {
1498 assert(!getMemoryAccess(BB) && "MemoryPhi already exists for this BB");
1499 MemoryPhi *Phi = new MemoryPhi(BB->getContext(), BB, NextID++);
1500 // Phi's always are placed at the front of the block.
1501 insertIntoListsForBlock(Phi, BB, Beginning);
1502 ValueToMemoryAccess[BB] = Phi;
1506 MemoryUseOrDef *MemorySSA::createDefinedAccess(Instruction *I,
1507 MemoryAccess *Definition) {
1508 assert(!isa<PHINode>(I) && "Cannot create a defined access for a PHI");
1509 MemoryUseOrDef *NewAccess = createNewAccess(I);
1511 NewAccess != nullptr &&
1512 "Tried to create a memory access for a non-memory touching instruction");
1513 NewAccess->setDefiningAccess(Definition);
1517 // Return true if the instruction has ordering constraints.
1518 // Note specifically that this only considers stores and loads
1519 // because others are still considered ModRef by getModRefInfo.
1520 static inline bool isOrdered(const Instruction *I) {
1521 if (auto *SI = dyn_cast<StoreInst>(I)) {
1522 if (!SI->isUnordered())
1524 } else if (auto *LI = dyn_cast<LoadInst>(I)) {
1525 if (!LI->isUnordered())
1531 /// Helper function to create new memory accesses
1532 MemoryUseOrDef *MemorySSA::createNewAccess(Instruction *I) {
1533 // The assume intrinsic has a control dependency which we model by claiming
1534 // that it writes arbitrarily. Ignore that fake memory dependency here.
1535 // FIXME: Replace this special casing with a more accurate modelling of
1536 // assume's control dependency.
1537 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
1538 if (II->getIntrinsicID() == Intrinsic::assume)
1541 // Find out what affect this instruction has on memory.
1542 ModRefInfo ModRef = AA->getModRefInfo(I, None);
1543 // The isOrdered check is used to ensure that volatiles end up as defs
1544 // (atomics end up as ModRef right now anyway). Until we separate the
1545 // ordering chain from the memory chain, this enables people to see at least
1546 // some relative ordering to volatiles. Note that getClobberingMemoryAccess
1547 // will still give an answer that bypasses other volatile loads. TODO:
1548 // Separate memory aliasing and ordering into two different chains so that we
1549 // can precisely represent both "what memory will this read/write/is clobbered
1550 // by" and "what instructions can I move this past".
1551 bool Def = isModSet(ModRef) || isOrdered(I);
1552 bool Use = isRefSet(ModRef);
1554 // It's possible for an instruction to not modify memory at all. During
1555 // construction, we ignore them.
1559 MemoryUseOrDef *MUD;
1561 MUD = new MemoryDef(I->getContext(), nullptr, I, I->getParent(), NextID++);
1563 MUD = new MemoryUse(I->getContext(), nullptr, I, I->getParent());
1564 ValueToMemoryAccess[I] = MUD;
1568 /// Returns true if \p Replacer dominates \p Replacee .
1569 bool MemorySSA::dominatesUse(const MemoryAccess *Replacer,
1570 const MemoryAccess *Replacee) const {
1571 if (isa<MemoryUseOrDef>(Replacee))
1572 return DT->dominates(Replacer->getBlock(), Replacee->getBlock());
1573 const auto *MP = cast<MemoryPhi>(Replacee);
1574 // For a phi node, the use occurs in the predecessor block of the phi node.
1575 // Since we may occur multiple times in the phi node, we have to check each
1576 // operand to ensure Replacer dominates each operand where Replacee occurs.
1577 for (const Use &Arg : MP->operands()) {
1578 if (Arg.get() != Replacee &&
1579 !DT->dominates(Replacer->getBlock(), MP->getIncomingBlock(Arg)))
1585 /// Properly remove \p MA from all of MemorySSA's lookup tables.
1586 void MemorySSA::removeFromLookups(MemoryAccess *MA) {
1587 assert(MA->use_empty() &&
1588 "Trying to remove memory access that still has uses");
1589 BlockNumbering.erase(MA);
1590 if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1591 MUD->setDefiningAccess(nullptr);
1592 // Invalidate our walker's cache if necessary
1593 if (!isa<MemoryUse>(MA))
1594 Walker->invalidateInfo(MA);
1597 if (const auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1598 MemoryInst = MUD->getMemoryInst();
1600 MemoryInst = MA->getBlock();
1602 auto VMA = ValueToMemoryAccess.find(MemoryInst);
1603 if (VMA->second == MA)
1604 ValueToMemoryAccess.erase(VMA);
1607 /// Properly remove \p MA from all of MemorySSA's lists.
1609 /// Because of the way the intrusive list and use lists work, it is important to
1610 /// do removal in the right order.
1611 /// ShouldDelete defaults to true, and will cause the memory access to also be
1612 /// deleted, not just removed.
1613 void MemorySSA::removeFromLists(MemoryAccess *MA, bool ShouldDelete) {
1614 BasicBlock *BB = MA->getBlock();
1615 // The access list owns the reference, so we erase it from the non-owning list
1617 if (!isa<MemoryUse>(MA)) {
1618 auto DefsIt = PerBlockDefs.find(BB);
1619 std::unique_ptr<DefsList> &Defs = DefsIt->second;
1622 PerBlockDefs.erase(DefsIt);
1625 // The erase call here will delete it. If we don't want it deleted, we call
1627 auto AccessIt = PerBlockAccesses.find(BB);
1628 std::unique_ptr<AccessList> &Accesses = AccessIt->second;
1630 Accesses->erase(MA);
1632 Accesses->remove(MA);
1634 if (Accesses->empty()) {
1635 PerBlockAccesses.erase(AccessIt);
1636 BlockNumberingValid.erase(BB);
1640 void MemorySSA::print(raw_ostream &OS) const {
1641 MemorySSAAnnotatedWriter Writer(this);
1642 F.print(OS, &Writer);
1645 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1646 LLVM_DUMP_METHOD void MemorySSA::dump() const { print(dbgs()); }
1649 void MemorySSA::verifyMemorySSA() const {
1651 verifyDomination(F);
1653 verifyDominationNumbers(F);
1654 Walker->verify(this);
1657 /// Verify that all of the blocks we believe to have valid domination numbers
1658 /// actually have valid domination numbers.
1659 void MemorySSA::verifyDominationNumbers(const Function &F) const {
1661 if (BlockNumberingValid.empty())
1664 SmallPtrSet<const BasicBlock *, 16> ValidBlocks = BlockNumberingValid;
1665 for (const BasicBlock &BB : F) {
1666 if (!ValidBlocks.count(&BB))
1669 ValidBlocks.erase(&BB);
1671 const AccessList *Accesses = getBlockAccesses(&BB);
1672 // It's correct to say an empty block has valid numbering.
1676 // Block numbering starts at 1.
1677 unsigned long LastNumber = 0;
1678 for (const MemoryAccess &MA : *Accesses) {
1679 auto ThisNumberIter = BlockNumbering.find(&MA);
1680 assert(ThisNumberIter != BlockNumbering.end() &&
1681 "MemoryAccess has no domination number in a valid block!");
1683 unsigned long ThisNumber = ThisNumberIter->second;
1684 assert(ThisNumber > LastNumber &&
1685 "Domination numbers should be strictly increasing!");
1686 LastNumber = ThisNumber;
1690 assert(ValidBlocks.empty() &&
1691 "All valid BasicBlocks should exist in F -- dangling pointers?");
1695 /// Verify that the order and existence of MemoryAccesses matches the
1696 /// order and existence of memory affecting instructions.
1697 void MemorySSA::verifyOrdering(Function &F) const {
1698 // Walk all the blocks, comparing what the lookups think and what the access
1699 // lists think, as well as the order in the blocks vs the order in the access
1701 SmallVector<MemoryAccess *, 32> ActualAccesses;
1702 SmallVector<MemoryAccess *, 32> ActualDefs;
1703 for (BasicBlock &B : F) {
1704 const AccessList *AL = getBlockAccesses(&B);
1705 const auto *DL = getBlockDefs(&B);
1706 MemoryAccess *Phi = getMemoryAccess(&B);
1708 ActualAccesses.push_back(Phi);
1709 ActualDefs.push_back(Phi);
1712 for (Instruction &I : B) {
1713 MemoryAccess *MA = getMemoryAccess(&I);
1714 assert((!MA || (AL && (isa<MemoryUse>(MA) || DL))) &&
1715 "We have memory affecting instructions "
1716 "in this block but they are not in the "
1717 "access list or defs list");
1719 ActualAccesses.push_back(MA);
1720 if (isa<MemoryDef>(MA))
1721 ActualDefs.push_back(MA);
1724 // Either we hit the assert, really have no accesses, or we have both
1725 // accesses and an access list.
1729 assert(AL->size() == ActualAccesses.size() &&
1730 "We don't have the same number of accesses in the block as on the "
1732 assert((DL || ActualDefs.size() == 0) &&
1733 "Either we should have a defs list, or we should have no defs");
1734 assert((!DL || DL->size() == ActualDefs.size()) &&
1735 "We don't have the same number of defs in the block as on the "
1737 auto ALI = AL->begin();
1738 auto AAI = ActualAccesses.begin();
1739 while (ALI != AL->end() && AAI != ActualAccesses.end()) {
1740 assert(&*ALI == *AAI && "Not the same accesses in the same order");
1744 ActualAccesses.clear();
1746 auto DLI = DL->begin();
1747 auto ADI = ActualDefs.begin();
1748 while (DLI != DL->end() && ADI != ActualDefs.end()) {
1749 assert(&*DLI == *ADI && "Not the same defs in the same order");
1758 /// Verify the domination properties of MemorySSA by checking that each
1759 /// definition dominates all of its uses.
1760 void MemorySSA::verifyDomination(Function &F) const {
1762 for (BasicBlock &B : F) {
1763 // Phi nodes are attached to basic blocks
1764 if (MemoryPhi *MP = getMemoryAccess(&B))
1765 for (const Use &U : MP->uses())
1766 assert(dominates(MP, U) && "Memory PHI does not dominate it's uses");
1768 for (Instruction &I : B) {
1769 MemoryAccess *MD = dyn_cast_or_null<MemoryDef>(getMemoryAccess(&I));
1773 for (const Use &U : MD->uses())
1774 assert(dominates(MD, U) && "Memory Def does not dominate it's uses");
1780 /// Verify the def-use lists in MemorySSA, by verifying that \p Use
1781 /// appears in the use list of \p Def.
1782 void MemorySSA::verifyUseInDefs(MemoryAccess *Def, MemoryAccess *Use) const {
1784 // The live on entry use may cause us to get a NULL def here
1786 assert(isLiveOnEntryDef(Use) &&
1787 "Null def but use not point to live on entry def");
1789 assert(is_contained(Def->users(), Use) &&
1790 "Did not find use in def's use list");
1794 /// Verify the immediate use information, by walking all the memory
1795 /// accesses and verifying that, for each use, it appears in the
1796 /// appropriate def's use list
1797 void MemorySSA::verifyDefUses(Function &F) const {
1798 for (BasicBlock &B : F) {
1799 // Phi nodes are attached to basic blocks
1800 if (MemoryPhi *Phi = getMemoryAccess(&B)) {
1801 assert(Phi->getNumOperands() == static_cast<unsigned>(std::distance(
1802 pred_begin(&B), pred_end(&B))) &&
1803 "Incomplete MemoryPhi Node");
1804 for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
1805 verifyUseInDefs(Phi->getIncomingValue(I), Phi);
1806 assert(find(predecessors(&B), Phi->getIncomingBlock(I)) !=
1808 "Incoming phi block not a block predecessor");
1812 for (Instruction &I : B) {
1813 if (MemoryUseOrDef *MA = getMemoryAccess(&I)) {
1814 verifyUseInDefs(MA->getDefiningAccess(), MA);
1820 MemoryUseOrDef *MemorySSA::getMemoryAccess(const Instruction *I) const {
1821 return cast_or_null<MemoryUseOrDef>(ValueToMemoryAccess.lookup(I));
1824 MemoryPhi *MemorySSA::getMemoryAccess(const BasicBlock *BB) const {
1825 return cast_or_null<MemoryPhi>(ValueToMemoryAccess.lookup(cast<Value>(BB)));
1828 /// Perform a local numbering on blocks so that instruction ordering can be
1829 /// determined in constant time.
1830 /// TODO: We currently just number in order. If we numbered by N, we could
1831 /// allow at least N-1 sequences of insertBefore or insertAfter (and at least
1832 /// log2(N) sequences of mixed before and after) without needing to invalidate
1834 void MemorySSA::renumberBlock(const BasicBlock *B) const {
1835 // The pre-increment ensures the numbers really start at 1.
1836 unsigned long CurrentNumber = 0;
1837 const AccessList *AL = getBlockAccesses(B);
1838 assert(AL != nullptr && "Asking to renumber an empty block");
1839 for (const auto &I : *AL)
1840 BlockNumbering[&I] = ++CurrentNumber;
1841 BlockNumberingValid.insert(B);
1844 /// Determine, for two memory accesses in the same block,
1845 /// whether \p Dominator dominates \p Dominatee.
1846 /// \returns True if \p Dominator dominates \p Dominatee.
1847 bool MemorySSA::locallyDominates(const MemoryAccess *Dominator,
1848 const MemoryAccess *Dominatee) const {
1849 const BasicBlock *DominatorBlock = Dominator->getBlock();
1851 assert((DominatorBlock == Dominatee->getBlock()) &&
1852 "Asking for local domination when accesses are in different blocks!");
1853 // A node dominates itself.
1854 if (Dominatee == Dominator)
1857 // When Dominatee is defined on function entry, it is not dominated by another
1859 if (isLiveOnEntryDef(Dominatee))
1862 // When Dominator is defined on function entry, it dominates the other memory
1864 if (isLiveOnEntryDef(Dominator))
1867 if (!BlockNumberingValid.count(DominatorBlock))
1868 renumberBlock(DominatorBlock);
1870 unsigned long DominatorNum = BlockNumbering.lookup(Dominator);
1871 // All numbers start with 1
1872 assert(DominatorNum != 0 && "Block was not numbered properly");
1873 unsigned long DominateeNum = BlockNumbering.lookup(Dominatee);
1874 assert(DominateeNum != 0 && "Block was not numbered properly");
1875 return DominatorNum < DominateeNum;
1878 bool MemorySSA::dominates(const MemoryAccess *Dominator,
1879 const MemoryAccess *Dominatee) const {
1880 if (Dominator == Dominatee)
1883 if (isLiveOnEntryDef(Dominatee))
1886 if (Dominator->getBlock() != Dominatee->getBlock())
1887 return DT->dominates(Dominator->getBlock(), Dominatee->getBlock());
1888 return locallyDominates(Dominator, Dominatee);
1891 bool MemorySSA::dominates(const MemoryAccess *Dominator,
1892 const Use &Dominatee) const {
1893 if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Dominatee.getUser())) {
1894 BasicBlock *UseBB = MP->getIncomingBlock(Dominatee);
1895 // The def must dominate the incoming block of the phi.
1896 if (UseBB != Dominator->getBlock())
1897 return DT->dominates(Dominator->getBlock(), UseBB);
1898 // If the UseBB and the DefBB are the same, compare locally.
1899 return locallyDominates(Dominator, cast<MemoryAccess>(Dominatee));
1901 // If it's not a PHI node use, the normal dominates can already handle it.
1902 return dominates(Dominator, cast<MemoryAccess>(Dominatee.getUser()));
1905 const static char LiveOnEntryStr[] = "liveOnEntry";
1907 void MemoryAccess::print(raw_ostream &OS) const {
1908 switch (getValueID()) {
1909 case MemoryPhiVal: return static_cast<const MemoryPhi *>(this)->print(OS);
1910 case MemoryDefVal: return static_cast<const MemoryDef *>(this)->print(OS);
1911 case MemoryUseVal: return static_cast<const MemoryUse *>(this)->print(OS);
1913 llvm_unreachable("invalid value id");
1916 void MemoryDef::print(raw_ostream &OS) const {
1917 MemoryAccess *UO = getDefiningAccess();
1919 auto printID = [&OS](MemoryAccess *A) {
1920 if (A && A->getID())
1923 OS << LiveOnEntryStr;
1926 OS << getID() << " = MemoryDef(";
1930 if (isOptimized()) {
1932 printID(getOptimized());
1934 if (Optional<AliasResult> AR = getOptimizedAccessType())
1939 void MemoryPhi::print(raw_ostream &OS) const {
1941 OS << getID() << " = MemoryPhi(";
1942 for (const auto &Op : operands()) {
1943 BasicBlock *BB = getIncomingBlock(Op);
1944 MemoryAccess *MA = cast<MemoryAccess>(Op);
1952 OS << BB->getName();
1954 BB->printAsOperand(OS, false);
1956 if (unsigned ID = MA->getID())
1959 OS << LiveOnEntryStr;
1965 void MemoryUse::print(raw_ostream &OS) const {
1966 MemoryAccess *UO = getDefiningAccess();
1968 if (UO && UO->getID())
1971 OS << LiveOnEntryStr;
1974 if (Optional<AliasResult> AR = getOptimizedAccessType())
1978 void MemoryAccess::dump() const {
1979 // Cannot completely remove virtual function even in release mode.
1980 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1986 char MemorySSAPrinterLegacyPass::ID = 0;
1988 MemorySSAPrinterLegacyPass::MemorySSAPrinterLegacyPass() : FunctionPass(ID) {
1989 initializeMemorySSAPrinterLegacyPassPass(*PassRegistry::getPassRegistry());
1992 void MemorySSAPrinterLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
1993 AU.setPreservesAll();
1994 AU.addRequired<MemorySSAWrapperPass>();
1997 bool MemorySSAPrinterLegacyPass::runOnFunction(Function &F) {
1998 auto &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA();
2000 if (VerifyMemorySSA)
2001 MSSA.verifyMemorySSA();
2005 AnalysisKey MemorySSAAnalysis::Key;
2007 MemorySSAAnalysis::Result MemorySSAAnalysis::run(Function &F,
2008 FunctionAnalysisManager &AM) {
2009 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
2010 auto &AA = AM.getResult<AAManager>(F);
2011 return MemorySSAAnalysis::Result(llvm::make_unique<MemorySSA>(F, &AA, &DT));
2014 PreservedAnalyses MemorySSAPrinterPass::run(Function &F,
2015 FunctionAnalysisManager &AM) {
2016 OS << "MemorySSA for function: " << F.getName() << "\n";
2017 AM.getResult<MemorySSAAnalysis>(F).getMSSA().print(OS);
2019 return PreservedAnalyses::all();
2022 PreservedAnalyses MemorySSAVerifierPass::run(Function &F,
2023 FunctionAnalysisManager &AM) {
2024 AM.getResult<MemorySSAAnalysis>(F).getMSSA().verifyMemorySSA();
2026 return PreservedAnalyses::all();
2029 char MemorySSAWrapperPass::ID = 0;
2031 MemorySSAWrapperPass::MemorySSAWrapperPass() : FunctionPass(ID) {
2032 initializeMemorySSAWrapperPassPass(*PassRegistry::getPassRegistry());
2035 void MemorySSAWrapperPass::releaseMemory() { MSSA.reset(); }
2037 void MemorySSAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
2038 AU.setPreservesAll();
2039 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
2040 AU.addRequiredTransitive<AAResultsWrapperPass>();
2043 bool MemorySSAWrapperPass::runOnFunction(Function &F) {
2044 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2045 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
2046 MSSA.reset(new MemorySSA(F, &AA, &DT));
2050 void MemorySSAWrapperPass::verifyAnalysis() const { MSSA->verifyMemorySSA(); }
2052 void MemorySSAWrapperPass::print(raw_ostream &OS, const Module *M) const {
2056 MemorySSAWalker::MemorySSAWalker(MemorySSA *M) : MSSA(M) {}
2058 MemorySSA::CachingWalker::CachingWalker(MemorySSA *M, AliasAnalysis *A,
2060 : MemorySSAWalker(M), Walker(*M, *A, *D) {}
2062 void MemorySSA::CachingWalker::invalidateInfo(MemoryAccess *MA) {
2063 if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
2064 MUD->resetOptimized();
2067 /// Walk the use-def chains starting at \p MA and find
2068 /// the MemoryAccess that actually clobbers Loc.
2070 /// \returns our clobbering memory access
2071 MemoryAccess *MemorySSA::CachingWalker::getClobberingMemoryAccess(
2072 MemoryAccess *StartingAccess, UpwardsMemoryQuery &Q) {
2073 return Walker.findClobber(StartingAccess, Q);
2076 MemoryAccess *MemorySSA::CachingWalker::getClobberingMemoryAccess(
2077 MemoryAccess *StartingAccess, const MemoryLocation &Loc) {
2078 if (isa<MemoryPhi>(StartingAccess))
2079 return StartingAccess;
2081 auto *StartingUseOrDef = cast<MemoryUseOrDef>(StartingAccess);
2082 if (MSSA->isLiveOnEntryDef(StartingUseOrDef))
2083 return StartingUseOrDef;
2085 Instruction *I = StartingUseOrDef->getMemoryInst();
2087 // Conservatively, fences are always clobbers, so don't perform the walk if we
2089 if (!ImmutableCallSite(I) && I->isFenceLike())
2090 return StartingUseOrDef;
2092 UpwardsMemoryQuery Q;
2093 Q.OriginalAccess = StartingUseOrDef;
2094 Q.StartingLoc = Loc;
2098 // Unlike the other function, do not walk to the def of a def, because we are
2099 // handed something we already believe is the clobbering access.
2100 MemoryAccess *DefiningAccess = isa<MemoryUse>(StartingUseOrDef)
2101 ? StartingUseOrDef->getDefiningAccess()
2104 MemoryAccess *Clobber = getClobberingMemoryAccess(DefiningAccess, Q);
2105 LLVM_DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is ");
2106 LLVM_DEBUG(dbgs() << *StartingUseOrDef << "\n");
2107 LLVM_DEBUG(dbgs() << "Final Memory SSA clobber for " << *I << " is ");
2108 LLVM_DEBUG(dbgs() << *Clobber << "\n");
2113 MemorySSA::CachingWalker::getClobberingMemoryAccess(MemoryAccess *MA) {
2114 auto *StartingAccess = dyn_cast<MemoryUseOrDef>(MA);
2115 // If this is a MemoryPhi, we can't do anything.
2116 if (!StartingAccess)
2119 // If this is an already optimized use or def, return the optimized result.
2120 // Note: Currently, we store the optimized def result in a separate field,
2121 // since we can't use the defining access.
2122 if (StartingAccess->isOptimized())
2123 return StartingAccess->getOptimized();
2125 const Instruction *I = StartingAccess->getMemoryInst();
2126 UpwardsMemoryQuery Q(I, StartingAccess);
2127 // We can't sanely do anything with a fence, since they conservatively clobber
2128 // all memory, and have no locations to get pointers from to try to
2130 if (!Q.IsCall && I->isFenceLike())
2131 return StartingAccess;
2133 if (isUseTriviallyOptimizableToLiveOnEntry(*MSSA->AA, I)) {
2134 MemoryAccess *LiveOnEntry = MSSA->getLiveOnEntryDef();
2135 StartingAccess->setOptimized(LiveOnEntry);
2136 StartingAccess->setOptimizedAccessType(None);
2140 // Start with the thing we already think clobbers this location
2141 MemoryAccess *DefiningAccess = StartingAccess->getDefiningAccess();
2143 // At this point, DefiningAccess may be the live on entry def.
2144 // If it is, we will not get a better result.
2145 if (MSSA->isLiveOnEntryDef(DefiningAccess)) {
2146 StartingAccess->setOptimized(DefiningAccess);
2147 StartingAccess->setOptimizedAccessType(None);
2148 return DefiningAccess;
2151 MemoryAccess *Result = getClobberingMemoryAccess(DefiningAccess, Q);
2152 LLVM_DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is ");
2153 LLVM_DEBUG(dbgs() << *DefiningAccess << "\n");
2154 LLVM_DEBUG(dbgs() << "Final Memory SSA clobber for " << *I << " is ");
2155 LLVM_DEBUG(dbgs() << *Result << "\n");
2157 StartingAccess->setOptimized(Result);
2158 if (MSSA->isLiveOnEntryDef(Result))
2159 StartingAccess->setOptimizedAccessType(None);
2160 else if (Q.AR == MustAlias)
2161 StartingAccess->setOptimizedAccessType(MustAlias);
2167 DoNothingMemorySSAWalker::getClobberingMemoryAccess(MemoryAccess *MA) {
2168 if (auto *Use = dyn_cast<MemoryUseOrDef>(MA))
2169 return Use->getDefiningAccess();
2173 MemoryAccess *DoNothingMemorySSAWalker::getClobberingMemoryAccess(
2174 MemoryAccess *StartingAccess, const MemoryLocation &) {
2175 if (auto *Use = dyn_cast<MemoryUseOrDef>(StartingAccess))
2176 return Use->getDefiningAccess();
2177 return StartingAccess;
2180 void MemoryPhi::deleteMe(DerivedUser *Self) {
2181 delete static_cast<MemoryPhi *>(Self);
2184 void MemoryDef::deleteMe(DerivedUser *Self) {
2185 delete static_cast<MemoryDef *>(Self);
2188 void MemoryUse::deleteMe(DerivedUser *Self) {
2189 delete static_cast<MemoryUse *>(Self);