d632e4ea4a6fb9037418c9961879e1dfcba7d64d
[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/Type.h"
26 #include "llvm/ADT/FoldingSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/Support/ValueHandle.h"
29
30 namespace llvm {
31
32 //===----------------------------------------------------------------------===//
33 /// MDNode - a tuple of other values.
34 /// These contain a list of the Constants that represent the metadata. The
35 /// operand list is always empty, query the element list instead.
36 ///
37 /// This class will attempt to keep track of values as they are modified. When
38 /// a value is replaced the element will be replaced with it, and when the
39 /// value is deleted the element is set to a null pointer. In order to preserve
40 /// structural equivalence while the elements mutate, the MDNode may call
41 /// replaceAllUsesWith on itself. Because of this, users of MDNode must use a
42 /// WeakVH or CallbackVH to hold the node pointer if there is a chance that one
43 /// of the elements held by the node may change.
44 ///
45 class MDNode : public Constant, public FoldingSetNode {
46   MDNode(const MDNode &);      // DO NOT IMPLEMENT
47
48   friend class ElementVH;
49   struct ElementVH : public CallbackVH {
50     MDNode *OwningNode;
51
52     ElementVH(Value *V, MDNode *Parent)
53       : CallbackVH(V), OwningNode(Parent) {}
54
55     ~ElementVH() {}
56
57     /// deleted - Set this entry in the MDNode to 'null'. This will reallocate
58     /// the MDNode.
59     virtual void deleted() {
60       OwningNode->replaceElement(this->operator Value*(), 0);
61     }
62
63     /// allUsesReplacedWith - Modify the MDNode by replacing this entry with
64     /// new_value. This will reallocate the MDNode.
65     virtual void allUsesReplacedWith(Value *new_value) {
66       OwningNode->replaceElement(this->operator Value*(), new_value);
67     }
68   };
69
70   void replaceElement(Value *From, Value *To);
71
72   SmallVector<ElementVH, 4> Node;
73   typedef SmallVectorImpl<ElementVH>::iterator elem_iterator;
74 protected:
75   explicit MDNode(Value*const* Vals, unsigned NumVals);
76 public:
77   typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator;
78
79   /// get() - Static factory methods - Return objects of the specified value.
80   ///
81   static MDNode *get(Value*const* Vals, unsigned NumVals);
82
83   Value *getElement(unsigned i) const {
84     return Node[i];
85   }
86
87   unsigned getNumElements() const {
88     return Node.size();
89   }
90
91   bool elem_empty() const {
92     return Node.empty();
93   }
94
95   const_elem_iterator elem_begin() const {
96     return Node.begin();
97   }
98
99   const_elem_iterator elem_end() const {
100     return Node.end();
101   }
102
103   /// getType() specialization - Type is always MetadataTy.
104   ///
105   inline const Type *getType() const {
106     return Type::MetadataTy;
107   }
108
109   /// isNullValue - Return true if this is the value that would be returned by
110   /// getNullValue.  This always returns false because getNullValue will never
111   /// produce metadata.
112   virtual bool isNullValue() const {
113     return false;
114   }
115
116   /// Profile - calculate a unique identifier for this MDNode to collapse
117   /// duplicates
118   void Profile(FoldingSetNodeID &ID) const;
119
120   virtual void destroyConstant();
121   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
122     assert(0 && "This should never be called because MDNodes have no ops");
123     abort();
124   }
125
126   /// Methods for support type inquiry through isa, cast, and dyn_cast:
127   static inline bool classof(const MDNode *) { return true; }
128   static bool classof(const Value *V) {
129     return V->getValueID() == MDNodeVal;
130   }
131 };
132
133 } // end llvm namespace
134
135 #endif