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 //===----------------------------------------------------------------------===//
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"
37 //===----------------------------------------------------------------------===//
38 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
39 class MetadataBase : public User {
41 /// ReservedSpace - The number of operands actually allocated. NumOperands is
42 /// the number actually in use.
43 unsigned ReservedSpace;
46 MetadataBase(const Type *Ty, unsigned scid)
47 : User(Ty, scid, NULL, 0), ReservedSpace(0) {}
49 void resizeOperands(unsigned NumOps);
51 /// isNullValue - Return true if this is the value that would be returned by
52 /// getNullValue. This always returns false because getNullValue will never
54 virtual bool isNullValue() const {
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;
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
77 explicit MDString(LLVMContext &C, const char *begin, unsigned l)
78 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(begin, l) {}
81 // Do not allocate any space for operands.
82 void *operator new(size_t s) {
83 return User::operator new(s, 0);
85 static MDString *get(LLVMContext &Context, const StringRef &Str);
87 StringRef getString() const { return Str; }
89 unsigned length() const { return Str.size(); }
91 /// begin() - Pointer to the first byte of the string.
93 const char *begin() const { return Str.begin(); }
95 /// end() - Pointer to one byte past the end of the string.
97 const char *end() const { return Str.end(); }
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;
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(); }
116 friend class ElementVH;
117 // Use CallbackVH to hold MDNOde elements.
118 struct ElementVH : public CallbackVH {
120 ElementVH(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
123 virtual void deleted() {
124 Parent->replaceElement(this->operator Value*(), 0);
127 virtual void allUsesReplacedWith(Value *NV) {
128 Parent->replaceElement(this->operator Value*(), NV);
131 // Replace each instance of F from the element list of this node with T.
132 void replaceElement(Value *F, Value *T);
134 SmallVector<ElementVH, 4> Node;
137 explicit MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals);
139 // Do not allocate any space for operands.
140 void *operator new(size_t s) {
141 return User::operator new(s, 0);
143 // Constructors and destructors.
144 static MDNode *get(LLVMContext &Context,
145 Value* const* Vals, unsigned NumVals);
147 /// dropAllReferences - Remove all uses and clear node vector.
148 void dropAllReferences();
150 /// ~MDNode - Destroy MDNode.
153 /// getElement - Return specified element.
154 Value *getElement(unsigned i) const {
155 assert (getNumElements() > i && "Invalid element number!");
159 /// getNumElements - Return number of MDNode elements.
160 unsigned getNumElements() const {
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(); }
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 {
181 /// Profile - calculate a unique identifier for this MDNode to collapse
183 void Profile(FoldingSetNodeID &ID) const;
185 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
186 llvm_unreachable("This should never be called because MDNodes have no ops");
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;
196 //===----------------------------------------------------------------------===//
197 /// WeakMetadataVH - a weak value handle for metadata.
198 class WeakMetadataVH : public WeakVH {
200 WeakMetadataVH() : WeakVH() {}
201 WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
202 WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
204 operator Value*() const {
205 llvm_unreachable("WeakMetadataVH only handles Metadata");
208 operator MetadataBase*() const {
209 return dyn_cast_or_null<MetadataBase>(getValPtr());
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;
219 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
220 friend class SymbolTableListTraits<NamedMDNode, Module>;
221 friend class LLVMContextImpl;
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(); }
229 SmallVector<WeakMetadataVH, 4> Node;
230 typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
233 explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const* Vals,
234 unsigned NumVals, Module *M = 0);
236 // Do not allocate any space for operands.
237 void *operator new(size_t s) {
238 return User::operator new(s, 0);
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);
246 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
248 /// eraseFromParent - Drop all references and remove the node from parent
250 void eraseFromParent();
252 /// dropAllReferences - Remove all uses and clear node vector.
253 void dropAllReferences();
255 /// ~NamedMDNode - Destroy NamedMDNode.
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; }
263 /// getElement - Return specified element.
264 MetadataBase *getElement(unsigned i) const {
265 assert (getNumElements() > i && "Invalid element number!");
269 /// getNumElements - Return number of NamedMDNode elements.
270 unsigned getNumElements() const {
274 /// addElement - Add metadata element.
275 void addElement(MetadataBase *M) {
277 OperandList[NumOperands++] = M;
278 Node.push_back(WeakMetadataVH(M));
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(); }
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 {
295 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
297 "This should never be called because NamedMDNodes have no ops");
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;
307 //===----------------------------------------------------------------------===//
309 /// Metadata manages metadata used in a context.
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;
316 typedef std::pair<MDKindID, WeakVH> MDPairTy;
317 typedef SmallVector<MDPairTy, 2> MDMapTy;
318 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
320 /// MetadataStore - Collection of metadata used in this context.
321 MDStoreTy MetadataStore;
323 /// MDHandlerNames - Map to hold metadata handler names.
324 StringMap<unsigned> MDHandlerNames;
328 /// RegisterMDKind - Register a new metadata kind and return its ID.
329 /// A metadata kind can be registered only once.
330 MDKindID RegisterMDKind(const char *Name);
332 /// getMDKind - Return metadata kind. If the requested metadata kind
333 /// is not registered then return 0.
334 MDKindID getMDKind(const char *Name);
336 /// getMD - Get the metadata of given kind attached with an Instruction.
337 /// If the metadata is not found then return 0.
338 MDNode *getMD(MDKindID Kind, const Instruction *Inst);
340 /// setMD - Attach the metadata of given kind with an Instruction.
341 void setMD(MDKindID Kind, MDNode *Node, Instruction *Inst);
343 /// ValueIsDeleted - This handler is used to update metadata store
344 /// when a value is deleted.
345 void ValueIsDeleted(Value *V) {}
346 void ValueIsDeleted(const Instruction *Inst);
349 } // end llvm namespace