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/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 Value {
38 MetadataBase(const Type *Ty, unsigned scid)
43 /// Methods for support type inquiry through isa, cast, and dyn_cast:
44 static inline bool classof(const MetadataBase *) { return true; }
45 static bool classof(const Value *V) {
46 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
47 || V->getValueID() == NamedMDNodeVal;
51 //===----------------------------------------------------------------------===//
52 /// MDString - a single uniqued string.
53 /// These are used to efficiently contain a byte sequence for metadata.
54 /// MDString is always unnamd.
55 class MDString : public MetadataBase {
56 MDString(const MDString &); // DO NOT IMPLEMENT
60 explicit MDString(LLVMContext &C, const char *begin, unsigned l)
61 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(begin, l) {}
64 static MDString *get(LLVMContext &Context, StringRef Str);
66 StringRef getString() const { return Str; }
68 unsigned getLength() const { return Str.size(); }
70 typedef StringRef::iterator iterator;
72 /// begin() - Pointer to the first byte of the string.
74 iterator begin() const { return Str.begin(); }
76 /// end() - Pointer to one byte past the end of the string.
78 iterator end() const { return Str.end(); }
80 /// Methods for support type inquiry through isa, cast, and dyn_cast:
81 static inline bool classof(const MDString *) { return true; }
82 static bool classof(const Value *V) {
83 return V->getValueID() == MDStringVal;
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
94 friend class ElementVH;
95 // Use CallbackVH to hold MDNOde elements.
96 struct ElementVH : public CallbackVH {
99 ElementVH(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
102 virtual void deleted() {
103 Parent->replaceElement(this->operator Value*(), 0);
106 virtual void allUsesReplacedWith(Value *NV) {
107 Parent->replaceElement(this->operator Value*(), NV);
110 // Replace each instance of F from the element list of this node with T.
111 void replaceElement(Value *F, Value *T);
117 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals);
119 // Constructors and destructors.
120 static MDNode *get(LLVMContext &Context,
121 Value *const *Vals, unsigned NumVals);
123 /// ~MDNode - Destroy MDNode.
126 /// getElement - Return specified element.
127 Value *getElement(unsigned i) const {
128 assert(i < getNumElements() && "Invalid element number!");
132 /// getNumElements - Return number of MDNode elements.
133 unsigned getNumElements() const { return NodeSize; }
135 /// Profile - calculate a unique identifier for this MDNode to collapse
137 void Profile(FoldingSetNodeID &ID) const;
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;
146 //===----------------------------------------------------------------------===//
147 /// WeakMetadataVH - a weak value handle for metadata.
148 class WeakMetadataVH : public WeakVH {
150 WeakMetadataVH() : WeakVH() {}
151 WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
152 WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
154 operator Value*() const {
155 llvm_unreachable("WeakMetadataVH only handles Metadata");
158 operator MetadataBase*() const {
159 return dyn_cast_or_null<MetadataBase>(getValPtr());
163 //===----------------------------------------------------------------------===//
164 /// NamedMDNode - a tuple of other metadata.
165 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
166 template<typename ValueSubClass, typename ItemParentClass>
167 class SymbolTableListTraits;
169 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
170 friend class SymbolTableListTraits<NamedMDNode, Module>;
171 friend class LLVMContextImpl;
173 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
176 SmallVector<WeakMetadataVH, 4> Node;
177 typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
179 void setParent(Module *M) { Parent = M; }
181 explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals,
182 unsigned NumVals, Module *M = 0);
184 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
185 MetadataBase *const *MDs,
186 unsigned NumMDs, Module *M = 0) {
187 return new NamedMDNode(C, N, MDs, NumMDs, M);
190 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
192 /// eraseFromParent - Drop all references and remove the node from parent
194 void eraseFromParent();
196 /// dropAllReferences - Remove all uses and clear node vector.
197 void dropAllReferences();
199 /// ~NamedMDNode - Destroy NamedMDNode.
202 /// getParent - Get the module that holds this named metadata collection.
203 inline Module *getParent() { return Parent; }
204 inline const Module *getParent() const { return Parent; }
206 /// getElement - Return specified element.
207 MetadataBase *getElement(unsigned i) const {
208 assert(i < getNumElements() && "Invalid element number!");
212 /// getNumElements - Return number of NamedMDNode elements.
213 unsigned getNumElements() const {
217 /// addElement - Add metadata element.
218 void addElement(MetadataBase *M) {
219 Node.push_back(WeakMetadataVH(M));
222 typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
223 bool elem_empty() const { return Node.empty(); }
224 const_elem_iterator elem_begin() const { return Node.begin(); }
225 const_elem_iterator elem_end() const { return Node.end(); }
226 elem_iterator elem_begin() { return Node.begin(); }
227 elem_iterator elem_end() { return Node.end(); }
229 /// Methods for support type inquiry through isa, cast, and dyn_cast:
230 static inline bool classof(const NamedMDNode *) { return true; }
231 static bool classof(const Value *V) {
232 return V->getValueID() == NamedMDNodeVal;
236 //===----------------------------------------------------------------------===//
237 /// MetadataContext -
238 /// MetadataContext handles uniquing and assignment of IDs for custom metadata
239 /// types. Custom metadata handler names do not contain spaces. And the name
240 /// must start with an alphabet. The regular expression used to check name
241 /// is [a-zA-Z$._][a-zA-Z$._0-9]*
242 class MetadataContext {
244 typedef std::pair<unsigned, WeakVH> MDPairTy;
245 typedef SmallVector<MDPairTy, 2> MDMapTy;
246 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
247 friend class BitcodeReader;
250 /// MetadataStore - Collection of metadata used in this context.
251 MDStoreTy MetadataStore;
253 /// MDHandlerNames - Map to hold metadata handler names.
254 StringMap<unsigned> MDHandlerNames;
257 /// registerMDKind - Register a new metadata kind and return its ID.
258 /// A metadata kind can be registered only once.
259 unsigned registerMDKind(StringRef Name);
261 /// getMDKind - Return metadata kind. If the requested metadata kind
262 /// is not registered then return 0.
263 unsigned getMDKind(StringRef Name) const;
265 /// isValidName - Return true if Name is a valid custom metadata handler name.
266 static bool isValidName(StringRef Name);
268 /// getMD - Get the metadata of given kind attached to an Instruction.
269 /// If the metadata is not found then return 0.
270 MDNode *getMD(unsigned Kind, const Instruction *Inst);
272 /// getMDs - Get the metadata attached to an Instruction.
273 const MDMapTy *getMDs(const Instruction *Inst);
275 /// addMD - Attach the metadata of given kind to an Instruction.
276 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
278 /// removeMD - Remove metadata of given kind attached with an instuction.
279 void removeMD(unsigned Kind, Instruction *Inst);
281 /// removeAllMetadata - Remove all metadata attached with an instruction.
282 void removeAllMetadata(Instruction *Inst);
284 /// copyMD - If metadata is attached with Instruction In1 then attach
285 /// the same metadata to In2.
286 void copyMD(Instruction *In1, Instruction *In2);
288 /// getHandlerNames - Get handler names. This is used by bitcode
290 const StringMap<unsigned> *getHandlerNames();
292 /// ValueIsDeleted - This handler is used to update metadata store
293 /// when a value is deleted.
294 void ValueIsDeleted(const Value *) {}
295 void ValueIsDeleted(Instruction *Inst) {
296 removeAllMetadata(Inst);
298 void ValueIsRAUWd(Value *V1, Value *V2);
300 /// ValueIsCloned - This handler is used to update metadata store
301 /// when In1 is cloned to create In2.
302 void ValueIsCloned(const Instruction *In1, Instruction *In2);
305 } // end llvm namespace