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);
258 static bool retrack(
void *Ref,
Metadata &MD,
void *New);
261 static bool isReplaceable(
const Metadata &MD);
285 uint64_t NextIndex = 0;
292 assert(UseMap.
empty() &&
"Cannot destroy in-use replaceable metadata");
300 void replaceAllUsesWith(
Metadata *MD);
307 void resolveAllUses(
bool ResolveUsers =
true);
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));
381 static void handleDeletion(
Value *V);
382 static void handleRAUW(
Value *From,
Value *To);
430 assert(!isa<Constant>(Local) &&
"Expected local value");
499 template <
class T>
T &
make();
505 template <
class U,
class V>
506 static Yes &hasDereference(
SFINAE<
sizeof(static_cast<V>(*make<U>()))> * = 0);
507 template <
class U,
class V>
static No &hasDereference(...);
509 static const bool value =
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))
653 explicit operator bool()
const {
return TBAA || Scope ||
NoAlias; }
671 Result.
TBAA = Other.
TBAA == TBAA ? TBAA :
nullptr;
672 Result.
Scope = Other.
Scope == Scope ? Scope :
nullptr;
746 assert(static_cast<void *>(
this) == &MD &&
"Expected same address");
774 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
775 : Ptr(ReplaceableUses.
release()) {
776 assert(getReplaceableUses() &&
"Expected non-null replaceable uses");
794 if (hasReplaceableUses())
795 return getReplaceableUses()->getContext();
800 if (hasReplaceableUses())
807 if (!hasReplaceableUses())
808 makeReplaceable(llvm::make_unique<ReplaceableMetadataImpl>(getContext()));
809 return getReplaceableUses();
818 assert(ReplaceableUses &&
"Expected non-null replaceable uses");
819 assert(&ReplaceableUses->getContext() == &getContext() &&
820 "Expected same context");
821 delete getReplaceableUses();
822 Ptr = ReplaceableUses.release();
829 assert(hasReplaceableUses() &&
"Expected to own replaceable uses");
830 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
831 getReplaceableUses());
832 Ptr = &ReplaceableUses->getContext();
833 return ReplaceableUses;
838 inline void operator()(
MDNode *Node)
const;
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);
889 void dropAllReferences();
902 void operator=(
const MDNode &) =
delete;
903 void *
operator new(
size_t) =
delete;
910 static inline TempMDTuple getTemporary(
LLVMContext &Context,
914 TempMDNode clone()
const;
920 static void deleteTemporary(
MDNode *
N);
925 void replaceOperandWith(
unsigned I,
Metadata *New);
938 bool isResolved()
const {
return !isTemporary() && !NumUnresolved; }
948 assert(isTemporary() &&
"Expected temporary node");
949 if (Context.hasReplaceableUses())
950 Context.getReplaceableUses()->replaceAllUsesWith(MD);
959 void resolveCycles();
969 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>
::type 971 return cast<T>(N.release()->replaceWithPermanentImpl());
981 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>
::type 983 return cast<T>(N.release()->replaceWithUniquedImpl());
991 static typename std::enable_if<std::is_base_of<MDNode, T>::value,
T *>
::type 993 return cast<T>(N.release()->replaceWithDistinctImpl());
997 MDNode *replaceWithPermanentImpl();
998 MDNode *replaceWithUniquedImpl();
999 MDNode *replaceWithDistinctImpl();
1005 void setOperand(
unsigned I,
Metadata *New);
1007 void storeDistinctInContext();
1008 template <
class T,
class StoreT>
1010 template <
class T>
static T *storeImpl(
T *N,
StorageType Storage);
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) {}
1058 return const_cast<MDNode *
>(
this)->mutable_begin();
1062 return const_cast<MDNode *
>(
this)->mutable_end();
1068 assert(I < NumOperands &&
"Out of range");
1069 return op_begin()[
I];
1080 #define HANDLE_MDNODE_LEAF(CLASS) \ 1083 #include "llvm/IR/Metadata.def" 1088 bool isTBAAVtableAccess()
const;
1110 :
MDNode(C, MDTupleKind, Storage, Vals) {
1114 ~
MDTuple() { dropAllReferences(); }
1117 void recalculateHash();
1122 TempMDTuple cloneImpl()
const {
1123 return getTemporary(getContext(),
1157 TempMDTuple
clone()
const {
return cloneImpl(); }
1191 :
public std::iterator<std::input_iterator_tag, T *, std::ptrdiff_t, void,
1230 typename std::enable_if<std::is_convertible<U *, T *>::value>::
type * =
1237 typename std::enable_if<!std::is_convertible<U *, T *>::value>::
type * =
1241 explicit operator bool()
const {
return get(); }
1242 explicit operator MDTuple *()
const {
return get(); }
1260 #define HANDLE_METADATA(CLASS) \ 1261 using CLASS##Array = MDTupleTypedArrayWrapper<CLASS>; 1262 #include "llvm/IR/Metadata.def" 1312 assert(!Use &&
"Use is still being tracked despite being untracked!");
1328 Module *Parent =
nullptr;
1331 void setParent(
Module *M) { Parent = M; }
1335 template<
class T1,
class T2>
1336 class op_iterator_impl :
1337 public std::iterator<std::bidirectional_iterator_tag, T2> {
1343 op_iterator_impl(
const NamedMDNode *
N,
unsigned i) : Node(N), Idx(i) {}
1346 op_iterator_impl() =
default;
1348 bool operator==(
const op_iterator_impl &o)
const {
return Idx == o.Idx; }
1349 bool operator!=(
const op_iterator_impl &o)
const {
return Idx != o.Idx; }
1351 op_iterator_impl &operator++() {
1356 op_iterator_impl operator++(
int) {
1357 op_iterator_impl tmp(*
this);
1362 op_iterator_impl &operator--() {
1367 op_iterator_impl operator--(
int) {
1368 op_iterator_impl tmp(*
this);
1381 void eraseFromParent();
1386 void clearOperands();
1392 MDNode *getOperand(
unsigned i)
const;
1393 unsigned getNumOperands()
const;
1395 void setOperand(
unsigned I,
MDNode *New);
1399 bool IsForDebug =
false)
const;
1425 #endif // LLVM_IR_METADATA_H void dropAllReferences()
Remove all uses and clear node vector.
static void deleteTemporary(MDNode *N)
Deallocate a node created by getTemporary.
Tracking metadata reference owned by Metadata.
ContextAndReplaceableUses(std::unique_ptr< ReplaceableMetadataImpl > ReplaceableUses)
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool classof(const Metadata *MD)
unsigned getValueID() const
Return an ID for the concrete type of this object.
MDNode * Scope
The tag for alias scope specification (used with noalias).
const unsigned char * bytes_end() const
MDNode * getOperand(unsigned i) const
Compute iterated dominance frontiers using a linear time algorithm.
MDNode * TBAA
The tag for type-based alias analysis.
const unsigned char * bytes_end() const
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. ...
iterator begin() const
Pointer to the first byte of the string.
unsigned getLength() const
std::unique_ptr< ReplaceableMetadataImpl > takeReplaceableUses()
Drop RAUW support.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Metadata * operator->() const
The two locations do not alias at all.
ContextAndReplaceableUses(LLVMContext &Context)
~ContextAndReplaceableUses()
LLVMContext & getContext() const
All values hold a context through their type.
MDTupleTypedArrayWrapper(const MDTuple *N)
const MDOperand & getOperand(unsigned I) const
LLVMContext & getContext() const
Manage lifetime of a slot tracker for printing IR.
Typed, array-like tuple of metadata.
void replaceUseWith(Metadata *MD)
Replace the use of this with MD.
op_iterator op_end() const
bool operator!=(const AAMDNodes &A) 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.
Metadata & operator*() const
static AAMDNodes getEmptyKey()
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Attribute unwrap(LLVMAttributeRef Attr)
void makeReplaceable(std::unique_ptr< ReplaceableMetadataImpl > ReplaceableUses)
Assign RAUW support to this.
bool isResolved() const
Check if node is fully resolved.
A Use represents the edge between a Value definition and its users.
The access may reference the value stored in memory.
unsigned getHash() const
Get the hash, if any.
Typed iterator through MDNode operands.
static StringRef getName(Value *V)
static MDTuple * getIfExists(LLVMContext &Context, ArrayRef< Metadata *> MDs)
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata *> MDs)
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)
APInt operator*(APInt a, uint64_t RHS)
op_iterator op_begin() const
static bool classof(const Metadata *MD)
Methods for support type inquiry through isa, cast, and dyn_cast:
Type * getType() const
All values are typed, get the type of this value.
op_range operands() const
MDTuple * operator->() const
LLVMContext & getContext() const
const_op_iterator op_end() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
const Module * getParent() const
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
static MDTuple * getIfExists(LLVMContext &Context, ArrayRef< Metadata *> MDs)
iterator_range< op_iterator > operands()
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Return a temporary node.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
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()
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
iterator end() const
Pointer to one byte past the end of the string.
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.
This is an important base class in LLVM.
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata *> MDs)
MDTupleTypedArrayWrapper(const MDTupleTypedArrayWrapper< U > &Other, typename std::enable_if<!std::is_convertible< U *, T *>::value >::type *=nullptr)
TempMDTuple clone() const
Return a (temporary) clone of this.
const_op_iterator op_begin() const
const unsigned char * bytes_begin() const
op_iterator_impl< MDNode *, MDNode > op_iterator
static SimpleType getSimplifiedValue(MDOperand &MD)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
mutable_op_range mutable_operands()
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)
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Return a distinct node.
bool hasReplaceableUses() const
Whether this contains RAUW support.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
iterator_range< const_op_iterator > operands() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
MDNode * NoAlias
The tag specifying the noalias scope.
A range adaptor for a pair of iterators.
std::enable_if< detail::IsValidPointer< X, Y >::value, X * >::type extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
T * operator[](unsigned I) const
std::enable_if< detail::IsValidReference< X, Y & >::value, bool >::type hasa(Y &MD)
bool operator==(const AAMDNodes &A) 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.
bool operator!=(uint64_t V1, const APInt &V2)
const unsigned char * bytes_begin() const
DistinctMDOperandPlaceholder(unsigned ID)
static ValueLatticeElement intersect(const ValueLatticeElement &A, const ValueLatticeElement &B)
Combine two sets of facts about the same value into a single set of facts.
static AAMDNodes getTombstoneKey()
static SimpleType getSimplifiedValue(const MDOperand &MD)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
MDTupleTypedArrayWrapper(const MDTupleTypedArrayWrapper< U > &Other, typename std::enable_if< std::is_convertible< U *, T *>::value >::type *=nullptr)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
LLVM_NODISCARD bool empty() const
T get() const
Returns the value of the specified pointer type.
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.
LLVM Value Representation.
void reset(Metadata *MD, Metadata *Owner)
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
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...
This class implements an extremely fast bulk output stream that can only output to a stream...
bool operator==(const TypedMDOperandIterator &X) const
StringRef - Represent a constant reference to a string, i.e.
int is() const
Test if the Union currently holds the type matching T.
std::enable_if< detail::IsValidPointer< X, Y >::value, X * >::type dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
bool operator==(uint64_t V1, const APInt &V2)
MDTuple & operator*() const
static LazyValueInfoImpl & getImpl(void *&PImpl, AssumptionCache *AC, const DataLayout *DL, DominatorTree *DT=nullptr)
This lazily constructs the LazyValueInfoImpl.
unsigned getNumOperands() const
Return number of MDNode operands.
TypedMDOperandIterator(MDNode::op_iterator I)
ReplaceableMetadataImpl * getReplaceableUses() const
void operator()(MDNode *Node) const
Module * getParent()
Get the module that holds this named metadata collection.
bool operator!=(const TypedMDOperandIterator &X) const
~DistinctMDOperandPlaceholder()
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...