From: Devang Patel Date: Wed, 29 Jul 2009 00:33:07 +0000 (+0000) Subject: Add NamedMDNode. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=f457d1316dec017cf204b54524878310c356bf64;p=oota-llvm.git Add NamedMDNode. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77409 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Metadata.h b/include/llvm/Metadata.h index baa9cb8d71c..9ee9b43eb77 100644 --- a/include/llvm/Metadata.h +++ b/include/llvm/Metadata.h @@ -26,7 +26,7 @@ namespace llvm { //===----------------------------------------------------------------------===// -// MetadataBase - A base class for MDNode and MDString. +// MetadataBase - A base class for MDNode, MDString and NamedMDNode. class MetadataBase : public Value { protected: MetadataBase(const Type *Ty, unsigned scid) @@ -49,14 +49,15 @@ public: /// Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const MDString *) { return true; } static bool classof(const Value *V) { - return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal; + return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal + || V->getValueID() == NamedMDNodeVal; } }; //===----------------------------------------------------------------------===// /// MDString - a single uniqued string. /// These are used to efficiently contain a byte sequence for metadata. -/// +/// MDString is always unnamd. class MDString : public MetadataBase { MDString(const MDString &); // DO NOT IMPLEMENT StringRef Str; @@ -89,7 +90,7 @@ public: //===----------------------------------------------------------------------===// /// MDNode - a tuple of other values. /// These contain a list of the values that represent the metadata. -/// +/// MDNode is always unnamed. class MDNode : public MetadataBase, public FoldingSetNode { MDNode(const MDNode &); // DO NOT IMPLEMENT @@ -151,6 +152,104 @@ public: } }; +//===----------------------------------------------------------------------===// +/// WeakMetadataVH - a weak value handle for metadata. +class WeakMetadataVH : public WeakVH { +public: + WeakMetadataVH() : WeakVH() {} + WeakMetadataVH(MetadataBase *M) : WeakVH(M) {} + WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {} + + operator Value*() const { + llvm_unreachable("WeakMetadataVH only handles Metadata"); + } + + operator MetadataBase*() const { + return cast(getValPtr()); + } +}; + +//===----------------------------------------------------------------------===// +/// NamedMDNode - a tuple of other metadata. +/// NamedMDNode is always named. All NamedMDNode element has a type of metadata. +class NamedMDNode : public MetadataBase { + NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT + + friend class LLVMContextImpl; + + Module *Parent; + StringRef Name; + SmallVector Node; + typedef SmallVectorImpl::iterator elem_iterator; + +protected: + explicit NamedMDNode(const char *N, unsigned NameLength, + MetadataBase*const* Vals, unsigned NumVals, + Module *M = 0); +public: + static NamedMDNode *Create(const char *N, unsigned NamedLength, + MetadataBase*const*MDs, unsigned NumMDs, + Module *M = 0) { + return new NamedMDNode(N, NamedLength, MDs, NumMDs, M); + } + + typedef SmallVectorImpl::const_iterator const_elem_iterator; + + StringRef getName() const { return Name; } + + /// getParent - Get the module that holds this named metadata collection. + inline Module *getParent() { return Parent; } + inline const Module *getParent() const { return Parent; } + + Value *getElement(unsigned i) const { + return Node[i]; + } + + unsigned getNumElements() const { + return Node.size(); + } + + bool elem_empty() const { + return Node.empty(); + } + + const_elem_iterator elem_begin() const { + return Node.begin(); + } + + const_elem_iterator elem_end() const { + return Node.end(); + } + + /// getType() specialization - Type is always MetadataTy. + /// + inline const Type *getType() const { + return Type::MetadataTy; + } + + /// isNullValue - Return true if this is the value that would be returned by + /// getNullValue. This always returns false because getNullValue will never + /// produce metadata. + virtual bool isNullValue() const { + return false; + } + + /// Profile - calculate a unique identifier for this MDNode to collapse + /// duplicates + void Profile(FoldingSetNodeID &ID) const; + + virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) { + llvm_unreachable( + "This should never be called because NamedMDNodes have no ops"); + } + + /// Methods for support type inquiry through isa, cast, and dyn_cast: + static inline bool classof(const NamedMDNode *) { return true; } + static bool classof(const Value *V) { + return V->getValueID() == NamedMDNodeVal; + } +}; + } // end llvm namespace #endif diff --git a/include/llvm/Value.h b/include/llvm/Value.h index f690f449a64..b1db1ce3e1e 100644 --- a/include/llvm/Value.h +++ b/include/llvm/Value.h @@ -219,6 +219,7 @@ public: ConstantPointerNullVal, // This is an instance of ConstantPointerNull MDNodeVal, // This is an instance of MDNode MDStringVal, // This is an instance of MDString + NamedMDNodeVal, // This is an instance of NamedMDNode InlineAsmVal, // This is an instance of InlineAsm PseudoSourceValueVal, // This is an instance of PseudoSourceValue InstructionVal, // This is an instance of Instruction diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index 1a6288e46a6..73f89caa388 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Metadata.h" +#include "llvm/Module.h" using namespace llvm; //===----------------------------------------------------------------------===// @@ -27,3 +28,17 @@ void MDNode::Profile(FoldingSetNodeID &ID) const { for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I) ID.AddPointer(*I); } + +//===----------------------------------------------------------------------===// +//NamedMDNode implementation +// +NamedMDNode::NamedMDNode(const char *N, unsigned NameLength, + MetadataBase*const* MDs, unsigned NumMDs, + Module *M) + : MetadataBase(Type::MetadataTy, Value::NamedMDNodeVal), + Parent(M), Name(N, NameLength) { + for (unsigned i = 0; i != NumMDs; ++i) + Node.push_back(WeakMetadataVH(MDs[i])); + + // FIXME : Add into the parent module. +}