1 //===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file contains the declarations for metadata subclasses.
12 /// They represent the different flavors of metadata that live in LLVM.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_METADATA_H
17 #define LLVM_METADATA_H
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"
34 //===----------------------------------------------------------------------===//
35 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
36 class MetadataBase : public User {
38 /// ReservedSpace - The number of operands actually allocated. NumOperands is
39 /// the number actually in use.
40 unsigned ReservedSpace;
43 MetadataBase(const Type *Ty, unsigned scid)
44 : User(Ty, scid, NULL, 0), ReservedSpace(0) {}
46 void resizeOperands(unsigned NumOps);
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;
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
68 explicit MDString(LLVMContext &C, const char *begin, unsigned l)
69 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(begin, l) {}
72 // Do not allocate any space for operands.
73 void *operator new(size_t s) {
74 return User::operator new(s, 0);
76 static MDString *get(LLVMContext &Context, const StringRef &Str);
78 StringRef getString() const { return Str; }
80 unsigned getLength() const { return Str.size(); }
82 typedef StringRef::iterator iterator;
84 /// begin() - Pointer to the first byte of the string.
86 iterator begin() const { return Str.begin(); }
88 /// end() - Pointer to one byte past the end of the string.
90 iterator end() const { return Str.end(); }
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;
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(); }
109 friend class ElementVH;
110 // Use CallbackVH to hold MDNOde elements.
111 struct ElementVH : public CallbackVH {
113 ElementVH(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
116 virtual void deleted() {
117 Parent->replaceElement(this->operator Value*(), 0);
120 virtual void allUsesReplacedWith(Value *NV) {
121 Parent->replaceElement(this->operator Value*(), NV);
124 // Replace each instance of F from the element list of this node with T.
125 void replaceElement(Value *F, Value *T);
127 SmallVector<ElementVH, 4> Node;
130 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals);
132 // Do not allocate any space for operands.
133 void *operator new(size_t s) {
134 return User::operator new(s, 0);
136 // Constructors and destructors.
137 static MDNode *get(LLVMContext &Context,
138 Value *const *Vals, unsigned NumVals);
140 /// dropAllReferences - Remove all uses and clear node vector.
141 void dropAllReferences();
143 /// ~MDNode - Destroy MDNode.
146 /// getElement - Return specified element.
147 Value *getElement(unsigned i) const {
148 assert(getNumElements() > i && "Invalid element number!");
152 /// getNumElements - Return number of MDNode elements.
153 unsigned getNumElements() const {
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(); }
167 /// Profile - calculate a unique identifier for this MDNode to collapse
169 void Profile(FoldingSetNodeID &ID) const;
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;
178 //===----------------------------------------------------------------------===//
179 /// WeakMetadataVH - a weak value handle for metadata.
180 class WeakMetadataVH : public WeakVH {
182 WeakMetadataVH() : WeakVH() {}
183 WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
184 WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
186 operator Value*() const {
187 llvm_unreachable("WeakMetadataVH only handles Metadata");
190 operator MetadataBase*() const {
191 return dyn_cast_or_null<MetadataBase>(getValPtr());
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;
201 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
202 friend class SymbolTableListTraits<NamedMDNode, Module>;
203 friend class LLVMContextImpl;
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(); }
211 SmallVector<WeakMetadataVH, 4> Node;
212 typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
215 explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals,
216 unsigned NumVals, Module *M = 0);
218 // Do not allocate any space for operands.
219 void *operator new(size_t s) {
220 return User::operator new(s, 0);
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);
228 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
230 /// eraseFromParent - Drop all references and remove the node from parent
232 void eraseFromParent();
234 /// dropAllReferences - Remove all uses and clear node vector.
235 void dropAllReferences();
237 /// ~NamedMDNode - Destroy NamedMDNode.
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; }
245 /// getElement - Return specified element.
246 MetadataBase *getElement(unsigned i) const {
247 assert(getNumElements() > i && "Invalid element number!");
251 /// getNumElements - Return number of NamedMDNode elements.
252 unsigned getNumElements() const {
256 /// addElement - Add metadata element.
257 void addElement(MetadataBase *M) {
259 OperandList[NumOperands++] = M;
260 Node.push_back(WeakMetadataVH(M));
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(); }
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;
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 {
285 typedef std::pair<unsigned, WeakVH> MDPairTy;
286 typedef SmallVector<MDPairTy, 2> MDMapTy;
287 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
288 friend class BitcodeReader;
291 /// MetadataStore - Collection of metadata used in this context.
292 MDStoreTy MetadataStore;
294 /// MDHandlerNames - Map to hold metadata handler names.
295 StringMap<unsigned> MDHandlerNames;
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);
302 /// getMDKind - Return metadata kind. If the requested metadata kind
303 /// is not registered then return 0.
304 unsigned getMDKind(const char *Name);
306 /// validName - Return true if Name is a valid custom metadata handler name.
307 bool validName(const char *Name);
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);
313 /// getMDs - Get the metadata attached with an Instruction.
314 const MDMapTy *getMDs(const Instruction *Inst);
316 /// addMD - Attach the metadata of given kind with an Instruction.
317 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
319 /// removeMD - Remove metadata of given kind attached with an instuction.
320 void removeMD(unsigned Kind, Instruction *Inst);
322 /// removeMDs - Remove all metadata attached with an instruction.
323 void removeMDs(const Instruction *Inst);
325 /// copyMD - If metadata is attached with Instruction In1 then attach
326 /// the same metadata to In2.
327 void copyMD(Instruction *In1, Instruction *In2);
329 /// getHandlerNames - Get handler names. This is used by bitcode
331 const StringMap<unsigned> *getHandlerNames();
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) {
339 void ValueIsRAUWd(Value *V1, Value *V2);
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);
346 } // end llvm namespace