[llvm-commits] [llvm] r109061 - in /llvm/trunk: include/llvm/ lib/Analysis/ lib/AsmParser/ lib/Bitcode/Reader/ lib/Bitcode/Writer/ lib/Linker/ lib/Transforms/Utils/ lib/VMCore/ unittests/VMCore/

Dan Gohman gohman at apple.com
Wed Jul 21 16:38:34 PDT 2010


Author: djg
Date: Wed Jul 21 18:38:33 2010
New Revision: 109061

URL: http://llvm.org/viewvc/llvm-project?rev=109061&view=rev
Log:
Make NamedMDNode not be a subclass of Value, and simplify the interface
for creating and populating NamedMDNodes.

Modified:
    llvm/trunk/include/llvm/Metadata.h
    llvm/trunk/include/llvm/Module.h
    llvm/trunk/include/llvm/Value.h
    llvm/trunk/include/llvm/ValueSymbolTable.h
    llvm/trunk/lib/Analysis/DebugInfo.cpp
    llvm/trunk/lib/AsmParser/LLParser.cpp
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
    llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
    llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.h
    llvm/trunk/lib/Linker/LinkModules.cpp
    llvm/trunk/lib/Transforms/Utils/CloneModule.cpp
    llvm/trunk/lib/VMCore/AsmWriter.cpp
    llvm/trunk/lib/VMCore/Metadata.cpp
    llvm/trunk/lib/VMCore/Module.cpp
    llvm/trunk/lib/VMCore/Value.cpp
    llvm/trunk/lib/VMCore/ValueSymbolTable.cpp
    llvm/trunk/lib/VMCore/Verifier.cpp
    llvm/trunk/unittests/VMCore/MetadataTest.cpp

Modified: llvm/trunk/include/llvm/Metadata.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Metadata.h?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Metadata.h (original)
+++ llvm/trunk/include/llvm/Metadata.h Wed Jul 21 18:38:33 2010
@@ -177,10 +177,11 @@
 //===----------------------------------------------------------------------===//
 /// NamedMDNode - a tuple of MDNodes.
 /// NamedMDNode is always named. All NamedMDNode operand has a type of metadata.
-class NamedMDNode : public Value, public ilist_node<NamedMDNode> {
+class NamedMDNode : public ilist_node<NamedMDNode> {
   friend class SymbolTableListTraits<NamedMDNode, Module>;
   friend struct ilist_traits<NamedMDNode>;
   friend class LLVMContextImpl;
+  friend class Module;
   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
 
   std::string Name;
@@ -188,18 +189,11 @@
   void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
 
   void setParent(Module *M) { Parent = M; }
-protected:
-  explicit NamedMDNode(LLVMContext &C, const Twine &N, MDNode*const *Vals, 
-                       unsigned NumVals, Module *M = 0);
-public:
-  static NamedMDNode *Create(LLVMContext &C, const Twine &N,
-                             MDNode *const *MDs, 
-                             unsigned NumMDs, Module *M = 0) {
-    return new NamedMDNode(C, N, MDs, NumMDs, M);
-  }
 
-  static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
+protected:
+  explicit NamedMDNode(const Twine &N);
 
+public:
   /// eraseFromParent - Drop all references and remove the node from parent
   /// module.
   void eraseFromParent();
@@ -223,17 +217,11 @@
   /// addOperand - Add metadata operand.
   void addOperand(MDNode *M);
 
-  /// setName - Set the name of this named metadata.
-  void setName(const Twine &NewName);
-
   /// 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) {
-    return V->getValueID() == NamedMDNodeVal;
-  }
+  /// print - Implement operator<< on NamedMDNode.
+  void print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW = 0) const;
 };
 
 } // end llvm namespace

Modified: llvm/trunk/include/llvm/Module.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Module.h?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Module.h (original)
+++ llvm/trunk/include/llvm/Module.h Wed Jul 21 18:38:33 2010
@@ -28,7 +28,6 @@
 class FunctionType;
 class GVMaterializer;
 class LLVMContext;
