X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FIR%2FMetadata.h;h=df8ce354bb7fdfba62a3d51d7502b3a39c503ab7;hb=HEAD;hp=449403f2a5e7b9f9ccdb027e0647d4d53b26e890;hpb=ce8f144e00580fbf4400d18750fc3d37d085239b;p=oota-llvm.git diff --git a/include/llvm/IR/Metadata.h b/include/llvm/IR/Metadata.h index 449403f2a5e..df8ce354bb7 100644 --- a/include/llvm/IR/Metadata.h +++ b/include/llvm/IR/Metadata.h @@ -1,4 +1,4 @@ -//===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===// +//===- llvm/IR/Metadata.h - Metadata definitions ----------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -18,23 +18,23 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/ilist_node.h" #include "llvm/ADT/iterator_range.h" #include "llvm/IR/Constant.h" -#include "llvm/IR/MetadataTracking.h" +#include "llvm/IR/LLVMContext.h" #include "llvm/IR/Value.h" #include "llvm/Support/ErrorHandling.h" #include namespace llvm { + class LLVMContext; class Module; -template - class SymbolTableListTraits; - +class ModuleSlotTracker; enum LLVMConstants : uint32_t { - DEBUG_METADATA_VERSION = 2 // Current debug info version number. + DEBUG_METADATA_VERSION = 3 // Current debug info version number. }; /// \brief Root of the metadata hierarchy. @@ -51,7 +51,7 @@ protected: enum StorageType { Uniqued, Distinct, Temporary }; /// \brief Storage flag for non-uniqued, otherwise unowned, metadata. - StorageType Storage : 2; + unsigned Storage : 2; // TODO: expose remaining bits to subclasses. unsigned short SubclassData16; @@ -60,17 +60,40 @@ protected: public: enum MetadataKind { MDTupleKind, - MDLocationKind, + DILocationKind, + GenericDINodeKind, + DISubrangeKind, + DIEnumeratorKind, + DIBasicTypeKind, + DIDerivedTypeKind, + DICompositeTypeKind, + DISubroutineTypeKind, + DIFileKind, + DICompileUnitKind, + DISubprogramKind, + DILexicalBlockKind, + DILexicalBlockFileKind, + DINamespaceKind, + DIModuleKind, + DITemplateTypeParameterKind, + DITemplateValueParameterKind, + DIGlobalVariableKind, + DILocalVariableKind, + DIExpressionKind, + DIObjCPropertyKind, + DIImportedEntityKind, ConstantAsMetadataKind, LocalAsMetadataKind, - MDStringKind + MDStringKind, + DIMacroKind, + DIMacroFileKind }; protected: Metadata(unsigned ID, StorageType Storage) : SubclassID(ID), Storage(Storage), SubclassData16(0), SubclassData32(0) { } - ~Metadata() {} + ~Metadata() = default; /// \brief Default handling of a changed operand, which asserts. /// @@ -84,15 +107,57 @@ public: unsigned getMetadataID() const { return SubclassID; } /// \brief User-friendly dump. + /// + /// If \c M is provided, metadata nodes will be numbered canonically; + /// otherwise, pointer addresses are substituted. + /// + /// Note: this uses an explicit overload instead of default arguments so that + /// the nullptr version is easy to call from a debugger. + /// + /// @{ void dump() const; - void print(raw_ostream &OS) const; - void printAsOperand(raw_ostream &OS, bool PrintType = true, + void dump(const Module *M) const; + /// @} + + /// \brief Print. + /// + /// Prints definition of \c this. + /// + /// If \c M is provided, metadata nodes will be numbered canonically; + /// otherwise, pointer addresses are substituted. + /// @{ + void print(raw_ostream &OS, const Module *M = nullptr, + bool IsForDebug = false) const; + void print(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M = nullptr, + bool IsForDebug = false) const; + /// @} + + /// \brief Print as operand. + /// + /// Prints reference of \c this. + /// + /// If \c M is provided, metadata nodes will be numbered canonically; + /// otherwise, pointer addresses are substituted. + /// @{ + void printAsOperand(raw_ostream &OS, const Module *M = nullptr) const; + void printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M = nullptr) const; + /// @} }; #define HANDLE_METADATA(CLASS) class CLASS; #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(CLASS) \ + template <> struct isa_impl { \ + static inline bool doit(const Metadata &MD) { \ + return MD.getMetadataID() == Metadata::CLASS##Kind; \ + } \ + }; +#include "llvm/IR/Metadata.def" + inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) { MD.print(OS); return OS; @@ -112,7 +177,7 @@ class MetadataAsValue : public Value { Metadata *MD; MetadataAsValue(Type *Ty, Metadata *MD); - ~MetadataAsValue(); + ~MetadataAsValue() override; /// \brief Drop use of metadata (during teardown). void dropUse() { MD = nullptr; } @@ -132,6 +197,77 @@ private: void untrack(); }; +/// \brief 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 { +public: + /// \brief Track the reference to metadata. + /// + /// Register \c MD with \c *MD, if the subclass supports tracking. If \c *MD + /// gets RAUW'ed, \c MD will be updated to the new address. If \c *MD gets + /// deleted, \c MD will be set to \c nullptr. + /// + /// If tracking isn't supported, \c *MD will not change. + /// + /// \return true iff tracking is supported by \c MD. + static bool track(Metadata *&MD) { + return track(&MD, *MD, static_cast(nullptr)); + } + + /// \brief Track the reference to metadata for \a Metadata. + /// + /// As \a track(Metadata*&), but with support for calling back to \c Owner to + /// tell it that its operand changed. This could trigger \c Owner being + /// re-uniqued. + static bool track(void *Ref, Metadata &MD, Metadata &Owner) { + return track(Ref, MD, &Owner); + } + + /// \brief Track the reference to metadata for \a MetadataAsValue. + /// + /// As \a track(Metadata*&), but with support for calling back to \c Owner to + /// tell it that its operand changed. This could trigger \c Owner being + /// re-uniqued. + static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) { + return track(Ref, MD, &Owner); + } + + /// \brief Stop tracking a reference to metadata. + /// + /// Stops \c *MD from tracking \c MD. + static void untrack(Metadata *&MD) { untrack(&MD, *MD); } + static void untrack(void *Ref, Metadata &MD); + + /// \brief Move tracking from one reference to another. + /// + /// Semantically equivalent to \c untrack(MD) followed by \c track(New), + /// except that ownership callbacks are maintained. + /// + /// Note: it is an error if \c *MD does not equal \c New. + /// + /// \return true iff tracking is supported by \c MD. + static bool retrack(Metadata *&MD, Metadata *&New) { + return retrack(&MD, *MD, &New); + } + static bool retrack(void *Ref, Metadata &MD, void *New); + + /// \brief Check whether metadata is replaceable. + static bool isReplaceable(const Metadata &MD); + + typedef PointerUnion OwnerTy; + +private: + /// \brief Track a reference to metadata for an owner. + /// + /// Generalized version of tracking. + static bool track(void *Ref, Metadata &MD, OwnerTy Owner); +}; + /// \brief Shared implementation of use-lists for replaceable metadata. /// /// Most metadata cannot be RAUW'ed. This is a shared implementation of @@ -147,14 +283,20 @@ private: LLVMContext &Context; uint64_t NextIndex; SmallDenseMap, 4> UseMap; + /// Flag that can be set to false if this metadata should not be + /// RAUW'ed, e.g. if it is used as the key of a map. + bool CanReplace; public: ReplaceableMetadataImpl(LLVMContext &Context) - : Context(Context), NextIndex(0) {} + : Context(Context), NextIndex(0), CanReplace(true) {} ~ReplaceableMetadataImpl() { assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata"); } + /// Set the CanReplace flag to the given value. + void setCanReplace(bool Replaceable) { CanReplace = Replaceable; } + LLVMContext &getContext() const { return Context; } /// \brief Replace all uses of this with MD. @@ -165,8 +307,8 @@ public: /// \brief Resolve all uses of this. /// /// Resolve all uses of this, turning off RAUW permanently. If \c - /// ResolveUsers, call \a UniquableMDNode::resolve() on any users whose last - /// operand is resolved. + /// ResolveUsers, call \a MDNode::resolve() on any users whose last operand + /// is resolved. void resolveAllUses(bool ResolveUsers = true); private: @@ -201,7 +343,7 @@ protected: : Metadata(ID, Uniqued), ReplaceableMetadataImpl(V->getContext()), V(V) { assert(V && "Expected valid value"); } - ~ValueAsMetadata() {} + ~ValueAsMetadata() = default; public: static ValueAsMetadata *get(Value *V); @@ -444,9 +586,9 @@ dyn_extract_or_null(Y &&MD) { class MDString : public Metadata { friend class StringMapEntry; - MDString(const MDString &) LLVM_DELETED_FUNCTION; - MDString &operator=(MDString &&) LLVM_DELETED_FUNCTION; - MDString &operator=(const MDString &) LLVM_DELETED_FUNCTION; + MDString(const MDString &) = delete; + MDString &operator=(MDString &&) = delete; + MDString &operator=(const MDString &) = delete; StringMapEntry *Entry; MDString() : Metadata(MDStringKind, Uniqued), Entry(nullptr) {} @@ -492,7 +634,7 @@ struct AAMDNodes { bool operator!=(const AAMDNodes &A) const { return !(*this == A); } - LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; } + explicit operator bool() const { return TBAA || Scope || NoAlias; } /// \brief The tag for type-based alias analysis. MDNode *TBAA; @@ -508,10 +650,12 @@ struct AAMDNodes { template<> struct DenseMapInfo { static inline AAMDNodes getEmptyKey() { - return AAMDNodes(DenseMapInfo::getEmptyKey(), 0, 0); + return AAMDNodes(DenseMapInfo::getEmptyKey(), + nullptr, nullptr); } static inline AAMDNodes getTombstoneKey() { - return AAMDNodes(DenseMapInfo::getTombstoneKey(), 0, 0); + return AAMDNodes(DenseMapInfo::getTombstoneKey(), + nullptr, nullptr); } static unsigned getHashValue(const AAMDNodes &Val) { return DenseMapInfo::getHashValue(Val.TBAA) ^ @@ -531,10 +675,10 @@ struct DenseMapInfo { /// /// In particular, this is used by \a MDNode. class MDOperand { - MDOperand(MDOperand &&) LLVM_DELETED_FUNCTION; - MDOperand(const MDOperand &) LLVM_DELETED_FUNCTION; - MDOperand &operator=(MDOperand &&) LLVM_DELETED_FUNCTION; - MDOperand &operator=(const MDOperand &) LLVM_DELETED_FUNCTION; + MDOperand(MDOperand &&) = delete; + MDOperand(const MDOperand &) = delete; + MDOperand &operator=(MDOperand &&) = delete; + MDOperand &operator=(const MDOperand &) = delete; Metadata *MD; @@ -590,15 +734,12 @@ template <> struct simplify_type { class ContextAndReplaceableUses { PointerUnion Ptr; - ContextAndReplaceableUses() LLVM_DELETED_FUNCTION; - ContextAndReplaceableUses(ContextAndReplaceableUses &&) - LLVM_DELETED_FUNCTION; - ContextAndReplaceableUses(const ContextAndReplaceableUses &) - LLVM_DELETED_FUNCTION; + ContextAndReplaceableUses() = delete; + ContextAndReplaceableUses(ContextAndReplaceableUses &&) = delete; + ContextAndReplaceableUses(const ContextAndReplaceableUses &) = delete; + ContextAndReplaceableUses &operator=(ContextAndReplaceableUses &&) = delete; ContextAndReplaceableUses & - operator=(ContextAndReplaceableUses &&) LLVM_DELETED_FUNCTION; - ContextAndReplaceableUses & - operator=(const ContextAndReplaceableUses &) LLVM_DELETED_FUNCTION; + operator=(const ContextAndReplaceableUses &) = delete; public: ContextAndReplaceableUses(LLVMContext &Context) : Ptr(&Context) {} @@ -655,28 +796,40 @@ struct TempMDNodeDeleter { inline void operator()(MDNode *Node) const; }; -#define HANDLE_UNIQUABLE_LEAF(CLASS) \ +#define HANDLE_MDNODE_LEAF(CLASS) \ typedef std::unique_ptr Temp##CLASS; -#define HANDLE_UNIQUABLE_BRANCH(CLASS) HANDLE_UNIQUABLE_LEAF(CLASS) +#define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS) #include "llvm/IR/Metadata.def" -//===----------------------------------------------------------------------===// -/// \brief Tuple of metadata. +/// \brief 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. class MDNode : public Metadata { friend class ReplaceableMetadataImpl; + friend class LLVMContextImpl; - MDNode(const MDNode &) LLVM_DELETED_FUNCTION; - void operator=(const MDNode &) LLVM_DELETED_FUNCTION; - void *operator new(size_t) LLVM_DELETED_FUNCTION; + MDNode(const MDNode &) = delete; + void operator=(const MDNode &) = delete; + void *operator new(size_t) = delete; -protected: - ContextAndReplaceableUses Context; - -private: unsigned NumOperands; + unsigned NumUnresolved; protected: - unsigned MDNodeSubclassData; + ContextAndReplaceableUses Context; void *operator new(size_t Size, unsigned NumOps); void operator delete(void *Mem); @@ -692,14 +845,19 @@ protected: } MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, - ArrayRef MDs); - ~MDNode() {} + ArrayRef Ops1, ArrayRef Ops2 = None); + ~MDNode() = default; void dropAllReferences(); MDOperand *mutable_begin() { return mutable_end() - NumOperands; } MDOperand *mutable_end() { return reinterpret_cast(this); } + typedef iterator_range mutable_op_range; + mutable_op_range mutable_operands() { + return mutable_op_range(mutable_begin(), mutable_end()); + } + public: static inline MDTuple *get(LLVMContext &Context, ArrayRef MDs); static inline MDTuple *getIfExists(LLVMContext &Context, @@ -709,9 +867,13 @@ public: static inline TempMDTuple getTemporary(LLVMContext &Context, ArrayRef MDs); + /// \brief Create a (temporary) clone of this. + TempMDNode clone() const; + /// \brief Deallocate a node created by getTemporary. /// - /// The node must not have any users. + /// Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining + /// references will be reset. static void deleteTemporary(MDNode *N); LLVMContext &getContext() const { return Context.getContext(); } @@ -745,23 +907,66 @@ public: Context.getReplaceableUses()->replaceAllUsesWith(MD); } + /// Set the CanReplace flag to the given value. + void setCanReplace(bool Replaceable) { + Context.getReplaceableUses()->setCanReplace(Replaceable); + } + + /// \brief Resolve cycles. + /// + /// Once all forward declarations have been resolved, force cycles to be + /// resolved. This interface is used when there are no more temporaries, + /// and thus unresolved nodes are part of cycles and no longer need RAUW + /// support. + /// + /// \pre No operands (or operands' operands, etc.) have \a isTemporary(). + void resolveCycles() { resolveRecursivelyImpl(/* AllowTemps */ false); } + + /// \brief Resolve cycles while ignoring temporaries. + /// + /// This drops RAUW support for any temporaries, which can no longer + /// be uniqued. + /// + void resolveNonTemporaries() { + resolveRecursivelyImpl(/* AllowTemps */ true); + } + + /// \brief Replace a temporary node with a permanent one. + /// + /// Try to create a uniqued version of \c N -- in place, if possible -- and + /// return it. If \c N cannot be uniqued, return a distinct node instead. + template + static typename std::enable_if::value, T *>::type + replaceWithPermanent(std::unique_ptr N) { + return cast(N.release()->replaceWithPermanentImpl()); + } + /// \brief Replace a temporary node with a uniqued one. /// /// Create a uniqued version of \c N -- in place, if possible -- and return /// it. Takes ownership of the temporary node. + /// + /// \pre N does not self-reference. template - static typename std::enable_if::value, - T *>::type - replaceWithUniqued(std::unique_ptr N); + static typename std::enable_if::value, T *>::type + replaceWithUniqued(std::unique_ptr N) { + return cast(N.release()->replaceWithUniquedImpl()); + } /// \brief Replace a temporary node with a distinct one. /// /// Create a distinct version of \c N -- in place, if possible -- and return /// it. Takes ownership of the temporary node. template - static typename std::enable_if::value, - T *>::type - replaceWithDistinct(std::unique_ptr N); + static typename std::enable_if::value, T *>::type + replaceWithDistinct(std::unique_ptr N) { + return cast(N.release()->replaceWithDistinctImpl()); + } + +private: + MDNode *replaceWithPermanentImpl(); + MDNode *replaceWithUniquedImpl(); + MDNode *replaceWithDistinctImpl(); protected: /// \brief Set an operand. @@ -769,6 +974,55 @@ protected: /// Sets the operand directly, without worrying about uniquing. void setOperand(unsigned I, Metadata *New); + void storeDistinctInContext(); + template + static T *storeImpl(T *N, StorageType Storage, StoreT &Store); + template static T *storeImpl(T *N, StorageType Storage); + +private: + void handleChangedOperand(void *Ref, Metadata *New); + + void resolve(); + void resolveAfterOperandChange(Metadata *Old, Metadata *New); + void decrementUnresolvedOperandCount(); + unsigned countUnresolvedOperands(); + + /// Resolve cycles recursively. If \p AllowTemps is true, then any temporary + /// metadata is ignored, otherwise it asserts when encountering temporary + /// metadata. + void resolveRecursivelyImpl(bool AllowTemps); + + /// \brief Mutate this to be "uniqued". + /// + /// Mutate this so that \a isUniqued(). + /// \pre \a isTemporary(). + /// \pre already added to uniquing set. + void makeUniqued(); + + /// \brief Mutate this to be "distinct". + /// + /// Mutate this so that \a isDistinct(). + /// \pre \a isTemporary(). + void makeDistinct(); + + void deleteAsSubclass(); + MDNode *uniquify(); + void eraseFromStore(); + + template struct HasCachedHash; + template + static void dispatchRecalculateHash(NodeTy *N, std::true_type) { + N->recalculateHash(); + } + template + static void dispatchRecalculateHash(NodeTy *, std::false_type) {} + template + static void dispatchResetHash(NodeTy *N, std::true_type) { + N->setHash(0); + } + template + static void dispatchResetHash(NodeTy *, std::false_type) {} + public: typedef const MDOperand *op_iterator; typedef iterator_range op_range; @@ -791,8 +1045,14 @@ public: /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: static bool classof(const Metadata *MD) { - return MD->getMetadataID() == MDTupleKind || - MD->getMetadataID() == MDLocationKind; + switch (MD->getMetadataID()) { + default: + return false; +#define HANDLE_MDNODE_LEAF(CLASS) \ + case CLASS##Kind: \ + return true; +#include "llvm/IR/Metadata.def" + } } /// \brief Check whether MDNode is a vtable access. @@ -804,130 +1064,40 @@ public: static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B); static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B); static MDNode *getMostGenericRange(MDNode *A, MDNode *B); -}; - -template -typename std::enable_if::value, - NodeTy *>::type -MDNode::replaceWithUniqued(std::unique_ptr Node) { - // Try to uniquify in place. - UniquableMDNode *UniquedNode = Node->uniquify(); - if (UniquedNode == Node.get()) { - Node->makeUniqued(); - return Node.release(); - } - - // Collision, so RAUW instead. - Node->replaceAllUsesWith(UniquedNode); - return cast(UniquedNode); -} - -template -typename std::enable_if::value, - NodeTy *>::type -MDNode::replaceWithDistinct(std::unique_ptr Node) { - Node->makeDistinct(); - return Node.release(); -} - -/// \brief Uniquable metadata node. -/// -/// A uniquable metadata node. This contains the basic functionality -/// for implementing sub-types of \a MDNode that can be uniqued like -/// constants. -/// -/// 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. -class UniquableMDNode : public MDNode { - friend class ReplaceableMetadataImpl; - friend class MDNode; - friend class LLVMContextImpl; - -protected: - /// \brief Create a new node. - /// - /// If \c AllowRAUW, then if any operands are unresolved support RAUW. RAUW - /// will be dropped once all operands have been resolved (or if \a - /// resolveCycles() is called). - UniquableMDNode(LLVMContext &C, unsigned ID, StorageType Storage, - ArrayRef Vals); - ~UniquableMDNode() {} - - void storeDistinctInContext(); - template - static T *storeImpl(T *N, StorageType Storage, StoreT &Store); - -public: - static bool classof(const Metadata *MD) { - return MD->getMetadataID() == MDTupleKind || - MD->getMetadataID() == MDLocationKind; - } + static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B); + static MDNode *getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B); - /// \brief Resolve cycles. - /// - /// Once all forward declarations have been resolved, force cycles to be - /// resolved. - /// - /// \pre No operands (or operands' operands, etc.) have \a isTemporary(). - void resolveCycles(); - -private: - void handleChangedOperand(void *Ref, Metadata *New); - - void resolve(); - void resolveAfterOperandChange(Metadata *Old, Metadata *New); - void decrementUnresolvedOperandCount(); - unsigned countUnresolvedOperands() const; - - /// \brief Mutate this to be "uniqued". - /// - /// Mutate this so that \a isUniqued(). - /// \pre \a isTemporary(). - /// \pre already added to uniquing set. - void makeUniqued(); - - /// \brief Mutate this to be "distinct". - /// - /// Mutate this so that \a isDistinct(). - /// \pre \a isTemporary(). - void makeDistinct(); - - void deleteAsSubclass(); - UniquableMDNode *uniquify(); - void eraseFromStore(); }; /// \brief Tuple of metadata. /// /// This is the simple \a MDNode arbitrary tuple. Nodes are uniqued by /// default based on their operands. -class MDTuple : public UniquableMDNode { +class MDTuple : public MDNode { friend class LLVMContextImpl; - friend class UniquableMDNode; + friend class MDNode; MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash, ArrayRef Vals) - : UniquableMDNode(C, MDTupleKind, Storage, Vals) { + : MDNode(C, MDTupleKind, Storage, Vals) { setHash(Hash); } ~MDTuple() { dropAllReferences(); } - void setHash(unsigned Hash) { MDNodeSubclassData = Hash; } + void setHash(unsigned Hash) { SubclassData32 = Hash; } void recalculateHash(); static MDTuple *getImpl(LLVMContext &Context, ArrayRef MDs, StorageType Storage, bool ShouldCreate = true); + TempMDTuple cloneImpl() const { + return getTemporary(getContext(), + SmallVector(op_begin(), op_end())); + } + public: /// \brief Get the hash, if any. - unsigned getHash() const { return MDNodeSubclassData; } + unsigned getHash() const { return SubclassData32; } static MDTuple *get(LLVMContext &Context, ArrayRef MDs) { return getImpl(Context, MDs, Uniqued); @@ -953,6 +1123,9 @@ public: return TempMDTuple(getImpl(Context, MDs, Temporary)); } + /// \brief Return a (temporary) clone of this. + TempMDTuple clone() const { return cloneImpl(); } + static bool classof(const Metadata *MD) { return MD->getMetadataID() == MDTupleKind; } @@ -976,62 +1149,78 @@ void TempMDNodeDeleter::operator()(MDNode *Node) const { MDNode::deleteTemporary(Node); } -/// \brief Debug location. +/// \brief Typed iterator through MDNode operands. /// -/// A debug location in source code, used for debug info and otherwise. -class MDLocation : public UniquableMDNode { - friend class LLVMContextImpl; - friend class UniquableMDNode; - - MDLocation(LLVMContext &C, StorageType Storage, unsigned Line, - unsigned Column, ArrayRef MDs); - ~MDLocation() { dropAllReferences(); } - - static MDLocation *getImpl(LLVMContext &Context, unsigned Line, - unsigned Column, Metadata *Scope, - Metadata *InlinedAt, StorageType Storage, - bool ShouldCreate = true); - - // Disallow replacing operands. - void replaceOperandWith(unsigned I, Metadata *New) LLVM_DELETED_FUNCTION; +/// An iterator that transforms an \a MDNode::iterator into an iterator over a +/// particular Metadata subclass. +template +class TypedMDOperandIterator + : std::iterator { + MDNode::op_iterator I = nullptr; public: - static MDLocation *get(LLVMContext &Context, unsigned Line, unsigned Column, - Metadata *Scope, Metadata *InlinedAt = nullptr) { - return getImpl(Context, Line, Column, Scope, InlinedAt, Uniqued); - } - static MDLocation *getIfExists(LLVMContext &Context, unsigned Line, - unsigned Column, Metadata *Scope, - Metadata *InlinedAt = nullptr) { - return getImpl(Context, Line, Column, Scope, InlinedAt, Uniqued, - /* ShouldCreate */ false); - } - static MDLocation *getDistinct(LLVMContext &Context, unsigned Line, - unsigned Column, Metadata *Scope, - Metadata *InlinedAt = nullptr) { - return getImpl(Context, Line, Column, Scope, InlinedAt, Distinct); - } - static TempMDLocation getTemporary(LLVMContext &Context, unsigned Line, - unsigned Column, Metadata *Scope, - Metadata *InlinedAt = nullptr) { - return TempMDLocation( - getImpl(Context, Line, Column, Scope, InlinedAt, Temporary)); - } - - unsigned getLine() const { return MDNodeSubclassData; } - unsigned getColumn() const { return SubclassData16; } - Metadata *getScope() const { return getOperand(0); } - Metadata *getInlinedAt() const { - if (getNumOperands() == 2) - return getOperand(1); - return nullptr; + TypedMDOperandIterator() = default; + explicit TypedMDOperandIterator(MDNode::op_iterator I) : I(I) {} + T *operator*() const { return cast_or_null(*I); } + TypedMDOperandIterator &operator++() { + ++I; + return *this; } - - static bool classof(const Metadata *MD) { - return MD->getMetadataID() == MDLocationKind; + TypedMDOperandIterator operator++(int) { + TypedMDOperandIterator Temp(*this); + ++I; + return Temp; } + bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; } + bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; } +}; + +/// \brief 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 MDTupleTypedArrayWrapper { + const MDTuple *N = nullptr; + +public: + MDTupleTypedArrayWrapper() = default; + MDTupleTypedArrayWrapper(const MDTuple *N) : N(N) {} + + template + MDTupleTypedArrayWrapper( + const MDTupleTypedArrayWrapper &Other, + typename std::enable_if::value>::type * = + nullptr) + : N(Other.get()) {} + + template + explicit MDTupleTypedArrayWrapper( + const MDTupleTypedArrayWrapper &Other, + typename std::enable_if::value>::type * = + nullptr) + : N(Other.get()) {} + + explicit operator bool() const { return get(); } + explicit operator MDTuple *() const { return get(); } + + MDTuple *get() const { return const_cast(N); } + MDTuple *operator->() const { return get(); } + MDTuple &operator*() const { return *get(); } + + // FIXME: Fix callers and remove condition on N. + unsigned size() const { return N ? N->getNumOperands() : 0u; } + T *operator[](unsigned I) const { return cast_or_null(N->getOperand(I)); } + + // FIXME: Fix callers and remove condition on N. + typedef TypedMDOperandIterator iterator; + iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); } + iterator end() const { return N ? iterator(N->op_end()) : iterator(); } }; +#define HANDLE_METADATA(CLASS) \ + typedef MDTupleTypedArrayWrapper CLASS##Array; +#include "llvm/IR/Metadata.def" + //===----------------------------------------------------------------------===// /// \brief A tuple of MDNodes. /// @@ -1040,11 +1229,10 @@ public: /// /// TODO: Inherit from Metadata. class NamedMDNode : public ilist_node { - friend class SymbolTableListTraits; friend struct ilist_traits; friend class LLVMContextImpl; friend class Module; - NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION; + NamedMDNode(const NamedMDNode &) = delete; std::string Name; Module *Parent; @@ -1108,7 +1296,7 @@ public: void addOperand(MDNode *M); void setOperand(unsigned I, MDNode *New); StringRef getName() const; - void print(raw_ostream &ROS) const; + void print(raw_ostream &ROS, bool IsForDebug = false) const; void dump() const; // --------------------------------------------------------------------------- @@ -1123,13 +1311,13 @@ public: const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); } inline iterator_range operands() { - return iterator_range(op_begin(), op_end()); + return make_range(op_begin(), op_end()); } inline iterator_range operands() const { - return iterator_range(op_begin(), op_end()); + return make_range(op_begin(), op_end()); } }; } // end llvm namespace -#endif +#endif // LLVM_IR_METADATA_H