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