-class MDSymbolTable;
 
 template<> struct ilist_traits<Function>
   : public SymbolTableListTraits<Function, Module> {
@@ -61,7 +60,7 @@
 };
 
 template<> struct ilist_traits<NamedMDNode>
-  : public SymbolTableListTraits<NamedMDNode, Module> {
+  : public ilist_default_traits<NamedMDNode> {
   // createSentinel is used to get hold of a node that marks the end of
   // the list...
   NamedMDNode *createSentinel() const {
@@ -72,8 +71,8 @@
   NamedMDNode *provideInitialHead() const { return createSentinel(); }
   NamedMDNode *ensureHead(NamedMDNode*) const { return createSentinel(); }
   static void noteHead(NamedMDNode*, NamedMDNode*) {}
-  void addNodeToList(NamedMDNode *N);
-  void removeNodeFromList(NamedMDNode *N);
+  void addNodeToList(NamedMDNode *N) {}
+  void removeNodeFromList(NamedMDNode *N) {}
 private:
   mutable ilist_node<NamedMDNode> Sentinel;
 };
@@ -100,7 +99,7 @@
   /// The type for the list of aliases.
   typedef iplist<GlobalAlias> AliasListType;
   /// The type for the list of named metadata.
-  typedef iplist<NamedMDNode> NamedMDListType;
+  typedef ilist<NamedMDNode> NamedMDListType;
 
   /// The type for the list of dependent libraries.
   typedef std::vector<std::string> LibraryListType;
@@ -151,7 +150,7 @@
   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.
+  void *NamedMDSymTab;            ///< NamedMDNode names.
 
   friend class Constant;
 
@@ -331,6 +330,10 @@
   /// NamedMDNode with the specified name is not found.
   NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
 
+  /// eraseNamedMetadata - Remove the given NamedMDNode from this module
+  /// and delete it.
+  void eraseNamedMetadata(NamedMDNode *NMD);
+
 /// @}
 /// @name Type Accessors
 /// @{
@@ -417,13 +420,6 @@
   static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
     return &Module::AliasList;
   }
-  /// Get the Module's list of named metadata (constant).
-  const NamedMDListType  &getNamedMDList() const      { return NamedMDList; }
-  /// Get the Module's list of named metadata.
-  NamedMDListType  &getNamedMDList()                  { return NamedMDList; }
-  static iplist<NamedMDNode> Module::*getSublistAccess(NamedMDNode *) {
-    return &Module::NamedMDList;
-  }
   /// Get the symbol table of global variable and function identifiers
   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
   /// Get the Module's symbol table of global variable and function identifiers.
@@ -432,10 +428,6 @@
   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

Modified: llvm/trunk/include/llvm/Value.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Value.h?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Value.h (original)
+++ llvm/trunk/include/llvm/Value.h Wed Jul 21 18:38:33 2010
@@ -220,7 +220,6 @@
     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
     FixedStackPseudoSourceValueVal, // This is an instance of 

Modified: llvm/trunk/include/llvm/ValueSymbolTable.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ValueSymbolTable.h?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ValueSymbolTable.h (original)
+++ llvm/trunk/include/llvm/ValueSymbolTable.h Wed Jul 21 18:38:33 2010
@@ -128,94 +128,6 @@
 /// @}
 };
 
