1 //===- MemorySSA.h - Build Memory SSA ---------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file exposes an interface to building/using memory SSA to
12 /// walk memory instructions using a use/def graph.
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.
20 /// As a trivial example,
21 /// define i32 @main() #0 {
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
36 /// define i32 @main() #0 {
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
49 /// %7 = load i32* %2, align 4
51 /// %8 = load i32* %4, align 4
52 /// %add = add nsw i32 %7, %8
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.
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).
67 /// MemoryDefs are not disambiguated because it would require multiple reaching
68 /// definitions, which would require multiple phis, and multiple memoryaccesses
70 //===----------------------------------------------------------------------===//
72 #ifndef LLVM_ANALYSIS_MEMORYSSA_H
73 #define LLVM_ANALYSIS_MEMORYSSA_H
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"
82 #include "llvm/ADT/iterator_range.h"
83 #include "llvm/Analysis/AliasAnalysis.h"
84 #include "llvm/Analysis/MemoryLocation.h"
85 #include "llvm/Analysis/PHITransAddr.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"
97 #include "llvm/Support/ErrorHandling.h"
112 namespace MSSAHelpers {
113 struct AllAccessTag {};
114 struct DefsOnlyTag {};
118 // Used to signify what the default invalid ID is for MemoryAccess's
120 INVALID_MEMORYACCESS_ID = 0
123 template <class T> class memoryaccess_def_iterator_base;
124 using memoryaccess_def_iterator = memoryaccess_def_iterator_base<MemoryAccess>;
125 using const_memoryaccess_def_iterator =
126 memoryaccess_def_iterator_base<const MemoryAccess>;
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>> {
135 using AllAccessType =
136 ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>;
138 ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>>;
140 // Methods for support type inquiry through isa, cast, and
142 static inline bool classof(const Value *V) {
143 unsigned ID = V->getValueID();
144 return ID == MemoryUseVal || ID == MemoryPhiVal || ID == MemoryDefVal;
147 MemoryAccess(const MemoryAccess &) = delete;
148 MemoryAccess &operator=(const MemoryAccess &) = delete;
150 void *operator new(size_t) = delete;
152 BasicBlock *getBlock() const { return Block; }
154 void print(raw_ostream &OS) const;
157 /// \brief The user iterators for a memory access
158 typedef user_iterator iterator;
159 typedef const_user_iterator const_iterator;
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.
164 memoryaccess_def_iterator defs_begin();
165 const_memoryaccess_def_iterator defs_begin() const;
166 memoryaccess_def_iterator defs_end();
167 const_memoryaccess_def_iterator defs_end() const;
169 /// \brief Get the iterators for the all access list and the defs only list
170 /// We default to the all access list.
171 AllAccessType::self_iterator getIterator() {
172 return this->AllAccessType::getIterator();
174 AllAccessType::const_self_iterator getIterator() const {
175 return this->AllAccessType::getIterator();
177 AllAccessType::reverse_self_iterator getReverseIterator() {
178 return this->AllAccessType::getReverseIterator();
180 AllAccessType::const_reverse_self_iterator getReverseIterator() const {
181 return this->AllAccessType::getReverseIterator();
183 DefsOnlyType::self_iterator getDefsIterator() {
184 return this->DefsOnlyType::getIterator();
186 DefsOnlyType::const_self_iterator getDefsIterator() const {
187 return this->DefsOnlyType::getIterator();
189 DefsOnlyType::reverse_self_iterator getReverseDefsIterator() {
190 return this->DefsOnlyType::getReverseIterator();
192 DefsOnlyType::const_reverse_self_iterator getReverseDefsIterator() const {
193 return this->DefsOnlyType::getReverseIterator();
197 friend class MemorySSA;
198 friend class MemoryUseOrDef;
199 friend class MemoryUse;
200 friend class MemoryDef;
201 friend class MemoryPhi;
203 /// \brief Used by MemorySSA to change the block of a MemoryAccess when it is
205 void setBlock(BasicBlock *BB) { Block = BB; }
207 /// \brief Used for debugging and tracking things about MemoryAccesses.
208 /// Guaranteed unique among MemoryAccesses, no guarantees otherwise.
209 inline unsigned getID() const;
211 MemoryAccess(LLVMContext &C, unsigned Vty, DeleteValueTy DeleteValue,
212 BasicBlock *BB, unsigned NumOperands)
213 : DerivedUser(Type::getVoidTy(C), Vty, nullptr, NumOperands, DeleteValue),
220 inline raw_ostream &operator<<(raw_ostream &OS, const MemoryAccess &MA) {
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.
230 /// This class should never be instantiated directly; make a MemoryUse or
231 /// MemoryDef instead.
232 class MemoryUseOrDef : public MemoryAccess {
234 void *operator new(size_t) = delete;
236 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
238 /// \brief Get the instruction that this MemoryUse represents.
239 Instruction *getMemoryInst() const { return MemoryInst; }
241 /// \brief Get the access that produces the memory state used by this Use.
242 MemoryAccess *getDefiningAccess() const { return getOperand(0); }
244 static inline bool classof(const Value *MA) {
245 return MA->getValueID() == MemoryUseVal || MA->getValueID() == MemoryDefVal;
248 // Sadly, these have to be public because they are needed in some of the
250 inline bool isOptimized() const;
251 inline MemoryAccess *getOptimized() const;
252 inline void setOptimized(MemoryAccess *);
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();
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);
267 void setDefiningAccess(MemoryAccess *DMA, bool Optimized = false) {
276 Instruction *MemoryInst;
280 struct OperandTraits<MemoryUseOrDef>
281 : public FixedNumOperandTraits<MemoryUseOrDef, 1> {};
282 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryUseOrDef, MemoryAccess)
284 /// \brief Represents read-only accesses to memory
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 {
291 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
293 MemoryUse(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB)
294 : MemoryUseOrDef(C, DMA, MemoryUseVal, deleteMe, MI, BB),
297 // allocate space for exactly one operand
298 void *operator new(size_t s) { return User::operator new(s, 1); }
300 static inline bool classof(const Value *MA) {
301 return MA->getValueID() == MemoryUseVal;
304 void print(raw_ostream &OS) const;
306 void setOptimized(MemoryAccess *DMA) {
307 OptimizedID = DMA->getID();
311 bool isOptimized() const {
312 return getDefiningAccess() && OptimizedID == getDefiningAccess()->getID();
315 MemoryAccess *getOptimized() const {
316 return getDefiningAccess();
318 void resetOptimized() {
319 OptimizedID = INVALID_MEMORYACCESS_ID;
323 friend class MemorySSA;
326 static void deleteMe(DerivedUser *Self);
328 unsigned int OptimizedID;
332 struct OperandTraits<MemoryUse> : public FixedNumOperandTraits<MemoryUse, 1> {};
333 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryUse, MemoryAccess)
335 /// \brief Represents a read-write access to memory, whether it is a must-alias,
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 {
346 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
348 MemoryDef(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB,
350 : MemoryUseOrDef(C, DMA, MemoryDefVal, deleteMe, MI, BB),
351 ID(Ver), Optimized(nullptr), OptimizedID(INVALID_MEMORYACCESS_ID) {}
353 // allocate space for exactly one operand
354 void *operator new(size_t s) { return User::operator new(s, 1); }
356 static inline bool classof(const Value *MA) {
357 return MA->getValueID() == MemoryDefVal;
360 void setOptimized(MemoryAccess *MA) {
362 OptimizedID = getDefiningAccess()->getID();
364 MemoryAccess *getOptimized() const { return Optimized; }
365 bool isOptimized() const {
366 return getOptimized() && getDefiningAccess() &&
367 OptimizedID == getDefiningAccess()->getID();
369 void resetOptimized() {
370 OptimizedID = INVALID_MEMORYACCESS_ID;
373 void print(raw_ostream &OS) const;
375 friend class MemorySSA;
377 unsigned getID() const { return ID; }
380 static void deleteMe(DerivedUser *Self);
383 MemoryAccess *Optimized;
384 unsigned int OptimizedID;
388 struct OperandTraits<MemoryDef> : public FixedNumOperandTraits<MemoryDef, 1> {};
389 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryDef, MemoryAccess)
391 /// \brief Represents phi nodes for memory accesses.
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.
404 /// it *must* be transformed into
406 /// 1 = MemoryDef(liveOnEntry)
413 /// 1 = MemoryDef(liveOnEntry)
415 /// 2 = MemoryDef(liveOnEntry)
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
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); }
428 /// Provide fast operand accessors
429 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
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);
437 // Block iterator interface. This provides access to the list of incoming
438 // basic blocks, which parallels the list of incoming values.
439 typedef BasicBlock **block_iterator;
440 typedef BasicBlock *const *const_block_iterator;
442 block_iterator block_begin() {
443 auto *Ref = reinterpret_cast<Use::UserRef *>(op_begin() + ReservedSpace);
444 return reinterpret_cast<block_iterator>(Ref + 1);
447 const_block_iterator block_begin() const {
449 reinterpret_cast<const Use::UserRef *>(op_begin() + ReservedSpace);
450 return reinterpret_cast<const_block_iterator>(Ref + 1);
453 block_iterator block_end() { return block_begin() + getNumOperands(); }
455 const_block_iterator block_end() const {
456 return block_begin() + getNumOperands();
459 iterator_range<block_iterator> blocks() {
460 return make_range(block_begin(), block_end());
463 iterator_range<const_block_iterator> blocks() const {
464 return make_range(block_begin(), block_end());
467 op_range incoming_values() { return operands(); }
469 const_op_range incoming_values() const { return operands(); }
471 /// \brief Return the number of incoming edges
472 unsigned getNumIncomingValues() const { return getNumOperands(); }
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!");
480 static unsigned getOperandNumForIncomingValue(unsigned I) { return I; }
481 static unsigned getIncomingValueNumForOperand(unsigned I) { return I; }
483 /// \brief Return incoming basic block number @p i.
484 BasicBlock *getIncomingBlock(unsigned I) const { return block_begin()[I]; }
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()));
493 /// \brief Return incoming basic block corresponding
494 /// to value use iterator.
495 BasicBlock *getIncomingBlock(MemoryAccess::const_user_iterator I) const {
496 return getIncomingBlock(I.getUse());
499 void setIncomingBlock(unsigned I, BasicBlock *BB) {
500 assert(BB && "PHI node got a null basic block!");
501 block_begin()[I] = BB;
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);
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)
523 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
524 int Idx = getBasicBlockIndex(BB);
525 assert(Idx >= 0 && "Invalid basic block argument!");
526 return getIncomingValue(Idx);
529 static inline bool classof(const Value *V) {
530 return V->getValueID() == MemoryPhiVal;
533 void print(raw_ostream &OS) const;
535 unsigned getID() const { return ID; }
538 friend class MemorySSA;
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);
548 // For debugging only
550 unsigned ReservedSpace;
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);
561 static void deleteMe(DerivedUser *Self);
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))
569 return cast<MemoryPhi>(this)->getID();
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();
578 inline MemoryAccess *MemoryUseOrDef::getOptimized() const {
579 if (const auto *MD = dyn_cast<MemoryDef>(this))
580 return MD->getOptimized();
581 return cast<MemoryUse>(this)->getOptimized();
584 inline void MemoryUseOrDef::setOptimized(MemoryAccess *MA) {
585 if (auto *MD = dyn_cast<MemoryDef>(this))
586 MD->setOptimized(MA);
588 cast<MemoryUse>(this)->setOptimized(MA);
591 inline void MemoryUseOrDef::resetOptimized() {
592 if (auto *MD = dyn_cast<MemoryDef>(this))
593 MD->resetOptimized();
595 cast<MemoryUse>(this)->resetOptimized();
599 template <> struct OperandTraits<MemoryPhi> : public HungoffOperandTraits<2> {};
600 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryPhi, MemoryAccess)
602 class MemorySSAWalker;
604 /// \brief Encapsulates MemorySSA, including all data associated with memory
608 MemorySSA(Function &, AliasAnalysis *, DominatorTree *);
611 MemorySSAWalker *getWalker();
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;
621 void print(raw_ostream &) const;
623 /// \brief Return true if \p MA represents the live on entry value
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();
634 inline MemoryAccess *getLiveOnEntryDef() const {
635 return LiveOnEntryDef.get();
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.
643 using AccessList = iplist<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>;
645 simple_ilist<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>>;
647 /// \brief Return the list of MemoryAccess's for a given basic block.
649 /// This list is not modifiable by the user.
650 const AccessList *getBlockAccesses(const BasicBlock *BB) const {
651 return getWritableBlockAccesses(BB);
654 /// \brief Return the list of MemoryDef's and MemoryPhi's for a given basic
657 /// This list is not modifiable by the user.
658 const DefsList *getBlockDefs(const BasicBlock *BB) const {
659 return getWritableBlockDefs(BB);
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;
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;
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;
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;
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 };
683 // Used by Memory SSA annotater, dumpers, and wrapper pass
684 friend class MemorySSAAnnotatedWriter;
685 friend class MemorySSAPrinterLegacyPass;
686 friend class MemorySSAUpdater;
688 void verifyDefUses(Function &F) const;
689 void verifyDomination(Function &F) const;
690 void verifyOrdering(Function &F) const;
692 // This is used by the use optimizer and updater.
693 AccessList *getWritableBlockAccesses(const BasicBlock *BB) const {
694 auto It = PerBlockAccesses.find(BB);
695 return It == PerBlockAccesses.end() ? nullptr : It->second.get();
698 // This is used by the use optimizer and updater.
699 DefsList *getWritableBlockDefs(const BasicBlock *BB) const {
700 auto It = PerBlockDefs.find(BB);
701 return It == PerBlockDefs.end() ? nullptr : It->second.get();
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.
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,
712 SmallPtrSetImpl<BasicBlock *> &Visited) {
713 renamePass(DT->getNode(BB), IncomingVal, Visited, true, true);
715 void removeFromLookups(MemoryAccess *);
716 void removeFromLists(MemoryAccess *, bool ShouldDelete = true);
717 void insertIntoListsForBlock(MemoryAccess *, const BasicBlock *,
719 void insertIntoListsBefore(MemoryAccess *, const BasicBlock *,
720 AccessList::iterator);
721 MemoryUseOrDef *createDefinedAccess(Instruction *, MemoryAccess *);
727 CachingWalker *getWalkerImpl();
728 void buildMemorySSA();
731 void verifyUseInDefs(MemoryAccess *, MemoryAccess *) const;
732 using AccessMap = DenseMap<const BasicBlock *, std::unique_ptr<AccessList>>;
733 using DefsMap = DenseMap<const BasicBlock *, std::unique_ptr<DefsList>>;
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 *> &,
743 const DenseMap<const BasicBlock *, unsigned int> &);
744 MemoryAccess *renameBlock(BasicBlock *, MemoryAccess *, bool);
745 void renameSuccessorPhis(BasicBlock *, MemoryAccess *, bool);
746 void renamePass(DomTreeNode *, MemoryAccess *IncomingVal,
747 SmallPtrSetImpl<BasicBlock *> &Visited,
748 bool SkipVisited = false, bool RenameAllUses = false);
749 AccessList *getOrCreateAccessList(const BasicBlock *);
750 DefsList *getOrCreateDefsList(const BasicBlock *);
751 void renumberBlock(const BasicBlock *) const;
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
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;
768 // Domination mappings
769 // Note that the numbering is local to a block, even though the map is
771 mutable SmallPtrSet<const BasicBlock *, 16> BlockNumberingValid;
772 mutable DenseMap<const MemoryAccess *, unsigned long> BlockNumbering;
774 // Memory SSA building info
775 std::unique_ptr<CachingWalker> Walker;
779 // Internal MemorySSA utils, for use by MemorySSA classes and walkers
780 class MemorySSAUtil {
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,
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.
791 class MemorySSAPrinterLegacyPass : public FunctionPass {
793 MemorySSAPrinterLegacyPass();
795 bool runOnFunction(Function &) override;
796 void getAnalysisUsage(AnalysisUsage &AU) const override;
801 /// An analysis that produces \c MemorySSA for a function.
803 class MemorySSAAnalysis : public AnalysisInfoMixin<MemorySSAAnalysis> {
804 friend AnalysisInfoMixin<MemorySSAAnalysis>;
806 static AnalysisKey Key;
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.
813 Result(std::unique_ptr<MemorySSA> &&MSSA) : MSSA(std::move(MSSA)) {}
814 MemorySSA &getMSSA() { return *MSSA.get(); }
816 std::unique_ptr<MemorySSA> MSSA;
819 Result run(Function &F, FunctionAnalysisManager &AM);
822 /// \brief Printer pass for \c MemorySSA.
823 class MemorySSAPrinterPass : public PassInfoMixin<MemorySSAPrinterPass> {
827 explicit MemorySSAPrinterPass(raw_ostream &OS) : OS(OS) {}
829 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
832 /// \brief Verifier pass for \c MemorySSA.
833 struct MemorySSAVerifierPass : PassInfoMixin<MemorySSAVerifierPass> {
834 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
837 /// \brief Legacy analysis pass which computes \c MemorySSA.
838 class MemorySSAWrapperPass : public FunctionPass {
840 MemorySSAWrapperPass();
844 bool runOnFunction(Function &) override;
845 void releaseMemory() override;
846 MemorySSA &getMSSA() { return *MSSA; }
847 const MemorySSA &getMSSA() const { return *MSSA; }
849 void getAnalysisUsage(AnalysisUsage &AU) const override;
851 void verifyAnalysis() const override;
852 void print(raw_ostream &OS, const Module *M = nullptr) const override;
855 std::unique_ptr<MemorySSA> MSSA;
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
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.
869 class MemorySSAWalker {
871 MemorySSAWalker(MemorySSA *);
872 virtual ~MemorySSAWalker() = default;
874 using MemoryAccessSet = SmallVector<MemoryAccess *, 8>;
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).
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
886 /// 1 = MemoryDef(liveOnEntry)
889 /// 2 = MemoryDef(liveOnEntry)
892 /// 3 = MemoryPhi(2, 1)
896 /// calling this API on load(%a) will return the MemoryPhi, not the MemoryDef
897 /// in the if (a) branch.
898 MemoryAccess *getClobberingMemoryAccess(const Instruction *I) {
899 MemoryAccess *MA = MSSA->getMemoryAccess(I);
900 assert(MA && "Handed an instruction that MemorySSA doesn't recognize?");
901 return getClobberingMemoryAccess(MA);
904 /// Does the same thing as getClobberingMemoryAccess(const Instruction *I),
905 /// but takes a MemoryAccess instead of an Instruction.
906 virtual MemoryAccess *getClobberingMemoryAccess(MemoryAccess *) = 0;
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).
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.
919 virtual MemoryAccess *getClobberingMemoryAccess(MemoryAccess *,
920 const MemoryLocation &) = 0;
922 /// \brief Given a memory access, invalidate anything this walker knows about
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 *) {}
929 virtual void verify(const MemorySSA *MSSA) { assert(MSSA == this->MSSA); }
932 friend class MemorySSA; // For updating MSSA pointer in MemorySSA move
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.
939 class DoNothingMemorySSAWalker final : public MemorySSAWalker {
941 // Keep the overrides below from hiding the Instruction overload of
942 // getClobberingMemoryAccess.
943 using MemorySSAWalker::getClobberingMemoryAccess;
945 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *) override;
946 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *,
947 const MemoryLocation &) override;
950 using MemoryAccessPair = std::pair<MemoryAccess *, MemoryLocation>;
951 using ConstMemoryAccessPair = std::pair<const MemoryAccess *, MemoryLocation>;
953 /// \brief Iterator base class used to implement const and non-const iterators
954 /// over the defining accesses of a MemoryAccess.
956 class memoryaccess_def_iterator_base
957 : public iterator_facade_base<memoryaccess_def_iterator_base<T>,
958 std::forward_iterator_tag, T, ptrdiff_t, T *,
960 using BaseT = typename memoryaccess_def_iterator_base::iterator_facade_base;
963 memoryaccess_def_iterator_base(T *Start) : Access(Start) {}
964 memoryaccess_def_iterator_base() = default;
966 bool operator==(const memoryaccess_def_iterator_base &Other) const {
967 return Access == Other.Access && (!Access || ArgNo == Other.ArgNo);
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.
976 BasicBlock *getPhiArgBlock() const {
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);
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
985 if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Access))
986 return MP->getIncomingValue(ArgNo);
987 return cast<MemoryUseOrDef>(Access)->getDefiningAccess();
989 using BaseT::operator++;
990 memoryaccess_def_iterator &operator++() {
991 assert(Access && "Hit end of iterator");
992 if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Access)) {
993 if (++ArgNo >= MP->getNumIncomingValues()) {
1004 T *Access = nullptr;
1008 inline memoryaccess_def_iterator MemoryAccess::defs_begin() {
1009 return memoryaccess_def_iterator(this);
1012 inline const_memoryaccess_def_iterator MemoryAccess::defs_begin() const {
1013 return const_memoryaccess_def_iterator(this);
1016 inline memoryaccess_def_iterator MemoryAccess::defs_end() {
1017 return memoryaccess_def_iterator();
1020 inline const_memoryaccess_def_iterator MemoryAccess::defs_end() const {
1021 return const_memoryaccess_def_iterator();
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 *> {
1027 using NodeRef = MemoryAccess *;
1028 using ChildIteratorType = memoryaccess_def_iterator;
1030 static NodeRef getEntryNode(NodeRef N) { return N; }
1031 static ChildIteratorType child_begin(NodeRef N) { return N->defs_begin(); }
1032 static ChildIteratorType child_end(NodeRef N) { return N->defs_end(); }
1035 template <> struct GraphTraits<Inverse<MemoryAccess *>> {
1036 using NodeRef = MemoryAccess *;
1037 using ChildIteratorType = MemoryAccess::iterator;
1039 static NodeRef getEntryNode(NodeRef N) { return N; }
1040 static ChildIteratorType child_begin(NodeRef N) { return N->user_begin(); }
1041 static ChildIteratorType child_end(NodeRef N) { return N->user_end(); }
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.
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.
1052 class upward_defs_iterator
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;
1059 upward_defs_iterator(const MemoryAccessPair &Info)
1060 : DefIterator(Info.first), Location(Info.second),
1061 OriginalAccess(Info.first) {
1062 CurrentPair.first = nullptr;
1064 WalkingPhi = Info.first && isa<MemoryPhi>(Info.first);
1065 fillInCurrentPair();
1068 upward_defs_iterator() { CurrentPair.first = nullptr; }
1070 bool operator==(const upward_defs_iterator &Other) const {
1071 return DefIterator == Other.DefIterator;
1074 BaseT::iterator::reference operator*() const {
1075 assert(DefIterator != OriginalAccess->defs_end() &&
1076 "Tried to access past the end of our iterator");
1080 using BaseT::operator++;
1081 upward_defs_iterator &operator++() {
1082 assert(DefIterator != OriginalAccess->defs_end() &&
1083 "Tried to access past the end of the iterator");
1085 if (DefIterator != OriginalAccess->defs_end())
1086 fillInCurrentPair();
1090 BasicBlock *getPhiArgBlock() const { return DefIterator.getPhiArgBlock(); }
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,
1102 if (Translator.getAddr() != Location.Ptr) {
1103 CurrentPair.second = Location.getWithNewPtr(Translator.getAddr());
1107 CurrentPair.second = Location;
1110 MemoryAccessPair CurrentPair;
1111 memoryaccess_def_iterator DefIterator;
1112 MemoryLocation Location;
1113 MemoryAccess *OriginalAccess = nullptr;
1114 bool WalkingPhi = false;
1117 inline upward_defs_iterator upward_defs_begin(const MemoryAccessPair &Pair) {
1118 return upward_defs_iterator(Pair);
1121 inline upward_defs_iterator upward_defs_end() { return upward_defs_iterator(); }
1123 inline iterator_range<upward_defs_iterator>
1124 upward_defs(const MemoryAccessPair &Pair) {
1125 return make_range(upward_defs_begin(Pair), upward_defs_end());
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.
1133 /// The UseOptimizedChain flag specifies whether to walk the clobbering
1134 /// access chain, or all the accesses.
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>
1142 struct def_chain_iterator
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) {}
1148 T operator*() const { return MA; }
1150 def_chain_iterator &operator++() {
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();
1156 MA = MUD->getDefiningAccess();
1164 bool operator==(const def_chain_iterator &O) const { return MA == O.MA; }
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!");
1177 return make_range(def_chain_iterator<T>(MA), def_chain_iterator<T>(UpTo));
1181 inline iterator_range<def_chain_iterator<T, true>> optimized_def_chain(T MA) {
1182 return make_range(def_chain_iterator<T, true>(MA),
1183 def_chain_iterator<T, true>(nullptr));
1186 } // end namespace llvm
1188 #endif // LLVM_ANALYSIS_MEMORYSSA_H