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