-/// This class provides a symbol table of name/NamedMDNode pairs. It is 
-/// essentially a StringMap wrapper.
-
-class MDSymbolTable {
-  friend class SymbolTableListTraits<NamedMDNode, Module>;
-/// @name Types
-/// @{
-private:
-  /// @brief A mapping of names to metadata
-  typedef StringMap<NamedMDNode*> MDMap;
-
-public:
-  /// @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(const MDNode &);             // DO NOT IMPLEMENT
-  void operator=(const MDSymbolTable &);     // DO NOT IMPLEMENT
-  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. This will
-  /// replace existing entry, if any.
-  void insert(StringRef Name,  NamedMDNode *Node) {
-    StringMapEntry<NamedMDNode *> &Entry = 
-      mmap.GetOrCreateValue(Name, Node);
-    if (Entry.getValue() != Node) {
-      mmap.remove(&Entry);
-      (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

Modified: llvm/trunk/lib/Analysis/DebugInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/DebugInfo.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/DebugInfo.cpp (original)
+++ llvm/trunk/lib/Analysis/DebugInfo.cpp Wed Jul 21 18:38:33 2010
@@ -13,7 +13,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/DebugInfo.h"
-#include "llvm/Target/TargetMachine.h"  // FIXME: LAYERING VIOLATION!
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Intrinsics.h"
@@ -22,6 +21,7 @@
 #include "llvm/Module.h"
 #include "llvm/Analysis/ValueTracking.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/Dwarf.h"
 #include "llvm/Support/raw_ostream.h"
@@ -1072,10 +1072,10 @@
     char One = '\1';
     if (FName.startswith(StringRef(&One, 1)))
       FName = FName.substr(1);
-    NamedMDNode *FnLocals = M.getNamedMetadata(Twine("llvm.dbg.lv.", FName));
-    if (!FnLocals)
-      FnLocals = NamedMDNode::Create(VMContext, Twine("llvm.dbg.lv.", FName),
-                                     NULL, 0, &M);
+
+    SmallString<32> Out;
+    NamedMDNode *FnLocals =
+      M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
     FnLocals->addOperand(Node);
   }
   return DIVariable(Node);

Modified: llvm/trunk/lib/AsmParser/LLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLParser.cpp Wed Jul 21 18:38:33 2010
@@ -543,7 +543,7 @@
       ParseToken(lltok::lbrace, "Expected '{' here"))
     return true;
 
-  SmallVector<MDNode *, 8> Elts;
+  NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
   if (Lex.getKind() != lltok::rbrace)
     do {
       if (ParseToken(lltok::exclaim, "Expected '!' here"))
@@ -551,13 +551,12 @@
     
       MDNode *N = 0;
       if (ParseMDNodeID(N)) return true;
-      Elts.push_back(N);
+      NMD->addOperand(N);
     } while (EatIfPresent(lltok::comma));
 
   if (ParseToken(lltok::rbrace, "expected end of metadata node"))
     return true;
 
-  NamedMDNode::Create(Context, Name, Elts.data(), Elts.size(), M);
   return false;
 }
 

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Wed Jul 21 18:38:33 2010
@@ -801,16 +801,13 @@
 
       // Read named metadata elements.
       unsigned Size = Record.size();
-      SmallVector<MDNode *, 8> Elts;
+      NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
       for (unsigned i = 0; i != Size; ++i) {
         MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i]));
         if (MD == 0)
           return Error("Malformed metadata record");
-        Elts.push_back(MD);
+        NMD->addOperand(MD);
       }
-      Value *V = NamedMDNode::Create(Context, Name.str(), Elts.data(),
-                                     Elts.size(), TheModule);
-      MDValueList.AssignValue(V, NextMDValueNo++);
       break;
     }
     case bitc::METADATA_FN_NODE:

Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Wed Jul 21 18:38:33 2010
@@ -509,7 +509,8 @@
   Record.clear();
 }
 
-static void WriteModuleMetadata(const ValueEnumerator &VE,
+static void WriteModuleMetadata(const Module *M,
+                                const ValueEnumerator &VE,
                                 BitstreamWriter &Stream) {
   const ValueEnumerator::ValueList &Vals = VE.getMDValues();
   bool StartedMetadataBlock = false;
@@ -544,25 +545,30 @@
       // Emit the finished record.
       Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
       Record.clear();
-    } else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(Vals[i].first)) {
-      if (!StartedMetadataBlock)  {
-        Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
-        StartedMetadataBlock = true;
-      }
-
-      // Write name.
-      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();
+    }
+  }
 
-      // Write named metadata operands.
-      for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
-        Record.push_back(VE.getValueID(NMD->getOperand(i)));
-      Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
-      Record.clear();
+  // Write named metadata.
+  for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
+       E = M->named_metadata_end(); I != E; ++I) {
+    const NamedMDNode *NMD = I;
+    if (!StartedMetadataBlock)  {
+      Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
+      StartedMetadataBlock = true;
     }
+
+    // Write name.
+    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();
+
+    // Write named metadata operands.
+    for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
+      Record.push_back(VE.getValueID(NMD->getOperand(i)));
+    Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
+    Record.clear();
   }
 
   if (StartedMetadataBlock)
