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() == GenericMDNodeVal ||
49 V->getValueID() == MDNodeFwdDeclVal ||
50 V->getValueID() == MDStringVal;
54 //===----------------------------------------------------------------------===//
55 /// \brief A single uniqued string.
57 /// These are used to efficiently contain a byte sequence for metadata.
58 /// MDString is always unnamed.
59 class MDString : public Metadata {
60 friend class StringMapEntry<MDString>;
62 virtual void anchor();
63 MDString(const MDString &) LLVM_DELETED_FUNCTION;
65 explicit MDString(LLVMContext &Context)
66 : Metadata(Context, Value::MDStringVal) {}
68 /// \brief Shadow Value::getName() to prevent its use.
69 StringRef getName() const LLVM_DELETED_FUNCTION;
72 static MDString *get(LLVMContext &Context, StringRef Str);
73 static MDString *get(LLVMContext &Context, const char *Str) {
74 return get(Context, Str ? StringRef(Str) : StringRef());
77 StringRef getString() const;
79 unsigned getLength() const { return (unsigned)getString().size(); }
81 typedef StringRef::iterator iterator;
83 /// \brief Pointer to the first byte of the string.
84 iterator begin() const { return getString().begin(); }
86 /// \brief Pointer to one byte past the end of the string.
87 iterator end() const { return getString().end(); }
89 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
90 static bool classof(const Value *V) {
91 return V->getValueID() == MDStringVal;
95 /// \brief A collection of metadata nodes that might be associated with a
96 /// memory access used by the alias-analysis infrastructure.
98 explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
100 : TBAA(T), Scope(S), NoAlias(N) {}
102 bool operator==(const AAMDNodes &A) const {
103 return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
106 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
108 LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
110 /// \brief The tag for type-based alias analysis.
113 /// \brief The tag for alias scope specification (used with noalias).
116 /// \brief The tag specifying the noalias scope.
120 // Specialize DenseMapInfo for AAMDNodes.
122 struct DenseMapInfo<AAMDNodes> {
123 static inline AAMDNodes getEmptyKey() {
124 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
126 static inline AAMDNodes getTombstoneKey() {
127 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
129 static unsigned getHashValue(const AAMDNodes &Val) {
130 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
131 DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
132 DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
134 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
141 //===----------------------------------------------------------------------===//
142 /// \brief Tuple of metadata.
143 class MDNode : public Metadata {
144 MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
145 void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
146 friend class MDNodeOperand;
147 friend class LLVMContextImpl;
150 // TODO: Sink this into GenericMDNode. Can't do this until operands are
151 // allocated at the front (currently they're at the back).
154 /// \brief Subclass data enums.
156 /// FunctionLocalBit - This bit is set if this MDNode is function local.
157 /// This is true when it (potentially transitively) contains a reference to
158 /// something in a function, like an argument, basicblock, or instruction.
159 FunctionLocalBit = 1 << 0,
161 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
162 /// have a null operand.
163 NotUniquedBit = 1 << 1,
165 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
166 /// that the node isn't being destroyed with a plain 'delete'.
170 /// \brief FunctionLocal enums.
171 enum FunctionLocalness {
177 /// \brief Replace each instance of the given operand with a new value.
178 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
181 MDNode(LLVMContext &C, unsigned ID, ArrayRef<Value *> Vals,
182 bool isFunctionLocal);
184 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
185 FunctionLocalness FL, bool Insert = true);
187 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
188 /// \brief Construct MDNode with an explicit function-localness.
190 /// Don't analyze Vals; trust isFunctionLocal.
191 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
192 ArrayRef<Value*> Vals,
193 bool isFunctionLocal);
195 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
197 /// \brief Return a temporary MDNode
199 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
200 /// not uniqued, may be RAUW'd, and must be manually deleted with
202 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
204 /// \brief Deallocate a node created by getTemporary.
206 /// The node must not have any users.
207 static void deleteTemporary(MDNode *N);
209 /// \brief Replace a specific operand.
210 void replaceOperandWith(unsigned i, Value *NewVal);
212 /// \brief Return specified operand.
213 Value *getOperand(unsigned i) const LLVM_READONLY;
215 /// \brief Return number of MDNode operands.
216 unsigned getNumOperands() const { return NumOperands; }
218 /// \brief Return whether MDNode is local to a function.
219 bool isFunctionLocal() const {
220 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
223 /// \brief Return the first function-local operand's function.
225 /// If this metadata is function-local and recursively has a function-local
226 /// operand, return the first such operand's parent function. Otherwise,
227 /// return null. getFunction() should not be used for performance- critical
228 /// code because it recursively visits all the MDNode's operands.
229 const Function *getFunction() const;
231 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
232 static bool classof(const Value *V) {
233 return V->getValueID() == GenericMDNodeVal ||
234 V->getValueID() == MDNodeFwdDeclVal;
237 /// \brief Check whether MDNode is a vtable access.
238 bool isTBAAVtableAccess() const;
240 /// \brief Methods for metadata merging.
241 static MDNode *concatenate(MDNode *A, MDNode *B);
242 static MDNode *intersect(MDNode *A, MDNode *B);
243 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
244 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
245 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
246 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
249 bool isNotUniqued() const {
250 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
252 void setIsNotUniqued();
254 // Shadow Value::setValueSubclassData with a private forwarding method so that
255 // any future subclasses cannot accidentally use it.
256 void setValueSubclassData(unsigned short D) {
257 Value::setValueSubclassData(D);
261 /// \brief Generic metadata node.
263 /// Generic metadata nodes, with opt-out support for uniquing.
265 /// Although nodes are uniqued by default, \a GenericMDNode has no support for
266 /// RAUW. If an operand change (due to RAUW or otherwise) causes a uniquing
267 /// collision, the uniquing bit is dropped.
269 /// TODO: Make uniquing opt-out (status: mandatory, sometimes dropped).
270 /// TODO: Drop support for RAUW.
271 class GenericMDNode : public MDNode {
274 GenericMDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
275 : MDNode(C, GenericMDNodeVal, Vals, isFunctionLocal) {}
279 /// \brief Get the hash, if any.
280 unsigned getHash() const { return Hash; }
282 static bool classof(const Value *V) {
283 return V->getValueID() == GenericMDNodeVal;
287 /// \brief Delete this node. Only when there are no uses.
290 friend class LLVMContextImpl;
293 /// \brief Forward declaration of metadata.
295 /// Forward declaration of metadata, in the form of a metadata node. Unlike \a
296 /// GenericMDNode, this class has support for RAUW and is suitable for forward
298 class MDNodeFwdDecl : public MDNode {
301 MDNodeFwdDecl(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
302 : MDNode(C, MDNodeFwdDeclVal, Vals, isFunctionLocal) {}
306 static bool classof(const Value *V) {
307 return V->getValueID() == MDNodeFwdDeclVal;
311 /// \brief Delete this node. Only when there are no uses.
316 //===----------------------------------------------------------------------===//
317 /// \brief A tuple of MDNodes.
319 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
320 /// to modules, have names, and contain lists of MDNodes.
322 /// TODO: Inherit from Metadata.
323 class NamedMDNode : public ilist_node<NamedMDNode> {
324 friend class SymbolTableListTraits<NamedMDNode, Module>;
325 friend struct ilist_traits<NamedMDNode>;
326 friend class LLVMContextImpl;
328 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
332 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
334 void setParent(Module *M) { Parent = M; }
336 explicit NamedMDNode(const Twine &N);
338 template<class T1, class T2>
339 class op_iterator_impl :
340 public std::iterator<std::bidirectional_iterator_tag, T2> {
341 const NamedMDNode *Node;
343 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
345 friend class NamedMDNode;
348 op_iterator_impl() : Node(nullptr), Idx(0) { }
350 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
351 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
352 op_iterator_impl &operator++() {
356 op_iterator_impl operator++(int) {
357 op_iterator_impl tmp(*this);
361 op_iterator_impl &operator--() {
365 op_iterator_impl operator--(int) {
366 op_iterator_impl tmp(*this);
371 T1 operator*() const { return Node->getOperand(Idx); }
375 /// \brief Drop all references and remove the node from parent module.
376 void eraseFromParent();
378 /// \brief Remove all uses and clear node vector.
379 void dropAllReferences();
383 /// \brief Get the module that holds this named metadata collection.
384 inline Module *getParent() { return Parent; }
385 inline const Module *getParent() const { return Parent; }
387 MDNode *getOperand(unsigned i) const;
388 unsigned getNumOperands() const;
389 void addOperand(MDNode *M);
390 StringRef getName() const;
391 void print(raw_ostream &ROS) const;
394 // ---------------------------------------------------------------------------
395 // Operand Iterator interface...
397 typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
398 op_iterator op_begin() { return op_iterator(this, 0); }
399 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
401 typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
402 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
403 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
405 inline iterator_range<op_iterator> operands() {
406 return iterator_range<op_iterator>(op_begin(), op_end());
408 inline iterator_range<const_op_iterator> operands() const {
409 return iterator_range<const_op_iterator>(op_begin(), op_end());
413 } // end llvm namespace