[llvm-commits] [llvm] r82060 - in /llvm/trunk: include/llvm/LLVMContext.h include/llvm/Metadata.h include/llvm/Value.h lib/VMCore/LLVMContext.cpp lib/VMCore/LLVMContextImpl.h lib/VMCore/Metadata.cpp lib/VMCore/Value.cpp

Devang Patel dpatel at apple.com
Wed Sep 16 11:09:00 PDT 2009


Author: dpatel
Date: Wed Sep 16 13:09:00 2009
New Revision: 82060

URL: http://llvm.org/viewvc/llvm-project?rev=82060&view=rev
Log:
Add llvm::Metadata to manage metadata used in a context. 
This interface will be used to attach metadata with an instruction.


Modified:
    llvm/trunk/include/llvm/LLVMContext.h
    llvm/trunk/include/llvm/Metadata.h
    llvm/trunk/include/llvm/Value.h
    llvm/trunk/lib/VMCore/LLVMContext.cpp
    llvm/trunk/lib/VMCore/LLVMContextImpl.h
    llvm/trunk/lib/VMCore/Metadata.cpp
    llvm/trunk/lib/VMCore/Value.cpp

Modified: llvm/trunk/include/llvm/LLVMContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LLVMContext.h?rev=82060&r1=82059&r2=82060&view=diff

==============================================================================
--- llvm/trunk/include/llvm/LLVMContext.h (original)
+++ llvm/trunk/include/llvm/LLVMContext.h Wed Sep 16 13:09:00 2009
@@ -18,7 +18,7 @@
 namespace llvm {
 
 class LLVMContextImpl;
-
+class Metadata;
 /// This is an important class for using LLVM in a threaded context.  It
 /// (opaquely) owns and manages the core "global" data of LLVM's core 
 /// infrastructure, including the type and constant uniquing tables.
@@ -30,6 +30,7 @@
   void operator=(LLVMContext&);
 public:
   LLVMContextImpl* pImpl;
+  Metadata &getMetadata();
   bool RemoveDeadMetadata();
   LLVMContext();
   ~LLVMContext();

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

==============================================================================
--- llvm/trunk/include/llvm/Metadata.h (original)
+++ llvm/trunk/include/llvm/Metadata.h Wed Sep 16 13:09:00 2009
@@ -22,12 +22,16 @@
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/ilist_node.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/ValueHandle.h"
 
 namespace llvm {
 class Constant;
+class Instruction;
 class LLVMContext;
 
 //===----------------------------------------------------------------------===//
@@ -300,6 +304,48 @@
   }
 };
 
+//===----------------------------------------------------------------------===//
+/// Metadata -
+/// Metadata manages metadata used in a context.
+
+/// MDKindID - This id identifies metadata kind the metadata store. Valid
+/// ID values are 1 or higher. This ID is set by RegisterMDKind.
+typedef unsigned MDKindID;
+class Metadata {
+private:
+  typedef std::pair<MDKindID, WeakVH> MDPairTy;
+  typedef SmallVector<MDPairTy, 2> MDMapTy;
+  typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
+
+  /// MetadataStore - Collection of metadata used in this context.
+  MDStoreTy MetadataStore;
+
+  /// MDHandlerNames - Map to hold metadata handler names.
+  StringMap<unsigned> MDHandlerNames;
+
+public:
+
+  /// RegisterMDKind - Register a new metadata kind and return its ID.
+  /// A metadata kind can be registered only once. 
+  MDKindID RegisterMDKind(const char *Name);
+
+  /// getMDKind - Return metadata kind. If the requested metadata kind
+  /// is not registered then return 0.
+  MDKindID getMDKind(const char *Name);
+
+  /// getMD - Get the metadata of given kind attached with an Instruction.
+  /// If the metadata is not found then return 0.
+  MDNode *getMD(MDKindID Kind, const Instruction *Inst);
+
+  /// setMD - Attach the metadata of given kind with an Instruction.
+  void setMD(MDKindID Kind, MDNode *Node, Instruction *Inst);
+  
+  /// ValueIsDeleted - This handler is used to update metadata store
+  /// when a value is deleted.
+  void ValueIsDeleted(Value *V) {}
+  void ValueIsDeleted(const Instruction *Inst);
+};
+
 } // end llvm namespace
 
 #endif

Modified: llvm/trunk/include/llvm/Value.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Value.h?rev=82060&r1=82059&r2=82060&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Value.h (original)
+++ llvm/trunk/include/llvm/Value.h Wed Sep 16 13:09:00 2009
@@ -42,6 +42,7 @@
 class AssemblyAnnotationWriter;
 class ValueHandleBase;
 class LLVMContext;
+class Metadata;
 
 //===----------------------------------------------------------------------===//
 //                                 Value Class