@@ -1530,7 +1536,7 @@
   WriteModuleConstants(VE, Stream);
 
   // Emit metadata.
-  WriteModuleMetadata(VE, Stream);
+  WriteModuleMetadata(M, VE, Stream);
 
   // Emit function bodies.
   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)

Modified: llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp Wed Jul 21 18:38:33 2010
@@ -75,7 +75,7 @@
   // 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());
+  EnumerateNamedMetadata(M);
 
   SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
 
@@ -207,31 +207,18 @@
     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());
+/// EnumerateNamedMetadata - Insert all of the values referenced by
+/// named metadata in the specified module.
+void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
+  for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
+       E = M->named_metadata_end(); I != E; ++I)
+    EnumerateNamedMDNode(I);
 }
 
 void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
-  // Check to see if it's already in!
-  unsigned &MDValueID = MDValueMap[MD];
-  if (MDValueID) {
-    // Increment use count.
-    MDValues[MDValueID-1].second++;
-    return;
-  }
-
-  // Enumerate the type of this value.
-  EnumerateType(MD->getType());
-
   for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
     if (MDNode *E = MD->getOperand(i))
       EnumerateValue(E);
-  MDValues.push_back(std::make_pair(MD, 1U));
-  MDValueMap[MD] = Values.size();
 }
 
 void ValueEnumerator::EnumerateMetadata(const Value *MD) {
@@ -272,8 +259,6 @@
   assert(!V->getType()->isVoidTy() && "Can't insert void values!");
   if (isa<MDNode>(V) || isa<MDString>(V))
     return EnumerateMetadata(V);
-  else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V))
-    return EnumerateNamedMDNode(NMD);
 
   // Check to see if it's already in!
   unsigned &ValueID = ValueMap[V];

Modified: llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.h?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.h (original)
+++ llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.h Wed Jul 21 18:38:33 2010
@@ -141,7 +141,7 @@
   
   void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
-  void EnumerateMDSymbolTable(const MDSymbolTable &ST);
+  void EnumerateNamedMetadata(const Module *M);
 };
 
 } // End llvm namespace

Modified: llvm/trunk/lib/Linker/LinkModules.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkModules.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/LinkModules.cpp (original)
+++ llvm/trunk/lib/Linker/LinkModules.cpp Wed Jul 21 18:38:33 2010
@@ -545,14 +545,10 @@
   for (Module::const_named_metadata_iterator I = Src->named_metadata_begin(),
          E = Src->named_metadata_end(); I != E; ++I) {
     const NamedMDNode *SrcNMD = I;
-    NamedMDNode *DestNMD = Dest->getNamedMetadata(SrcNMD->getName());
-    if (!DestNMD)
-      NamedMDNode::Create(SrcNMD, Dest);
-    else {
-      // Add Src elements into Dest node.
-      for (unsigned i = 0, e = SrcNMD->getNumOperands(); i != e; ++i) 
-        DestNMD->addOperand(SrcNMD->getOperand(i));
-    }
+    NamedMDNode *DestNMD = Dest->getOrInsertNamedMetadata(SrcNMD->getName());
+    // Add Src elements into Dest node.
+    for (unsigned i = 0, e = SrcNMD->getNumOperands(); i != e; ++i) 
+      DestNMD->addOperand(SrcNMD->getOperand(i));
   }
 }
 

Modified: llvm/trunk/lib/Transforms/Utils/CloneModule.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/CloneModule.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/CloneModule.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/CloneModule.cpp Wed Jul 21 18:38:33 2010
@@ -127,11 +127,9 @@
   for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
          E = M->named_metadata_end(); I != E; ++I) {
     const NamedMDNode &NMD = *I;
-    SmallVector<MDNode*, 4> MDs;
+    NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
     for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
-      MDs.push_back(cast<MDNode>(MapValue(NMD.getOperand(i), VMap)));
-    NamedMDNode::Create(New->getContext(), NMD.getName(),
-                        MDs.data(), MDs.size(), New);
+      NewNMD->addOperand(cast<MDNode>(MapValue(NMD.getOperand(i), VMap)));
   }
 
   // Update metadata attach with instructions.

