LLVM  10.0.0
MemorySSA.h
Go to the documentation of this file.
1 //===- MemorySSA.h - Build Memory SSA ---------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This file exposes an interface to building/using memory SSA to
11 /// walk memory instructions using a use/def graph.
12 ///
13 /// Memory SSA class builds an SSA form that links together memory access
14 /// instructions such as loads, stores, atomics, and calls. Additionally, it
15 /// does a trivial form of "heap versioning" Every time the memory state changes
16 /// in the program, we generate a new heap version. It generates
17 /// MemoryDef/Uses/Phis that are overlayed on top of the existing instructions.
18 ///
19 /// As a trivial example,
20 /// define i32 @main() #0 {
21 /// entry:
22 /// %call = call noalias i8* @_Znwm(i64 4) #2
23 /// %0 = bitcast i8* %call to i32*
24 /// %call1 = call noalias i8* @_Znwm(i64 4) #2
25 /// %1 = bitcast i8* %call1 to i32*
26 /// store i32 5, i32* %0, align 4
27 /// store i32 7, i32* %1, align 4
28 /// %2 = load i32* %0, align 4
29 /// %3 = load i32* %1, align 4
30 /// %add = add nsw i32 %2, %3
31 /// ret i32 %add
32 /// }
33 ///
34 /// Will become
35 /// define i32 @main() #0 {
36 /// entry:
37 /// ; 1 = MemoryDef(0)
38 /// %call = call noalias i8* @_Znwm(i64 4) #3
39 /// %2 = bitcast i8* %call to i32*
40 /// ; 2 = MemoryDef(1)
41 /// %call1 = call noalias i8* @_Znwm(i64 4) #3
42 /// %4 = bitcast i8* %call1 to i32*
43 /// ; 3 = MemoryDef(2)
44 /// store i32 5, i32* %2, align 4
45 /// ; 4 = MemoryDef(3)
46 /// store i32 7, i32* %4, align 4
47 /// ; MemoryUse(3)
48 /// %7 = load i32* %2, align 4
49 /// ; MemoryUse(4)
50 /// %8 = load i32* %4, align 4
51 /// %add = add nsw i32 %7, %8
52 /// ret i32 %add
53 /// }
54 ///
55 /// Given this form, all the stores that could ever effect the load at %8 can be
56 /// gotten by using the MemoryUse associated with it, and walking from use to
57 /// def until you hit the top of the function.
58 ///
59 /// Each def also has a list of users associated with it, so you can walk from
60 /// both def to users, and users to defs. Note that we disambiguate MemoryUses,
61 /// but not the RHS of MemoryDefs. You can see this above at %7, which would
62 /// otherwise be a MemoryUse(4). Being disambiguated means that for a given
63 /// store, all the MemoryUses on its use lists are may-aliases of that store
64 /// (but the MemoryDefs on its use list may not be).
65 ///
66 /// MemoryDefs are not disambiguated because it would require multiple reaching
67 /// definitions, which would require multiple phis, and multiple memoryaccesses
68 /// per instruction.
69 //
70 //===----------------------------------------------------------------------===//
71 
72 #ifndef LLVM_ANALYSIS_MEMORYSSA_H
73 #define LLVM_ANALYSIS_MEMORYSSA_H
74 
75 #include "llvm/ADT/DenseMap.h"
76 #include "llvm/ADT/GraphTraits.h"
77 #include "llvm/ADT/SmallPtrSet.h"
78 #include "llvm/ADT/SmallVector.h"
79 #include "llvm/ADT/ilist.h"
80 #include "llvm/ADT/ilist_node.h"
81 #include "llvm/ADT/iterator.h"
83 #include "llvm/ADT/simple_ilist.h"
87 #include "llvm/IR/BasicBlock.h"
88 #include "llvm/IR/DerivedUser.h"
89 #include "llvm/IR/Dominators.h"
90 #include "llvm/IR/Module.h"
91 #include "llvm/IR/Type.h"
92 #include "llvm/IR/Use.h"
93 #include "llvm/IR/User.h"
94 #include "llvm/IR/Value.h"
95 #include "llvm/IR/ValueHandle.h"
96 #include "llvm/Pass.h"
97 #include "llvm/Support/Casting.h"
99 #include <algorithm>
100 #include <cassert>
101 #include <cstddef>
102 #include <iterator>
103 #include <memory>
104 #include <utility>
105 
106 namespace llvm {
107 
108 /// Enables memory ssa as a dependency for loop passes.
109 extern cl::opt<bool> EnableMSSALoopDependency;
110 
111 class Function;
112 class Instruction;
113 class MemoryAccess;
114 class MemorySSAWalker;
115 class LLVMContext;
116 class raw_ostream;
117 
118 namespace MSSAHelpers {
119 
120 struct AllAccessTag {};
121 struct DefsOnlyTag {};
122 
123 } // end namespace MSSAHelpers
124 
125 enum : unsigned {
126  // Used to signify what the default invalid ID is for MemoryAccess's
127  // getID()
129 };
130 
131 template <class T> class memoryaccess_def_iterator_base;
135 
136 // The base for all memory accesses. All memory accesses in a block are
137 // linked together using an intrusive list.
139  : public DerivedUser,
140  public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>,
141  public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>> {
142 public:
143  using AllAccessType =
145  using DefsOnlyType =
147 
148  MemoryAccess(const MemoryAccess &) = delete;
149  MemoryAccess &operator=(const MemoryAccess &) = delete;
150 
151  void *operator new(size_t) = delete;
152 
153  // Methods for support type inquiry through isa, cast, and
154  // dyn_cast
155  static bool classof(const Value *V) {
156  unsigned ID = V->getValueID();
157  return ID == MemoryUseVal || ID == MemoryPhiVal || ID == MemoryDefVal;
158  }
159 
160  BasicBlock *getBlock() const { return Block; }
161 
162  void print(raw_ostream &OS) const;
163  void dump() const;
164 
165  /// The user iterators for a memory access
168 
169  /// This iterator walks over all of the defs in a given
170  /// MemoryAccess. For MemoryPhi nodes, this walks arguments. For
171  /// MemoryUse/MemoryDef, this walks the defining access.
172  memoryaccess_def_iterator defs_begin();
173  const_memoryaccess_def_iterator defs_begin() const;
174  memoryaccess_def_iterator defs_end();
175  const_memoryaccess_def_iterator defs_end() const;
176 
177  /// Get the iterators for the all access list and the defs only list
178  /// We default to the all access list.
180  return this->AllAccessType::getIterator();
181  }
183  return this->AllAccessType::getIterator();
184  }
186  return this->AllAccessType::getReverseIterator();
187  }
189  return this->AllAccessType::getReverseIterator();
190  }
192  return this->DefsOnlyType::getIterator();
193  }
195  return this->DefsOnlyType::getIterator();
196  }
198  return this->DefsOnlyType::getReverseIterator();
199  }
201  return this->DefsOnlyType::getReverseIterator();
202  }
203 
204 protected:
205  friend class MemoryDef;
206  friend class MemoryPhi;
207  friend class MemorySSA;
208  friend class MemoryUse;
209  friend class MemoryUseOrDef;
210 
211  /// Used by MemorySSA to change the block of a MemoryAccess when it is
212  /// moved.
213  void setBlock(BasicBlock *BB) { Block = BB; }
214 
215  /// Used for debugging and tracking things about MemoryAccesses.
216  /// Guaranteed unique among MemoryAccesses, no guarantees otherwise.
217  inline unsigned getID() const;
218 
219  MemoryAccess(LLVMContext &C, unsigned Vty, DeleteValueTy DeleteValue,
220  BasicBlock *BB, unsigned NumOperands)
221  : DerivedUser(Type::getVoidTy(C), Vty, nullptr, NumOperands, DeleteValue),
222  Block(BB) {}
223 
224  // Use deleteValue() to delete a generic MemoryAccess.
225  ~MemoryAccess() = default;
226 
227 private:
228  BasicBlock *Block;
229 };
230 
231 template <>
233  static void deleteNode(MemoryAccess *MA) { MA->deleteValue(); }
234 };
235 
237  MA.print(OS);
238  return OS;
239 }
240 
241 /// Class that has the common methods + fields of memory uses/defs. It's
242 /// a little awkward to have, but there are many cases where we want either a
243 /// use or def, and there are many cases where uses are needed (defs aren't
244 /// acceptable), and vice-versa.
245 ///
246 /// This class should never be instantiated directly; make a MemoryUse or
247 /// MemoryDef instead.
248 class MemoryUseOrDef : public MemoryAccess {
249 public:
250  void *operator new(size_t) = delete;
251 
253 
254  /// Get the instruction that this MemoryUse represents.
255  Instruction *getMemoryInst() const { return MemoryInstruction; }
256 
257  /// Get the access that produces the memory state used by this Use.
258  MemoryAccess *getDefiningAccess() const { return getOperand(0); }
259 
260  static bool classof(const Value *MA) {
261  return MA->getValueID() == MemoryUseVal || MA->getValueID() == MemoryDefVal;
262  }
263 
264  // Sadly, these have to be public because they are needed in some of the
265  // iterators.
266  inline bool isOptimized() const;
267  inline MemoryAccess *getOptimized() const;
268  inline void setOptimized(MemoryAccess *);
269 
270  // Retrieve AliasResult type of the optimized access. Ideally this would be
271  // returned by the caching walker and may go away in the future.
273  return OptimizedAccessAlias;
274  }
275 
276  /// Reset the ID of what this MemoryUse was optimized to, causing it to
277  /// be rewalked by the walker if necessary.
278  /// This really should only be called by tests.
279  inline void resetOptimized();
280 
281 protected:
282  friend class MemorySSA;
283  friend class MemorySSAUpdater;
284 
285  MemoryUseOrDef(LLVMContext &C, MemoryAccess *DMA, unsigned Vty,
286  DeleteValueTy DeleteValue, Instruction *MI, BasicBlock *BB,
287  unsigned NumOperands)
288  : MemoryAccess(C, Vty, DeleteValue, BB, NumOperands),
289  MemoryInstruction(MI), OptimizedAccessAlias(MayAlias) {
290  setDefiningAccess(DMA);
291  }
292 
293  // Use deleteValue() to delete a generic MemoryUseOrDef.
294  ~MemoryUseOrDef() = default;
295 
297  OptimizedAccessAlias = AR;
298  }
299 
300  void setDefiningAccess(MemoryAccess *DMA, bool Optimized = false,
302  if (!Optimized) {
303  setOperand(0, DMA);
304  return;
305  }
306  setOptimized(DMA);
307  setOptimizedAccessType(AR);
308  }
309 
310 private:
311  Instruction *MemoryInstruction;
312  Optional<AliasResult> OptimizedAccessAlias;
313 };
314 
315 /// Represents read-only accesses to memory
316 ///
317 /// In particular, the set of Instructions that will be represented by
318 /// MemoryUse's is exactly the set of Instructions for which
319 /// AliasAnalysis::getModRefInfo returns "Ref".
320 class MemoryUse final : public MemoryUseOrDef {
321 public:
323 
325  : MemoryUseOrDef(C, DMA, MemoryUseVal, deleteMe, MI, BB,
326  /*NumOperands=*/1) {}
327 
328  // allocate space for exactly one operand
329  void *operator new(size_t s) { return User::operator new(s, 1); }
330 
331  static bool classof(const Value *MA) {
332  return MA->getValueID() == MemoryUseVal;
333  }
334 
335  void print(raw_ostream &OS) const;
336 
338  OptimizedID = DMA->getID();
339  setOperand(0, DMA);
340  }
341 
342  bool isOptimized() const {
343  return getDefiningAccess() && OptimizedID == getDefiningAccess()->getID();
344  }
345 
347  return getDefiningAccess();
348  }
349 
350  void resetOptimized() {
351  OptimizedID = INVALID_MEMORYACCESS_ID;
352  }
353 
354 protected:
355  friend class MemorySSA;
356 
357 private:
358  static void deleteMe(DerivedUser *Self);
359 
360  unsigned OptimizedID = INVALID_MEMORYACCESS_ID;
361 };
362 
363 template <>
364 struct OperandTraits<MemoryUse> : public FixedNumOperandTraits<MemoryUse, 1> {};
366 
367 /// Represents a read-write access to memory, whether it is a must-alias,
368 /// or a may-alias.
369 ///
370 /// In particular, the set of Instructions that will be represented by
371 /// MemoryDef's is exactly the set of Instructions for which
372 /// AliasAnalysis::getModRefInfo returns "Mod" or "ModRef".
373 /// Note that, in order to provide def-def chains, all defs also have a use
374 /// associated with them. This use points to the nearest reaching
375 /// MemoryDef/MemoryPhi.
376 class MemoryDef final : public MemoryUseOrDef {
377 public:
378  friend class MemorySSA;
379 
381 
383  unsigned Ver)
384  : MemoryUseOrDef(C, DMA, MemoryDefVal, deleteMe, MI, BB,
385  /*NumOperands=*/2),
386  ID(Ver) {}
387 
388  // allocate space for exactly two operands
389  void *operator new(size_t s) { return User::operator new(s, 2); }
390 
391  static bool classof(const Value *MA) {
392  return MA->getValueID() == MemoryDefVal;
393  }
394 
396  setOperand(1, MA);
397  OptimizedID = MA->getID();
398  }
399 
401  return cast_or_null<MemoryAccess>(getOperand(1));
402  }
403 
404  bool isOptimized() const {
405  return getOptimized() && OptimizedID == getOptimized()->getID();
406  }
407 
408  void resetOptimized() {
409  OptimizedID = INVALID_MEMORYACCESS_ID;
410  setOperand(1, nullptr);
411  }
412 
413  void print(raw_ostream &OS) const;
414 
415  unsigned getID() const { return ID; }
416 
417 private:
418  static void deleteMe(DerivedUser *Self);
419 
420  const unsigned ID;
421  unsigned OptimizedID = INVALID_MEMORYACCESS_ID;
422 };
423 
424 template <>
425 struct OperandTraits<MemoryDef> : public FixedNumOperandTraits<MemoryDef, 2> {};
427 
428 template <>
430  static Use *op_begin(MemoryUseOrDef *MUD) {
431  if (auto *MU = dyn_cast<MemoryUse>(MUD))
433  return OperandTraits<MemoryDef>::op_begin(cast<MemoryDef>(MUD));
434  }
435 
436  static Use *op_end(MemoryUseOrDef *MUD) {
437  if (auto *MU = dyn_cast<MemoryUse>(MUD))
439  return OperandTraits<MemoryDef>::op_end(cast<MemoryDef>(MUD));
440  }
441 
442  static unsigned operands(const MemoryUseOrDef *MUD) {
443  if (const auto *MU = dyn_cast<MemoryUse>(MUD))
445  return OperandTraits<MemoryDef>::operands(cast<MemoryDef>(MUD));
446  }
447 };
449 
450 /// Represents phi nodes for memory accesses.
451 ///
452 /// These have the same semantic as regular phi nodes, with the exception that
453 /// only one phi will ever exist in a given basic block.
454 /// Guaranteeing one phi per block means guaranteeing there is only ever one
455 /// valid reaching MemoryDef/MemoryPHI along each path to the phi node.
456 /// This is ensured by not allowing disambiguation of the RHS of a MemoryDef or
457 /// a MemoryPhi's operands.
458 /// That is, given
459 /// if (a) {
460 /// store %a
461 /// store %b
462 /// }
463 /// it *must* be transformed into
464 /// if (a) {
465 /// 1 = MemoryDef(liveOnEntry)
466 /// store %a
467 /// 2 = MemoryDef(1)
468 /// store %b
469 /// }
470 /// and *not*
471 /// if (a) {
472 /// 1 = MemoryDef(liveOnEntry)
473 /// store %a
474 /// 2 = MemoryDef(liveOnEntry)
475 /// store %b
476 /// }
477 /// even if the two stores do not conflict. Otherwise, both 1 and 2 reach the
478 /// end of the branch, and if there are not two phi nodes, one will be
479 /// disconnected completely from the SSA graph below that point.
480 /// Because MemoryUse's do not generate new definitions, they do not have this
481 /// issue.
482 class MemoryPhi final : public MemoryAccess {
483  // allocate space for exactly zero operands
484  void *operator new(size_t s) { return User::operator new(s); }
485 
486 public:
487  /// Provide fast operand accessors
489 
490  MemoryPhi(LLVMContext &C, BasicBlock *BB, unsigned Ver, unsigned NumPreds = 0)
491  : MemoryAccess(C, MemoryPhiVal, deleteMe, BB, 0), ID(Ver),
492  ReservedSpace(NumPreds) {
493  allocHungoffUses(ReservedSpace);
494  }
495 
496  // Block iterator interface. This provides access to the list of incoming
497  // basic blocks, which parallels the list of incoming values.
500 
502  auto *Ref = reinterpret_cast<Use::UserRef *>(op_begin() + ReservedSpace);
503  return reinterpret_cast<block_iterator>(Ref + 1);
504  }
505 
507  const auto *Ref =
508  reinterpret_cast<const Use::UserRef *>(op_begin() + ReservedSpace);
509  return reinterpret_cast<const_block_iterator>(Ref + 1);
510  }
511 
512  block_iterator block_end() { return block_begin() + getNumOperands(); }
513 
515  return block_begin() + getNumOperands();
516  }
517 
519  return make_range(block_begin(), block_end());
520  }
521 
523  return make_range(block_begin(), block_end());
524  }
525 
526  op_range incoming_values() { return operands(); }
527 
528  const_op_range incoming_values() const { return operands(); }
529 
530  /// Return the number of incoming edges
531  unsigned getNumIncomingValues() const { return getNumOperands(); }
532 
533  /// Return incoming value number x
534  MemoryAccess *getIncomingValue(unsigned I) const { return getOperand(I); }
535  void setIncomingValue(unsigned I, MemoryAccess *V) {
536  assert(V && "PHI node got a null value!");
537  setOperand(I, V);
538  }
539 
540  static unsigned getOperandNumForIncomingValue(unsigned I) { return I; }
541  static unsigned getIncomingValueNumForOperand(unsigned I) { return I; }
542 
543  /// Return incoming basic block number @p i.
544  BasicBlock *getIncomingBlock(unsigned I) const { return block_begin()[I]; }
545 
546  /// Return incoming basic block corresponding
547  /// to an operand of the PHI.
548  BasicBlock *getIncomingBlock(const Use &U) const {
549  assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
550  return getIncomingBlock(unsigned(&U - op_begin()));
551  }
552 
553  /// Return incoming basic block corresponding
554  /// to value use iterator.
556  return getIncomingBlock(I.getUse());
557  }
558 
559  void setIncomingBlock(unsigned I, BasicBlock *BB) {
560  assert(BB && "PHI node got a null basic block!");
561  block_begin()[I] = BB;
562  }
563 
564  /// Add an incoming value to the end of the PHI list
566  if (getNumOperands() == ReservedSpace)
567  growOperands(); // Get more space!
568  // Initialize some new operands.
569  setNumHungOffUseOperands(getNumOperands() + 1);
570  setIncomingValue(getNumOperands() - 1, V);
571  setIncomingBlock(getNumOperands() - 1, BB);
572  }
573 
574  /// Return the first index of the specified basic
575  /// block in the value list for this PHI. Returns -1 if no instance.
576  int getBasicBlockIndex(const BasicBlock *BB) const {
577  for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
578  if (block_begin()[I] == BB)
579  return I;
580  return -1;
581  }
582 
584  int Idx = getBasicBlockIndex(BB);
585  assert(Idx >= 0 && "Invalid basic block argument!");
586  return getIncomingValue(Idx);
587  }
588 
589  // After deleting incoming position I, the order of incoming may be changed.
590  void unorderedDeleteIncoming(unsigned I) {
591  unsigned E = getNumOperands();
592  assert(I < E && "Cannot remove out of bounds Phi entry.");
593  // MemoryPhi must have at least two incoming values, otherwise the MemoryPhi
594  // itself should be deleted.
595  assert(E >= 2 && "Cannot only remove incoming values in MemoryPhis with "
596  "at least 2 values.");
597  setIncomingValue(I, getIncomingValue(E - 1));
598  setIncomingBlock(I, block_begin()[E - 1]);
599  setOperand(E - 1, nullptr);
600  block_begin()[E - 1] = nullptr;
601  setNumHungOffUseOperands(getNumOperands() - 1);
602  }
603 
604  // After deleting entries that satisfy Pred, remaining entries may have
605  // changed order.
606  template <typename Fn> void unorderedDeleteIncomingIf(Fn &&Pred) {
607  for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
608  if (Pred(getIncomingValue(I), getIncomingBlock(I))) {
609  unorderedDeleteIncoming(I);
610  E = getNumOperands();
611  --I;
612  }
613  assert(getNumOperands() >= 1 &&
614  "Cannot remove all incoming blocks in a MemoryPhi.");
615  }
616 
617  // After deleting incoming block BB, the incoming blocks order may be changed.
619  unorderedDeleteIncomingIf(
620  [&](const MemoryAccess *, const BasicBlock *B) { return BB == B; });
621  }
622 
623  // After deleting incoming memory access MA, the incoming accesses order may
624  // be changed.
626  unorderedDeleteIncomingIf(
627  [&](const MemoryAccess *M, const BasicBlock *) { return MA == M; });
628  }
629 
630  static bool classof(const Value *V) {
631  return V->getValueID() == MemoryPhiVal;
632  }
633 
634  void print(raw_ostream &OS) const;
635 
636  unsigned getID() const { return ID; }
637 
638 protected:
639  friend class MemorySSA;
640 
641  /// this is more complicated than the generic
642  /// User::allocHungoffUses, because we have to allocate Uses for the incoming
643  /// values and pointers to the incoming blocks, all in one allocation.
644  void allocHungoffUses(unsigned N) {
645  User::allocHungoffUses(N, /* IsPhi */ true);
646  }
647 
648 private:
649  // For debugging only
650  const unsigned ID;
651  unsigned ReservedSpace;
652 
653  /// This grows the operand list in response to a push_back style of
654  /// operation. This grows the number of ops by 1.5 times.
655  void growOperands() {
656  unsigned E = getNumOperands();
657  // 2 op PHI nodes are VERY common, so reserve at least enough for that.
658  ReservedSpace = std::max(E + E / 2, 2u);
659  growHungoffUses(ReservedSpace, /* IsPhi */ true);
660  }
661 
662  static void deleteMe(DerivedUser *Self);
663 };
664 
665 inline unsigned MemoryAccess::getID() const {
666  assert((isa<MemoryDef>(this) || isa<MemoryPhi>(this)) &&
667  "only memory defs and phis have ids");
668  if (const auto *MD = dyn_cast<MemoryDef>(this))
669  return MD->getID();
670  return cast<MemoryPhi>(this)->getID();
671 }
672 
673 inline bool MemoryUseOrDef::isOptimized() const {
674  if (const auto *MD = dyn_cast<MemoryDef>(this))
675  return MD->isOptimized();
676  return cast<MemoryUse>(this)->isOptimized();
677 }
678 
680  if (const auto *MD = dyn_cast<MemoryDef>(this))
681  return MD->getOptimized();
682  return cast<MemoryUse>(this)->getOptimized();
683 }
684 
686  if (auto *MD = dyn_cast<MemoryDef>(this))
687  MD->setOptimized(MA);
688  else
689  cast<MemoryUse>(this)->setOptimized(MA);
690 }
691 
693  if (auto *MD = dyn_cast<MemoryDef>(this))
694  MD->resetOptimized();
695  else
696  cast<MemoryUse>(this)->resetOptimized();
697 }
698 
699 template <> struct OperandTraits<MemoryPhi> : public HungoffOperandTraits<2> {};
701 
702 /// Encapsulates MemorySSA, including all data associated with memory
703 /// accesses.
704 class MemorySSA {
705 public:
707 
708  // MemorySSA must remain where it's constructed; Walkers it creates store
709  // pointers to it.
710  MemorySSA(MemorySSA &&) = delete;
711 
712  ~MemorySSA();
713 
714  MemorySSAWalker *getWalker();
715  MemorySSAWalker *getSkipSelfWalker();
716 
717  /// Given a memory Mod/Ref'ing instruction, get the MemorySSA
718  /// access associated with it. If passed a basic block gets the memory phi
719  /// node that exists for that block, if there is one. Otherwise, this will get
720  /// a MemoryUseOrDef.
722  return cast_or_null<MemoryUseOrDef>(ValueToMemoryAccess.lookup(I));
723  }
724 
725  MemoryPhi *getMemoryAccess(const BasicBlock *BB) const {
726  return cast_or_null<MemoryPhi>(ValueToMemoryAccess.lookup(cast<Value>(BB)));
727  }
728 
729  void dump() const;
730  void print(raw_ostream &) const;
731 
732  /// Return true if \p MA represents the live on entry value
733  ///
734  /// Loads and stores from pointer arguments and other global values may be
735  /// defined by memory operations that do not occur in the current function, so
736  /// they may be live on entry to the function. MemorySSA represents such
737  /// memory state by the live on entry definition, which is guaranteed to occur
738  /// before any other memory access in the function.
739  inline bool isLiveOnEntryDef(const MemoryAccess *MA) const {
740  return MA == LiveOnEntryDef.get();
741  }
742 
743  inline MemoryAccess *getLiveOnEntryDef() const {
744  return LiveOnEntryDef.get();
745  }
746 
747  // Sadly, iplists, by default, owns and deletes pointers added to the
748  // list. It's not currently possible to have two iplists for the same type,
749  // where one owns the pointers, and one does not. This is because the traits
750  // are per-type, not per-tag. If this ever changes, we should make the
751  // DefList an iplist.
753  using DefsList =
755 
756  /// Return the list of MemoryAccess's for a given basic block.
757  ///
758  /// This list is not modifiable by the user.
759  const AccessList *getBlockAccesses(const BasicBlock *BB) const {
760  return getWritableBlockAccesses(BB);
761  }
762 
763  /// Return the list of MemoryDef's and MemoryPhi's for a given basic
764  /// block.
765  ///
766  /// This list is not modifiable by the user.
767  const DefsList *getBlockDefs(const BasicBlock *BB) const {
768  return getWritableBlockDefs(BB);
769  }
770 
771  /// Given two memory accesses in the same basic block, determine
772  /// whether MemoryAccess \p A dominates MemoryAccess \p B.
773  bool locallyDominates(const MemoryAccess *A, const MemoryAccess *B) const;
774 
775  /// Given two memory accesses in potentially different blocks,
776  /// determine whether MemoryAccess \p A dominates MemoryAccess \p B.
777  bool dominates(const MemoryAccess *A, const MemoryAccess *B) const;
778 
779  /// Given a MemoryAccess and a Use, determine whether MemoryAccess \p A
780  /// dominates Use \p B.
781  bool dominates(const MemoryAccess *A, const Use &B) const;
782 
783  /// Verify that MemorySSA is self consistent (IE definitions dominate
784  /// all uses, uses appear in the right places). This is used by unit tests.
785  void verifyMemorySSA() const;
786 
787  /// Used in various insertion functions to specify whether we are talking
788  /// about the beginning or end of a block.
789  enum InsertionPlace { Beginning, End, BeforeTerminator };
790 
791 protected:
792  // Used by Memory SSA annotater, dumpers, and wrapper pass
795  friend class MemorySSAUpdater;
796 
797  void verifyOrderingDominationAndDefUses(Function &F) const;
798  void verifyDominationNumbers(const Function &F) const;
799  void verifyPrevDefInPhis(Function &F) const;
800 
801  // This is used by the use optimizer and updater.
803  auto It = PerBlockAccesses.find(BB);
804  return It == PerBlockAccesses.end() ? nullptr : It->second.get();
805  }
806 
807  // This is used by the use optimizer and updater.
809  auto It = PerBlockDefs.find(BB);
810  return It == PerBlockDefs.end() ? nullptr : It->second.get();
811  }
812 
813  // These is used by the updater to perform various internal MemorySSA
814  // machinsations. They do not always leave the IR in a correct state, and
815  // relies on the updater to fixup what it breaks, so it is not public.
816 
817  void moveTo(MemoryUseOrDef *What, BasicBlock *BB, AccessList::iterator Where);
818  void moveTo(MemoryAccess *What, BasicBlock *BB, InsertionPlace Point);
819 
820  // Rename the dominator tree branch rooted at BB.
821  void renamePass(BasicBlock *BB, MemoryAccess *IncomingVal,
823  renamePass(DT->getNode(BB), IncomingVal, Visited, true, true);
824  }
825 
826  void removeFromLookups(MemoryAccess *);
827  void removeFromLists(MemoryAccess *, bool ShouldDelete = true);
828  void insertIntoListsForBlock(MemoryAccess *, const BasicBlock *,
830  void insertIntoListsBefore(MemoryAccess *, const BasicBlock *,
831  AccessList::iterator);
832  MemoryUseOrDef *createDefinedAccess(Instruction *, MemoryAccess *,
833  const MemoryUseOrDef *Template = nullptr,
834  bool CreationMustSucceed = true);
835 
836 private:
837  template <class AliasAnalysisType> class ClobberWalkerBase;
838  template <class AliasAnalysisType> class CachingWalker;
839  template <class AliasAnalysisType> class SkipSelfWalker;
840  class OptimizeUses;
841 
842  CachingWalker<AliasAnalysis> *getWalkerImpl();
843  void buildMemorySSA(BatchAAResults &BAA);
844  void optimizeUses();
845 
846  void prepareForMoveTo(MemoryAccess *, BasicBlock *);
847  void verifyUseInDefs(MemoryAccess *, MemoryAccess *) const;
848 
851 
852  void
853  determineInsertionPoint(const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks);
854  void markUnreachableAsLiveOnEntry(BasicBlock *BB);
855  bool dominatesUse(const MemoryAccess *, const MemoryAccess *) const;
856  MemoryPhi *createMemoryPhi(BasicBlock *BB);
857  template <typename AliasAnalysisType>
858  MemoryUseOrDef *createNewAccess(Instruction *, AliasAnalysisType *,
859  const MemoryUseOrDef *Template = nullptr);
860  MemoryAccess *findDominatingDef(BasicBlock *, enum InsertionPlace);
861  void placePHINodes(const SmallPtrSetImpl<BasicBlock *> &);
862  MemoryAccess *renameBlock(BasicBlock *, MemoryAccess *, bool);
863  void renameSuccessorPhis(BasicBlock *, MemoryAccess *, bool);
864  void renamePass(DomTreeNode *, MemoryAccess *IncomingVal,
866  bool SkipVisited = false, bool RenameAllUses = false);
867  AccessList *getOrCreateAccessList(const BasicBlock *);
868  DefsList *getOrCreateDefsList(const BasicBlock *);
869  void renumberBlock(const BasicBlock *) const;
870  AliasAnalysis *AA;
871  DominatorTree *DT;
872  Function &F;
873 
874  // Memory SSA mappings
875  DenseMap<const Value *, MemoryAccess *> ValueToMemoryAccess;
876 
877  // These two mappings contain the main block to access/def mappings for
878  // MemorySSA. The list contained in PerBlockAccesses really owns all the
879  // MemoryAccesses.
880  // Both maps maintain the invariant that if a block is found in them, the
881  // corresponding list is not empty, and if a block is not found in them, the
882  // corresponding list is empty.
883  AccessMap PerBlockAccesses;
884  DefsMap PerBlockDefs;
885  std::unique_ptr<MemoryAccess, ValueDeleter> LiveOnEntryDef;
886 
887  // Domination mappings
888  // Note that the numbering is local to a block, even though the map is
889  // global.
890  mutable SmallPtrSet<const BasicBlock *, 16> BlockNumberingValid;
891  mutable DenseMap<const MemoryAccess *, unsigned long> BlockNumbering;
892 
893  // Memory SSA building info
894  std::unique_ptr<ClobberWalkerBase<AliasAnalysis>> WalkerBase;
895  std::unique_ptr<CachingWalker<AliasAnalysis>> Walker;
896  std::unique_ptr<SkipSelfWalker<AliasAnalysis>> SkipWalker;
897  unsigned NextID;
898 };
899 
900 // Internal MemorySSA utils, for use by MemorySSA classes and walkers
902 protected:
903  friend class GVNHoist;
904  friend class MemorySSAWalker;
905 
906  // This function should not be used by new passes.
907  static bool defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU,
908  AliasAnalysis &AA);
909 };
910 
911 // This pass does eager building and then printing of MemorySSA. It is used by
912 // the tests to be able to build, dump, and verify Memory SSA.
914 public:
916 
917  bool runOnFunction(Function &) override;
918  void getAnalysisUsage(AnalysisUsage &AU) const override;
919 
920  static char ID;
921 };
922 
923 /// An analysis that produces \c MemorySSA for a function.
924 ///
925 class MemorySSAAnalysis : public AnalysisInfoMixin<MemorySSAAnalysis> {
927 
928  static AnalysisKey Key;
929 
930 public:
931  // Wrap MemorySSA result to ensure address stability of internal MemorySSA
932  // pointers after construction. Use a wrapper class instead of plain
933  // unique_ptr<MemorySSA> to avoid build breakage on MSVC.
934  struct Result {
935  Result(std::unique_ptr<MemorySSA> &&MSSA) : MSSA(std::move(MSSA)) {}
936 
937  MemorySSA &getMSSA() { return *MSSA.get(); }
938 
939  std::unique_ptr<MemorySSA> MSSA;
940 
941  bool invalidate(Function &F, const PreservedAnalyses &PA,
943  };
944 
946 };
947 
948 /// Printer pass for \c MemorySSA.
949 class MemorySSAPrinterPass : public PassInfoMixin<MemorySSAPrinterPass> {
950  raw_ostream &OS;
951 
952 public:
953  explicit MemorySSAPrinterPass(raw_ostream &OS) : OS(OS) {}
954 
956 };
957 
958 /// Verifier pass for \c MemorySSA.
959 struct MemorySSAVerifierPass : PassInfoMixin<MemorySSAVerifierPass> {
961 };
962 
963 /// Legacy analysis pass which computes \c MemorySSA.
965 public:
967 
968  static char ID;
969 
970  bool runOnFunction(Function &) override;
971  void releaseMemory() override;
972  MemorySSA &getMSSA() { return *MSSA; }
973  const MemorySSA &getMSSA() const { return *MSSA; }
974 
975  void getAnalysisUsage(AnalysisUsage &AU) const override;
976 
977  void verifyAnalysis() const override;
978  void print(raw_ostream &OS, const Module *M = nullptr) const override;
979 
980 private:
981  std::unique_ptr<MemorySSA> MSSA;
982 };
983 
984 /// This is the generic walker interface for walkers of MemorySSA.
985 /// Walkers are used to be able to further disambiguate the def-use chains
986 /// MemorySSA gives you, or otherwise produce better info than MemorySSA gives
987 /// you.
988 /// In particular, while the def-use chains provide basic information, and are
989 /// guaranteed to give, for example, the nearest may-aliasing MemoryDef for a
990 /// MemoryUse as AliasAnalysis considers it, a user mant want better or other
991 /// information. In particular, they may want to use SCEV info to further
992 /// disambiguate memory accesses, or they may want the nearest dominating
993 /// may-aliasing MemoryDef for a call or a store. This API enables a
994 /// standardized interface to getting and using that info.
996 public:
998  virtual ~MemorySSAWalker() = default;
999 
1001 
1002  /// Given a memory Mod/Ref/ModRef'ing instruction, calling this
1003  /// will give you the nearest dominating MemoryAccess that Mod's the location
1004  /// the instruction accesses (by skipping any def which AA can prove does not
1005  /// alias the location(s) accessed by the instruction given).
1006  ///
1007  /// Note that this will return a single access, and it must dominate the
1008  /// Instruction, so if an operand of a MemoryPhi node Mod's the instruction,
1009  /// this will return the MemoryPhi, not the operand. This means that
1010  /// given:
1011  /// if (a) {
1012  /// 1 = MemoryDef(liveOnEntry)
1013  /// store %a
1014  /// } else {
1015  /// 2 = MemoryDef(liveOnEntry)
1016  /// store %b
1017  /// }
1018  /// 3 = MemoryPhi(2, 1)
1019  /// MemoryUse(3)
1020  /// load %a
1021  ///
1022  /// calling this API on load(%a) will return the MemoryPhi, not the MemoryDef
1023  /// in the if (a) branch.
1025  MemoryAccess *MA = MSSA->getMemoryAccess(I);
1026  assert(MA && "Handed an instruction that MemorySSA doesn't recognize?");
1027  return getClobberingMemoryAccess(MA);
1028  }
1029 
1030  /// Does the same thing as getClobberingMemoryAccess(const Instruction *I),
1031  /// but takes a MemoryAccess instead of an Instruction.
1032  virtual MemoryAccess *getClobberingMemoryAccess(MemoryAccess *) = 0;
1033 
1034  /// Given a potentially clobbering memory access and a new location,
1035  /// calling this will give you the nearest dominating clobbering MemoryAccess
1036  /// (by skipping non-aliasing def links).
1037  ///
1038  /// This version of the function is mainly used to disambiguate phi translated
1039  /// pointers, where the value of a pointer may have changed from the initial
1040  /// memory access. Note that this expects to be handed either a MemoryUse,
1041  /// or an already potentially clobbering access. Unlike the above API, if
1042  /// given a MemoryDef that clobbers the pointer as the starting access, it
1043  /// will return that MemoryDef, whereas the above would return the clobber
1044  /// starting from the use side of the memory def.
1045  virtual MemoryAccess *getClobberingMemoryAccess(MemoryAccess *,
1046  const MemoryLocation &) = 0;
1047 
1048  /// Given a memory access, invalidate anything this walker knows about
1049  /// that access.
1050  /// This API is used by walkers that store information to perform basic cache
1051  /// invalidation. This will be called by MemorySSA at appropriate times for
1052  /// the walker it uses or returns.
1053  virtual void invalidateInfo(MemoryAccess *) {}
1054 
1055 protected:
1056  friend class MemorySSA; // For updating MSSA pointer in MemorySSA move
1057  // constructor.
1059 };
1060 
1061 /// A MemorySSAWalker that does no alias queries, or anything else. It
1062 /// simply returns the links as they were constructed by the builder.
1064 public:
1065  // Keep the overrides below from hiding the Instruction overload of
1066  // getClobberingMemoryAccess.
1068 
1069  MemoryAccess *getClobberingMemoryAccess(MemoryAccess *) override;
1070  MemoryAccess *getClobberingMemoryAccess(MemoryAccess *,
1071  const MemoryLocation &) override;
1072 };
1073 
1074 using MemoryAccessPair = std::pair<MemoryAccess *, MemoryLocation>;
1075 using ConstMemoryAccessPair = std::pair<const MemoryAccess *, MemoryLocation>;
1076 
1077 /// Iterator base class used to implement const and non-const iterators
1078 /// over the defining accesses of a MemoryAccess.
1079 template <class T>
1081  : public iterator_facade_base<memoryaccess_def_iterator_base<T>,
1082  std::forward_iterator_tag, T, ptrdiff_t, T *,
1083  T *> {
1084  using BaseT = typename memoryaccess_def_iterator_base::iterator_facade_base;
1085 
1086 public:
1087  memoryaccess_def_iterator_base(T *Start) : Access(Start) {}
1088  memoryaccess_def_iterator_base() = default;
1089 
1090  bool operator==(const memoryaccess_def_iterator_base &Other) const {
1091  return Access == Other.Access && (!Access || ArgNo == Other.ArgNo);
1092  }
1093 
1094  // This is a bit ugly, but for MemoryPHI's, unlike PHINodes, you can't get the
1095  // block from the operand in constant time (In a PHINode, the uselist has
1096  // both, so it's just subtraction). We provide it as part of the
1097  // iterator to avoid callers having to linear walk to get the block.
1098  // If the operation becomes constant time on MemoryPHI's, this bit of
1099  // abstraction breaking should be removed.
1101  MemoryPhi *MP = dyn_cast<MemoryPhi>(Access);
1102  assert(MP && "Tried to get phi arg block when not iterating over a PHI");
1103  return MP->getIncomingBlock(ArgNo);
1104  }
1105 
1106  typename BaseT::iterator::pointer operator*() const {
1107  assert(Access && "Tried to access past the end of our iterator");
1108  // Go to the first argument for phis, and the defining access for everything
1109  // else.
1110  if (const MemoryPhi *MP = dyn_cast<MemoryPhi>(Access))
1111  return MP->getIncomingValue(ArgNo);
1112  return cast<MemoryUseOrDef>(Access)->getDefiningAccess();
1113  }
1114 
1115  using BaseT::operator++;
1117  assert(Access && "Hit end of iterator");
1118  if (const MemoryPhi *MP = dyn_cast<MemoryPhi>(Access)) {
1119  if (++ArgNo >= MP->getNumIncomingValues()) {
1120  ArgNo = 0;
1121  Access = nullptr;
1122  }
1123  } else {
1124  Access = nullptr;
1125  }
1126  return *this;
1127  }
1128 
1129 private:
1130  T *Access = nullptr;
1131  unsigned ArgNo = 0;
1132 };
1133 
1135  return memoryaccess_def_iterator(this);
1136 }
1137 
1139  return const_memoryaccess_def_iterator(this);
1140 }
1141 
1143  return memoryaccess_def_iterator();
1144 }
1145 
1148 }
1149 
1150 /// GraphTraits for a MemoryAccess, which walks defs in the normal case,
1151 /// and uses in the inverse case.
1152 template <> struct GraphTraits<MemoryAccess *> {
1155 
1156  static NodeRef getEntryNode(NodeRef N) { return N; }
1158  static ChildIteratorType child_end(NodeRef N) { return N->defs_end(); }
1159 };
1160 
1161 template <> struct GraphTraits<Inverse<MemoryAccess *>> {
1164 
1165  static NodeRef getEntryNode(NodeRef N) { return N; }
1167  static ChildIteratorType child_end(NodeRef N) { return N->user_end(); }
1168 };
1169 
1170 /// Provide an iterator that walks defs, giving both the memory access,
1171 /// and the current pointer location, updating the pointer location as it
1172 /// changes due to phi node translation.
1173 ///
1174 /// This iterator, while somewhat specialized, is what most clients actually
1175 /// want when walking upwards through MemorySSA def chains. It takes a pair of
1176 /// <MemoryAccess,MemoryLocation>, and walks defs, properly translating the
1177 /// memory location through phi nodes for the user.
1179  : public iterator_facade_base<upward_defs_iterator,
1180  std::forward_iterator_tag,
1181  const MemoryAccessPair> {
1182  using BaseT = upward_defs_iterator::iterator_facade_base;
1183 
1184 public:
1186  : DefIterator(Info.first), Location(Info.second),
1187  OriginalAccess(Info.first) {
1188  CurrentPair.first = nullptr;
1189 
1190  WalkingPhi = Info.first && isa<MemoryPhi>(Info.first);
1191  fillInCurrentPair();
1192  }
1193 
1194  upward_defs_iterator() { CurrentPair.first = nullptr; }
1195 
1196  bool operator==(const upward_defs_iterator &Other) const {
1197  return DefIterator == Other.DefIterator;
1198  }
1199 
1200  BaseT::iterator::reference operator*() const {
1201  assert(DefIterator != OriginalAccess->defs_end() &&
1202  "Tried to access past the end of our iterator");
1203  return CurrentPair;
1204  }
1205 
1206  using BaseT::operator++;
1208  assert(DefIterator != OriginalAccess->defs_end() &&
1209  "Tried to access past the end of the iterator");
1210  ++DefIterator;
1211  if (DefIterator != OriginalAccess->defs_end())
1212  fillInCurrentPair();
1213  return *this;
1214  }
1215 
1216  BasicBlock *getPhiArgBlock() const { return DefIterator.getPhiArgBlock(); }
1217 
1218 private:
1219  void fillInCurrentPair() {
1220  CurrentPair.first = *DefIterator;
1221  if (WalkingPhi && Location.Ptr) {
1222  PHITransAddr Translator(
1223  const_cast<Value *>(Location.Ptr),
1224  OriginalAccess->getBlock()->getModule()->getDataLayout(), nullptr);
1225  if (!Translator.PHITranslateValue(OriginalAccess->getBlock(),
1226  DefIterator.getPhiArgBlock(), nullptr,
1227  false))
1228  if (Translator.getAddr() != Location.Ptr) {
1229  CurrentPair.second = Location.getWithNewPtr(Translator.getAddr());
1230  return;
1231  }
1232  }
1233  CurrentPair.second = Location;
1234  }
1235 
1236  MemoryAccessPair CurrentPair;
1237  memoryaccess_def_iterator DefIterator;
1238  MemoryLocation Location;
1239  MemoryAccess *OriginalAccess = nullptr;
1240  bool WalkingPhi = false;
1241 };
1242 
1244  return upward_defs_iterator(Pair);
1245 }
1246 
1248 
1251  return make_range(upward_defs_begin(Pair), upward_defs_end());
1252 }
1253 
1254 /// Walks the defining accesses of MemoryDefs. Stops after we hit something that
1255 /// has no defining use (e.g. a MemoryPhi or liveOnEntry). Note that, when
1256 /// comparing against a null def_chain_iterator, this will compare equal only
1257 /// after walking said Phi/liveOnEntry.
1258 ///
1259 /// The UseOptimizedChain flag specifies whether to walk the clobbering
1260 /// access chain, or all the accesses.
1261 ///
1262 /// Normally, MemoryDef are all just def/use linked together, so a def_chain on
1263 /// a MemoryDef will walk all MemoryDefs above it in the program until it hits
1264 /// a phi node. The optimized chain walks the clobbering access of a store.
1265 /// So if you are just trying to find, given a store, what the next
1266 /// thing that would clobber the same memory is, you want the optimized chain.
1267 template <class T, bool UseOptimizedChain = false>
1269  : public iterator_facade_base<def_chain_iterator<T, UseOptimizedChain>,
1270  std::forward_iterator_tag, MemoryAccess *> {
1271  def_chain_iterator() : MA(nullptr) {}
1272  def_chain_iterator(T MA) : MA(MA) {}
1273 
1274  T operator*() const { return MA; }
1275 
1277  // N.B. liveOnEntry has a null defining access.
1278  if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA)) {
1279  if (UseOptimizedChain && MUD->isOptimized())
1280  MA = MUD->getOptimized();
1281  else
1282  MA = MUD->getDefiningAccess();
1283  } else {
1284  MA = nullptr;
1285  }
1286 
1287  return *this;
1288  }
1289 
1290  bool operator==(const def_chain_iterator &O) const { return MA == O.MA; }
1291 
1292 private:
1293  T MA;
1294 };
1295 
1296 template <class T>
1298 def_chain(T MA, MemoryAccess *UpTo = nullptr) {
1299 #ifdef EXPENSIVE_CHECKS
1300  assert((!UpTo || find(def_chain(MA), UpTo) != def_chain_iterator<T>()) &&
1301  "UpTo isn't in the def chain!");
1302 #endif
1304 }
1305 
1306 template <class T>
1309  def_chain_iterator<T, true>(nullptr));
1310 }
1311 
1312 } // end namespace llvm
1313 
1314 #endif // LLVM_ANALYSIS_MEMORYSSA_H
uint64_t CallInst * C
AccessList * getWritableBlockAccesses(const BasicBlock *BB) const
Definition: MemorySSA.h:802
BasicBlock * getIncomingBlock(MemoryAccess::const_user_iterator I) const
Return incoming basic block corresponding to value use iterator.
Definition: MemorySSA.h:555
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:484
void unorderedDeleteIncomingValue(const MemoryAccess *MA)
Definition: MemorySSA.h:625
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void unorderedDeleteIncomingIf(Fn &&Pred)
Definition: MemorySSA.h:606
Result(std::unique_ptr< MemorySSA > &&MSSA)
Definition: MemorySSA.h:935
AllAccessType::self_iterator getIterator()
Get the iterators for the all access list and the defs only list We default to the all access list...
Definition: MemorySSA.h:179
BasicBlock * getIncomingBlock(const Use &U) const
Return incoming basic block corresponding to an operand of the PHI.
Definition: MemorySSA.h:548
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
Definition: MemorySSA.h:258
MemorySSAPrinterPass(raw_ostream &OS)
Definition: MemorySSA.h:953
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
void resetOptimized()
Definition: MemorySSA.h:350
const MemorySSA & getMSSA() const
Definition: MemorySSA.h:973
const AccessList * getBlockAccesses(const BasicBlock *BB) const
Return the list of MemoryAccess&#39;s for a given basic block.
Definition: MemorySSA.h:759
BasicBlock *const * const_block_iterator
Definition: MemorySSA.h:499
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
MemoryAccess * getOptimized() const
Definition: MemorySSA.h:400
Extension point for the Value hierarchy.
Definition: DerivedUser.h:27
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
Definition: MemorySSA.h:376
BaseT::iterator::reference operator*() const
Definition: MemorySSA.h:1200
void setOptimized(MemoryAccess *)
Definition: MemorySSA.h:685
void deleteValue()
Delete a pointer to a generic Value.
Definition: Value.cpp:99
BasicBlock * getPhiArgBlock() const
Definition: MemorySSA.h:1100
memoryaccess_def_iterator defs_begin()
This iterator walks over all of the defs in a given MemoryAccess.
Definition: MemorySSA.h:1134
unsigned second
F(f)
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
Definition: MemorySSA.h:576
void(*)(DerivedUser *) DeleteValueTy
Definition: DerivedUser.h:29
This defines the Use class.
AllAccessType::const_self_iterator getIterator() const
Definition: MemorySSA.h:182
DefsOnlyType::self_iterator getDefsIterator()
Definition: MemorySSA.h:191
void setIncomingValue(unsigned I, MemoryAccess *V)
Definition: MemorySSA.h:535
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:343
MemoryPhi * getMemoryAccess(const BasicBlock *BB) const
Definition: MemorySSA.h:725
Represents read-only accesses to memory.
Definition: MemorySSA.h:320
This class is a batch walker of all MemoryUse&#39;s in the program, and points their defining access at t...
Definition: MemorySSA.cpp:1275
Legacy analysis pass which computes MemorySSA.
Definition: MemorySSA.h:964
Definition: BitVector.h:937
void resetOptimized()
Reset the ID of what this MemoryUse was optimized to, causing it to be rewalked by the walker if nece...
Definition: MemorySSA.h:692
void renamePass(BasicBlock *BB, MemoryAccess *IncomingVal, SmallPtrSetImpl< BasicBlock *> &Visited)
Definition: MemorySSA.h:821
block_iterator block_begin()
Definition: MemorySSA.h:501
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:704
The access may reference the value stored in memory.
const DefsList * getBlockDefs(const BasicBlock *BB) const
Return the list of MemoryDef&#39;s and MemoryPhi&#39;s for a given basic block.
Definition: MemorySSA.h:767
const_block_iterator block_end() const
Definition: MemorySSA.h:514
bool isOptimized() const
Definition: MemorySSA.h:404
Walks the defining accesses of MemoryDefs.
Definition: MemorySSA.h:1268
def_chain_iterator & operator++()
Definition: MemorySSA.h:1276
bool isOptimized() const
Definition: MemorySSA.h:673
BaseT::iterator::pointer operator*() const
Definition: MemorySSA.h:1106
static bool classof(const Value *MA)
Definition: MemorySSA.h:391
iterator_range< block_iterator > blocks()
Definition: MemorySSA.h:518
A simple intrusive list implementation.
Definition: simple_ilist.h:78
Key
PAL metadata keys.
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Definition: Use.cpp:40
static ChildIteratorType child_end(NodeRef N)
Definition: MemorySSA.h:1167
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref&#39;ing instruction, get the MemorySSA access associated with it.
Definition: MemorySSA.h:721
MemoryAccess * getOptimized() const
Definition: MemorySSA.h:346
AllAccessType::const_reverse_self_iterator getReverseIterator() const
Definition: MemorySSA.h:188
upward_defs_iterator upward_defs_end()
Definition: MemorySSA.h:1247
std::unique_ptr< MemorySSA > MSSA
Definition: MemorySSA.h:939
void unorderedDeleteIncoming(unsigned I)
Definition: MemorySSA.h:590
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:373
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
This is the generic walker interface for walkers of MemorySSA.
Definition: MemorySSA.h:995
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:67
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
op_range incoming_values()
Definition: MemorySSA.h:526
memoryaccess_def_iterator defs_end()
Definition: MemorySSA.h:1142
Analysis containing CSE Info
Definition: CSEInfo.cpp:25
unsigned getID() const
Definition: MemorySSA.h:636
MemoryAccess * getIncomingValue(unsigned I) const
Return incoming value number x.
Definition: MemorySSA.h:534
const_block_iterator block_begin() const
Definition: MemorySSA.h:506
An assembly annotator class to print Memory SSA information in comments.
Definition: MemorySSA.cpp:99
Use delete by default for iplist and ilist.
Definition: ilist.h:40
static bool runOnFunction(Function &F, bool PostInlining)
static Use * op_end(MemoryUseOrDef *MUD)
Definition: MemorySSA.h:436
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
DefsOnlyType::const_reverse_self_iterator getReverseDefsIterator() const
Definition: MemorySSA.h:200
AllAccessType::reverse_self_iterator getReverseIterator()
Definition: MemorySSA.h:185
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
PointerIntPair - This class implements a pair of a pointer and small integer.
PHITransAddr - An address value which tracks and handles phi translation.
Definition: PHITransAddr.h:35
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
const_op_range incoming_values() const
Definition: MemorySSA.h:528
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
upward_defs_iterator & operator++()
Definition: MemorySSA.h:1207
void addIncoming(MemoryAccess *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Definition: MemorySSA.h:565
early cse Early CSE w MemorySSA
Definition: EarlyCSE.cpp:1463
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:390
static unsigned getIncomingValueNumForOperand(unsigned I)
Definition: MemorySSA.h:541
void setIncomingBlock(unsigned I, BasicBlock *BB)
Definition: MemorySSA.h:559
upward_defs_iterator(const MemoryAccessPair &Info)
Definition: MemorySSA.h:1185
InsertionPlace
Used in various insertion functions to specify whether we are talking about the beginning or end of a...
Definition: MemorySSA.h:789
Represent the analysis usage information of a pass.
iterator_range< def_chain_iterator< T, true > > optimized_def_chain(T MA)
Definition: MemorySSA.h:1307
void print(raw_ostream &OS) const
Definition: MemorySSA.cpp:2149
Printer pass for MemorySSA.
Definition: MemorySSA.h:949
static unsigned getOperandNumForIncomingValue(unsigned I)
Definition: MemorySSA.h:540
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
static bool classof(const Value *MA)
Definition: MemorySSA.h:331
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static unsigned operands(const MemoryUseOrDef *MUD)
Definition: MemorySSA.h:442
BasicBlock * getPhiArgBlock() const
Definition: MemorySSA.h:1216
virtual void invalidateInfo(MemoryAccess *)
Given a memory access, invalidate anything this walker knows about that access.
Definition: MemorySSA.h:1053
Iterator base class used to implement const and non-const iterators over the defining accesses of a M...
Definition: MemorySSA.h:131
#define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS)
Macro for generating in-class operand accessor declarations.
Provide an iterator that walks defs, giving both the memory access, and the current pointer location...
Definition: MemorySSA.h:1178
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1203
An intrusive list with ownership and callbacks specified/controlled by ilist_traits, only with API safe for polymorphic types.
Definition: ilist.h:388
MemoryAccess * getOptimized() const
Definition: MemorySSA.h:679
A MemorySSAWalker that does no alias queries, or anything else.
Definition: MemorySSA.h:1063
std::pair< const MemoryAccess *, MemoryLocation > ConstMemoryAccessPair
Definition: MemorySSA.h:1075
static void deleteNode(MemoryAccess *MA)
Definition: MemorySSA.h:233
unsigned first
bool operator==(const memoryaccess_def_iterator_base &Other) const
Definition: MemorySSA.h:1090
bool isOptimized() const
Definition: MemorySSA.h:342
The two locations may or may not alias. This is the least precise result.
Definition: AliasAnalysis.h:86
Representation for a specific memory location.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void setDefiningAccess(MemoryAccess *DMA, bool Optimized=false, Optional< AliasResult > AR=MayAlias)
Definition: MemorySSA.h:300
Iterator for intrusive lists based on ilist_node.
static bool classof(const Value *V)
Definition: MemorySSA.h:630
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:443
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Module.h This file contains the declarations for the Module class.
memoryaccess_def_iterator_base & operator++()
Definition: MemorySSA.h:1116
An analysis that produces MemorySSA for a function.
Definition: MemorySSA.h:925
BasicBlock * getBlock() const
Definition: MemorySSA.h:160
void unorderedDeleteIncomingBlock(const BasicBlock *BB)
Definition: MemorySSA.h:618
MemoryUseOrDef(LLVMContext &C, MemoryAccess *DMA, unsigned Vty, DeleteValueTy DeleteValue, Instruction *MI, BasicBlock *BB, unsigned NumOperands)
Definition: MemorySSA.h:285
MemoryAccess * getLiveOnEntryDef() const
Definition: MemorySSA.h:743
Verifier pass for MemorySSA.
Definition: MemorySSA.h:959
A range adaptor for a pair of iterators.
MemoryPhi(LLVMContext &C, BasicBlock *BB, unsigned Ver, unsigned NumPreds=0)
Definition: MemorySSA.h:490
static Use * op_begin(MemoryUseOrDef *MUD)
Definition: MemorySSA.h:430
Class that has the common methods + fields of memory uses/defs.
Definition: MemorySSA.h:248
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Definition: MemorySSA.h:531
BasicBlock * getIncomingBlock(unsigned I) const
Return incoming basic block number i.
Definition: MemorySSA.h:544
Instruction * getMemoryInst() const
Get the instruction that this MemoryUse represents.
Definition: MemorySSA.h:255
iterator_range< const_block_iterator > blocks() const
Definition: MemorySSA.h:522
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:390
DefsOnlyType::const_self_iterator getDefsIterator() const
Definition: MemorySSA.h:194
void setOptimizedAccessType(Optional< AliasResult > AR)
Definition: MemorySSA.h:296
iterator_range< def_chain_iterator< T > > def_chain(T MA, MemoryAccess *UpTo=nullptr)
Definition: MemorySSA.h:1298
memoryaccess_def_iterator_base< MemoryAccess > memoryaccess_def_iterator
Definition: MemorySSA.h:132
cl::opt< bool > EnableMSSALoopDependency
Enables memory ssa as a dependency for loop passes.
block_iterator block_end()
Definition: MemorySSA.h:512
bool operator==(const def_chain_iterator &O) const
Definition: MemorySSA.h:1290
This file provides utility analysis objects describing memory locations.
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
user_iterator_impl< User > user_iterator
Definition: Value.h:389
MemoryAccess * getIncomingValueForBlock(const BasicBlock *BB) const
Definition: MemorySSA.h:583
Compile-time customization of User operands.
Definition: User.h:42
MemoryAccess(LLVMContext &C, unsigned Vty, DeleteValueTy DeleteValue, BasicBlock *BB, unsigned NumOperands)
Definition: MemorySSA.h:219
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:332
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
Definition: MemorySSA.h:739
Optional< AliasResult > getOptimizedAccessType() const
Definition: MemorySSA.h:272
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2096
static ChildIteratorType child_begin(NodeRef N)
Definition: MemorySSA.h:1166
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:649
MemoryAccess * getClobberingMemoryAccess(const Instruction *I)
Given a memory Mod/Ref/ModRef&#39;ing instruction, calling this will give you the nearest dominating Memo...
Definition: MemorySSA.h:1024
static ChildIteratorType child_begin(NodeRef N)
Definition: MemorySSA.h:1157
static bool classof(const Value *MA)
Definition: MemorySSA.h:260
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
Definition: Value.h:396
void setOptimized(MemoryAccess *MA)
Definition: MemorySSA.h:395
static NodeRef getEntryNode(NodeRef N)
Definition: MemorySSA.h:1156
LLVM Value Representation.
Definition: Value.h:74
MemoryDef(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB, unsigned Ver)
Definition: MemorySSA.h:382
DefsOnlyType::reverse_self_iterator getReverseDefsIterator()
Definition: MemorySSA.h:197
upward_defs_iterator upward_defs_begin(const MemoryAccessPair &Pair)
Definition: MemorySSA.h:1243
HungoffOperandTraits - determine the allocation regime of the Use array when it is not a prefix to th...
Definition: OperandTraits.h:95
void allocHungoffUses(unsigned N, bool IsPhi=false)
Allocate the array of Uses, followed by a pointer (with bottom bit set) to the User.
Definition: User.cpp:39
unsigned getID() const
Used for debugging and tracking things about MemoryAccesses.
Definition: MemorySSA.h:665
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
const_user_iterator const_iterator
Definition: MemorySSA.h:167
IRTranslator LLVM IR MI
FixedNumOperandTraits - determine the allocation regime of the Use array when it is a prefix to the U...
Definition: OperandTraits.h:30
A container for analyses that lazily runs them and caches their results.
void setBlock(BasicBlock *BB)
Used by MemorySSA to change the block of a MemoryAccess when it is moved.
Definition: MemorySSA.h:213
MemoryUse(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB)
Definition: MemorySSA.h:324
static bool classof(const Value *V)
Definition: MemorySSA.h:155
iterator_range< upward_defs_iterator > upward_defs(const MemoryAccessPair &Pair)
Definition: MemorySSA.h:1250
Represents phi nodes for memory accesses.
Definition: MemorySSA.h:482
unsigned getID() const
Definition: MemorySSA.h:415
static ChildIteratorType child_end(NodeRef N)
Definition: MemorySSA.h:1158
DefsList * getWritableBlockDefs(const BasicBlock *BB) const
Definition: MemorySSA.h:808
user_iterator iterator
The user iterators for a memory access.
Definition: MemorySSA.h:166
memoryaccess_def_iterator_base< const MemoryAccess > const_memoryaccess_def_iterator
Definition: MemorySSA.h:134
bool operator==(const upward_defs_iterator &Other) const
Definition: MemorySSA.h:1196
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71
void resetOptimized()
Definition: MemorySSA.h:408
void setOptimized(MemoryAccess *DMA)
Definition: MemorySSA.h:337
std::pair< MemoryAccess *, MemoryLocation > MemoryAccessPair
Definition: MemorySSA.h:1074
void allocHungoffUses(unsigned N)
this is more complicated than the generic User::allocHungoffUses, because we have to allocate Uses fo...
Definition: MemorySSA.h:644
user_iterator user_end()
Definition: Value.h:404