rename MDNode instance variables to something meaningful.
[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/ADT/FoldingSet.h"
21 #include "llvm/ADT/ilist_node.h"
22
23 namespace llvm {
24 class Constant;
25 class Instruction;
26 class LLVMContext;
27 class Module;
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
60 public:
61   static MDString *get(LLVMContext &Context, StringRef Str);
62   static MDString *get(LLVMContext &Context, const char *Str);
63   
64   StringRef getString() const { return Str; }
65
66   unsigned getLength() const { return (unsigned)Str.size(); }
67
68   typedef StringRef::iterator iterator;
69   
70   /// begin() - Pointer to the first byte of the string.
71   ///
72   iterator begin() const { return Str.begin(); }
73
74   /// end() - Pointer to one byte past the end of the string.
75   ///
76   iterator end() const { return Str.end(); }
77
78   /// Methods for support type inquiry through isa, cast, and dyn_cast:
79   static inline bool classof(const MDString *) { return true; }
80   static bool classof(const Value *V) {
81     return V->getValueID() == MDStringVal;
82   }
83 };
84
85   
86 class MDNodeElement;
87   
88 //===----------------------------------------------------------------------===//
89 /// MDNode - a tuple of other values.
90 /// These contain a list of the values that represent the metadata. 
91 /// MDNode is always unnamed.
92 class MDNode : public MetadataBase, public FoldingSetNode {
93   MDNode(const MDNode &);                // DO NOT IMPLEMENT
94
95   friend class MDNodeElement;
96   
97   static const unsigned short FunctionLocalBit = 1;
98   
99   // Replace each instance of F from the element list of this node with T.
100   void replaceElement(Value *F, Value *T);
101
102   MDNodeElement *Operands;
103   unsigned NumOperands;
104
105 protected:
106   explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
107                   bool isFunctionLocal);
108 public:
109   // Constructors and destructors.
110   static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
111                      bool isFunctionLocal = false);
112
113   /// ~MDNode - Destroy MDNode.
114   ~MDNode();
115   
116   /// getElement - Return specified element.
117   Value *getElement(unsigned i) const;
118   
119   /// getNumElements - Return number of MDNode elements.
120   unsigned getNumElements() const { return NumOperands; }
121   
122   /// isFunctionLocal - Return whether MDNode is local to a function.
123   /// Note: MDNodes are designated as function-local when created, and keep
124   ///       that designation even if their operands are modified to no longer
125   ///       refer to function-local IR.
126   bool isFunctionLocal() const { return SubclassData & FunctionLocalBit; }
127
128   /// getLocalFunction - Return false if MDNode's recursive function-localness
129   /// is invalid (local to more than one function).  Return true otherwise.
130   /// If MDNode has one function to which it is local, set LocalFunction to that
131   /// function.
132   bool getLocalFunction(Function *LocalFunction,
133                         SmallPtrSet<MDNode *, 32> *VisitedMDNodes = NULL);
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   void *Operands; // SmallVector<TrackingVH<MetadataBase>, 4>
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   
191   /// getNumElements - Return number of NamedMDNode elements.
192   unsigned getNumElements() const;
193
194   /// addElement - Add metadata element.
195   void addElement(MetadataBase *M);
196   
197   /// Methods for support type inquiry through isa, cast, and dyn_cast:
198   static inline bool classof(const NamedMDNode *) { return true; }
199   static bool classof(const Value *V) {
200     return V->getValueID() == NamedMDNodeVal;
201   }
202 };
203
204 //===----------------------------------------------------------------------===//
205 /// MetadataContext -
206 /// MetadataContext handles uniquing and assignment of IDs for custom metadata
207 /// types. Custom metadata handler names do not contain spaces. And the name
208 /// must start with an alphabet. The regular expression used to check name
209 /// is [a-zA-Z$._][a-zA-Z$._0-9]*
210 class MetadataContext {
211   // DO NOT IMPLEMENT
212   MetadataContext(MetadataContext&);
213   void operator=(MetadataContext&);
214
215   MetadataContextImpl *const pImpl;
216 public:
217   MetadataContext();
218   ~MetadataContext();
219
220   /// registerMDKind - Register a new metadata kind and return its ID.
221   /// A metadata kind can be registered only once. 
222   unsigned registerMDKind(StringRef Name);
223
224   /// getMDKind - Return metadata kind. If the requested metadata kind
225   /// is not registered then return 0.
226   unsigned getMDKind(StringRef Name) const;
227
228   /// isValidName - Return true if Name is a valid custom metadata handler name.
229   static bool isValidName(StringRef Name);
230
231   /// getMD - Get the metadata of given kind attached to an Instruction.
232   /// If the metadata is not found then return 0.
233   MDNode *getMD(unsigned Kind, const Instruction *Inst);
234
235   /// getMDs - Get the metadata attached to an Instruction.
236   void getMDs(const Instruction *Inst, 
237               SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const;
238
239   /// addMD - Attach the metadata of given kind to an Instruction.
240   void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
241   
242   /// removeMD - Remove metadata of given kind attached with an instuction.
243   void removeMD(unsigned Kind, Instruction *Inst);
244   
245   /// removeAllMetadata - Remove all metadata attached with an instruction.
246   void removeAllMetadata(Instruction *Inst);
247
248   /// copyMD - If metadata is attached with Instruction In1 then attach
249   /// the same metadata to In2.
250   void copyMD(Instruction *In1, Instruction *In2);
251
252   /// getHandlerNames - Populate client supplied smallvector using custom
253   /// metadata name and ID.
254   void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
255
256   /// ValueIsDeleted - This handler is used to update metadata store
257   /// when a value is deleted.
258   void ValueIsDeleted(const Value *) {}
259   void ValueIsDeleted(Instruction *Inst);
260   void ValueIsRAUWd(Value *V1, Value *V2);
261
262   /// ValueIsCloned - This handler is used to update metadata store
263   /// when In1 is cloned to create In2.
264   void ValueIsCloned(const Instruction *In1, Instruction *In2);
265 };
266
267 } // end llvm namespace
268
269 #endif