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 /// \brief Root of the metadata hierarchy.
39 /// This is a root class for typeless data in the IR.
41 /// TODO: Detach from the Value hierarchy.
42 class Metadata : public Value {
44 Metadata(LLVMContext &Context, unsigned ID);
47 static bool classof(const Value *V) {
48 return V->getValueID() == MDNodeVal || V->getValueID() == MDStringVal;
52 //===----------------------------------------------------------------------===//
53 /// \brief A single uniqued string.
55 /// These are used to efficiently contain a byte sequence for metadata.
56 /// MDString is always unnamed.
57 class MDString : public Metadata {
58 friend class StringMapEntry<MDString>;
60 virtual void anchor();
61 MDString(const MDString &) LLVM_DELETED_FUNCTION;
63 explicit MDString(LLVMContext &Context)
64 : Metadata(Context, Value::MDStringVal) {}
66 /// \brief Shadow Value::getName() to prevent its use.
67 StringRef getName() const LLVM_DELETED_FUNCTION;
70 static MDString *get(LLVMContext &Context, StringRef Str);
71 static MDString *get(LLVMContext &Context, const char *Str) {
72 return get(Context, Str ? StringRef(Str) : StringRef());
75 StringRef getString() const;
77 unsigned getLength() const { return (unsigned)getString().size(); }
79 typedef StringRef::iterator iterator;
81 /// \brief Pointer to the first byte of the string.
82 iterator begin() const { return getString().begin(); }
84 /// \brief Pointer to one byte past the end of the string.
85 iterator end() const { return getString().end(); }
87 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
88 static bool classof(const Value *V) {
89 return V->getValueID() == MDStringVal;
93 /// \brief A collection of metadata nodes that might be associated with a
94 /// memory access used by the alias-analysis infrastructure.
96 explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
98 : TBAA(T), Scope(S), NoAlias(N) {}
100 bool operator==(const AAMDNodes &A) const {
101 return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
104 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
106 LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
108 /// \brief The tag for type-based alias analysis.
111 /// \brief The tag for alias scope specification (used with noalias).
114 /// \brief The tag specifying the noalias scope.
118 // Specialize DenseMapInfo for AAMDNodes.
120 struct DenseMapInfo<AAMDNodes> {
121 static inline AAMDNodes getEmptyKey() {
122 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
124 static inline AAMDNodes getTombstoneKey() {
125 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
127 static unsigned getHashValue(const AAMDNodes &Val) {
128 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
129 DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
130 DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
132 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
139 //===----------------------------------------------------------------------===//
140 /// \brief Generic tuple of metadata.
141 class MDNode : public Metadata {
142 MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
143 void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
144 friend class MDNodeOperand;
145 friend class LLVMContextImpl;
147 /// \brief If the MDNode is uniqued cache the hash to speed up lookup.
150 /// \brief Subclass data enums.
152 /// FunctionLocalBit - This bit is set if this MDNode is function local.
153 /// This is true when it (potentially transitively) contains a reference to
154 /// something in a function, like an argument, basicblock, or instruction.
155 FunctionLocalBit = 1 << 0,
157 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
158 /// have a null operand.
159 NotUniquedBit = 1 << 1,
161 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
162 /// that the node isn't being destroyed with a plain 'delete'.
166 /// \brief FunctionLocal enums.
167 enum FunctionLocalness {
173 /// \brief Replace each instance of the given operand with a new value.
174 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
177 MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
179 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
180 FunctionLocalness FL, bool Insert = true);
182 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
183 /// \brief Construct MDNode with an explicit function-localness.
185 /// Don't analyze Vals; trust isFunctionLocal.
186 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
187 ArrayRef<Value*> Vals,
188 bool isFunctionLocal);
190 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
192 /// \brief Return a temporary MDNode
194 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
195 /// not uniqued, may be RAUW'd, and must be manually deleted with
197 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
199 /// \brief Deallocate a node created by getTemporary.
201 /// The node must not have any users.
202 static void deleteTemporary(MDNode *N);
204 /// \brief Replace a specific operand.
205 void replaceOperandWith(unsigned i, Value *NewVal);
207 /// \brief Return specified operand.
208 Value *getOperand(unsigned i) const LLVM_READONLY;
210 /// \brief Return number of MDNode operands.
211 unsigned getNumOperands() const { return NumOperands; }
213 /// \brief Return whether MDNode is local to a function.
214 bool isFunctionLocal() const {
215 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
218 /// \brief Return the first function-local operand's function.
220 /// If this metadata is function-local and recursively has a function-local
221 /// operand, return the first such operand's parent function. Otherwise,
222 /// return null. getFunction() should not be used for performance- critical
223 /// code because it recursively visits all the MDNode's operands.
224 const Function *getFunction() const;
226 /// \brief Get the hash, if any.
227 unsigned getHash() const { return Hash; }
229 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
230 static bool classof(const Value *V) {
231 return V->getValueID() == MDNodeVal;
234 /// \brief Check whether MDNode is a vtable access.
235 bool isTBAAVtableAccess() const;
237 /// \brief Methods for metadata merging.
238 static MDNode *concatenate(MDNode *A, MDNode *B);
239 static MDNode *intersect(MDNode *A, MDNode *B);
240 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
241 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
242 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
243 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
245 /// \brief Delete this node. Only when there are no uses.
248 bool isNotUniqued() const {
249 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
251 void setIsNotUniqued();
253 // Shadow Value::setValueSubclassData with a private forwarding method so that
254 // any future subclasses cannot accidentally use it.
255 void setValueSubclassData(unsigned short D) {
256 Value::setValueSubclassData(D);
260 //===----------------------------------------------------------------------===//
261 /// \brief A tuple of MDNodes.
263 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
264 /// to modules, have names, and contain lists of MDNodes.
266 /// TODO: Inherit from Metadata.
267 class NamedMDNode : public ilist_node<NamedMDNode> {
268 friend class SymbolTableListTraits<NamedMDNode, Module>;
269 friend struct ilist_traits<NamedMDNode>;
270 friend class LLVMContextImpl;
272 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
276 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
278 void setParent(Module *M) { Parent = M; }
280 explicit NamedMDNode(const Twine &N);
282 template<class T1, class T2>
283 class op_iterator_impl :
284 public std::iterator<std::bidirectional_iterator_tag, T2> {
285 const NamedMDNode *Node;
287 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
289 friend class NamedMDNode;
292 op_iterator_impl() : Node(nullptr), Idx(0) { }
294 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
295 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
296 op_iterator_impl &operator++() {
300 op_iterator_impl operator++(int) {
301 op_iterator_impl tmp(*this);
305 op_iterator_impl &operator--() {
309 op_iterator_impl operator--(int) {
310 op_iterator_impl tmp(*this);
315 T1 operator*() const { return Node->getOperand(Idx); }
319 /// \brief Drop all references and remove the node from parent module.
320 void eraseFromParent();
322 /// \brief Remove all uses and clear node vector.
323 void dropAllReferences();
327 /// \brief Get the module that holds this named metadata collection.
328 inline Module *getParent() { return Parent; }
329 inline const Module *getParent() const { return Parent; }
331 MDNode *getOperand(unsigned i) const;
332 unsigned getNumOperands() const;
333 void addOperand(MDNode *M);
334 StringRef getName() const;
335 void print(raw_ostream &ROS) const;
338 // ---------------------------------------------------------------------------
339 // Operand Iterator interface...
341 typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
342 op_iterator op_begin() { return op_iterator(this, 0); }
343 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
345 typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
346 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
347 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
349 inline iterator_range<op_iterator> operands() {
350 return iterator_range<op_iterator>(op_begin(), op_end());
352 inline iterator_range<const_op_iterator> operands() const {
353 return iterator_range<const_op_iterator>(op_begin(), op_end());
357 } // end llvm namespace