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, 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);
62 static MDString *get(LLVMContext &Context, StringRef Str);
63 static MDString *get(LLVMContext &Context, const char *Str);
65 StringRef getString() const { return Str; }
67 unsigned getLength() const { return (unsigned)Str.size(); }
69 typedef StringRef::iterator iterator;
71 /// begin() - Pointer to the first byte of the string.
73 iterator begin() const { return Str.begin(); }
75 /// end() - Pointer to one byte past the end of the string.
77 iterator end() const { return Str.end(); }
79 /// Methods for support type inquiry through isa, cast, and dyn_cast:
80 static inline bool classof(const MDString *) { return true; }
81 static bool classof(const Value *V) {
82 return V->getValueID() == MDStringVal;
89 //===----------------------------------------------------------------------===//
90 /// MDNode - a tuple of other values.
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 MDNodeOperand;
96 /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
97 /// end of this MDNode.
100 // Subclass data enums.
102 /// FunctionLocalBit - This bit is set if this MDNode is function local.
103 /// This is true when it (potentially transitively) contains a reference to
104 /// something in a function, like an argument, basicblock, or instruction.
105 FunctionLocalBit = 1 << 0,
107 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
108 /// have a null perand.
109 NotUniquedBit = 1 << 1,
111 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
112 /// that the node isn't being destroyed with a plain 'delete'.
116 // Replace each instance of F from the operand list of this node with T.
117 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
121 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
122 bool isFunctionLocal);
124 // Constructors and destructors.
125 static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
126 bool isFunctionLocal = false);
128 /// getOperand - Return specified operand.
129 Value *getOperand(unsigned i) const;
131 /// getNumOperands - Return number of MDNode operands.
132 unsigned getNumOperands() const { return NumOperands; }
134 /// isFunctionLocal - Return whether MDNode is local to a function.
135 /// Note: MDNodes are designated as function-local when created, and keep
136 /// that designation even if their operands are modified to no longer
137 /// refer to function-local IR.
138 bool isFunctionLocal() const {
139 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
142 // destroy - Delete this node. Only when there are no uses.
145 /// Profile - calculate a unique identifier for this MDNode to collapse
147 void Profile(FoldingSetNodeID &ID) const;
149 /// Methods for support type inquiry through isa, cast, and dyn_cast:
150 static inline bool classof(const MDNode *) { return true; }
151 static bool classof(const Value *V) {
152 return V->getValueID() == MDNodeVal;
155 bool isNotUniqued() const {
156 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
158 void setIsNotUniqued() {
159 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
162 // Shadow Value::setValueSubclassData with a private forwarding method so that
163 // any future subclasses cannot accidentally use it.
164 void setValueSubclassData(unsigned short D) {
165 Value::setValueSubclassData(D);
169 //===----------------------------------------------------------------------===//
170 /// NamedMDNode - a tuple of MDNodes.
171 /// NamedMDNode is always named. All NamedMDNode operand has a type of metadata.
172 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
173 friend class SymbolTableListTraits<NamedMDNode, Module>;
174 friend class LLVMContextImpl;
176 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
180 void *Operands; // SmallVector<WeakVH<MDNode>, 4>
182 void setParent(Module *M) { Parent = M; }
184 explicit NamedMDNode(LLVMContext &C, StringRef N, MDNode*const *Vals,
185 unsigned NumVals, Module *M = 0);
187 static NamedMDNode *Create(LLVMContext &C, StringRef N,
189 unsigned NumMDs, Module *M = 0) {
190 return new NamedMDNode(C, N, MDs, NumMDs, M);
193 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
195 /// eraseFromParent - Drop all references and remove the node from parent
197 void eraseFromParent();
199 /// dropAllReferences - Remove all uses and clear node vector.
200 void dropAllReferences();
202 /// ~NamedMDNode - Destroy NamedMDNode.
205 /// getParent - Get the module that holds this named metadata collection.
206 inline Module *getParent() { return Parent; }
207 inline const Module *getParent() const { return Parent; }
209 /// getOperand - Return specified operand.
210 MDNode *getOperand(unsigned i) const;
212 /// getNumOperands - Return the number of NamedMDNode operands.
213 unsigned getNumOperands() const;
215 /// addOperand - Add metadata operand.
216 void addOperand(MDNode *M);
218 /// setName - Set the name of this named metadata.
219 void setName(StringRef Name);
221 /// getName - Return a constant reference to this named metadata's name.
222 StringRef getName() const;
224 /// Methods for support type inquiry through isa, cast, and dyn_cast:
225 static inline bool classof(const NamedMDNode *) { return true; }
226 static bool classof(const Value *V) {
227 return V->getValueID() == NamedMDNodeVal;
231 } // end llvm namespace