72 #ifndef LLVM_ANALYSIS_MEMORYSSA_H 73 #define LLVM_ANALYSIS_MEMORYSSA_H 114 class MemorySSAWalker;
118 namespace MSSAHelpers {
140 public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>,
141 public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>> {
151 void *
operator new(
size_t) =
delete;
157 return ID == MemoryUseVal || ID == MemoryPhiVal || ID == MemoryDefVal;
180 return this->AllAccessType::getIterator();
183 return this->AllAccessType::getIterator();
186 return this->AllAccessType::getReverseIterator();
189 return this->AllAccessType::getReverseIterator();
192 return this->DefsOnlyType::getIterator();
195 return this->DefsOnlyType::getIterator();
198 return this->DefsOnlyType::getReverseIterator();
201 return this->DefsOnlyType::getReverseIterator();
217 inline unsigned getID()
const;
221 :
DerivedUser(
Type::getVoidTy(C), Vty, nullptr, NumOperands, DeleteValue),
250 void *
operator new(
size_t) =
delete;
266 inline bool isOptimized()
const;
273 return OptimizedAccessAlias;
279 inline void resetOptimized();
287 unsigned NumOperands)
289 MemoryInstruction(MI), OptimizedAccessAlias(
MayAlias) {
290 setDefiningAccess(DMA);
297 OptimizedAccessAlias = AR;
307 setOptimizedAccessType(AR);
329 void *
operator new(
size_t s) {
return User::operator
new(s, 1); }
338 OptimizedID = DMA->
getID();
343 return getDefiningAccess() && OptimizedID == getDefiningAccess()->getID();
347 return getDefiningAccess();
384 : MemoryUseOrDef(C, DMA, MemoryDefVal, deleteMe, MI, BB,
389 void *
operator new(
size_t s) {
return User::operator
new(s, 2); }
397 OptimizedID = MA->
getID();
401 return cast_or_null<MemoryAccess>(getOperand(1));
405 return getOptimized() && OptimizedID == getOptimized()->getID();
410 setOperand(1,
nullptr);
431 if (
auto *MU = dyn_cast<MemoryUse>(MUD))
437 if (
auto *MU = dyn_cast<MemoryUse>(MUD))
443 if (
const auto *MU = dyn_cast<MemoryUse>(MUD))
484 void *
operator new(
size_t s) {
return User::operator
new(s); }
492 ReservedSpace(NumPreds) {
493 allocHungoffUses(ReservedSpace);
502 auto *
Ref =
reinterpret_cast<Use::UserRef *
>(op_begin() + ReservedSpace);
508 reinterpret_cast<const Use::UserRef *
>(op_begin() + ReservedSpace);
515 return block_begin() + getNumOperands();
519 return make_range(block_begin(), block_end());
523 return make_range(block_begin(), block_end());
536 assert(V &&
"PHI node got a null value!");
549 assert(
this == U.
getUser() &&
"Iterator doesn't point to PHI's Uses?");
550 return getIncomingBlock(
unsigned(&U - op_begin()));
556 return getIncomingBlock(I.getUse());
560 assert(BB &&
"PHI node got a null basic block!");
561 block_begin()[
I] = BB;
566 if (getNumOperands() == ReservedSpace)
569 setNumHungOffUseOperands(getNumOperands() + 1);
570 setIncomingValue(getNumOperands() - 1, V);
571 setIncomingBlock(getNumOperands() - 1, BB);
577 for (
unsigned I = 0,
E = getNumOperands();
I !=
E; ++
I)
578 if (block_begin()[
I] == BB)
584 int Idx = getBasicBlockIndex(BB);
585 assert(Idx >= 0 &&
"Invalid basic block argument!");
586 return getIncomingValue(Idx);
591 unsigned E = getNumOperands();
592 assert(I < E &&
"Cannot remove out of bounds Phi entry.");
595 assert(E >= 2 &&
"Cannot only remove incoming values in MemoryPhis with " 596 "at least 2 values.");
597 setIncomingValue(I, getIncomingValue(E - 1));
598 setIncomingBlock(I, block_begin()[E - 1]);
599 setOperand(E - 1,
nullptr);
600 block_begin()[E - 1] =
nullptr;
601 setNumHungOffUseOperands(getNumOperands() - 1);
607 for (
unsigned I = 0,
E = getNumOperands();
I !=
E; ++
I)
608 if (Pred(getIncomingValue(
I), getIncomingBlock(
I))) {
609 unorderedDeleteIncoming(
I);
610 E = getNumOperands();
613 assert(getNumOperands() >= 1 &&
614 "Cannot remove all incoming blocks in a MemoryPhi.");
619 unorderedDeleteIncomingIf(
626 unorderedDeleteIncomingIf(
651 unsigned ReservedSpace;
655 void growOperands() {
656 unsigned E = getNumOperands();
658 ReservedSpace =
std::max(E + E / 2, 2u);
659 growHungoffUses(ReservedSpace,
true);
666 assert((isa<MemoryDef>(
this) || isa<MemoryPhi>(
this)) &&
667 "only memory defs and phis have ids");
668 if (
const auto *MD = dyn_cast<MemoryDef>(
this))
670 return cast<MemoryPhi>(
this)->getID();
674 if (
const auto *MD = dyn_cast<MemoryDef>(
this))
675 return MD->isOptimized();
676 return cast<MemoryUse>(
this)->isOptimized();
680 if (
const auto *MD = dyn_cast<MemoryDef>(
this))
681 return MD->getOptimized();
682 return cast<MemoryUse>(
this)->getOptimized();
686 if (
auto *MD = dyn_cast<MemoryDef>(
this))
687 MD->setOptimized(MA);
689 cast<MemoryUse>(
this)->setOptimized(MA);
693 if (
auto *MD = dyn_cast<MemoryDef>(
this))
694 MD->resetOptimized();
696 cast<MemoryUse>(
this)->resetOptimized();
722 return cast_or_null<MemoryUseOrDef>(ValueToMemoryAccess.lookup(I));
726 return cast_or_null<MemoryPhi>(ValueToMemoryAccess.lookup(cast<Value>(BB)));
740 return MA == LiveOnEntryDef.get();
744 return LiveOnEntryDef.get();
760 return getWritableBlockAccesses(BB);
768 return getWritableBlockDefs(BB);
785 void verifyMemorySSA()
const;
797 void verifyOrderingDominationAndDefUses(
Function &
F)
const;
798 void verifyDominationNumbers(
const Function &
F)
const;
799 void verifyPrevDefInPhis(
Function &
F)
const;
803 auto It = PerBlockAccesses.find(BB);
804 return It == PerBlockAccesses.end() ? nullptr : It->second.get();
809 auto It = PerBlockDefs.find(BB);
810 return It == PerBlockDefs.
end() ? nullptr : It->second.get();
823 renamePass(DT->getNode(BB), IncomingVal, Visited,
true,
true);
827 void removeFromLists(
MemoryAccess *,
bool ShouldDelete =
true);
831 AccessList::iterator);
834 bool CreationMustSucceed =
true);
837 template <
class AliasAnalysisType>
class ClobberWalkerBase;
838 template <
class AliasAnalysisType>
class CachingWalker;
839 template <
class AliasAnalysisType>
class SkipSelfWalker;
842 CachingWalker<AliasAnalysis> *getWalkerImpl();
854 void markUnreachableAsLiveOnEntry(
BasicBlock *BB);
857 template <
typename AliasAnalysisType>
866 bool SkipVisited =
false,
bool RenameAllUses =
false);
885 std::unique_ptr<MemoryAccess, ValueDeleter> LiveOnEntryDef;
894 std::unique_ptr<ClobberWalkerBase<AliasAnalysis>> WalkerBase;
895 std::unique_ptr<CachingWalker<AliasAnalysis>> Walker;
896 std::unique_ptr<SkipSelfWalker<AliasAnalysis>> SkipWalker;
907 static bool defClobbersUseOrDef(MemoryDef *MD,
const MemoryUseOrDef *MU,
935 Result(std::unique_ptr<MemorySSA> &&MSSA) : MSSA(
std::move(MSSA)) {}
939 std::unique_ptr<MemorySSA>
MSSA;
971 void releaseMemory()
override;
977 void verifyAnalysis()
const override;
981 std::unique_ptr<MemorySSA> MSSA;
1026 assert(MA &&
"Handed an instruction that MemorySSA doesn't recognize?");
1027 return getClobberingMemoryAccess(MA);
1082 std::forward_iterator_tag, T, ptrdiff_t, T *,
1084 using BaseT =
typename memoryaccess_def_iterator_base::iterator_facade_base;
1091 return Access == Other.Access && (!Access || ArgNo == Other.ArgNo);
1101 MemoryPhi *MP =
dyn_cast<MemoryPhi>(Access);
1102 assert(MP &&
"Tried to get phi arg block when not iterating over a PHI");
1107 assert(Access &&
"Tried to access past the end of our iterator");
1110 if (
const MemoryPhi *MP = dyn_cast<MemoryPhi>(Access))
1111 return MP->getIncomingValue(ArgNo);
1112 return cast<MemoryUseOrDef>(Access)->getDefiningAccess();
1115 using BaseT::operator++;
1117 assert(Access &&
"Hit end of iterator");
1118 if (
const MemoryPhi *MP = dyn_cast<MemoryPhi>(Access)) {
1119 if (++ArgNo >= MP->getNumIncomingValues()) {
1130 T *Access =
nullptr;
1180 std::forward_iterator_tag,
1181 const MemoryAccessPair> {
1182 using BaseT = upward_defs_iterator::iterator_facade_base;
1187 OriginalAccess(Info.
first) {
1188 CurrentPair.first =
nullptr;
1190 WalkingPhi = Info.first && isa<MemoryPhi>(Info.first);
1191 fillInCurrentPair();
1197 return DefIterator == Other.DefIterator;
1201 assert(DefIterator != OriginalAccess->defs_end() &&
1202 "Tried to access past the end of our iterator");
1206 using BaseT::operator++;
1208 assert(DefIterator != OriginalAccess->defs_end() &&
1209 "Tried to access past the end of the iterator");
1211 if (DefIterator != OriginalAccess->defs_end())
1212 fillInCurrentPair();
1219 void fillInCurrentPair() {
1220 CurrentPair.first = *DefIterator;
1221 if (WalkingPhi && Location.Ptr) {
1223 const_cast<Value *>(Location.Ptr),
1224 OriginalAccess->getBlock()->getModule()->getDataLayout(),
nullptr);
1225 if (!Translator.PHITranslateValue(OriginalAccess->getBlock(),
1226 DefIterator.getPhiArgBlock(),
nullptr,
1228 if (Translator.getAddr() != Location.Ptr) {
1229 CurrentPair.second = Location.getWithNewPtr(Translator.getAddr());
1233 CurrentPair.second = Location;
1240 bool WalkingPhi =
false;
1267 template <
class T,
bool UseOptimizedChain = false>
1270 std::forward_iterator_tag, MemoryAccess *> {
1278 if (
auto *MUD = dyn_cast<MemoryUseOrDef>(MA)) {
1279 if (UseOptimizedChain && MUD->isOptimized())
1280 MA = MUD->getOptimized();
1282 MA = MUD->getDefiningAccess();
1299 #ifdef EXPENSIVE_CHECKS 1301 "UpTo isn't in the def chain!");
1314 #endif // LLVM_ANALYSIS_MEMORYSSA_H
AccessList * getWritableBlockAccesses(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(MemoryAccess::const_user_iterator I) const
Return incoming basic block corresponding to value use iterator.
unsigned getValueID() const
Return an ID for the concrete type of this object.
void unorderedDeleteIncomingValue(const MemoryAccess *MA)
This class represents lattice values for constants.
void unorderedDeleteIncomingIf(Fn &&Pred)
Result(std::unique_ptr< MemorySSA > &&MSSA)
AllAccessType::self_iterator getIterator()
Get the iterators for the all access list and the defs only list We default to the all access list...
BasicBlock * getIncomingBlock(const Use &U) const
Return incoming basic block corresponding to an operand of the PHI.
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
MemorySSAPrinterPass(raw_ostream &OS)
A Module instance is used to store all the information related to an LLVM module. ...
const MemorySSA & getMSSA() const
const AccessList * getBlockAccesses(const BasicBlock *BB) const
Return the list of MemoryAccess's for a given basic block.
BasicBlock *const * const_block_iterator
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
MemoryAccess * getOptimized() const
Extension point for the Value hierarchy.
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
BaseT::iterator::reference operator*() const
void setOptimized(MemoryAccess *)
void deleteValue()
Delete a pointer to a generic Value.
BasicBlock * getPhiArgBlock() const
memoryaccess_def_iterator defs_begin()
This iterator walks over all of the defs in a given MemoryAccess.
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
void(*)(DerivedUser *) DeleteValueTy
This defines the Use class.
AllAccessType::const_self_iterator getIterator() const
DefsOnlyType::self_iterator getDefsIterator()
void setIncomingValue(unsigned I, MemoryAccess *V)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
MemoryPhi * getMemoryAccess(const BasicBlock *BB) const
Represents read-only accesses to memory.
This class is a batch walker of all MemoryUse's in the program, and points their defining access at t...
Legacy analysis pass which computes MemorySSA.
void resetOptimized()
Reset the ID of what this MemoryUse was optimized to, causing it to be rewalked by the walker if nece...
void renamePass(BasicBlock *BB, MemoryAccess *IncomingVal, SmallPtrSetImpl< BasicBlock *> &Visited)
block_iterator block_begin()
A Use represents the edge between a Value definition and its users.
Encapsulates MemorySSA, including all data associated with memory accesses.
The access may reference the value stored in memory.
const DefsList * getBlockDefs(const BasicBlock *BB) const
Return the list of MemoryDef's and MemoryPhi's for a given basic block.
const_block_iterator block_end() const
Walks the defining accesses of MemoryDefs.
def_chain_iterator & operator++()
BaseT::iterator::pointer operator*() const
static bool classof(const Value *MA)
iterator_range< block_iterator > blocks()
A simple intrusive list implementation.
#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.
static ChildIteratorType child_end(NodeRef N)
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
MemoryAccess * getOptimized() const
AllAccessType::const_reverse_self_iterator getReverseIterator() const
upward_defs_iterator upward_defs_end()
std::unique_ptr< MemorySSA > MSSA
void unorderedDeleteIncoming(unsigned I)
A CRTP mix-in to automatically provide informational APIs needed for passes.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
This is the generic walker interface for walkers of MemorySSA.
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
op_range incoming_values()
memoryaccess_def_iterator defs_end()
Analysis containing CSE Info
MemoryAccess * getIncomingValue(unsigned I) const
Return incoming value number x.
const_block_iterator block_begin() const
An assembly annotator class to print Memory SSA information in comments.
Use delete by default for iplist and ilist.
static bool runOnFunction(Function &F, bool PostInlining)
static Use * op_end(MemoryUseOrDef *MUD)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
A set of analyses that are preserved following a run of a transformation pass.
DefsOnlyType::const_reverse_self_iterator getReverseDefsIterator() const
AllAccessType::reverse_self_iterator getReverseIterator()
LLVM Basic Block Representation.
PointerIntPair - This class implements a pair of a pointer and small integer.
PHITransAddr - An address value which tracks and handles phi translation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
const_op_range incoming_values() const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
upward_defs_iterator & operator++()
void addIncoming(MemoryAccess *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
early cse Early CSE w MemorySSA
A CRTP mix-in that provides informational APIs needed for analysis passes.
static unsigned getIncomingValueNumForOperand(unsigned I)
void setIncomingBlock(unsigned I, BasicBlock *BB)
upward_defs_iterator(const MemoryAccessPair &Info)
InsertionPlace
Used in various insertion functions to specify whether we are talking about the beginning or end of a...
Represent the analysis usage information of a pass.
iterator_range< def_chain_iterator< T, true > > optimized_def_chain(T MA)
void print(raw_ostream &OS) const
Printer pass for MemorySSA.
static unsigned getOperandNumForIncomingValue(unsigned I)
FunctionPass class - This class is used to implement most global optimizations.
static bool classof(const Value *MA)
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
MemoryAccess::iterator ChildIteratorType
static unsigned operands(const MemoryUseOrDef *MUD)
BasicBlock * getPhiArgBlock() const
virtual void invalidateInfo(MemoryAccess *)
Given a memory access, invalidate anything this walker knows about that access.
Iterator base class used to implement const and non-const iterators over the defining accesses of a M...
#define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS)
Macro for generating in-class operand accessor declarations.
Provide an iterator that walks defs, giving both the memory access, and the current pointer location...
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
An intrusive list with ownership and callbacks specified/controlled by ilist_traits, only with API safe for polymorphic types.
MemoryAccess * getOptimized() const
A MemorySSAWalker that does no alias queries, or anything else.
std::pair< const MemoryAccess *, MemoryLocation > ConstMemoryAccessPair
static void deleteNode(MemoryAccess *MA)
bool operator==(const memoryaccess_def_iterator_base &Other) const
The two locations may or may not alias. This is the least precise result.
Representation for a specific memory location.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void setDefiningAccess(MemoryAccess *DMA, bool Optimized=false, Optional< AliasResult > AR=MayAlias)
Iterator for intrusive lists based on ilist_node.
static bool classof(const Value *V)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
static NodeRef getEntryNode(NodeRef N)
Align max(MaybeAlign Lhs, Align Rhs)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
memoryaccess_def_iterator_base & operator++()
An analysis that produces MemorySSA for a function.
BasicBlock * getBlock() const
void unorderedDeleteIncomingBlock(const BasicBlock *BB)
MemoryUseOrDef(LLVMContext &C, MemoryAccess *DMA, unsigned Vty, DeleteValueTy DeleteValue, Instruction *MI, BasicBlock *BB, unsigned NumOperands)
MemoryAccess * getLiveOnEntryDef() const
Verifier pass for MemorySSA.
A range adaptor for a pair of iterators.
MemoryPhi(LLVMContext &C, BasicBlock *BB, unsigned Ver, unsigned NumPreds=0)
static Use * op_begin(MemoryUseOrDef *MUD)
Class that has the common methods + fields of memory uses/defs.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
BasicBlock * getIncomingBlock(unsigned I) const
Return incoming basic block number i.
Instruction * getMemoryInst() const
Get the instruction that this MemoryUse represents.
iterator_range< const_block_iterator > blocks() const
user_iterator_impl< const User > const_user_iterator
DefsOnlyType::const_self_iterator getDefsIterator() const
void setOptimizedAccessType(Optional< AliasResult > AR)
iterator_range< def_chain_iterator< T > > def_chain(T MA, MemoryAccess *UpTo=nullptr)
memoryaccess_def_iterator_base< MemoryAccess > memoryaccess_def_iterator
cl::opt< bool > EnableMSSALoopDependency
Enables memory ssa as a dependency for loop passes.
block_iterator block_end()
bool operator==(const def_chain_iterator &O) const
This file provides utility analysis objects describing memory locations.
user_iterator_impl< User > user_iterator
MemoryAccess * getIncomingValueForBlock(const BasicBlock *BB) const
Compile-time customization of User operands.
MemoryAccess(LLVMContext &C, unsigned Vty, DeleteValueTy DeleteValue, BasicBlock *BB, unsigned NumOperands)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
Optional< AliasResult > getOptimizedAccessType() const
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
static ChildIteratorType child_begin(NodeRef N)
API to communicate dependencies between analyses during invalidation.
MemoryAccess * getClobberingMemoryAccess(const Instruction *I)
Given a memory Mod/Ref/ModRef'ing instruction, calling this will give you the nearest dominating Memo...
static ChildIteratorType child_begin(NodeRef N)
static bool classof(const Value *MA)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
void setOptimized(MemoryAccess *MA)
static NodeRef getEntryNode(NodeRef N)
LLVM Value Representation.
MemoryDef(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB, unsigned Ver)
DefsOnlyType::reverse_self_iterator getReverseDefsIterator()
upward_defs_iterator upward_defs_begin(const MemoryAccessPair &Pair)
HungoffOperandTraits - determine the allocation regime of the Use array when it is not a prefix to th...
void allocHungoffUses(unsigned N, bool IsPhi=false)
Allocate the array of Uses, followed by a pointer (with bottom bit set) to the User.
unsigned getID() const
Used for debugging and tracking things about MemoryAccesses.
memoryaccess_def_iterator_base(T *Start)
This class implements an extremely fast bulk output stream that can only output to a stream...
const_user_iterator const_iterator
FixedNumOperandTraits - determine the allocation regime of the Use array when it is a prefix to the U...
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.
MemoryUse(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB)
static bool classof(const Value *V)
iterator_range< upward_defs_iterator > upward_defs(const MemoryAccessPair &Pair)
Represents phi nodes for memory accesses.
static ChildIteratorType child_end(NodeRef N)
DefsList * getWritableBlockDefs(const BasicBlock *BB) const
user_iterator iterator
The user iterators for a memory access.
memoryaccess_def_iterator_base< const MemoryAccess > const_memoryaccess_def_iterator
bool operator==(const upward_defs_iterator &Other) const
A special type used by analysis passes to provide an address that identifies that particular analysis...
void setOptimized(MemoryAccess *DMA)
std::pair< MemoryAccess *, MemoryLocation > MemoryAccessPair
void allocHungoffUses(unsigned N)
this is more complicated than the generic User::allocHungoffUses, because we have to allocate Uses fo...