@@ -63,6 +64,7 @@
 class Value {
   const unsigned char SubclassID;   // Subclass identifier (for isa/dyn_cast)
   unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
+  unsigned char HasMetadata : 1;    // Has a metadata attached to this ?
 protected:
   /// SubclassOptionalData - This member is similar to SubclassData, however it
   /// is for holding information which may be used to aid optimization, but
@@ -81,6 +83,7 @@
   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
   friend class SymbolTable;      // Allow SymbolTable to directly poke Name.
   friend class ValueHandleBase;
+  friend class Metadata;
   friend class AbstractTypeUser;
   ValueName *Name;
 

Modified: llvm/trunk/lib/VMCore/LLVMContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/LLVMContext.cpp?rev=82060&r1=82059&r2=82060&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/LLVMContext.cpp (original)
+++ llvm/trunk/lib/VMCore/LLVMContext.cpp Wed Sep 16 13:09:00 2009
@@ -70,3 +70,7 @@
   }
   return Changed;
 }
+
+Metadata &LLVMContext::getMetadata() {
+  return pImpl->TheMetadata;
+}

Modified: llvm/trunk/lib/VMCore/LLVMContextImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/LLVMContextImpl.h?rev=82060&r1=82059&r2=82060&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/LLVMContextImpl.h (original)
+++ llvm/trunk/lib/VMCore/LLVMContextImpl.h Wed Sep 16 13:09:00 2009
@@ -179,6 +179,7 @@
   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
   ValueHandlesTy ValueHandles;
   
+  Metadata TheMetadata;
   LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
     VoidTy(C, Type::VoidTyID),
     LabelTy(C, Type::LabelTyID),

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

==============================================================================
--- llvm/trunk/lib/VMCore/Metadata.cpp (original)
+++ llvm/trunk/lib/VMCore/Metadata.cpp Wed Sep 16 13:09:00 2009
@@ -15,6 +15,7 @@
 #include "llvm/Metadata.h"
 #include "llvm/LLVMContext.h"
 #include "llvm/Module.h"
+#include "llvm/Instruction.h"
 #include "SymbolTableListTraitsImpl.h"
 using namespace llvm;
 
@@ -251,3 +252,74 @@
 NamedMDNode::~NamedMDNode() {
   dropAllReferences();
 }
+
+//===----------------------------------------------------------------------===//
+//Metadata implementation
+//
+
+/// RegisterMDKind - Register a new metadata kind and return its ID.
+/// A metadata kind can be registered only once. 
+MDKindID Metadata::RegisterMDKind(const char *Name) {
+  MDKindID Count = MDHandlerNames.size();
+  StringMap<unsigned>::iterator I = MDHandlerNames.find(Name);
+  assert(I == MDHandlerNames.end() && "Already registered MDKind!");
+  MDHandlerNames[Name] = Count + 1;
+  return Count + 1;
+}
+
+/// getMDKind - Return metadata kind. If the requested metadata kind
+/// is not registered then return 0.
+MDKindID Metadata::getMDKind(const char *Name) {
+  StringMap<unsigned>::iterator I = MDHandlerNames.find(Name);
+  if (I == MDHandlerNames.end())
+    return 0;
+
+  return I->getValue();
+}
+
+/// setMD - Attach the metadata of given kind with an Instruction.
+void Metadata::setMD(MDKindID MDKind, MDNode *Node, Instruction *Inst) {
+  MDStoreTy::iterator I = MetadataStore.find(Inst);
+  Inst->HasMetadata = true;
+  if (I == MetadataStore.end()) {
+    MDMapTy Info;
+    Info.push_back(std::make_pair(MDKind, Node));
+    MetadataStore.insert(std::make_pair(Inst, Info));
+    return;
+  }
+  
+  MDMapTy &Info = I->second;
+  Info.push_back(std::make_pair(MDKind, Node));
+  return;
+}
+
+/// getMD - Get the metadata of given kind attached with an Instruction.
+/// If the metadata is not found then return 0.
+MDNode *Metadata::getMD(MDKindID MDKind, const Instruction *Inst) {
+  MDNode *Node = NULL;
+  MDStoreTy::iterator I = MetadataStore.find(Inst);
+  if (I == MetadataStore.end())
+    return Node;
+  
+  MDMapTy &Info = I->second;
+  for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
+    if (I->first == MDKind)
+      Node = dyn_cast_or_null<MDNode>(I->second);
+  return Node;
+}
+
+/// ValueIsDeleted - This handler is used to update metadata store
+/// when a value is deleted.
+void Metadata::ValueIsDeleted(const Instruction *Inst) {
+  // Find Metadata handles for this instruction.
+  MDStoreTy::iterator I = MetadataStore.find(Inst);
+  if (I == MetadataStore.end())
+    return;
+  MDMapTy &Info = I->second;
+  
+  // FIXME : Give all metadata handlers a chance to adjust.
+  
+  // Remove the entries for this instruction.
+  Info.clear();
+  MetadataStore.erase(Inst);
+}

Modified: llvm/trunk/lib/VMCore/Value.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Value.cpp?rev=82060&r1=82059&r2=82060&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Value.cpp (original)
+++ llvm/trunk/lib/VMCore/Value.cpp Wed Sep 16 13:09:00 2009
@@ -60,6 +60,11 @@
 }
 
 Value::~Value() {
+  if (HasMetadata) {
+    LLVMContext &Context = getContext();
+    Context.pImpl->TheMetadata.ValueIsDeleted(this);
+  }
+    
   // Notify all ValueHandles (if present) that this value is going away.
   if (HasValueHandle)
     ValueHandleBase::ValueIsDeleted(this);





More information about the llvm-commits mailing list