72 #ifndef LLVM_ANALYSIS_MEMORYSSA_H
73 #define LLVM_ANALYSIS_MEMORYSSA_H
112 namespace MSSAHelpers {
132 public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>,
133 public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>> {
144 return ID == MemoryUseVal || ID == MemoryPhiVal || ID == MemoryDefVal;
150 void *
operator new(
size_t) =
delete;
209 inline unsigned getID()
const;
213 :
DerivedUser(
Type::getVoidTy(C), Vty, nullptr, NumOperands, DeleteValue),
234 void *
operator new(
size_t) =
delete;
264 :
MemoryAccess(C, Vty, DeleteValue, BB, 1), MemoryInst(MI) {
294 : MemoryUseOrDef(C, DMA, MemoryUseVal, deleteMe, MI, BB),
298 void *
operator new(
size_t s) {
return User::operator
new(s, 1); }
307 OptimizedID = DMA->
getID();
312 return getDefiningAccess() && OptimizedID == getDefiningAccess()->getID();
316 return getDefiningAccess();
328 unsigned int OptimizedID;
350 : MemoryUseOrDef(C, DMA, MemoryDefVal, deleteMe, MI, BB),
354 void *
operator new(
size_t s) {
return User::operator
new(s, 1); }
362 OptimizedID = getDefiningAccess()->
getID();
366 return getOptimized() && getDefiningAccess() &&
367 OptimizedID == getDefiningAccess()->getID();
384 unsigned int OptimizedID;
425 void *
operator new(
size_t s) {
return User::operator
new(s); }
432 : MemoryAccess(C, MemoryPhiVal, deleteMe, BB, 0),
ID(Ver),
433 ReservedSpace(NumPreds) {
434 allocHungoffUses(ReservedSpace);
443 auto *Ref =
reinterpret_cast<Use::UserRef *
>(op_begin() + ReservedSpace);
444 return reinterpret_cast<block_iterator
>(Ref + 1);
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(); }
456 return block_begin() + getNumOperands();
460 return make_range(block_begin(), block_end());
464 return make_range(block_begin(), block_end());
477 assert(V &&
"PHI node got a null value!");
489 assert(
this == U.
getUser() &&
"Iterator doesn't point to PHI's Uses?");
490 return getIncomingBlock(
unsigned(&U - op_begin()));
496 return getIncomingBlock(I.getUse());
500 assert(BB &&
"PHI node got a null basic block!");
501 block_begin()[
I] = BB;
506 if (getNumOperands() == ReservedSpace)
509 setNumHungOffUseOperands(getNumOperands() + 1);
510 setIncomingValue(getNumOperands() - 1, V);
511 setIncomingBlock(getNumOperands() - 1, BB);
517 for (
unsigned I = 0,
E = getNumOperands();
I !=
E; ++
I)
518 if (block_begin()[
I] == BB)
524 int Idx = getBasicBlockIndex(BB);
525 assert(Idx >= 0 &&
"Invalid basic block argument!");
526 return getIncomingValue(Idx);
550 unsigned ReservedSpace;
554 void growOperands() {
555 unsigned E = getNumOperands();
557 ReservedSpace =
std::max(E + E / 2, 2u);
558 growHungoffUses(ReservedSpace,
true);
561 static void deleteMe(DerivedUser *Self);
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();
573 if (
const auto *MD = dyn_cast<MemoryDef>(
this))
574 return MD->isOptimized();
579 if (
const auto *MD = dyn_cast<MemoryDef>(
this))
580 return MD->getOptimized();
585 if (
auto *MD = dyn_cast<MemoryDef>(
this))
586 MD->setOptimized(MA);
592 if (
auto *MD = dyn_cast<MemoryDef>(
this))
593 MD->resetOptimized();
611 MemorySSAWalker *getWalker();
631 return MA == LiveOnEntryDef.get();
635 return LiveOnEntryDef.get();
651 return getWritableBlockAccesses(BB);
659 return getWritableBlockDefs(BB);
676 void verifyMemorySSA()
const;
689 void verifyDomination(
Function &
F)
const;
694 auto It = PerBlockAccesses.find(BB);
695 return It == PerBlockAccesses.end() ?
nullptr : It->second.get();
700 auto It = PerBlockDefs.find(BB);
701 return It == PerBlockDefs.
end() ?
nullptr : It->second.get();
713 renamePass(DT->getNode(BB), IncomingVal, Visited,
true,
true);
716 void removeFromLists(
MemoryAccess *,
bool ShouldDelete =
true);
720 AccessList::iterator);
727 CachingWalker *getWalkerImpl();
728 void buildMemorySSA();
737 void markUnreachableAsLiveOnEntry(
BasicBlock *BB);
748 bool SkipVisited =
false,
bool RenameAllUses =
false);
749 AccessList *getOrCreateAccessList(
const BasicBlock *);
750 DefsList *getOrCreateDefsList(
const BasicBlock *);
764 AccessMap PerBlockAccesses;
765 DefsMap PerBlockDefs;
766 std::unique_ptr<MemoryAccess> LiveOnEntryDef;
775 std::unique_ptr<CachingWalker> Walker;
816 std::unique_ptr<MemorySSA>
MSSA;
855 std::unique_ptr<MemorySSA> MSSA;
900 assert(MA &&
"Handed an instruction that MemorySSA doesn't recognize?");
958 std::forward_iterator_tag, T, ptrdiff_t, T *,
960 using BaseT =
typename memoryaccess_def_iterator_base::iterator_facade_base;
967 return Access == Other.Access && (!Access || ArgNo == Other.ArgNo);
978 assert(MP &&
"Tried to get phi arg block when not iterating over a PHI");
982 assert(Access &&
"Tried to access past the end of our iterator");
985 if (
MemoryPhi *MP = dyn_cast<MemoryPhi>(Access))
986 return MP->getIncomingValue(ArgNo);
987 return cast<MemoryUseOrDef>(Access)->getDefiningAccess();
989 using BaseT::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;
1054 std::forward_iterator_tag,
1055 const MemoryAccessPair> {
1056 using BaseT = upward_defs_iterator::iterator_facade_base;
1061 OriginalAccess(Info.
first) {
1062 CurrentPair.first =
nullptr;
1064 WalkingPhi = Info.first && isa<MemoryPhi>(Info.first);
1065 fillInCurrentPair();
1071 return DefIterator == Other.DefIterator;
1076 "Tried to access past the end of our iterator");
1080 using BaseT::operator++;
1083 "Tried to access past the end of the iterator");
1085 if (DefIterator != OriginalAccess->
defs_end())
1086 fillInCurrentPair();
1093 void fillInCurrentPair() {
1094 CurrentPair.first = *DefIterator;
1095 if (WalkingPhi && Location.
Ptr) {
1097 const_cast<Value *>(Location.
Ptr),
1099 if (!Translator.PHITranslateValue(OriginalAccess->
getBlock(),
1102 if (Translator.getAddr() != Location.
Ptr) {
1103 CurrentPair.second = Location.
getWithNewPtr(Translator.getAddr());
1107 CurrentPair.second = Location;
1112 MemoryLocation Location;
1113 MemoryAccess *OriginalAccess =
nullptr;
1114 bool WalkingPhi =
false;
1123 inline iterator_range<upward_defs_iterator>
1141 template <
class T,
bool UseOptimizedChain = false>
1144 std::forward_iterator_tag, MemoryAccess *> {
1152 if (
auto *MUD = dyn_cast<MemoryUseOrDef>(MA)) {
1153 if (UseOptimizedChain && MUD->isOptimized())
1154 MA = MUD->getOptimized();
1156 MA = MUD->getDefiningAccess();
1171 inline iterator_range<def_chain_iterator<T>>
1173 #ifdef EXPENSIVE_CHECKS
1175 "UpTo isn't in the def chain!");
1188 #endif // LLVM_ANALYSIS_MEMORYSSA_H
MemoryAccess * getOptimized() const
memoryaccess_def_iterator & operator++()
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *) override
Does the same thing as getClobberingMemoryAccess(const Instruction *I), but takes a MemoryAccess inst...
BasicBlock *const * const_block_iterator
MemoryAccess * getOptimized() const
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
virtual void verify(const MemorySSA *MSSA)
MemoryAccess & operator=(const MemoryAccess &)=delete
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...
bool operator==(const memoryaccess_def_iterator_base &Other) const
MemorySSAPrinterPass(raw_ostream &OS)
A Module instance is used to store all the information related to an LLVM module. ...
Result run(Function &F, FunctionAnalysisManager &AM)
AllAccessType::const_self_iterator getIterator() const
BaseT::iterator::reference operator*() const
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.
Extension point for the Value hierarchy.
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
unsigned getID() const
Used for debugging and tracking things about MemoryAccesses.
void setOptimized(MemoryAccess *)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
memoryaccess_def_iterator defs_begin()
This iterator walks over all of the defs in a given MemoryAccess.
const_block_iterator block_end() const
MemoryAccess(const MemoryAccess &)=delete
BasicBlock * getPhiArgBlock() const
void(*)(DerivedUser *) DeleteValueTy
This defines the Use class.
DefsOnlyType::self_iterator getDefsIterator()
BasicBlock * getIncomingBlock(MemoryAccess::const_user_iterator I) const
Return incoming basic block corresponding to value use iterator.
const MemorySSA & getMSSA() const
void setIncomingValue(unsigned I, MemoryAccess *V)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
const_op_range incoming_values() const
Represents read-only accesses to memory.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
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...
block_iterator block_begin()
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
A Use represents the edge between a Value definition and its users.
AccessList * getWritableBlockAccesses(const BasicBlock *BB) const
Encapsulates MemorySSA, including all data associated with memory accesses.
MemoryUseOrDef * getMemoryAccess(const Instruction *) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
MemoryLocation getWithNewPtr(const Value *NewPtr) const
Walks the defining accesses of MemoryDefs.
def_chain_iterator & operator++()
MemoryUseOrDef(LLVMContext &C, MemoryAccess *DMA, unsigned Vty, DeleteValueTy DeleteValue, Instruction *MI, BasicBlock *BB)
static bool classof(const Value *MA)
iterator_range< block_iterator > blocks()
A simple intrusive list implementation.
BasicBlock * getBlock() const
#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)
DefsOnlyType::const_reverse_self_iterator getReverseDefsIterator() const
upward_defs_iterator upward_defs_end()
std::unique_ptr< MemorySSA > MSSA
A CRTP mix-in to automatically provide informational APIs needed for passes.
Base class for the actual dominator tree node.
const_block_iterator block_begin() const
bool runOnFunction(Function &) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
memoryaccess_def_iterator_base()=default
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()
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
An assembly annotator class to print Memory SSA information in comments.
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
void renamePass(BasicBlock *BB, MemoryAccess *IncomingVal, SmallPtrSetImpl< BasicBlock * > &Visited)
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
A set of analyses that are preserved following a run of a transformation pass.
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.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
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.
upward_defs_iterator & operator++()
void addIncoming(MemoryAccess *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
bool operator==(const def_chain_iterator &O) const
early cse Early CSE w MemorySSA
A CRTP mix-in that provides informational APIs needed for analysis passes.
static unsigned getIncomingValueNumForOperand(unsigned I)
bool runOnFunction(Function &) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
MemorySSAWalker(MemorySSA *)
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)
BasicBlock ** block_iterator
Printer pass for MemorySSA.
unsigned getValueID() const
Return an ID for the concrete type of this object.
static const unsigned End
static unsigned getOperandNumForIncomingValue(unsigned I)
FunctionPass class - This class is used to implement most global optimizations.
static bool classof(const Value *MA)
Value * getOperand(unsigned i) const
MemoryAccess::iterator ChildIteratorType
self_iterator getIterator()
MemoryAccess * getIncomingValue(unsigned I) const
Return incoming value number x.
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.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Provide an iterator that walks defs, giving both the memory access, and the current pointer location...
An intrusive list with ownership and callbacks specified/controlled by ilist_traits, only with API safe for polymorphic types.
A MemorySSAWalker that does no alias queries, or anything else.
MemoryAccess * getLiveOnEntryDef() const
std::pair< const MemoryAccess *, MemoryLocation > ConstMemoryAccessPair
iterator_range< const_block_iterator > blocks() const
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
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)
static bool defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU, AliasAnalysis &AA)
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...
virtual ~MemorySSAWalker()=default
void print(raw_ostream &OS) const
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.
An analysis that produces MemorySSA for a function.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool operator==(const upward_defs_iterator &Other) const
DefsOnlyType::const_self_iterator getDefsIterator() const
void setOperand(unsigned i, Value *Val)
Verifier pass for MemorySSA.
A range adaptor for a pair of iterators.
MemoryPhi(LLVMContext &C, BasicBlock *BB, unsigned Ver, unsigned NumPreds=0)
Class that has the common methods + fields of memory uses/defs.
BasicBlock * getPhiArgBlock() const
user_iterator_impl< const User > const_user_iterator
BasicBlock * getIncomingBlock(unsigned I) const
Return incoming basic block number i.
user_iterator iterator
The user iterators for a memory access.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess)
iterator_range< def_chain_iterator< T > > def_chain(T MA, MemoryAccess *UpTo=nullptr)
reverse_self_iterator getReverseIterator()
memoryaccess_def_iterator_base< MemoryAccess > memoryaccess_def_iterator
block_iterator block_end()
This file provides utility analysis objects describing memory locations.
void setDefiningAccess(MemoryAccess *DMA, bool Optimized=false)
user_iterator_impl< User > user_iterator
BasicBlock * getIncomingBlock(const Use &U) const
Return incoming basic block corresponding to an operand of the PHI.
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)
MemorySSAPrinterLegacyPass()
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
static ChildIteratorType child_begin(NodeRef N)
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)
Instruction * getMemoryInst() const
Get the instruction that this MemoryUse represents.
static bool classof(const Value *MA)
MemoryAccess * getOptimized() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
void setOptimized(MemoryAccess *MA)
static NodeRef getEntryNode(NodeRef N)
LLVM Value Representation.
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
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.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
memoryaccess_def_iterator_base(T *Start)
This class implements an extremely fast bulk output stream that can only output to a stream...
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)
DefsList * getWritableBlockDefs(const BasicBlock *BB) const
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)
const AccessList * getBlockAccesses(const BasicBlock *BB) const
Return the list of MemoryAccess's for a given basic block.
AllAccessType::const_reverse_self_iterator getReverseIterator() const
memoryaccess_def_iterator_base< const MemoryAccess > const_memoryaccess_def_iterator
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
const_user_iterator const_iterator
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
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
void allocHungoffUses(unsigned N)
this is more complicated than the generic User::allocHungoffUses, because we have to allocate Uses fo...