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/ArrayRef.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/ilist_node.h"
29 template <typename T> class SmallVectorImpl;
30 template<typename ValueSubClass, typename ItemParentClass>
31 class SymbolTableListTraits;
34 //===----------------------------------------------------------------------===//
35 /// MDString - a single uniqued string.
36 /// These are used to efficiently contain a byte sequence for metadata.
37 /// MDString is always unnamd.
38 class MDString : public Value {
39 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 operand.
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);
114 MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
115 bool isFunctionLocal);
117 static MDNode *getMDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
118 FunctionLocalness FL, bool Insert = true);
120 // Constructors and destructors.
121 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> V);
122 // FIXME: Eliminate this constructor form.
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 /// getTemporary - Return a temporary MDNode, for use in constructing
134 /// cyclic MDNode structures. A temporary MDNode is not uniqued,
135 /// may be RAUW'd, and must be manually deleted with deleteTemporary.
136 static MDNode *getTemporary(LLVMContext &Context, Value *const *Vals,
139 /// deleteTemporary - Deallocate a node created by getTemporary. The
140 /// node must not have any users.
141 static void deleteTemporary(MDNode *N);
143 /// getOperand - Return specified operand.
144 Value *getOperand(unsigned i) const;
146 /// getNumOperands - Return number of MDNode operands.
147 unsigned getNumOperands() const { return NumOperands; }
149 /// isFunctionLocal - Return whether MDNode is local to a function.
150 bool isFunctionLocal() const {
151 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
154 // getFunction - If this metadata is function-local and recursively has a
155 // function-local operand, return the first such operand's parent function.
156 // Otherwise, return null. getFunction() should not be used for performance-
157 // critical code because it recursively visits all the MDNode's operands.
158 const Function *getFunction() const;
160 /// Profile - calculate a unique identifier for this MDNode to collapse
162 void Profile(FoldingSetNodeID &ID) const;
164 /// Methods for support type inquiry through isa, cast, and dyn_cast:
165 static inline bool classof(const MDNode *) { return true; }
166 static bool classof(const Value *V) {
167 return V->getValueID() == MDNodeVal;
170 // destroy - Delete this node. Only when there are no uses.
173 bool isNotUniqued() const {
174 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
176 void setIsNotUniqued();
178 // Shadow Value::setValueSubclassData with a private forwarding method so that
179 // any future subclasses cannot accidentally use it.
180 void setValueSubclassData(unsigned short D) {
181 Value::setValueSubclassData(D);
185 //===----------------------------------------------------------------------===//
186 /// NamedMDNode - a tuple of MDNodes. Despite its name, a NamedMDNode isn't
187 /// itself an MDNode. NamedMDNodes belong to modules, have names, and contain
188 /// lists of MDNodes.
189 class NamedMDNode : public ilist_node<NamedMDNode> {
190 friend class SymbolTableListTraits<NamedMDNode, Module>;
191 friend struct ilist_traits<NamedMDNode>;
192 friend class LLVMContextImpl;
194 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
198 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
200 void setParent(Module *M) { Parent = M; }
202 explicit NamedMDNode(const Twine &N);
205 /// eraseFromParent - Drop all references and remove the node from parent
207 void eraseFromParent();
209 /// dropAllReferences - Remove all uses and clear node vector.
210 void dropAllReferences();
212 /// ~NamedMDNode - Destroy NamedMDNode.
215 /// getParent - Get the module that holds this named metadata collection.
216 inline Module *getParent() { return Parent; }
217 inline const Module *getParent() const { return Parent; }
219 /// getOperand - Return specified operand.
220 MDNode *getOperand(unsigned i) const;
222 /// getNumOperands - Return the number of NamedMDNode operands.
223 unsigned getNumOperands() const;
225 /// addOperand - Add metadata operand.
226 void addOperand(MDNode *M);
228 /// getName - Return a constant reference to this named metadata's name.
229 StringRef getName() const;
231 /// print - Implement operator<< on NamedMDNode.
232 void print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW = 0) const;
235 } // end llvm namespace