16 #ifndef LLVM_IR_METADATA_H
17 #define LLVM_IR_METADATA_H
27 #include <type_traits>
33 class ModuleSlotTracker;
35 template<
typename ValueSubClass,
typename ItemParentClass>
36 class SymbolTableListTraits;
49 const unsigned char SubclassID;
131 const Module *M =
nullptr)
const;
143 const Module *M =
nullptr)
const;
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"
182 void dropUse() { MD =
nullptr; }
194 void handleChangedMetadata(
Metadata *MD);
217 : Context(Context), NextIndex(0) {}
219 assert(UseMap.empty() &&
"Cannot destroy in-use replaceable metadata");
237 void addRef(
void *Ref,
OwnerTy Owner);
238 void dropRef(
void *Ref);
239 void moveRef(
void *Ref,
void *New,
const Metadata &MD);
266 assert(V &&
"Expected valid value");
273 return cast<ConstantAsMetadata>(
get(
C));
276 return cast<LocalAsMetadata>(
get(
Local));
281 return cast_or_null<ConstantAsMetadata>(
getIfExists(C));
284 return cast_or_null<LocalAsMetadata>(
getIfExists(Local));
339 assert(!isa<Constant>(Local) &&
"Expected local value");
406 template <
class T>
T &
make();
412 template <
class U,
class V>
417 sizeof(hasDereference<T, Result>(
nullptr)) ==
sizeof(
Yes);
420 static const bool value = std::is_base_of<Constant, V>::value &&
424 static const bool value = std::is_base_of<Constant, V>::value &&
425 std::is_convertible<M, const Metadata &>::value;
433 template <
class X,
class Y>
434 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
bool>::type
436 assert(MD &&
"Null pointer sent into hasa");
437 if (
auto *V = dyn_cast<ConstantAsMetadata>(MD))
438 return isa<X>(V->getValue());
441 template <
class X,
class Y>
443 typename std::enable_if<detail::IsValidReference<X, Y &>::value,
bool>::type
451 template <
class X,
class Y>
452 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
454 return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
456 template <
class X,
class Y>
458 typename std::enable_if<detail::IsValidReference<X, Y &>::value,
X *>::type
467 template <
class X,
class Y>
468 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
470 if (
auto *V = cast_or_null<ConstantAsMetadata>(MD))
471 return cast<X>(V->getValue());
480 template <
class X,
class Y>
481 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
483 if (
auto *V = dyn_cast<ConstantAsMetadata>(MD))
493 template <
class X,
class Y>
494 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value,
X *>::type
496 if (
auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
520 static MDString *
get(LLVMContext &Context, StringRef Str);
522 return get(Context, Str ?
StringRef(Str) : StringRef());
634 assert(static_cast<void *>(
this) == &MD &&
"Expected same address");
667 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
668 : Ptr(ReplaceableUses.release()) {
696 assert(ReplaceableUses &&
"Expected non-null replaceable uses");
697 assert(&ReplaceableUses->getContext() == &
getContext() &&
698 "Expected same context");
700 Ptr = ReplaceableUses.release();
708 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
710 Ptr = &ReplaceableUses->getContext();
711 return ReplaceableUses;
719 #define HANDLE_MDNODE_LEAF(CLASS) \
720 typedef std::unique_ptr<CLASS, TempMDNodeDeleter> Temp##CLASS;
721 #define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS)
722 #include "llvm/IR/Metadata.def"
745 void operator=(
const MDNode &) =
delete;
746 void *
operator new(size_t) =
delete;
748 unsigned NumOperands;
749 unsigned NumUnresolved;
754 void *
operator new(
size_t Size,
unsigned NumOps);
755 void operator delete(
void *Mem);
791 TempMDNode
clone()
const;
825 assert(
isTemporary() &&
"Expected temporary node");
826 assert(!
isResolved() &&
"Expected RAUW support");
843 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>::type
845 return cast<T>(N.release()->replaceWithPermanentImpl());
855 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>::type
857 return cast<T>(N.release()->replaceWithUniquedImpl());
865 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>::type
867 return cast<T>(N.release()->replaceWithDistinctImpl());
871 MDNode *replaceWithPermanentImpl();
872 MDNode *replaceWithUniquedImpl();
873 MDNode *replaceWithDistinctImpl();
882 template <
class T,
class StoreT>
886 void handleChangedOperand(
void *Ref,
Metadata *New);
890 void decrementUnresolvedOperandCount();
891 unsigned countUnresolvedOperands();
906 void deleteAsSubclass();
908 void eraseFromStore();
910 template <
class NodeTy>
struct HasCachedHash;
911 template <
class NodeTy>
912 static void dispatchRecalculateHash(NodeTy *
N, std::true_type) {
913 N->recalculateHash();
915 template <
class NodeTy>
916 static void dispatchRecalculateHash(NodeTy *N, std::false_type) {}
917 template <
class NodeTy>
918 static void dispatchResetHash(NodeTy *N, std::true_type) {
921 template <
class NodeTy>
922 static void dispatchResetHash(NodeTy *N, std::false_type) {}
937 assert(I < NumOperands &&
"Out of range");
949 #define HANDLE_MDNODE_LEAF(CLASS) \
952 #include "llvm/IR/Metadata.def"
984 void recalculateHash();
986 static MDTuple *getImpl(LLVMContext &
Context, ArrayRef<Metadata *> MDs,
989 TempMDTuple cloneImpl()
const {
1002 return getImpl(Context, MDs,
Uniqued,
false);
1009 return getImpl(Context, MDs,
Distinct);
1019 return TempMDTuple(getImpl(Context, MDs,
Temporary));
1023 TempMDTuple
clone()
const {
return cloneImpl(); }
1054 : std::iterator<std::input_iterator_tag, T *, std::ptrdiff_t, void, T *> {
1088 typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
1095 typename std::enable_if<!std::is_convertible<U *, T *>::value>::type * =
1099 explicit operator bool()
const {
return get(); }
1100 explicit operator MDTuple *()
const {
return get(); }
1107 unsigned size()
const {
return N ? N->getNumOperands() : 0u; }
1108 T *
operator[](
unsigned I)
const {
return cast_or_null<T>(N->getOperand(I)); }
1116 #define HANDLE_METADATA(CLASS) \
1117 typedef MDTupleTypedArrayWrapper<CLASS> CLASS##Array;
1118 #include "llvm/IR/Metadata.def"
1138 void setParent(
Module *M) { Parent = M; }
1142 template<
class T1,
class T2>
1143 class op_iterator_impl :
1144 public std::iterator<std::bidirectional_iterator_tag, T2> {
1147 op_iterator_impl(
const NamedMDNode *N,
unsigned i) : Node(N), Idx(i) { }
1149 friend class NamedMDNode;
1152 op_iterator_impl() : Node(nullptr), Idx(0) { }
1154 bool operator==(
const op_iterator_impl &o)
const {
return Idx == o.Idx; }
1155 bool operator!=(
const op_iterator_impl &o)
const {
return Idx != o.Idx; }
1156 op_iterator_impl &operator++() {
1160 op_iterator_impl operator++(
int) {
1161 op_iterator_impl tmp(*
this);
1165 op_iterator_impl &operator--() {
1169 op_iterator_impl operator--(
int) {
1170 op_iterator_impl tmp(*
this);
1175 T1 operator*()
const {
return Node->getOperand(Idx); }
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).
size_t size() const
size - Get the string size.
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.
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()
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 print(raw_ostream &ROS) const
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.
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.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const unsigned char * bytes_begin() const
Typed iterator through MDNode operands.
MDTuple & operator*() const
TypedMDOperandIterator & operator++()
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()(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)
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a temporary node.
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
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
const MDOperand * op_iterator
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
This is an important base class in LLVM.
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.
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)
unsigned getHash() const
Get the hash, if any.
StringRef getString() const
const MDOperand & getOperand(unsigned I) const
BlockMass operator*(const BlockMass &L, const BranchProbability &R)
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)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
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)
static AAMDNodes getTombstoneKey()
ilist_node - Base class that provides next/prev services for nodes that use ilist_nextprev_traits or ...
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.
Metadata * operator->() const
op_iterator_impl< const MDNode *, MDNode > const_op_iterator
op_range operands() const
LLVMContext & getContext() const
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
iterator_range< const_op_iterator > operands() const
const unsigned char * bytes_begin() const
LLVM Value Representation.
const_op_iterator op_end() const
void reset(Metadata *MD, Metadata *Owner)
ContextAndReplaceableUses Context
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...
std::string Hash(const Unit &U)
void dropAllReferences()
Remove all uses and clear node vector.
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 MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
bool isResolved() const
Check if node is fully resolved.
MDTupleTypedArrayWrapper()=default
TypedMDOperandIterator(MDNode::op_iterator I)
Module * getParent()
Get the module that holds this named metadata collection.
PointerUnion - This implements a discriminated union of two pointer types, and keeps the discriminato...