prune #includes more.
[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/ilist_node.h"
23
24 namespace llvm {
25 class Constant;
26 class Instruction;
27 class LLVMContext;
28 class MetadataContextImpl;
29 template <typename T> class SmallVectorImpl;
30 template<class PtrType, unsigned SmallSize> class SmallPtrSet;
31
32 //===----------------------------------------------------------------------===//
33 // MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
34 class MetadataBase : public Value {
35 protected:
36   MetadataBase(const Type *Ty, unsigned scid)
37     : Value(Ty, scid) {}
38
39 public:
40
41   /// Methods for support type inquiry through isa, cast, and dyn_cast:
42   static inline bool classof(const MetadataBase *) { return true; }
43   static bool classof(const Value *V) {
44     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
45       || V->getValueID() == NamedMDNodeVal;
46   }
47 };
48
49 //===----------------------------------------------------------------------===//
50 /// MDString - a single uniqued string.
51 /// These are used to efficiently contain a byte sequence for metadata.
52 /// MDString is always unnamd.
53 class MDString : public MetadataBase {
54   MDString(const MDString &);            // DO NOT IMPLEMENT
55
56   StringRef Str;
57 protected:
58   explicit MDString(LLVMContext &C, StringRef S)
59     : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
60
61 public:
62   static MDString *get(LLVMContext &Context, StringRef Str);
63   static MDString *get(LLVMContext &Context, const char *Str);
64   
65   StringRef getString() const { return Str; }
66
67   unsigned getLength() const { return (unsigned)Str.size(); }
68
69   typedef StringRef::iterator iterator;
70   
71   /// begin() - Pointer to the first byte of the string.
72   ///
73   iterator begin() const { return Str.begin(); }
74
75   /// end() - Pointer to one byte past the end of the string.
76   ///
77   iterator end() const { return Str.end(); }
78
79   /// Methods for support type inquiry through isa, cast, and dyn_cast:
80   static inline bool classof(const MDString *) { return true; }
81   static bool classof(const Value *V) {
82     return V->getValueID() == MDStringVal;
83   }
84 };
85
86   
87 class MDNodeElement;
88   
89 //===----------------------------------------------------------------------===//
90 /// MDNode - a tuple of other values.
91 /// These contain a list of the values that represent the metadata. 
92 /// MDNode is always unnamed.
93 class MDNode : public MetadataBase, public FoldingSetNode {
94   MDNode(const MDNode &);                // DO NOT IMPLEMENT
95
96   friend class MDNodeElement;
97   
98   static const unsigned short FunctionLocalBit = 1;
99   
100   // Replace each instance of F from the element list of this node with T.
101   void replaceElement(Value *F, Value *T);
102
103   MDNodeElement *Node;
104   unsigned NodeSize;
105
106 protected:
107   explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
108                   bool isFunctionLocal);
109 public:
110   // Constructors and destructors.
111   static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
112                      bool isFunctionLocal = false);
113
114   /// ~MDNode - Destroy MDNode.
115   ~MDNode();
116   
117   /// getElement - Return specified element.
118   Value *getElement(unsigned i) const;
119   
120   /// getNumElements - Return number of MDNode elements.
121   unsigned getNumElements() const { return NodeSize; }
122   
123   /// isFunctionLocal - Return whether MDNode is local to a function.
124   /// Note: MDNodes are designated as function-local when created, and keep
125   ///       that designation even if their operands are modified to no longer
126   ///       refer to function-local IR.
127   bool isFunctionLocal() const { return SubclassData & FunctionLocalBit; }
128
129   /// getLocalFunction - Return false if MDNode's recursive function-localness
130   /// is invalid (local to more than one function).  Return true otherwise.
131   /// If MDNode has one function to which it is local, set LocalFunction to that
132   /// function.
133   bool getLocalFunction(Function *LocalFunction,
134                         SmallPtrSet<MDNode *, 32> *VisitedMDNodes = NULL);
135
136   /// Profile - calculate a unique identifier for this MDNode to collapse
137   /// duplicates
138   void Profile(FoldingSetNodeID &ID) const;
139
140   /// Methods for support type inquiry through isa, cast, and dyn_cast:
141   static inline bool classof(const MDNode *) { return true; }
142   static bool classof(const Value *V) {
143     return V->getValueID() == MDNodeVal;
144   }
145 };
146
147 //===----------------------------------------------------------------------===//
148 /// NamedMDNode - a tuple of other metadata. 
149 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
150 template<typename ValueSubClass, typename ItemParentClass>
151   class SymbolTableListTraits;
152
153 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
154   friend class SymbolTableListTraits<NamedMDNode, Module>;
155   friend class LLVMContextImpl;
156
157   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
158
159   Module *Parent;
160   void *Operands; // SmallVector<TrackingVH<MetadataBase>, 4>
161
162   void setParent(Module *M) { Parent = M; }
163 protected:
164   explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals, 
165                        unsigned NumVals, Module *M = 0);
166 public:
167   static NamedMDNode *Create(LLVMContext &C, const Twine &N, 
168                              MetadataBase *const *MDs, 
169                              unsigned NumMDs, Module *M = 0) {
170     return new NamedMDNode(C, N, MDs, NumMDs, M);
171   }
172
173   static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
174
175   /// eraseFromParent - Drop all references and remove the node from parent
176   /// module.
177   void eraseFromParent();
178
179   /// dropAllReferences - Remove all uses and clear node vector.
180   void dropAllReferences();
181
182   /// ~NamedMDNode - Destroy NamedMDNode.
183   ~NamedMDNode();
184
185   /// getParent - Get the module that holds this named metadata collection.
186   inline Module *getParent() { return Parent; }
187   inline const Module *getParent() const { return Parent; }
188
189   /// getElement - Return specified element.
190   MetadataBase *getElement(unsigned i) const;
191   
192   /// getNumElements - Return number of NamedMDNode elements.
193   unsigned getNumElements() const;
194
195   /// addElement - Add metadata element.
196   void addElement(MetadataBase *M);
197   
198   /// Methods for support type inquiry through isa, cast, and dyn_cast:
199   static inline bool classof(const NamedMDNode *) { return true; }
200   static bool classof(const Value *V) {
201     return V->getValueID() == NamedMDNodeVal;
202   }
203 };
204
205 //===----------------------------------------------------------------------===//
206 /// MetadataContext -
207 /// MetadataContext handles uniquing and assignment of IDs for custom metadata
208 /// types. Custom metadata handler names do not contain spaces. And the name
209 /// must start with an alphabet. The regular expression used to check name
210 /// is [a-zA-Z$._][a-zA-Z$._0-9]*
211 class MetadataContext {
212   // DO NOT IMPLEMENT
213   MetadataContext(MetadataContext&);
214   void operator=(MetadataContext&);
215
216   MetadataContextImpl *const pImpl;
217 public:
218   MetadataContext();
219   ~MetadataContext();
220
221   /// registerMDKind - Register a new metadata kind and return its ID.
222   /// A metadata kind can be registered only once. 
223   unsigned registerMDKind(StringRef Name);
224
225   /// getMDKind - Return metadata kind. If the requested metadata kind
226   /// is not registered then return 0.
227   unsigned getMDKind(StringRef Name) const;
228
229   /// isValidName - Return true if Name is a valid custom metadata handler name.
230   static bool isValidName(StringRef Name);
231
232   /// getMD - Get the metadata of given kind attached to an Instruction.
233   /// If the metadata is not found then return 0.
234   MDNode *getMD(unsigned Kind, const Instruction *Inst);
235
236   /// getMDs - Get the metadata attached to an Instruction.
237   void getMDs(const Instruction *Inst, 
238               SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const;
239
240   /// addMD - Attach the metadata of given kind to an Instruction.
241   void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
242   
243   /// removeMD - Remove metadata of given kind attached with an instuction.
244   void removeMD(unsigned Kind, Instruction *Inst);
245   
246   /// removeAllMetadata - Remove all metadata attached with an instruction.
247   void removeAllMetadata(Instruction *Inst);
248
249   /// copyMD - If metadata is attached with Instruction In1 then attach
250   /// the same metadata to In2.
251   void copyMD(Instruction *In1, Instruction *In2);
252
253   /// getHandlerNames - Populate client supplied smallvector using custom
254   /// metadata name and ID.
255   void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
256
257   /// ValueIsDeleted - This handler is used to update metadata store
258   /// when a value is deleted.
259   void ValueIsDeleted(const Value *) {}
260   void ValueIsDeleted(Instruction *Inst);
261   void ValueIsRAUWd(Value *V1, Value *V2);
262
263   /// ValueIsCloned - This handler is used to update metadata store
264   /// when In1 is cloned to create In2.
265   void ValueIsCloned(const Instruction *In1, Instruction *In2);
266 };
267
268 } // end llvm namespace
269
270 #endif