Use ilist_tratis to autoinsert and remove NamedMDNode from MDSymbolTable.
authorDevang Patel <dpatel@apple.com>
Tue, 12 Jan 2010 18:34:06 +0000 (18:34 +0000)
committerDevang Patel <dpatel@apple.com>
Tue, 12 Jan 2010 18:34:06 +0000 (18:34 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93247 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Metadata.h
include/llvm/Module.h
include/llvm/ValueSymbolTable.h
lib/VMCore/Metadata.cpp
lib/VMCore/Module.cpp

index 282ee85e58328434d2eb2e62298ea5519b0a7bc8..f490bad2cc1c22be791ddc0edce6f2f5ff39d4a4 100644 (file)
@@ -184,8 +184,8 @@ private:
 /// NamedMDNode is always named. All NamedMDNode operand has a type of metadata.
 class NamedMDNode : public Value, public ilist_node<NamedMDNode> {
   friend class SymbolTableListTraits<NamedMDNode, Module>;
+  friend class ilist_traits<NamedMDNode>;
   friend class LLVMContextImpl;
-
   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
 
   std::string Name;
index a5a2ad46005751b3226dc1f185d7b1237dd60101..3c8055d09fd799c0ad5a7176849a934773394bcf 100644 (file)
@@ -57,6 +57,7 @@ template<> struct ilist_traits<GlobalAlias>
   static GlobalAlias *createSentinel();
   static void destroySentinel(GlobalAlias *GA) { delete GA; }
 };
+
 template<> struct ilist_traits<NamedMDNode>
   : public SymbolTableListTraits<NamedMDNode, Module> {
   // createSentinel is used to get hold of a node that marks the end of
@@ -69,6 +70,8 @@ template<> struct ilist_traits<NamedMDNode>
   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<NamedMDNode> 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
 /// @{
index 060dba3101854c2d94dd69c8e5bdd161c48934dc..53815ba7a4e6bda90f8edd4ae65604b8af069e55 100644 (file)
@@ -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<typename ValueSubClass, typename ItemParentClass>
@@ -39,7 +40,6 @@ class ValueSymbolTable {
   friend class SymbolTableListTraits<Function, Module>;
   friend class SymbolTableListTraits<GlobalVariable, Module>;
   friend class SymbolTableListTraits<GlobalAlias, Module>;
-  friend class SymbolTableListTraits<NamedMDNode, Module>;
 /// @name Types
 /// @{
 public:
@@ -133,6 +133,7 @@ private:
 /// essentially a StringMap wrapper.
 
 class MDSymbolTable {
+  friend class SymbolTableListTraits<NamedMDNode, Module>;
 /// @name Types
 /// @{
 private:
index 14993134c7fff25a67393a6225360a4bbc368149..5ea7bd45e57807921a62665d18f1d85204cb46ca 100644 (file)
@@ -239,6 +239,26 @@ void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
 //===----------------------------------------------------------------------===//
 // NamedMDNode implementation.
 //
+
+namespace llvm {
+// SymbolTableListTraits specialization for MDSymbolTable.
+void ilist_traits<NamedMDNode>
+::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<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
+  N->setParent(0);
+  Module *Owner = getListOwner();
+  MDSymbolTable &ST = Owner->getMDSymbolTable();
+  ST.remove(N->getName());
+}
+}
+
 static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
   return *(SmallVector<WeakVH, 4>*)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.
index 510f3d5bd77c23aef8feb37ee00b5caa55fbf652..503e70891721bfa4f8cc6c4992edb3dada455229 100644 (file)
@@ -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.
 //