Modified: llvm/trunk/lib/VMCore/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/AsmWriter.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/AsmWriter.cpp (original)
+++ llvm/trunk/lib/VMCore/AsmWriter.cpp Wed Jul 21 18:38:33 2010
@@ -63,8 +63,6 @@
   
   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
     return GV->getParent();
-  if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V))
-    return NMD->getParent();
   return 0;
 }
 
@@ -2111,6 +2109,13 @@
   W.printModule(this);
 }
 
+void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
+  SlotTracker SlotTable(getParent());
+  formatted_raw_ostream OS(ROS);
+  AssemblyWriter W(OS, SlotTable, getParent(), AAW);
+  W.printNamedMDNode(this);
+}
+
 void Type::print(raw_ostream &OS) const {
   if (this == 0) {
     OS << "<null Type>";
@@ -2148,10 +2153,6 @@
     SlotTracker SlotTable(F);
     AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
     W.printMDNodeBody(N);
-  } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(this)) {
-    SlotTracker SlotTable(N->getParent());
-    AssemblyWriter W(OS, SlotTable, N->getParent(), AAW);
-    W.printNamedMDNode(N);
   } else if (const Constant *C = dyn_cast<Constant>(this)) {
     TypePrinting TypePrinter;
     TypePrinter.print(C->getType(), OS);

Modified: llvm/trunk/lib/VMCore/Metadata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Metadata.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Metadata.cpp (original)
+++ llvm/trunk/lib/VMCore/Metadata.cpp Wed Jul 21 18:38:33 2010
@@ -330,50 +330,13 @@
 // NamedMDNode implementation.
 //
 
-// 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<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
   return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
 }
 
-NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
-                         MDNode *const *MDs,
-                         unsigned NumMDs, Module *ParentModule)
-  : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
-  setName(N);
-  Operands = new SmallVector<TrackingVH<MDNode>, 4>();
-
-  SmallVector<TrackingVH<MDNode>, 4> &Node = getNMDOps(Operands);
-  for (unsigned i = 0; i != NumMDs; ++i)
-    Node.push_back(TrackingVH<MDNode>(MDs[i]));
-
-  if (ParentModule)
-    ParentModule->getNamedMDList().push_back(this);
-}
-
-NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
-  assert(NMD && "Invalid source NamedMDNode!");
-  SmallVector<MDNode *, 4> Elems;
-  Elems.reserve(NMD->getNumOperands());
-
-  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
-    Elems.push_back(NMD->getOperand(i));
-  return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
-                         Elems.data(), Elems.size(), M);
+NamedMDNode::NamedMDNode(const Twine &N)
+  : Name(N.str()), Parent(0),
+    Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
 }
 
 NamedMDNode::~NamedMDNode() {
@@ -400,7 +363,7 @@
 /// eraseFromParent - Drop all references and remove the node from parent
 /// module.
 void NamedMDNode::eraseFromParent() {
-  getParent()->getNamedMDList().erase(this);
+  getParent()->eraseNamedMetadata(this);
 }
 
 /// dropAllReferences - Remove all uses and clear node vector.
@@ -408,22 +371,6 @@
   getNMDOps(Operands).clear();
 }
 
