1 //===- llvm/IR/Metadata.h - Metadata definitions ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file contains the declarations for metadata subclasses.
12 /// They represent the different flavors of metadata that live in LLVM.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_METADATA_H
17 #define LLVM_IR_METADATA_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/DenseMapInfo.h"
22 #include "llvm/ADT/None.h"
23 #include "llvm/ADT/PointerUnion.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/ilist_node.h"
28 #include "llvm/ADT/iterator_range.h"
29 #include "llvm/IR/Constant.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/Value.h"
32 #include "llvm/Support/CBindingWrapping.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/ErrorHandling.h"
41 #include <type_traits>
47 class ModuleSlotTracker;
51 enum LLVMConstants : uint32_t {
52 DEBUG_METADATA_VERSION = 3 // Current debug info version number.
55 /// Root of the metadata hierarchy.
57 /// This is a root class for typeless data in the IR.
59 friend class ReplaceableMetadataImpl;
62 const unsigned char SubclassID;
65 /// Active type of storage.
66 enum StorageType { Uniqued, Distinct, Temporary };
68 /// Storage flag for non-uniqued, otherwise unowned, metadata.
69 unsigned char Storage;
70 // TODO: expose remaining bits to subclasses.
72 unsigned short SubclassData16 = 0;
73 unsigned SubclassData32 = 0;
77 #define HANDLE_METADATA_LEAF(CLASS) CLASS##Kind,
78 #include "llvm/IR/Metadata.def"
82 Metadata(unsigned ID, StorageType Storage)
83 : SubclassID(ID), Storage(Storage) {
84 static_assert(sizeof(*this) == 8, "Metadata fields poorly packed");
87 ~Metadata() = default;
89 /// Default handling of a changed operand, which asserts.
91 /// If subclasses pass themselves in as owners to a tracking node reference,
92 /// they must provide an implementation of this method.
93 void handleChangedOperand(void *, Metadata *) {
94 llvm_unreachable("Unimplemented in Metadata subclass");
98 unsigned getMetadataID() const { return SubclassID; }
100 /// User-friendly dump.
102 /// If \c M is provided, metadata nodes will be numbered canonically;
103 /// otherwise, pointer addresses are substituted.
105 /// Note: this uses an explicit overload instead of default arguments so that
106 /// the nullptr version is easy to call from a debugger.
110 void dump(const Module *M) const;
115 /// Prints definition of \c this.
117 /// If \c M is provided, metadata nodes will be numbered canonically;
118 /// otherwise, pointer addresses are substituted.
120 void print(raw_ostream &OS, const Module *M = nullptr,
121 bool IsForDebug = false) const;
122 void print(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M = nullptr,
123 bool IsForDebug = false) const;
126 /// Print as operand.
128 /// Prints reference of \c this.
130 /// If \c M is provided, metadata nodes will be numbered canonically;
131 /// otherwise, pointer addresses are substituted.
133 void printAsOperand(raw_ostream &OS, const Module *M = nullptr) const;
134 void printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
135 const Module *M = nullptr) const;
139 // Create wrappers for C Binding types (see CBindingWrapping.h).
140 DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)
142 // Specialized opaque metadata conversions.
143 inline Metadata **unwrap(LLVMMetadataRef *MDs) {
144 return reinterpret_cast<Metadata**>(MDs);
147 #define HANDLE_METADATA(CLASS) class CLASS;
148 #include "llvm/IR/Metadata.def"
150 // Provide specializations of isa so that we don't need definitions of
151 // subclasses to see if the metadata is a subclass.
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"
160 inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) {
165 /// Metadata wrapper in the Value hierarchy.
167 /// A member of the \a Value hierarchy to represent a reference to metadata.
168 /// This allows, e.g., instrinsics to have metadata as operands.
170 /// Notably, this is the only thing in either hierarchy that is allowed to
171 /// reference \a LocalAsMetadata.
172 class MetadataAsValue : public Value {
173 friend class ReplaceableMetadataImpl;
174 friend class LLVMContextImpl;
178 MetadataAsValue(Type *Ty, Metadata *MD);
180 /// Drop use of metadata (during teardown).
181 void dropUse() { MD = nullptr; }
186 static MetadataAsValue *get(LLVMContext &Context, Metadata *MD);
187 static MetadataAsValue *getIfExists(LLVMContext &Context, Metadata *MD);
189 Metadata *getMetadata() const { return MD; }
191 static bool classof(const Value *V) {
192 return V->getValueID() == MetadataAsValueVal;
196 void handleChangedMetadata(Metadata *MD);
201 /// API for tracking metadata references through RAUW and deletion.
203 /// Shared API for updating \a Metadata pointers in subclasses that support
206 /// This API is not meant to be used directly. See \a TrackingMDRef for a
207 /// user-friendly tracking reference.
208 class MetadataTracking {
210 /// Track the reference to metadata.
212 /// Register \c MD with \c *MD, if the subclass supports tracking. If \c *MD
213 /// gets RAUW'ed, \c MD will be updated to the new address. If \c *MD gets
214 /// deleted, \c MD will be set to \c nullptr.
216 /// If tracking isn't supported, \c *MD will not change.
218 /// \return true iff tracking is supported by \c MD.
219 static bool track(Metadata *&MD) {
220 return track(&MD, *MD, static_cast<Metadata *>(nullptr));
223 /// Track the reference to metadata for \a Metadata.
225 /// As \a track(Metadata*&), but with support for calling back to \c Owner to
226 /// tell it that its operand changed. This could trigger \c Owner being
228 static bool track(void *Ref, Metadata &MD, Metadata &Owner) {
229 return track(Ref, MD, &Owner);
232 /// Track the reference to metadata for \a MetadataAsValue.
234 /// As \a track(Metadata*&), but with support for calling back to \c Owner to
235 /// tell it that its operand changed. This could trigger \c Owner being
237 static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) {
238 return track(Ref, MD, &Owner);
241 /// Stop tracking a reference to metadata.
243 /// Stops \c *MD from tracking \c MD.
244 static void untrack(Metadata *&MD) { untrack(&MD, *MD); }
245 static void untrack(void *Ref, Metadata &MD);
247 /// Move tracking from one reference to another.
249 /// Semantically equivalent to \c untrack(MD) followed by \c track(New),
250 /// except that ownership callbacks are maintained.
252 /// Note: it is an error if \c *MD does not equal \c New.
254 /// \return true iff tracking is supported by \c MD.
255 static bool retrack(Metadata *&MD, Metadata *&New) {
256 return retrack(&MD, *MD, &New);
258 static bool retrack(void *Ref, Metadata &MD, void *New);
260 /// Check whether metadata is replaceable.
261 static bool isReplaceable(const Metadata &MD);
263 using OwnerTy = PointerUnion<MetadataAsValue *, Metadata *>;
266 /// Track a reference to metadata for an owner.
268 /// Generalized version of tracking.
269 static bool track(void *Ref, Metadata &MD, OwnerTy Owner);
272 /// Shared implementation of use-lists for replaceable metadata.
274 /// Most metadata cannot be RAUW'ed. This is a shared implementation of
275 /// use-lists and associated API for the two that support it (\a ValueAsMetadata
276 /// and \a TempMDNode).
277 class ReplaceableMetadataImpl {
278 friend class MetadataTracking;
281 using OwnerTy = MetadataTracking::OwnerTy;
284 LLVMContext &Context;
285 uint64_t NextIndex = 0;
286 SmallDenseMap<void *, std::pair<OwnerTy, uint64_t>, 4> UseMap;
289 ReplaceableMetadataImpl(LLVMContext &Context) : Context(Context) {}
291 ~ReplaceableMetadataImpl() {
292 assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata");
295 LLVMContext &getContext() const { return Context; }
297 /// Replace all uses of this with MD.
299 /// Replace all uses of this with \c MD, which is allowed to be null.
300 void replaceAllUsesWith(Metadata *MD);
302 /// Resolve all uses of this.
304 /// Resolve all uses of this, turning off RAUW permanently. If \c
305 /// ResolveUsers, call \a MDNode::resolve() on any users whose last operand
307 void resolveAllUses(bool ResolveUsers = true);
310 void addRef(void *Ref, OwnerTy Owner);
311 void dropRef(void *Ref);
312 void moveRef(void *Ref, void *New, const Metadata &MD);
314 /// Lazily construct RAUW support on MD.
316 /// If this is an unresolved MDNode, RAUW support will be created on-demand.
317 /// ValueAsMetadata always has RAUW support.
318 static ReplaceableMetadataImpl *getOrCreate(Metadata &MD);
320 /// Get RAUW support on MD, if it exists.
321 static ReplaceableMetadataImpl *getIfExists(Metadata &MD);
323 /// Check whether this node will support RAUW.
325 /// Returns \c true unless getOrCreate() would return null.
326 static bool isReplaceable(const Metadata &MD);
329 /// Value wrapper in the Metadata hierarchy.
331 /// This is a custom value handle that allows other metadata to refer to
332 /// classes in the Value hierarchy.
334 /// Because of full uniquing support, each value is only wrapped by a single \a
335 /// ValueAsMetadata object, so the lookup maps are far more efficient than
336 /// those using ValueHandleBase.
337 class ValueAsMetadata : public Metadata, ReplaceableMetadataImpl {
338 friend class ReplaceableMetadataImpl;
339 friend class LLVMContextImpl;
343 /// Drop users without RAUW (during teardown).
345 ReplaceableMetadataImpl::resolveAllUses(/* ResolveUsers */ false);
349 ValueAsMetadata(unsigned ID, Value *V)
350 : Metadata(ID, Uniqued), ReplaceableMetadataImpl(V->getContext()), V(V) {
351 assert(V && "Expected valid value");
354 ~ValueAsMetadata() = default;
357 static ValueAsMetadata *get(Value *V);
359 static ConstantAsMetadata *getConstant(Value *C) {
360 return cast<ConstantAsMetadata>(get(C));
363 static LocalAsMetadata *getLocal(Value *Local) {
364 return cast<LocalAsMetadata>(get(Local));
367 static ValueAsMetadata *getIfExists(Value *V);
369 static ConstantAsMetadata *getConstantIfExists(Value *C) {
370 return cast_or_null<ConstantAsMetadata>(getIfExists(C));
373 static LocalAsMetadata *getLocalIfExists(Value *Local) {
374 return cast_or_null<LocalAsMetadata>(getIfExists(Local));
377 Value *getValue() const { return V; }
378 Type *getType() const { return V->getType(); }
379 LLVMContext &getContext() const { return V->getContext(); }
381 static void handleDeletion(Value *V);
382 static void handleRAUW(Value *From, Value *To);
385 /// Handle collisions after \a Value::replaceAllUsesWith().
387 /// RAUW isn't supported directly for \a ValueAsMetadata, but if the wrapped
388 /// \a Value gets RAUW'ed and the target already exists, this is used to
389 /// merge the two metadata nodes.
390 void replaceAllUsesWith(Metadata *MD) {
391 ReplaceableMetadataImpl::replaceAllUsesWith(MD);
395 static bool classof(const Metadata *MD) {
396 return MD->getMetadataID() == LocalAsMetadataKind ||
397 MD->getMetadataID() == ConstantAsMetadataKind;
401 class ConstantAsMetadata : public ValueAsMetadata {
402 friend class ValueAsMetadata;
404 ConstantAsMetadata(Constant *C)
405 : ValueAsMetadata(ConstantAsMetadataKind, C) {}
408 static ConstantAsMetadata *get(Constant *C) {
409 return ValueAsMetadata::getConstant(C);
412 static ConstantAsMetadata *getIfExists(Constant *C) {
413 return ValueAsMetadata::getConstantIfExists(C);
416 Constant *getValue() const {
417 return cast<Constant>(ValueAsMetadata::getValue());
420 static bool classof(const Metadata *MD) {
421 return MD->getMetadataID() == ConstantAsMetadataKind;
425 class LocalAsMetadata : public ValueAsMetadata {
426 friend class ValueAsMetadata;
428 LocalAsMetadata(Value *Local)
429 : ValueAsMetadata(LocalAsMetadataKind, Local) {
430 assert(!isa<Constant>(Local) && "Expected local value");
434 static LocalAsMetadata *get(Value *Local) {
435 return ValueAsMetadata::getLocal(Local);
438 static LocalAsMetadata *getIfExists(Value *Local) {
439 return ValueAsMetadata::getLocalIfExists(Local);
442 static bool classof(const Metadata *MD) {
443 return MD->getMetadataID() == LocalAsMetadataKind;
447 /// Transitional API for extracting constants from Metadata.
449 /// This namespace contains transitional functions for metadata that points to
452 /// In prehistory -- when metadata was a subclass of \a Value -- \a MDNode
453 /// operands could refer to any \a Value. There's was a lot of code like this:
457 /// auto *CI = dyn_cast<ConstantInt>(N->getOperand(2));
460 /// Now that \a Value and \a Metadata are in separate hierarchies, maintaining
461 /// the semantics for \a isa(), \a cast(), \a dyn_cast() (etc.) requires three
462 /// steps: cast in the \a Metadata hierarchy, extraction of the \a Value, and
463 /// cast in the \a Value hierarchy. Besides creating boiler-plate, this
464 /// requires subtle control flow changes.
466 /// The end-goal is to create a new type of metadata, called (e.g.) \a MDInt,
467 /// so that metadata can refer to numbers without traversing a bridge to the \a
468 /// Value hierarchy. In this final state, the code above would look like this:
472 /// auto *MI = dyn_cast<MDInt>(N->getOperand(2));
475 /// The API in this namespace supports the transition. \a MDInt doesn't exist
476 /// yet, and even once it does, changing each metadata schema to use it is its
477 /// own mini-project. In the meantime this API prevents us from introducing
478 /// complex and bug-prone control flow that will disappear in the end. In
479 /// particular, the above code looks like this:
483 /// auto *CI = mdconst::dyn_extract<ConstantInt>(N->getOperand(2));
486 /// The full set of provided functions includes:
488 /// mdconst::hasa <=> isa
489 /// mdconst::extract <=> cast
490 /// mdconst::extract_or_null <=> cast_or_null
491 /// mdconst::dyn_extract <=> dyn_cast
492 /// mdconst::dyn_extract_or_null <=> dyn_cast_or_null
494 /// The target of the cast must be a subclass of \a Constant.
499 template <class T> T &make();
500 template <class T, class Result> struct HasDereference {
503 template <size_t N> struct SFINAE {};
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);
512 template <class V, class M> struct IsValidPointer {
513 static const bool value = std::is_base_of<Constant, V>::value &&
514 HasDereference<M, const Metadata &>::value;
516 template <class V, class M> struct IsValidReference {
517 static const bool value = std::is_base_of<Constant, V>::value &&
518 std::is_convertible<M, const Metadata &>::value;
521 } // end namespace detail
523 /// Check whether Metadata has a Value.
525 /// As an analogue to \a isa(), check whether \c MD has an \a Value inside of
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
542 /// Extract a Value from Metadata.
544 /// As an analogue to \a cast(), extract the \a Value subclass \c X from \c MD.
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
557 /// Extract a Value from Metadata, allowing null.
559 /// As an analogue to \a cast_or_null(), extract the \a Value subclass \c X
560 /// from \c MD, allowing \c MD to be null.
561 template <class X, class Y>
562 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
563 extract_or_null(Y &&MD) {
564 if (auto *V = cast_or_null<ConstantAsMetadata>(MD))
565 return cast<X>(V->getValue());
569 /// Extract a Value from Metadata, if any.
571 /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
572 /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
573 /// Value it does contain is of the wrong subclass.
574 template <class X, class Y>
575 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
576 dyn_extract(Y &&MD) {
577 if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
578 return dyn_cast<X>(V->getValue());
582 /// Extract a Value from Metadata, if any, allowing null.
584 /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
585 /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
586 /// Value it does contain is of the wrong subclass, allowing \c MD to be null.
587 template <class X, class Y>
588 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
589 dyn_extract_or_null(Y &&MD) {
590 if (auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
591 return dyn_cast<X>(V->getValue());
595 } // end namespace mdconst
597 //===----------------------------------------------------------------------===//
598 /// A single uniqued string.
600 /// These are used to efficiently contain a byte sequence for metadata.
601 /// MDString is always unnamed.
602 class MDString : public Metadata {
603 friend class StringMapEntry<MDString>;
605 StringMapEntry<MDString> *Entry = nullptr;
607 MDString() : Metadata(MDStringKind, Uniqued) {}
610 MDString(const MDString &) = delete;
611 MDString &operator=(MDString &&) = delete;
612 MDString &operator=(const MDString &) = delete;
614 static MDString *get(LLVMContext &Context, StringRef Str);
615 static MDString *get(LLVMContext &Context, const char *Str) {
616 return get(Context, Str ? StringRef(Str) : StringRef());
619 StringRef getString() const;
621 unsigned getLength() const { return (unsigned)getString().size(); }
623 using iterator = StringRef::iterator;
625 /// Pointer to the first byte of the string.
626 iterator begin() const { return getString().begin(); }
628 /// Pointer to one byte past the end of the string.
629 iterator end() const { return getString().end(); }
631 const unsigned char *bytes_begin() const { return getString().bytes_begin(); }
632 const unsigned char *bytes_end() const { return getString().bytes_end(); }
634 /// Methods for support type inquiry through isa, cast, and dyn_cast.
635 static bool classof(const Metadata *MD) {
636 return MD->getMetadataID() == MDStringKind;
640 /// A collection of metadata nodes that might be associated with a
641 /// memory access used by the alias-analysis infrastructure.
643 explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
645 : TBAA(T), Scope(S), NoAlias(N) {}
647 bool operator==(const AAMDNodes &A) const {
648 return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
651 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
653 explicit operator bool() const { return TBAA || Scope || NoAlias; }
655 /// The tag for type-based alias analysis.
658 /// The tag for alias scope specification (used with noalias).
661 /// The tag specifying the noalias scope.
664 /// Given two sets of AAMDNodes that apply to the same pointer,
665 /// give the best AAMDNodes that are compatible with both (i.e. a set of
666 /// nodes whose allowable aliasing conclusions are a subset of those
667 /// allowable by both of the inputs). However, for efficiency
668 /// reasons, do not create any new MDNodes.
669 AAMDNodes intersect(const AAMDNodes &Other) {
671 Result.TBAA = Other.TBAA == TBAA ? TBAA : nullptr;
672 Result.Scope = Other.Scope == Scope ? Scope : nullptr;
673 Result.NoAlias = Other.NoAlias == NoAlias ? NoAlias : nullptr;
678 // Specialize DenseMapInfo for AAMDNodes.
680 struct DenseMapInfo<AAMDNodes> {
681 static inline AAMDNodes getEmptyKey() {
682 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(),
686 static inline AAMDNodes getTombstoneKey() {
687 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(),
691 static unsigned getHashValue(const AAMDNodes &Val) {
692 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
693 DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
694 DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
697 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
702 /// Tracking metadata reference owned by Metadata.
704 /// Similar to \a TrackingMDRef, but it's expected to be owned by an instance
705 /// of \a Metadata, which has the option of registering itself for callbacks to
706 /// re-unique itself.
708 /// In particular, this is used by \a MDNode.
710 Metadata *MD = nullptr;
713 MDOperand() = default;
714 MDOperand(MDOperand &&) = delete;
715 MDOperand(const MDOperand &) = delete;
716 MDOperand &operator=(MDOperand &&) = delete;
717 MDOperand &operator=(const MDOperand &) = delete;
718 ~MDOperand() { untrack(); }
720 Metadata *get() const { return MD; }
721 operator Metadata *() const { return get(); }
722 Metadata *operator->() const { return get(); }
723 Metadata &operator*() const { return *get(); }
729 void reset(Metadata *MD, Metadata *Owner) {
736 void track(Metadata *Owner) {
739 MetadataTracking::track(this, *MD, *Owner);
741 MetadataTracking::track(MD);
746 assert(static_cast<void *>(this) == &MD && "Expected same address");
748 MetadataTracking::untrack(MD);
752 template <> struct simplify_type<MDOperand> {
753 using SimpleType = Metadata *;
755 static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); }
758 template <> struct simplify_type<const MDOperand> {
759 using SimpleType = Metadata *;
761 static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); }
764 /// Pointer to the context, with optional RAUW support.
766 /// Either a raw (non-null) pointer to the \a LLVMContext, or an owned pointer
767 /// to \a ReplaceableMetadataImpl (which has a reference to \a LLVMContext).
768 class ContextAndReplaceableUses {
769 PointerUnion<LLVMContext *, ReplaceableMetadataImpl *> Ptr;
772 ContextAndReplaceableUses(LLVMContext &Context) : Ptr(&Context) {}
773 ContextAndReplaceableUses(
774 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
775 : Ptr(ReplaceableUses.release()) {
776 assert(getReplaceableUses() && "Expected non-null replaceable uses");
778 ContextAndReplaceableUses() = delete;
779 ContextAndReplaceableUses(ContextAndReplaceableUses &&) = delete;
780 ContextAndReplaceableUses(const ContextAndReplaceableUses &) = delete;
781 ContextAndReplaceableUses &operator=(ContextAndReplaceableUses &&) = delete;
782 ContextAndReplaceableUses &
783 operator=(const ContextAndReplaceableUses &) = delete;
784 ~ContextAndReplaceableUses() { delete getReplaceableUses(); }
786 operator LLVMContext &() { return getContext(); }
788 /// Whether this contains RAUW support.
789 bool hasReplaceableUses() const {
790 return Ptr.is<ReplaceableMetadataImpl *>();
793 LLVMContext &getContext() const {
794 if (hasReplaceableUses())
795 return getReplaceableUses()->getContext();
796 return *Ptr.get<LLVMContext *>();
799 ReplaceableMetadataImpl *getReplaceableUses() const {
800 if (hasReplaceableUses())
801 return Ptr.get<ReplaceableMetadataImpl *>();
805 /// Ensure that this has RAUW support, and then return it.
806 ReplaceableMetadataImpl *getOrCreateReplaceableUses() {
807 if (!hasReplaceableUses())
808 makeReplaceable(llvm::make_unique<ReplaceableMetadataImpl>(getContext()));
809 return getReplaceableUses();
812 /// Assign RAUW support to this.
814 /// Make this replaceable, taking ownership of \c ReplaceableUses (which must
817 makeReplaceable(std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) {
818 assert(ReplaceableUses && "Expected non-null replaceable uses");
819 assert(&ReplaceableUses->getContext() == &getContext() &&
820 "Expected same context");
821 delete getReplaceableUses();
822 Ptr = ReplaceableUses.release();
825 /// Drop RAUW support.
827 /// Cede ownership of RAUW support, returning it.
828 std::unique_ptr<ReplaceableMetadataImpl> takeReplaceableUses() {
829 assert(hasReplaceableUses() && "Expected to own replaceable uses");
830 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
831 getReplaceableUses());
832 Ptr = &ReplaceableUses->getContext();
833 return ReplaceableUses;
837 struct TempMDNodeDeleter {
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"
848 /// Metadata nodes can be uniqued, like constants, or distinct. Temporary
849 /// metadata nodes (with full support for RAUW) can be used to delay uniquing
850 /// until forward references are known. The basic metadata node is an \a
853 /// There is limited support for RAUW at construction time. At construction
854 /// time, if any operand is a temporary node (or an unresolved uniqued node,
855 /// which indicates a transitive temporary operand), the node itself will be
856 /// unresolved. As soon as all operands become resolved, it will drop RAUW
857 /// support permanently.
859 /// If an unresolved node is part of a cycle, \a resolveCycles() needs
860 /// to be called on some member of the cycle once all temporary nodes have been
862 class MDNode : public Metadata {
863 friend class ReplaceableMetadataImpl;
864 friend class LLVMContextImpl;
866 unsigned NumOperands;
867 unsigned NumUnresolved;
869 ContextAndReplaceableUses Context;
872 MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
873 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None);
876 void *operator new(size_t Size, unsigned NumOps);
877 void operator delete(void *Mem);
879 /// Required by std, but never called.
880 void operator delete(void *, unsigned) {
881 llvm_unreachable("Constructor throws?");
884 /// Required by std, but never called.
885 void operator delete(void *, unsigned, bool) {
886 llvm_unreachable("Constructor throws?");
889 void dropAllReferences();
891 MDOperand *mutable_begin() { return mutable_end() - NumOperands; }
892 MDOperand *mutable_end() { return reinterpret_cast<MDOperand *>(this); }
894 using mutable_op_range = iterator_range<MDOperand *>;
896 mutable_op_range mutable_operands() {
897 return mutable_op_range(mutable_begin(), mutable_end());
901 MDNode(const MDNode &) = delete;
902 void operator=(const MDNode &) = delete;
903 void *operator new(size_t) = delete;
905 static inline MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs);
906 static inline MDTuple *getIfExists(LLVMContext &Context,
907 ArrayRef<Metadata *> MDs);
908 static inline MDTuple *getDistinct(LLVMContext &Context,
909 ArrayRef<Metadata *> MDs);
910 static inline TempMDTuple getTemporary(LLVMContext &Context,
911 ArrayRef<Metadata *> MDs);
913 /// Create a (temporary) clone of this.
914 TempMDNode clone() const;
916 /// Deallocate a node created by getTemporary.
918 /// Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining
919 /// references will be reset.
920 static void deleteTemporary(MDNode *N);
922 LLVMContext &getContext() const { return Context.getContext(); }
924 /// Replace a specific operand.
925 void replaceOperandWith(unsigned I, Metadata *New);
927 /// Check if node is fully resolved.
929 /// If \a isTemporary(), this always returns \c false; if \a isDistinct(),
930 /// this always returns \c true.
932 /// If \a isUniqued(), returns \c true if this has already dropped RAUW
933 /// support (because all operands are resolved).
935 /// As forward declarations are resolved, their containers should get
936 /// resolved automatically. However, if this (or one of its operands) is
937 /// involved in a cycle, \a resolveCycles() needs to be called explicitly.
938 bool isResolved() const { return !isTemporary() && !NumUnresolved; }
940 bool isUniqued() const { return Storage == Uniqued; }
941 bool isDistinct() const { return Storage == Distinct; }
942 bool isTemporary() const { return Storage == Temporary; }
944 /// RAUW a temporary.
946 /// \pre \a isTemporary() must be \c true.
947 void replaceAllUsesWith(Metadata *MD) {
948 assert(isTemporary() && "Expected temporary node");
949 if (Context.hasReplaceableUses())
950 Context.getReplaceableUses()->replaceAllUsesWith(MD);
955 /// Once all forward declarations have been resolved, force cycles to be
958 /// \pre No operands (or operands' operands, etc.) have \a isTemporary().
959 void resolveCycles();
961 /// Resolve a unique, unresolved node.
964 /// Replace a temporary node with a permanent one.
966 /// Try to create a uniqued version of \c N -- in place, if possible -- and
967 /// return it. If \c N cannot be uniqued, return a distinct node instead.
969 static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
970 replaceWithPermanent(std::unique_ptr<T, TempMDNodeDeleter> N) {
971 return cast<T>(N.release()->replaceWithPermanentImpl());
974 /// Replace a temporary node with a uniqued one.
976 /// Create a uniqued version of \c N -- in place, if possible -- and return
977 /// it. Takes ownership of the temporary node.
979 /// \pre N does not self-reference.
981 static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
982 replaceWithUniqued(std::unique_ptr<T, TempMDNodeDeleter> N) {
983 return cast<T>(N.release()->replaceWithUniquedImpl());
986 /// Replace a temporary node with a distinct one.
988 /// Create a distinct version of \c N -- in place, if possible -- and return
989 /// it. Takes ownership of the temporary node.
991 static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
992 replaceWithDistinct(std::unique_ptr<T, TempMDNodeDeleter> N) {
993 return cast<T>(N.release()->replaceWithDistinctImpl());
997 MDNode *replaceWithPermanentImpl();
998 MDNode *replaceWithUniquedImpl();
999 MDNode *replaceWithDistinctImpl();
1004 /// Sets the operand directly, without worrying about uniquing.
1005 void setOperand(unsigned I, Metadata *New);
1007 void storeDistinctInContext();
1008 template <class T, class StoreT>
1009 static T *storeImpl(T *N, StorageType Storage, StoreT &Store);
1010 template <class T> static T *storeImpl(T *N, StorageType Storage);
1013 void handleChangedOperand(void *Ref, Metadata *New);
1015 /// Drop RAUW support, if any.
1016 void dropReplaceableUses();
1018 void resolveAfterOperandChange(Metadata *Old, Metadata *New);
1019 void decrementUnresolvedOperandCount();
1020 void countUnresolvedOperands();
1022 /// Mutate this to be "uniqued".
1024 /// Mutate this so that \a isUniqued().
1025 /// \pre \a isTemporary().
1026 /// \pre already added to uniquing set.
1029 /// Mutate this to be "distinct".
1031 /// Mutate this so that \a isDistinct().
1032 /// \pre \a isTemporary().
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) {}
1054 using op_iterator = const MDOperand *;
1055 using op_range = iterator_range<op_iterator>;
1057 op_iterator op_begin() const {
1058 return const_cast<MDNode *>(this)->mutable_begin();
1061 op_iterator op_end() const {
1062 return const_cast<MDNode *>(this)->mutable_end();
1065 op_range operands() const { return op_range(op_begin(), op_end()); }
1067 const MDOperand &getOperand(unsigned I) const {
1068 assert(I < NumOperands && "Out of range");
1069 return op_begin()[I];
1072 /// Return number of MDNode operands.
1073 unsigned getNumOperands() const { return NumOperands; }
1075 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1076 static bool classof(const Metadata *MD) {
1077 switch (MD->getMetadataID()) {
1080 #define HANDLE_MDNODE_LEAF(CLASS) \
1083 #include "llvm/IR/Metadata.def"
1087 /// Check whether MDNode is a vtable access.
1088 bool isTBAAVtableAccess() const;
1090 /// Methods for metadata merging.
1091 static MDNode *concatenate(MDNode *A, MDNode *B);
1092 static MDNode *intersect(MDNode *A, MDNode *B);
1093 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
1094 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
1095 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
1096 static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B);
1097 static MDNode *getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B);
1100 /// Tuple of metadata.
1102 /// This is the simple \a MDNode arbitrary tuple. Nodes are uniqued by
1103 /// default based on their operands.
1104 class MDTuple : public MDNode {
1105 friend class LLVMContextImpl;
1106 friend class MDNode;
1108 MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash,
1109 ArrayRef<Metadata *> Vals)
1110 : MDNode(C, MDTupleKind, Storage, Vals) {
1114 ~MDTuple() { dropAllReferences(); }
1116 void setHash(unsigned Hash) { SubclassData32 = Hash; }
1117 void recalculateHash();
1119 static MDTuple *getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
1120 StorageType Storage, bool ShouldCreate = true);
1122 TempMDTuple cloneImpl() const {
1123 return getTemporary(getContext(),
1124 SmallVector<Metadata *, 4>(op_begin(), op_end()));
1128 /// Get the hash, if any.
1129 unsigned getHash() const { return SubclassData32; }
1131 static MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1132 return getImpl(Context, MDs, Uniqued);
1135 static MDTuple *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1136 return getImpl(Context, MDs, Uniqued, /* ShouldCreate */ false);
1139 /// Return a distinct node.
1141 /// Return a distinct node -- i.e., a node that is not uniqued.
1142 static MDTuple *getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1143 return getImpl(Context, MDs, Distinct);
1146 /// Return a temporary node.
1148 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
1149 /// not uniqued, may be RAUW'd, and must be manually deleted with
1150 /// deleteTemporary.
1151 static TempMDTuple getTemporary(LLVMContext &Context,
1152 ArrayRef<Metadata *> MDs) {
1153 return TempMDTuple(getImpl(Context, MDs, Temporary));
1156 /// Return a (temporary) clone of this.
1157 TempMDTuple clone() const { return cloneImpl(); }
1159 static bool classof(const Metadata *MD) {
1160 return MD->getMetadataID() == MDTupleKind;
1164 MDTuple *MDNode::get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1165 return MDTuple::get(Context, MDs);
1168 MDTuple *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1169 return MDTuple::getIfExists(Context, MDs);
1172 MDTuple *MDNode::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1173 return MDTuple::getDistinct(Context, MDs);
1176 TempMDTuple MDNode::getTemporary(LLVMContext &Context,
1177 ArrayRef<Metadata *> MDs) {
1178 return MDTuple::getTemporary(Context, MDs);
1181 void TempMDNodeDeleter::operator()(MDNode *Node) const {
1182 MDNode::deleteTemporary(Node);
1185 /// Typed iterator through MDNode operands.
1187 /// An iterator that transforms an \a MDNode::iterator into an iterator over a
1188 /// particular Metadata subclass.
1190 class TypedMDOperandIterator
1191 : public std::iterator<std::input_iterator_tag, T *, std::ptrdiff_t, void,
1193 MDNode::op_iterator I = nullptr;
1196 TypedMDOperandIterator() = default;
1197 explicit TypedMDOperandIterator(MDNode::op_iterator I) : I(I) {}
1199 T *operator*() const { return cast_or_null<T>(*I); }
1201 TypedMDOperandIterator &operator++() {
1206 TypedMDOperandIterator operator++(int) {
1207 TypedMDOperandIterator Temp(*this);
1212 bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; }
1213 bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; }
1216 /// Typed, array-like tuple of metadata.
1218 /// This is a wrapper for \a MDTuple that makes it act like an array holding a
1219 /// particular type of metadata.
1220 template <class T> class MDTupleTypedArrayWrapper {
1221 const MDTuple *N = nullptr;
1224 MDTupleTypedArrayWrapper() = default;
1225 MDTupleTypedArrayWrapper(const MDTuple *N) : N(N) {}
1228 MDTupleTypedArrayWrapper(
1229 const MDTupleTypedArrayWrapper<U> &Other,
1230 typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
1235 explicit MDTupleTypedArrayWrapper(
1236 const MDTupleTypedArrayWrapper<U> &Other,
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(); }
1244 MDTuple *get() const { return const_cast<MDTuple *>(N); }
1245 MDTuple *operator->() const { return get(); }
1246 MDTuple &operator*() const { return *get(); }
1248 // FIXME: Fix callers and remove condition on N.
1249 unsigned size() const { return N ? N->getNumOperands() : 0u; }
1250 bool empty() const { return N ? N->getNumOperands() == 0 : true; }
1251 T *operator[](unsigned I) const { return cast_or_null<T>(N->getOperand(I)); }
1253 // FIXME: Fix callers and remove condition on N.
1254 using iterator = TypedMDOperandIterator<T>;
1256 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
1257 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
1260 #define HANDLE_METADATA(CLASS) \
1261 using CLASS##Array = MDTupleTypedArrayWrapper<CLASS>;
1262 #include "llvm/IR/Metadata.def"
1264 /// Placeholder metadata for operands of distinct MDNodes.
1266 /// This is a lightweight placeholder for an operand of a distinct node. It's
1267 /// purpose is to help track forward references when creating a distinct node.
1268 /// This allows distinct nodes involved in a cycle to be constructed before
1269 /// their operands without requiring a heavyweight temporary node with
1270 /// full-blown RAUW support.
1272 /// Each placeholder supports only a single MDNode user. Clients should pass
1273 /// an ID, retrieved via \a getID(), to indicate the "real" operand that this
1274 /// should be replaced with.
1276 /// While it would be possible to implement move operators, they would be
1277 /// fairly expensive. Leave them unimplemented to discourage their use
1278 /// (clients can use std::deque, std::list, BumpPtrAllocator, etc.).
1279 class DistinctMDOperandPlaceholder : public Metadata {
1280 friend class MetadataTracking;
1282 Metadata **Use = nullptr;
1285 explicit DistinctMDOperandPlaceholder(unsigned ID)
1286 : Metadata(DistinctMDOperandPlaceholderKind, Distinct) {
1287 SubclassData32 = ID;
1290 DistinctMDOperandPlaceholder() = delete;
1291 DistinctMDOperandPlaceholder(DistinctMDOperandPlaceholder &&) = delete;
1292 DistinctMDOperandPlaceholder(const DistinctMDOperandPlaceholder &) = delete;
1294 ~DistinctMDOperandPlaceholder() {
1299 unsigned getID() const { return SubclassData32; }
1301 /// Replace the use of this with MD.
1302 void replaceUseWith(Metadata *MD) {
1308 MetadataTracking::track(*Use);
1310 Metadata *T = cast<Metadata>(this);
1311 MetadataTracking::untrack(T);
1312 assert(!Use && "Use is still being tracked despite being untracked!");
1316 //===----------------------------------------------------------------------===//
1317 /// A tuple of MDNodes.
1319 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
1320 /// to modules, have names, and contain lists of MDNodes.
1322 /// TODO: Inherit from Metadata.
1323 class NamedMDNode : public ilist_node<NamedMDNode> {
1324 friend class LLVMContextImpl;
1325 friend class Module;
1328 Module *Parent = nullptr;
1329 void *Operands; // SmallVector<TrackingMDRef, 4>
1331 void setParent(Module *M) { Parent = M; }
1333 explicit NamedMDNode(const Twine &N);
1335 template<class T1, class T2>
1336 class op_iterator_impl :
1337 public std::iterator<std::bidirectional_iterator_tag, T2> {
1338 friend class NamedMDNode;
1340 const NamedMDNode *Node = nullptr;
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);
1373 T1 operator*() const { return Node->getOperand(Idx); }
1377 NamedMDNode(const NamedMDNode &) = delete;
1380 /// Drop all references and remove the node from parent module.
1381 void eraseFromParent();
1383 /// Remove all uses and clear node vector.
1384 void dropAllReferences() { clearOperands(); }
1385 /// Drop all references to this node's operands.
1386 void clearOperands();
1388 /// Get the module that holds this named metadata collection.
1389 inline Module *getParent() { return Parent; }
1390 inline const Module *getParent() const { return Parent; }
1392 MDNode *getOperand(unsigned i) const;
1393 unsigned getNumOperands() const;
1394 void addOperand(MDNode *M);
1395 void setOperand(unsigned I, MDNode *New);
1396 StringRef getName() const;
1397 void print(raw_ostream &ROS, bool IsForDebug = false) const;
1398 void print(raw_ostream &ROS, ModuleSlotTracker &MST,
1399 bool IsForDebug = false) const;
1402 // ---------------------------------------------------------------------------
1403 // Operand Iterator interface...
1405 using op_iterator = op_iterator_impl<MDNode *, MDNode>;
1407 op_iterator op_begin() { return op_iterator(this, 0); }
1408 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
1410 using const_op_iterator = op_iterator_impl<const MDNode *, MDNode>;
1412 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
1413 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
1415 inline iterator_range<op_iterator> operands() {
1416 return make_range(op_begin(), op_end());
1418 inline iterator_range<const_op_iterator> operands() const {
1419 return make_range(op_begin(), op_end());
1423 } // end namespace llvm
1425 #endif // LLVM_IR_METADATA_H