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 = 2 // Current debug info version number.
37 //===----------------------------------------------------------------------===//
38 /// \brief A single uniqued string.
40 /// These are used to efficiently contain a byte sequence for metadata.
41 /// MDString is always unnamed.
42 class MDString : public Value {
43 virtual void anchor();
44 MDString(const MDString &) LLVM_DELETED_FUNCTION;
46 explicit MDString(LLVMContext &C);
48 static MDString *get(LLVMContext &Context, StringRef Str);
49 static MDString *get(LLVMContext &Context, const char *Str) {
50 return get(Context, Str ? StringRef(Str) : StringRef());
53 StringRef getString() const { return getName(); }
55 unsigned getLength() const { return (unsigned)getName().size(); }
57 typedef StringRef::iterator iterator;
59 /// \brief Pointer to the first byte of the string.
60 iterator begin() const { return getName().begin(); }
62 /// \brief Pointer to one byte past the end of the string.
63 iterator end() const { return getName().end(); }
65 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
66 static bool classof(const Value *V) {
67 return V->getValueID() == MDStringVal;
71 /// \brief A collection of metadata nodes that might be associated with a
72 /// memory access used by the alias-analysis infrastructure.
74 explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
76 : TBAA(T), Scope(S), NoAlias(N) {}
78 bool operator==(const AAMDNodes &A) const {
79 return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
82 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
84 LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
86 /// \brief The tag for type-based alias analysis.
89 /// \brief The tag for alias scope specification (used with noalias).
92 /// \brief The tag specifying the noalias scope.
96 // Specialize DenseMapInfo for AAMDNodes.
98 struct DenseMapInfo<AAMDNodes> {
99 static inline AAMDNodes getEmptyKey() {
100 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
102 static inline AAMDNodes getTombstoneKey() {
103 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
105 static unsigned getHashValue(const AAMDNodes &Val) {
106 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
107 DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
108 DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
110 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
117 //===----------------------------------------------------------------------===//
118 /// \brief A tuple of other values.
119 class MDNode : public Value, public FoldingSetNode {
120 MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
121 void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
122 friend class MDNodeOperand;
123 friend class LLVMContextImpl;
124 friend struct FoldingSetTrait<MDNode>;
126 /// \brief If the MDNode is uniqued cache the hash to speed up lookup.
129 /// \brief Subclass data enums.
131 /// FunctionLocalBit - This bit is set if this MDNode is function local.
132 /// This is true when it (potentially transitively) contains a reference to
133 /// something in a function, like an argument, basicblock, or instruction.
134 FunctionLocalBit = 1 << 0,
136 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
137 /// have a null operand.
138 NotUniquedBit = 1 << 1,
140 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
141 /// that the node isn't being destroyed with a plain 'delete'.
145 /// \brief FunctionLocal enums.
146 enum FunctionLocalness {
152 /// \brief Replace each instance of the given operand with a new value.
153 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
156 MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
158 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
159 FunctionLocalness FL, bool Insert = true);
161 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
162 /// \brief Construct MDNode with an explicit function-localness.
164 /// Don't analyze Vals; trust isFunctionLocal.
165 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
166 ArrayRef<Value*> Vals,
167 bool isFunctionLocal);
169 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
171 /// \brief Return a temporary MDNode
173 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
174 /// not uniqued, may be RAUW'd, and must be manually deleted with
176 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
178 /// \brief Deallocate a node created by getTemporary.
180 /// The node must not have any users.
181 static void deleteTemporary(MDNode *N);
183 /// \brief Replace a specific operand.
184 void replaceOperandWith(unsigned i, Value *NewVal);
186 /// \brief Return specified operand.
187 Value *getOperand(unsigned i) const LLVM_READONLY;
189 /// \brief Return number of MDNode operands.
190 unsigned getNumOperands() const { return NumOperands; }
192 /// \brief Return whether MDNode is local to a function.
193 bool isFunctionLocal() const {
194 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
197 /// \brief Return the first function-local operand's function.
199 /// If this metadata is function-local and recursively has a function-local
200 /// operand, return the first such operand's parent function. Otherwise,
201 /// return null. getFunction() should not be used for performance- critical
202 /// code because it recursively visits all the MDNode's operands.
203 const Function *getFunction() const;
205 /// \brief Calculate a unique identifier for this MDNode.
206 void Profile(FoldingSetNodeID &ID) const;
208 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
209 static bool classof(const Value *V) {
210 return V->getValueID() == MDNodeVal;
213 /// \brief Check whether MDNode is a vtable access.
214 bool isTBAAVtableAccess() const;
216 /// \brief Methods for metadata merging.
217 static MDNode *concatenate(MDNode *A, MDNode *B);
218 static MDNode *intersect(MDNode *A, MDNode *B);
219 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
220 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
221 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
222 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
224 /// \brief Delete this node. Only when there are no uses.
227 bool isNotUniqued() const {
228 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
230 void setIsNotUniqued();
232 // Shadow Value::setValueSubclassData with a private forwarding method so that
233 // any future subclasses cannot accidentally use it.
234 void setValueSubclassData(unsigned short D) {
235 Value::setValueSubclassData(D);
239 //===----------------------------------------------------------------------===//
240 /// \brief A tuple of MDNodes.
242 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
243 /// to modules, have names, and contain lists of MDNodes.
244 class NamedMDNode : public ilist_node<NamedMDNode> {
245 friend class SymbolTableListTraits<NamedMDNode, Module>;
246 friend struct ilist_traits<NamedMDNode>;
247 friend class LLVMContextImpl;
249 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
253 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
255 void setParent(Module *M) { Parent = M; }
257 explicit NamedMDNode(const Twine &N);
259 template<class T1, class T2>
260 class op_iterator_impl :
261 public std::iterator<std::bidirectional_iterator_tag, T2> {
262 const NamedMDNode *Node;
264 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
266 friend class NamedMDNode;
269 op_iterator_impl() : Node(nullptr), Idx(0) { }
271 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
272 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
273 op_iterator_impl &operator++() {
277 op_iterator_impl operator++(int) {
278 op_iterator_impl tmp(*this);
282 op_iterator_impl &operator--() {
286 op_iterator_impl operator--(int) {
287 op_iterator_impl tmp(*this);
292 T1 operator*() const { return Node->getOperand(Idx); }
296 /// \brief Drop all references and remove the node from parent module.
297 void eraseFromParent();
299 /// \brief Remove all uses and clear node vector.
300 void dropAllReferences();
304 /// \brief Get the module that holds this named metadata collection.
305 inline Module *getParent() { return Parent; }
306 inline const Module *getParent() const { return Parent; }
308 MDNode *getOperand(unsigned i) const;
309 unsigned getNumOperands() const;
310 void addOperand(MDNode *M);
311 StringRef getName() const;
312 void print(raw_ostream &ROS) const;
315 // ---------------------------------------------------------------------------
316 // Operand Iterator interface...
318 typedef op_iterator_impl<MDNode*, MDNode> op_iterator;
319 op_iterator op_begin() { return op_iterator(this, 0); }
320 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
322 typedef op_iterator_impl<const MDNode*, MDNode> const_op_iterator;
323 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
324 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
326 inline iterator_range<op_iterator> operands() {
327 return iterator_range<op_iterator>(op_begin(), op_end());
329 inline iterator_range<const_op_iterator> operands() const {
330 return iterator_range<const_op_iterator>(op_begin(), op_end());
334 } // end llvm namespace