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/ADT/FoldingSet.h"
21 #include "llvm/ADT/ilist_node.h"
28 class MetadataContextImpl;
29 template <typename T> class SmallVectorImpl;
31 //===----------------------------------------------------------------------===//
32 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
33 class MetadataBase : public Value {
35 MetadataBase(const Type *Ty, unsigned scid)
40 /// Methods for support type inquiry through isa, cast, and dyn_cast:
41 static inline bool classof(const MetadataBase *) { return true; }
42 static bool classof(const Value *V) {
43 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
44 || V->getValueID() == NamedMDNodeVal;
48 //===----------------------------------------------------------------------===//
49 /// MDString - a single uniqued string.
50 /// These are used to efficiently contain a byte sequence for metadata.
51 /// MDString is always unnamd.
52 class MDString : public MetadataBase {
53 MDString(const MDString &); // DO NOT IMPLEMENT
57 explicit MDString(LLVMContext &C, StringRef S);
60 static MDString *get(LLVMContext &Context, StringRef Str);
61 static MDString *get(LLVMContext &Context, const char *Str);
63 StringRef getString() const { return Str; }
65 unsigned getLength() const { return (unsigned)Str.size(); }
67 typedef StringRef::iterator iterator;
69 /// begin() - Pointer to the first byte of the string.
71 iterator begin() const { return Str.begin(); }
73 /// end() - Pointer to one byte past the end of the string.
75 iterator end() const { return Str.end(); }
77 /// Methods for support type inquiry through isa, cast, and dyn_cast:
78 static inline bool classof(const MDString *) { return true; }
79 static bool classof(const Value *V) {
80 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
93 void operator=(const MDNode &); // DO NOT IMPLEMENT
94 friend class MDNodeElement;
96 MDNodeElement *Operands;
99 // Subclass data enums.
104 // Replace each instance of F from the element list of this node with T.
105 void replaceElement(MDNodeElement *Op, Value *NewVal);
108 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
109 bool isFunctionLocal);
111 // Constructors and destructors.
112 static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
113 bool isFunctionLocal = false);
115 /// ~MDNode - Destroy MDNode.
118 /// getElement - Return specified element.
119 Value *getElement(unsigned i) const;
121 /// getNumElements - Return number of MDNode elements.
122 unsigned getNumElements() const { return NumOperands; }
124 /// isFunctionLocal - Return whether MDNode is local to a function.
125 /// Note: MDNodes are designated as function-local when created, and keep
126 /// that designation even if their operands are modified to no longer
127 /// refer to function-local IR.
128 bool isFunctionLocal() const {
129 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
132 /// Profile - calculate a unique identifier for this MDNode to collapse
134 void Profile(FoldingSetNodeID &ID) const;
136 /// Methods for support type inquiry through isa, cast, and dyn_cast:
137 static inline bool classof(const MDNode *) { return true; }
138 static bool classof(const Value *V) {
139 return V->getValueID() == MDNodeVal;
142 // Shadow Value::setValueSubclassData with a private forwarding method so that
143 // any future subclasses cannot accidentally use it.
144 void setValueSubclassData(unsigned short D) {
145 Value::setValueSubclassData(D);
149 //===----------------------------------------------------------------------===//
150 /// NamedMDNode - a tuple of other metadata.
151 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
152 template<typename ValueSubClass, typename ItemParentClass>
153 class SymbolTableListTraits;
155 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
156 friend class SymbolTableListTraits<NamedMDNode, Module>;
157 friend class LLVMContextImpl;
159 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
162 void *Operands; // SmallVector<TrackingVH<MetadataBase>, 4>
164 void setParent(Module *M) { Parent = M; }
166 explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals,
167 unsigned NumVals, Module *M = 0);
169 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
170 MetadataBase *const *MDs,
171 unsigned NumMDs, Module *M = 0) {
172 return new NamedMDNode(C, N, MDs, NumMDs, M);
175 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
177 /// eraseFromParent - Drop all references and remove the node from parent
179 void eraseFromParent();
181 /// dropAllReferences - Remove all uses and clear node vector.
182 void dropAllReferences();
184 /// ~NamedMDNode - Destroy NamedMDNode.
187 /// getParent - Get the module that holds this named metadata collection.
188 inline Module *getParent() { return Parent; }
189 inline const Module *getParent() const { return Parent; }
191 /// getElement - Return specified element.
192 MetadataBase *getElement(unsigned i) const;
194 /// getNumElements - Return number of NamedMDNode elements.
195 unsigned getNumElements() const;
197 /// addElement - Add metadata element.
198 void addElement(MetadataBase *M);
200 /// Methods for support type inquiry through isa, cast, and dyn_cast:
201 static inline bool classof(const NamedMDNode *) { return true; }
202 static bool classof(const Value *V) {
203 return V->getValueID() == NamedMDNodeVal;
207 //===----------------------------------------------------------------------===//
208 /// MetadataContext - MetadataContext handles uniquing and assignment of IDs for
209 /// custom metadata types.
211 class MetadataContext {
212 MetadataContext(MetadataContext&); // DO NOT IMPLEMENT
213 void operator=(MetadataContext&); // DO NOT IMPLEMENT
215 MetadataContextImpl *const pImpl;
216 friend class Instruction;
221 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
222 unsigned getMDKindID(StringRef Name) const;
224 /// isValidName - Return true if Name is a valid custom metadata handler name.
225 static bool isValidName(StringRef Name);
227 /// copyMD - If metadata is attached with Instruction In1 then attach
228 /// the same metadata to In2.
229 void copyMD(Instruction *In1, Instruction *In2);
231 /// getMDKindNames - Populate client supplied SmallVector with the name for
232 /// each custom metadata ID. ID #0 is not used, so it is filled in as empty.
233 void getMDKindNames(SmallVectorImpl<StringRef> &) const;
235 /// ValueIsDeleted - This handler is used to update metadata store
236 /// when a value is deleted.
237 void ValueIsDeleted(Instruction *Inst);
239 /// ValueIsCloned - This handler is used to update metadata store
240 /// when In1 is cloned to create In2.
241 void ValueIsCloned(const Instruction *In1, Instruction *In2);
244 } // end llvm namespace