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;
29 template<typename ValueSubClass, typename ItemParentClass>
30 class SymbolTableListTraits;
33 //===----------------------------------------------------------------------===//
34 // MetadataBase - A base class for MDNode and MDString.
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;
49 //===----------------------------------------------------------------------===//
50 /// MDString - a single uniqued string.
51 /// These are used to efficiently contain a byte sequence for metadata.
52 /// MDString is always unnamd.
53 class MDString : public MetadataBase {
54 MDString(const MDString &); // DO NOT IMPLEMENT
58 explicit MDString(LLVMContext &C, StringRef S);
61 static MDString *get(LLVMContext &Context, StringRef Str);
62 static MDString *get(LLVMContext &Context, const char *Str);
64 StringRef getString() const { return Str; }
66 unsigned getLength() const { return (unsigned)Str.size(); }
68 typedef StringRef::iterator iterator;
70 /// begin() - Pointer to the first byte of the string.
72 iterator begin() const { return Str.begin(); }
74 /// end() - Pointer to one byte past the end of the string.
76 iterator end() const { return Str.end(); }
78 /// Methods for support type inquiry through isa, cast, and dyn_cast:
79 static inline bool classof(const MDString *) { return true; }
80 static bool classof(const Value *V) {
81 return V->getValueID() == MDStringVal;
88 //===----------------------------------------------------------------------===//
89 /// MDNode - a tuple of other values.
90 class MDNode : public MetadataBase, public FoldingSetNode {
91 MDNode(const MDNode &); // DO NOT IMPLEMENT
92 void operator=(const MDNode &); // DO NOT IMPLEMENT
93 friend class MDNodeOperand;
95 /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
96 /// end of this MDNode.
99 // Subclass data enums.
101 /// FunctionLocalBit - This bit is set if this MDNode is function local.
102 /// This is true when it (potentially transitively) contains a reference to
103 /// something in a function, like an argument, basicblock, or instruction.
104 FunctionLocalBit = 1 << 0,
106 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
107 /// have a null perand.
108 NotUniquedBit = 1 << 1,
110 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
111 /// that the node isn't being destroyed with a plain 'delete'.
115 // FunctionLocal enums.
116 enum FunctionLocalness {
122 // Replace each instance of F from the operand list of this node with T.
123 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
127 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
128 bool isFunctionLocal);
130 static MDNode *getMDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
131 FunctionLocalness FL);
133 // Constructors and destructors.
134 static MDNode *get(LLVMContext &Context, Value *const *Vals,
136 // getWhenValsUnresolved - Construct MDNode determining function-localness
137 // from isFunctionLocal argument, not by analyzing Vals.
138 static MDNode *getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
139 unsigned NumVals, bool isFunctionLocal);
141 /// getOperand - Return specified operand.
142 Value *getOperand(unsigned i) const;
144 /// getNumOperands - Return number of MDNode operands.
145 unsigned getNumOperands() const { return NumOperands; }
147 /// isFunctionLocal - Return whether MDNode is local to a function.
148 /// Note: MDNodes are designated as function-local when created, and keep
149 /// that designation even if their operands are modified to no longer
150 /// refer to function-local IR.
151 bool isFunctionLocal() const {
152 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
155 // getFunction - If this metadata is function-local and recursively has a
156 // function-local operand, return the first such operand's parent function.
157 // Otherwise, return null. getFunction() should not be used for performance-
158 // critical code because it recursively visits all the MDNode's operands.
159 const Function *getFunction() const;
161 // destroy - Delete this node. Only when there are no uses.
164 /// Profile - calculate a unique identifier for this MDNode to collapse
166 void Profile(FoldingSetNodeID &ID) const;
168 /// Methods for support type inquiry through isa, cast, and dyn_cast:
169 static inline bool classof(const MDNode *) { return true; }
170 static bool classof(const Value *V) {
171 return V->getValueID() == MDNodeVal;
174 bool isNotUniqued() const {
175 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
177 void setIsNotUniqued() {
178 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
181 // Shadow Value::setValueSubclassData with a private forwarding method so that
182 // any future subclasses cannot accidentally use it.
183 void setValueSubclassData(unsigned short D) {
184 Value::setValueSubclassData(D);
188 //===----------------------------------------------------------------------===//
189 /// NamedMDNode - a tuple of MDNodes.
190 /// NamedMDNode is always named. All NamedMDNode operand has a type of metadata.
191 class NamedMDNode : public Value, public ilist_node<NamedMDNode> {
192 friend class SymbolTableListTraits<NamedMDNode, Module>;
193 friend struct ilist_traits<NamedMDNode>;
194 friend class LLVMContextImpl;
195 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
199 void *Operands; // SmallVector<WeakVH<MDNode>, 4>
201 void setParent(Module *M) { Parent = M; }
203 explicit NamedMDNode(LLVMContext &C, const Twine &N, MDNode*const *Vals,
204 unsigned NumVals, Module *M = 0);
206 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
208 unsigned NumMDs, Module *M = 0) {
209 return new NamedMDNode(C, N, MDs, NumMDs, M);
212 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
214 /// eraseFromParent - Drop all references and remove the node from parent
216 void eraseFromParent();
218 /// dropAllReferences - Remove all uses and clear node vector.
219 void dropAllReferences();
221 /// ~NamedMDNode - Destroy NamedMDNode.
224 /// getParent - Get the module that holds this named metadata collection.
225 inline Module *getParent() { return Parent; }
226 inline const Module *getParent() const { return Parent; }
228 /// getOperand - Return specified operand.
229 MDNode *getOperand(unsigned i) const;
231 /// getNumOperands - Return the number of NamedMDNode operands.
232 unsigned getNumOperands() const;
234 /// addOperand - Add metadata operand.
235 void addOperand(MDNode *M);
237 /// setName - Set the name of this named metadata.
238 void setName(const Twine &NewName);
240 /// getName - Return a constant reference to this named metadata's name.
241 StringRef getName() const;
243 /// Methods for support type inquiry through isa, cast, and dyn_cast:
244 static inline bool classof(const NamedMDNode *) { return true; }
245 static bool classof(const Value *V) {
246 return V->getValueID() == NamedMDNodeVal;
250 } // end llvm namespace