//===- llvm/IR/Metadata.h - Metadata definitions ----------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // /// @file /// This file contains the declarations for metadata subclasses. /// They represent the different flavors of metadata that live in LLVM. // //===----------------------------------------------------------------------===// #ifndef LLVM_IR_METADATA_H #define LLVM_IR_METADATA_H #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMapInfo.h" #include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/ilist_node.h" #include "llvm/ADT/iterator_range.h" #include "llvm/IR/Constant.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Value.h" #include "llvm/Support/CBindingWrapping.h" #include "llvm/Support/Casting.h" #include "llvm/Support/ErrorHandling.h" #include <cassert> #include <cstddef> #include <cstdint> #include <iterator> #include <memory> #include <string> #include <type_traits> #include <utility> namespace llvm { class Module; class ModuleSlotTracker; class raw_ostream; class DbgVariableRecord; template <typename T> class StringMapEntry; template <typename ValueTy> class StringMapEntryStorage; class Type; enum LLVMConstants : uint32_t { … }; /// Magic number in the value profile metadata showing a target has been /// promoted for the instruction and shouldn't be promoted again. const uint64_t NOMORE_ICP_MAGICNUM = …; /// Root of the metadata hierarchy. /// /// This is a root class for typeless data in the IR. class Metadata { … }; // Create wrappers for C Binding types (see CBindingWrapping.h). DEFINE_ISA_CONVERSION_FUNCTIONS(…) // Specialized opaque metadata conversions. inline Metadata **unwrap(LLVMMetadataRef *MDs) { … } #define HANDLE_METADATA … #include "llvm/IR/Metadata.def" // Provide specializations of isa so that we don't need definitions of // subclasses to see if the metadata is a subclass. #define HANDLE_METADATA_LEAF … #include "llvm/IR/Metadata.def" inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) { … } /// Metadata wrapper in the Value hierarchy. /// /// A member of the \a Value hierarchy to represent a reference to metadata. /// This allows, e.g., intrinsics to have metadata as operands. /// /// Notably, this is the only thing in either hierarchy that is allowed to /// reference \a LocalAsMetadata. class MetadataAsValue : public Value { … }; /// Base class for tracking ValueAsMetadata/DIArgLists with user lookups and /// Owner callbacks outside of ValueAsMetadata. /// /// Currently only inherited by DbgVariableRecord; if other classes need to use /// it, then a SubclassID will need to be added (either as a new field or by /// making DebugValue into a PointerIntUnion) to discriminate between the /// subclasses in lookup and callback handling. class DebugValueUser { … }; /// API for tracking metadata references through RAUW and deletion. /// /// Shared API for updating \a Metadata pointers in subclasses that support /// RAUW. /// /// This API is not meant to be used directly. See \a TrackingMDRef for a /// user-friendly tracking reference. class MetadataTracking { … }; /// Shared implementation of use-lists for replaceable metadata. /// /// Most metadata cannot be RAUW'ed. This is a shared implementation of /// use-lists and associated API for the three that support it ( /// \a ValueAsMetadata, \a TempMDNode, and \a DIArgList). class ReplaceableMetadataImpl { … }; /// Value wrapper in the Metadata hierarchy. /// /// This is a custom value handle that allows other metadata to refer to /// classes in the Value hierarchy. /// /// Because of full uniquing support, each value is only wrapped by a single \a /// ValueAsMetadata object, so the lookup maps are far more efficient than /// those using ValueHandleBase. class ValueAsMetadata : public Metadata, ReplaceableMetadataImpl { … }; class ConstantAsMetadata : public ValueAsMetadata { … }; class LocalAsMetadata : public ValueAsMetadata { … }; /// Transitional API for extracting constants from Metadata. /// /// This namespace contains transitional functions for metadata that points to /// \a Constants. /// /// In prehistory -- when metadata was a subclass of \a Value -- \a MDNode /// operands could refer to any \a Value. There's was a lot of code like this: /// /// \code /// MDNode *N = ...; /// auto *CI = dyn_cast<ConstantInt>(N->getOperand(2)); /// \endcode /// /// Now that \a Value and \a Metadata are in separate hierarchies, maintaining /// the semantics for \a isa(), \a cast(), \a dyn_cast() (etc.) requires three /// steps: cast in the \a Metadata hierarchy, extraction of the \a Value, and /// cast in the \a Value hierarchy. Besides creating boiler-plate, this /// requires subtle control flow changes. /// /// The end-goal is to create a new type of metadata, called (e.g.) \a MDInt, /// so that metadata can refer to numbers without traversing a bridge to the \a /// Value hierarchy. In this final state, the code above would look like this: /// /// \code /// MDNode *N = ...; /// auto *MI = dyn_cast<MDInt>(N->getOperand(2)); /// \endcode /// /// The API in this namespace supports the transition. \a MDInt doesn't exist /// yet, and even once it does, changing each metadata schema to use it is its /// own mini-project. In the meantime this API prevents us from introducing /// complex and bug-prone control flow that will disappear in the end. In /// particular, the above code looks like this: /// /// \code /// MDNode *N = ...; /// auto *CI = mdconst::dyn_extract<ConstantInt>(N->getOperand(2)); /// \endcode /// /// The full set of provided functions includes: /// /// mdconst::hasa <=> isa /// mdconst::extract <=> cast /// mdconst::extract_or_null <=> cast_or_null /// mdconst::dyn_extract <=> dyn_cast /// mdconst::dyn_extract_or_null <=> dyn_cast_or_null /// /// The target of the cast must be a subclass of \a Constant. namespace mdconst { namespace detail { template <class T> T &make(); template <class T, class Result> struct HasDereference { … }; template <class V, class M> struct IsValidPointer { … }; template <class V, class M> struct IsValidReference { … }; } // end namespace detail /// Check whether Metadata has a Value. /// /// As an analogue to \a isa(), check whether \c MD has an \a Value inside of /// type \c X. template <class X, class Y> inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, bool> hasa(Y &&MD) { … } template <class X, class Y> inline std::enable_if_t<detail::IsValidReference<X, Y &>::value, bool> hasa(Y &MD) { … } /// Extract a Value from Metadata. /// /// As an analogue to \a cast(), extract the \a Value subclass \c X from \c MD. template <class X, class Y> inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *> extract(Y &&MD) { … } template <class X, class Y> inline std::enable_if_t<detail::IsValidReference<X, Y &>::value, X *> extract(Y &MD) { … } /// Extract a Value from Metadata, allowing null. /// /// As an analogue to \a cast_or_null(), extract the \a Value subclass \c X /// from \c MD, allowing \c MD to be null. template <class X, class Y> inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *> extract_or_null(Y &&MD) { … } /// Extract a Value from Metadata, if any. /// /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a /// Value it does contain is of the wrong subclass. template <class X, class Y> inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *> dyn_extract(Y &&MD) { … } /// Extract a Value from Metadata, if any, allowing null. /// /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a /// Value it does contain is of the wrong subclass, allowing \c MD to be null. template <class X, class Y> inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *> dyn_extract_or_null(Y &&MD) { … } } // end namespace mdconst //===----------------------------------------------------------------------===// /// A single uniqued string. /// /// These are used to efficiently contain a byte sequence for metadata. /// MDString is always unnamed. class MDString : public Metadata { … }; /// A collection of metadata nodes that might be associated with a /// memory access used by the alias-analysis infrastructure. struct AAMDNodes { … }; // Specialize DenseMapInfo for AAMDNodes. template<> struct DenseMapInfo<AAMDNodes> { … }; /// Tracking metadata reference owned by Metadata. /// /// Similar to \a TrackingMDRef, but it's expected to be owned by an instance /// of \a Metadata, which has the option of registering itself for callbacks to /// re-unique itself. /// /// In particular, this is used by \a MDNode. class MDOperand { … }; template <> struct simplify_type<MDOperand> { … }; template <> struct simplify_type<const MDOperand> { … }; /// Pointer to the context, with optional RAUW support. /// /// Either a raw (non-null) pointer to the \a LLVMContext, or an owned pointer /// to \a ReplaceableMetadataImpl (which has a reference to \a LLVMContext). class ContextAndReplaceableUses { … }; struct TempMDNodeDeleter { … }; #define HANDLE_MDNODE_LEAF … #define HANDLE_MDNODE_BRANCH … #include "llvm/IR/Metadata.def" /// Metadata node. /// /// Metadata nodes can be uniqued, like constants, or distinct. Temporary /// metadata nodes (with full support for RAUW) can be used to delay uniquing /// until forward references are known. The basic metadata node is an \a /// MDTuple. /// /// There is limited support for RAUW at construction time. At construction /// time, if any operand is a temporary node (or an unresolved uniqued node, /// which indicates a transitive temporary operand), the node itself will be /// unresolved. As soon as all operands become resolved, it will drop RAUW /// support permanently. /// /// If an unresolved node is part of a cycle, \a resolveCycles() needs /// to be called on some member of the cycle once all temporary nodes have been /// replaced. /// /// MDNodes can be large or small, as well as resizable or non-resizable. /// Large MDNodes' operands are allocated in a separate storage vector, /// whereas small MDNodes' operands are co-allocated. Distinct and temporary /// MDnodes are resizable, but only MDTuples support this capability. /// /// Clients can add operands to resizable MDNodes using push_back(). class MDNode : public Metadata { … }; /// Tuple of metadata. /// /// This is the simple \a MDNode arbitrary tuple. Nodes are uniqued by /// default based on their operands. class MDTuple : public MDNode { … }; MDTuple *MDNode::get(LLVMContext &Context, ArrayRef<Metadata *> MDs) { … } MDTuple *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) { … } MDTuple *MDNode::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) { … } TempMDTuple MDNode::getTemporary(LLVMContext &Context, ArrayRef<Metadata *> MDs) { … } void TempMDNodeDeleter::operator()(MDNode *Node) const { … } /// This is a simple wrapper around an MDNode which provides a higher-level /// interface by hiding the details of how alias analysis information is encoded /// in its operands. class AliasScopeNode { … }; /// Typed iterator through MDNode operands. /// /// An iterator that transforms an \a MDNode::iterator into an iterator over a /// particular Metadata subclass. template <class T> class TypedMDOperandIterator { … }; /// Typed, array-like tuple of metadata. /// /// This is a wrapper for \a MDTuple that makes it act like an array holding a /// particular type of metadata. template <class T> class MDTupleTypedArrayWrapper { … }; #define HANDLE_METADATA … #include "llvm/IR/Metadata.def" /// Placeholder metadata for operands of distinct MDNodes. /// /// This is a lightweight placeholder for an operand of a distinct node. It's /// purpose is to help track forward references when creating a distinct node. /// This allows distinct nodes involved in a cycle to be constructed before /// their operands without requiring a heavyweight temporary node with /// full-blown RAUW support. /// /// Each placeholder supports only a single MDNode user. Clients should pass /// an ID, retrieved via \a getID(), to indicate the "real" operand that this /// should be replaced with. /// /// While it would be possible to implement move operators, they would be /// fairly expensive. Leave them unimplemented to discourage their use /// (clients can use std::deque, std::list, BumpPtrAllocator, etc.). class DistinctMDOperandPlaceholder : public Metadata { … }; //===----------------------------------------------------------------------===// /// A tuple of MDNodes. /// /// Despite its name, a NamedMDNode isn't itself an MDNode. /// /// NamedMDNodes are named module-level entities that contain lists of MDNodes. /// /// It is illegal for a NamedMDNode to appear as an operand of an MDNode. class NamedMDNode : public ilist_node<NamedMDNode> { … }; // Create wrappers for C Binding types (see CBindingWrapping.h). DEFINE_ISA_CONVERSION_FUNCTIONS(…) } // end namespace llvm #endif // LLVM_IR_METADATA_H