[llvm-commits] [llvm] r92931 - in /llvm/trunk: include/llvm/Metadata.h include/llvm/Module.h include/llvm/ValueSymbolTable.h lib/Bitcode/Writer/BitcodeWriter.cpp lib/Bitcode/Writer/ValueEnumerator.cpp lib/Bitcode/Writer/ValueEnumerator.h lib/VMCore/Metadata.cpp lib/VMCore/Module.cpp test/Feature/NamedMDNode.ll

Devang Patel dpatel at apple.com
Thu Jan 7 11:39:37 PST 2010


Author: dpatel
Date: Thu Jan  7 13:39:36 2010
New Revision: 92931

URL: http://llvm.org/viewvc/llvm-project?rev=92931&view=rev
Log:
Use separate namespace for named metadata.

Modified:
    llvm/trunk/include/llvm/Metadata.h
    llvm/trunk/include/llvm/Module.h
    llvm/trunk/include/llvm/ValueSymbolTable.h
    llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
    llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
    llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.h
    llvm/trunk/lib/VMCore/Metadata.cpp
    llvm/trunk/lib/VMCore/Module.cpp
    llvm/trunk/test/Feature/NamedMDNode.ll

Modified: llvm/trunk/include/llvm/Metadata.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Metadata.h?rev=92931&r1=92930&r2=92931&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Metadata.h (original)
+++ llvm/trunk/include/llvm/Metadata.h Thu Jan  7 13:39:36 2010
@@ -175,15 +175,16 @@
 
   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
 
+  std::string Name;
   Module *Parent;
   void *Operands; // SmallVector<WeakVH<MDNode>, 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 @@
 
   /// 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) {

Modified: llvm/trunk/include/llvm/Module.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Module.h?rev=92931&r1=92930&r2=92931&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Module.h (original)
+++ llvm/trunk/include/llvm/Module.h Thu Jan  7 13:39:36 2010
@@ -26,6 +26,7 @@
 
 class FunctionType;
 class LLVMContext;
+class MDSymbolTable;
 
 template<> struct ilist_traits<Function>
   : public SymbolTableListTraits<Function, Module> {
@@ -131,19 +132,20 @@
 /// @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 @@
   /// 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 @@
   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/ValueSymbolTable.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ValueSymbolTable.h?rev=92931&r1=92930&r2=92931&view=diff

==============================================================================
--- llvm/trunk/include/llvm/ValueSymbolTable.h (original)
+++ llvm/trunk/include/llvm/ValueSymbolTable.h Thu Jan  7 13:39:36 2010
@@ -26,7 +26,7 @@
   class NamedMDNode;
   class Module;
   class StringRef;
-  
+
 /// This class provides a symbol table of name/value pairs. It is essentially
 /// a std::map<std::string,Value*> but has a controlled interface provided by
 /// LLVM as well as ensuring uniqueness of names.
@@ -129,6 +129,84 @@
 /// @}
 };
 
+/// 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<NamedMDNode*> 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

Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=92931&r1=92930&r2=92931&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Thu Jan  7 13:39:36 2010
@@ -528,10 +528,9 @@
       }
 
       // 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();
 

Modified: llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp?rev=92931&r1=92930&r2=92931&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp Thu Jan  7 13:39:36 2010
@@ -74,9 +74,10 @@
   // 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<std::pair<unsigned, MDNode*>, 8> MDs;
 
@@ -196,6 +197,14 @@
     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];

Modified: llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.h?rev=92931&r1=92930&r2=92931&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.h (original)
+++ llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.h Thu Jan  7 13:39:36 2010
@@ -30,6 +30,7 @@
 class AttrListPtr;
 class TypeSymbolTable;
 class ValueSymbolTable;
+class MDSymbolTable;
 
 class ValueEnumerator {
 public:
@@ -133,6 +134,7 @@
   
   void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
+  void EnumerateMDSymbolTable(const MDSymbolTable &ST);
 };
 
 } // End llvm namespace

Modified: llvm/trunk/lib/VMCore/Metadata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Metadata.cpp?rev=92931&r1=92930&r2=92931&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Metadata.cpp (original)
+++ llvm/trunk/lib/VMCore/Metadata.cpp Thu Jan  7 13:39:36 2010
@@ -214,20 +214,21 @@
   return *(SmallVector<WeakVH, 4>*)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<WeakVH, 4>();
     
   SmallVector<WeakVH, 4> &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 @@
 /// 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 @@
   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.

Modified: llvm/trunk/lib/VMCore/Module.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Module.cpp?rev=92931&r1=92930&r2=92931&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Module.cpp (original)
+++ llvm/trunk/lib/VMCore/Module.cpp Thu Jan  7 13:39:36 2010
@@ -59,6 +59,7 @@
   : Context(C), ModuleID(MID), DataLayout("")  {
   ValSymTab = new ValueSymbolTable();
   TypeSymTab = new TypeSymbolTable();
+  NamedMDSymTab = new MDSymbolTable();
 }
 
 Module::~Module() {
@@ -307,20 +308,25 @@
 /// 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<NamedMDNode>(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<NamedMDNode>(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.
 //

Modified: llvm/trunk/test/Feature/NamedMDNode.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Feature/NamedMDNode.ll?rev=92931&r1=92930&r2=92931&view=diff

==============================================================================
--- llvm/trunk/test/Feature/NamedMDNode.ll (original)
+++ llvm/trunk/test/Feature/NamedMDNode.ll Thu Jan  7 13:39:36 2010
@@ -4,3 +4,6 @@
 !0 = metadata !{i32 42}
 !1 = metadata !{metadata !"foo"}
 !llvm.stuff = !{!0, !1, null}
+
+!samename = !{!0, !1}
+define void @samename() {}





More information about the llvm-commits mailing list