50b3104c8fa96e8f09a677fc2b6c10e70a8fa712
[oota-llvm.git] / include / llvm / MDNode.h
1 //===-- llvm/Metadata.h - Constant class subclass definitions ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file
11 /// This file contains the declarations for the subclasses of Constant, 
12 /// which represent the different flavors of constant values that live in LLVM.
13 /// Note that Constants are immutable (once created they never change) and are 
14 /// fully shared by structural equivalence.  This means that two structurally
15 /// equivalent constants will always have the same address.  Constant's are
16 /// created on demand as needed and never deleted: thus clients don't have to
17 /// worry about the lifetime of the objects.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_MDNODE_H
22 #define LLVM_MDNODE_H
23
24 #include "llvm/Constant.h"
25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Support/ValueHandle.h"
28
29 namespace llvm {
30
31 //===----------------------------------------------------------------------===//
32 /// MDNode - a tuple of other values.
33 /// These contain a list of the Constants that represent the metadata. The
34 /// operand list is always empty, query the element list instead.
35 ///
36 /// This class will attempt to keep track of values as they are modified. When
37 /// a value is replaced the element will be replaced with it, and when the
38 /// value is deleted the element is set to a null pointer. In order to preserve
39 /// structural equivalence while the elements mutate, the MDNode may call
40 /// replaceAllUsesWith on itself. Because of this, users of MDNode must use a
41 /// WeakVH or CallbackVH to hold the node pointer if there is a chance that one
42 /// of the elements held by the node may change.
43 ///
44 class MDNode : public Constant, public FoldingSetNode {
45   MDNode(const MDNode &);      // DO NOT IMPLEMENT
46
47   friend class ElementVH;
48   struct ElementVH : public CallbackVH {
49     MDNode *OwningNode;
50
51     ElementVH(Value *V, MDNode *Parent)
52       : CallbackVH(V), OwningNode(Parent) {}
53
54     ~ElementVH() {}
55
56     /// deleted - Set this entry in the MDNode to 'null'. This will reallocate
57     /// the MDNode.
58     virtual void deleted() {
59       OwningNode->replaceElement(this->operator Value*(), 0);
60     }
61
62     /// allUsesReplacedWith - Modify the MDNode by replacing this entry with
63     /// new_value. This will reallocate the MDNode.
64     virtual void allUsesReplacedWith(Value *new_value) {
65       OwningNode->replaceElement(this->operator Value*(), new_value);
66     }
67   };
68
69   void replaceElement(Value *From, Value *To);
70
71   SmallVector<ElementVH, 4> Node;
72   typedef SmallVectorImpl<ElementVH>::iterator elem_iterator;
73 protected:
74   explicit MDNode(Value*const* Vals, unsigned NumVals);
75 public:
76   typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator;
77
78   /// get() - Static factory methods - Return objects of the specified value.
79   ///
80   static MDNode *get(Value*const* Vals, unsigned NumVals);
81
82   Value *getElement(unsigned i) const {
83     return Node[i];
84   }
85
86   unsigned getNumElements() const {
87     return Node.size();
88   }
89
90   const_elem_iterator elem_begin() const {
91     return Node.begin();
92   }
93
94   const_elem_iterator elem_end() const {
95     return Node.end();
96   }
97
98   /// getType() specialization - Type is always an empty struct.
99   ///
100   inline const Type *getType() const {
101     return Type::EmptyStructTy;
102   }
103
104   /// isNullValue - Return true if this is the value that would be returned by
105   /// getNullValue.  This always returns false because getNullValue will never
106   /// produce metadata.
107   virtual bool isNullValue() const {
108     return false;
109   }
110
111   /// Profile - calculate a unique identifier for this MDNode to collapse
112   /// duplicates
113   void Profile(FoldingSetNodeID &ID) const;
114
115   virtual void destroyConstant();
116   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
117     assert(0 && "This should never be called because MDNodes have no ops");
118     abort();
119   }
120
121   /// Methods for support type inquiry through isa, cast, and dyn_cast:
122   static inline bool classof(const MDNode *) { return true; }
123   static bool classof(const Value *V) {
124     return V->getValueID() == MDNodeVal;
125   }
126 };
127
128 } // end llvm namespace
129
130 #endif