-/// setName - Set the name of this named metadata.
-void NamedMDNode::setName(const Twine &NewName) {
-  assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
-
-  SmallString<256> NameData;
-  StringRef NameRef = NewName.toStringRef(NameData);
-
-  // Name isn't changing?
-  if (getName() == NameRef)
-    return;
-
-  Name = NameRef.str();
-  if (Parent)
-    Parent->getMDSymbolTable().insert(NameRef, this);
-}
-
 /// getName - Return a constant reference to this named metadata's name.
 StringRef NamedMDNode::getName() const {
   return StringRef(Name);

Modified: llvm/trunk/lib/VMCore/Module.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Module.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Module.cpp (original)
+++ llvm/trunk/lib/VMCore/Module.cpp Wed Jul 21 18:38:33 2010
@@ -61,7 +61,7 @@
   : Context(C), Materializer(NULL), ModuleID(MID), DataLayout("")  {
   ValSymTab = new ValueSymbolTable();
   TypeSymTab = new TypeSymbolTable();
-  NamedMDSymTab = new MDSymbolTable();
+  NamedMDSymTab = new StringMap<NamedMDNode *>();
 }
 
 Module::~Module() {
@@ -73,7 +73,7 @@
   NamedMDList.clear();
   delete ValSymTab;
   delete TypeSymTab;
-  delete NamedMDSymTab;
+  delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
 }
 
 /// Target endian information...
@@ -316,19 +316,28 @@
 NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
   SmallString<256> NameData;
   StringRef NameRef = Name.toStringRef(NameData);
-  return NamedMDSymTab->lookup(NameRef);
+  return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
 }
 
 /// 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 = NamedMDSymTab->lookup(Name);
-  if (!NMD)
-    NMD = NamedMDNode::Create(getContext(), Name, NULL, 0, this);
+  NamedMDNode *&NMD =
+    (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
+  if (!NMD) {
+    NMD = new NamedMDNode(Name);
+    NMD->setParent(this);
+    NamedMDList.push_back(NMD);
+  }
   return NMD;
 }
 
+void Module::eraseNamedMetadata(NamedMDNode *NMD) {
+  static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
+  NamedMDList.erase(NMD);
+}
+
 //===----------------------------------------------------------------------===//
 // Methods for easy access to the types in the module.
 //

Modified: llvm/trunk/lib/VMCore/Value.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Value.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Value.cpp (original)
+++ llvm/trunk/lib/VMCore/Value.cpp Wed Jul 21 18:38:33 2010
@@ -139,10 +139,6 @@
   } else if (Argument *A = dyn_cast<Argument>(V)) {
     if (Function *P = A->getParent())
       ST = &P->getValueSymbolTable();
-  } else if (NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
-    if (Module *P = N->getParent()) {
-      ST = &P->getValueSymbolTable();
-    }
   } else if (isa<MDString>(V))
     return true;
   else {

Modified: llvm/trunk/lib/VMCore/ValueSymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/ValueSymbolTable.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/ValueSymbolTable.cpp (original)
+++ llvm/trunk/lib/VMCore/ValueSymbolTable.cpp Wed Jul 21 18:38:33 2010
@@ -115,5 +115,3 @@
     //DEBUG(dbgs() << "\n");
   }
 }
-
-MDSymbolTable::~MDSymbolTable() { }

Modified: llvm/trunk/lib/VMCore/Verifier.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Verifier.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Verifier.cpp (original)
+++ llvm/trunk/lib/VMCore/Verifier.cpp Wed Jul 21 18:38:33 2010
@@ -504,8 +504,8 @@
     if (!MD)
       continue;
 
-    Assert2(!MD->isFunctionLocal(),
-            "Named metadata operand cannot be function local!", &NMD, MD);
+    Assert1(!MD->isFunctionLocal(),
+            "Named metadata operand cannot be function local!", MD);
     visitMDNode(*MD, 0);
   }
 }

Modified: llvm/trunk/unittests/VMCore/MetadataTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/VMCore/MetadataTest.cpp?rev=109061&r1=109060&r2=109061&view=diff
==============================================================================
--- llvm/trunk/unittests/VMCore/MetadataTest.cpp (original)
+++ llvm/trunk/unittests/VMCore/MetadataTest.cpp Wed Jul 21 18:38:33 2010
@@ -130,11 +130,12 @@
   MDNode *n = MDNode::get(Context, &V, 1);
   MDNode *n2 = MDNode::get(Context, &V2, 1);
 
-  MDNode *Nodes[2] = { n, n2 };
-
   Module M("MyModule", Context);
   const char *Name = "llvm.NMD1";
-  NamedMDNode *NMD = NamedMDNode::Create(Context, Name, &Nodes[0], 2, &M);
+  NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
+  NMD->addOperand(n);
+  NMD->addOperand(n2);
+
   std::string Str;
   raw_string_ostream oss(Str);
   NMD->print(oss);





More information about the llvm-commits mailing list