Derive MDNode from MetadataBase instead of Constant. Emit MDNodes into METADATA_BLOCK...
[oota-llvm.git] / include / llvm / MDNode.h
index e3944365ee465e8e568c756d120f4042c808a72c..a06d720bf11f787d4bd9fd32a6c68155fb97723a 100644 (file)
 namespace llvm {
 
 //===----------------------------------------------------------------------===//
-/// MDNode - a tuple of other values.
-/// These contain a list of the Constants that represent the metadata. The
-/// operand list is always empty, query the element list instead.
-///
-/// This class will attempt to keep track of values as they are modified. When
-/// a value is replaced the element will be replaced with it, and when the
-/// value is deleted the element is set to a null pointer. In order to preserve
-/// structural equivalence while the elements mutate, the MDNode may call
-/// replaceAllUsesWith on itself. Because of this, users of MDNode must use a
-/// WeakVH or CallbackVH to hold the node pointer if there is a chance that one
-/// of the elements held by the node may change.
+// MetadataBase  - A base class for MDNode and MDString.
+class MetadataBase : public Value {
+public:
+  MetadataBase(const Type *Ty, unsigned scid)
+    : Value(Ty, scid) {}
+
+  /// 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;
+  }
+
+  /// 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;
+  }
+};
+
+//===----------------------------------------------------------------------===//
+/// MDString - a single uniqued string.
+/// These are used to efficiently contain a byte sequence for metadata.
 ///
-class MDNode : public Constant, public FoldingSetNode {
-  MDNode(const MDNode &);      // DO NOT IMPLEMENT
+class MDString : public MetadataBase {
+  MDString(const MDString &);            // DO NOT IMPLEMENT
 
+  const char *StrBegin, *StrEnd;
   friend class LLVMContextImpl;
 
-  friend class ElementVH;
-  struct ElementVH : public CallbackVH {
-    MDNode *OwningNode;
+public:
+  MDString(const char *begin, const char *end)
+    : MetadataBase(Type::MetadataTy, Value::MDStringVal),
+      StrBegin(begin), StrEnd(end) {}
 
-    ElementVH(Value *V, MDNode *Parent)
-      : CallbackVH(V), OwningNode(Parent) {}
+  intptr_t size() const { return StrEnd - StrBegin; }
 
-    ~ElementVH() {}
+  /// begin() - Pointer to the first byte of the string.
+  ///
+  const char *begin() const { return StrBegin; }
 
-    /// deleted - Set this entry in the MDNode to 'null'. This will reallocate
-    /// the MDNode.
-    virtual void deleted() {
-      OwningNode->replaceElement(this->operator Value*(), 0);
-    }
+  /// end() - Pointer to one byte past the end of the string.
+  ///
+  const char *end() const { return StrEnd; }
 
-    /// allUsesReplacedWith - Modify the MDNode by replacing this entry with
-    /// new_value. This will reallocate the MDNode.
-    virtual void allUsesReplacedWith(Value *new_value) {
-      OwningNode->replaceElement(this->operator Value*(), new_value);
-    }
-  };
+  /// 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;
+  }
+};
+
+//===----------------------------------------------------------------------===//
+/// MDNode - a tuple of other values.
+/// These contain a list of the values that represent the metadata. 
+///
+class MDNode : public MetadataBase, public FoldingSetNode {
+  MDNode(const MDNode &);      // DO NOT IMPLEMENT
+
+  friend class LLVMContextImpl;
 
   void replaceElement(Value *From, Value *To);
 
-  SmallVector<ElementVH, 4> Node;
-  typedef SmallVectorImpl<ElementVH>::iterator elem_iterator;
+  SmallVector<WeakVH, 4> Node;
+  typedef SmallVectorImpl<WeakVH>::iterator elem_iterator;
 
 protected:
   explicit MDNode(Value*const* Vals, unsigned NumVals);
 public:
-  typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator;
+  typedef SmallVectorImpl<WeakVH>::const_iterator const_elem_iterator;
 
   Value *getElement(unsigned i) const {
     return Node[i];
@@ -117,7 +145,6 @@ public:
   /// duplicates
   void Profile(FoldingSetNodeID &ID) const;
 
-  virtual void destroyConstant();
   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
     llvm_unreachable("This should never be called because MDNodes have no ops");
   }