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