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 Number of co-allocated 'MDNodeOperand' items.
130 unsigned NumOperands;
132 /// \brief 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 /// \brief FunctionLocal enums.
149 enum FunctionLocalness {
155 /// \brief Replace each instance of the given operand with a new value.
156 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
159 MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
161 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
162 FunctionLocalness FL, bool Insert = true);
164 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
165 /// \brief Construct MDNode with an explicit function-localness.
167 /// Don't analyze Vals; trust isFunctionLocal.
168 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
169 ArrayRef<Value*> Vals,
170 bool isFunctionLocal);
172 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
174 /// \brief Return a temporary MDNode
176 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
177 /// not uniqued, may be RAUW'd, and must be manually deleted with
179 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
181 /// \brief Deallocate a node created by getTemporary.
183 /// The node must not have any users.
184 static void deleteTemporary(MDNode *N);
186 /// \brief Replace a specific operand.
187 void replaceOperandWith(unsigned i, Value *NewVal);
189 /// \brief Return specified operand.
190 Value *getOperand(unsigned i) const LLVM_READONLY;
192 /// \brief Return number of MDNode operands.
193 unsigned getNumOperands() const { return NumOperands; }
195 /// \brief Return whether MDNode is local to a function.
196 bool isFunctionLocal() const {
197 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
200 /// \brief Return the first function-local operand's function.
202 /// If this metadata is function-local and recursively has a function-local
203 /// operand, return the first such operand's parent function. Otherwise,
204 /// return null. getFunction() should not be used for performance- critical
205 /// code because it recursively visits all the MDNode's operands.
206 const Function *getFunction() const;
208 /// \brief Calculate a unique identifier for this MDNode.
209 void Profile(FoldingSetNodeID &ID) const;
211 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
212 static bool classof(const Value *V) {
213 return V->getValueID() == MDNodeVal;
216 /// \brief Check whether MDNode is a vtable access.
217 bool isTBAAVtableAccess() const;
219 /// \brief Methods for metadata merging.
220 static MDNode *concatenate(MDNode *A, MDNode *B);
221 static MDNode *intersect(MDNode *A, MDNode *B);
222 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
223 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
224 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
225 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
227 /// \brief Delete this node. Only when there are no uses.
230 bool isNotUniqued() const {
231 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
233 void setIsNotUniqued();
235 // Shadow Value::setValueSubclassData with a private forwarding method so that
236 // any future subclasses cannot accidentally use it.
237 void setValueSubclassData(unsigned short D) {
238 Value::setValueSubclassData(D);
242 //===----------------------------------------------------------------------===//
243 /// \brief A tuple of MDNodes.
245 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
246 /// to modules, have names, and contain lists of MDNodes.
247 class NamedMDNode : public ilist_node<NamedMDNode> {
248 friend class SymbolTableListTraits<NamedMDNode, Module>;
249 friend struct ilist_traits<NamedMDNode>;
250 friend class LLVMContextImpl;
252 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
256 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
258 void setParent(Module *M) { Parent = M; }
260 explicit NamedMDNode(const Twine &N);
262 template<class T1, class T2>
263 class op_iterator_impl :
264 public std::iterator<std::bidirectional_iterator_tag, T2> {
265 const NamedMDNode *Node;
267 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
269 friend class NamedMDNode;
272 op_iterator_impl() : Node(nullptr), Idx(0) { }
274 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
275 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
276 op_iterator_impl &operator++() {
280 op_iterator_impl operator++(int) {
281 op_iterator_impl tmp(*this);
285 op_iterator_impl &operator--() {
289 op_iterator_impl operator--(int) {
290 op_iterator_impl tmp(*this);
295 T1 operator*() const { return Node->getOperand(Idx); }
299 /// \brief Drop all references and remove the node from parent module.
300 void eraseFromParent();
302 /// \brief Remove all uses and clear node vector.
303 void dropAllReferences();
307 /// \brief Get the module that holds this named metadata collection.
308 inline Module *getParent() { return Parent; }
309 inline const Module *getParent() const { return Parent; }
311 MDNode *getOperand(unsigned i) const;
312 unsigned getNumOperands() const;
313 void addOperand(MDNode *M);
314 StringRef getName() const;
315 void print(raw_ostream &ROS) const;
318 // ---------------------------------------------------------------------------
319 // Operand Iterator interface...
321 typedef op_iterator_impl<MDNode*, MDNode> op_iterator;
322 op_iterator op_begin() { return op_iterator(this, 0); }
323 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
325 typedef op_iterator_impl<const MDNode*, MDNode> const_op_iterator;
326 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
327 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
329 inline iterator_range<op_iterator> operands() {
330 return iterator_range<op_iterator>(op_begin(), op_end());
332 inline iterator_range<const_op_iterator> operands() const {
333 return iterator_range<const_op_iterator>(op_begin(), op_end());
337 } // end llvm namespace