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