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