Using TrackingVH instead of WeakVH or WeakMetadataVH.
[oota-llvm.git] / include / llvm / Metadata.h
1 //===-- llvm/Metadata.h - Metadata 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 metadata subclasses.
12 /// They represent the different flavors of metadata that live in LLVM.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_METADATA_H
17 #define LLVM_METADATA_H
18
19 #include "llvm/Value.h"
20 #include "llvm/Type.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/ADT/ilist_node.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/ValueHandle.h"
28
29 namespace llvm {
30 class Constant;
31 class Instruction;
32 class LLVMContext;
33
34 //===----------------------------------------------------------------------===//
35 // MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
36 class MetadataBase : public Value {
37 protected:
38   MetadataBase(const Type *Ty, unsigned scid)
39     : Value(Ty, scid) {}
40
41 public:
42
43   /// Methods for support type inquiry through isa, cast, and dyn_cast:
44   static inline bool classof(const MetadataBase *) { return true; }
45   static bool classof(const Value *V) {
46     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
47       || V->getValueID() == NamedMDNodeVal;
48   }
49 };
50
51 //===----------------------------------------------------------------------===//
52 /// MDString - a single uniqued string.
53 /// These are used to efficiently contain a byte sequence for metadata.
54 /// MDString is always unnamd.
55 class MDString : public MetadataBase {
56   MDString(const MDString &);            // DO NOT IMPLEMENT
57
58   StringRef Str;
59 protected:
60   explicit MDString(LLVMContext &C, StringRef S)
61     : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
62
63 public:
64   static MDString *get(LLVMContext &Context, StringRef Str);
65   
66   StringRef getString() const { return Str; }
67
68   unsigned getLength() const { return Str.size(); }
69
70   typedef StringRef::iterator iterator;
71   
72   /// begin() - Pointer to the first byte of the string.
73   ///
74   iterator begin() const { return Str.begin(); }
75
76   /// end() - Pointer to one byte past the end of the string.
77   ///
78   iterator end() const { return Str.end(); }
79
80   /// Methods for support type inquiry through isa, cast, and dyn_cast:
81   static inline bool classof(const MDString *) { return true; }
82   static bool classof(const Value *V) {
83     return V->getValueID() == MDStringVal;
84   }
85 };
86
87 //===----------------------------------------------------------------------===//
88 /// MDNode - a tuple of other values.
89 /// These contain a list of the values that represent the metadata. 
90 /// MDNode is always unnamed.
91 class MDNode : public MetadataBase, public FoldingSetNode {
92   MDNode(const MDNode &);                // DO NOT IMPLEMENT
93
94   friend class ElementVH;
95   // Use CallbackVH to hold MDNOde elements.
96   struct ElementVH : public CallbackVH {
97     MDNode *Parent;
98     ElementVH() {}
99     ElementVH(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
100     ~ElementVH() {}
101
102     virtual void deleted() {
103       Parent->replaceElement(this->operator Value*(), 0);
104     }
105
106     virtual void allUsesReplacedWith(Value *NV) {
107       Parent->replaceElement(this->operator Value*(), NV);
108     }
109   };
110   // Replace each instance of F from the element list of this node with T.
111   void replaceElement(Value *F, Value *T);
112
113   ElementVH *Node;
114   unsigned NodeSize;
115
116 protected:
117   explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals);
118 public:
119   // Constructors and destructors.
120   static MDNode *get(LLVMContext &Context, 
121                      Value *const *Vals, unsigned NumVals);
122
123   /// ~MDNode - Destroy MDNode.
124   ~MDNode();
125   
126   /// getElement - Return specified element.
127   Value *getElement(unsigned i) const {
128     assert(i < getNumElements() && "Invalid element number!");
129     return Node[i];
130   }
131
132   /// getNumElements - Return number of MDNode elements.
133   unsigned getNumElements() const { return NodeSize; }
134
135   /// Profile - calculate a unique identifier for this MDNode to collapse
136   /// duplicates
137   void Profile(FoldingSetNodeID &ID) const;
138
139   /// Methods for support type inquiry through isa, cast, and dyn_cast:
140   static inline bool classof(const MDNode *) { return true; }
141   static bool classof(const Value *V) {
142     return V->getValueID() == MDNodeVal;
143   }
144 };
145
146 //===----------------------------------------------------------------------===//
147 /// NamedMDNode - a tuple of other metadata. 
148 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
149 template<typename ValueSubClass, typename ItemParentClass>
150   class SymbolTableListTraits;
151
152 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
153   friend class SymbolTableListTraits<NamedMDNode, Module>;
154   friend class LLVMContextImpl;
155
156   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
157
158   Module *Parent;
159   SmallVector<TrackingVH<MetadataBase>, 4> Node;
160
161   void setParent(Module *M) { Parent = M; }
162 protected:
163   explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals, 
164                        unsigned NumVals, Module *M = 0);
165 public:
166   static NamedMDNode *Create(LLVMContext &C, const Twine &N, 
167                              MetadataBase *const *MDs, 
168                              unsigned NumMDs, Module *M = 0) {
169     return new NamedMDNode(C, N, MDs, NumMDs, M);
170   }
171
172   static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
173
174   /// eraseFromParent - Drop all references and remove the node from parent
175   /// module.
176   void eraseFromParent();
177
178   /// dropAllReferences - Remove all uses and clear node vector.
179   void dropAllReferences();
180
181   /// ~NamedMDNode - Destroy NamedMDNode.
182   ~NamedMDNode();
183
184   /// getParent - Get the module that holds this named metadata collection.
185   inline Module *getParent() { return Parent; }
186   inline const Module *getParent() const { return Parent; }
187
188   /// getElement - Return specified element.
189   MetadataBase *getElement(unsigned i) const {
190     assert(i < getNumElements() && "Invalid element number!");
191     return Node[i];
192   }
193
194   /// getNumElements - Return number of NamedMDNode elements.
195   unsigned getNumElements() const {
196     return Node.size();
197   }
198
199   /// addElement - Add metadata element.
200   void addElement(MetadataBase *M) {
201     Node.push_back(TrackingVH<MetadataBase>(M));
202   }
203
204   typedef SmallVectorImpl<TrackingVH<MetadataBase> >::iterator elem_iterator;
205   typedef SmallVectorImpl<TrackingVH<MetadataBase> >::const_iterator 
206     const_elem_iterator;
207   bool elem_empty() const                { return Node.empty(); }
208   const_elem_iterator elem_begin() const { return Node.begin(); }
209   const_elem_iterator elem_end() const   { return Node.end();   }
210   elem_iterator elem_begin()             { return Node.begin(); }
211   elem_iterator elem_end()               { return Node.end();   }
212
213   /// Methods for support type inquiry through isa, cast, and dyn_cast:
214   static inline bool classof(const NamedMDNode *) { return true; }
215   static bool classof(const Value *V) {
216     return V->getValueID() == NamedMDNodeVal;
217   }
218 };
219
220 //===----------------------------------------------------------------------===//
221 /// MetadataContext -
222 /// MetadataContext handles uniquing and assignment of IDs for custom metadata
223 /// types. Custom metadata handler names do not contain spaces. And the name
224 /// must start with an alphabet. The regular expression used to check name
225 /// is [a-zA-Z$._][a-zA-Z$._0-9]*
226 class MetadataContext {
227 public:
228   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
229   typedef SmallVector<MDPairTy, 2> MDMapTy;
230   typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
231   friend class BitcodeReader;
232 private:
233
234   /// MetadataStore - Collection of metadata used in this context.
235   MDStoreTy MetadataStore;
236
237   /// MDHandlerNames - Map to hold metadata handler names.
238   StringMap<unsigned> MDHandlerNames;
239
240 public:
241   /// registerMDKind - Register a new metadata kind and return its ID.
242   /// A metadata kind can be registered only once. 
243   unsigned registerMDKind(StringRef Name);
244
245   /// getMDKind - Return metadata kind. If the requested metadata kind
246   /// is not registered then return 0.
247   unsigned getMDKind(StringRef Name) const;
248
249   /// isValidName - Return true if Name is a valid custom metadata handler name.
250   static bool isValidName(StringRef Name);
251
252   /// getMD - Get the metadata of given kind attached to an Instruction.
253   /// If the metadata is not found then return 0.
254   MDNode *getMD(unsigned Kind, const Instruction *Inst);
255
256   /// getMDs - Get the metadata attached to an Instruction.
257   const MDMapTy *getMDs(const Instruction *Inst);
258
259   /// addMD - Attach the metadata of given kind to an Instruction.
260   void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
261   
262   /// removeMD - Remove metadata of given kind attached with an instuction.
263   void removeMD(unsigned Kind, Instruction *Inst);
264   
265   /// removeAllMetadata - Remove all metadata attached with an instruction.
266   void removeAllMetadata(Instruction *Inst);
267
268   /// copyMD - If metadata is attached with Instruction In1 then attach
269   /// the same metadata to In2.
270   void copyMD(Instruction *In1, Instruction *In2);
271
272   /// getHandlerNames - Populate client supplied smallvector using custome
273   /// metadata name and ID.
274   void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
275
276   /// ValueIsDeleted - This handler is used to update metadata store
277   /// when a value is deleted.
278   void ValueIsDeleted(const Value *) {}
279   void ValueIsDeleted(Instruction *Inst) {
280     removeAllMetadata(Inst);
281   }
282   void ValueIsRAUWd(Value *V1, Value *V2);
283
284   /// ValueIsCloned - This handler is used to update metadata store
285   /// when In1 is cloned to create In2.
286   void ValueIsCloned(const Instruction *In1, Instruction *In2);
287 };
288
289 } // end llvm namespace
290
291 #endif