LLVM  5.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
16 /// does a trivial form of "heap versioning" Every time the memory state changes
17 /// in the program, we generate a new heap version. It generates
18 /// MemoryDef/Uses/Phis 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
58 /// def 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
65 /// (but 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_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"
86 #include "llvm/IR/BasicBlock.h"
87 #include "llvm/IR/DerivedUser.h"
88 #include "llvm/IR/Dominators.h"
89 #include "llvm/IR/Module.h"
90 #include "llvm/IR/OperandTraits.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/Pass.h"
96 #include "llvm/Support/Casting.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 Function;
108 class Instruction;
109 class MemoryAccess;
110 class LLVMContext;
111 class raw_ostream;
112 namespace MSSAHelpers {
113 struct AllAccessTag {};
114 struct DefsOnlyTag {};
115 }
116 
117 enum {
118  // Used to signify what the default invalid ID is for MemoryAccess's
119  // getID()
121 };
122 
123 template <class T> class memoryaccess_def_iterator_base;
127 
128 // \brief The base for all memory accesses. All memory accesses in a block are
129 // linked together using an intrusive list.
131  : public DerivedUser,
132  public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>,
133  public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>> {
134 public:
135  using AllAccessType =
137  using DefsOnlyType =
139 
140  // Methods for support type inquiry through isa, cast, and
141  // dyn_cast
142  static bool classof(const Value *V) {
143  unsigned ID = V->getValueID();
144  return ID == MemoryUseVal || ID == MemoryPhiVal || ID == MemoryDefVal;
145  }
146 
147  MemoryAccess(const MemoryAccess &) = delete;
148  MemoryAccess &operator=(const MemoryAccess &) = delete;
149 
150  void *operator new(size_t) = delete;
151 
152  BasicBlock *getBlock() const { return Block; }
153 
154  void print(raw_ostream &OS) const;
155  void dump() const;
156 
157  /// \brief The user iterators for a memory access
160 
161  /// \brief This iterator walks over all of the defs in a given
162  /// MemoryAccess. For MemoryPhi nodes, this walks arguments. For
163  /// MemoryUse/MemoryDef, this walks the defining access.
168 
169  /// \brief Get the iterators for the all access list and the defs only list
170  /// We default to the all access list.
172  return this->AllAccessType::getIterator();
173  }
175  return this->AllAccessType::getIterator();
176  }
178  return this->AllAccessType::getReverseIterator();
179  }
181  return this->AllAccessType::getReverseIterator();
182  }
184  return this->DefsOnlyType::getIterator();
185  }
187  return this->DefsOnlyType::getIterator();
188  }
190  return this->DefsOnlyType::getReverseIterator();
191  }
193  return this->DefsOnlyType::getReverseIterator();
194  }
195 
196 protected:
197  friend class MemorySSA;
198  friend class MemoryUseOrDef;
199  friend class MemoryUse;
200  friend class MemoryDef;
201  friend class MemoryPhi;
202 
203  /// \brief Used by MemorySSA to change the block of a MemoryAccess when it is
204  /// moved.
205  void setBlock(BasicBlock *BB) { Block = BB; }
206 
207  /// \brief Used for debugging and tracking things about MemoryAccesses.
208  /// Guaranteed unique among MemoryAccesses, no guarantees otherwise.
209  inline unsigned getID() const;
210 
211  MemoryAccess(LLVMContext &C, unsigned Vty, DeleteValueTy DeleteValue,
212  BasicBlock *BB, unsigned NumOperands)
213  : DerivedUser(Type::getVoidTy(C), Vty, nullptr, NumOperands, DeleteValue),
214  Block(BB) {}
215 
216 private:
217  BasicBlock *Block;
218 };
219 
221  MA.print(OS);
222  return OS;
223 }
224 
225 /// \brief Class that has the common methods + fields of memory uses/defs. It's
226 /// a little awkward to have, but there are many cases where we want either a
227 /// use or def, and there are many cases where uses are needed (defs aren't
228 /// acceptable), and vice-versa.
229 ///
230 /// This class should never be instantiated directly; make a MemoryUse or
231 /// MemoryDef instead.
232 class MemoryUseOrDef : public MemoryAccess {
233 public:
234  void *operator new(size_t) = delete;
235 
237 
238  /// \brief Get the instruction that this MemoryUse represents.
239  Instruction *getMemoryInst() const { return MemoryInst; }
240 
241  /// \brief Get the access that produces the memory state used by this Use.
242  MemoryAccess *getDefiningAccess() const { return getOperand(0); }
243 
244  static bool classof(const Value *MA) {
245  return MA->getValueID() == MemoryUseVal || MA->getValueID() == MemoryDefVal;
246  }
247 
248  // Sadly, these have to be public because they are needed in some of the
249  // iterators.
250  inline bool isOptimized() const;
251  inline MemoryAccess *getOptimized() const;
252  inline void setOptimized(MemoryAccess *);
253 
254  /// \brief Reset the ID of what this MemoryUse was optimized to, causing it to
255  /// be rewalked by the walker if necessary.
256  /// This really should only be called by tests.
257  inline void resetOptimized();
258 
259 protected:
260  friend class MemorySSA;
261  friend class MemorySSAUpdater;
262  MemoryUseOrDef(LLVMContext &C, MemoryAccess *DMA, unsigned Vty,
263  DeleteValueTy DeleteValue, Instruction *MI, BasicBlock *BB)
264  : MemoryAccess(C, Vty, DeleteValue, BB, 1), MemoryInst(MI) {
265  setDefiningAccess(DMA);
266  }
267  void setDefiningAccess(MemoryAccess *DMA, bool Optimized = false) {
268  if (!Optimized) {
269  setOperand(0, DMA);
270  return;
271  }
272  setOptimized(DMA);
273  }
274 
275 private:
276  Instruction *MemoryInst;
277 };
278 
279 template <>
281  : public FixedNumOperandTraits<MemoryUseOrDef, 1> {};
283 
284 /// \brief Represents read-only accesses to memory
285 ///
286 /// In particular, the set of Instructions that will be represented by
287 /// MemoryUse's is exactly the set of Instructions for which
288 /// AliasAnalysis::getModRefInfo returns "Ref".
289 class MemoryUse final : public MemoryUseOrDef {
290 public:
292 
294  : MemoryUseOrDef(C, DMA, MemoryUseVal, deleteMe, MI, BB),
295  OptimizedID(0) {}
296 
297  // allocate space for exactly one operand
298  void *operator new(size_t s) { return User::operator new(s, 1); }
299 
300  static bool classof(const Value *MA) {
301  return MA->getValueID() == MemoryUseVal;
302  }
303 
304  void print(raw_ostream &OS) const;
305 
307  OptimizedID = DMA->getID();
308  setOperand(0, DMA);
309  }
310 
311  bool isOptimized() const {
312  return getDefiningAccess() && OptimizedID == getDefiningAccess()->getID();
313  }
314 
316  return getDefiningAccess();
317  }
318  void resetOptimized() {
319  OptimizedID = INVALID_MEMORYACCESS_ID;
320  }
321 
322 protected:
323  friend class MemorySSA;
324 
325 private:
326  static void deleteMe(DerivedUser *Self);
327 
328  unsigned int OptimizedID;
329 };
330 
331 template <>
332 struct OperandTraits<MemoryUse> : public FixedNumOperandTraits<MemoryUse, 1> {};
334 
335 /// \brief Represents a read-write access to memory, whether it is a must-alias,
336 /// or a may-alias.
337 ///
338 /// In particular, the set of Instructions that will be represented by
339 /// MemoryDef's is exactly the set of Instructions for which
340 /// AliasAnalysis::getModRefInfo returns "Mod" or "ModRef".
341 /// Note that, in order to provide def-def chains, all defs also have a use
342 /// associated with them. This use points to the nearest reaching
343 /// MemoryDef/MemoryPhi.
344 class MemoryDef final : public MemoryUseOrDef {
345 public:
347 
349  unsigned Ver)
350  : MemoryUseOrDef(C, DMA, MemoryDefVal, deleteMe, MI, BB),
351  ID(Ver), Optimized(nullptr), OptimizedID(INVALID_MEMORYACCESS_ID) {}
352 
353  // allocate space for exactly one operand
354  void *operator new(size_t s) { return User::operator new(s, 1); }
355 
356  static bool classof(const Value *MA) {
357  return MA->getValueID() == MemoryDefVal;
358  }
359 
361  Optimized = MA;
362  OptimizedID = getDefiningAccess()->getID();
363  }
364  MemoryAccess *getOptimized() const { return Optimized; }
365  bool isOptimized() const {
366  return getOptimized() && getDefiningAccess() &&
367  OptimizedID == getDefiningAccess()->getID();
368  }
369  void resetOptimized() {
370  OptimizedID = INVALID_MEMORYACCESS_ID;
371  }
372 
373  void print(raw_ostream &OS) const;
374 
375  friend class MemorySSA;
376 
377  unsigned getID() const { return ID; }
378 
379 private:
380  static void deleteMe(DerivedUser *Self);
381 
382  const unsigned ID;
383  MemoryAccess *Optimized;
384  unsigned int OptimizedID;
385 };
386 
387 template <>
388 struct OperandTraits<MemoryDef> : public FixedNumOperandTraits<MemoryDef, 1> {};
390 
391 /// \brief Represents phi nodes for memory accesses.
392 ///
393 /// These have the same semantic as regular phi nodes, with the exception that
394 /// only one phi will ever exist in a given basic block.
395 /// Guaranteeing one phi per block means guaranteeing there is only ever one
396 /// valid reaching MemoryDef/MemoryPHI along each path to the phi node.
397 /// This is ensured by not allowing disambiguation of the RHS of a MemoryDef or
398 /// a MemoryPhi's operands.
399 /// That is, given
400 /// if (a) {
401 /// store %a
402 /// store %b
403 /// }
404 /// it *must* be transformed into
405 /// if (a) {
406 /// 1 = MemoryDef(liveOnEntry)
407 /// store %a
408 /// 2 = MemoryDef(1)
409 /// store %b
410 /// }
411 /// and *not*
412 /// if (a) {
413 /// 1 = MemoryDef(liveOnEntry)
414 /// store %a
415 /// 2 = MemoryDef(liveOnEntry)
416 /// store %b
417 /// }
418 /// even if the two stores do not conflict. Otherwise, both 1 and 2 reach the
419 /// end of the branch, and if there are not two phi nodes, one will be
420 /// disconnected completely from the SSA graph below that point.
421 /// Because MemoryUse's do not generate new definitions, they do not have this
422 /// issue.
423 class MemoryPhi final : public MemoryAccess {
424  // allocate space for exactly zero operands
425  void *operator new(size_t s) { return User::operator new(s); }
426 
427 public:
428  /// Provide fast operand accessors
430 
431  MemoryPhi(LLVMContext &C, BasicBlock *BB, unsigned Ver, unsigned NumPreds = 0)
432  : MemoryAccess(C, MemoryPhiVal, deleteMe, BB, 0), ID(Ver),
433  ReservedSpace(NumPreds) {
434  allocHungoffUses(ReservedSpace);
435  }
436 
437  // Block iterator interface. This provides access to the list of incoming
438  // basic blocks, which parallels the list of incoming values.
441 
442  block_iterator block_begin() {
443  auto *Ref = reinterpret_cast<Use::UserRef *>(op_begin() + ReservedSpace);
444  return reinterpret_cast<block_iterator>(Ref + 1);
445  }
446 
447  const_block_iterator block_begin() const {
448  const auto *Ref =
449  reinterpret_cast<const Use::UserRef *>(op_begin() + ReservedSpace);
450  return reinterpret_cast<const_block_iterator>(Ref + 1);
451  }
452 
453  block_iterator block_end() { return block_begin() + getNumOperands(); }
454 
455  const_block_iterator block_end() const {
456  return block_begin() + getNumOperands();
457  }
458 
460  return make_range(block_begin(), block_end());
461  }
462 
464  return make_range(block_begin(), block_end());
465  }
466 
467  op_range incoming_values() { return operands(); }
468 
469  const_op_range incoming_values() const { return operands(); }
470 
471  /// \brief Return the number of incoming edges
472  unsigned getNumIncomingValues() const { return getNumOperands(); }
473 
474  /// \brief Return incoming value number x
475  MemoryAccess *getIncomingValue(unsigned I) const { return getOperand(I); }
476  void setIncomingValue(unsigned I, MemoryAccess *V) {
477  assert(V && "PHI node got a null value!");
478  setOperand(I, V);
479  }
480  static unsigned getOperandNumForIncomingValue(unsigned I) { return I; }
481  static unsigned getIncomingValueNumForOperand(unsigned I) { return I; }
482 
483  /// \brief Return incoming basic block number @p i.
484  BasicBlock *getIncomingBlock(unsigned I) const { return block_begin()[I]; }
485 
486  /// \brief Return incoming basic block corresponding
487  /// to an operand of the PHI.
488  BasicBlock *getIncomingBlock(const Use &U) const {
489  assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
490  return getIncomingBlock(unsigned(&U - op_begin()));
491  }
492 
493  /// \brief Return incoming basic block corresponding
494  /// to value use iterator.
496  return getIncomingBlock(I.getUse());
497  }
498 
499  void setIncomingBlock(unsigned I, BasicBlock *BB) {
500  assert(BB && "PHI node got a null basic block!");
501  block_begin()[I] = BB;
502  }
503 
504  /// \brief Add an incoming value to the end of the PHI list
505  void addIncoming(MemoryAccess *V, BasicBlock *BB) {
506  if (getNumOperands() == ReservedSpace)
507  growOperands(); // Get more space!
508  // Initialize some new operands.
509  setNumHungOffUseOperands(getNumOperands() + 1);
510  setIncomingValue(getNumOperands() - 1, V);
511  setIncomingBlock(getNumOperands() - 1, BB);
512  }
513 
514  /// \brief Return the first index of the specified basic
515  /// block in the value list for this PHI. Returns -1 if no instance.
516  int getBasicBlockIndex(const BasicBlock *BB) const {
517  for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
518  if (block_begin()[I] == BB)
519  return I;
520  return -1;
521  }
522 
524  int Idx = getBasicBlockIndex(BB);
525  assert(Idx >= 0 && "Invalid basic block argument!");
526  return getIncomingValue(Idx);
527  }
528 
529  static bool classof(const Value *V) {
530  return V->getValueID() == MemoryPhiVal;
531  }
532 
533  void print(raw_ostream &OS) const;
534 
535  unsigned getID() const { return ID; }
536 
537 protected:
538  friend class MemorySSA;
539 
540  /// \brief this is more complicated than the generic
541  /// User::allocHungoffUses, because we have to allocate Uses for the incoming
542  /// values and pointers to the incoming blocks, all in one allocation.
543  void allocHungoffUses(unsigned N) {
544  User::allocHungoffUses(N, /* IsPhi */ true);
545  }
546 
547 private:
548  // For debugging only
549  const unsigned ID;
550  unsigned ReservedSpace;
551 
552  /// \brief This grows the operand list in response to a push_back style of
553  /// operation. This grows the number of ops by 1.5 times.
554  void growOperands() {
555  unsigned E = getNumOperands();
556  // 2 op PHI nodes are VERY common, so reserve at least enough for that.
557  ReservedSpace = std::max(E + E / 2, 2u);
558  growHungoffUses(ReservedSpace, /* IsPhi */ true);
559  }
560 
561  static void deleteMe(DerivedUser *Self);
562 };
563 
564 inline unsigned MemoryAccess::getID() const {
565  assert((isa<MemoryDef>(this) || isa<MemoryPhi>(this)) &&
566  "only memory defs and phis have ids");
567  if (const auto *MD = dyn_cast<MemoryDef>(this))
568  return MD->getID();
569  return cast<MemoryPhi>(this)->getID();
570 }
571 
572 inline bool MemoryUseOrDef::isOptimized() const {
573  if (const auto *MD = dyn_cast<MemoryDef>(this))
574  return MD->isOptimized();
575  return cast<MemoryUse>(this)->isOptimized();
576 }
577 
579  if (const auto *MD = dyn_cast<MemoryDef>(this))
580  return MD->getOptimized();
581  return cast<MemoryUse>(this)->getOptimized();
582 }
583 
585  if (auto *MD = dyn_cast<MemoryDef>(this))
586  MD->setOptimized(MA);
587  else
588  cast<MemoryUse>(this)->setOptimized(MA);
589 }
590 
592  if (auto *MD = dyn_cast<MemoryDef>(this))
593  MD->resetOptimized();
594  else
595  cast<MemoryUse>(this)->resetOptimized();
596 }
597 
598 
599 template <> struct OperandTraits<MemoryPhi> : public HungoffOperandTraits<2> {};
601 
602 class MemorySSAWalker;
603 
604 /// \brief Encapsulates MemorySSA, including all data associated with memory
605 /// accesses.
606 class MemorySSA {
607 public:
609  ~MemorySSA();
610 
611  MemorySSAWalker *getWalker();
612 
613  /// \brief Given a memory Mod/Ref'ing instruction, get the MemorySSA
614  /// access associated with it. If passed a basic block gets the memory phi
615  /// node that exists for that block, if there is one. Otherwise, this will get
616  /// a MemoryUseOrDef.
617  MemoryUseOrDef *getMemoryAccess(const Instruction *) const;
618  MemoryPhi *getMemoryAccess(const BasicBlock *BB) const;
619 
620  void dump() const;
621  void print(raw_ostream &) const;
622 
623  /// \brief Return true if \p MA represents the live on entry value
624  ///
625  /// Loads and stores from pointer arguments and other global values may be
626  /// defined by memory operations that do not occur in the current function, so
627  /// they may be live on entry to the function. MemorySSA represents such
628  /// memory state by the live on entry definition, which is guaranteed to occur
629  /// before any other memory access in the function.
630  inline bool isLiveOnEntryDef(const MemoryAccess *MA) const {
631  return MA == LiveOnEntryDef.get();
632  }
633 
634  inline MemoryAccess *getLiveOnEntryDef() const {
635  return LiveOnEntryDef.get();
636  }
637 
638  // Sadly, iplists, by default, owns and deletes pointers added to the
639  // list. It's not currently possible to have two iplists for the same type,
640  // where one owns the pointers, and one does not. This is because the traits
641  // are per-type, not per-tag. If this ever changes, we should make the
642  // DefList an iplist.
644  using DefsList =
646 
647  /// \brief Return the list of MemoryAccess's for a given basic block.
648  ///
649  /// This list is not modifiable by the user.
650  const AccessList *getBlockAccesses(const BasicBlock *BB) const {
651  return getWritableBlockAccesses(BB);
652  }
653 
654  /// \brief Return the list of MemoryDef's and MemoryPhi's for a given basic
655  /// block.
656  ///
657  /// This list is not modifiable by the user.
658  const DefsList *getBlockDefs(const BasicBlock *BB) const {
659  return getWritableBlockDefs(BB);
660  }
661 
662  /// \brief Given two memory accesses in the same basic block, determine
663  /// whether MemoryAccess \p A dominates MemoryAccess \p B.
664  bool locallyDominates(const MemoryAccess *A, const MemoryAccess *B) const;
665 
666  /// \brief Given two memory accesses in potentially different blocks,
667  /// determine whether MemoryAccess \p A dominates MemoryAccess \p B.
668  bool dominates(const MemoryAccess *A, const MemoryAccess *B) const;
669 
670  /// \brief Given a MemoryAccess and a Use, determine whether MemoryAccess \p A
671  /// dominates Use \p B.
672  bool dominates(const MemoryAccess *A, const Use &B) const;
673 
674  /// \brief Verify that MemorySSA is self consistent (IE definitions dominate
675  /// all uses, uses appear in the right places). This is used by unit tests.
676  void verifyMemorySSA() const;
677 
678  /// Used in various insertion functions to specify whether we are talking
679  /// about the beginning or end of a block.
680  enum InsertionPlace { Beginning, End };
681 
682 protected:
683  // Used by Memory SSA annotater, dumpers, and wrapper pass
686  friend class MemorySSAUpdater;
687 
688  void verifyDefUses(Function &F) const;
689  void verifyDomination(Function &F) const;
690  void verifyOrdering(Function &F) const;
691 
692  // This is used by the use optimizer and updater.
694  auto It = PerBlockAccesses.find(BB);
695  return It == PerBlockAccesses.end() ? nullptr : It->second.get();
696  }
697 
698  // This is used by the use optimizer and updater.
700  auto It = PerBlockDefs.find(BB);
701  return It == PerBlockDefs.end() ? nullptr : It->second.get();
702  }
703 
704  // These is used by the updater to perform various internal MemorySSA
705  // machinsations. They do not always leave the IR in a correct state, and
706  // relies on the updater to fixup what it breaks, so it is not public.
707 
708  void moveTo(MemoryUseOrDef *What, BasicBlock *BB, AccessList::iterator Where);
709  void moveTo(MemoryUseOrDef *What, BasicBlock *BB, InsertionPlace Point);
710  // Rename the dominator tree branch rooted at BB.
711  void renamePass(BasicBlock *BB, MemoryAccess *IncomingVal,
713  renamePass(DT->getNode(BB), IncomingVal, Visited, true, true);
714  }
715  void removeFromLookups(MemoryAccess *);
716  void removeFromLists(MemoryAccess *, bool ShouldDelete = true);
717  void insertIntoListsForBlock(MemoryAccess *, const BasicBlock *,
718  InsertionPlace);
719  void insertIntoListsBefore(MemoryAccess *, const BasicBlock *,
720  AccessList::iterator);
721  MemoryUseOrDef *createDefinedAccess(Instruction *, MemoryAccess *);
722 
723 private:
724  class CachingWalker;
725  class OptimizeUses;
726 
727  CachingWalker *getWalkerImpl();
728  void buildMemorySSA();
729  void optimizeUses();
730 
731  void verifyUseInDefs(MemoryAccess *, MemoryAccess *) const;
734 
735  void
736  determineInsertionPoint(const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks);
737  void markUnreachableAsLiveOnEntry(BasicBlock *BB);
738  bool dominatesUse(const MemoryAccess *, const MemoryAccess *) const;
739  MemoryPhi *createMemoryPhi(BasicBlock *BB);
740  MemoryUseOrDef *createNewAccess(Instruction *);
741  MemoryAccess *findDominatingDef(BasicBlock *, enum InsertionPlace);
742  void placePHINodes(const SmallPtrSetImpl<BasicBlock *> &,
744  MemoryAccess *renameBlock(BasicBlock *, MemoryAccess *, bool);
745  void renameSuccessorPhis(BasicBlock *, MemoryAccess *, bool);
746  void renamePass(DomTreeNode *, MemoryAccess *IncomingVal,
748  bool SkipVisited = false, bool RenameAllUses = false);
749  AccessList *getOrCreateAccessList(const BasicBlock *);
750  DefsList *getOrCreateDefsList(const BasicBlock *);
751  void renumberBlock(const BasicBlock *) const;
752  AliasAnalysis *AA;
753  DominatorTree *DT;
754  Function &F;
755 
756  // Memory SSA mappings
757  DenseMap<const Value *, MemoryAccess *> ValueToMemoryAccess;
758  // These two mappings contain the main block to access/def mappings for
759  // MemorySSA. The list contained in PerBlockAccesses really owns all the
760  // MemoryAccesses.
761  // Both maps maintain the invariant that if a block is found in them, the
762  // corresponding list is not empty, and if a block is not found in them, the
763  // corresponding list is empty.
764  AccessMap PerBlockAccesses;
765  DefsMap PerBlockDefs;
766  std::unique_ptr<MemoryAccess> LiveOnEntryDef;
767 
768  // Domination mappings
769  // Note that the numbering is local to a block, even though the map is
770  // global.
771  mutable SmallPtrSet<const BasicBlock *, 16> BlockNumberingValid;
772  mutable DenseMap<const MemoryAccess *, unsigned long> BlockNumbering;
773 
774  // Memory SSA building info
775  std::unique_ptr<CachingWalker> Walker;
776  unsigned NextID;
777 };
778 
779 // Internal MemorySSA utils, for use by MemorySSA classes and walkers
781 protected:
782  friend class MemorySSAWalker;
783  friend class GVNHoist;
784  // This function should not be used by new passes.
785  static bool defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU,
786  AliasAnalysis &AA);
787 };
788 
789 // This pass does eager building and then printing of MemorySSA. It is used by
790 // the tests to be able to build, dump, and verify Memory SSA.
792 public:
794 
795  bool runOnFunction(Function &) override;
796  void getAnalysisUsage(AnalysisUsage &AU) const override;
797 
798  static char ID;
799 };
800 
801 /// An analysis that produces \c MemorySSA for a function.
802 ///
803 class MemorySSAAnalysis : public AnalysisInfoMixin<MemorySSAAnalysis> {
805 
806  static AnalysisKey Key;
807 
808 public:
809  // Wrap MemorySSA result to ensure address stability of internal MemorySSA
810  // pointers after construction. Use a wrapper class instead of plain
811  // unique_ptr<MemorySSA> to avoid build breakage on MSVC.
812  struct Result {
813  Result(std::unique_ptr<MemorySSA> &&MSSA) : MSSA(std::move(MSSA)) {}
814  MemorySSA &getMSSA() { return *MSSA.get(); }
815 
816  std::unique_ptr<MemorySSA> MSSA;
817  };
818 
820 };
821 
822 /// \brief Printer pass for \c MemorySSA.
823 class MemorySSAPrinterPass : public PassInfoMixin<MemorySSAPrinterPass> {
824  raw_ostream &OS;
825 
826 public:
827  explicit MemorySSAPrinterPass(raw_ostream &OS) : OS(OS) {}
828 
830 };
831 
832 /// \brief Verifier pass for \c MemorySSA.
833 struct MemorySSAVerifierPass : PassInfoMixin<MemorySSAVerifierPass> {
835 };
836 
837 /// \brief Legacy analysis pass which computes \c MemorySSA.
839 public:
841 
842  static char ID;
843 
844  bool runOnFunction(Function &) override;
845  void releaseMemory() override;
846  MemorySSA &getMSSA() { return *MSSA; }
847  const MemorySSA &getMSSA() const { return *MSSA; }
848 
849  void getAnalysisUsage(AnalysisUsage &AU) const override;
850 
851  void verifyAnalysis() const override;
852  void print(raw_ostream &OS, const Module *M = nullptr) const override;
853 
854 private:
855  std::unique_ptr<MemorySSA> MSSA;
856 };
857 
858 /// \brief This is the generic walker interface for walkers of MemorySSA.
859 /// Walkers are used to be able to further disambiguate the def-use chains
860 /// MemorySSA gives you, or otherwise produce better info than MemorySSA gives
861 /// you.
862 /// In particular, while the def-use chains provide basic information, and are
863 /// guaranteed to give, for example, the nearest may-aliasing MemoryDef for a
864 /// MemoryUse as AliasAnalysis considers it, a user mant want better or other
865 /// information. In particular, they may want to use SCEV info to further
866 /// disambiguate memory accesses, or they may want the nearest dominating
867 /// may-aliasing MemoryDef for a call or a store. This API enables a
868 /// standardized interface to getting and using that info.
870 public:
872  virtual ~MemorySSAWalker() = default;
873 
875 
876  /// \brief Given a memory Mod/Ref/ModRef'ing instruction, calling this
877  /// will give you the nearest dominating MemoryAccess that Mod's the location
878  /// the instruction accesses (by skipping any def which AA can prove does not
879  /// alias the location(s) accessed by the instruction given).
880  ///
881  /// Note that this will return a single access, and it must dominate the
882  /// Instruction, so if an operand of a MemoryPhi node Mod's the instruction,
883  /// this will return the MemoryPhi, not the operand. This means that
884  /// given:
885  /// if (a) {
886  /// 1 = MemoryDef(liveOnEntry)
887  /// store %a
888  /// } else {
889  /// 2 = MemoryDef(liveOnEntry)
890  /// store %b
891  /// }
892  /// 3 = MemoryPhi(2, 1)
893  /// MemoryUse(3)
894  /// load %a
895  ///
896  /// calling this API on load(%a) will return the MemoryPhi, not the MemoryDef
897  /// in the if (a) branch.
900  assert(MA && "Handed an instruction that MemorySSA doesn't recognize?");
901  return getClobberingMemoryAccess(MA);
902  }
903 
904  /// Does the same thing as getClobberingMemoryAccess(const Instruction *I),
905  /// but takes a MemoryAccess instead of an Instruction.
907 
908  /// \brief Given a potentially clobbering memory access and a new location,
909  /// calling this will give you the nearest dominating clobbering MemoryAccess
910  /// (by skipping non-aliasing def links).
911  ///
912  /// This version of the function is mainly used to disambiguate phi translated
913  /// pointers, where the value of a pointer may have changed from the initial
914  /// memory access. Note that this expects to be handed either a MemoryUse,
915  /// or an already potentially clobbering access. Unlike the above API, if
916  /// given a MemoryDef that clobbers the pointer as the starting access, it
917  /// will return that MemoryDef, whereas the above would return the clobber
918  /// starting from the use side of the memory def.
920  const MemoryLocation &) = 0;
921 
922  /// \brief Given a memory access, invalidate anything this walker knows about
923  /// that access.
924  /// This API is used by walkers that store information to perform basic cache
925  /// invalidation. This will be called by MemorySSA at appropriate times for
926  /// the walker it uses or returns.
927  virtual void invalidateInfo(MemoryAccess *) {}
928 
929  virtual void verify(const MemorySSA *MSSA) { assert(MSSA == this->MSSA); }
930 
931 protected:
932  friend class MemorySSA; // For updating MSSA pointer in MemorySSA move
933  // constructor.
935 };
936 
937 /// \brief A MemorySSAWalker that does no alias queries, or anything else. It
938 /// simply returns the links as they were constructed by the builder.
940 public:
941  // Keep the overrides below from hiding the Instruction overload of
942  // getClobberingMemoryAccess.
944 
947  const MemoryLocation &) override;
948 };
949 
950 using MemoryAccessPair = std::pair<MemoryAccess *, MemoryLocation>;
951 using ConstMemoryAccessPair = std::pair<const MemoryAccess *, MemoryLocation>;
952 
953 /// \brief Iterator base class used to implement const and non-const iterators
954 /// over the defining accesses of a MemoryAccess.
955 template <class T>
957  : public iterator_facade_base<memoryaccess_def_iterator_base<T>,
958  std::forward_iterator_tag, T, ptrdiff_t, T *,
959  T *> {
960  using BaseT = typename memoryaccess_def_iterator_base::iterator_facade_base;
961 
962 public:
963  memoryaccess_def_iterator_base(T *Start) : Access(Start) {}
964  memoryaccess_def_iterator_base() = default;
965 
966  bool operator==(const memoryaccess_def_iterator_base &Other) const {
967  return Access == Other.Access && (!Access || ArgNo == Other.ArgNo);
968  }
969 
970  // This is a bit ugly, but for MemoryPHI's, unlike PHINodes, you can't get the
971  // block from the operand in constant time (In a PHINode, the uselist has
972  // both, so it's just subtraction). We provide it as part of the
973  // iterator to avoid callers having to linear walk to get the block.
974  // If the operation becomes constant time on MemoryPHI's, this bit of
975  // abstraction breaking should be removed.
977  MemoryPhi *MP = dyn_cast<MemoryPhi>(Access);
978  assert(MP && "Tried to get phi arg block when not iterating over a PHI");
979  return MP->getIncomingBlock(ArgNo);
980  }
981  typename BaseT::iterator::pointer operator*() const {
982  assert(Access && "Tried to access past the end of our iterator");
983  // Go to the first argument for phis, and the defining access for everything
984  // else.
985  if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Access))
986  return MP->getIncomingValue(ArgNo);
987  return cast<MemoryUseOrDef>(Access)->getDefiningAccess();
988  }
989  using BaseT::operator++;
991  assert(Access && "Hit end of iterator");
992  if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Access)) {
993  if (++ArgNo >= MP->getNumIncomingValues()) {
994  ArgNo = 0;
995  Access = nullptr;
996  }
997  } else {
998  Access = nullptr;
999  }
1000  return *this;
1001  }
1002 
1003 private:
1004  T *Access = nullptr;
1005  unsigned ArgNo = 0;
1006 };
1007 
1009  return memoryaccess_def_iterator(this);
1010 }
1011 
1013  return const_memoryaccess_def_iterator(this);
1014 }
1015 
1017  return memoryaccess_def_iterator();
1018 }
1019 
1022 }
1023 
1024 /// \brief GraphTraits for a MemoryAccess, which walks defs in the normal case,
1025 /// and uses in the inverse case.
1026 template <> struct GraphTraits<MemoryAccess *> {
1029 
1030  static NodeRef getEntryNode(NodeRef N) { return N; }
1032  static ChildIteratorType child_end(NodeRef N) { return N->defs_end(); }
1033 };
1034 
1035 template <> struct GraphTraits<Inverse<MemoryAccess *>> {
1038 
1039  static NodeRef getEntryNode(NodeRef N) { return N; }
1041  static ChildIteratorType child_end(NodeRef N) { return N->user_end(); }
1042 };
1043 
1044 /// \brief Provide an iterator that walks defs, giving both the memory access,
1045 /// and the current pointer location, updating the pointer location as it
1046 /// changes due to phi node translation.
1047 ///
1048 /// This iterator, while somewhat specialized, is what most clients actually
1049 /// want when walking upwards through MemorySSA def chains. It takes a pair of
1050 /// <MemoryAccess,MemoryLocation>, and walks defs, properly translating the
1051 /// memory location through phi nodes for the user.
1053  : public iterator_facade_base<upward_defs_iterator,
1054  std::forward_iterator_tag,
1055  const MemoryAccessPair> {
1056  using BaseT = upward_defs_iterator::iterator_facade_base;
1057 
1058 public:
1060  : DefIterator(Info.first), Location(Info.second),
1061  OriginalAccess(Info.first) {
1062  CurrentPair.first = nullptr;
1063 
1064  WalkingPhi = Info.first && isa<MemoryPhi>(Info.first);
1065  fillInCurrentPair();
1066  }
1067 
1068  upward_defs_iterator() { CurrentPair.first = nullptr; }
1069 
1070  bool operator==(const upward_defs_iterator &Other) const {
1071  return DefIterator == Other.DefIterator;
1072  }
1073 
1074  BaseT::iterator::reference operator*() const {
1075  assert(DefIterator != OriginalAccess->defs_end() &&
1076  "Tried to access past the end of our iterator");
1077  return CurrentPair;
1078  }
1079 
1080  using BaseT::operator++;
1082  assert(DefIterator != OriginalAccess->defs_end() &&
1083  "Tried to access past the end of the iterator");
1084  ++DefIterator;
1085  if (DefIterator != OriginalAccess->defs_end())
1086  fillInCurrentPair();
1087  return *this;
1088  }
1089 
1090  BasicBlock *getPhiArgBlock() const { return DefIterator.getPhiArgBlock(); }
1091 
1092 private:
1093  void fillInCurrentPair() {
1094  CurrentPair.first = *DefIterator;
1095  if (WalkingPhi && Location.Ptr) {
1096  PHITransAddr Translator(
1097  const_cast<Value *>(Location.Ptr),
1098  OriginalAccess->getBlock()->getModule()->getDataLayout(), nullptr);
1099  if (!Translator.PHITranslateValue(OriginalAccess->getBlock(),
1100  DefIterator.getPhiArgBlock(), nullptr,
1101  false))
1102  if (Translator.getAddr() != Location.Ptr) {
1103  CurrentPair.second = Location.getWithNewPtr(Translator.getAddr());
1104  return;
1105  }
1106  }
1107  CurrentPair.second = Location;
1108  }
1109 
1110  MemoryAccessPair CurrentPair;
1111  memoryaccess_def_iterator DefIterator;
1112  MemoryLocation Location;
1113  MemoryAccess *OriginalAccess = nullptr;
1114  bool WalkingPhi = false;
1115 };
1116 
1118  return upward_defs_iterator(Pair);
1119 }
1120 
1122 
1123 inline iterator_range<upward_defs_iterator>
1125  return make_range(upward_defs_begin(Pair), upward_defs_end());
1126 }
1127 
1128 /// Walks the defining accesses of MemoryDefs. Stops after we hit something that
1129 /// has no defining use (e.g. a MemoryPhi or liveOnEntry). Note that, when
1130 /// comparing against a null def_chain_iterator, this will compare equal only
1131 /// after walking said Phi/liveOnEntry.
1132 ///
1133 /// The UseOptimizedChain flag specifies whether to walk the clobbering
1134 /// access chain, or all the accesses.
1135 ///
1136 /// Normally, MemoryDef are all just def/use linked together, so a def_chain on
1137 /// a MemoryDef will walk all MemoryDefs above it in the program until it hits
1138 /// a phi node. The optimized chain walks the clobbering access of a store.
1139 /// So if you are just trying to find, given a store, what the next
1140 /// thing that would clobber the same memory is, you want the optimized chain.
1141 template <class T, bool UseOptimizedChain = false>
1143  : public iterator_facade_base<def_chain_iterator<T, UseOptimizedChain>,
1144  std::forward_iterator_tag, MemoryAccess *> {
1145  def_chain_iterator() : MA(nullptr) {}
1146  def_chain_iterator(T MA) : MA(MA) {}
1147 
1148  T operator*() const { return MA; }
1149 
1151  // N.B. liveOnEntry has a null defining access.
1152  if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA)) {
1153  if (UseOptimizedChain && MUD->isOptimized())
1154  MA = MUD->getOptimized();
1155  else
1156  MA = MUD->getDefiningAccess();
1157  } else {
1158  MA = nullptr;
1159  }
1160 
1161  return *this;
1162  }
1163 
1164  bool operator==(const def_chain_iterator &O) const { return MA == O.MA; }
1165 
1166 private:
1167  T MA;
1168 };
1169 
1170 template <class T>
1171 inline iterator_range<def_chain_iterator<T>>
1172 def_chain(T MA, MemoryAccess *UpTo = nullptr) {
1173 #ifdef EXPENSIVE_CHECKS
1174  assert((!UpTo || find(def_chain(MA), UpTo) != def_chain_iterator<T>()) &&
1175  "UpTo isn't in the def chain!");
1176 #endif
1178 }
1179 
1180 template <class T>
1183  def_chain_iterator<T, true>(nullptr));
1184 }
1185 
1186 } // end namespace llvm
1187 
1188 #endif // LLVM_ANALYSIS_MEMORYSSA_H
MemoryAccess * getOptimized() const
Definition: MemorySSA.h:315
MemorySSA * MSSA
Definition: MemorySSA.h:934
memoryaccess_def_iterator & operator++()
Definition: MemorySSA.h:990
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *) override
Does the same thing as getClobberingMemoryAccess(const Instruction *I), but takes a MemoryAccess inst...
Definition: MemorySSA.cpp:2050
BasicBlock *const * const_block_iterator
Definition: MemorySSA.h:440
MemoryAccess * getOptimized() const
Definition: MemorySSA.h:364
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
virtual void verify(const MemorySSA *MSSA)
Definition: MemorySSA.h:929
MemoryAccess & operator=(const MemoryAccess &)=delete
Result(std::unique_ptr< MemorySSA > &&MSSA)
Definition: MemorySSA.h:813
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:171
bool operator==(const memoryaccess_def_iterator_base &Other) const
Definition: MemorySSA.h:966
MemorySSAPrinterPass(raw_ostream &OS)
Definition: MemorySSA.h:827
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
void resetOptimized()
Definition: MemorySSA.h:318
Result run(Function &F, FunctionAnalysisManager &AM)
Definition: MemorySSA.cpp:1886
AllAccessType::const_self_iterator getIterator() const
Definition: MemorySSA.h:174
BaseT::iterator::reference operator*() const
Definition: MemorySSA.h:1074
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
const DefsList * getBlockDefs(const BasicBlock *BB) const
Return the list of MemoryDef's and MemoryPhi's for a given basic block.
Definition: MemorySSA.h:658
Extension point for the Value hierarchy.
Definition: DerivedUser.h:28
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
Definition: MemorySSA.h:344
unsigned getID() const
Used for debugging and tracking things about MemoryAccesses.
Definition: MemorySSA.h:564
void setOptimized(MemoryAccess *)
Definition: MemorySSA.h:584
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: MemorySSA.cpp:1871
memoryaccess_def_iterator defs_begin()
This iterator walks over all of the defs in a given MemoryAccess.
Definition: MemorySSA.h:1008
unsigned second
const_block_iterator block_end() const
Definition: MemorySSA.h:455
MemoryAccess(const MemoryAccess &)=delete
BasicBlock * getPhiArgBlock() const
Definition: MemorySSA.h:1090
void(*)(DerivedUser *) DeleteValueTy
Definition: DerivedUser.h:30
This defines the Use class.
DefsOnlyType::self_iterator getDefsIterator()
Definition: MemorySSA.h:183
BasicBlock * getIncomingBlock(MemoryAccess::const_user_iterator I) const
Return incoming basic block corresponding to value use iterator.
Definition: MemorySSA.h:495
const MemorySSA & getMSSA() const
Definition: MemorySSA.h:847
void setIncomingValue(unsigned I, MemoryAccess *V)
Definition: MemorySSA.h:476
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:469
Represents read-only accesses to memory.
Definition: MemorySSA.h:289
void dump() const
Definition: MemorySSA.cpp:1857
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: MemorySSA.cpp:1901
Legacy analysis pass which computes MemorySSA.
Definition: MemorySSA.h:838
unsigned getID() const
Definition: MemorySSA.h:377
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:591
block_iterator block_begin()
Definition: MemorySSA.h:442
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
bool isOptimized() const
Definition: MemorySSA.h:365
AccessList * getWritableBlockAccesses(const BasicBlock *BB) const
Definition: MemorySSA.h:693
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:606
MemoryUseOrDef * getMemoryAccess(const Instruction *) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition: MemorySSA.cpp:1713
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
Definition: MemorySSA.h:242
MemoryLocation getWithNewPtr(const Value *NewPtr) const
Walks the defining accesses of MemoryDefs.
Definition: MemorySSA.h:1142
def_chain_iterator & operator++()
Definition: MemorySSA.h:1150
MemoryUseOrDef(LLVMContext &C, MemoryAccess *DMA, unsigned Vty, DeleteValueTy DeleteValue, Instruction *MI, BasicBlock *BB)
Definition: MemorySSA.h:262
static bool classof(const Value *MA)
Definition: MemorySSA.h:356
iterator_range< block_iterator > blocks()
Definition: MemorySSA.h:459
A simple intrusive list implementation.
Definition: simple_ilist.h:79
BasicBlock * getBlock() const
Definition: MemorySSA.h:152
#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:41
#define F(x, y, z)
Definition: MD5.cpp:55
static ChildIteratorType child_end(NodeRef N)
Definition: MemorySSA.h:1041
DefsOnlyType::const_reverse_self_iterator getReverseDefsIterator() const
Definition: MemorySSA.h:192
upward_defs_iterator upward_defs_end()
Definition: MemorySSA.h:1121
std::unique_ptr< MemorySSA > MSSA
Definition: MemorySSA.h:816
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
Base class for the actual dominator tree node.
const_block_iterator block_begin() const
Definition: MemorySSA.h:447
bool runOnFunction(Function &) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Definition: MemorySSA.cpp:1876
This is the generic walker interface for walkers of MemorySSA.
Definition: MemorySSA.h:869
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:68
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:134
op_range incoming_values()
Definition: MemorySSA.h:467
memoryaccess_def_iterator defs_end()
Definition: MemorySSA.h:1016
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: MemorySSA.cpp:1893
An assembly annotator class to print Memory SSA information in comments.
Definition: MemorySSA.cpp:71
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: MemorySSA.cpp:1929
void renamePass(BasicBlock *BB, MemoryAccess *IncomingVal, SmallPtrSetImpl< BasicBlock * > &Visited)
Definition: MemorySSA.h:711
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:153
AllAccessType::reverse_self_iterator getReverseIterator()
Definition: MemorySSA.h:177
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
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:472
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:68
upward_defs_iterator & operator++()
Definition: MemorySSA.h:1081
bool isOptimized() const
Definition: MemorySSA.h:572
void addIncoming(MemoryAccess *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Definition: MemorySSA.h:505
#define A
Definition: LargeTest.cpp:12
bool operator==(const def_chain_iterator &O) const
Definition: MemorySSA.h:1164
early cse Early CSE w MemorySSA
Definition: EarlyCSE.cpp:1100
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:382
static unsigned getIncomingValueNumForOperand(unsigned I)
Definition: MemorySSA.h:481
bool runOnFunction(Function &) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Definition: MemorySSA.cpp:1922
MemorySSAWalker(MemorySSA *)
Definition: MemorySSA.cpp:1935
void setIncomingBlock(unsigned I, BasicBlock *BB)
Definition: MemorySSA.h:499
upward_defs_iterator(const MemoryAccessPair &Info)
Definition: MemorySSA.h:1059
InsertionPlace
Used in various insertion functions to specify whether we are talking about the beginning or end of a...
Definition: MemorySSA.h:680
Represent the analysis usage information of a pass.
iterator_range< def_chain_iterator< T, true > > optimized_def_chain(T MA)
Definition: MemorySSA.h:1181
BasicBlock ** block_iterator
Definition: MemorySSA.h:439
Printer pass for MemorySSA.
Definition: MemorySSA.h:823
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:459
static const unsigned End
static unsigned getOperandNumForIncomingValue(unsigned I)
Definition: MemorySSA.h:480
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
static bool classof(const Value *MA)
Definition: MemorySSA.h:300
Value * getOperand(unsigned i) const
Definition: User.h:154
MemoryAccess * getIncomingValue(unsigned I) const
Return incoming value number x.
Definition: MemorySSA.h:475
virtual void invalidateInfo(MemoryAccess *)
Given a memory access, invalidate anything this walker knows about that access.
Definition: MemorySSA.h:927
Iterator base class used to implement const and non-const iterators over the defining accesses of a M...
Definition: MemorySSA.h:123
#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:1931
bool isOptimized() const
Definition: MemorySSA.h:311
Provide an iterator that walks defs, giving both the memory access, and the current pointer location...
Definition: MemorySSA.h:1052
An intrusive list with ownership and callbacks specified/controlled by ilist_traits, only with API safe for polymorphic types.
Definition: ilist.h:403
A MemorySSAWalker that does no alias queries, or anything else.
Definition: MemorySSA.h:939
MemoryAccess * getLiveOnEntryDef() const
Definition: MemorySSA.h:634
std::pair< const MemoryAccess *, MemoryLocation > ConstMemoryAccessPair
Definition: MemorySSA.h:951
unsigned first
iterator_range< const_block_iterator > blocks() const
Definition: MemorySSA.h:463
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:981
Iterator for intrusive lists based on ilist_node.
static bool classof(const Value *V)
Definition: MemorySSA.h:529
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:423
#define E
Definition: LargeTest.cpp:27
static bool defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU, AliasAnalysis &AA)
Definition: MemorySSA.cpp:270
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:839
#define B
Definition: LargeTest.cpp:24
virtual ~MemorySSAWalker()=default
void print(raw_ostream &OS) const
Definition: MemorySSA.cpp:1801
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
unsigned getID() const
Definition: MemorySSA.h:535
const size_t N
An analysis that produces MemorySSA for a function.
Definition: MemorySSA.h:803
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Definition: MemorySSA.h:523
bool operator==(const upward_defs_iterator &Other) const
Definition: MemorySSA.h:1070
DefsOnlyType::const_self_iterator getDefsIterator() const
Definition: MemorySSA.h:186
void setOperand(unsigned i, Value *Val)
Definition: User.h:159
Verifier pass for MemorySSA.
Definition: MemorySSA.h:833
A range adaptor for a pair of iterators.
MemoryPhi(LLVMContext &C, BasicBlock *BB, unsigned Ver, unsigned NumPreds=0)
Definition: MemorySSA.h:431
Class that has the common methods + fields of memory uses/defs.
Definition: MemorySSA.h:232
BasicBlock * getPhiArgBlock() const
Definition: MemorySSA.h:976
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:365
BasicBlock * getIncomingBlock(unsigned I) const
Return incoming basic block number i.
Definition: MemorySSA.h:484
user_iterator iterator
The user iterators for a memory access.
Definition: MemorySSA.h:158
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:361
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess)
iterator_range< def_chain_iterator< T > > def_chain(T MA, MemoryAccess *UpTo=nullptr)
Definition: MemorySSA.h:1172
memoryaccess_def_iterator_base< MemoryAccess > memoryaccess_def_iterator
Definition: MemorySSA.h:124
block_iterator block_end()
Definition: MemorySSA.h:453
#define C
Definition: LargeTest.cpp:25
This file provides utility analysis objects describing memory locations.
void setDefiningAccess(MemoryAccess *DMA, bool Optimized=false)
Definition: MemorySSA.h:267
#define I(x, y, z)
Definition: MD5.cpp:58
user_iterator_impl< User > user_iterator
Definition: Value.h:364
BasicBlock * getIncomingBlock(const Use &U) const
Return incoming basic block corresponding to an operand of the PHI.
Definition: MemorySSA.h:488
Compile-time customization of User operands.
Definition: User.h:43
MemoryAccess(LLVMContext &C, unsigned Vty, DeleteValueTy DeleteValue, BasicBlock *BB, unsigned NumOperands)
Definition: MemorySSA.h:211
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:323
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2018
static ChildIteratorType child_begin(NodeRef N)
Definition: MemorySSA.h:1040
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:898
static ChildIteratorType child_begin(NodeRef N)
Definition: MemorySSA.h:1031
Instruction * getMemoryInst() const
Get the instruction that this MemoryUse represents.
Definition: MemorySSA.h:239
static bool classof(const Value *MA)
Definition: MemorySSA.h:244
MemoryAccess * getOptimized() const
Definition: MemorySSA.h:578
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
Definition: Value.h:371
void setOptimized(MemoryAccess *MA)
Definition: MemorySSA.h:360
static NodeRef getEntryNode(NodeRef N)
Definition: MemorySSA.h:1030
LLVM Value Representation.
Definition: Value.h:73
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:516
MemoryDef(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB, unsigned Ver)
Definition: MemorySSA.h:348
DefsOnlyType::reverse_self_iterator getReverseDefsIterator()
Definition: MemorySSA.h:189
upward_defs_iterator upward_defs_begin(const MemoryAccessPair &Pair)
Definition: MemorySSA.h:1117
HungoffOperandTraits - determine the allocation regime of the Use array when it is not a prefix to th...
Definition: OperandTraits.h:96
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:41
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: MemorySSA.cpp:1916
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.
void setBlock(BasicBlock *BB)
Used by MemorySSA to change the block of a MemoryAccess when it is moved.
Definition: MemorySSA.h:205
MemoryUse(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB)
Definition: MemorySSA.h:293
DefsList * getWritableBlockDefs(const BasicBlock *BB) const
Definition: MemorySSA.h:699
static bool classof(const Value *V)
Definition: MemorySSA.h:142
iterator_range< upward_defs_iterator > upward_defs(const MemoryAccessPair &Pair)
Definition: MemorySSA.h:1124
Represents phi nodes for memory accesses.
Definition: MemorySSA.h:423
static ChildIteratorType child_end(NodeRef N)
Definition: MemorySSA.h:1032
const AccessList * getBlockAccesses(const BasicBlock *BB) const
Return the list of MemoryAccess's for a given basic block.
Definition: MemorySSA.h:650
AllAccessType::const_reverse_self_iterator getReverseIterator() const
Definition: MemorySSA.h:180
memoryaccess_def_iterator_base< const MemoryAccess > const_memoryaccess_def_iterator
Definition: MemorySSA.h:126
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:1914
const_user_iterator const_iterator
Definition: MemorySSA.h:159
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70
void resetOptimized()
Definition: MemorySSA.h:369
void setOptimized(MemoryAccess *DMA)
Definition: MemorySSA.h:306
std::pair< MemoryAccess *, MemoryLocation > MemoryAccessPair
Definition: MemorySSA.h:950
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
Definition: MemorySSA.h:630
void allocHungoffUses(unsigned N)
this is more complicated than the generic User::allocHungoffUses, because we have to allocate Uses fo...
Definition: MemorySSA.h:543
user_iterator user_end()
Definition: Value.h:379