LLVM  4.0.0
MemorySSA.h
Go to the documentation of this file.
1 //===- MemorySSA.h - Build Memory SSA ---------------------------*- C++ -*-===//
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 // \file
11 // \brief This file exposes an interface to building/using memory SSA to
12 // walk memory instructions using a use/def graph.
13 //
14 // Memory SSA class builds an SSA form that links together memory access
15 // instructions such as loads, stores, atomics, and calls. Additionally, it does
16 // a trivial form of "heap versioning" Every time the memory state changes in
17 // the program, we generate a new heap version. It generates MemoryDef/Uses/Phis
18 // that are overlayed on top of the existing instructions.
19 //
20 // As a trivial example,
21 // define i32 @main() #0 {
22 // entry:
23 // %call = call noalias i8* @_Znwm(i64 4) #2
24 // %0 = bitcast i8* %call to i32*
25 // %call1 = call noalias i8* @_Znwm(i64 4) #2
26 // %1 = bitcast i8* %call1 to i32*
27 // store i32 5, i32* %0, align 4
28 // store i32 7, i32* %1, align 4
29 // %2 = load i32* %0, align 4
30 // %3 = load i32* %1, align 4
31 // %add = add nsw i32 %2, %3
32 // ret i32 %add
33 // }
34 //
35 // Will become
36 // define i32 @main() #0 {
37 // entry:
38 // ; 1 = MemoryDef(0)
39 // %call = call noalias i8* @_Znwm(i64 4) #3
40 // %2 = bitcast i8* %call to i32*
41 // ; 2 = MemoryDef(1)
42 // %call1 = call noalias i8* @_Znwm(i64 4) #3
43 // %4 = bitcast i8* %call1 to i32*
44 // ; 3 = MemoryDef(2)
45 // store i32 5, i32* %2, align 4
46 // ; 4 = MemoryDef(3)
47 // store i32 7, i32* %4, align 4
48 // ; MemoryUse(3)
49 // %7 = load i32* %2, align 4
50 // ; MemoryUse(4)
51 // %8 = load i32* %4, align 4
52 // %add = add nsw i32 %7, %8
53 // ret i32 %add
54 // }
55 //
56 // Given this form, all the stores that could ever effect the load at %8 can be
57 // gotten by using the MemoryUse associated with it, and walking from use to def
58 // until you hit the top of the function.
59 //
60 // Each def also has a list of users associated with it, so you can walk from
61 // both def to users, and users to defs. Note that we disambiguate MemoryUses,
62 // but not the RHS of MemoryDefs. You can see this above at %7, which would
63 // otherwise be a MemoryUse(4). Being disambiguated means that for a given
64 // store, all the MemoryUses on its use lists are may-aliases of that store (but
65 // the MemoryDefs on its use list may not be).
66 //
67 // MemoryDefs are not disambiguated because it would require multiple reaching
68 // definitions, which would require multiple phis, and multiple memoryaccesses
69 // per instruction.
70 //===----------------------------------------------------------------------===//
71 
72 #ifndef LLVM_TRANSFORMS_UTILS_MEMORYSSA_H
73 #define LLVM_TRANSFORMS_UTILS_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"
85 #include "llvm/IR/BasicBlock.h"
86 #include "llvm/IR/Dominators.h"
87 #include "llvm/IR/Module.h"
88 #include "llvm/IR/OperandTraits.h"
89 #include "llvm/IR/Type.h"
90 #include "llvm/IR/Use.h"
91 #include "llvm/IR/User.h"
92 #include "llvm/IR/Value.h"
93 #include "llvm/Pass.h"
95 #include "llvm/Support/Casting.h"
96 #include "llvm/Support/Compiler.h"
98 #include <algorithm>
99 #include <cassert>
100 #include <cstddef>
101 #include <iterator>
102 #include <memory>
103 #include <utility>
104 
105 namespace llvm {
106 
107 class DominatorTree;
108 class Function;
109 class Instruction;
110 class MemoryAccess;
111 class LLVMContext;
112 class raw_ostream;
113 enum {
114  // Used to signify what the default invalid ID is for MemoryAccess's
115  // getID()
117 };
118 
119 template <class T> class memoryaccess_def_iterator_base;
123 
124 // \brief The base for all memory accesses. All memory accesses in a block are
125 // linked together using an intrusive list.
126 class MemoryAccess : public User, public ilist_node<MemoryAccess> {
127  void *operator new(size_t, unsigned) = delete;
128  void *operator new(size_t) = delete;
129 
130 public:
131  // Methods for support type inquiry through isa, cast, and
132  // dyn_cast
133  static inline bool classof(const MemoryAccess *) { return true; }
134  static inline bool classof(const Value *V) {
135  unsigned ID = V->getValueID();
136  return ID == MemoryUseVal || ID == MemoryPhiVal || ID == MemoryDefVal;
137  }
138 
139  ~MemoryAccess() override;
140 
141  BasicBlock *getBlock() const { return Block; }
142 
143  virtual void print(raw_ostream &OS) const = 0;
144  virtual void dump() const;
145 
146  /// \brief The user iterators for a memory access
149 
150  /// \brief This iterator walks over all of the defs in a given
151  /// MemoryAccess. For MemoryPhi nodes, this walks arguments. For
152  /// MemoryUse/MemoryDef, this walks the defining access.
157 
158 protected:
159  friend class MemorySSA;
160  friend class MemoryUseOrDef;
161  friend class MemoryUse;
162  friend class MemoryDef;
163  friend class MemoryPhi;
164 
165  /// \brief Used for debugging and tracking things about MemoryAccesses.
166  /// Guaranteed unique among MemoryAccesses, no guarantees otherwise.
167  virtual unsigned getID() const = 0;
168 
169  MemoryAccess(LLVMContext &C, unsigned Vty, BasicBlock *BB,
170  unsigned NumOperands)
171  : User(Type::getVoidTy(C), Vty, nullptr, NumOperands), Block(BB) {}
172 
173 private:
174  MemoryAccess(const MemoryAccess &);
175  void operator=(const MemoryAccess &);
176  BasicBlock *Block;
177 };
178 
180  MA.print(OS);
181  return OS;
182 }
183 
184 /// \brief Class that has the common methods + fields of memory uses/defs. It's
185 /// a little awkward to have, but there are many cases where we want either a
186 /// use or def, and there are many cases where uses are needed (defs aren't
187 /// acceptable), and vice-versa.
188 ///
189 /// This class should never be instantiated directly; make a MemoryUse or
190 /// MemoryDef instead.
191 class MemoryUseOrDef : public MemoryAccess {
192  void *operator new(size_t, unsigned) = delete;
193  void *operator new(size_t) = delete;
194 
195 public:
197 
198  /// \brief Get the instruction that this MemoryUse represents.
199  Instruction *getMemoryInst() const { return MemoryInst; }
200 
201  /// \brief Get the access that produces the memory state used by this Use.
202  MemoryAccess *getDefiningAccess() const { return getOperand(0); }
203 
204  static inline bool classof(const MemoryUseOrDef *) { return true; }
205  static inline bool classof(const Value *MA) {
206  return MA->getValueID() == MemoryUseVal || MA->getValueID() == MemoryDefVal;
207  }
208 
209 protected:
210  friend class MemorySSA;
211 
212  MemoryUseOrDef(LLVMContext &C, MemoryAccess *DMA, unsigned Vty,
213  Instruction *MI, BasicBlock *BB)
214  : MemoryAccess(C, Vty, BB, 1), MemoryInst(MI) {
215  setDefiningAccess(DMA);
216  }
217 
218  void setDefiningAccess(MemoryAccess *DMA) { setOperand(0, DMA); }
219 
220 private:
221  Instruction *MemoryInst;
222 };
223 
224 template <>
226  : public FixedNumOperandTraits<MemoryUseOrDef, 1> {};
228 
229 /// \brief Represents read-only accesses to memory
230 ///
231 /// In particular, the set of Instructions that will be represented by
232 /// MemoryUse's is exactly the set of Instructions for which
233 /// AliasAnalysis::getModRefInfo returns "Ref".
234 class MemoryUse final : public MemoryUseOrDef {
235  void *operator new(size_t, unsigned) = delete;
236 
237 public:
239 
240  // allocate space for exactly one operand
241  void *operator new(size_t s) { return User::operator new(s, 1); }
242 
244  : MemoryUseOrDef(C, DMA, MemoryUseVal, MI, BB), OptimizedID(0) {}
245 
246  static inline bool classof(const MemoryUse *) { return true; }
247  static inline bool classof(const Value *MA) {
248  return MA->getValueID() == MemoryUseVal;
249  }
250 
251  void print(raw_ostream &OS) const override;
252  void setDefiningAccess(MemoryAccess *DMA, bool Optimized = false) {
253  if (Optimized)
254  OptimizedID = DMA->getID();
256  }
257  bool isOptimized() const {
258  return getDefiningAccess() && OptimizedID == getDefiningAccess()->getID();
259  }
260  /// \brief Reset the ID of what this MemoryUse was optimized to, causing it to
261  /// be rewalked by the walker if necessary.
262  /// This really should only be called by tests.
263  void resetOptimized() { OptimizedID = INVALID_MEMORYACCESS_ID; }
264 
265 protected:
266  friend class MemorySSA;
267 
268  unsigned getID() const override {
269  llvm_unreachable("MemoryUses do not have IDs");
270  }
271 
272 private:
273  unsigned int OptimizedID;
274 };
275 
276 template <>
277 struct OperandTraits<MemoryUse> : public FixedNumOperandTraits<MemoryUse, 1> {};
279 
280 /// \brief Represents a read-write access to memory, whether it is a must-alias,
281 /// or a may-alias.
282 ///
283 /// In particular, the set of Instructions that will be represented by
284 /// MemoryDef's is exactly the set of Instructions for which
285 /// AliasAnalysis::getModRefInfo returns "Mod" or "ModRef".
286 /// Note that, in order to provide def-def chains, all defs also have a use
287 /// associated with them. This use points to the nearest reaching
288 /// MemoryDef/MemoryPhi.
289 class MemoryDef final : public MemoryUseOrDef {
290  void *operator new(size_t, unsigned) = delete;
291 
292 public:
294 
295  // allocate space for exactly one operand
296  void *operator new(size_t s) { return User::operator new(s, 1); }
297 
299  unsigned Ver)
300  : MemoryUseOrDef(C, DMA, MemoryDefVal, MI, BB), ID(Ver) {}
301 
302  static inline bool classof(const MemoryDef *) { return true; }
303  static inline bool classof(const Value *MA) {
304  return MA->getValueID() == MemoryDefVal;
305  }
306 
307  void print(raw_ostream &OS) const override;
308 
309 protected:
310  friend class MemorySSA;
311 
312  unsigned getID() const override { return ID; }
313 
314 private:
315  const unsigned ID;
316 };
317 
318 template <>
319 struct OperandTraits<MemoryDef> : public FixedNumOperandTraits<MemoryDef, 1> {};
321 
322 /// \brief Represents phi nodes for memory accesses.
323 ///
324 /// These have the same semantic as regular phi nodes, with the exception that
325 /// only one phi will ever exist in a given basic block.
326 /// Guaranteeing one phi per block means guaranteeing there is only ever one
327 /// valid reaching MemoryDef/MemoryPHI along each path to the phi node.
328 /// This is ensured by not allowing disambiguation of the RHS of a MemoryDef or
329 /// a MemoryPhi's operands.
330 /// That is, given
331 /// if (a) {
332 /// store %a
333 /// store %b
334 /// }
335 /// it *must* be transformed into
336 /// if (a) {
337 /// 1 = MemoryDef(liveOnEntry)
338 /// store %a
339 /// 2 = MemoryDef(1)
340 /// store %b
341 /// }
342 /// and *not*
343 /// if (a) {
344 /// 1 = MemoryDef(liveOnEntry)
345 /// store %a
346 /// 2 = MemoryDef(liveOnEntry)
347 /// store %b
348 /// }
349 /// even if the two stores do not conflict. Otherwise, both 1 and 2 reach the
350 /// end of the branch, and if there are not two phi nodes, one will be
351 /// disconnected completely from the SSA graph below that point.
352 /// Because MemoryUse's do not generate new definitions, they do not have this
353 /// issue.
354 class MemoryPhi final : public MemoryAccess {
355  void *operator new(size_t, unsigned) = delete;
356  // allocate space for exactly zero operands
357  void *operator new(size_t s) { return User::operator new(s); }
358 
359 public:
360  /// Provide fast operand accessors
362 
363  MemoryPhi(LLVMContext &C, BasicBlock *BB, unsigned Ver, unsigned NumPreds = 0)
364  : MemoryAccess(C, MemoryPhiVal, BB, 0), ID(Ver), ReservedSpace(NumPreds) {
365  allocHungoffUses(ReservedSpace);
366  }
367 
368  // Block iterator interface. This provides access to the list of incoming
369  // basic blocks, which parallels the list of incoming values.
372 
373  block_iterator block_begin() {
374  auto *Ref = reinterpret_cast<Use::UserRef *>(op_begin() + ReservedSpace);
375  return reinterpret_cast<block_iterator>(Ref + 1);
376  }
377 
378  const_block_iterator block_begin() const {
379  const auto *Ref =
380  reinterpret_cast<const Use::UserRef *>(op_begin() + ReservedSpace);
381  return reinterpret_cast<const_block_iterator>(Ref + 1);
382  }
383 
384  block_iterator block_end() { return block_begin() + getNumOperands(); }
385 
386  const_block_iterator block_end() const {
387  return block_begin() + getNumOperands();
388  }
389 
391  return make_range(block_begin(), block_end());
392  }
393 
395  return make_range(block_begin(), block_end());
396  }
397 
398  op_range incoming_values() { return operands(); }
399 
400  const_op_range incoming_values() const { return operands(); }
401 
402  /// \brief Return the number of incoming edges
403  unsigned getNumIncomingValues() const { return getNumOperands(); }
404 
405  /// \brief Return incoming value number x
406  MemoryAccess *getIncomingValue(unsigned I) const { return getOperand(I); }
407  void setIncomingValue(unsigned I, MemoryAccess *V) {
408  assert(V && "PHI node got a null value!");
409  setOperand(I, V);
410  }
411  static unsigned getOperandNumForIncomingValue(unsigned I) { return I; }
412  static unsigned getIncomingValueNumForOperand(unsigned I) { return I; }
413 
414  /// \brief Return incoming basic block number @p i.
415  BasicBlock *getIncomingBlock(unsigned I) const { return block_begin()[I]; }
416 
417  /// \brief Return incoming basic block corresponding
418  /// to an operand of the PHI.
419  BasicBlock *getIncomingBlock(const Use &U) const {
420  assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
421  return getIncomingBlock(unsigned(&U - op_begin()));
422  }
423 
424  /// \brief Return incoming basic block corresponding
425  /// to value use iterator.
427  return getIncomingBlock(I.getUse());
428  }
429 
430  void setIncomingBlock(unsigned I, BasicBlock *BB) {
431  assert(BB && "PHI node got a null basic block!");
432  block_begin()[I] = BB;
433  }
434 
435  /// \brief Add an incoming value to the end of the PHI list
436  void addIncoming(MemoryAccess *V, BasicBlock *BB) {
437  if (getNumOperands() == ReservedSpace)
438  growOperands(); // Get more space!
439  // Initialize some new operands.
440  setNumHungOffUseOperands(getNumOperands() + 1);
441  setIncomingValue(getNumOperands() - 1, V);
442  setIncomingBlock(getNumOperands() - 1, BB);
443  }
444 
445  /// \brief Return the first index of the specified basic
446  /// block in the value list for this PHI. Returns -1 if no instance.
447  int getBasicBlockIndex(const BasicBlock *BB) const {
448  for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
449  if (block_begin()[I] == BB)
450  return I;
451  return -1;
452  }
453 
455  int Idx = getBasicBlockIndex(BB);
456  assert(Idx >= 0 && "Invalid basic block argument!");
457  return getIncomingValue(Idx);
458  }
459 
460  static inline bool classof(const MemoryPhi *) { return true; }
461  static inline bool classof(const Value *V) {
462  return V->getValueID() == MemoryPhiVal;
463  }
464 
465  void print(raw_ostream &OS) const override;
466 
467 protected:
468  friend class MemorySSA;
469  /// \brief this is more complicated than the generic
470  /// User::allocHungoffUses, because we have to allocate Uses for the incoming
471  /// values and pointers to the incoming blocks, all in one allocation.
472  void allocHungoffUses(unsigned N) {
473  User::allocHungoffUses(N, /* IsPhi */ true);
474  }
475 
476  unsigned getID() const final { return ID; }
477 
478 private:
479  // For debugging only
480  const unsigned ID;
481  unsigned ReservedSpace;
482 
483  /// \brief This grows the operand list in response to a push_back style of
484  /// operation. This grows the number of ops by 1.5 times.
485  void growOperands() {
486  unsigned E = getNumOperands();
487  // 2 op PHI nodes are VERY common, so reserve at least enough for that.
488  ReservedSpace = std::max(E + E / 2, 2u);
489  growHungoffUses(ReservedSpace, /* IsPhi */ true);
490  }
491 };
492 
493 template <> struct OperandTraits<MemoryPhi> : public HungoffOperandTraits<2> {};
495 
496 class MemorySSAWalker;
497 
498 /// \brief Encapsulates MemorySSA, including all data associated with memory
499 /// accesses.
500 class MemorySSA {
501 public:
503  ~MemorySSA();
504 
505  MemorySSAWalker *getWalker();
506 
507  /// \brief Given a memory Mod/Ref'ing instruction, get the MemorySSA
508  /// access associated with it. If passed a basic block gets the memory phi
509  /// node that exists for that block, if there is one. Otherwise, this will get
510  /// a MemoryUseOrDef.
511  MemoryUseOrDef *getMemoryAccess(const Instruction *) const;
512  MemoryPhi *getMemoryAccess(const BasicBlock *BB) const;
513 
514  void dump() const;
515  void print(raw_ostream &) const;
516 
517  /// \brief Return true if \p MA represents the live on entry value
518  ///
519  /// Loads and stores from pointer arguments and other global values may be
520  /// defined by memory operations that do not occur in the current function, so
521  /// they may be live on entry to the function. MemorySSA represents such
522  /// memory state by the live on entry definition, which is guaranteed to occur
523  /// before any other memory access in the function.
524  inline bool isLiveOnEntryDef(const MemoryAccess *MA) const {
525  return MA == LiveOnEntryDef.get();
526  }
527 
528  inline MemoryAccess *getLiveOnEntryDef() const {
529  return LiveOnEntryDef.get();
530  }
531 
533 
534  /// \brief Return the list of MemoryAccess's for a given basic block.
535  ///
536  /// This list is not modifiable by the user.
537  const AccessList *getBlockAccesses(const BasicBlock *BB) const {
538  return getWritableBlockAccesses(BB);
539  }
540 
541  /// \brief Create an empty MemoryPhi in MemorySSA for a given basic block.
542  /// Only one MemoryPhi for a block exists at a time, so this function will
543  /// assert if you try to create one where it already exists.
544  MemoryPhi *createMemoryPhi(BasicBlock *BB);
545 
546  enum InsertionPlace { Beginning, End };
547 
548  /// \brief Create a MemoryAccess in MemorySSA at a specified point in a block,
549  /// with a specified clobbering definition.
550  ///
551  /// Returns the new MemoryAccess.
552  /// This should be called when a memory instruction is created that is being
553  /// used to replace an existing memory instruction. It will *not* create PHI
554  /// nodes, or verify the clobbering definition. The insertion place is used
555  /// solely to determine where in the memoryssa access lists the instruction
556  /// will be placed. The caller is expected to keep ordering the same as
557  /// instructions.
558  /// It will return the new MemoryAccess.
559  /// Note: If a MemoryAccess already exists for I, this function will make it
560  /// inaccessible and it *must* have removeMemoryAccess called on it.
561  MemoryAccess *createMemoryAccessInBB(Instruction *I, MemoryAccess *Definition,
562  const BasicBlock *BB,
563  InsertionPlace Point);
564  /// \brief Create a MemoryAccess in MemorySSA before or after an existing
565  /// MemoryAccess.
566  ///
567  /// Returns the new MemoryAccess.
568  /// This should be called when a memory instruction is created that is being
569  /// used to replace an existing memory instruction. It will *not* create PHI
570  /// nodes, or verify the clobbering definition. The clobbering definition
571  /// must be non-null.
572  /// Note: If a MemoryAccess already exists for I, this function will make it
573  /// inaccessible and it *must* have removeMemoryAccess called on it.
574  MemoryUseOrDef *createMemoryAccessBefore(Instruction *I,
575  MemoryAccess *Definition,
576  MemoryUseOrDef *InsertPt);
577  MemoryUseOrDef *createMemoryAccessAfter(Instruction *I,
578  MemoryAccess *Definition,
579  MemoryAccess *InsertPt);
580 
581  // \brief Splice \p What to just before \p Where.
582  //
583  // In order to be efficient, the following conditions must be met:
584  // - \p Where dominates \p What,
585  // - All memory accesses in [\p Where, \p What) are no-alias with \p What.
586  //
587  // TODO: relax the MemoryDef requirement on Where.
588  void spliceMemoryAccessAbove(MemoryDef *Where, MemoryUseOrDef *What);
589 
590  /// \brief Remove a MemoryAccess from MemorySSA, including updating all
591  /// definitions and uses.
592  /// This should be called when a memory instruction that has a MemoryAccess
593  /// associated with it is erased from the program. For example, if a store or
594  /// load is simply erased (not replaced), removeMemoryAccess should be called
595  /// on the MemoryAccess for that store/load.
596  void removeMemoryAccess(MemoryAccess *);
597 
598  /// \brief Given two memory accesses in the same basic block, determine
599  /// whether MemoryAccess \p A dominates MemoryAccess \p B.
600  bool locallyDominates(const MemoryAccess *A, const MemoryAccess *B) const;
601 
602  /// \brief Given two memory accesses in potentially different blocks,
603  /// determine whether MemoryAccess \p A dominates MemoryAccess \p B.
604  bool dominates(const MemoryAccess *A, const MemoryAccess *B) const;
605 
606  /// \brief Given a MemoryAccess and a Use, determine whether MemoryAccess \p A
607  /// dominates Use \p B.
608  bool dominates(const MemoryAccess *A, const Use &B) const;
609 
610  /// \brief Verify that MemorySSA is self consistent (IE definitions dominate
611  /// all uses, uses appear in the right places). This is used by unit tests.
612  void verifyMemorySSA() const;
613 
614 protected:
615  // Used by Memory SSA annotater, dumpers, and wrapper pass
618  void verifyDefUses(Function &F) const;
619  void verifyDomination(Function &F) const;
620  void verifyOrdering(Function &F) const;
621 
622  // This is used by the use optimizer class
624  auto It = PerBlockAccesses.find(BB);
625  return It == PerBlockAccesses.end() ? nullptr : It->second.get();
626  }
627 
628 private:
629  class CachingWalker;
630  class OptimizeUses;
631 
632  CachingWalker *getWalkerImpl();
633  void buildMemorySSA();
634  void optimizeUses();
635 
636  void verifyUseInDefs(MemoryAccess *, MemoryAccess *) const;
638 
639  void
640  determineInsertionPoint(const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks);
641  void computeDomLevels(DenseMap<DomTreeNode *, unsigned> &DomLevels);
642  void markUnreachableAsLiveOnEntry(BasicBlock *BB);
643  bool dominatesUse(const MemoryAccess *, const MemoryAccess *) const;
644  MemoryUseOrDef *createNewAccess(Instruction *);
645  MemoryUseOrDef *createDefinedAccess(Instruction *, MemoryAccess *);
646  MemoryAccess *findDominatingDef(BasicBlock *, enum InsertionPlace);
647  void removeFromLookups(MemoryAccess *);
648 
649  void placePHINodes(const SmallPtrSetImpl<BasicBlock *> &,
651  MemoryAccess *renameBlock(BasicBlock *, MemoryAccess *);
652  void renamePass(DomTreeNode *, MemoryAccess *IncomingVal,
654  AccessList *getOrCreateAccessList(const BasicBlock *);
655  void renumberBlock(const BasicBlock *) const;
656 
657  AliasAnalysis *AA;
658  DominatorTree *DT;
659  Function &F;
660 
661  // Memory SSA mappings
662  DenseMap<const Value *, MemoryAccess *> ValueToMemoryAccess;
663  AccessMap PerBlockAccesses;
664  std::unique_ptr<MemoryAccess> LiveOnEntryDef;
665 
666  // Domination mappings
667  // Note that the numbering is local to a block, even though the map is
668  // global.
669  mutable SmallPtrSet<const BasicBlock *, 16> BlockNumberingValid;
670  mutable DenseMap<const MemoryAccess *, unsigned long> BlockNumbering;
671 
672  // Memory SSA building info
673  std::unique_ptr<CachingWalker> Walker;
674  unsigned NextID;
675 };
676 
677 // This pass does eager building and then printing of MemorySSA. It is used by
678 // the tests to be able to build, dump, and verify Memory SSA.
680 public:
682 
683  static char ID;
684  bool runOnFunction(Function &) override;
685  void getAnalysisUsage(AnalysisUsage &AU) const override;
686 };
687 
688 /// An analysis that produces \c MemorySSA for a function.
689 ///
690 class MemorySSAAnalysis : public AnalysisInfoMixin<MemorySSAAnalysis> {
692  static AnalysisKey Key;
693 
694 public:
695  // Wrap MemorySSA result to ensure address stability of internal MemorySSA
696  // pointers after construction. Use a wrapper class instead of plain
697  // unique_ptr<MemorySSA> to avoid build breakage on MSVC.
698  struct Result {
699  Result(std::unique_ptr<MemorySSA> &&MSSA) : MSSA(std::move(MSSA)) {}
700  MemorySSA &getMSSA() { return *MSSA.get(); }
701 
702  std::unique_ptr<MemorySSA> MSSA;
703  };
704 
706 };
707 
708 /// \brief Printer pass for \c MemorySSA.
709 class MemorySSAPrinterPass : public PassInfoMixin<MemorySSAPrinterPass> {
710  raw_ostream &OS;
711 
712 public:
713  explicit MemorySSAPrinterPass(raw_ostream &OS) : OS(OS) {}
715 };
716 
717 /// \brief Verifier pass for \c MemorySSA.
718 struct MemorySSAVerifierPass : PassInfoMixin<MemorySSAVerifierPass> {
720 };
721 
722 /// \brief Legacy analysis pass which computes \c MemorySSA.
724 public:
726 
727  static char ID;
728  bool runOnFunction(Function &) override;
729  void releaseMemory() override;
730  MemorySSA &getMSSA() { return *MSSA; }
731  const MemorySSA &getMSSA() const { return *MSSA; }
732 
733  void getAnalysisUsage(AnalysisUsage &AU) const override;
734 
735  void verifyAnalysis() const override;
736  void print(raw_ostream &OS, const Module *M = nullptr) const override;
737 
738 private:
739  std::unique_ptr<MemorySSA> MSSA;
740 };
741 
742 /// \brief This is the generic walker interface for walkers of MemorySSA.
743 /// Walkers are used to be able to further disambiguate the def-use chains
744 /// MemorySSA gives you, or otherwise produce better info than MemorySSA gives
745 /// you.
746 /// In particular, while the def-use chains provide basic information, and are
747 /// guaranteed to give, for example, the nearest may-aliasing MemoryDef for a
748 /// MemoryUse as AliasAnalysis considers it, a user mant want better or other
749 /// information. In particular, they may want to use SCEV info to further
750 /// disambiguate memory accesses, or they may want the nearest dominating
751 /// may-aliasing MemoryDef for a call or a store. This API enables a
752 /// standardized interface to getting and using that info.
754 public:
756  virtual ~MemorySSAWalker() {}
757 
759 
760  /// \brief Given a memory Mod/Ref/ModRef'ing instruction, calling this
761  /// will give you the nearest dominating MemoryAccess that Mod's the location
762  /// the instruction accesses (by skipping any def which AA can prove does not
763  /// alias the location(s) accessed by the instruction given).
764  ///
765  /// Note that this will return a single access, and it must dominate the
766  /// Instruction, so if an operand of a MemoryPhi node Mod's the instruction,
767  /// this will return the MemoryPhi, not the operand. This means that
768  /// given:
769  /// if (a) {
770  /// 1 = MemoryDef(liveOnEntry)
771  /// store %a
772  /// } else {
773  /// 2 = MemoryDef(liveOnEntry)
774  /// store %b
775  /// }
776  /// 3 = MemoryPhi(2, 1)
777  /// MemoryUse(3)
778  /// load %a
779  ///
780  /// calling this API on load(%a) will return the MemoryPhi, not the MemoryDef
781  /// in the if (a) branch.
784  assert(MA && "Handed an instruction that MemorySSA doesn't recognize?");
785  return getClobberingMemoryAccess(MA);
786  }
787 
788  /// Does the same thing as getClobberingMemoryAccess(const Instruction *I),
789  /// but takes a MemoryAccess instead of an Instruction.
791 
792  /// \brief Given a potentially clobbering memory access and a new location,
793  /// calling this will give you the nearest dominating clobbering MemoryAccess
794  /// (by skipping non-aliasing def links).
795  ///
796  /// This version of the function is mainly used to disambiguate phi translated
797  /// pointers, where the value of a pointer may have changed from the initial
798  /// memory access. Note that this expects to be handed either a MemoryUse,
799  /// or an already potentially clobbering access. Unlike the above API, if
800  /// given a MemoryDef that clobbers the pointer as the starting access, it
801  /// will return that MemoryDef, whereas the above would return the clobber
802  /// starting from the use side of the memory def.
804  const MemoryLocation &) = 0;
805 
806  /// \brief Given a memory access, invalidate anything this walker knows about
807  /// that access.
808  /// This API is used by walkers that store information to perform basic cache
809  /// invalidation. This will be called by MemorySSA at appropriate times for
810  /// the walker it uses or returns.
811  virtual void invalidateInfo(MemoryAccess *) {}
812 
813  virtual void verify(const MemorySSA *MSSA) { assert(MSSA == this->MSSA); }
814 
815 protected:
816  friend class MemorySSA; // For updating MSSA pointer in MemorySSA move
817  // constructor.
819 };
820 
821 /// \brief A MemorySSAWalker that does no alias queries, or anything else. It
822 /// simply returns the links as they were constructed by the builder.
824 public:
825  // Keep the overrides below from hiding the Instruction overload of
826  // getClobberingMemoryAccess.
830  const MemoryLocation &) override;
831 };
832 
833 using MemoryAccessPair = std::pair<MemoryAccess *, MemoryLocation>;
834 using ConstMemoryAccessPair = std::pair<const MemoryAccess *, MemoryLocation>;
835 
836 /// \brief Iterator base class used to implement const and non-const iterators
837 /// over the defining accesses of a MemoryAccess.
838 template <class T>
840  : public iterator_facade_base<memoryaccess_def_iterator_base<T>,
841  std::forward_iterator_tag, T, ptrdiff_t, T *,
842  T *> {
843  using BaseT = typename memoryaccess_def_iterator_base::iterator_facade_base;
844 
845 public:
846  memoryaccess_def_iterator_base(T *Start) : Access(Start), ArgNo(0) {}
847  memoryaccess_def_iterator_base() : Access(nullptr), ArgNo(0) {}
848  bool operator==(const memoryaccess_def_iterator_base &Other) const {
849  return Access == Other.Access && (!Access || ArgNo == Other.ArgNo);
850  }
851 
852  // This is a bit ugly, but for MemoryPHI's, unlike PHINodes, you can't get the
853  // block from the operand in constant time (In a PHINode, the uselist has
854  // both, so it's just subtraction). We provide it as part of the
855  // iterator to avoid callers having to linear walk to get the block.
856  // If the operation becomes constant time on MemoryPHI's, this bit of
857  // abstraction breaking should be removed.
859  MemoryPhi *MP = dyn_cast<MemoryPhi>(Access);
860  assert(MP && "Tried to get phi arg block when not iterating over a PHI");
861  return MP->getIncomingBlock(ArgNo);
862  }
863  typename BaseT::iterator::pointer operator*() const {
864  assert(Access && "Tried to access past the end of our iterator");
865  // Go to the first argument for phis, and the defining access for everything
866  // else.
867  if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Access))
868  return MP->getIncomingValue(ArgNo);
869  return cast<MemoryUseOrDef>(Access)->getDefiningAccess();
870  }
871  using BaseT::operator++;
873  assert(Access && "Hit end of iterator");
874  if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Access)) {
875  if (++ArgNo >= MP->getNumIncomingValues()) {
876  ArgNo = 0;
877  Access = nullptr;
878  }
879  } else {
880  Access = nullptr;
881  }
882  return *this;
883  }
884 
885 private:
886  T *Access;
887  unsigned ArgNo;
888 };
889 
891  return memoryaccess_def_iterator(this);
892 }
893 
895  return const_memoryaccess_def_iterator(this);
896 }
897 
899  return memoryaccess_def_iterator();
900 }
901 
904 }
905 
906 /// \brief GraphTraits for a MemoryAccess, which walks defs in the normal case,
907 /// and uses in the inverse case.
908 template <> struct GraphTraits<MemoryAccess *> {
911 
912  static NodeRef getEntryNode(NodeRef N) { return N; }
914  static ChildIteratorType child_end(NodeRef N) { return N->defs_end(); }
915 };
916 
917 template <> struct GraphTraits<Inverse<MemoryAccess *>> {
920 
921  static NodeRef getEntryNode(NodeRef N) { return N; }
923  static ChildIteratorType child_end(NodeRef N) { return N->user_end(); }
924 };
925 
926 /// \brief Provide an iterator that walks defs, giving both the memory access,
927 /// and the current pointer location, updating the pointer location as it
928 /// changes due to phi node translation.
929 ///
930 /// This iterator, while somewhat specialized, is what most clients actually
931 /// want when walking upwards through MemorySSA def chains. It takes a pair of
932 /// <MemoryAccess,MemoryLocation>, and walks defs, properly translating the
933 /// memory location through phi nodes for the user.
935  : public iterator_facade_base<upward_defs_iterator,
936  std::forward_iterator_tag,
937  const MemoryAccessPair> {
938  using BaseT = upward_defs_iterator::iterator_facade_base;
939 
940 public:
942  : DefIterator(Info.first), Location(Info.second),
943  OriginalAccess(Info.first) {
944  CurrentPair.first = nullptr;
945 
946  WalkingPhi = Info.first && isa<MemoryPhi>(Info.first);
947  fillInCurrentPair();
948  }
949 
951  : DefIterator(), Location(), OriginalAccess(), WalkingPhi(false) {
952  CurrentPair.first = nullptr;
953  }
954 
955  bool operator==(const upward_defs_iterator &Other) const {
956  return DefIterator == Other.DefIterator;
957  }
958 
959  BaseT::iterator::reference operator*() const {
960  assert(DefIterator != OriginalAccess->defs_end() &&
961  "Tried to access past the end of our iterator");
962  return CurrentPair;
963  }
964 
965  using BaseT::operator++;
967  assert(DefIterator != OriginalAccess->defs_end() &&
968  "Tried to access past the end of the iterator");
969  ++DefIterator;
970  if (DefIterator != OriginalAccess->defs_end())
971  fillInCurrentPair();
972  return *this;
973  }
974 
975  BasicBlock *getPhiArgBlock() const { return DefIterator.getPhiArgBlock(); }
976 
977 private:
978  void fillInCurrentPair() {
979  CurrentPair.first = *DefIterator;
980  if (WalkingPhi && Location.Ptr) {
981  PHITransAddr Translator(
982  const_cast<Value *>(Location.Ptr),
983  OriginalAccess->getBlock()->getModule()->getDataLayout(), nullptr);
984  if (!Translator.PHITranslateValue(OriginalAccess->getBlock(),
985  DefIterator.getPhiArgBlock(), nullptr,
986  false))
987  if (Translator.getAddr() != Location.Ptr) {
988  CurrentPair.second = Location.getWithNewPtr(Translator.getAddr());
989  return;
990  }
991  }
992  CurrentPair.second = Location;
993  }
994 
995  MemoryAccessPair CurrentPair;
996  memoryaccess_def_iterator DefIterator;
997  MemoryLocation Location;
998  MemoryAccess *OriginalAccess;
999  bool WalkingPhi;
1000 };
1001 
1003  return upward_defs_iterator(Pair);
1004 }
1005 
1007 
1008 // Return true when MD may alias MU, return false otherwise.
1009 bool defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU,
1010  AliasAnalysis &AA);
1011 
1012 } // end namespace llvm
1013 
1014 #endif // LLVM_TRANSFORMS_UTILS_MEMORYSSA_H
static bool classof(const MemoryUseOrDef *)
Definition: MemorySSA.h:204
MemorySSA * MSSA
Definition: MemorySSA.h:818
memoryaccess_def_iterator & operator++()
Definition: MemorySSA.h:872
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *) override
Does the same thing as getClobberingMemoryAccess(const Instruction *I), but takes a MemoryAccess inst...
Definition: MemorySSA.cpp:2293
BasicBlock *const * const_block_iterator
Definition: MemorySSA.h:371
virtual void verify(const MemorySSA *MSSA)
Definition: MemorySSA.h:813
Result(std::unique_ptr< MemorySSA > &&MSSA)
Definition: MemorySSA.h:699
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
bool operator==(const memoryaccess_def_iterator_base &Other) const
Definition: MemorySSA.h:848
MemorySSAPrinterPass(raw_ostream &OS)
Definition: MemorySSA.h:713
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
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:263
Result run(Function &F, FunctionAnalysisManager &AM)
Definition: MemorySSA.cpp:2095
bool defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU, AliasAnalysis &AA)
Definition: MemorySSA.cpp:272
BaseT::iterator::reference operator*() const
Definition: MemorySSA.h:959
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
memoryaccess_def_iterator defs_begin()
This iterator walks over all of the defs in a given MemoryAccess.
Definition: MemorySSA.h:890
const_block_iterator block_end() const
Definition: MemorySSA.h:386
MemoryAccess(LLVMContext &C, unsigned Vty, BasicBlock *BB, unsigned NumOperands)
Definition: MemorySSA.h:169
BasicBlock * getPhiArgBlock() const
Definition: MemorySSA.h:975
This defines the Use class.
BasicBlock * getIncomingBlock(MemoryAccess::const_user_iterator I) const
Return incoming basic block corresponding to value use iterator.
Definition: MemorySSA.h:426
const MemorySSA & getMSSA() const
Definition: MemorySSA.h:731
void setIncomingValue(unsigned I, MemoryAccess *V)
Definition: MemorySSA.h:407
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:345
const_op_range incoming_values() const
Definition: MemorySSA.h:400
Represents read-only accesses to memory.
Definition: MemorySSA.h:234
static bool classof(const MemoryPhi *)
Definition: MemorySSA.h:460
virtual void dump() const
Definition: MemorySSA.cpp:2068
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: MemorySSA.cpp:2110
Legacy analysis pass which computes MemorySSA.
Definition: MemorySSA.h:723
block_iterator block_begin()
Definition: MemorySSA.h:373
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
Definition: BasicBlock.cpp:116
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
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
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
Definition: MemorySSA.h:202
MemoryLocation getWithNewPtr(const Value *NewPtr) const
static bool classof(const Value *MA)
Definition: MemorySSA.h:303
iterator_range< block_iterator > blocks()
Definition: MemorySSA.h:390
BasicBlock * getBlock() const
Definition: MemorySSA.h:141
user_iterator_impl< User > user_iterator
Definition: Value.h:340
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
#define F(x, y, z)
Definition: MD5.cpp:51
static bool classof(const MemoryAccess *)
Definition: MemorySSA.h:133
static ChildIteratorType child_end(NodeRef N)
Definition: MemorySSA.h:923
upward_defs_iterator upward_defs_end()
Definition: MemorySSA.h:1006
std::unique_ptr< MemorySSA > MSSA
Definition: MemorySSA.h:702
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:311
Function Alias Analysis false
Base class for the actual dominator tree node.
const_block_iterator block_begin() const
Definition: MemorySSA.h:378
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")
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
op_range incoming_values()
Definition: MemorySSA.h:398
memoryaccess_def_iterator defs_end()
Definition: MemorySSA.h:898
unsigned getID() const final
Used for debugging and tracking things about MemoryAccesses.
Definition: MemorySSA.h:476
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: MemorySSA.cpp:2102
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
MemoryUseOrDef(LLVMContext &C, MemoryAccess *DMA, unsigned Vty, Instruction *MI, BasicBlock *BB)
Definition: MemorySSA.h:212
An assembly annotator class to print Memory SSA information in comments.
Definition: MemorySSA.cpp:76
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: MemorySSA.cpp:2138
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:107
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
void setDefiningAccess(MemoryAccess *DMA, bool Optimized=false)
Definition: MemorySSA.h:252
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:36
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Definition: MemorySSA.h:403
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:48
upward_defs_iterator & operator++()
Definition: MemorySSA.h:966
static bool classof(const MemoryUse *)
Definition: MemorySSA.h:246
void addIncoming(MemoryAccess *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Definition: MemorySSA.h:436
virtual void print(raw_ostream &OS) const =0
early cse Early CSE w MemorySSA
Definition: EarlyCSE.cpp:1064
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:328
static unsigned getIncomingValueNumForOperand(unsigned I)
Definition: MemorySSA.h:412
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
void setIncomingBlock(unsigned I, BasicBlock *BB)
Definition: MemorySSA.h:430
upward_defs_iterator(const MemoryAccessPair &Info)
Definition: MemorySSA.h:941
Represent the analysis usage information of a pass.
BasicBlock ** block_iterator
Definition: MemorySSA.h:370
void setDefiningAccess(MemoryAccess *DMA)
Definition: MemorySSA.h:218
Printer pass for MemorySSA.
Definition: MemorySSA.h:709
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:434
static const unsigned End
static bool classof(const MemoryDef *)
Definition: MemorySSA.h:302
static unsigned getOperandNumForIncomingValue(unsigned I)
Definition: MemorySSA.h:411
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
static bool classof(const Value *MA)
Definition: MemorySSA.h:247
Value * getOperand(unsigned i) const
Definition: User.h:145
virtual ~MemorySSAWalker()
Definition: MemorySSA.h:756
MemoryAccess * getIncomingValue(unsigned I) const
Return incoming value number x.
Definition: MemorySSA.h:406
virtual void invalidateInfo(MemoryAccess *)
Given a memory access, invalidate anything this walker knows about that access.
Definition: MemorySSA.h:811
Iterator base class used to implement const and non-const iterators over the defining accesses of a M...
Definition: MemorySSA.h:119
#define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS)
Macro for generating in-class operand accessor declarations.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition: MemorySSA.cpp:2140
bool isOptimized() const
Definition: MemorySSA.h:257
~MemoryAccess() override
Definition: MemorySSA.cpp:2056
Provide an iterator that walks defs, giving both the memory access, and the current pointer location...
Definition: MemorySSA.h:934
An intrusive list with ownership and callbacks specified/controlled by ilist_traits, only with API safe for polymorphic types.
Definition: ilist.h:403
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A MemorySSAWalker that does no alias queries, or anything else.
Definition: MemorySSA.h:823
MemoryAccess * getLiveOnEntryDef() const
Definition: MemorySSA.h:528
std::pair< const MemoryAccess *, MemoryLocation > ConstMemoryAccessPair
Definition: MemorySSA.h:834
unsigned getID() const override
Used for debugging and tracking things about MemoryAccesses.
Definition: MemorySSA.h:268
iterator_range< const_block_iterator > blocks() const
Definition: MemorySSA.h:394
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
BaseT::iterator::pointer operator*() const
Definition: MemorySSA.h:863
static bool classof(const Value *V)
Definition: MemorySSA.h:461
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:425
static NodeRef getEntryNode(NodeRef N)
Definition: MemorySSA.h:921
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.
An analysis that produces MemorySSA for a function.
Definition: MemorySSA.h:690
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Definition: MemorySSA.h:454
bool operator==(const upward_defs_iterator &Other) const
Definition: MemorySSA.h:955
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
void setOperand(unsigned i, Value *Val)
Definition: User.h:150
Verifier pass for MemorySSA.
Definition: MemorySSA.h:718
A range adaptor for a pair of iterators.
MemoryPhi(LLVMContext &C, BasicBlock *BB, unsigned Ver, unsigned NumPreds=0)
Definition: MemorySSA.h:363
Class that has the common methods + fields of memory uses/defs.
Definition: MemorySSA.h:191
BasicBlock * getPhiArgBlock() const
Definition: MemorySSA.h:858
BasicBlock * getIncomingBlock(unsigned I) const
Return incoming basic block number i.
Definition: MemorySSA.h:415
user_iterator iterator
The user iterators for a memory access.
Definition: MemorySSA.h:147
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:384
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:341
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess)
memoryaccess_def_iterator_base< MemoryAccess > memoryaccess_def_iterator
Definition: MemorySSA.h:120
block_iterator block_end()
Definition: MemorySSA.h:384
This file provides utility analysis objects describing memory locations.
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
BasicBlock * getIncomingBlock(const Use &U) const
Return incoming basic block corresponding to an operand of the PHI.
Definition: MemorySSA.h:419
Compile-time customization of User operands.
Definition: User.h:43
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
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:1726
static ChildIteratorType child_begin(NodeRef N)
Definition: MemorySSA.h:922
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
static ChildIteratorType child_begin(NodeRef N)
Definition: MemorySSA.h:913
Instruction * getMemoryInst() const
Get the instruction that this MemoryUse represents.
Definition: MemorySSA.h:199
static bool classof(const Value *MA)
Definition: MemorySSA.h:205
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
Definition: Value.h:346
aarch64 promote const
static NodeRef getEntryNode(NodeRef N)
Definition: MemorySSA.h:912
LLVM Value Representation.
Definition: Value.h:71
unsigned getID() const override
Used for debugging and tracking things about MemoryAccesses.
Definition: MemorySSA.h:312
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:447
MemoryDef(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB, unsigned Ver)
Definition: MemorySSA.h:298
upward_defs_iterator upward_defs_begin(const MemoryAccessPair &Pair)
Definition: MemorySSA.h:1002
HungoffOperandTraits - determine the allocation regime of the Use array when it is not a prefix to th...
Definition: OperandTraits.h:93
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:43
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
IRTranslator LLVM IR MI
FixedNumOperandTraits - determine the allocation regime of the Use array when it is a prefix to the U...
Definition: OperandTraits.h:31
A container for analyses that lazily runs them and caches their results.
MemoryUse(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB)
Definition: MemorySSA.h:243
static bool classof(const Value *V)
Definition: MemorySSA.h:134
Represents phi nodes for memory accesses.
Definition: MemorySSA.h:354
static ChildIteratorType child_end(NodeRef N)
Definition: MemorySSA.h:914
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")
memoryaccess_def_iterator_base< const MemoryAccess > const_memoryaccess_def_iterator
Definition: MemorySSA.h:122
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
const_user_iterator const_iterator
Definition: MemorySSA.h:148
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:64
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
void allocHungoffUses(unsigned N)
this is more complicated than the generic User::allocHungoffUses, because we have to allocate Uses fo...
Definition: MemorySSA.h:472
user_iterator user_end()
Definition: Value.h:354