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/Value.h"
20 #include "llvm/Type.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/ilist_node.h"
24 #include "llvm/Support/ValueHandle.h"
30 class MetadataContextImpl;
32 //===----------------------------------------------------------------------===//
33 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
34 class MetadataBase : public Value {
36 MetadataBase(const Type *Ty, unsigned scid)
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;
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
58 explicit MDString(LLVMContext &C, StringRef S)
59 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
62 static MDString *get(LLVMContext &Context, StringRef Str);
63 static MDString *get(LLVMContext &Context, const char *Str);
65 StringRef getString() const { return Str; }
67 unsigned getLength() const { return (unsigned)Str.size(); }
69 typedef StringRef::iterator iterator;
71 /// begin() - Pointer to the first byte of the string.
73 iterator begin() const { return Str.begin(); }
75 /// end() - Pointer to one byte past the end of the string.
77 iterator end() const { return Str.end(); }
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;
86 //===----------------------------------------------------------------------===//
87 /// MDNode - a tuple of other values.
88 /// These contain a list of the values that represent the metadata.
89 /// MDNode is always unnamed.
90 class MDNode : public MetadataBase, public FoldingSetNode {
91 MDNode(const MDNode &); // DO NOT IMPLEMENT
93 friend class ElementVH;
94 // Use CallbackVH to hold MDNode elements.
95 struct ElementVH : public CallbackVH {
98 ElementVH(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
101 virtual void deleted() {
102 Parent->replaceElement(this->operator Value*(), 0);
105 virtual void allUsesReplacedWith(Value *NV) {
106 Parent->replaceElement(this->operator Value*(), NV);
109 // Replace each instance of F from the element list of this node with T.
110 void replaceElement(Value *F, Value *T);
116 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals);
118 // Constructors and destructors.
119 static MDNode *get(LLVMContext &Context,
120 Value *const *Vals, unsigned NumVals);
122 /// ~MDNode - Destroy MDNode.
125 /// getElement - Return specified element.
126 Value *getElement(unsigned i) const {
127 assert(i < getNumElements() && "Invalid element number!");
131 /// getNumElements - Return number of MDNode elements.
132 unsigned getNumElements() const { return NodeSize; }
134 /// Profile - calculate a unique identifier for this MDNode to collapse
136 void Profile(FoldingSetNodeID &ID) const;
138 /// Methods for support type inquiry through isa, cast, and dyn_cast:
139 static inline bool classof(const MDNode *) { return true; }
140 static bool classof(const Value *V) {
141 return V->getValueID() == MDNodeVal;
145 //===----------------------------------------------------------------------===//
146 /// NamedMDNode - a tuple of other metadata.
147 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
148 template<typename ValueSubClass, typename ItemParentClass>
149 class SymbolTableListTraits;
151 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
152 friend class SymbolTableListTraits<NamedMDNode, Module>;
153 friend class LLVMContextImpl;
155 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
158 SmallVector<TrackingVH<MetadataBase>, 4> Node;
160 void setParent(Module *M) { Parent = M; }
162 explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals,
163 unsigned NumVals, Module *M = 0);
165 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
166 MetadataBase *const *MDs,
167 unsigned NumMDs, Module *M = 0) {
168 return new NamedMDNode(C, N, MDs, NumMDs, M);
171 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
173 /// eraseFromParent - Drop all references and remove the node from parent
175 void eraseFromParent();
177 /// dropAllReferences - Remove all uses and clear node vector.
178 void dropAllReferences();
180 /// ~NamedMDNode - Destroy NamedMDNode.
183 /// getParent - Get the module that holds this named metadata collection.
184 inline Module *getParent() { return Parent; }
185 inline const Module *getParent() const { return Parent; }
187 /// getElement - Return specified element.
188 MetadataBase *getElement(unsigned i) const {
189 assert(i < getNumElements() && "Invalid element number!");
193 /// getNumElements - Return number of NamedMDNode elements.
194 unsigned getNumElements() const {
195 return (unsigned)Node.size();
198 /// addElement - Add metadata element.
199 void addElement(MetadataBase *M) {
200 Node.push_back(TrackingVH<MetadataBase>(M));
203 typedef SmallVectorImpl<TrackingVH<MetadataBase> >::iterator elem_iterator;
204 typedef SmallVectorImpl<TrackingVH<MetadataBase> >::const_iterator
206 bool elem_empty() const { return Node.empty(); }
207 const_elem_iterator elem_begin() const { return Node.begin(); }
208 const_elem_iterator elem_end() const { return Node.end(); }
209 elem_iterator elem_begin() { return Node.begin(); }
210 elem_iterator elem_end() { return Node.end(); }
212 /// Methods for support type inquiry through isa, cast, and dyn_cast:
213 static inline bool classof(const NamedMDNode *) { return true; }
214 static bool classof(const Value *V) {
215 return V->getValueID() == NamedMDNodeVal;
219 //===----------------------------------------------------------------------===//
220 /// MetadataContext -
221 /// MetadataContext handles uniquing and assignment of IDs for custom metadata
222 /// types. Custom metadata handler names do not contain spaces. And the name
223 /// must start with an alphabet. The regular expression used to check name
224 /// is [a-zA-Z$._][a-zA-Z$._0-9]*
225 class MetadataContext {
227 MetadataContext(MetadataContext&);
228 void operator=(MetadataContext&);
230 MetadataContextImpl *const pImpl;
235 /// registerMDKind - Register a new metadata kind and return its ID.
236 /// A metadata kind can be registered only once.
237 unsigned registerMDKind(StringRef Name);
239 /// getMDKind - Return metadata kind. If the requested metadata kind
240 /// is not registered then return 0.
241 unsigned getMDKind(StringRef Name) const;
243 /// isValidName - Return true if Name is a valid custom metadata handler name.
244 static bool isValidName(StringRef Name);
246 /// getMD - Get the metadata of given kind attached to an Instruction.
247 /// If the metadata is not found then return 0.
248 MDNode *getMD(unsigned Kind, const Instruction *Inst);
250 /// getMDs - Get the metadata attached to an Instruction.
251 void getMDs(const Instruction *Inst,
252 SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const;
254 /// addMD - Attach the metadata of given kind to an Instruction.
255 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
257 /// removeMD - Remove metadata of given kind attached with an instuction.
258 void removeMD(unsigned Kind, Instruction *Inst);
260 /// removeAllMetadata - Remove all metadata attached with an instruction.
261 void removeAllMetadata(Instruction *Inst);
263 /// copyMD - If metadata is attached with Instruction In1 then attach
264 /// the same metadata to In2.
265 void copyMD(Instruction *In1, Instruction *In2);
267 /// getHandlerNames - Populate client supplied smallvector using custom
268 /// metadata name and ID.
269 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
271 /// ValueIsDeleted - This handler is used to update metadata store
272 /// when a value is deleted.
273 void ValueIsDeleted(const Value *) {}
274 void ValueIsDeleted(Instruction *Inst);
275 void ValueIsRAUWd(Value *V1, Value *V2);
277 /// ValueIsCloned - This handler is used to update metadata store
278 /// when In1 is cloned to create In2.
279 void ValueIsCloned(const Instruction *In1, Instruction *In2);
282 } // end llvm namespace