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) {
47 return get(Context, Str ? StringRef(Str) : StringRef());
50 StringRef getString() const { return Str; }
52 unsigned getLength() const { return (unsigned)Str.size(); }
54 typedef StringRef::iterator iterator;
56 /// begin() - Pointer to the first byte of the string.
58 iterator begin() const { return Str.begin(); }
60 /// end() - Pointer to one byte past the end of the string.
62 iterator end() const { return Str.end(); }
64 /// Methods for support type inquiry through isa, cast, and dyn_cast:
65 static inline bool classof(const MDString *) { return true; }
66 static bool classof(const Value *V) {
67 return V->getValueID() == MDStringVal;
74 //===----------------------------------------------------------------------===//
75 /// MDNode - a tuple of other values.
76 class MDNode : public Value, public FoldingSetNode {
77 MDNode(const MDNode &); // DO NOT IMPLEMENT
78 void operator=(const MDNode &); // DO NOT IMPLEMENT
79 friend class MDNodeOperand;
80 friend class LLVMContextImpl;
82 /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
83 /// end of this MDNode.
86 // Subclass data enums.
88 /// FunctionLocalBit - This bit is set if this MDNode is function local.
89 /// This is true when it (potentially transitively) contains a reference to
90 /// something in a function, like an argument, basicblock, or instruction.
91 FunctionLocalBit = 1 << 0,
93 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
94 /// have a null perand.
95 NotUniquedBit = 1 << 1,
97 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
98 /// that the node isn't being destroyed with a plain 'delete'.
102 // FunctionLocal enums.
103 enum FunctionLocalness {
109 /// replaceOperand - Replace each instance of F from the operand list of this
111 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
113 /// replaceAllOperandsWithNull - This is used while destroying llvm context to
114 /// gracefully delete all nodes. This method replaces all operands with null.
115 void replaceAllOperandsWithNull();
118 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
119 bool isFunctionLocal);
121 static MDNode *getMDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
122 FunctionLocalness FL, bool Insert = true);
124 // Constructors and destructors.
125 static MDNode *get(LLVMContext &Context, Value *const *Vals,
127 // getWhenValsUnresolved - Construct MDNode determining function-localness
128 // from isFunctionLocal argument, not by analyzing Vals.
129 static MDNode *getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
130 unsigned NumVals, bool isFunctionLocal);
132 static MDNode *getIfExists(LLVMContext &Context, Value *const *Vals,
135 /// getOperand - Return specified operand.
136 Value *getOperand(unsigned i) const;
138 /// getNumOperands - Return number of MDNode operands.
139 unsigned getNumOperands() const { return NumOperands; }
141 /// isFunctionLocal - Return whether MDNode is local to a function.
142 /// Note: MDNodes are designated as function-local when created, and keep
143 /// that designation even if their operands are modified to no longer
144 /// refer to function-local IR.
145 bool isFunctionLocal() const {
146 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
149 // getFunction - If this metadata is function-local and recursively has a
150 // function-local operand, return the first such operand's parent function.
151 // Otherwise, return null. getFunction() should not be used for performance-
152 // critical code because it recursively visits all the MDNode's operands.
153 const Function *getFunction() const;
155 // destroy - Delete this node. Only when there are no uses.
158 /// Profile - calculate a unique identifier for this MDNode to collapse
160 void Profile(FoldingSetNodeID &ID) const;
162 /// Methods for support type inquiry through isa, cast, and dyn_cast:
163 static inline bool classof(const MDNode *) { return true; }
164 static bool classof(const Value *V) {
165 return V->getValueID() == MDNodeVal;
168 bool isNotUniqued() const {
169 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
171 void setIsNotUniqued() {
172 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
175 // Shadow Value::setValueSubclassData with a private forwarding method so that
176 // any future subclasses cannot accidentally use it.
177 void setValueSubclassData(unsigned short D) {
178 Value::setValueSubclassData(D);
182 //===----------------------------------------------------------------------===//
183 /// NamedMDNode - a tuple of MDNodes.
184 /// NamedMDNode is always named. All NamedMDNode operand has a type of metadata.
185 class NamedMDNode : public Value, public ilist_node<NamedMDNode> {
186 friend class SymbolTableListTraits<NamedMDNode, Module>;
187 friend struct ilist_traits<NamedMDNode>;
188 friend class LLVMContextImpl;
189 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
193 void *Operands; // SmallVector<WeakVH<MDNode>, 4>
195 void setParent(Module *M) { Parent = M; }
197 explicit NamedMDNode(LLVMContext &C, const Twine &N, MDNode*const *Vals,
198 unsigned NumVals, Module *M = 0);
200 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
202 unsigned NumMDs, Module *M = 0) {
203 return new NamedMDNode(C, N, MDs, NumMDs, M);
206 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
208 /// eraseFromParent - Drop all references and remove the node from parent
210 void eraseFromParent();
212 /// dropAllReferences - Remove all uses and clear node vector.
213 void dropAllReferences();
215 /// ~NamedMDNode - Destroy NamedMDNode.
218 /// getParent - Get the module that holds this named metadata collection.
219 inline Module *getParent() { return Parent; }
220 inline const Module *getParent() const { return Parent; }
222 /// getOperand - Return specified operand.
223 MDNode *getOperand(unsigned i) const;
225 /// getNumOperands - Return the number of NamedMDNode operands.
226 unsigned getNumOperands() const;
228 /// addOperand - Add metadata operand.
229 void addOperand(MDNode *M);
231 /// setName - Set the name of this named metadata.
232 void setName(const Twine &NewName);
234 /// getName - Return a constant reference to this named metadata's name.
235 StringRef getName() const;
237 /// Methods for support type inquiry through isa, cast, and dyn_cast:
238 static inline bool classof(const NamedMDNode *) { return true; }
239 static bool classof(const Value *V) {
240 return V->getValueID() == NamedMDNodeVal;
244 } // end llvm namespace