diff --git a/include/llvm/Metadata.h b/include/llvm/Metadata.h index 491b469193d..87fe17cf7eb 100644 --- a/include/llvm/Metadata.h +++ b/include/llvm/Metadata.h @@ -175,15 +175,16 @@ class NamedMDNode : public MetadataBase, public ilist_node { NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT + std::string Name; Module *Parent; void *Operands; // SmallVector, 4> void setParent(Module *M) { Parent = M; } protected: - explicit NamedMDNode(LLVMContext &C, const Twine &N, MDNode*const *Vals, + explicit NamedMDNode(LLVMContext &C, StringRef N, MDNode*const *Vals, unsigned NumVals, Module *M = 0); public: - static NamedMDNode *Create(LLVMContext &C, const Twine &N, + static NamedMDNode *Create(LLVMContext &C, StringRef N, MDNode *const *MDs, unsigned NumMDs, Module *M = 0) { return new NamedMDNode(C, N, MDs, NumMDs, M); @@ -213,7 +214,13 @@ public: /// addOperand - Add metadata operand. void addOperand(MDNode *M); - + + /// setName - Set the name of this named metadata. + void setName(StringRef Name); + + /// getName - Return a constant reference to this named metadata's name. + StringRef getName() const; + /// 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) { diff --git a/include/llvm/Module.h b/include/llvm/Module.h index 9a8b53ac586..a5a2ad46005 100644 --- a/include/llvm/Module.h +++ b/include/llvm/Module.h @@ -26,6 +26,7 @@ namespace llvm { class FunctionType; class LLVMContext; +class MDSymbolTable; template<> struct ilist_traits : public SymbolTableListTraits { @@ -131,19 +132,20 @@ public: /// @name Member Variables /// @{ private: - LLVMContext &Context; ///< The LLVMContext from which types and - ///< constants are allocated. - GlobalListType GlobalList; ///< The Global Variables in the module - FunctionListType FunctionList; ///< The Functions in the module - AliasListType AliasList; ///< The Aliases in the module - LibraryListType LibraryList; ///< The Libraries needed by the module - NamedMDListType NamedMDList; ///< The named metadata in the module - std::string GlobalScopeAsm; ///< Inline Asm at global scope. - ValueSymbolTable *ValSymTab; ///< Symbol table for values - TypeSymbolTable *TypeSymTab; ///< Symbol table for types - std::string ModuleID; ///< Human readable identifier for the module - std::string TargetTriple; ///< Platform target triple Module compiled on - std::string DataLayout; ///< Target data description + LLVMContext &Context; ///< The LLVMContext from which types and + ///< constants are allocated. + GlobalListType GlobalList; ///< The Global Variables in the module + FunctionListType FunctionList; ///< The Functions in the module + AliasListType AliasList; ///< The Aliases in the module + LibraryListType LibraryList; ///< The Libraries needed by the module + NamedMDListType NamedMDList; ///< The named metadata in the module + std::string GlobalScopeAsm; ///< Inline Asm at global scope. + ValueSymbolTable *ValSymTab; ///< Symbol table for values + TypeSymbolTable *TypeSymTab; ///< Symbol table for types + std::string ModuleID; ///< Human readable identifier for the module + std::string TargetTriple; ///< Platform target triple Module compiled on + std::string DataLayout; ///< Target data description + MDSymbolTable *NamedMDSymTab; ///< NamedMDNode names. friend class Constant; @@ -322,6 +324,10 @@ public: /// NamedMDNode with the specified name is not found. NamedMDNode *getOrInsertNamedMetadata(StringRef Name); + /// addMDNodeName - Insert an entry in the NamedMDNode symbol table mapping + /// Name to NMD. + void addMDNodeName(StringRef Name, NamedMDNode *NMD); + /// @} /// @name Type Accessors /// @{ @@ -379,6 +385,10 @@ public: const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; } /// Get the Module's symbol table of types TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; } + /// Get the symbol table of named metadata + const MDSymbolTable &getMDSymbolTable() const { return *NamedMDSymTab; } + /// Get the Module's symbol table of named metadata + MDSymbolTable &getMDSymbolTable() { return *NamedMDSymTab; } /// @} /// @name Global Variable Iteration diff --git a/include/llvm/ValueSymbolTable.h b/include/llvm/ValueSymbolTable.h index e05fdbd08dd..e5ea49f39b1 100644 --- a/include/llvm/ValueSymbolTable.h +++ b/include/llvm/ValueSymbolTable.h @@ -26,7 +26,7 @@ namespace llvm { class NamedMDNode; class Module; class StringRef; - + /// This class provides a symbol table of name/value pairs. It is essentially /// a std::map but has a controlled interface provided by /// LLVM as well as ensuring uniqueness of names. @@ -129,6 +129,84 @@ private: /// @} }; +/// This class provides a symbol table of name/NamedMDNode pairs. It is +/// essentially a StringMap wrapper. + +class MDSymbolTable { +/// @name Types +/// @{ +public: + /// @brief A mapping of names to metadata + typedef StringMap MDMap; + + /// @brief An iterator over a ValueMap. + typedef MDMap::iterator iterator; + + /// @brief A const_iterator over a ValueMap. + typedef MDMap::const_iterator const_iterator; + +/// @} +/// @name Constructors +/// @{ +public: + + MDSymbolTable() : mmap(0) {} + ~MDSymbolTable(); + +/// @} +/// @name Accessors +/// @{ +public: + + /// This method finds the value with the given \p Name in the + /// the symbol table. + /// @returns the NamedMDNode associated with the \p Name + /// @brief Lookup a named Value. + NamedMDNode *lookup(StringRef Name) const { return mmap.lookup(Name); } + + /// @returns true iff the symbol table is empty + /// @brief Determine if the symbol table is empty + inline bool empty() const { return mmap.empty(); } + + /// @brief The number of name/type pairs is returned. + inline unsigned size() const { return unsigned(mmap.size()); } + +/// @} +/// @name Iteration +/// @{ +public: + /// @brief Get an iterator that from the beginning of the symbol table. + inline iterator begin() { return mmap.begin(); } + + /// @brief Get a const_iterator that from the beginning of the symbol table. + inline const_iterator begin() const { return mmap.begin(); } + + /// @brief Get an iterator to the end of the symbol table. + inline iterator end() { return mmap.end(); } + + /// @brief Get a const_iterator to the end of the symbol table. + inline const_iterator end() const { return mmap.end(); } + +/// @} +/// @name Mutators +/// @{ +public: + /// insert - The method inserts a new entry into the stringmap. + void insert(StringRef Name, NamedMDNode *Node) { + (void) mmap.GetOrCreateValue(Name, Node); + } + + /// This method removes a NamedMDNode from the symbol table. + void remove(StringRef Name) { mmap.erase(Name); } + +/// @} +/// @name Internal Data +/// @{ +private: + MDMap mmap; ///< The map that holds the symbol table. +/// @} +}; + } // End llvm namespace #endif diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index ce9036bd06e..d34e24ccb5a 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -528,10 +528,9 @@ static void WriteModuleMetadata(const ValueEnumerator &VE, } // Write name. - std::string Str = NMD->getNameStr(); - const char *StrBegin = Str.c_str(); - for (unsigned i = 0, e = Str.length(); i != e; ++i) - Record.push_back(StrBegin[i]); + StringRef Str = NMD->getName(); + for (unsigned i = 0, e = Str.size(); i != e; ++i) + Record.push_back(Str[i]); Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/); Record.clear(); diff --git a/lib/Bitcode/Writer/ValueEnumerator.cpp b/lib/Bitcode/Writer/ValueEnumerator.cpp index c409c2066d1..54bf84d261d 100644 --- a/lib/Bitcode/Writer/ValueEnumerator.cpp +++ b/lib/Bitcode/Writer/ValueEnumerator.cpp @@ -74,9 +74,10 @@ ValueEnumerator::ValueEnumerator(const Module *M) { // Enumerate types used by the type symbol table. EnumerateTypeSymbolTable(M->getTypeSymbolTable()); - // Insert constants that are named at module level into the slot pool so that - // the module symbol table can refer to them... + // Insert constants and metadata that are named at module level into the slot + // pool so that the module symbol table can refer to them... EnumerateValueSymbolTable(M->getValueSymbolTable()); + EnumerateMDSymbolTable(M->getMDSymbolTable()); SmallVector, 8> MDs; @@ -196,6 +197,14 @@ void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) { EnumerateValue(VI->getValue()); } +/// EnumerateMDSymbolTable - Insert all of the values in the specified metadata +/// table. +void ValueEnumerator::EnumerateMDSymbolTable(const MDSymbolTable &MST) { + for (MDSymbolTable::const_iterator MI = MST.begin(), ME = MST.end(); + MI != ME; ++MI) + EnumerateValue(MI->getValue()); +} + void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD) { // Check to see if it's already in! unsigned &MDValueID = MDValueMap[MD]; diff --git a/lib/Bitcode/Writer/ValueEnumerator.h b/lib/Bitcode/Writer/ValueEnumerator.h index 3c83e356956..a1b188ef9ba 100644 --- a/lib/Bitcode/Writer/ValueEnumerator.h +++ b/lib/Bitcode/Writer/ValueEnumerator.h @@ -30,6 +30,7 @@ class MetadataBase; class AttrListPtr; class TypeSymbolTable; class ValueSymbolTable; +class MDSymbolTable; class ValueEnumerator { public: @@ -133,6 +134,7 @@ private: void EnumerateTypeSymbolTable(const TypeSymbolTable &ST); void EnumerateValueSymbolTable(const ValueSymbolTable &ST); + void EnumerateMDSymbolTable(const MDSymbolTable &ST); }; } // End llvm namespace diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index 09cd1d5cbae..6cc1e1bdedb 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -214,20 +214,21 @@ static SmallVector &getNMDOps(void *Operands) { return *(SmallVector*)Operands; } -NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N, +NamedMDNode::NamedMDNode(LLVMContext &C, StringRef N, MDNode *const *MDs, unsigned NumMDs, Module *ParentModule) : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) { setName(N); - Operands = new SmallVector(); SmallVector &Node = getNMDOps(Operands); for (unsigned i = 0; i != NumMDs; ++i) Node.push_back(WeakVH(MDs[i])); - if (ParentModule) + if (ParentModule) { ParentModule->getNamedMDList().push_back(this); + ParentModule->addMDNodeName(N, this); + } } NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) { @@ -265,6 +266,7 @@ void NamedMDNode::addOperand(MDNode *M) { /// eraseFromParent - Drop all references and remove the node from parent /// module. void NamedMDNode::eraseFromParent() { + getParent()->getMDSymbolTable().remove(getName()); getParent()->getNamedMDList().erase(this); } @@ -273,6 +275,16 @@ void NamedMDNode::dropAllReferences() { getNMDOps(Operands).clear(); } +/// setName - Set the name of this named metadata. +void NamedMDNode::setName(StringRef N) { + if (!N.empty()) + Name = N.str(); +} + +/// getName - Return a constant reference to this named metadata's name. +StringRef NamedMDNode::getName() const { + return StringRef(Name); +} //===----------------------------------------------------------------------===// // LLVMContext MDKind naming implementation. diff --git a/lib/VMCore/Module.cpp b/lib/VMCore/Module.cpp index a7f503bacb9..e25a29517a4 100644 --- a/lib/VMCore/Module.cpp +++ b/lib/VMCore/Module.cpp @@ -59,6 +59,7 @@ Module::Module(StringRef MID, LLVMContext& C) : Context(C), ModuleID(MID), DataLayout("") { ValSymTab = new ValueSymbolTable(); TypeSymTab = new TypeSymbolTable(); + NamedMDSymTab = new MDSymbolTable(); } Module::~Module() { @@ -307,20 +308,25 @@ GlobalAlias *Module::getNamedAlias(StringRef Name) const { /// specified name. This method returns null if a NamedMDNode with the //// specified name is not found. NamedMDNode *Module::getNamedMetadata(StringRef Name) const { - return dyn_cast_or_null(getValueSymbolTable().lookup(Name)); + return NamedMDSymTab->lookup(Name); } /// getOrInsertNamedMetadata - Return the first named MDNode in the module /// with the specified name. This method returns a new NamedMDNode if a /// NamedMDNode with the specified name is not found. NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { - NamedMDNode *NMD = - dyn_cast_or_null(getValueSymbolTable().lookup(Name)); + NamedMDNode *NMD = NamedMDSymTab->lookup(Name); if (!NMD) NMD = NamedMDNode::Create(getContext(), Name, NULL, 0, this); return NMD; } +/// addMDNodeName - Insert an entry in the NamedMDNode symbol table mapping +/// Name to NMD. +void Module::addMDNodeName(StringRef Name, NamedMDNode *NMD) { + NamedMDSymTab->insert(Name, NMD); +} + //===----------------------------------------------------------------------===// // Methods for easy access to the types in the module. // diff --git a/test/Feature/NamedMDNode.ll b/test/Feature/NamedMDNode.ll index a56480fceb4..dbd34ed0d97 100644 --- a/test/Feature/NamedMDNode.ll +++ b/test/Feature/NamedMDNode.ll @@ -4,3 +4,6 @@ !0 = metadata !{i32 42} !1 = metadata !{metadata !"foo"} !llvm.stuff = !{!0, !1, null} + +!samename = !{!0, !1} +define void @samename() {}