From 26028f27ddd132b3284943e11aca130c2911abc4 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Tue, 12 Jan 2010 18:34:06 +0000 Subject: [PATCH] Use ilist_tratis to autoinsert and remove NamedMDNode from MDSymbolTable. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93247 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Metadata.h | 2 +- include/llvm/Module.h | 7 +++---- include/llvm/ValueSymbolTable.h | 3 ++- lib/VMCore/Metadata.cpp | 31 +++++++++++++++++++++++++------ lib/VMCore/Module.cpp | 6 ------ 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/include/llvm/Metadata.h b/include/llvm/Metadata.h index 282ee85e583..f490bad2cc1 100644 --- a/include/llvm/Metadata.h +++ b/include/llvm/Metadata.h @@ -184,8 +184,8 @@ private: /// NamedMDNode is always named. All NamedMDNode operand has a type of metadata. class NamedMDNode : public Value, public ilist_node { friend class SymbolTableListTraits; + friend class ilist_traits; friend class LLVMContextImpl; - NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT std::string Name; diff --git a/include/llvm/Module.h b/include/llvm/Module.h index a5a2ad46005..3c8055d09fd 100644 --- a/include/llvm/Module.h +++ b/include/llvm/Module.h @@ -57,6 +57,7 @@ template<> struct ilist_traits static GlobalAlias *createSentinel(); static void destroySentinel(GlobalAlias *GA) { delete GA; } }; + template<> struct ilist_traits : public SymbolTableListTraits { // createSentinel is used to get hold of a node that marks the end of @@ -69,6 +70,8 @@ template<> struct ilist_traits NamedMDNode *provideInitialHead() const { return createSentinel(); } NamedMDNode *ensureHead(NamedMDNode*) const { return createSentinel(); } static void noteHead(NamedMDNode*, NamedMDNode*) {} + void addNodeToList(NamedMDNode *N); + void removeNodeFromList(NamedMDNode *N); private: mutable ilist_node Sentinel; }; @@ -324,10 +327,6 @@ 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 /// @{ diff --git a/include/llvm/ValueSymbolTable.h b/include/llvm/ValueSymbolTable.h index 060dba31018..53815ba7a4e 100644 --- a/include/llvm/ValueSymbolTable.h +++ b/include/llvm/ValueSymbolTable.h @@ -17,6 +17,7 @@ #include "llvm/Value.h" #include "llvm/ADT/StringMap.h" #include "llvm/System/DataTypes.h" +#include "llvm/ADT/ilist_node.h" namespace llvm { template @@ -39,7 +40,6 @@ class ValueSymbolTable { friend class SymbolTableListTraits; friend class SymbolTableListTraits; friend class SymbolTableListTraits; - friend class SymbolTableListTraits; /// @name Types /// @{ public: @@ -133,6 +133,7 @@ private: /// essentially a StringMap wrapper. class MDSymbolTable { + friend class SymbolTableListTraits; /// @name Types /// @{ private: diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index 14993134c7f..5ea7bd45e57 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -239,6 +239,26 @@ void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) { //===----------------------------------------------------------------------===// // NamedMDNode implementation. // + +namespace llvm { +// SymbolTableListTraits specialization for MDSymbolTable. +void ilist_traits +::addNodeToList(NamedMDNode *N) { + assert(N->getParent() == 0 && "Value already in a container!!"); + Module *Owner = getListOwner(); + N->setParent(Owner); + MDSymbolTable &ST = Owner->getMDSymbolTable(); + ST.insert(N->getName(), N); +} + +void ilist_traits::removeNodeFromList(NamedMDNode *N) { + N->setParent(0); + Module *Owner = getListOwner(); + MDSymbolTable &ST = Owner->getMDSymbolTable(); + ST.remove(N->getName()); +} +} + static SmallVector &getNMDOps(void *Operands) { return *(SmallVector*)Operands; } @@ -254,10 +274,8 @@ NamedMDNode::NamedMDNode(LLVMContext &C, StringRef N, 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) { @@ -295,7 +313,6 @@ 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); } @@ -306,8 +323,10 @@ void NamedMDNode::dropAllReferences() { /// setName - Set the name of this named metadata. void NamedMDNode::setName(StringRef N) { - if (!N.empty()) - Name = N.str(); + assert (!N.empty() && "Invalid named metadata name!"); + Name = N.str(); + if (Parent) + Parent->getMDSymbolTable().insert(N, this); } /// getName - Return a constant reference to this named metadata's name. diff --git a/lib/VMCore/Module.cpp b/lib/VMCore/Module.cpp index 510f3d5bd77..503e7089172 100644 --- a/lib/VMCore/Module.cpp +++ b/lib/VMCore/Module.cpp @@ -325,12 +325,6 @@ NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { 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. // -- 2.34.1