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/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/ilist_node.h"
25 #include "llvm/Support/ValueHandle.h"
31 class MetadataContextImpl;
33 //===----------------------------------------------------------------------===//
34 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
35 class MetadataBase : public Value {
37 MetadataBase(const Type *Ty, unsigned scid)
42 /// Methods for support type inquiry through isa, cast, and dyn_cast:
43 static inline bool classof(const MetadataBase *) { return true; }
44 static bool classof(const Value *V) {
45 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
46 || V->getValueID() == NamedMDNodeVal;
50 //===----------------------------------------------------------------------===//
51 /// MDString - a single uniqued string.
52 /// These are used to efficiently contain a byte sequence for metadata.
53 /// MDString is always unnamd.
54 class MDString : public MetadataBase {
55 MDString(const MDString &); // DO NOT IMPLEMENT
59 explicit MDString(LLVMContext &C, StringRef S)
60 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
63 static MDString *get(LLVMContext &Context, StringRef Str);
64 static MDString *get(LLVMContext &Context, const char *Str);
66 StringRef getString() const { return Str; }
68 unsigned getLength() const { return (unsigned)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);
111 static const unsigned short FunctionLocalBit = 1;
113 // Replace each instance of F from the element list of this node with T.
114 void replaceElement(Value *F, Value *T);
120 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
121 bool isFunctionLocal);
123 // Constructors and destructors.
124 static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
125 bool isFunctionLocal = false);
127 /// ~MDNode - Destroy MDNode.
130 /// getElement - Return specified element.
131 Value *getElement(unsigned i) const {
132 assert(i < getNumElements() && "Invalid element number!");
136 /// getNumElements - Return number of MDNode elements.
137 unsigned getNumElements() const { return NodeSize; }
139 /// isFunctionLocal - Return whether MDNode is local to a function.
140 /// Note: MDNodes are designated as function-local when created, and keep
141 /// that designation even if their operands are modified to no longer
142 /// refer to function-local IR.
143 bool isFunctionLocal() const { return SubclassData & FunctionLocalBit; }
145 /// getLocalFunction - Return false if MDNode's recursive function-localness
146 /// is invalid (local to more than one function). Return true otherwise.
147 /// If MDNode has one function to which it is local, set LocalFunction to that
149 bool getLocalFunction(Function *LocalFunction,
150 SmallPtrSet<MDNode *, 32> *VisitedMDNodes = NULL);
152 /// Profile - calculate a unique identifier for this MDNode to collapse
154 void Profile(FoldingSetNodeID &ID) const;
156 /// Methods for support type inquiry through isa, cast, and dyn_cast:
157 static inline bool classof(const MDNode *) { return true; }
158 static bool classof(const Value *V) {
159 return V->getValueID() == MDNodeVal;
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<TrackingVH<MetadataBase>, 4> Node;
178 void setParent(Module *M) { Parent = M; }
180 explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals,
181 unsigned NumVals, Module *M = 0);
183 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
184 MetadataBase *const *MDs,
185 unsigned NumMDs, Module *M = 0) {
186 return new NamedMDNode(C, N, MDs, NumMDs, M);
189 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
191 /// eraseFromParent - Drop all references and remove the node from parent
193 void eraseFromParent();
195 /// dropAllReferences - Remove all uses and clear node vector.
196 void dropAllReferences();
198 /// ~NamedMDNode - Destroy NamedMDNode.
201 /// getParent - Get the module that holds this named metadata collection.
202 inline Module *getParent() { return Parent; }
203 inline const Module *getParent() const { return Parent; }
205 /// getElement - Return specified element.
206 MetadataBase *getElement(unsigned i) const {
207 assert(i < getNumElements() && "Invalid element number!");
211 /// getNumElements - Return number of NamedMDNode elements.
212 unsigned getNumElements() const {
213 return (unsigned)Node.size();
216 /// addElement - Add metadata element.
217 void addElement(MetadataBase *M) {
218 Node.push_back(TrackingVH<MetadataBase>(M));
221 typedef SmallVectorImpl<TrackingVH<MetadataBase> >::iterator elem_iterator;
222 typedef SmallVectorImpl<TrackingVH<MetadataBase> >::const_iterator
224 bool elem_empty() const { return Node.empty(); }
225 const_elem_iterator elem_begin() const { return Node.begin(); }
226 const_elem_iterator elem_end() const { return Node.end(); }
227 elem_iterator elem_begin() { return Node.begin(); }
228 elem_iterator elem_end() { return Node.end(); }
230 /// Methods for support type inquiry through isa, cast, and dyn_cast:
231 static inline bool classof(const NamedMDNode *) { return true; }
232 static bool classof(const Value *V) {
233 return V->getValueID() == NamedMDNodeVal;
237 //===----------------------------------------------------------------------===//
238 /// MetadataContext -
239 /// MetadataContext handles uniquing and assignment of IDs for custom metadata
240 /// types. Custom metadata handler names do not contain spaces. And the name
241 /// must start with an alphabet. The regular expression used to check name
242 /// is [a-zA-Z$._][a-zA-Z$._0-9]*
243 class MetadataContext {
245 MetadataContext(MetadataContext&);
246 void operator=(MetadataContext&);
248 MetadataContextImpl *const pImpl;
253 /// registerMDKind - Register a new metadata kind and return its ID.
254 /// A metadata kind can be registered only once.
255 unsigned registerMDKind(StringRef Name);
257 /// getMDKind - Return metadata kind. If the requested metadata kind
258 /// is not registered then return 0.
259 unsigned getMDKind(StringRef Name) const;
261 /// isValidName - Return true if Name is a valid custom metadata handler name.
262 static bool isValidName(StringRef Name);
264 /// getMD - Get the metadata of given kind attached to an Instruction.
265 /// If the metadata is not found then return 0.
266 MDNode *getMD(unsigned Kind, const Instruction *Inst);
268 /// getMDs - Get the metadata attached to an Instruction.
269 void getMDs(const Instruction *Inst,
270 SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const;
272 /// addMD - Attach the metadata of given kind to an Instruction.
273 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
275 /// removeMD - Remove metadata of given kind attached with an instuction.
276 void removeMD(unsigned Kind, Instruction *Inst);
278 /// removeAllMetadata - Remove all metadata attached with an instruction.
279 void removeAllMetadata(Instruction *Inst);
281 /// copyMD - If metadata is attached with Instruction In1 then attach
282 /// the same metadata to In2.
283 void copyMD(Instruction *In1, Instruction *In2);
285 /// getHandlerNames - Populate client supplied smallvector using custom
286 /// metadata name and ID.
287 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
289 /// ValueIsDeleted - This handler is used to update metadata store
290 /// when a value is deleted.
291 void ValueIsDeleted(const Value *) {}
292 void ValueIsDeleted(Instruction *Inst);
293 void ValueIsRAUWd(Value *V1, Value *V2);
295 /// ValueIsCloned - This handler is used to update metadata store
296 /// when In1 is cloned to create In2.
297 void ValueIsCloned(const Instruction *In1, Instruction *In2);
300 } // end llvm namespace