16 #ifndef LLVM_IR_METADATA_H
17 #define LLVM_IR_METADATA_H
41 #include <type_traits>
47 class ModuleSlotTracker;
62 const unsigned char SubclassID;
77 #define HANDLE_METADATA_LEAF(CLASS) CLASS##Kind,
78 #include "llvm/IR/Metadata.def"
83 : SubclassID(ID), Storage(Storage) {
84 static_assert(
sizeof(*
this) == 8,
"Metadata fields poorly packed");
121 bool IsForDebug =
false)
const;
123 bool IsForDebug =
false)
const;
135 const Module *M =
nullptr)
const;
144 return reinterpret_cast<Metadata**
>(MDs);
147 #define HANDLE_METADATA(CLASS) class CLASS;
148 #include "llvm/IR/Metadata.def"
152 #define HANDLE_METADATA_LEAF(CLASS) \
153 template <> struct isa_impl<CLASS, Metadata> { \
154 static inline bool doit(const Metadata &MD) { \
155 return MD.getMetadataID() == Metadata::CLASS##Kind; \
158 #include "llvm/IR/Metadata.def"
181 void dropUse() { MD =
nullptr; }
196 void handleChangedMetadata(
Metadata *MD);
220 return track(&MD, *MD, static_cast<Metadata *>(
nullptr));
229 return track(Ref, MD, &Owner);
238 return track(Ref, MD, &Owner);
256 return retrack(&MD, *MD, &New);
285 uint64_t NextIndex = 0;
292 assert(UseMap.empty() &&
"Cannot destroy in-use replaceable metadata");
310 void addRef(
void *Ref,
OwnerTy Owner);
311 void dropRef(
void *Ref);
312 void moveRef(
void *Ref,
void *New,
const Metadata &MD);
326 static bool isReplaceable(
const Metadata &MD);
351 assert(V &&
"Expected valid value");
360 return cast<ConstantAsMetadata>(
get(
C));
364 return cast<LocalAsMetadata>(
get(Local));
370 return cast_or_null<ConstantAsMetadata>(
getIfExists(C));
374 return cast_or_null<LocalAsMetadata>(
getIfExists(Local));
430 assert(!isa<Constant>(Local) &&
"Expected local value");
499 template <
class T>
T &
make();
505 template <
class U,
class V>
510 sizeof(hasDereference<T, Result>(
nullptr)) ==
sizeof(
Yes);
513 static const bool value = std::is_base_of<Constant, V>::value &&
517 static const bool value = std::is_base_of<Constant, V>::value &&
518 std::is_convertible<M, const Metadata &>::value;
527 template <
class X,
class Y>
528 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
bool>::type
530 assert(MD &&
"Null pointer sent into hasa");
531 if (
auto *V = dyn_cast<ConstantAsMetadata>(MD))
532 return isa<X>(V->getValue());
535 template <
class X,
class Y>
537 typename std::enable_if<detail::IsValidReference<X, Y &>::value,
bool>::type
545 template <
class X,
class Y>
546 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
548 return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
550 template <
class X,
class Y>
552 typename std::enable_if<detail::IsValidReference<X, Y &>::value,
X *>::type
561 template <
class X,
class Y>
562 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
564 if (
auto *V = cast_or_null<ConstantAsMetadata>(MD))
565 return cast<X>(V->getValue());
574 template <
class X,
class Y>
575 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
577 if (
auto *V = dyn_cast<ConstantAsMetadata>(MD))
587 template <
class X,
class Y>
588 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
590 if (
auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
746 assert(static_cast<void *>(
this) == &MD &&
"Expected same address");
774 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
775 : Ptr(ReplaceableUses.
release()) {
818 assert(ReplaceableUses &&
"Expected non-null replaceable uses");
820 "Expected same context");
822 Ptr = ReplaceableUses.release();
830 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
832 Ptr = &ReplaceableUses->getContext();
833 return ReplaceableUses;
841 #define HANDLE_MDNODE_LEAF(CLASS) \
842 using Temp##CLASS = std::unique_ptr<CLASS, TempMDNodeDeleter>;
843 #define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS)
844 #include "llvm/IR/Metadata.def"
866 unsigned NumOperands;
867 unsigned NumUnresolved;
876 void *
operator new(
size_t Size,
unsigned NumOps);
877 void operator delete(
void *Mem);
903 void *
operator new(
size_t) =
delete;
914 TempMDNode
clone()
const;
949 if (
Context.hasReplaceableUses())
950 Context.getReplaceableUses()->replaceAllUsesWith(MD);
966 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>::type
968 return cast<T>(N.release()->replaceWithPermanentImpl());
978 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>::type
980 return cast<T>(N.release()->replaceWithUniquedImpl());
988 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>::type
990 return cast<T>(N.release()->replaceWithDistinctImpl());
994 MDNode *replaceWithPermanentImpl();
995 MDNode *replaceWithUniquedImpl();
996 MDNode *replaceWithDistinctImpl();
1005 template <
class T,
class StoreT>
1010 void handleChangedOperand(
void *Ref,
Metadata *New);
1016 void dropReplaceableUses();
1019 void decrementUnresolvedOperandCount();
1020 void countUnresolvedOperands();
1033 void makeDistinct();
1035 void deleteAsSubclass();
1037 void eraseFromStore();
1039 template <
class NodeTy>
struct HasCachedHash;
1040 template <
class NodeTy>
1041 static void dispatchRecalculateHash(NodeTy *
N, std::true_type) {
1042 N->recalculateHash();
1044 template <
class NodeTy>
1045 static void dispatchRecalculateHash(NodeTy *, std::false_type) {}
1046 template <
class NodeTy>
1047 static void dispatchResetHash(NodeTy *N, std::true_type) {
1050 template <
class NodeTy>
1051 static void dispatchResetHash(NodeTy *, std::false_type) {}
1068 assert(I < NumOperands &&
"Out of range");
1080 #define HANDLE_MDNODE_LEAF(CLASS) \
1083 #include "llvm/IR/Metadata.def"
1110 :
MDNode(C, MDTupleKind, Storage, Vals) {
1117 void recalculateHash();
1119 static MDTuple *
getImpl(LLVMContext &
Context, ArrayRef<Metadata *> MDs,
1122 TempMDTuple cloneImpl()
const {
1157 TempMDTuple
clone()
const {
return cloneImpl(); }
1191 : std::iterator<std::input_iterator_tag, T *, std::ptrdiff_t, void, T *> {
1229 typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
1236 typename std::enable_if<!std::is_convertible<U *, T *>::value>::type * =
1240 explicit operator bool()
const {
return get(); }
1241 explicit operator MDTuple *()
const {
return get(); }
1248 unsigned size()
const {
return N ? N->getNumOperands() : 0u; }
1249 bool empty()
const {
return N ? N->getNumOperands() == 0 :
true; }
1250 T *
operator[](
unsigned I)
const {
return cast_or_null<T>(N->getOperand(I)); }
1259 #define HANDLE_METADATA(CLASS) \
1260 using CLASS##Array = MDTupleTypedArrayWrapper<CLASS>;
1261 #include "llvm/IR/Metadata.def"
1321 Module *Parent =
nullptr;
1324 void setParent(
Module *M) { Parent = M; }
1328 template<
class T1,
class T2>
1329 class op_iterator_impl :
1330 public std::iterator<std::bidirectional_iterator_tag, T2> {
1336 op_iterator_impl(
const NamedMDNode *N,
unsigned i) : Node(N), Idx(i) {}
1339 op_iterator_impl() =
default;
1341 bool operator==(
const op_iterator_impl &o)
const {
return Idx == o.Idx; }
1342 bool operator!=(
const op_iterator_impl &o)
const {
return Idx != o.Idx; }
1344 op_iterator_impl &operator++() {
1349 op_iterator_impl operator++(
int) {
1350 op_iterator_impl tmp(*
this);
1355 op_iterator_impl &operator--() {
1360 op_iterator_impl operator--(
int) {
1361 op_iterator_impl tmp(*
this);
1366 T1 operator*()
const {
return Node->getOperand(Idx); }
1370 NamedMDNode(
const NamedMDNode &) =
delete;
1392 bool IsForDebug =
false)
const;
1418 #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).
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
Attribute unwrap(LLVMAttributeRef Attr)
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++()
op_iterator_impl< const MDNode *, MDNode > const_op_iterator
#define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)
MDString & operator=(MDString &&)=delete
APInt operator*(APInt a, uint64_t RHS)
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
iterator_range< op_iterator > op_range
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
static MDNode * intersect(MDNode *A, MDNode *B)
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()
iterator_range< MDOperand * > mutable_op_range
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()
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)
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.
op_iterator_impl< MDNode *, MDNode > op_iterator
static SimpleType getSimplifiedValue(MDOperand &MD)
static MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)
TempMDNode clone() const
Create a (temporary) clone of this.
Metadata & operator*() const
LLVMContext & getContext() const
All values hold a context through their type.
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.
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
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Pointer to the context, with optional RAUW support.
llvm_unreachable("Unknown RegBank!")
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()
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
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
struct LLVMOpaqueMetadata * LLVMMetadataRef
Represents an LLVM Metadata.
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)
AAMDNodes intersect(const AAMDNodes &Other)
Given two sets of AAMDNodes that apply to the same pointer, give the best AAMDNodes that are compatib...
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)
TypedMDOperandIterator< T > iterator
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...