LLVM  4.0.0
MemorySSA.cpp
Go to the documentation of this file.
1 //===-- MemorySSA.cpp - Memory SSA Builder---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------===//
9 //
10 // This file implements the MemorySSA class.
11 //
12 //===----------------------------------------------------------------===//
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/GraphTraits.h"
19 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallSet.h"
23 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Analysis/CFG.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/Dominators.h"
33 #include "llvm/IR/GlobalVariable.h"
34 #include "llvm/IR/IRBuilder.h"
35 #include "llvm/IR/IntrinsicInst.h"
36 #include "llvm/IR/LLVMContext.h"
37 #include "llvm/IR/Metadata.h"
38 #include "llvm/IR/Module.h"
39 #include "llvm/IR/PatternMatch.h"
40 #include "llvm/Support/Debug.h"
42 #include "llvm/Transforms/Scalar.h"
43 #include <algorithm>
44 
45 #define DEBUG_TYPE "memoryssa"
46 using namespace llvm;
47 STATISTIC(NumClobberCacheLookups, "Number of Memory SSA version cache lookups");
48 STATISTIC(NumClobberCacheHits, "Number of Memory SSA version cache hits");
49 STATISTIC(NumClobberCacheInserts, "Number of MemorySSA version cache inserts");
50 
51 INITIALIZE_PASS_BEGIN(MemorySSAWrapperPass, "memoryssa", "Memory SSA", false,
52  true)
56  true)
57 
59  "Memory SSA Printer", false, false)
60 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
61 INITIALIZE_PASS_END(MemorySSAPrinterLegacyPass, "print-memoryssa",
62  "Memory SSA Printer", false, false)
63 
64 static cl::opt<unsigned> MaxCheckLimit(
65  "memssa-check-limit", cl::Hidden, cl::init(100),
66  cl::desc("The maximum number of stores/phis MemorySSA"
67  "will consider trying to walk past (default = 100)"));
68 
69 static cl::opt<bool>
70  VerifyMemorySSA("verify-memoryssa", cl::init(false), cl::Hidden,
71  cl::desc("Verify MemorySSA in legacy printer pass."));
72 
73 namespace llvm {
74 /// \brief An assembly annotator class to print Memory SSA information in
75 /// comments.
77  friend class MemorySSA;
78  const MemorySSA *MSSA;
79 
80 public:
81  MemorySSAAnnotatedWriter(const MemorySSA *M) : MSSA(M) {}
82 
83  virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
85  if (MemoryAccess *MA = MSSA->getMemoryAccess(BB))
86  OS << "; " << *MA << "\n";
87  }
88 
89  virtual void emitInstructionAnnot(const Instruction *I,
91  if (MemoryAccess *MA = MSSA->getMemoryAccess(I))
92  OS << "; " << *MA << "\n";
93  }
94 };
95 }
96 
97 namespace {
98 /// Our current alias analysis API differentiates heavily between calls and
99 /// non-calls, and functions called on one usually assert on the other.
100 /// This class encapsulates the distinction to simplify other code that wants
101 /// "Memory affecting instructions and related data" to use as a key.
102 /// For example, this class is used as a densemap key in the use optimizer.
103 class MemoryLocOrCall {
104 public:
105  MemoryLocOrCall() : IsCall(false) {}
106  MemoryLocOrCall(MemoryUseOrDef *MUD)
107  : MemoryLocOrCall(MUD->getMemoryInst()) {}
108  MemoryLocOrCall(const MemoryUseOrDef *MUD)
109  : MemoryLocOrCall(MUD->getMemoryInst()) {}
110 
111  MemoryLocOrCall(Instruction *Inst) {
112  if (ImmutableCallSite(Inst)) {
113  IsCall = true;
114  CS = ImmutableCallSite(Inst);
115  } else {
116  IsCall = false;
117  // There is no such thing as a memorylocation for a fence inst, and it is
118  // unique in that regard.
119  if (!isa<FenceInst>(Inst))
120  Loc = MemoryLocation::get(Inst);
121  }
122  }
123 
124  explicit MemoryLocOrCall(const MemoryLocation &Loc)
125  : IsCall(false), Loc(Loc) {}
126 
127  bool IsCall;
128  ImmutableCallSite getCS() const {
129  assert(IsCall);
130  return CS;
131  }
132  MemoryLocation getLoc() const {
133  assert(!IsCall);
134  return Loc;
135  }
136 
137  bool operator==(const MemoryLocOrCall &Other) const {
138  if (IsCall != Other.IsCall)
139  return false;
140 
141  if (IsCall)
142  return CS.getCalledValue() == Other.CS.getCalledValue();
143  return Loc == Other.Loc;
144  }
145 
146 private:
147  union {
149  MemoryLocation Loc;
150  };
151 };
152 }
153 
154 namespace llvm {
155 template <> struct DenseMapInfo<MemoryLocOrCall> {
156  static inline MemoryLocOrCall getEmptyKey() {
157  return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getEmptyKey());
158  }
159  static inline MemoryLocOrCall getTombstoneKey() {
160  return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getTombstoneKey());
161  }
162  static unsigned getHashValue(const MemoryLocOrCall &MLOC) {
163  if (MLOC.IsCall)
164  return hash_combine(MLOC.IsCall,
166  MLOC.getCS().getCalledValue()));
167  return hash_combine(
168  MLOC.IsCall, DenseMapInfo<MemoryLocation>::getHashValue(MLOC.getLoc()));
169  }
170  static bool isEqual(const MemoryLocOrCall &LHS, const MemoryLocOrCall &RHS) {
171  return LHS == RHS;
172  }
173 };
174 
176 
177 /// This does one-way checks to see if Use could theoretically be hoisted above
178 /// MayClobber. This will not check the other way around.
179 ///
180 /// This assumes that, for the purposes of MemorySSA, Use comes directly after
181 /// MayClobber, with no potentially clobbering operations in between them.
182 /// (Where potentially clobbering ops are memory barriers, aliased stores, etc.)
184  const LoadInst *MayClobber) {
185  bool VolatileUse = Use->isVolatile();
186  bool VolatileClobber = MayClobber->isVolatile();
187  // Volatile operations may never be reordered with other volatile operations.
188  if (VolatileUse && VolatileClobber)
189  return Reorderability::Never;
190 
191  // The lang ref allows reordering of volatile and non-volatile operations.
192  // Whether an aliasing nonvolatile load and volatile load can be reordered,
193  // though, is ambiguous. Because it may not be best to exploit this ambiguity,
194  // we only allow volatile/non-volatile reordering if the volatile and
195  // non-volatile operations don't alias.
196  Reorderability Result = VolatileUse || VolatileClobber
199 
200  // If a load is seq_cst, it cannot be moved above other loads. If its ordering
201  // is weaker, it can be moved above other loads. We just need to be sure that
202  // MayClobber isn't an acquire load, because loads can't be moved above
203  // acquire loads.
204  //
205  // Note that this explicitly *does* allow the free reordering of monotonic (or
206  // weaker) loads of the same address.
207  bool SeqCstUse = Use->getOrdering() == AtomicOrdering::SequentiallyConsistent;
208  bool MayClobberIsAcquire = isAtLeastOrStrongerThan(MayClobber->getOrdering(),
210  if (SeqCstUse || MayClobberIsAcquire)
211  return Reorderability::Never;
212  return Result;
213 }
214 
216  const MemoryLocation &UseLoc,
217  const Instruction *UseInst,
218  AliasAnalysis &AA) {
219  Instruction *DefInst = MD->getMemoryInst();
220  assert(DefInst && "Defining instruction not actually an instruction");
221 
222  if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(DefInst)) {
223  // These intrinsics will show up as affecting memory, but they are just
224  // markers.
225  switch (II->getIntrinsicID()) {
226  case Intrinsic::lifetime_start:
227  case Intrinsic::lifetime_end:
228  case Intrinsic::invariant_start:
229  case Intrinsic::invariant_end:
230  case Intrinsic::assume:
231  return false;
232  default:
233  break;
234  }
235  }
236 
237  ImmutableCallSite UseCS(UseInst);
238  if (UseCS) {
239  ModRefInfo I = AA.getModRefInfo(DefInst, UseCS);
240  return I != MRI_NoModRef;
241  }
242 
243  if (auto *DefLoad = dyn_cast<LoadInst>(DefInst)) {
244  if (auto *UseLoad = dyn_cast<LoadInst>(UseInst)) {
245  switch (getLoadReorderability(UseLoad, DefLoad)) {
247  return false;
249  return true;
251  return !AA.isNoAlias(UseLoc, MemoryLocation::get(DefLoad));
252  }
253  }
254  }
255 
256  return AA.getModRefInfo(DefInst, UseLoc) & MRI_Mod;
257 }
258 
260  const MemoryLocOrCall &UseMLOC,
261  AliasAnalysis &AA) {
262  // FIXME: This is a temporary hack to allow a single instructionClobbersQuery
263  // to exist while MemoryLocOrCall is pushed through places.
264  if (UseMLOC.IsCall)
266  AA);
267  return instructionClobbersQuery(MD, UseMLOC.getLoc(), MU->getMemoryInst(),
268  AA);
269 }
270 
271 // Return true when MD may alias MU, return false otherwise.
273  AliasAnalysis &AA) {
274  return instructionClobbersQuery(MD, MU, MemoryLocOrCall(MU), AA);
275 }
276 }
277 
278 namespace {
279 struct UpwardsMemoryQuery {
280  // True if our original query started off as a call
281  bool IsCall;
282  // The pointer location we started the query with. This will be empty if
283  // IsCall is true.
284  MemoryLocation StartingLoc;
285  // This is the instruction we were querying about.
286  const Instruction *Inst;
287  // The MemoryAccess we actually got called with, used to test local domination
288  const MemoryAccess *OriginalAccess;
289 
290  UpwardsMemoryQuery()
291  : IsCall(false), Inst(nullptr), OriginalAccess(nullptr) {}
292 
293  UpwardsMemoryQuery(const Instruction *Inst, const MemoryAccess *Access)
294  : IsCall(ImmutableCallSite(Inst)), Inst(Inst), OriginalAccess(Access) {
295  if (!IsCall)
296  StartingLoc = MemoryLocation::get(Inst);
297  }
298 };
299 
300 static bool lifetimeEndsAt(MemoryDef *MD, const MemoryLocation &Loc,
301  AliasAnalysis &AA) {
302  Instruction *Inst = MD->getMemoryInst();
303  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
304  switch (II->getIntrinsicID()) {
305  case Intrinsic::lifetime_start:
306  case Intrinsic::lifetime_end:
307  return AA.isMustAlias(MemoryLocation(II->getArgOperand(1)), Loc);
308  default:
309  return false;
310  }
311  }
312  return false;
313 }
314 
315 static bool isUseTriviallyOptimizableToLiveOnEntry(AliasAnalysis &AA,
316  const Instruction *I) {
317  // If the memory can't be changed, then loads of the memory can't be
318  // clobbered.
319  //
320  // FIXME: We should handle invariant groups, as well. It's a bit harder,
321  // because we need to pay close attention to invariant group barriers.
322  return isa<LoadInst>(I) && (I->getMetadata(LLVMContext::MD_invariant_load) ||
323  AA.pointsToConstantMemory(I));
324 }
325 
326 /// Cache for our caching MemorySSA walker.
327 class WalkerCache {
330 
331 public:
332  MemoryAccess *lookup(const MemoryAccess *MA, const MemoryLocation &Loc,
333  bool IsCall) const {
334  ++NumClobberCacheLookups;
335  MemoryAccess *R = IsCall ? Calls.lookup(MA) : Accesses.lookup({MA, Loc});
336  if (R)
337  ++NumClobberCacheHits;
338  return R;
339  }
340 
341  bool insert(const MemoryAccess *MA, MemoryAccess *To,
342  const MemoryLocation &Loc, bool IsCall) {
343  // This is fine for Phis, since there are times where we can't optimize
344  // them. Making a def its own clobber is never correct, though.
345  assert((MA != To || isa<MemoryPhi>(MA)) &&
346  "Something can't clobber itself!");
347 
348  ++NumClobberCacheInserts;
349  bool Inserted;
350  if (IsCall)
351  Inserted = Calls.insert({MA, To}).second;
352  else
353  Inserted = Accesses.insert({{MA, Loc}, To}).second;
354 
355  return Inserted;
356  }
357 
358  bool remove(const MemoryAccess *MA, const MemoryLocation &Loc, bool IsCall) {
359  return IsCall ? Calls.erase(MA) : Accesses.erase({MA, Loc});
360  }
361 
362  void clear() {
363  Accesses.clear();
364  Calls.clear();
365  }
366 
367  bool contains(const MemoryAccess *MA) const {
368  for (auto &P : Accesses)
369  if (P.first.first == MA || P.second == MA)
370  return true;
371  for (auto &P : Calls)
372  if (P.first == MA || P.second == MA)
373  return true;
374  return false;
375  }
376 };
377 
378 /// Walks the defining uses of MemoryDefs. Stops after we hit something that has
379 /// no defining use (e.g. a MemoryPhi or liveOnEntry). Note that, when comparing
380 /// against a null def_chain_iterator, this will compare equal only after
381 /// walking said Phi/liveOnEntry.
382 struct def_chain_iterator
383  : public iterator_facade_base<def_chain_iterator, std::forward_iterator_tag,
384  MemoryAccess *> {
385  def_chain_iterator() : MA(nullptr) {}
386  def_chain_iterator(MemoryAccess *MA) : MA(MA) {}
387 
388  MemoryAccess *operator*() const { return MA; }
389 
390  def_chain_iterator &operator++() {
391  // N.B. liveOnEntry has a null defining access.
392  if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
393  MA = MUD->getDefiningAccess();
394  else
395  MA = nullptr;
396  return *this;
397  }
398 
399  bool operator==(const def_chain_iterator &O) const { return MA == O.MA; }
400 
401 private:
402  MemoryAccess *MA;
403 };
404 
406 def_chain(MemoryAccess *MA, MemoryAccess *UpTo = nullptr) {
407 #ifdef EXPENSIVE_CHECKS
408  assert((!UpTo || find(def_chain(MA), UpTo) != def_chain_iterator()) &&
409  "UpTo isn't in the def chain!");
410 #endif
411  return make_range(def_chain_iterator(MA), def_chain_iterator(UpTo));
412 }
413 
414 /// Verifies that `Start` is clobbered by `ClobberAt`, and that nothing
415 /// inbetween `Start` and `ClobberAt` can clobbers `Start`.
416 ///
417 /// This is meant to be as simple and self-contained as possible. Because it
418 /// uses no cache, etc., it can be relatively expensive.
419 ///
420 /// \param Start The MemoryAccess that we want to walk from.
421 /// \param ClobberAt A clobber for Start.
422 /// \param StartLoc The MemoryLocation for Start.
423 /// \param MSSA The MemorySSA isntance that Start and ClobberAt belong to.
424 /// \param Query The UpwardsMemoryQuery we used for our search.
425 /// \param AA The AliasAnalysis we used for our search.
426 static void LLVM_ATTRIBUTE_UNUSED
427 checkClobberSanity(MemoryAccess *Start, MemoryAccess *ClobberAt,
428  const MemoryLocation &StartLoc, const MemorySSA &MSSA,
429  const UpwardsMemoryQuery &Query, AliasAnalysis &AA) {
430  assert(MSSA.dominates(ClobberAt, Start) && "Clobber doesn't dominate start?");
431 
432  if (MSSA.isLiveOnEntryDef(Start)) {
433  assert(MSSA.isLiveOnEntryDef(ClobberAt) &&
434  "liveOnEntry must clobber itself");
435  return;
436  }
437 
438  bool FoundClobber = false;
439  DenseSet<MemoryAccessPair> VisitedPhis;
441  Worklist.emplace_back(Start, StartLoc);
442  // Walk all paths from Start to ClobberAt, while looking for clobbers. If one
443  // is found, complain.
444  while (!Worklist.empty()) {
445  MemoryAccessPair MAP = Worklist.pop_back_val();
446  // All we care about is that nothing from Start to ClobberAt clobbers Start.
447  // We learn nothing from revisiting nodes.
448  if (!VisitedPhis.insert(MAP).second)
449  continue;
450 
451  for (MemoryAccess *MA : def_chain(MAP.first)) {
452  if (MA == ClobberAt) {
453  if (auto *MD = dyn_cast<MemoryDef>(MA)) {
454  // instructionClobbersQuery isn't essentially free, so don't use `|=`,
455  // since it won't let us short-circuit.
456  //
457  // Also, note that this can't be hoisted out of the `Worklist` loop,
458  // since MD may only act as a clobber for 1 of N MemoryLocations.
459  FoundClobber =
460  FoundClobber || MSSA.isLiveOnEntryDef(MD) ||
461  instructionClobbersQuery(MD, MAP.second, Query.Inst, AA);
462  }
463  break;
464  }
465 
466  // We should never hit liveOnEntry, unless it's the clobber.
467  assert(!MSSA.isLiveOnEntryDef(MA) && "Hit liveOnEntry before clobber?");
468 
469  if (auto *MD = dyn_cast<MemoryDef>(MA)) {
470  (void)MD;
471  assert(!instructionClobbersQuery(MD, MAP.second, Query.Inst, AA) &&
472  "Found clobber before reaching ClobberAt!");
473  continue;
474  }
475 
476  assert(isa<MemoryPhi>(MA));
477  Worklist.append(upward_defs_begin({MA, MAP.second}), upward_defs_end());
478  }
479  }
480 
481  // If ClobberAt is a MemoryPhi, we can assume something above it acted as a
482  // clobber. Otherwise, `ClobberAt` should've acted as a clobber at some point.
483  assert((isa<MemoryPhi>(ClobberAt) || FoundClobber) &&
484  "ClobberAt never acted as a clobber");
485 }
486 
487 /// Our algorithm for walking (and trying to optimize) clobbers, all wrapped up
488 /// in one class.
489 class ClobberWalker {
490  /// Save a few bytes by using unsigned instead of size_t.
491  using ListIndex = unsigned;
492 
493  /// Represents a span of contiguous MemoryDefs, potentially ending in a
494  /// MemoryPhi.
495  struct DefPath {
496  MemoryLocation Loc;
497  // Note that, because we always walk in reverse, Last will always dominate
498  // First. Also note that First and Last are inclusive.
499  MemoryAccess *First;
501  Optional<ListIndex> Previous;
502 
503  DefPath(const MemoryLocation &Loc, MemoryAccess *First, MemoryAccess *Last,
504  Optional<ListIndex> Previous)
505  : Loc(Loc), First(First), Last(Last), Previous(Previous) {}
506 
507  DefPath(const MemoryLocation &Loc, MemoryAccess *Init,
508  Optional<ListIndex> Previous)
509  : DefPath(Loc, Init, Init, Previous) {}
510  };
511 
512  const MemorySSA &MSSA;
513  AliasAnalysis &AA;
514  DominatorTree &DT;
515  WalkerCache &WC;
516  UpwardsMemoryQuery *Query;
517  bool UseCache;
518 
519  // Phi optimization bookkeeping
523 
524  void setUseCache(bool Use) { UseCache = Use; }
525  bool shouldIgnoreCache() const {
526  // UseCache will only be false when we're debugging, or when expensive
527  // checks are enabled. In either case, we don't care deeply about speed.
528  return LLVM_UNLIKELY(!UseCache);
529  }
530 
531  void addCacheEntry(const MemoryAccess *What, MemoryAccess *To,
532  const MemoryLocation &Loc) const {
533 // EXPENSIVE_CHECKS because most of these queries are redundant.
534 #ifdef EXPENSIVE_CHECKS
535  assert(MSSA.dominates(To, What));
536 #endif
537  if (shouldIgnoreCache())
538  return;
539  WC.insert(What, To, Loc, Query->IsCall);
540  }
541 
542  MemoryAccess *lookupCache(const MemoryAccess *MA, const MemoryLocation &Loc) {
543  return shouldIgnoreCache() ? nullptr : WC.lookup(MA, Loc, Query->IsCall);
544  }
545 
546  void cacheDefPath(const DefPath &DN, MemoryAccess *Target) const {
547  if (shouldIgnoreCache())
548  return;
549 
550  for (MemoryAccess *MA : def_chain(DN.First, DN.Last))
551  addCacheEntry(MA, Target, DN.Loc);
552 
553  // DefPaths only express the path we walked. So, DN.Last could either be a
554  // thing we want to cache, or not.
555  if (DN.Last != Target)
556  addCacheEntry(DN.Last, Target, DN.Loc);
557  }
558 
559  /// Find the nearest def or phi that `From` can legally be optimized to.
560  ///
561  /// FIXME: Deduplicate this with MSSA::findDominatingDef. Ideally, MSSA should
562  /// keep track of this information for us, and allow us O(1) lookups of this
563  /// info.
564  MemoryAccess *getWalkTarget(const MemoryPhi *From) {
565  assert(From->getNumOperands() && "Phi with no operands?");
566 
567  BasicBlock *BB = From->getBlock();
568  auto At = WalkTargetCache.find(BB);
569  if (At != WalkTargetCache.end())
570  return At->second;
571 
573  ToCache.push_back(BB);
574 
575  MemoryAccess *Result = MSSA.getLiveOnEntryDef();
576  DomTreeNode *Node = DT.getNode(BB);
577  while ((Node = Node->getIDom())) {
578  auto At = WalkTargetCache.find(BB);
579  if (At != WalkTargetCache.end()) {
580  Result = At->second;
581  break;
582  }
583 
584  auto *Accesses = MSSA.getBlockAccesses(Node->getBlock());
585  if (Accesses) {
586  auto Iter = find_if(reverse(*Accesses), [](const MemoryAccess &MA) {
587  return !isa<MemoryUse>(MA);
588  });
589  if (Iter != Accesses->rend()) {
590  Result = const_cast<MemoryAccess *>(&*Iter);
591  break;
592  }
593  }
594 
595  ToCache.push_back(Node->getBlock());
596  }
597 
598  for (const BasicBlock *BB : ToCache)
599  WalkTargetCache.insert({BB, Result});
600  return Result;
601  }
602 
603  /// Result of calling walkToPhiOrClobber.
604  struct UpwardsWalkResult {
605  /// The "Result" of the walk. Either a clobber, the last thing we walked, or
606  /// both.
607  MemoryAccess *Result;
608  bool IsKnownClobber;
609  bool FromCache;
610  };
611 
612  /// Walk to the next Phi or Clobber in the def chain starting at Desc.Last.
613  /// This will update Desc.Last as it walks. It will (optionally) also stop at
614  /// StopAt.
615  ///
616  /// This does not test for whether StopAt is a clobber
617  UpwardsWalkResult walkToPhiOrClobber(DefPath &Desc,
618  MemoryAccess *StopAt = nullptr) {
619  assert(!isa<MemoryUse>(Desc.Last) && "Uses don't exist in my world");
620 
621  for (MemoryAccess *Current : def_chain(Desc.Last)) {
622  Desc.Last = Current;
623  if (Current == StopAt)
624  return {Current, false, false};
625 
626  if (auto *MD = dyn_cast<MemoryDef>(Current))
627  if (MSSA.isLiveOnEntryDef(MD) ||
628  instructionClobbersQuery(MD, Desc.Loc, Query->Inst, AA))
629  return {MD, true, false};
630 
631  // Cache checks must be done last, because if Current is a clobber, the
632  // cache will contain the clobber for Current.
633  if (MemoryAccess *MA = lookupCache(Current, Desc.Loc))
634  return {MA, true, true};
635  }
636 
637  assert(isa<MemoryPhi>(Desc.Last) &&
638  "Ended at a non-clobber that's not a phi?");
639  return {Desc.Last, false, false};
640  }
641 
642  void addSearches(MemoryPhi *Phi, SmallVectorImpl<ListIndex> &PausedSearches,
643  ListIndex PriorNode) {
644  auto UpwardDefs = make_range(upward_defs_begin({Phi, Paths[PriorNode].Loc}),
645  upward_defs_end());
646  for (const MemoryAccessPair &P : UpwardDefs) {
647  PausedSearches.push_back(Paths.size());
648  Paths.emplace_back(P.second, P.first, PriorNode);
649  }
650  }
651 
652  /// Represents a search that terminated after finding a clobber. This clobber
653  /// may or may not be present in the path of defs from LastNode..SearchStart,
654  /// since it may have been retrieved from cache.
655  struct TerminatedPath {
656  MemoryAccess *Clobber;
657  ListIndex LastNode;
658  };
659 
660  /// Get an access that keeps us from optimizing to the given phi.
661  ///
662  /// PausedSearches is an array of indices into the Paths array. Its incoming
663  /// value is the indices of searches that stopped at the last phi optimization
664  /// target. It's left in an unspecified state.
665  ///
666  /// If this returns None, NewPaused is a vector of searches that terminated
667  /// at StopWhere. Otherwise, NewPaused is left in an unspecified state.
669  getBlockingAccess(MemoryAccess *StopWhere,
670  SmallVectorImpl<ListIndex> &PausedSearches,
671  SmallVectorImpl<ListIndex> &NewPaused,
672  SmallVectorImpl<TerminatedPath> &Terminated) {
673  assert(!PausedSearches.empty() && "No searches to continue?");
674 
675  // BFS vs DFS really doesn't make a difference here, so just do a DFS with
676  // PausedSearches as our stack.
677  while (!PausedSearches.empty()) {
678  ListIndex PathIndex = PausedSearches.pop_back_val();
679  DefPath &Node = Paths[PathIndex];
680 
681  // If we've already visited this path with this MemoryLocation, we don't
682  // need to do so again.
683  //
684  // NOTE: That we just drop these paths on the ground makes caching
685  // behavior sporadic. e.g. given a diamond:
686  // A
687  // B C
688  // D
689  //
690  // ...If we walk D, B, A, C, we'll only cache the result of phi
691  // optimization for A, B, and D; C will be skipped because it dies here.
692  // This arguably isn't the worst thing ever, since:
693  // - We generally query things in a top-down order, so if we got below D
694  // without needing cache entries for {C, MemLoc}, then chances are
695  // that those cache entries would end up ultimately unused.
696  // - We still cache things for A, so C only needs to walk up a bit.
697  // If this behavior becomes problematic, we can fix without a ton of extra
698  // work.
699  if (!VisitedPhis.insert({Node.Last, Node.Loc}).second)
700  continue;
701 
702  UpwardsWalkResult Res = walkToPhiOrClobber(Node, /*StopAt=*/StopWhere);
703  if (Res.IsKnownClobber) {
704  assert(Res.Result != StopWhere || Res.FromCache);
705  // If this wasn't a cache hit, we hit a clobber when walking. That's a
706  // failure.
707  TerminatedPath Term{Res.Result, PathIndex};
708  if (!Res.FromCache || !MSSA.dominates(Res.Result, StopWhere))
709  return Term;
710 
711  // Otherwise, it's a valid thing to potentially optimize to.
712  Terminated.push_back(Term);
713  continue;
714  }
715 
716  if (Res.Result == StopWhere) {
717  // We've hit our target. Save this path off for if we want to continue
718  // walking.
719  NewPaused.push_back(PathIndex);
720  continue;
721  }
722 
723  assert(!MSSA.isLiveOnEntryDef(Res.Result) && "liveOnEntry is a clobber");
724  addSearches(cast<MemoryPhi>(Res.Result), PausedSearches, PathIndex);
725  }
726 
727  return None;
728  }
729 
730  template <typename T, typename Walker>
731  struct generic_def_path_iterator
732  : public iterator_facade_base<generic_def_path_iterator<T, Walker>,
733  std::forward_iterator_tag, T *> {
734  generic_def_path_iterator() : W(nullptr), N(None) {}
735  generic_def_path_iterator(Walker *W, ListIndex N) : W(W), N(N) {}
736 
737  T &operator*() const { return curNode(); }
738 
739  generic_def_path_iterator &operator++() {
740  N = curNode().Previous;
741  return *this;
742  }
743 
744  bool operator==(const generic_def_path_iterator &O) const {
745  if (N.hasValue() != O.N.hasValue())
746  return false;
747  return !N.hasValue() || *N == *O.N;
748  }
749 
750  private:
751  T &curNode() const { return W->Paths[*N]; }
752 
753  Walker *W;
755  };
756 
757  using def_path_iterator = generic_def_path_iterator<DefPath, ClobberWalker>;
758  using const_def_path_iterator =
759  generic_def_path_iterator<const DefPath, const ClobberWalker>;
760 
761  iterator_range<def_path_iterator> def_path(ListIndex From) {
762  return make_range(def_path_iterator(this, From), def_path_iterator());
763  }
764 
765  iterator_range<const_def_path_iterator> const_def_path(ListIndex From) const {
766  return make_range(const_def_path_iterator(this, From),
767  const_def_path_iterator());
768  }
769 
770  struct OptznResult {
771  /// The path that contains our result.
772  TerminatedPath PrimaryClobber;
773  /// The paths that we can legally cache back from, but that aren't
774  /// necessarily the result of the Phi optimization.
775  SmallVector<TerminatedPath, 4> OtherClobbers;
776  };
777 
778  ListIndex defPathIndex(const DefPath &N) const {
779  // The assert looks nicer if we don't need to do &N
780  const DefPath *NP = &N;
781  assert(!Paths.empty() && NP >= &Paths.front() && NP <= &Paths.back() &&
782  "Out of bounds DefPath!");
783  return NP - &Paths.front();
784  }
785 
786  /// Try to optimize a phi as best as we can. Returns a SmallVector of Paths
787  /// that act as legal clobbers. Note that this won't return *all* clobbers.
788  ///
789  /// Phi optimization algorithm tl;dr:
790  /// - Find the earliest def/phi, A, we can optimize to
791  /// - Find if all paths from the starting memory access ultimately reach A
792  /// - If not, optimization isn't possible.
793  /// - Otherwise, walk from A to another clobber or phi, A'.
794  /// - If A' is a def, we're done.
795  /// - If A' is a phi, try to optimize it.
796  ///
797  /// A path is a series of {MemoryAccess, MemoryLocation} pairs. A path
798  /// terminates when a MemoryAccess that clobbers said MemoryLocation is found.
799  OptznResult tryOptimizePhi(MemoryPhi *Phi, MemoryAccess *Start,
800  const MemoryLocation &Loc) {
801  assert(Paths.empty() && VisitedPhis.empty() &&
802  "Reset the optimization state.");
803 
804  Paths.emplace_back(Loc, Start, Phi, None);
805  // Stores how many "valid" optimization nodes we had prior to calling
806  // addSearches/getBlockingAccess. Necessary for caching if we had a blocker.
807  auto PriorPathsSize = Paths.size();
808 
809  SmallVector<ListIndex, 16> PausedSearches;
810  SmallVector<ListIndex, 8> NewPaused;
811  SmallVector<TerminatedPath, 4> TerminatedPaths;
812 
813  addSearches(Phi, PausedSearches, 0);
814 
815  // Moves the TerminatedPath with the "most dominated" Clobber to the end of
816  // Paths.
817  auto MoveDominatedPathToEnd = [&](SmallVectorImpl<TerminatedPath> &Paths) {
818  assert(!Paths.empty() && "Need a path to move");
819  auto Dom = Paths.begin();
820  for (auto I = std::next(Dom), E = Paths.end(); I != E; ++I)
821  if (!MSSA.dominates(I->Clobber, Dom->Clobber))
822  Dom = I;
823  auto Last = Paths.end() - 1;
824  if (Last != Dom)
825  std::iter_swap(Last, Dom);
826  };
827 
828  MemoryPhi *Current = Phi;
829  while (1) {
830  assert(!MSSA.isLiveOnEntryDef(Current) &&
831  "liveOnEntry wasn't treated as a clobber?");
832 
833  MemoryAccess *Target = getWalkTarget(Current);
834  // If a TerminatedPath doesn't dominate Target, then it wasn't a legal
835  // optimization for the prior phi.
836  assert(all_of(TerminatedPaths, [&](const TerminatedPath &P) {
837  return MSSA.dominates(P.Clobber, Target);
838  }));
839 
840  // FIXME: This is broken, because the Blocker may be reported to be
841  // liveOnEntry, and we'll happily wait for that to disappear (read: never)
842  // For the moment, this is fine, since we do nothing with blocker info.
843  if (Optional<TerminatedPath> Blocker = getBlockingAccess(
844  Target, PausedSearches, NewPaused, TerminatedPaths)) {
845  // Cache our work on the blocking node, since we know that's correct.
846  cacheDefPath(Paths[Blocker->LastNode], Blocker->Clobber);
847 
848  // Find the node we started at. We can't search based on N->Last, since
849  // we may have gone around a loop with a different MemoryLocation.
850  auto Iter = find_if(def_path(Blocker->LastNode), [&](const DefPath &N) {
851  return defPathIndex(N) < PriorPathsSize;
852  });
853  assert(Iter != def_path_iterator());
854 
855  DefPath &CurNode = *Iter;
856  assert(CurNode.Last == Current);
857 
858  // Two things:
859  // A. We can't reliably cache all of NewPaused back. Consider a case
860  // where we have two paths in NewPaused; one of which can't optimize
861  // above this phi, whereas the other can. If we cache the second path
862  // back, we'll end up with suboptimal cache entries. We can handle
863  // cases like this a bit better when we either try to find all
864  // clobbers that block phi optimization, or when our cache starts
865  // supporting unfinished searches.
866  // B. We can't reliably cache TerminatedPaths back here without doing
867  // extra checks; consider a case like:
868  // T
869  // / \
870  // D C
871  // \ /
872  // S
873  // Where T is our target, C is a node with a clobber on it, D is a
874  // diamond (with a clobber *only* on the left or right node, N), and
875  // S is our start. Say we walk to D, through the node opposite N
876  // (read: ignoring the clobber), and see a cache entry in the top
877  // node of D. That cache entry gets put into TerminatedPaths. We then
878  // walk up to C (N is later in our worklist), find the clobber, and
879  // quit. If we append TerminatedPaths to OtherClobbers, we'll cache
880  // the bottom part of D to the cached clobber, ignoring the clobber
881  // in N. Again, this problem goes away if we start tracking all
882  // blockers for a given phi optimization.
883  TerminatedPath Result{CurNode.Last, defPathIndex(CurNode)};
884  return {Result, {}};
885  }
886 
887  // If there's nothing left to search, then all paths led to valid clobbers
888  // that we got from our cache; pick the nearest to the start, and allow
889  // the rest to be cached back.
890  if (NewPaused.empty()) {
891  MoveDominatedPathToEnd(TerminatedPaths);
892  TerminatedPath Result = TerminatedPaths.pop_back_val();
893  return {Result, std::move(TerminatedPaths)};
894  }
895 
896  MemoryAccess *DefChainEnd = nullptr;
898  for (ListIndex Paused : NewPaused) {
899  UpwardsWalkResult WR = walkToPhiOrClobber(Paths[Paused]);
900  if (WR.IsKnownClobber)
901  Clobbers.push_back({WR.Result, Paused});
902  else
903  // Micro-opt: If we hit the end of the chain, save it.
904  DefChainEnd = WR.Result;
905  }
906 
907  if (!TerminatedPaths.empty()) {
908  // If we couldn't find the dominating phi/liveOnEntry in the above loop,
909  // do it now.
910  if (!DefChainEnd)
911  for (MemoryAccess *MA : def_chain(Target))
912  DefChainEnd = MA;
913 
914  // If any of the terminated paths don't dominate the phi we'll try to
915  // optimize, we need to figure out what they are and quit.
916  const BasicBlock *ChainBB = DefChainEnd->getBlock();
917  for (const TerminatedPath &TP : TerminatedPaths) {
918  // Because we know that DefChainEnd is as "high" as we can go, we
919  // don't need local dominance checks; BB dominance is sufficient.
920  if (DT.dominates(ChainBB, TP.Clobber->getBlock()))
921  Clobbers.push_back(TP);
922  }
923  }
924 
925  // If we have clobbers in the def chain, find the one closest to Current
926  // and quit.
927  if (!Clobbers.empty()) {
928  MoveDominatedPathToEnd(Clobbers);
929  TerminatedPath Result = Clobbers.pop_back_val();
930  return {Result, std::move(Clobbers)};
931  }
932 
933  assert(all_of(NewPaused,
934  [&](ListIndex I) { return Paths[I].Last == DefChainEnd; }));
935 
936  // Because liveOnEntry is a clobber, this must be a phi.
937  auto *DefChainPhi = cast<MemoryPhi>(DefChainEnd);
938 
939  PriorPathsSize = Paths.size();
940  PausedSearches.clear();
941  for (ListIndex I : NewPaused)
942  addSearches(DefChainPhi, PausedSearches, I);
943  NewPaused.clear();
944 
945  Current = DefChainPhi;
946  }
947  }
948 
949  /// Caches everything in an OptznResult.
950  void cacheOptResult(const OptznResult &R) {
951  if (R.OtherClobbers.empty()) {
952  // If we're not going to be caching OtherClobbers, don't bother with
953  // marking visited/etc.
954  for (const DefPath &N : const_def_path(R.PrimaryClobber.LastNode))
955  cacheDefPath(N, R.PrimaryClobber.Clobber);
956  return;
957  }
958 
959  // PrimaryClobber is our answer. If we can cache anything back, we need to
960  // stop caching when we visit PrimaryClobber.
961  SmallBitVector Visited(Paths.size());
962  for (const DefPath &N : const_def_path(R.PrimaryClobber.LastNode)) {
963  Visited[defPathIndex(N)] = true;
964  cacheDefPath(N, R.PrimaryClobber.Clobber);
965  }
966 
967  for (const TerminatedPath &P : R.OtherClobbers) {
968  for (const DefPath &N : const_def_path(P.LastNode)) {
969  ListIndex NIndex = defPathIndex(N);
970  if (Visited[NIndex])
971  break;
972  Visited[NIndex] = true;
973  cacheDefPath(N, P.Clobber);
974  }
975  }
976  }
977 
978  void verifyOptResult(const OptznResult &R) const {
979  assert(all_of(R.OtherClobbers, [&](const TerminatedPath &P) {
980  return MSSA.dominates(P.Clobber, R.PrimaryClobber.Clobber);
981  }));
982  }
983 
984  void resetPhiOptznState() {
985  Paths.clear();
986  VisitedPhis.clear();
987  }
988 
989 public:
990  ClobberWalker(const MemorySSA &MSSA, AliasAnalysis &AA, DominatorTree &DT,
991  WalkerCache &WC)
992  : MSSA(MSSA), AA(AA), DT(DT), WC(WC), UseCache(true) {}
993 
994  void reset() { WalkTargetCache.clear(); }
995 
996  /// Finds the nearest clobber for the given query, optimizing phis if
997  /// possible.
998  MemoryAccess *findClobber(MemoryAccess *Start, UpwardsMemoryQuery &Q,
999  bool UseWalkerCache = true) {
1000  setUseCache(UseWalkerCache);
1001  Query = &Q;
1002 
1003  MemoryAccess *Current = Start;
1004  // This walker pretends uses don't exist. If we're handed one, silently grab
1005  // its def. (This has the nice side-effect of ensuring we never cache uses)
1006  if (auto *MU = dyn_cast<MemoryUse>(Start))
1007  Current = MU->getDefiningAccess();
1008 
1009  DefPath FirstDesc(Q.StartingLoc, Current, Current, None);
1010  // Fast path for the overly-common case (no crazy phi optimization
1011  // necessary)
1012  UpwardsWalkResult WalkResult = walkToPhiOrClobber(FirstDesc);
1013  MemoryAccess *Result;
1014  if (WalkResult.IsKnownClobber) {
1015  cacheDefPath(FirstDesc, WalkResult.Result);
1016  Result = WalkResult.Result;
1017  } else {
1018  OptznResult OptRes = tryOptimizePhi(cast<MemoryPhi>(FirstDesc.Last),
1019  Current, Q.StartingLoc);
1020  verifyOptResult(OptRes);
1021  cacheOptResult(OptRes);
1022  resetPhiOptznState();
1023  Result = OptRes.PrimaryClobber.Clobber;
1024  }
1025 
1026 #ifdef EXPENSIVE_CHECKS
1027  checkClobberSanity(Current, Result, Q.StartingLoc, MSSA, Q, AA);
1028 #endif
1029  return Result;
1030  }
1031 
1032  void verify(const MemorySSA *MSSA) { assert(MSSA == &this->MSSA); }
1033 };
1034 
1035 struct RenamePassData {
1036  DomTreeNode *DTN;
1038  MemoryAccess *IncomingVal;
1039 
1040  RenamePassData(DomTreeNode *D, DomTreeNode::const_iterator It,
1041  MemoryAccess *M)
1042  : DTN(D), ChildIt(It), IncomingVal(M) {}
1043  void swap(RenamePassData &RHS) {
1044  std::swap(DTN, RHS.DTN);
1045  std::swap(ChildIt, RHS.ChildIt);
1046  std::swap(IncomingVal, RHS.IncomingVal);
1047  }
1048 };
1049 } // anonymous namespace
1050 
1051 namespace llvm {
1052 /// \brief A MemorySSAWalker that does AA walks and caching of lookups to
1053 /// disambiguate accesses.
1054 ///
1055 /// FIXME: The current implementation of this can take quadratic space in rare
1056 /// cases. This can be fixed, but it is something to note until it is fixed.
1057 ///
1058 /// In order to trigger this behavior, you need to store to N distinct locations
1059 /// (that AA can prove don't alias), perform M stores to other memory
1060 /// locations that AA can prove don't alias any of the initial N locations, and
1061 /// then load from all of the N locations. In this case, we insert M cache
1062 /// entries for each of the N loads.
1063 ///
1064 /// For example:
1065 /// define i32 @foo() {
1066 /// %a = alloca i32, align 4
1067 /// %b = alloca i32, align 4
1068 /// store i32 0, i32* %a, align 4
1069 /// store i32 0, i32* %b, align 4
1070 ///
1071 /// ; Insert M stores to other memory that doesn't alias %a or %b here
1072 ///
1073 /// %c = load i32, i32* %a, align 4 ; Caches M entries in
1074 /// ; CachedUpwardsClobberingAccess for the
1075 /// ; MemoryLocation %a
1076 /// %d = load i32, i32* %b, align 4 ; Caches M entries in
1077 /// ; CachedUpwardsClobberingAccess for the
1078 /// ; MemoryLocation %b
1079 ///
1080 /// ; For completeness' sake, loading %a or %b again would not cache *another*
1081 /// ; M entries.
1082 /// %r = add i32 %c, %d
1083 /// ret i32 %r
1084 /// }
1086  WalkerCache Cache;
1087  ClobberWalker Walker;
1088  bool AutoResetWalker;
1089 
1090  MemoryAccess *getClobberingMemoryAccess(MemoryAccess *, UpwardsMemoryQuery &);
1091  void verifyRemoved(MemoryAccess *);
1092 
1093 public:
1095  ~CachingWalker() override;
1096 
1098  MemoryAccess *getClobberingMemoryAccess(MemoryAccess *) override;
1099  MemoryAccess *getClobberingMemoryAccess(MemoryAccess *,
1100  const MemoryLocation &) override;
1101  void invalidateInfo(MemoryAccess *) override;
1102 
1103  /// Whether we call resetClobberWalker() after each time we *actually* walk to
1104  /// answer a clobber query.
1105  void setAutoResetWalker(bool AutoReset) { AutoResetWalker = AutoReset; }
1106 
1107  /// Drop the walker's persistent data structures. At the moment, this means
1108  /// "drop the walker's cache of BasicBlocks ->
1109  /// earliest-MemoryAccess-we-can-optimize-to". This is necessary if we're
1110  /// going to have DT updates, if we remove MemoryAccesses, etc.
1111  void resetClobberWalker() { Walker.reset(); }
1112 
1113  void verify(const MemorySSA *MSSA) override {
1115  Walker.verify(MSSA);
1116  }
1117 };
1118 
1119 /// \brief Rename a single basic block into MemorySSA form.
1120 /// Uses the standard SSA renaming algorithm.
1121 /// \returns The new incoming value.
1122 MemoryAccess *MemorySSA::renameBlock(BasicBlock *BB,
1123  MemoryAccess *IncomingVal) {
1124  auto It = PerBlockAccesses.find(BB);
1125  // Skip most processing if the list is empty.
1126  if (It != PerBlockAccesses.end()) {
1127  AccessList *Accesses = It->second.get();
1128  for (MemoryAccess &L : *Accesses) {
1129  if (MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(&L)) {
1130  if (MUD->getDefiningAccess() == nullptr)
1131  MUD->setDefiningAccess(IncomingVal);
1132  if (isa<MemoryDef>(&L))
1133  IncomingVal = &L;
1134  } else {
1135  IncomingVal = &L;
1136  }
1137  }
1138  }
1139 
1140  // Pass through values to our successors
1141  for (const BasicBlock *S : successors(BB)) {
1142  auto It = PerBlockAccesses.find(S);
1143  // Rename the phi nodes in our successor block
1144  if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front()))
1145  continue;
1146  AccessList *Accesses = It->second.get();
1147  auto *Phi = cast<MemoryPhi>(&Accesses->front());
1148  Phi->addIncoming(IncomingVal, BB);
1149  }
1150 
1151  return IncomingVal;
1152 }
1153 
1154 /// \brief This is the standard SSA renaming algorithm.
1155 ///
1156 /// We walk the dominator tree in preorder, renaming accesses, and then filling
1157 /// in phi nodes in our successors.
1158 void MemorySSA::renamePass(DomTreeNode *Root, MemoryAccess *IncomingVal,
1159  SmallPtrSet<BasicBlock *, 16> &Visited) {
1161  IncomingVal = renameBlock(Root->getBlock(), IncomingVal);
1162  WorkStack.push_back({Root, Root->begin(), IncomingVal});
1163  Visited.insert(Root->getBlock());
1164 
1165  while (!WorkStack.empty()) {
1166  DomTreeNode *Node = WorkStack.back().DTN;
1167  DomTreeNode::const_iterator ChildIt = WorkStack.back().ChildIt;
1168  IncomingVal = WorkStack.back().IncomingVal;
1169 
1170  if (ChildIt == Node->end()) {
1171  WorkStack.pop_back();
1172  } else {
1173  DomTreeNode *Child = *ChildIt;
1174  ++WorkStack.back().ChildIt;
1175  BasicBlock *BB = Child->getBlock();
1176  Visited.insert(BB);
1177  IncomingVal = renameBlock(BB, IncomingVal);
1178  WorkStack.push_back({Child, Child->begin(), IncomingVal});
1179  }
1180  }
1181 }
1182 
1183 /// \brief Compute dominator levels, used by the phi insertion algorithm above.
1184 void MemorySSA::computeDomLevels(DenseMap<DomTreeNode *, unsigned> &DomLevels) {
1185  for (auto DFI = df_begin(DT->getRootNode()), DFE = df_end(DT->getRootNode());
1186  DFI != DFE; ++DFI)
1187  DomLevels[*DFI] = DFI.getPathLength() - 1;
1188 }
1189 
1190 /// \brief This handles unreachable block accesses by deleting phi nodes in
1191 /// unreachable blocks, and marking all other unreachable MemoryAccess's as
1192 /// being uses of the live on entry definition.
1193 void MemorySSA::markUnreachableAsLiveOnEntry(BasicBlock *BB) {
1194  assert(!DT->isReachableFromEntry(BB) &&
1195  "Reachable block found while handling unreachable blocks");
1196 
1197  // Make sure phi nodes in our reachable successors end up with a
1198  // LiveOnEntryDef for our incoming edge, even though our block is forward
1199  // unreachable. We could just disconnect these blocks from the CFG fully,
1200  // but we do not right now.
1201  for (const BasicBlock *S : successors(BB)) {
1202  if (!DT->isReachableFromEntry(S))
1203  continue;
1204  auto It = PerBlockAccesses.find(S);
1205  // Rename the phi nodes in our successor block
1206  if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front()))
1207  continue;
1208  AccessList *Accesses = It->second.get();
1209  auto *Phi = cast<MemoryPhi>(&Accesses->front());
1210  Phi->addIncoming(LiveOnEntryDef.get(), BB);
1211  }
1212 
1213  auto It = PerBlockAccesses.find(BB);
1214  if (It == PerBlockAccesses.end())
1215  return;
1216 
1217  auto &Accesses = It->second;
1218  for (auto AI = Accesses->begin(), AE = Accesses->end(); AI != AE;) {
1219  auto Next = std::next(AI);
1220  // If we have a phi, just remove it. We are going to replace all
1221  // users with live on entry.
1222  if (auto *UseOrDef = dyn_cast<MemoryUseOrDef>(AI))
1223  UseOrDef->setDefiningAccess(LiveOnEntryDef.get());
1224  else
1225  Accesses->erase(AI);
1226  AI = Next;
1227  }
1228 }
1229 
1231  : AA(AA), DT(DT), F(Func), LiveOnEntryDef(nullptr), Walker(nullptr),
1232  NextID(INVALID_MEMORYACCESS_ID) {
1233  buildMemorySSA();
1234 }
1235 
1237  // Drop all our references
1238  for (const auto &Pair : PerBlockAccesses)
1239  for (MemoryAccess &MA : *Pair.second)
1240  MA.dropAllReferences();
1241 }
1242 
1243 MemorySSA::AccessList *MemorySSA::getOrCreateAccessList(const BasicBlock *BB) {
1244  auto Res = PerBlockAccesses.insert(std::make_pair(BB, nullptr));
1245 
1246  if (Res.second)
1247  Res.first->second = make_unique<AccessList>();
1248  return Res.first->second.get();
1249 }
1250 
1251 /// This class is a batch walker of all MemoryUse's in the program, and points
1252 /// their defining access at the thing that actually clobbers them. Because it
1253 /// is a batch walker that touches everything, it does not operate like the
1254 /// other walkers. This walker is basically performing a top-down SSA renaming
1255 /// pass, where the version stack is used as the cache. This enables it to be
1256 /// significantly more time and memory efficient than using the regular walker,
1257 /// which is walking bottom-up.
1259 public:
1261  DominatorTree *DT)
1262  : MSSA(MSSA), Walker(Walker), AA(AA), DT(DT) {
1263  Walker = MSSA->getWalker();
1264  }
1265 
1266  void optimizeUses();
1267 
1268 private:
1269  /// This represents where a given memorylocation is in the stack.
1270  struct MemlocStackInfo {
1271  // This essentially is keeping track of versions of the stack. Whenever
1272  // the stack changes due to pushes or pops, these versions increase.
1273  unsigned long StackEpoch;
1274  unsigned long PopEpoch;
1275  // This is the lower bound of places on the stack to check. It is equal to
1276  // the place the last stack walk ended.
1277  // Note: Correctness depends on this being initialized to 0, which densemap
1278  // does
1279  unsigned long LowerBound;
1280  const BasicBlock *LowerBoundBlock;
1281  // This is where the last walk for this memory location ended.
1282  unsigned long LastKill;
1283  bool LastKillValid;
1284  };
1285  void optimizeUsesInBlock(const BasicBlock *, unsigned long &, unsigned long &,
1288  MemorySSA *MSSA;
1289  MemorySSAWalker *Walker;
1290  AliasAnalysis *AA;
1291  DominatorTree *DT;
1292 };
1293 
1294 /// Optimize the uses in a given block This is basically the SSA renaming
1295 /// algorithm, with one caveat: We are able to use a single stack for all
1296 /// MemoryUses. This is because the set of *possible* reaching MemoryDefs is
1297 /// the same for every MemoryUse. The *actual* clobbering MemoryDef is just
1298 /// going to be some position in that stack of possible ones.
1299 ///
1300 /// We track the stack positions that each MemoryLocation needs
1301 /// to check, and last ended at. This is because we only want to check the
1302 /// things that changed since last time. The same MemoryLocation should
1303 /// get clobbered by the same store (getModRefInfo does not use invariantness or
1304 /// things like this, and if they start, we can modify MemoryLocOrCall to
1305 /// include relevant data)
1306 void MemorySSA::OptimizeUses::optimizeUsesInBlock(
1307  const BasicBlock *BB, unsigned long &StackEpoch, unsigned long &PopEpoch,
1308  SmallVectorImpl<MemoryAccess *> &VersionStack,
1310 
1311  /// If no accesses, nothing to do.
1312  MemorySSA::AccessList *Accesses = MSSA->getWritableBlockAccesses(BB);
1313  if (Accesses == nullptr)
1314  return;
1315 
1316  // Pop everything that doesn't dominate the current block off the stack,
1317  // increment the PopEpoch to account for this.
1318  while (!VersionStack.empty()) {
1319  BasicBlock *BackBlock = VersionStack.back()->getBlock();
1320  if (DT->dominates(BackBlock, BB))
1321  break;
1322  while (VersionStack.back()->getBlock() == BackBlock)
1323  VersionStack.pop_back();
1324  ++PopEpoch;
1325  }
1326  for (MemoryAccess &MA : *Accesses) {
1327  auto *MU = dyn_cast<MemoryUse>(&MA);
1328  if (!MU) {
1329  VersionStack.push_back(&MA);
1330  ++StackEpoch;
1331  continue;
1332  }
1333 
1334  if (isUseTriviallyOptimizableToLiveOnEntry(*AA, MU->getMemoryInst())) {
1335  MU->setDefiningAccess(MSSA->getLiveOnEntryDef(), true);
1336  continue;
1337  }
1338 
1339  MemoryLocOrCall UseMLOC(MU);
1340  auto &LocInfo = LocStackInfo[UseMLOC];
1341  // If the pop epoch changed, it means we've removed stuff from top of
1342  // stack due to changing blocks. We may have to reset the lower bound or
1343  // last kill info.
1344  if (LocInfo.PopEpoch != PopEpoch) {
1345  LocInfo.PopEpoch = PopEpoch;
1346  LocInfo.StackEpoch = StackEpoch;
1347  // If the lower bound was in something that no longer dominates us, we
1348  // have to reset it.
1349  // We can't simply track stack size, because the stack may have had
1350  // pushes/pops in the meantime.
1351  // XXX: This is non-optimal, but only is slower cases with heavily
1352  // branching dominator trees. To get the optimal number of queries would
1353  // be to make lowerbound and lastkill a per-loc stack, and pop it until
1354  // the top of that stack dominates us. This does not seem worth it ATM.
1355  // A much cheaper optimization would be to always explore the deepest
1356  // branch of the dominator tree first. This will guarantee this resets on
1357  // the smallest set of blocks.
1358  if (LocInfo.LowerBoundBlock && LocInfo.LowerBoundBlock != BB &&
1359  !DT->dominates(LocInfo.LowerBoundBlock, BB)) {
1360  // Reset the lower bound of things to check.
1361  // TODO: Some day we should be able to reset to last kill, rather than
1362  // 0.
1363  LocInfo.LowerBound = 0;
1364  LocInfo.LowerBoundBlock = VersionStack[0]->getBlock();
1365  LocInfo.LastKillValid = false;
1366  }
1367  } else if (LocInfo.StackEpoch != StackEpoch) {
1368  // If all that has changed is the StackEpoch, we only have to check the
1369  // new things on the stack, because we've checked everything before. In
1370  // this case, the lower bound of things to check remains the same.
1371  LocInfo.PopEpoch = PopEpoch;
1372  LocInfo.StackEpoch = StackEpoch;
1373  }
1374  if (!LocInfo.LastKillValid) {
1375  LocInfo.LastKill = VersionStack.size() - 1;
1376  LocInfo.LastKillValid = true;
1377  }
1378 
1379  // At this point, we should have corrected last kill and LowerBound to be
1380  // in bounds.
1381  assert(LocInfo.LowerBound < VersionStack.size() &&
1382  "Lower bound out of range");
1383  assert(LocInfo.LastKill < VersionStack.size() &&
1384  "Last kill info out of range");
1385  // In any case, the new upper bound is the top of the stack.
1386  unsigned long UpperBound = VersionStack.size() - 1;
1387 
1388  if (UpperBound - LocInfo.LowerBound > MaxCheckLimit) {
1389  DEBUG(dbgs() << "MemorySSA skipping optimization of " << *MU << " ("
1390  << *(MU->getMemoryInst()) << ")"
1391  << " because there are " << UpperBound - LocInfo.LowerBound
1392  << " stores to disambiguate\n");
1393  // Because we did not walk, LastKill is no longer valid, as this may
1394  // have been a kill.
1395  LocInfo.LastKillValid = false;
1396  continue;
1397  }
1398  bool FoundClobberResult = false;
1399  while (UpperBound > LocInfo.LowerBound) {
1400  if (isa<MemoryPhi>(VersionStack[UpperBound])) {
1401  // For phis, use the walker, see where we ended up, go there
1402  Instruction *UseInst = MU->getMemoryInst();
1403  MemoryAccess *Result = Walker->getClobberingMemoryAccess(UseInst);
1404  // We are guaranteed to find it or something is wrong
1405  while (VersionStack[UpperBound] != Result) {
1406  assert(UpperBound != 0);
1407  --UpperBound;
1408  }
1409  FoundClobberResult = true;
1410  break;
1411  }
1412 
1413  MemoryDef *MD = cast<MemoryDef>(VersionStack[UpperBound]);
1414  // If the lifetime of the pointer ends at this instruction, it's live on
1415  // entry.
1416  if (!UseMLOC.IsCall && lifetimeEndsAt(MD, UseMLOC.getLoc(), *AA)) {
1417  // Reset UpperBound to liveOnEntryDef's place in the stack
1418  UpperBound = 0;
1419  FoundClobberResult = true;
1420  break;
1421  }
1422  if (instructionClobbersQuery(MD, MU, UseMLOC, *AA)) {
1423  FoundClobberResult = true;
1424  break;
1425  }
1426  --UpperBound;
1427  }
1428  // At the end of this loop, UpperBound is either a clobber, or lower bound
1429  // PHI walking may cause it to be < LowerBound, and in fact, < LastKill.
1430  if (FoundClobberResult || UpperBound < LocInfo.LastKill) {
1431  MU->setDefiningAccess(VersionStack[UpperBound], true);
1432  // We were last killed now by where we got to
1433  LocInfo.LastKill = UpperBound;
1434  } else {
1435  // Otherwise, we checked all the new ones, and now we know we can get to
1436  // LastKill.
1437  MU->setDefiningAccess(VersionStack[LocInfo.LastKill], true);
1438  }
1439  LocInfo.LowerBound = VersionStack.size() - 1;
1440  LocInfo.LowerBoundBlock = BB;
1441  }
1442 }
1443 
1444 /// Optimize uses to point to their actual clobbering definitions.
1446 
1447  // We perform a non-recursive top-down dominator tree walk
1448  struct StackInfo {
1449  const DomTreeNode *Node;
1451  };
1452 
1453  SmallVector<MemoryAccess *, 16> VersionStack;
1454  SmallVector<StackInfo, 16> DomTreeWorklist;
1456  VersionStack.push_back(MSSA->getLiveOnEntryDef());
1457 
1458  unsigned long StackEpoch = 1;
1459  unsigned long PopEpoch = 1;
1460  for (const auto *DomNode : depth_first(DT->getRootNode()))
1461  optimizeUsesInBlock(DomNode->getBlock(), StackEpoch, PopEpoch, VersionStack,
1462  LocStackInfo);
1463 }
1464 
1465 void MemorySSA::placePHINodes(
1466  const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks,
1467  const DenseMap<const BasicBlock *, unsigned int> &BBNumbers) {
1468  // Determine where our MemoryPhi's should go
1469  ForwardIDFCalculator IDFs(*DT);
1470  IDFs.setDefiningBlocks(DefiningBlocks);
1472  IDFs.calculate(IDFBlocks);
1473 
1474  std::sort(IDFBlocks.begin(), IDFBlocks.end(),
1475  [&BBNumbers](const BasicBlock *A, const BasicBlock *B) {
1476  return BBNumbers.lookup(A) < BBNumbers.lookup(B);
1477  });
1478 
1479  // Now place MemoryPhi nodes.
1480  for (auto &BB : IDFBlocks) {
1481  // Insert phi node
1482  AccessList *Accesses = getOrCreateAccessList(BB);
1483  MemoryPhi *Phi = new MemoryPhi(BB->getContext(), BB, NextID++);
1484  ValueToMemoryAccess[BB] = Phi;
1485  // Phi's always are placed at the front of the block.
1486  Accesses->push_front(Phi);
1487  }
1488 }
1489 
1490 void MemorySSA::buildMemorySSA() {
1491  // We create an access to represent "live on entry", for things like
1492  // arguments or users of globals, where the memory they use is defined before
1493  // the beginning of the function. We do not actually insert it into the IR.
1494  // We do not define a live on exit for the immediate uses, and thus our
1495  // semantics do *not* imply that something with no immediate uses can simply
1496  // be removed.
1497  BasicBlock &StartingPoint = F.getEntryBlock();
1498  LiveOnEntryDef = make_unique<MemoryDef>(F.getContext(), nullptr, nullptr,
1499  &StartingPoint, NextID++);
1501  unsigned NextBBNum = 0;
1502 
1503  // We maintain lists of memory accesses per-block, trading memory for time. We
1504  // could just look up the memory access for every possible instruction in the
1505  // stream.
1506  SmallPtrSet<BasicBlock *, 32> DefiningBlocks;
1507  SmallPtrSet<BasicBlock *, 32> DefUseBlocks;
1508  // Go through each block, figure out where defs occur, and chain together all
1509  // the accesses.
1510  for (BasicBlock &B : F) {
1511  BBNumbers[&B] = NextBBNum++;
1512  bool InsertIntoDef = false;
1513  AccessList *Accesses = nullptr;
1514  for (Instruction &I : B) {
1515  MemoryUseOrDef *MUD = createNewAccess(&I);
1516  if (!MUD)
1517  continue;
1518  InsertIntoDef |= isa<MemoryDef>(MUD);
1519 
1520  if (!Accesses)
1521  Accesses = getOrCreateAccessList(&B);
1522  Accesses->push_back(MUD);
1523  }
1524  if (InsertIntoDef)
1525  DefiningBlocks.insert(&B);
1526  if (Accesses)
1527  DefUseBlocks.insert(&B);
1528  }
1529  placePHINodes(DefiningBlocks, BBNumbers);
1530 
1531  // Now do regular SSA renaming on the MemoryDef/MemoryUse. Visited will get
1532  // filled in with all blocks.
1534  renamePass(DT->getRootNode(), LiveOnEntryDef.get(), Visited);
1535 
1536  CachingWalker *Walker = getWalkerImpl();
1537 
1538  // We're doing a batch of updates; don't drop useful caches between them.
1539  Walker->setAutoResetWalker(false);
1540  OptimizeUses(this, Walker, AA, DT).optimizeUses();
1541  Walker->setAutoResetWalker(true);
1542  Walker->resetClobberWalker();
1543 
1544  // Mark the uses in unreachable blocks as live on entry, so that they go
1545  // somewhere.
1546  for (auto &BB : F)
1547  if (!Visited.count(&BB))
1548  markUnreachableAsLiveOnEntry(&BB);
1549 }
1550 
1551 MemorySSAWalker *MemorySSA::getWalker() { return getWalkerImpl(); }
1552 
1553 MemorySSA::CachingWalker *MemorySSA::getWalkerImpl() {
1554  if (Walker)
1555  return Walker.get();
1556 
1557  Walker = make_unique<CachingWalker>(this, AA, DT);
1558  return Walker.get();
1559 }
1560 
1562  assert(!getMemoryAccess(BB) && "MemoryPhi already exists for this BB");
1563  AccessList *Accesses = getOrCreateAccessList(BB);
1564  MemoryPhi *Phi = new MemoryPhi(BB->getContext(), BB, NextID++);
1565  ValueToMemoryAccess[BB] = Phi;
1566  // Phi's always are placed at the front of the block.
1567  Accesses->push_front(Phi);
1568  BlockNumberingValid.erase(BB);
1569  return Phi;
1570 }
1571 
1572 MemoryUseOrDef *MemorySSA::createDefinedAccess(Instruction *I,
1573  MemoryAccess *Definition) {
1574  assert(!isa<PHINode>(I) && "Cannot create a defined access for a PHI");
1575  MemoryUseOrDef *NewAccess = createNewAccess(I);
1576  assert(
1577  NewAccess != nullptr &&
1578  "Tried to create a memory access for a non-memory touching instruction");
1579  NewAccess->setDefiningAccess(Definition);
1580  return NewAccess;
1581 }
1582 
1584  MemoryAccess *Definition,
1585  const BasicBlock *BB,
1586  InsertionPlace Point) {
1587  MemoryUseOrDef *NewAccess = createDefinedAccess(I, Definition);
1588  auto *Accesses = getOrCreateAccessList(BB);
1589  if (Point == Beginning) {
1590  // It goes after any phi nodes
1591  auto AI = find_if(
1592  *Accesses, [](const MemoryAccess &MA) { return !isa<MemoryPhi>(MA); });
1593 
1594  Accesses->insert(AI, NewAccess);
1595  } else {
1596  Accesses->push_back(NewAccess);
1597  }
1598  BlockNumberingValid.erase(BB);
1599  return NewAccess;
1600 }
1601 
1603  MemoryAccess *Definition,
1604  MemoryUseOrDef *InsertPt) {
1605  assert(I->getParent() == InsertPt->getBlock() &&
1606  "New and old access must be in the same block");
1607  MemoryUseOrDef *NewAccess = createDefinedAccess(I, Definition);
1608  auto *Accesses = getOrCreateAccessList(InsertPt->getBlock());
1609  Accesses->insert(AccessList::iterator(InsertPt), NewAccess);
1610  BlockNumberingValid.erase(InsertPt->getBlock());
1611  return NewAccess;
1612 }
1613 
1615  MemoryAccess *Definition,
1616  MemoryAccess *InsertPt) {
1617  assert(I->getParent() == InsertPt->getBlock() &&
1618  "New and old access must be in the same block");
1619  MemoryUseOrDef *NewAccess = createDefinedAccess(I, Definition);
1620  auto *Accesses = getOrCreateAccessList(InsertPt->getBlock());
1621  Accesses->insertAfter(AccessList::iterator(InsertPt), NewAccess);
1622  BlockNumberingValid.erase(InsertPt->getBlock());
1623  return NewAccess;
1624 }
1625 
1627  MemoryUseOrDef *What) {
1628  assert(What != getLiveOnEntryDef() &&
1629  Where != getLiveOnEntryDef() && "Can't splice (above) LOE.");
1630  assert(dominates(Where, What) && "Only upwards splices are permitted.");
1631 
1632  if (Where == What)
1633  return;
1634  if (isa<MemoryDef>(What)) {
1635  // TODO: possibly use removeMemoryAccess' more efficient RAUW
1636  What->replaceAllUsesWith(What->getDefiningAccess());
1637  What->setDefiningAccess(Where->getDefiningAccess());
1638  Where->setDefiningAccess(What);
1639  }
1641  AccessList *Dest = getWritableBlockAccesses(Where->getBlock());
1642  Dest->splice(AccessList::iterator(Where), *Src, What);
1643 
1644  BlockNumberingValid.erase(What->getBlock());
1645  if (What->getBlock() != Where->getBlock())
1646  BlockNumberingValid.erase(Where->getBlock());
1647 }
1648 
1649 /// \brief Helper function to create new memory accesses
1650 MemoryUseOrDef *MemorySSA::createNewAccess(Instruction *I) {
1651  // The assume intrinsic has a control dependency which we model by claiming
1652  // that it writes arbitrarily. Ignore that fake memory dependency here.
1653  // FIXME: Replace this special casing with a more accurate modelling of
1654  // assume's control dependency.
1655  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
1656  if (II->getIntrinsicID() == Intrinsic::assume)
1657  return nullptr;
1658 
1659  // Find out what affect this instruction has on memory.
1660  ModRefInfo ModRef = AA->getModRefInfo(I);
1661  bool Def = bool(ModRef & MRI_Mod);
1662  bool Use = bool(ModRef & MRI_Ref);
1663 
1664  // It's possible for an instruction to not modify memory at all. During
1665  // construction, we ignore them.
1666  if (!Def && !Use)
1667  return nullptr;
1668 
1669  assert((Def || Use) &&
1670  "Trying to create a memory access with a non-memory instruction");
1671 
1672  MemoryUseOrDef *MUD;
1673  if (Def)
1674  MUD = new MemoryDef(I->getContext(), nullptr, I, I->getParent(), NextID++);
1675  else
1676  MUD = new MemoryUse(I->getContext(), nullptr, I, I->getParent());
1677  ValueToMemoryAccess[I] = MUD;
1678  return MUD;
1679 }
1680 
1681 MemoryAccess *MemorySSA::findDominatingDef(BasicBlock *UseBlock,
1682  enum InsertionPlace Where) {
1683  // Handle the initial case
1684  if (Where == Beginning)
1685  // The only thing that could define us at the beginning is a phi node
1686  if (MemoryPhi *Phi = getMemoryAccess(UseBlock))
1687  return Phi;
1688 
1689  DomTreeNode *CurrNode = DT->getNode(UseBlock);
1690  // Need to be defined by our dominator
1691  if (Where == Beginning)
1692  CurrNode = CurrNode->getIDom();
1693  Where = End;
1694  while (CurrNode) {
1695  auto It = PerBlockAccesses.find(CurrNode->getBlock());
1696  if (It != PerBlockAccesses.end()) {
1697  auto &Accesses = It->second;
1698  for (MemoryAccess &RA : reverse(*Accesses)) {
1699  if (isa<MemoryDef>(RA) || isa<MemoryPhi>(RA))
1700  return &RA;
1701  }
1702  }
1703  CurrNode = CurrNode->getIDom();
1704  }
1705  return LiveOnEntryDef.get();
1706 }
1707 
1708 /// \brief Returns true if \p Replacer dominates \p Replacee .
1709 bool MemorySSA::dominatesUse(const MemoryAccess *Replacer,
1710  const MemoryAccess *Replacee) const {
1711  if (isa<MemoryUseOrDef>(Replacee))
1712  return DT->dominates(Replacer->getBlock(), Replacee->getBlock());
1713  const auto *MP = cast<MemoryPhi>(Replacee);
1714  // For a phi node, the use occurs in the predecessor block of the phi node.
1715  // Since we may occur multiple times in the phi node, we have to check each
1716  // operand to ensure Replacer dominates each operand where Replacee occurs.
1717  for (const Use &Arg : MP->operands()) {
1718  if (Arg.get() != Replacee &&
1719  !DT->dominates(Replacer->getBlock(), MP->getIncomingBlock(Arg)))
1720  return false;
1721  }
1722  return true;
1723 }
1724 
1725 /// \brief If all arguments of a MemoryPHI are defined by the same incoming
1726 /// argument, return that argument.
1728  MemoryAccess *MA = nullptr;
1729 
1730  for (auto &Arg : MP->operands()) {
1731  if (!MA)
1732  MA = cast<MemoryAccess>(Arg);
1733  else if (MA != Arg)
1734  return nullptr;
1735  }
1736  return MA;
1737 }
1738 
1739 /// \brief Properly remove \p MA from all of MemorySSA's lookup tables.
1740 ///
1741 /// Because of the way the intrusive list and use lists work, it is important to
1742 /// do removal in the right order.
1743 void MemorySSA::removeFromLookups(MemoryAccess *MA) {
1744  assert(MA->use_empty() &&
1745  "Trying to remove memory access that still has uses");
1746  BlockNumbering.erase(MA);
1747  if (MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(MA))
1748  MUD->setDefiningAccess(nullptr);
1749  // Invalidate our walker's cache if necessary
1750  if (!isa<MemoryUse>(MA))
1751  Walker->invalidateInfo(MA);
1752  // The call below to erase will destroy MA, so we can't change the order we
1753  // are doing things here
1754  Value *MemoryInst;
1755  if (MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(MA)) {
1756  MemoryInst = MUD->getMemoryInst();
1757  } else {
1758  MemoryInst = MA->getBlock();
1759  }
1760  auto VMA = ValueToMemoryAccess.find(MemoryInst);
1761  if (VMA->second == MA)
1762  ValueToMemoryAccess.erase(VMA);
1763 
1764  auto AccessIt = PerBlockAccesses.find(MA->getBlock());
1765  std::unique_ptr<AccessList> &Accesses = AccessIt->second;
1766  Accesses->erase(MA);
1767  if (Accesses->empty())
1768  PerBlockAccesses.erase(AccessIt);
1769 }
1770 
1772  assert(!isLiveOnEntryDef(MA) && "Trying to remove the live on entry def");
1773  // We can only delete phi nodes if they have no uses, or we can replace all
1774  // uses with a single definition.
1775  MemoryAccess *NewDefTarget = nullptr;
1776  if (MemoryPhi *MP = dyn_cast<MemoryPhi>(MA)) {
1777  // Note that it is sufficient to know that all edges of the phi node have
1778  // the same argument. If they do, by the definition of dominance frontiers
1779  // (which we used to place this phi), that argument must dominate this phi,
1780  // and thus, must dominate the phi's uses, and so we will not hit the assert
1781  // below.
1782  NewDefTarget = onlySingleValue(MP);
1783  assert((NewDefTarget || MP->use_empty()) &&
1784  "We can't delete this memory phi");
1785  } else {
1786  NewDefTarget = cast<MemoryUseOrDef>(MA)->getDefiningAccess();
1787  }
1788 
1789  // Re-point the uses at our defining access
1790  if (!MA->use_empty()) {
1791  // Reset optimized on users of this store, and reset the uses.
1792  // A few notes:
1793  // 1. This is a slightly modified version of RAUW to avoid walking the
1794  // uses twice here.
1795  // 2. If we wanted to be complete, we would have to reset the optimized
1796  // flags on users of phi nodes if doing the below makes a phi node have all
1797  // the same arguments. Instead, we prefer users to removeMemoryAccess those
1798  // phi nodes, because doing it here would be N^3.
1799  if (MA->hasValueHandle())
1800  ValueHandleBase::ValueIsRAUWd(MA, NewDefTarget);
1801  // Note: We assume MemorySSA is not used in metadata since it's not really
1802  // part of the IR.
1803 
1804  while (!MA->use_empty()) {
1805  Use &U = *MA->use_begin();
1806  if (MemoryUse *MU = dyn_cast<MemoryUse>(U.getUser()))
1807  MU->resetOptimized();
1808  U.set(NewDefTarget);
1809  }
1810  }
1811 
1812  // The call below to erase will destroy MA, so we can't change the order we
1813  // are doing things here
1814  removeFromLookups(MA);
1815 }
1816 
1818  MemorySSAAnnotatedWriter Writer(this);
1819  F.print(OS, &Writer);
1820 }
1821 
1822 void MemorySSA::dump() const {
1823  MemorySSAAnnotatedWriter Writer(this);
1824  F.print(dbgs(), &Writer);
1825 }
1826 
1828  verifyDefUses(F);
1829  verifyDomination(F);
1830  verifyOrdering(F);
1831  Walker->verify(this);
1832 }
1833 
1834 /// \brief Verify that the order and existence of MemoryAccesses matches the
1835 /// order and existence of memory affecting instructions.
1837  // Walk all the blocks, comparing what the lookups think and what the access
1838  // lists think, as well as the order in the blocks vs the order in the access
1839  // lists.
1840  SmallVector<MemoryAccess *, 32> ActualAccesses;
1841  for (BasicBlock &B : F) {
1842  const AccessList *AL = getBlockAccesses(&B);
1843  MemoryAccess *Phi = getMemoryAccess(&B);
1844  if (Phi)
1845  ActualAccesses.push_back(Phi);
1846  for (Instruction &I : B) {
1847  MemoryAccess *MA = getMemoryAccess(&I);
1848  assert((!MA || AL) && "We have memory affecting instructions "
1849  "in this block but they are not in the "
1850  "access list");
1851  if (MA)
1852  ActualAccesses.push_back(MA);
1853  }
1854  // Either we hit the assert, really have no accesses, or we have both
1855  // accesses and an access list
1856  if (!AL)
1857  continue;
1858  assert(AL->size() == ActualAccesses.size() &&
1859  "We don't have the same number of accesses in the block as on the "
1860  "access list");
1861  auto ALI = AL->begin();
1862  auto AAI = ActualAccesses.begin();
1863  while (ALI != AL->end() && AAI != ActualAccesses.end()) {
1864  assert(&*ALI == *AAI && "Not the same accesses in the same order");
1865  ++ALI;
1866  ++AAI;
1867  }
1868  ActualAccesses.clear();
1869  }
1870 }
1871 
1872 /// \brief Verify the domination properties of MemorySSA by checking that each
1873 /// definition dominates all of its uses.
1875 #ifndef NDEBUG
1876  for (BasicBlock &B : F) {
1877  // Phi nodes are attached to basic blocks
1878  if (MemoryPhi *MP = getMemoryAccess(&B))
1879  for (const Use &U : MP->uses())
1880  assert(dominates(MP, U) && "Memory PHI does not dominate it's uses");
1881 
1882  for (Instruction &I : B) {
1883  MemoryAccess *MD = dyn_cast_or_null<MemoryDef>(getMemoryAccess(&I));
1884  if (!MD)
1885  continue;
1886 
1887  for (const Use &U : MD->uses())
1888  assert(dominates(MD, U) && "Memory Def does not dominate it's uses");
1889  }
1890  }
1891 #endif
1892 }
1893 
1894 /// \brief Verify the def-use lists in MemorySSA, by verifying that \p Use
1895 /// appears in the use list of \p Def.
1896 
1897 void MemorySSA::verifyUseInDefs(MemoryAccess *Def, MemoryAccess *Use) const {
1898 #ifndef NDEBUG
1899  // The live on entry use may cause us to get a NULL def here
1900  if (!Def)
1901  assert(isLiveOnEntryDef(Use) &&
1902  "Null def but use not point to live on entry def");
1903  else
1904  assert(is_contained(Def->users(), Use) &&
1905  "Did not find use in def's use list");
1906 #endif
1907 }
1908 
1909 /// \brief Verify the immediate use information, by walking all the memory
1910 /// accesses and verifying that, for each use, it appears in the
1911 /// appropriate def's use list
1913  for (BasicBlock &B : F) {
1914  // Phi nodes are attached to basic blocks
1915  if (MemoryPhi *Phi = getMemoryAccess(&B)) {
1916  assert(Phi->getNumOperands() == static_cast<unsigned>(std::distance(
1917  pred_begin(&B), pred_end(&B))) &&
1918  "Incomplete MemoryPhi Node");
1919  for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I)
1920  verifyUseInDefs(Phi->getIncomingValue(I), Phi);
1921  }
1922 
1923  for (Instruction &I : B) {
1924  if (MemoryUseOrDef *MA = getMemoryAccess(&I)) {
1925  verifyUseInDefs(MA->getDefiningAccess(), MA);
1926  }
1927  }
1928  }
1929 }
1930 
1932  return cast_or_null<MemoryUseOrDef>(ValueToMemoryAccess.lookup(I));
1933 }
1934 
1936  return cast_or_null<MemoryPhi>(ValueToMemoryAccess.lookup(cast<Value>(BB)));
1937 }
1938 
1939 /// Perform a local numbering on blocks so that instruction ordering can be
1940 /// determined in constant time.
1941 /// TODO: We currently just number in order. If we numbered by N, we could
1942 /// allow at least N-1 sequences of insertBefore or insertAfter (and at least
1943 /// log2(N) sequences of mixed before and after) without needing to invalidate
1944 /// the numbering.
1945 void MemorySSA::renumberBlock(const BasicBlock *B) const {
1946  // The pre-increment ensures the numbers really start at 1.
1947  unsigned long CurrentNumber = 0;
1948  const AccessList *AL = getBlockAccesses(B);
1949  assert(AL != nullptr && "Asking to renumber an empty block");
1950  for (const auto &I : *AL)
1951  BlockNumbering[&I] = ++CurrentNumber;
1952  BlockNumberingValid.insert(B);
1953 }
1954 
1955 /// \brief Determine, for two memory accesses in the same block,
1956 /// whether \p Dominator dominates \p Dominatee.
1957 /// \returns True if \p Dominator dominates \p Dominatee.
1959  const MemoryAccess *Dominatee) const {
1960 
1961  const BasicBlock *DominatorBlock = Dominator->getBlock();
1962 
1963  assert((DominatorBlock == Dominatee->getBlock()) &&
1964  "Asking for local domination when accesses are in different blocks!");
1965  // A node dominates itself.
1966  if (Dominatee == Dominator)
1967  return true;
1968 
1969  // When Dominatee is defined on function entry, it is not dominated by another
1970  // memory access.
1971  if (isLiveOnEntryDef(Dominatee))
1972  return false;
1973 
1974  // When Dominator is defined on function entry, it dominates the other memory
1975  // access.
1976  if (isLiveOnEntryDef(Dominator))
1977  return true;
1978 
1979  if (!BlockNumberingValid.count(DominatorBlock))
1980  renumberBlock(DominatorBlock);
1981 
1982  unsigned long DominatorNum = BlockNumbering.lookup(Dominator);
1983  // All numbers start with 1
1984  assert(DominatorNum != 0 && "Block was not numbered properly");
1985  unsigned long DominateeNum = BlockNumbering.lookup(Dominatee);
1986  assert(DominateeNum != 0 && "Block was not numbered properly");
1987  return DominatorNum < DominateeNum;
1988 }
1989 
1990 bool MemorySSA::dominates(const MemoryAccess *Dominator,
1991  const MemoryAccess *Dominatee) const {
1992  if (Dominator == Dominatee)
1993  return true;
1994 
1995  if (isLiveOnEntryDef(Dominatee))
1996  return false;
1997 
1998  if (Dominator->getBlock() != Dominatee->getBlock())
1999  return DT->dominates(Dominator->getBlock(), Dominatee->getBlock());
2000  return locallyDominates(Dominator, Dominatee);
2001 }
2002 
2003 bool MemorySSA::dominates(const MemoryAccess *Dominator,
2004  const Use &Dominatee) const {
2005  if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Dominatee.getUser())) {
2006  BasicBlock *UseBB = MP->getIncomingBlock(Dominatee);
2007  // The def must dominate the incoming block of the phi.
2008  if (UseBB != Dominator->getBlock())
2009  return DT->dominates(Dominator->getBlock(), UseBB);
2010  // If the UseBB and the DefBB are the same, compare locally.
2011  return locallyDominates(Dominator, cast<MemoryAccess>(Dominatee));
2012  }
2013  // If it's not a PHI node use, the normal dominates can already handle it.
2014  return dominates(Dominator, cast<MemoryAccess>(Dominatee.getUser()));
2015 }
2016 
2017 const static char LiveOnEntryStr[] = "liveOnEntry";
2018 
2020  MemoryAccess *UO = getDefiningAccess();
2021 
2022  OS << getID() << " = MemoryDef(";
2023  if (UO && UO->getID())
2024  OS << UO->getID();
2025  else
2026  OS << LiveOnEntryStr;
2027  OS << ')';
2028 }
2029 
2031  bool First = true;
2032  OS << getID() << " = MemoryPhi(";
2033  for (const auto &Op : operands()) {
2034  BasicBlock *BB = getIncomingBlock(Op);
2035  MemoryAccess *MA = cast<MemoryAccess>(Op);
2036  if (!First)
2037  OS << ',';
2038  else
2039  First = false;
2040 
2041  OS << '{';
2042  if (BB->hasName())
2043  OS << BB->getName();
2044  else
2045  BB->printAsOperand(OS, false);
2046  OS << ',';
2047  if (unsigned ID = MA->getID())
2048  OS << ID;
2049  else
2050  OS << LiveOnEntryStr;
2051  OS << '}';
2052  }
2053  OS << ')';
2054 }
2055 
2057 
2059  MemoryAccess *UO = getDefiningAccess();
2060  OS << "MemoryUse(";
2061  if (UO && UO->getID())
2062  OS << UO->getID();
2063  else
2064  OS << LiveOnEntryStr;
2065  OS << ')';
2066 }
2067 
2068 void MemoryAccess::dump() const {
2069  print(dbgs());
2070  dbgs() << "\n";
2071 }
2072 
2074 
2077 }
2078 
2080  AU.setPreservesAll();
2083 }
2084 
2086  auto &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA();
2087  MSSA.print(dbgs());
2088  if (VerifyMemorySSA)
2089  MSSA.verifyMemorySSA();
2090  return false;
2091 }
2092 
2093 AnalysisKey MemorySSAAnalysis::Key;
2094 
2097  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
2098  auto &AA = AM.getResult<AAManager>(F);
2099  return MemorySSAAnalysis::Result(make_unique<MemorySSA>(F, &AA, &DT));
2100 }
2101 
2104  OS << "MemorySSA for function: " << F.getName() << "\n";
2105  AM.getResult<MemorySSAAnalysis>(F).getMSSA().print(OS);
2106 
2107  return PreservedAnalyses::all();
2108 }
2109 
2112  AM.getResult<MemorySSAAnalysis>(F).getMSSA().verifyMemorySSA();
2113 
2114  return PreservedAnalyses::all();
2115 }
2116 
2117 char MemorySSAWrapperPass::ID = 0;
2118 
2121 }
2122 
2123 void MemorySSAWrapperPass::releaseMemory() { MSSA.reset(); }
2124 
2126  AU.setPreservesAll();
2129 }
2130 
2132  auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2133  auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
2134  MSSA.reset(new MemorySSA(F, &AA, &DT));
2135  return false;
2136 }
2137 
2139 
2141  MSSA->print(OS);
2142 }
2143 
2145 
2147  DominatorTree *D)
2148  : MemorySSAWalker(M), Walker(*M, *A, *D, Cache), AutoResetWalker(true) {}
2149 
2151 
2153  // TODO: We can do much better cache invalidation with differently stored
2154  // caches. For now, for MemoryUses, we simply remove them
2155  // from the cache, and kill the entire call/non-call cache for everything
2156  // else. The problem is for phis or defs, currently we'd need to follow use
2157  // chains down and invalidate anything below us in the chain that currently
2158  // terminates at this access.
2159 
2160  // See if this is a MemoryUse, if so, just remove the cached info. MemoryUse
2161  // is by definition never a barrier, so nothing in the cache could point to
2162  // this use. In that case, we only need invalidate the info for the use
2163  // itself.
2164 
2165  if (MemoryUse *MU = dyn_cast<MemoryUse>(MA)) {
2166  UpwardsMemoryQuery Q(MU->getMemoryInst(), MU);
2167  Cache.remove(MU, Q.StartingLoc, Q.IsCall);
2168  MU->resetOptimized();
2169  } else {
2170  // If it is not a use, the best we can do right now is destroy the cache.
2171  Cache.clear();
2172  }
2173 
2174 #ifdef EXPENSIVE_CHECKS
2175  verifyRemoved(MA);
2176 #endif
2177 }
2178 
2179 /// \brief Walk the use-def chains starting at \p MA and find
2180 /// the MemoryAccess that actually clobbers Loc.
2181 ///
2182 /// \returns our clobbering memory access
2183 MemoryAccess *MemorySSA::CachingWalker::getClobberingMemoryAccess(
2184  MemoryAccess *StartingAccess, UpwardsMemoryQuery &Q) {
2185  MemoryAccess *New = Walker.findClobber(StartingAccess, Q);
2186 #ifdef EXPENSIVE_CHECKS
2187  MemoryAccess *NewNoCache =
2188  Walker.findClobber(StartingAccess, Q, /*UseWalkerCache=*/false);
2189  assert(NewNoCache == New && "Cache made us hand back a different result?");
2190 #endif
2191  if (AutoResetWalker)
2192  resetClobberWalker();
2193  return New;
2194 }
2195 
2196 MemoryAccess *MemorySSA::CachingWalker::getClobberingMemoryAccess(
2197  MemoryAccess *StartingAccess, const MemoryLocation &Loc) {
2198  if (isa<MemoryPhi>(StartingAccess))
2199  return StartingAccess;
2200 
2201  auto *StartingUseOrDef = cast<MemoryUseOrDef>(StartingAccess);
2202  if (MSSA->isLiveOnEntryDef(StartingUseOrDef))
2203  return StartingUseOrDef;
2204 
2205  Instruction *I = StartingUseOrDef->getMemoryInst();
2206 
2207  // Conservatively, fences are always clobbers, so don't perform the walk if we
2208  // hit a fence.
2209  if (!ImmutableCallSite(I) && I->isFenceLike())
2210  return StartingUseOrDef;
2211 
2212  UpwardsMemoryQuery Q;
2213  Q.OriginalAccess = StartingUseOrDef;
2214  Q.StartingLoc = Loc;
2215  Q.Inst = I;
2216  Q.IsCall = false;
2217 
2218  if (auto *CacheResult = Cache.lookup(StartingUseOrDef, Loc, Q.IsCall))
2219  return CacheResult;
2220 
2221  // Unlike the other function, do not walk to the def of a def, because we are
2222  // handed something we already believe is the clobbering access.
2223  MemoryAccess *DefiningAccess = isa<MemoryUse>(StartingUseOrDef)
2224  ? StartingUseOrDef->getDefiningAccess()
2225  : StartingUseOrDef;
2226 
2227  MemoryAccess *Clobber = getClobberingMemoryAccess(DefiningAccess, Q);
2228  DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is ");
2229  DEBUG(dbgs() << *StartingUseOrDef << "\n");
2230  DEBUG(dbgs() << "Final Memory SSA clobber for " << *I << " is ");
2231  DEBUG(dbgs() << *Clobber << "\n");
2232  return Clobber;
2233 }
2234 
2235 MemoryAccess *
2236 MemorySSA::CachingWalker::getClobberingMemoryAccess(MemoryAccess *MA) {
2237  auto *StartingAccess = dyn_cast<MemoryUseOrDef>(MA);
2238  // If this is a MemoryPhi, we can't do anything.
2239  if (!StartingAccess)
2240  return MA;
2241 
2242  // If this is an already optimized use or def, return the optimized result.
2243  // Note: Currently, we do not store the optimized def result because we'd need
2244  // a separate field, since we can't use it as the defining access.
2245  if (MemoryUse *MU = dyn_cast<MemoryUse>(StartingAccess))
2246  if (MU->isOptimized())
2247  return MU->getDefiningAccess();
2248 
2249  const Instruction *I = StartingAccess->getMemoryInst();
2250  UpwardsMemoryQuery Q(I, StartingAccess);
2251  // We can't sanely do anything with a fences, they conservatively
2252  // clobber all memory, and have no locations to get pointers from to
2253  // try to disambiguate.
2254  if (!Q.IsCall && I->isFenceLike())
2255  return StartingAccess;
2256 
2257  if (auto *CacheResult = Cache.lookup(StartingAccess, Q.StartingLoc, Q.IsCall))
2258  return CacheResult;
2259 
2260  if (isUseTriviallyOptimizableToLiveOnEntry(*MSSA->AA, I)) {
2261  MemoryAccess *LiveOnEntry = MSSA->getLiveOnEntryDef();
2262  Cache.insert(StartingAccess, LiveOnEntry, Q.StartingLoc, Q.IsCall);
2263  if (MemoryUse *MU = dyn_cast<MemoryUse>(StartingAccess))
2264  MU->setDefiningAccess(LiveOnEntry, true);
2265  return LiveOnEntry;
2266  }
2267 
2268  // Start with the thing we already think clobbers this location
2269  MemoryAccess *DefiningAccess = StartingAccess->getDefiningAccess();
2270 
2271  // At this point, DefiningAccess may be the live on entry def.
2272  // If it is, we will not get a better result.
2273  if (MSSA->isLiveOnEntryDef(DefiningAccess))
2274  return DefiningAccess;
2275 
2276  MemoryAccess *Result = getClobberingMemoryAccess(DefiningAccess, Q);
2277  DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is ");
2278  DEBUG(dbgs() << *DefiningAccess << "\n");
2279  DEBUG(dbgs() << "Final Memory SSA clobber for " << *I << " is ");
2280  DEBUG(dbgs() << *Result << "\n");
2281  if (MemoryUse *MU = dyn_cast<MemoryUse>(StartingAccess))
2282  MU->setDefiningAccess(Result, true);
2283 
2284  return Result;
2285 }
2286 
2287 // Verify that MA doesn't exist in any of the caches.
2288 void MemorySSA::CachingWalker::verifyRemoved(MemoryAccess *MA) {
2289  assert(!Cache.contains(MA) && "Found removed MemoryAccess in cache.");
2290 }
2291 
2292 MemoryAccess *
2294  if (auto *Use = dyn_cast<MemoryUseOrDef>(MA))
2295  return Use->getDefiningAccess();
2296  return MA;
2297 }
2298 
2300  MemoryAccess *StartingAccess, const MemoryLocation &) {
2301  if (auto *Use = dyn_cast<MemoryUseOrDef>(StartingAccess))
2302  return Use->getDefiningAccess();
2303  return StartingAccess;
2304 }
2305 } // namespace llvm
MachineLoop * L
const NoneType None
Definition: None.h:23
static Reorderability getLoadReorderability(const LoadInst *Use, const LoadInst *MayClobber)
This does one-way checks to see if Use could theoretically be hoisted above MayClobber.
Definition: MemorySSA.cpp:183
void initializeMemorySSAWrapperPassPass(PassRegistry &)
void push_back(const T &Elt)
Definition: SmallVector.h:211
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:1990
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *) override
Does the same thing as getClobberingMemoryAccess(const Instruction *I), but takes a MemoryAccess inst...
Definition: MemorySSA.cpp:2293
iterator_range< use_iterator > uses()
Definition: Value.h:326
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:226
virtual void verify(const MemorySSA *MSSA)
Definition: MemorySSA.h:813
void dropAllReferences()
Drop all references to operands.
Definition: User.h:269
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
bool isFenceLike() const
Return true if this instruction behaves like a memory fence: it can load or store to memory location ...
Definition: Instruction.h:429
bool hasName() const
Definition: Value.h:236
STATISTIC(NumFunctions,"Total number of functions")
virtual void emitBasicBlockStartAnnot(const BasicBlock *BB, formatted_raw_ostream &OS)
emitBasicBlockStartAnnot - This may be implemented to emit a string right after the basic block label...
Definition: MemorySSA.cpp:83
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:162
This is the interface for a simple mod/ref and alias analysis over globals.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:182
Result run(Function &F, FunctionAnalysisManager &AM)
Definition: MemorySSA.cpp:2095
bool defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU, AliasAnalysis &AA)
Definition: MemorySSA.cpp:272
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
This class provides various memory handling functions that manipulate MemoryBlock instances...
Definition: Memory.h:46
Implements a dense probed hash-table based set.
Definition: DenseSet.h:202
unsigned getNumOperands() const
Definition: User.h:167
bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
A trivial helper function to check to see if the specified pointers are no-alias. ...
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
This file contains the declarations for metadata subclasses.
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:380
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
Definition: MemorySSA.h:289
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: MemorySSA.cpp:2079
void print(raw_ostream &OS) const override
Definition: MemorySSA.cpp:2030
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:736
virtual void emitInstructionAnnot(const Instruction *I, formatted_raw_ostream &OS)
emitInstructionAnnot - This may be implemented to emit a string right before an instruction is emitte...
Definition: MemorySSA.cpp:89
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:189
OptimizeUses(MemorySSA *MSSA, MemorySSAWalker *Walker, AliasAnalysis *AA, DominatorTree *DT)
Definition: MemorySSA.cpp:1260
An instruction for reading from memory.
Definition: Instructions.h:164
memoryssa
Definition: MemorySSA.cpp:55
The access modifies the value stored in memory.
INITIALIZE_PASS_BEGIN(MemorySSAWrapperPass,"memoryssa","Memory SSA", false, true) INITIALIZE_PASS_END(MemorySSAWrapperPass
MemoryUseOrDef * createMemoryAccessBefore(Instruction *I, MemoryAccess *Definition, MemoryUseOrDef *InsertPt)
Create a MemoryAccess in MemorySSA before or after an existing MemoryAccess.
Definition: MemorySSA.cpp:1602
return AArch64::GPR64RegClass contains(Reg)
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:345
DomTreeNodeBase< NodeT > * getIDom() const
Represents read-only accesses to memory.
Definition: MemorySSA.h:234
virtual void dump() const
Definition: MemorySSA.cpp:2068
safe Safe Stack instrumentation pass
Definition: SafeStack.cpp:796
This class is a batch walker of all MemoryUse's in the program, and points their defining access at t...
Definition: MemorySSA.cpp:1258
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: MemorySSA.cpp:2110
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
Legacy analysis pass which computes MemorySSA.
Definition: MemorySSA.h:723
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: AliasAnalysis.h:94
ppc ctr loops PowerPC CTR Loops Verify
A MemorySSAWalker that does AA walks and caching of lookups to disambiguate accesses.
Definition: MemorySSA.cpp:1085
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
AccessList * getWritableBlockAccesses(const BasicBlock *BB) const
Definition: MemorySSA.h:623
MemorySSAAnnotatedWriter(const MemorySSA *M)
Definition: MemorySSA.cpp:81
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:500
MemoryUseOrDef * getMemoryAccess(const Instruction *) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition: MemorySSA.cpp:1931
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
Definition: MemorySSA.h:202
The access references the value stored in memory.
Definition: AliasAnalysis.h:98
static const uint16_t * lookup(unsigned opcode, unsigned domain, ArrayRef< uint16_t[3]> Table)
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:241
BasicBlock * getBlock() const
Definition: MemorySSA.h:141
CachingWalker(MemorySSA *, AliasAnalysis *, DominatorTree *)
Definition: MemorySSA.cpp:2146
#define F(x, y, z)
Definition: MD5.cpp:51
static int getID(struct InternalInstruction *insn, const void *miiArg)
upward_defs_iterator upward_defs_end()
Definition: MemorySSA.h:1006
Memory false
Definition: MemorySSA.cpp:55
early cse memssa
Definition: EarlyCSE.cpp:1064
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:3473
void removeMemoryAccess(MemoryAccess *)
Remove a MemoryAccess from MemorySSA, including updating all definitions and uses.
Definition: MemorySSA.cpp:1771
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:269
Memory SSA
Definition: MemorySSA.cpp:55
Base class for the actual dominator tree node.
bool runOnFunction(Function &) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Definition: MemorySSA.cpp:2085
virtual unsigned getID() const =0
Used for debugging and tracking things about MemoryAccesses.
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
iplist< MemoryAccess > AccessList
Definition: MemorySSA.h:532
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:401
static void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:837
This is the generic walker interface for walkers of MemorySSA.
Definition: MemorySSA.h:753
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:65
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:96
NodeT * getBlock() const
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: MemorySSA.cpp:2102
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
static MemoryAccess * onlySingleValue(MemoryPhi *MP)
If all arguments of a MemoryPHI are defined by the same incoming argument, return that argument...
Definition: MemorySSA.cpp:1727
static bool instructionClobbersQuery(MemoryDef *MD, const MemoryLocation &UseLoc, const Instruction *UseInst, AliasAnalysis &AA)
Definition: MemorySSA.cpp:215
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
Checks whether the given location points to constant memory, or if OrLocal is true whether it points ...
An assembly annotator class to print Memory SSA information in comments.
Definition: MemorySSA.cpp:76
void setAutoResetWalker(bool AutoReset)
Whether we call resetClobberWalker() after each time we actually walk to answer a clobber query...
Definition: MemorySSA.cpp:1105
The access neither references nor modifies the value stored in memory.
Definition: AliasAnalysis.h:96
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
#define P(N)
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:395
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: MemorySSA.cpp:2138
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
void set(Value *Val)
Definition: Value.h:624
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:653
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Definition: MemorySSA.h:403
void optimizeUses()
Optimize uses to point to their actual clobbering definitions.
Definition: MemorySSA.cpp:1445
df_iterator< T > df_end(const T &G)
void verifyOrdering(Function &F) const
Verify that the order and existence of MemoryAccesses matches the order and existence of memory affec...
Definition: MemorySSA.cpp:1836
static bool isAtLeastOrStrongerThan(AtomicOrdering ao, AtomicOrdering other)
void push_front(pointer val)
Definition: ilist.h:325
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
A manager for alias analyses.
void addIncoming(MemoryAccess *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Definition: MemorySSA.h:436
static bool isEqual(const MemoryLocOrCall &LHS, const MemoryLocOrCall &RHS)
Definition: MemorySSA.cpp:170
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:368
early cse Early CSE w MemorySSA
Definition: EarlyCSE.cpp:1064
MemorySSA(Function &, AliasAnalysis *, DominatorTree *)
Definition: MemorySSA.cpp:1230
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:116
bool runOnFunction(Function &) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Definition: MemorySSA.cpp:2131
MemorySSAWalker(MemorySSA *)
Definition: MemorySSA.cpp:2144
Represent the analysis usage information of a pass.
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:1958
void splice(iterator where, iplist_impl &L2)
Definition: ilist.h:342
void setDefiningAccess(MemoryAccess *DMA)
Definition: MemorySSA.h:218
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:150
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
User * getUser() const
Returns the User that contains this Use.
Definition: Use.cpp:41
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:119
op_range operands()
Definition: User.h:213
Memory true print Memory SSA Printer
Definition: MemorySSA.cpp:61
bool hasValueHandle() const
Return true if there is a value handle associated with this value.
Definition: Value.h:456
void verifyDefUses(Function &F) const
Verify the immediate use information, by walking all the memory accesses and verifying that...
Definition: MemorySSA.cpp:1912
void print(raw_ostream &OS) const override
Definition: MemorySSA.cpp:2058
MemoryAccess * getIncomingValue(unsigned I) const
Return incoming value number x.
Definition: MemorySSA.h:406
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:392
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition: MemorySSA.cpp:2140
BlockMass operator*(BlockMass L, BranchProbability R)
static const char LiveOnEntryStr[]
Definition: MemorySSA.cpp:2017
void verifyDomination(Function &F) const
Verify the domination properties of MemorySSA by checking that each definition dominates all of its u...
Definition: MemorySSA.cpp:1874
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:113
~MemoryAccess() override
Definition: MemorySSA.cpp:2056
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:654
An intrusive list with ownership and callbacks specified/controlled by ilist_traits, only with API safe for polymorphic types.
Definition: ilist.h:403
MemoryAccess * getLiveOnEntryDef() const
Definition: MemorySSA.h:528
MemorySSAWalker * getWalker()
Definition: MemorySSA.cpp:1551
MemoryAccess * createMemoryAccessInBB(Instruction *I, MemoryAccess *Definition, const BasicBlock *BB, InsertionPlace Point)
Create a MemoryAccess in MemorySSA at a specified point in a block, with a specified clobbering defin...
Definition: MemorySSA.cpp:1583
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:218
Representation for a specific memory location.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:425
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:757
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
Module.h This file contains the declarations for the Module class.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:175
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Definition: Instructions.h:218
An analysis that produces MemorySSA for a function.
Definition: MemorySSA.h:690
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:382
std::vector< DomTreeNodeBase< NodeT > * >::const_iterator const_iterator
AtomicOrdering getOrdering() const
Returns the ordering effect of this fence.
Definition: Instructions.h:234
void verify(const MemorySSA *MSSA) override
Definition: MemorySSA.cpp:1113
const BasicBlock & getEntryBlock() const
Definition: Function.h:519
#define MAP(n)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void print(raw_ostream &OS) const override
Definition: MemorySSA.cpp:2019
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:586
df_iterator< T > df_begin(const T &G)
A range adaptor for a pair of iterators.
Target - Wrapper for Target specific information.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:602
void verifyMemorySSA() const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
Definition: MemorySSA.cpp:1827
Class that has the common methods + fields of memory uses/defs.
Definition: MemorySSA.h:191
ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc)
getModRefInfo (for call sites) - Return information about whether a particular call site modifies or ...
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator_range< user_iterator > users()
Definition: Value.h:370
bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
A trivial helper function to check to see if the specified pointers are must-alias.
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:191
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)"))
void resetClobberWalker()
Drop the walker's persistent data structures.
Definition: MemorySSA.cpp:1111
void initializeMemorySSAPrinterLegacyPassPass(PassRegistry &)
use_iterator use_begin()
Definition: Value.h:310
Basic Alias true
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
iterator insert(iterator where, pointer New)
Definition: ilist.h:241
void emplace_back(ArgTypes &&...Args)
Definition: SmallVector.h:635
This file provides utility analysis objects describing memory locations.
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:665
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read, bool &Write, bool &Effects, bool &StackPointer)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:287
void dump() const
Definition: MemorySSA.cpp:1822
MemoryPhi * createMemoryPhi(BasicBlock *BB)
Create an empty MemoryPhi in MemorySSA for a given basic block.
Definition: MemorySSA.cpp:1561
AnalysisUsage & addRequiredTransitive()
iterator_range< df_iterator< T > > depth_first(const T &G)
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:782
Determine the iterated dominance frontier, given a set of defining blocks, and optionally, a set of live-in blocks.
Instruction * getMemoryInst() const
Get the instruction that this MemoryUse represents.
Definition: MemorySSA.h:199
void spliceMemoryAccessAbove(MemoryDef *Where, MemoryUseOrDef *What)
Definition: MemorySSA.cpp:1626
bool use_empty() const
Definition: Value.h:299
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static cl::opt< bool > VerifyMemorySSA("verify-memoryssa", cl::init(false), cl::Hidden, cl::desc("Verify MemorySSA in legacy printer pass."))
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:33
LLVM Value Representation.
Definition: Value.h:71
static MemoryLocOrCall getTombstoneKey()
Definition: MemorySSA.cpp:159
succ_range successors(BasicBlock *BB)
Definition: IR/CFG.h:143
upward_defs_iterator upward_defs_begin(const MemoryAccessPair &Pair)
Definition: MemorySSA.h:1002
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: MemorySSA.cpp:2125
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
#define DEBUG(X)
Definition: Debug.h:100
static unsigned getHashValue(const MemoryLocOrCall &MLOC)
Definition: MemorySSA.cpp:162
void invalidateInfo(MemoryAccess *) override
Given a memory access, invalidate anything this walker knows about that access.
Definition: MemorySSA.cpp:2152
void print(raw_ostream &) const
Definition: MemorySSA.cpp:1817
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:217
ppc ctr loops verify
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1722
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
Represents phi nodes for memory accesses.
Definition: MemorySSA.h:354
auto find_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:764
iterator insertAfter(iterator where, pointer New)
Definition: ilist.h:250
const AccessList * getBlockAccesses(const BasicBlock *BB) const
Return the list of MemoryAccess's for a given basic block.
Definition: MemorySSA.h:537
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
MemoryUseOrDef * createMemoryAccessAfter(Instruction *I, MemoryAccess *Definition, MemoryAccess *InsertPt)
Definition: MemorySSA.cpp:1614
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:2123
Reorderability
Definition: MemorySSA.cpp:175
static MemoryLocOrCall getEmptyKey()
Definition: MemorySSA.cpp:156
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:64
const BasicBlock * getParent() const
Definition: Instruction.h:62
hexagon widen stores
std::pair< MemoryAccess *, MemoryLocation > MemoryAccessPair
Definition: MemorySSA.h:833
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
Definition: MemorySSA.h:524
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
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:783