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_IR_METADATA_H
17 #define LLVM_IR_METADATA_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/ilist_node.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/IR/Value.h"
28 template<typename ValueSubClass, typename ItemParentClass>
29 class SymbolTableListTraits;
32 enum LLVMConstants : uint32_t {
33 DEBUG_METADATA_VERSION = 1 // Current debug info version number.
36 //===----------------------------------------------------------------------===//
37 /// MDString - a single uniqued string.
38 /// These are used to efficiently contain a byte sequence for metadata.
39 /// MDString is always unnamed.
40 class MDString : public Value {
41 virtual void anchor();
42 MDString(const MDString &) LLVM_DELETED_FUNCTION;
44 explicit MDString(LLVMContext &C);
46 static MDString *get(LLVMContext &Context, StringRef Str);
47 static MDString *get(LLVMContext &Context, const char *Str) {
48 return get(Context, Str ? StringRef(Str) : StringRef());
51 StringRef getString() const { return getName(); }
53 unsigned getLength() const { return (unsigned)getName().size(); }
55 typedef StringRef::iterator iterator;
57 /// begin() - Pointer to the first byte of the string.
58 iterator begin() const { return getName().begin(); }
60 /// end() - Pointer to one byte past the end of the string.
61 iterator end() const { return getName().end(); }
63 /// Methods for support type inquiry through isa, cast, and dyn_cast:
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 &) LLVM_DELETED_FUNCTION;
76 void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
77 friend class MDNodeOperand;
78 friend class LLVMContextImpl;
79 friend struct FoldingSetTrait<MDNode>;
81 /// Hash - If the MDNode is uniqued cache the hash to speed up lookup.
84 /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
85 /// end of this MDNode.
88 // Subclass data enums.
90 /// FunctionLocalBit - This bit is set if this MDNode is function local.
91 /// This is true when it (potentially transitively) contains a reference to
92 /// something in a function, like an argument, basicblock, or instruction.
93 FunctionLocalBit = 1 << 0,
95 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
96 /// have a null operand.
97 NotUniquedBit = 1 << 1,
99 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
100 /// that the node isn't being destroyed with a plain 'delete'.
104 // FunctionLocal enums.
105 enum FunctionLocalness {
111 /// replaceOperand - Replace each instance of F from the operand list of this
113 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
116 MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
118 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
119 FunctionLocalness FL, bool Insert = true);
121 // Constructors and destructors.
122 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
123 // getWhenValsUnresolved - Construct MDNode determining function-localness
124 // from isFunctionLocal argument, not by analyzing Vals.
125 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
126 ArrayRef<Value*> Vals,
127 bool isFunctionLocal);
129 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
131 /// getTemporary - Return a temporary MDNode, for use in constructing
132 /// cyclic MDNode structures. A temporary MDNode is not uniqued,
133 /// may be RAUW'd, and must be manually deleted with deleteTemporary.
134 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
136 /// deleteTemporary - Deallocate a node created by getTemporary. The
137 /// node must not have any users.
138 static void deleteTemporary(MDNode *N);
140 /// replaceOperandWith - Replace a specific operand.
141 void replaceOperandWith(unsigned i, Value *NewVal);
143 /// getOperand - Return specified operand.
144 Value *getOperand(unsigned i) const LLVM_READONLY;
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 bool classof(const Value *V) {
166 return V->getValueID() == MDNodeVal;
169 /// Check whether MDNode is a vtable access.
170 bool isTBAAVtableAccess() const;
172 /// Methods for metadata merging.
173 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
174 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
175 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
177 // destroy - Delete this node. Only when there are no uses.
180 bool isNotUniqued() const {
181 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
183 void setIsNotUniqued();
185 // Shadow Value::setValueSubclassData with a private forwarding method so that
186 // any future subclasses cannot accidentally use it.
187 void setValueSubclassData(unsigned short D) {
188 Value::setValueSubclassData(D);
192 //===----------------------------------------------------------------------===//
193 /// NamedMDNode - a tuple of MDNodes. Despite its name, a NamedMDNode isn't
194 /// itself an MDNode. NamedMDNodes belong to modules, have names, and contain
195 /// lists of MDNodes.
196 class NamedMDNode : public ilist_node<NamedMDNode> {
197 friend class SymbolTableListTraits<NamedMDNode, Module>;
198 friend struct ilist_traits<NamedMDNode>;
199 friend class LLVMContextImpl;
201 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
205 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
207 void setParent(Module *M) { Parent = M; }
209 explicit NamedMDNode(const Twine &N);
211 template<class T1, class T2>
212 class op_iterator_impl :
213 public std::iterator<std::bidirectional_iterator_tag, T2> {
214 const NamedMDNode *Node;
216 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
218 friend class NamedMDNode;
221 op_iterator_impl() : Node(nullptr), Idx(0) { }
223 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
224 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
225 op_iterator_impl &operator++() {
229 op_iterator_impl operator++(int) {
230 op_iterator_impl tmp(*this);
234 op_iterator_impl &operator--() {
238 op_iterator_impl operator--(int) {
239 op_iterator_impl tmp(*this);
244 T1 operator*() const { return Node->getOperand(Idx); }
248 /// eraseFromParent - Drop all references and remove the node from parent
250 void eraseFromParent();
252 /// dropAllReferences - Remove all uses and clear node vector.
253 void dropAllReferences();
255 /// ~NamedMDNode - Destroy NamedMDNode.
258 /// getParent - Get the module that holds this named metadata collection.
259 inline Module *getParent() { return Parent; }
260 inline const Module *getParent() const { return Parent; }
262 /// getOperand - Return specified operand.
263 MDNode *getOperand(unsigned i) const;
265 /// getNumOperands - Return the number of NamedMDNode operands.
266 unsigned getNumOperands() const;
268 /// addOperand - Add metadata operand.
269 void addOperand(MDNode *M);
271 /// getName - Return a constant reference to this named metadata's name.
272 StringRef getName() const;
274 /// print - Implement operator<< on NamedMDNode.
275 void print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW = nullptr) const;
277 /// dump() - Allow printing of NamedMDNodes from the debugger.
280 // ---------------------------------------------------------------------------
281 // Operand Iterator interface...
283 typedef op_iterator_impl<MDNode*, MDNode> op_iterator;
284 op_iterator op_begin() { return op_iterator(this, 0); }
285 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
287 typedef op_iterator_impl<const MDNode*, MDNode> const_op_iterator;
288 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
289 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
291 inline iterator_range<op_iterator> operands() {
292 return iterator_range<op_iterator>(op_begin(), op_end());
294 inline iterator_range<const_op_iterator> operands() const {
295 return iterator_range<const_op_iterator>(op_begin(), op_end());
299 } // end llvm namespace