16 #ifndef LLVM_IR_METADATA_H
17 #define LLVM_IR_METADATA_H
40 #include <type_traits>
46 class ModuleSlotTracker;
59 const unsigned char SubclassID;
74 #define HANDLE_METADATA_LEAF(CLASS) CLASS##Kind,
75 #include "llvm/IR/Metadata.def"
81 static_assert(
sizeof(*
this) == 8,
"Metdata fields poorly packed");
118 bool IsForDebug =
false)
const;
120 bool IsForDebug =
false)
const;
132 const Module *M =
nullptr)
const;
136 #define HANDLE_METADATA(CLASS) class CLASS;
137 #include "llvm/IR/Metadata.def"
141 #define HANDLE_METADATA_LEAF(CLASS) \
142 template <> struct isa_impl<CLASS, Metadata> { \
143 static inline bool doit(const Metadata &MD) { \
144 return MD.getMetadataID() == Metadata::CLASS##Kind; \
147 #include "llvm/IR/Metadata.def"
171 void dropUse() { MD =
nullptr; }
183 void handleChangedMetadata(
Metadata *MD);
207 return track(&MD, *MD, static_cast<Metadata *>(
nullptr));
216 return track(Ref, MD, &Owner);
225 return track(Ref, MD, &Owner);
243 return retrack(&MD, *MD, &New);
277 : Context(Context), NextIndex(0) {}
280 assert(UseMap.empty() &&
"Cannot destroy in-use replaceable metadata");
298 void addRef(
void *Ref,
OwnerTy Owner);
299 void dropRef(
void *Ref);
300 void moveRef(
void *Ref,
void *New,
const Metadata &MD);
314 static bool isReplaceable(
const Metadata &MD);
339 assert(V &&
"Expected valid value");
347 return cast<ConstantAsMetadata>(
get(
C));
350 return cast<LocalAsMetadata>(
get(
Local));
355 return cast_or_null<ConstantAsMetadata>(
getIfExists(C));
358 return cast_or_null<LocalAsMetadata>(
getIfExists(Local));
414 assert(!isa<Constant>(Local) &&
"Expected local value");
483 template <
class T>
T &
make();
489 template <
class U,
class V>
494 sizeof(hasDereference<T, Result>(
nullptr)) ==
sizeof(
Yes);
497 static const bool value = std::is_base_of<Constant, V>::value &&
501 static const bool value = std::is_base_of<Constant, V>::value &&
502 std::is_convertible<M, const Metadata &>::value;
511 template <
class X,
class Y>
512 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
bool>::type
514 assert(MD &&
"Null pointer sent into hasa");
515 if (
auto *V = dyn_cast<ConstantAsMetadata>(MD))
516 return isa<X>(V->getValue());
519 template <
class X,
class Y>
521 typename std::enable_if<detail::IsValidReference<X, Y &>::value,
bool>::type
529 template <
class X,
class Y>
530 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
532 return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
534 template <
class X,
class Y>
536 typename std::enable_if<detail::IsValidReference<X, Y &>::value,
X *>::type
545 template <
class X,
class Y>
546 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
548 if (
auto *V = cast_or_null<ConstantAsMetadata>(MD))
549 return cast<X>(V->getValue());
558 template <
class X,
class Y>
559 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
561 if (
auto *V = dyn_cast<ConstantAsMetadata>(MD))
571 template <
class X,
class Y>
572 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
574 if (
auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
716 assert(static_cast<void *>(
this) == &MD &&
"Expected same address");
742 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
743 : Ptr(ReplaceableUses.
release()) {
786 assert(ReplaceableUses &&
"Expected non-null replaceable uses");
788 "Expected same context");
790 Ptr = ReplaceableUses.release();
798 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
800 Ptr = &ReplaceableUses->getContext();
801 return ReplaceableUses;
809 #define HANDLE_MDNODE_LEAF(CLASS) \
810 typedef std::unique_ptr<CLASS, TempMDNodeDeleter> Temp##CLASS;
811 #define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS)
812 #include "llvm/IR/Metadata.def"
834 unsigned NumOperands;
835 unsigned NumUnresolved;
840 void *
operator new(
size_t Size,
unsigned NumOps);
841 void operator delete(
void *Mem);
870 void *
operator new(size_t) =
delete;
881 TempMDNode
clone()
const;
916 if (
Context.hasReplaceableUses())
917 Context.getReplaceableUses()->replaceAllUsesWith(MD);
933 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>::type
935 return cast<T>(N.release()->replaceWithPermanentImpl());
945 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>::type
947 return cast<T>(N.release()->replaceWithUniquedImpl());
955 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>::type
957 return cast<T>(N.release()->replaceWithDistinctImpl());
961 MDNode *replaceWithPermanentImpl();
962 MDNode *replaceWithUniquedImpl();
963 MDNode *replaceWithDistinctImpl();
972 template <
class T,
class StoreT>
977 void handleChangedOperand(
void *Ref,
Metadata *New);
983 void dropReplaceableUses();
986 void decrementUnresolvedOperandCount();
987 void countUnresolvedOperands();
1000 void makeDistinct();
1002 void deleteAsSubclass();
1004 void eraseFromStore();
1006 template <
class NodeTy>
struct HasCachedHash;
1007 template <
class NodeTy>
1008 static void dispatchRecalculateHash(NodeTy *
N, std::true_type) {
1009 N->recalculateHash();
1011 template <
class NodeTy>
1012 static void dispatchRecalculateHash(NodeTy *, std::false_type) {}
1013 template <
class NodeTy>
1014 static void dispatchResetHash(NodeTy *N, std::true_type) {
1017 template <
class NodeTy>
1018 static void dispatchResetHash(NodeTy *, std::false_type) {}
1035 assert(I < NumOperands &&
"Out of range");
1047 #define HANDLE_MDNODE_LEAF(CLASS) \
1050 #include "llvm/IR/Metadata.def"
1078 :
MDNode(C, MDTupleKind, Storage, Vals) {
1085 void recalculateHash();
1087 static MDTuple *
getImpl(LLVMContext &
Context, ArrayRef<Metadata *> MDs,
1090 TempMDTuple cloneImpl()
const {
1125 TempMDTuple
clone()
const {
return cloneImpl(); }
1159 : std::iterator<std::input_iterator_tag, T *, std::ptrdiff_t, void, T *> {
1197 typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
1204 typename std::enable_if<!std::is_convertible<U *, T *>::value>::type * =
1208 explicit operator bool()
const {
return get(); }
1209 explicit operator MDTuple *()
const {
return get(); }
1216 unsigned size()
const {
return N ? N->getNumOperands() : 0u; }
1217 T *
operator[](
unsigned I)
const {
return cast_or_null<T>(N->getOperand(I)); }
1225 #define HANDLE_METADATA(CLASS) \
1226 typedef MDTupleTypedArrayWrapper<CLASS> CLASS##Array;
1227 #include "llvm/IR/Metadata.def"
1290 void setParent(
Module *M) { Parent = M; }
1294 template<
class T1,
class T2>
1295 class op_iterator_impl :
1296 public std::iterator<std::bidirectional_iterator_tag, T2> {
1300 op_iterator_impl(
const NamedMDNode *N,
unsigned i) : Node(N), Idx(i) { }
1302 friend class NamedMDNode;
1305 op_iterator_impl() =
default;
1307 bool operator==(
const op_iterator_impl &o)
const {
return Idx == o.Idx; }
1308 bool operator!=(
const op_iterator_impl &o)
const {
return Idx != o.Idx; }
1310 op_iterator_impl &operator++() {
1315 op_iterator_impl operator++(
int) {
1316 op_iterator_impl tmp(*
this);
1321 op_iterator_impl &operator--() {
1326 op_iterator_impl operator--(
int) {
1327 op_iterator_impl tmp(*
this);
1332 T1 operator*()
const {
return Node->getOperand(Idx); }
1336 NamedMDNode(
const NamedMDNode &) =
delete;
1358 bool IsForDebug =
false)
const;
1382 #endif // LLVM_IR_METADATA_H
void dropAllReferences()
Remove all uses and clear node vector.
LLVMContext & getContext() const
StringRef getName() const
static void deleteTemporary(MDNode *N)
Deallocate a node created by getTemporary.
unsigned getLength() const
Tracking metadata reference owned by Metadata.
bool hasReplaceableUses() const
Whether this contains RAUW support.
MDTupleTypedArrayWrapper(const MDTupleTypedArrayWrapper< U > &Other, typename std::enable_if< std::is_convertible< U *, T * >::value >::type *=nullptr)
ContextAndReplaceableUses(std::unique_ptr< ReplaceableMetadataImpl > ReplaceableUses)
static bool classof(const Metadata *MD)
const Module * getParent() const
MDNode * Scope
The tag for alias scope specification (used with noalias).
iterator_range< op_iterator > op_range
MDNode * TBAA
The tag for type-based alias analysis.
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
TypedMDOperandIterator operator++(int)
static std::enable_if< std::is_base_of< MDNode, T >::value, T * >::type replaceWithPermanent(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a permanent one.
A Module instance is used to store all the information related to an LLVM module. ...
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
bool operator==(const AAMDNodes &A) const
MDTuple * operator->() const
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
void clearOperands()
Drop all references to this node's operands.
std::unique_ptr< ReplaceableMetadataImpl > takeReplaceableUses()
Drop RAUW support.
unsigned getNumOperands() const
Return number of MDNode operands.
static MDTuple * getIfExists(LLVMContext &Context, ArrayRef< Metadata * > MDs)
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
static MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
void addOperand(MDNode *M)
ContextAndReplaceableUses(LLVMContext &Context)
void storeDistinctInContext()
const unsigned char * bytes_end() const
~ContextAndReplaceableUses()
MDOperand & operator=(MDOperand &&)=delete
op_iterator op_begin() const
MDTupleTypedArrayWrapper(const MDTuple *N)
Manage lifetime of a slot tracker for printing IR.
static T * storeImpl(T *N, StorageType Storage, StoreT &Store)
Typed, array-like tuple of metadata.
void setOperand(unsigned I, Metadata *New)
Set an operand.
bool operator==(const TypedMDOperandIterator &X) const
void setOperand(unsigned I, MDNode *New)
void replaceUseWith(Metadata *MD)
Replace the use of this with MD.
op_iterator op_end() const
static std::enable_if< std::is_base_of< MDNode, T >::value, T * >::type replaceWithDistinct(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a distinct one.
static AAMDNodes getEmptyKey()
iterator end() const
Pointer to one byte past the end of the string.
DistinctMDOperandPlaceholder()=delete
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
T * operator[](unsigned I) const
void makeReplaceable(std::unique_ptr< ReplaceableMetadataImpl > ReplaceableUses)
Assign RAUW support to this.
const unsigned char * bytes_end() const
static Yes & hasDereference(SFINAE< sizeof(static_cast< V >(*make< U >()))> *=0)
void eraseFromParent()
Drop all references and remove the node from parent module.
A Use represents the edge between a Value definition and its users.
const unsigned char * bytes_begin() const
Typed iterator through MDNode operands.
MDTuple & operator*() const
Placeholder metadata for operands of distinct MDNodes.
TypedMDOperandIterator & operator++()
MDString & operator=(MDString &&)=delete
void resolveCycles()
Resolve cycles.
MDTupleTypedArrayWrapper(const MDTupleTypedArrayWrapper< U > &Other, typename std::enable_if<!std::is_convertible< U *, T * >::value >::type *=nullptr)
bool isTBAAVtableAccess() const
Check whether MDNode is a vtable access.
static bool classof(const Metadata *MD)
Methods for support type inquiry through isa, cast, and dyn_cast:
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
void operator=(const MDNode &)=delete
void operator()(MDNode *Node) const
bool operator!=(const TypedMDOperandIterator &X) const
StringRef::iterator iterator
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
static MDNode * intersect(MDNode *A, MDNode *B)
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a temporary node.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
iterator_range< op_iterator > operands()
static MDNode * getMostGenericRange(MDNode *A, MDNode *B)
AAMDNodes(MDNode *T=nullptr, MDNode *S=nullptr, MDNode *N=nullptr)
static std::enable_if< std::is_base_of< MDNode, T >::value, T * >::type replaceWithUniqued(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a uniqued one.
MDOperand * mutable_end()
iterator_range< MDOperand * > mutable_op_range
ContextAndReplaceableUses()=delete
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.
bool operator!=(const AAMDNodes &A) const
static MDNode * getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B)
const MDOperand * op_iterator
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
This is an important base class in LLVM.
ContextAndReplaceableUses & operator=(ContextAndReplaceableUses &&)=delete
TypedMDOperandIterator()=default
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
MDNode * getOperand(unsigned i) const
unsigned getValueID() const
Return an ID for the concrete type of this object.
static SimpleType getSimplifiedValue(MDOperand &MD)
static MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)
TempMDNode clone() const
Create a (temporary) clone of this.
BlockMass operator*(BlockMass L, BranchProbability R)
Metadata & operator*() const
LLVMContext & getContext() const
All values hold a context through their type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
mutable_op_range mutable_operands()
std::enable_if< detail::IsValidPointer< X, Y >::value, X * >::type extract(Y &&MD)
Extract a Value from Metadata.
static bool classof(const Metadata *MD)
Methods for support type inquiry through isa, cast, and dyn_cast.
static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
unsigned getHash() const
Get the hash, if any.
StringRef getString() const
const MDOperand & getOperand(unsigned I) const
Type * getType() const
All values are typed, get the type of this value.
static MDTuple * getIfExists(LLVMContext &Context, ArrayRef< Metadata * > MDs)
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
TempMDTuple clone() const
Return a (temporary) clone of this.
MDNode * NoAlias
The tag specifying the noalias scope.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
A range adaptor for a pair of iterators.
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
std::enable_if< detail::IsValidPointer< X, Y >::value, X * >::type extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
std::enable_if< detail::IsValidPointer< X, Y >::value, bool >::type hasa(Y &&MD)
Check whether Metadata has a Value.
const_op_iterator op_begin() const
Pointer to the context, with optional RAUW support.
MDOperand * mutable_begin()
std::enable_if< detail::IsValidPointer< X, Y >::value, X * >::type dyn_extract_or_null(Y &&MD)
Extract a Value from Metadata, if any, allowing null.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
bool operator!=(uint64_t V1, const APInt &V2)
DistinctMDOperandPlaceholder(unsigned ID)
static AAMDNodes getTombstoneKey()
TypedMDOperandIterator< T > iterator
op_iterator_impl< MDNode *, MDNode > op_iterator
static SimpleType getSimplifiedValue(const MDOperand &MD)
static MDNode * concatenate(MDNode *A, MDNode *B)
Methods for metadata merging.
iterator begin() const
Pointer to the first byte of the string.
MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, ArrayRef< Metadata * > Ops1, ArrayRef< Metadata * > Ops2=None)
Metadata * operator->() const
op_iterator_impl< const MDNode *, MDNode > const_op_iterator
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
op_range operands() const
LLVMContext & getContext() const
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
iterator_range< const_op_iterator > operands() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ReplaceableMetadataImpl * getOrCreateReplaceableUses()
Ensure that this has RAUW support, and then return it.
const unsigned char * bytes_begin() const
LLVM Value Representation.
const_op_iterator op_end() const
void reset(Metadata *MD, Metadata *Owner)
static unsigned getHashValue(const AAMDNodes &Val)
unsigned getNumOperands() const
This class implements an extremely fast bulk output stream that can only output to a stream...
void print(raw_ostream &ROS, bool IsForDebug=false) const
std::string Hash(const Unit &U)
StringRef - Represent a constant reference to a string, i.e.
std::enable_if< detail::IsValidPointer< X, Y >::value, X * >::type dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
ReplaceableMetadataImpl * getReplaceableUses() const
bool operator==(uint64_t V1, const APInt &V2)
static LazyValueInfoImpl & getImpl(void *&PImpl, AssumptionCache *AC, const DataLayout *DL, DominatorTree *DT=nullptr)
This lazily constructs the LazyValueInfoImpl.
static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
bool isResolved() const
Check if node is fully resolved.
MDTupleTypedArrayWrapper()=default
TypedMDOperandIterator(MDNode::op_iterator I)
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
Module * getParent()
Get the module that holds this named metadata collection.
~DistinctMDOperandPlaceholder()
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...