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 /// MDString - a single uniqued string.
35 /// These are used to efficiently contain a byte sequence for metadata.
36 /// MDString is always unnamd.
37 class MDString : public Value {
38 MDString(const MDString &); // DO NOT IMPLEMENT
42 explicit MDString(LLVMContext &C, StringRef S);
45 static MDString *get(LLVMContext &Context, StringRef Str);
46 static MDString *get(LLVMContext &Context, const char *Str);
48 StringRef getString() const { return Str; }
50 unsigned getLength() const { return (unsigned)Str.size(); }
52 typedef StringRef::iterator iterator;
54 /// begin() - Pointer to the first byte of the string.
56 iterator begin() const { return Str.begin(); }
58 /// end() - Pointer to one byte past the end of the string.
60 iterator end() const { return Str.end(); }
62 /// Methods for support type inquiry through isa, cast, and dyn_cast:
63 static inline bool classof(const MDString *) { return true; }
64 static bool classof(const Value *V) {
65 return V->getValueID() == MDStringVal;
72 //===----------------------------------------------------------------------===//
73 /// MDNode - a tuple of other values.
74 class MDNode : public Value, public FoldingSetNode {
75 MDNode(const MDNode &); // DO NOT IMPLEMENT
76 void operator=(const MDNode &); // DO NOT IMPLEMENT
77 friend class MDNodeOperand;
78 friend class LLVMContextImpl;
80 /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
81 /// end of this MDNode.
84 // Subclass data enums.
86 /// FunctionLocalBit - This bit is set if this MDNode is function local.
87 /// This is true when it (potentially transitively) contains a reference to
88 /// something in a function, like an argument, basicblock, or instruction.
89 FunctionLocalBit = 1 << 0,
91 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
92 /// have a null perand.
93 NotUniquedBit = 1 << 1,
95 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
96 /// that the node isn't being destroyed with a plain 'delete'.
100 // FunctionLocal enums.
101 enum FunctionLocalness {
107 /// replaceOperand - Replace each instance of F from the operand list of this
109 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
111 /// replaceAllOperandsWithNull - This is used while destroying llvm context to
112 /// gracefully delete all nodes. This method replaces all operands with null.
113 void replaceAllOperandsWithNull();
116 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
117 bool isFunctionLocal);
119 static MDNode *getMDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
120 FunctionLocalness FL, bool Insert = true);
122 // Constructors and destructors.
123 static MDNode *get(LLVMContext &Context, Value *const *Vals,
125 // getWhenValsUnresolved - Construct MDNode determining function-localness
126 // from isFunctionLocal argument, not by analyzing Vals.
127 static MDNode *getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
128 unsigned NumVals, bool isFunctionLocal);
130 static MDNode *getIfExists(LLVMContext &Context, Value *const *Vals,
133 /// getOperand - Return specified operand.
134 Value *getOperand(unsigned i) const;
136 /// getNumOperands - Return number of MDNode operands.
137 unsigned getNumOperands() const { return NumOperands; }
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 {
144 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
147 // getFunction - If this metadata is function-local and recursively has a
148 // function-local operand, return the first such operand's parent function.
149 // Otherwise, return null. getFunction() should not be used for performance-
150 // critical code because it recursively visits all the MDNode's operands.
151 const Function *getFunction() const;
153 // destroy - Delete this node. Only when there are no uses.
156 /// Profile - calculate a unique identifier for this MDNode to collapse
158 void Profile(FoldingSetNodeID &ID) const;
160 /// Methods for support type inquiry through isa, cast, and dyn_cast:
161 static inline bool classof(const MDNode *) { return true; }
162 static bool classof(const Value *V) {
163 return V->getValueID() == MDNodeVal;
166 bool isNotUniqued() const {
167 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
169 void setIsNotUniqued() {
170 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
173 // Shadow Value::setValueSubclassData with a private forwarding method so that
174 // any future subclasses cannot accidentally use it.
175 void setValueSubclassData(unsigned short D) {
176 Value::setValueSubclassData(D);
180 //===----------------------------------------------------------------------===//
181 /// NamedMDNode - a tuple of MDNodes.
182 /// NamedMDNode is always named. All NamedMDNode operand has a type of metadata.
183 class NamedMDNode : public Value, public ilist_node<NamedMDNode> {
184 friend class SymbolTableListTraits<NamedMDNode, Module>;
185 friend struct ilist_traits<NamedMDNode>;
186 friend class LLVMContextImpl;
187 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
191 void *Operands; // SmallVector<WeakVH<MDNode>, 4>
193 void setParent(Module *M) { Parent = M; }
195 explicit NamedMDNode(LLVMContext &C, const Twine &N, MDNode*const *Vals,
196 unsigned NumVals, Module *M = 0);
198 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
200 unsigned NumMDs, Module *M = 0) {
201 return new NamedMDNode(C, N, MDs, NumMDs, M);
204 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
206 /// eraseFromParent - Drop all references and remove the node from parent
208 void eraseFromParent();
210 /// dropAllReferences - Remove all uses and clear node vector.
211 void dropAllReferences();
213 /// ~NamedMDNode - Destroy NamedMDNode.
216 /// getParent - Get the module that holds this named metadata collection.
217 inline Module *getParent() { return Parent; }
218 inline const Module *getParent() const { return Parent; }
220 /// getOperand - Return specified operand.
221 MDNode *getOperand(unsigned i) const;
223 /// getNumOperands - Return the number of NamedMDNode operands.
224 unsigned getNumOperands() const;
226 /// addOperand - Add metadata operand.
227 void addOperand(MDNode *M);
229 /// setName - Set the name of this named metadata.
230 void setName(const Twine &NewName);
232 /// getName - Return a constant reference to this named metadata's name.
233 StringRef getName() const;
235 /// Methods for support type inquiry through isa, cast, and dyn_cast:
236 static inline bool classof(const NamedMDNode *) { return true; }
237 static bool classof(const Value *V) {
238 return V->getValueID() == NamedMDNodeVal;
242 } // end llvm namespace