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/DenseMap.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/ilist_node.h"
23 #include "llvm/ADT/iterator_range.h"
24 #include "llvm/IR/Value.h"
29 template<typename ValueSubClass, typename ItemParentClass>
30 class SymbolTableListTraits;
33 enum LLVMConstants : uint32_t {
34 DEBUG_METADATA_VERSION = 1 // Current debug info version number.
37 //===----------------------------------------------------------------------===//
38 /// MDString - a single uniqued string.
39 /// These are used to efficiently contain a byte sequence for metadata.
40 /// MDString is always unnamed.
41 class MDString : public Value {
42 virtual void anchor();
43 MDString(const MDString &) LLVM_DELETED_FUNCTION;
45 explicit MDString(LLVMContext &C);
47 static MDString *get(LLVMContext &Context, StringRef Str);
48 static MDString *get(LLVMContext &Context, const char *Str) {
49 return get(Context, Str ? StringRef(Str) : StringRef());
52 StringRef getString() const { return getName(); }
54 unsigned getLength() const { return (unsigned)getName().size(); }
56 typedef StringRef::iterator iterator;
58 /// begin() - Pointer to the first byte of the string.
59 iterator begin() const { return getName().begin(); }
61 /// end() - Pointer to one byte past the end of the string.
62 iterator end() const { return getName().end(); }
64 /// Methods for support type inquiry through isa, cast, and dyn_cast:
65 static bool classof(const Value *V) {
66 return V->getValueID() == MDStringVal;
70 /// AAMDNodes - A collection of metadata nodes that might be associated with a
71 /// memory access used by the alias-analysis infrastructure.
73 AAMDNodes(MDNode *T = nullptr)
76 bool operator == (const AAMDNodes &A) const {
80 bool operator != (const AAMDNodes &A) const {
84 operator bool() const {
88 /// TBAA - The tag for type-based alias analysis.
92 bool equals(const AAMDNodes &A) const {
93 return TBAA == A.TBAA;
97 // Specialize DenseMapInfo for AAMDNodes.
99 struct DenseMapInfo<AAMDNodes> {
100 static inline AAMDNodes getEmptyKey() {
101 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey());
103 static inline AAMDNodes getTombstoneKey() {
104 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey());
106 static unsigned getHashValue(const AAMDNodes &Val) {
107 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA);
109 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
116 //===----------------------------------------------------------------------===//
117 /// MDNode - a tuple of other values.
118 class MDNode : public Value, public FoldingSetNode {
119 MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
120 void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
121 friend class MDNodeOperand;
122 friend class LLVMContextImpl;
123 friend struct FoldingSetTrait<MDNode>;
125 /// Hash - If the MDNode is uniqued cache the hash to speed up lookup.
128 /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
129 /// end of this MDNode.
130 unsigned NumOperands;
132 // Subclass data enums.
134 /// FunctionLocalBit - This bit is set if this MDNode is function local.
135 /// This is true when it (potentially transitively) contains a reference to
136 /// something in a function, like an argument, basicblock, or instruction.
137 FunctionLocalBit = 1 << 0,
139 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
140 /// have a null operand.
141 NotUniquedBit = 1 << 1,
143 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
144 /// that the node isn't being destroyed with a plain 'delete'.
148 // FunctionLocal enums.
149 enum FunctionLocalness {
155 /// replaceOperand - Replace each instance of F from the operand list of this
157 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
160 MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
162 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
163 FunctionLocalness FL, bool Insert = true);
165 // Constructors and destructors.
166 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
167 // getWhenValsUnresolved - Construct MDNode determining function-localness
168 // from isFunctionLocal argument, not by analyzing Vals.
169 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
170 ArrayRef<Value*> Vals,
171 bool isFunctionLocal);
173 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
175 /// getTemporary - Return a temporary MDNode, for use in constructing
176 /// cyclic MDNode structures. A temporary MDNode is not uniqued,
177 /// may be RAUW'd, and must be manually deleted with deleteTemporary.
178 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
180 /// deleteTemporary - Deallocate a node created by getTemporary. The
181 /// node must not have any users.
182 static void deleteTemporary(MDNode *N);
184 /// replaceOperandWith - Replace a specific operand.
185 void replaceOperandWith(unsigned i, Value *NewVal);
187 /// getOperand - Return specified operand.
188 Value *getOperand(unsigned i) const LLVM_READONLY;
190 /// getNumOperands - Return number of MDNode operands.
191 unsigned getNumOperands() const { return NumOperands; }
193 /// isFunctionLocal - Return whether MDNode is local to a function.
194 bool isFunctionLocal() const {
195 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
198 // getFunction - If this metadata is function-local and recursively has a
199 // function-local operand, return the first such operand's parent function.
200 // Otherwise, return null. getFunction() should not be used for performance-
201 // critical code because it recursively visits all the MDNode's operands.
202 const Function *getFunction() const;
204 /// Profile - calculate a unique identifier for this MDNode to collapse
206 void Profile(FoldingSetNodeID &ID) const;
208 /// Methods for support type inquiry through isa, cast, and dyn_cast:
209 static bool classof(const Value *V) {
210 return V->getValueID() == MDNodeVal;
213 /// Check whether MDNode is a vtable access.
214 bool isTBAAVtableAccess() const;
216 /// Methods for metadata merging.
217 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
218 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
219 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
220 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
222 // destroy - Delete this node. Only when there are no uses.
225 bool isNotUniqued() const {
226 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
228 void setIsNotUniqued();
230 // Shadow Value::setValueSubclassData with a private forwarding method so that
231 // any future subclasses cannot accidentally use it.
232 void setValueSubclassData(unsigned short D) {
233 Value::setValueSubclassData(D);
237 //===----------------------------------------------------------------------===//
238 /// NamedMDNode - a tuple of MDNodes. Despite its name, a NamedMDNode isn't
239 /// itself an MDNode. NamedMDNodes belong to modules, have names, and contain
240 /// lists of MDNodes.
241 class NamedMDNode : public ilist_node<NamedMDNode> {
242 friend class SymbolTableListTraits<NamedMDNode, Module>;
243 friend struct ilist_traits<NamedMDNode>;
244 friend class LLVMContextImpl;
246 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
250 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
252 void setParent(Module *M) { Parent = M; }
254 explicit NamedMDNode(const Twine &N);
256 template<class T1, class T2>
257 class op_iterator_impl :
258 public std::iterator<std::bidirectional_iterator_tag, T2> {
259 const NamedMDNode *Node;
261 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
263 friend class NamedMDNode;
266 op_iterator_impl() : Node(nullptr), Idx(0) { }
268 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
269 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
270 op_iterator_impl &operator++() {
274 op_iterator_impl operator++(int) {
275 op_iterator_impl tmp(*this);
279 op_iterator_impl &operator--() {
283 op_iterator_impl operator--(int) {
284 op_iterator_impl tmp(*this);
289 T1 operator*() const { return Node->getOperand(Idx); }
293 /// eraseFromParent - Drop all references and remove the node from parent
295 void eraseFromParent();
297 /// dropAllReferences - Remove all uses and clear node vector.
298 void dropAllReferences();
300 /// ~NamedMDNode - Destroy NamedMDNode.
303 /// getParent - Get the module that holds this named metadata collection.
304 inline Module *getParent() { return Parent; }
305 inline const Module *getParent() const { return Parent; }
307 /// getOperand - Return specified operand.
308 MDNode *getOperand(unsigned i) const;
310 /// getNumOperands - Return the number of NamedMDNode operands.
311 unsigned getNumOperands() const;
313 /// addOperand - Add metadata operand.
314 void addOperand(MDNode *M);
316 /// getName - Return a constant reference to this named metadata's name.
317 StringRef getName() const;
319 /// print - Implement operator<< on NamedMDNode.
320 void print(raw_ostream &ROS) const;
322 /// dump() - Allow printing of NamedMDNodes from the debugger.
325 // ---------------------------------------------------------------------------
326 // Operand Iterator interface...
328 typedef op_iterator_impl<MDNode*, MDNode> op_iterator;
329 op_iterator op_begin() { return op_iterator(this, 0); }
330 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
332 typedef op_iterator_impl<const MDNode*, MDNode> const_op_iterator;
333 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
334 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
336 inline iterator_range<op_iterator> operands() {
337 return iterator_range<op_iterator>(op_begin(), op_end());
339 inline iterator_range<const_op_iterator> operands() const {
340 return iterator_range<const_op_iterator>(op_begin(), op_end());
344 } // end llvm namespace