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 template <typename T> class SmallVectorImpl;
30 //===----------------------------------------------------------------------===//
31 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
32 class MetadataBase : public Value {
34 MetadataBase(const Type *Ty, unsigned scid)
39 /// Methods for support type inquiry through isa, cast, and dyn_cast:
40 static inline bool classof(const MetadataBase *) { return true; }
41 static bool classof(const Value *V) {
42 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
43 || V->getValueID() == NamedMDNodeVal;
47 //===----------------------------------------------------------------------===//
48 /// MDString - a single uniqued string.
49 /// These are used to efficiently contain a byte sequence for metadata.
50 /// MDString is always unnamd.
51 class MDString : public MetadataBase {
52 MDString(const MDString &); // DO NOT IMPLEMENT
56 explicit MDString(LLVMContext &C, StringRef S);
59 static MDString *get(LLVMContext &Context, StringRef Str);
60 static MDString *get(LLVMContext &Context, const char *Str);
62 StringRef getString() const { return Str; }
64 unsigned getLength() const { return (unsigned)Str.size(); }
66 typedef StringRef::iterator iterator;
68 /// begin() - Pointer to the first byte of the string.
70 iterator begin() const { return Str.begin(); }
72 /// end() - Pointer to one byte past the end of the string.
74 iterator end() const { return Str.end(); }
76 /// Methods for support type inquiry through isa, cast, and dyn_cast:
77 static inline bool classof(const MDString *) { return true; }
78 static bool classof(const Value *V) {
79 return V->getValueID() == MDStringVal;
86 //===----------------------------------------------------------------------===//
87 /// MDNode - a tuple of other values.
88 class MDNode : public MetadataBase, public FoldingSetNode {
89 MDNode(const MDNode &); // DO NOT IMPLEMENT
90 void operator=(const MDNode &); // DO NOT IMPLEMENT
91 friend class MDNodeElement;
93 MDNodeElement *Operands;
96 // Subclass data enums.
98 /// FunctionLocalBit - This bit is set if this MDNode is function local.
99 /// This is true when it (potentially transitively) contains a reference to
100 /// something in a function, like an argument, basicblock, or instruction.
101 FunctionLocalBit = 1 << 0,
103 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
104 /// have a null perand.
105 NotUniquedBit = 1 << 1
108 // Replace each instance of F from the element list of this node with T.
109 void replaceElement(MDNodeElement *Op, Value *NewVal);
112 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
113 bool isFunctionLocal);
115 // Constructors and destructors.
116 static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
117 bool isFunctionLocal = false);
119 /// ~MDNode - Destroy MDNode.
122 /// getElement - Return specified element.
123 Value *getElement(unsigned i) const;
125 /// getNumElements - Return number of MDNode elements.
126 unsigned getNumElements() const { return NumOperands; }
128 /// isFunctionLocal - Return whether MDNode is local to a function.
129 /// Note: MDNodes are designated as function-local when created, and keep
130 /// that designation even if their operands are modified to no longer
131 /// refer to function-local IR.
132 bool isFunctionLocal() const {
133 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
136 /// Profile - calculate a unique identifier for this MDNode to collapse
138 void Profile(FoldingSetNodeID &ID) const;
140 /// Methods for support type inquiry through isa, cast, and dyn_cast:
141 static inline bool classof(const MDNode *) { return true; }
142 static bool classof(const Value *V) {
143 return V->getValueID() == MDNodeVal;
146 bool isNotUniqued() const {
147 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
149 void setIsNotUniqued() {
150 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
153 // Shadow Value::setValueSubclassData with a private forwarding method so that
154 // any future subclasses cannot accidentally use it.
155 void setValueSubclassData(unsigned short D) {
156 Value::setValueSubclassData(D);
160 //===----------------------------------------------------------------------===//
161 /// NamedMDNode - a tuple of other metadata.
162 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
163 template<typename ValueSubClass, typename ItemParentClass>
164 class SymbolTableListTraits;
166 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
167 friend class SymbolTableListTraits<NamedMDNode, Module>;
168 friend class LLVMContextImpl;
170 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
173 void *Operands; // SmallVector<TrackingVH<MetadataBase>, 4>
175 void setParent(Module *M) { Parent = M; }
177 explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals,
178 unsigned NumVals, Module *M = 0);
180 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
181 MetadataBase *const *MDs,
182 unsigned NumMDs, Module *M = 0) {
183 return new NamedMDNode(C, N, MDs, NumMDs, M);
186 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
188 /// eraseFromParent - Drop all references and remove the node from parent
190 void eraseFromParent();
192 /// dropAllReferences - Remove all uses and clear node vector.
193 void dropAllReferences();
195 /// ~NamedMDNode - Destroy NamedMDNode.
198 /// getParent - Get the module that holds this named metadata collection.
199 inline Module *getParent() { return Parent; }
200 inline const Module *getParent() const { return Parent; }
202 /// getElement - Return specified element.
203 MetadataBase *getElement(unsigned i) const;
205 /// getNumElements - Return number of NamedMDNode elements.
206 unsigned getNumElements() const;
208 /// addElement - Add metadata element.
209 void addElement(MetadataBase *M);
211 /// Methods for support type inquiry through isa, cast, and dyn_cast:
212 static inline bool classof(const NamedMDNode *) { return true; }
213 static bool classof(const Value *V) {
214 return V->getValueID() == NamedMDNodeVal;
218 } // end llvm namespace