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, StringRef S)
61 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
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 /// NamedMDNode - a tuple of other metadata.
148 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
149 template<typename ValueSubClass, typename ItemParentClass>
150 class SymbolTableListTraits;
152 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
153 friend class SymbolTableListTraits<NamedMDNode, Module>;
154 friend class LLVMContextImpl;
156 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
159 SmallVector<TrackingVH<MetadataBase>, 4> Node;
161 void setParent(Module *M) { Parent = M; }
163 explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals,
164 unsigned NumVals, Module *M = 0);
166 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
167 MetadataBase *const *MDs,
168 unsigned NumMDs, Module *M = 0) {
169 return new NamedMDNode(C, N, MDs, NumMDs, M);
172 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
174 /// eraseFromParent - Drop all references and remove the node from parent
176 void eraseFromParent();
178 /// dropAllReferences - Remove all uses and clear node vector.
179 void dropAllReferences();
181 /// ~NamedMDNode - Destroy NamedMDNode.
184 /// getParent - Get the module that holds this named metadata collection.
185 inline Module *getParent() { return Parent; }
186 inline const Module *getParent() const { return Parent; }
188 /// getElement - Return specified element.
189 MetadataBase *getElement(unsigned i) const {
190 assert(i < getNumElements() && "Invalid element number!");
194 /// getNumElements - Return number of NamedMDNode elements.
195 unsigned getNumElements() const {
199 /// addElement - Add metadata element.
200 void addElement(MetadataBase *M) {
201 Node.push_back(TrackingVH<MetadataBase>(M));
204 typedef SmallVectorImpl<TrackingVH<MetadataBase> >::iterator elem_iterator;
205 typedef SmallVectorImpl<TrackingVH<MetadataBase> >::const_iterator
207 bool elem_empty() const { return Node.empty(); }
208 const_elem_iterator elem_begin() const { return Node.begin(); }
209 const_elem_iterator elem_end() const { return Node.end(); }
210 elem_iterator elem_begin() { return Node.begin(); }
211 elem_iterator elem_end() { return Node.end(); }
213 /// Methods for support type inquiry through isa, cast, and dyn_cast:
214 static inline bool classof(const NamedMDNode *) { return true; }
215 static bool classof(const Value *V) {
216 return V->getValueID() == NamedMDNodeVal;
220 //===----------------------------------------------------------------------===//
221 /// MetadataContext -
222 /// MetadataContext handles uniquing and assignment of IDs for custom metadata
223 /// types. Custom metadata handler names do not contain spaces. And the name
224 /// must start with an alphabet. The regular expression used to check name
225 /// is [a-zA-Z$._][a-zA-Z$._0-9]*
226 class MetadataContext {
228 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
229 typedef SmallVector<MDPairTy, 2> MDMapTy;
230 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
231 friend class BitcodeReader;
234 /// MetadataStore - Collection of metadata used in this context.
235 MDStoreTy MetadataStore;
237 /// MDHandlerNames - Map to hold metadata handler names.
238 StringMap<unsigned> MDHandlerNames;
241 /// registerMDKind - Register a new metadata kind and return its ID.
242 /// A metadata kind can be registered only once.
243 unsigned registerMDKind(StringRef Name);
245 /// getMDKind - Return metadata kind. If the requested metadata kind
246 /// is not registered then return 0.
247 unsigned getMDKind(StringRef Name) const;
249 /// isValidName - Return true if Name is a valid custom metadata handler name.
250 static bool isValidName(StringRef Name);
252 /// getMD - Get the metadata of given kind attached to an Instruction.
253 /// If the metadata is not found then return 0.
254 MDNode *getMD(unsigned Kind, const Instruction *Inst);
256 /// getMDs - Get the metadata attached to an Instruction.
257 const MDMapTy *getMDs(const Instruction *Inst);
259 /// addMD - Attach the metadata of given kind to an Instruction.
260 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
262 /// removeMD - Remove metadata of given kind attached with an instuction.
263 void removeMD(unsigned Kind, Instruction *Inst);
265 /// removeAllMetadata - Remove all metadata attached with an instruction.
266 void removeAllMetadata(Instruction *Inst);
268 /// copyMD - If metadata is attached with Instruction In1 then attach
269 /// the same metadata to In2.
270 void copyMD(Instruction *In1, Instruction *In2);
272 /// getHandlerNames - Populate client supplied smallvector using custome
273 /// metadata name and ID.
274 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
276 /// ValueIsDeleted - This handler is used to update metadata store
277 /// when a value is deleted.
278 void ValueIsDeleted(const Value *) {}
279 void ValueIsDeleted(Instruction *Inst) {
280 removeAllMetadata(Inst);
282 void ValueIsRAUWd(Value *V1, Value *V2);
284 /// ValueIsCloned - This handler is used to update metadata store
285 /// when In1 is cloned to create In2.
286 void ValueIsCloned(const Instruction *In1, Instruction *In2);
289 } // end llvm namespace