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

Devang Patel dpatel at apple.com
Tue Jul 28 17:33:07 PDT 2009


Author: dpatel
Date: Tue Jul 28 19:33:07 2009
New Revision: 77409

URL: http://llvm.org/viewvc/llvm-project?rev=77409&view=rev
Log:
Add NamedMDNode.

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

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

==============================================================================
--- llvm/trunk/include/llvm/Metadata.h (original)
+++ llvm/trunk/include/llvm/Metadata.h Tue Jul 28 19:33:07 2009
@@ -26,7 +26,7 @@
 namespace llvm {
 
 //===----------------------------------------------------------------------===//
-// MetadataBase  - A base class for MDNode and MDString.
+// MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
 class MetadataBase : public Value {
 protected:
   MetadataBase(const Type *Ty, unsigned scid)
@@ -49,14 +49,15 @@
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const MDString *) { return true; }
   static bool classof(const Value *V) {
-    return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal;
+    return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
+      || V->getValueID() == NamedMDNodeVal;
   }
 };
 
 //===----------------------------------------------------------------------===//
 /// MDString - a single uniqued string.
 /// These are used to efficiently contain a byte sequence for metadata.
-///
+/// MDString is always unnamd.
 class MDString : public MetadataBase {
   MDString(const MDString &);            // DO NOT IMPLEMENT
   StringRef Str;
@@ -89,7 +90,7 @@
 //===----------------------------------------------------------------------===//
 /// MDNode - a tuple of other values.
 /// These contain a list of the values that represent the metadata. 
-///
+/// MDNode is always unnamed.
 class MDNode : public MetadataBase, public FoldingSetNode {
   MDNode(const MDNode &);      // DO NOT IMPLEMENT
 
@@ -151,6 +152,104 @@
   }
 };
 
+//===----------------------------------------------------------------------===//
+/// WeakMetadataVH - a weak value handle for metadata.
+class WeakMetadataVH : public WeakVH {
+public:
+  WeakMetadataVH() : WeakVH() {}
+  WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
+  WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
+  
+  operator Value*() const {
+    llvm_unreachable("WeakMetadataVH only handles Metadata");
+  }
+
+  operator MetadataBase*() const {
+   return cast<MetadataBase>(getValPtr());
+  }
+};
+
+//===----------------------------------------------------------------------===//
+/// NamedMDNode - a tuple of other metadata. 
+/// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
+class NamedMDNode : public MetadataBase {
+  NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
+
+  friend class LLVMContextImpl;
+
+  Module *Parent;
+  StringRef Name;
+  SmallVector<WeakMetadataVH, 4> Node;
+  typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
+
+protected:
+  explicit NamedMDNode(const char *N, unsigned NameLength,
+                       MetadataBase*const* Vals, unsigned NumVals,
+                       Module *M = 0);
+public:
+  static NamedMDNode *Create(const char *N, unsigned NamedLength,
+                             MetadataBase*const*MDs, unsigned NumMDs,
+                             Module *M = 0) {
+    return new NamedMDNode(N,  NamedLength, MDs, NumMDs, M);
+  }
+
+  typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
+
+  StringRef getName() const { return Name; }
+
+  /// getParent - Get the module that holds this named metadata collection.
+  inline Module *getParent() { return Parent; }
+  inline const Module *getParent() const { return Parent; }
+
+  Value *getElement(unsigned i) const {
+    return Node[i];
+  }
+
+  unsigned getNumElements() const {
+    return Node.size();
+  }
+
+  bool elem_empty() const {
+    return Node.empty();
+  }
+
+  const_elem_iterator elem_begin() const {
+    return Node.begin();
+  }
+
+  const_elem_iterator elem_end() const {
+    return Node.end();
+  }
+
+  /// getType() specialization - Type is always MetadataTy.
+  ///
+  inline const Type *getType() const {
+    return Type::MetadataTy;
+  }
+
+  /// isNullValue - Return true if this is the value that would be returned by
+  /// getNullValue.  This always returns false because getNullValue will never
+  /// produce metadata.
+  virtual bool isNullValue() const {
+    return false;
+  }
+
+  /// Profile - calculate a unique identifier for this MDNode to collapse
+  /// duplicates
+  void Profile(FoldingSetNodeID &ID) const;
+
+  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
+    llvm_unreachable(
+                "This should never be called because NamedMDNodes have no ops");
+  }
+
+  /// 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;
+  }
+};
+
 } // 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=77409&r1=77408&r2=77409&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Value.h (original)
+++ llvm/trunk/include/llvm/Value.h Tue Jul 28 19:33:07 2009
@@ -219,6 +219,7 @@
     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
     InstructionVal,           // This is an instance of Instruction

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

==============================================================================
--- llvm/trunk/lib/VMCore/Metadata.cpp (original)
+++ llvm/trunk/lib/VMCore/Metadata.cpp Tue Jul 28 19:33:07 2009
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Metadata.h"
+#include "llvm/Module.h"
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -27,3 +28,17 @@
   for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
     ID.AddPointer(*I);
 }
+
+//===----------------------------------------------------------------------===//
+//NamedMDNode implementation
+//
+NamedMDNode::NamedMDNode(const char *N, unsigned NameLength,
+                         MetadataBase*const* MDs, unsigned NumMDs,
+                         Module *M)
+  : MetadataBase(Type::MetadataTy, Value::NamedMDNodeVal),
+    Parent(M), Name(N, NameLength) {
+  for (unsigned i = 0; i != NumMDs; ++i)
+    Node.push_back(WeakMetadataVH(MDs[i]));
+
+  // FIXME : Add into the parent module.
+}





More information about the llvm-commits mailing list