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"
25 #include "llvm/Support/ErrorHandling.h"
30 template<typename ValueSubClass, typename ItemParentClass>
31 class SymbolTableListTraits;
34 enum LLVMConstants : uint32_t {
35 DEBUG_METADATA_VERSION = 2 // Current debug info version number.
38 /// \brief Root of the metadata hierarchy.
40 /// This is a root class for typeless data in the IR.
42 /// TODO: Detach from the Value hierarchy.
43 class Metadata : public Value {
45 Metadata(LLVMContext &Context, unsigned ID);
48 static bool classof(const Value *V) {
49 return V->getValueID() == GenericMDNodeVal ||
50 V->getValueID() == MDNodeFwdDeclVal ||
51 V->getValueID() == MDStringVal;
55 //===----------------------------------------------------------------------===//
56 /// \brief A single uniqued string.
58 /// These are used to efficiently contain a byte sequence for metadata.
59 /// MDString is always unnamed.
60 class MDString : public Metadata {
61 friend class StringMapEntry<MDString>;
63 virtual void anchor();
64 MDString(const MDString &) LLVM_DELETED_FUNCTION;
66 explicit MDString(LLVMContext &Context)
67 : Metadata(Context, Value::MDStringVal) {}
69 /// \brief Shadow Value::getName() to prevent its use.
70 StringRef getName() const LLVM_DELETED_FUNCTION;
73 static MDString *get(LLVMContext &Context, StringRef Str);
74 static MDString *get(LLVMContext &Context, const char *Str) {
75 return get(Context, Str ? StringRef(Str) : StringRef());
78 StringRef getString() const;
80 unsigned getLength() const { return (unsigned)getString().size(); }
82 typedef StringRef::iterator iterator;
84 /// \brief Pointer to the first byte of the string.
85 iterator begin() const { return getString().begin(); }
87 /// \brief Pointer to one byte past the end of the string.
88 iterator end() const { return getString().end(); }
90 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
91 static bool classof(const Value *V) {
92 return V->getValueID() == MDStringVal;
96 /// \brief A collection of metadata nodes that might be associated with a
97 /// memory access used by the alias-analysis infrastructure.
99 explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
101 : TBAA(T), Scope(S), NoAlias(N) {}
103 bool operator==(const AAMDNodes &A) const {
104 return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
107 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
109 LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
111 /// \brief The tag for type-based alias analysis.
114 /// \brief The tag for alias scope specification (used with noalias).
117 /// \brief The tag specifying the noalias scope.
121 // Specialize DenseMapInfo for AAMDNodes.
123 struct DenseMapInfo<AAMDNodes> {
124 static inline AAMDNodes getEmptyKey() {
125 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
127 static inline AAMDNodes getTombstoneKey() {
128 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
130 static unsigned getHashValue(const AAMDNodes &Val) {
131 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
132 DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
133 DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
135 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
142 //===----------------------------------------------------------------------===//
143 /// \brief Tuple of metadata.
144 class MDNode : public Metadata {
145 MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
146 void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
147 friend class MDNodeOperand;
148 friend class LLVMContextImpl;
149 void *operator new(size_t) LLVM_DELETED_FUNCTION;
152 void *operator new(size_t Size, unsigned NumOps);
154 /// \brief Required by std, but never called.
155 void operator delete(void *Mem);
157 /// \brief Required by std, but never called.
158 void operator delete(void *, unsigned) {
159 llvm_unreachable("Constructor throws?");
162 /// \brief Required by std, but never called.
163 void operator delete(void *, unsigned, bool) {
164 llvm_unreachable("Constructor throws?");
167 /// \brief Subclass data enums.
169 /// FunctionLocalBit - This bit is set if this MDNode is function local.
170 /// This is true when it (potentially transitively) contains a reference to
171 /// something in a function, like an argument, basicblock, or instruction.
172 FunctionLocalBit = 1 << 0,
174 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
175 /// have a null operand.
176 NotUniquedBit = 1 << 1
179 /// \brief FunctionLocal enums.
180 enum FunctionLocalness {
186 /// \brief Replace each instance of the given operand with a new value.
187 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
189 MDNode(LLVMContext &C, unsigned ID, ArrayRef<Value *> Vals,
190 bool isFunctionLocal);
193 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
194 FunctionLocalness FL, bool Insert = true);
196 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
197 /// \brief Construct MDNode with an explicit function-localness.
199 /// Don't analyze Vals; trust isFunctionLocal.
200 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
201 ArrayRef<Value*> Vals,
202 bool isFunctionLocal);
204 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
206 /// \brief Return a temporary MDNode
208 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
209 /// not uniqued, may be RAUW'd, and must be manually deleted with
211 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
213 /// \brief Deallocate a node created by getTemporary.
215 /// The node must not have any users.
216 static void deleteTemporary(MDNode *N);
218 /// \brief Replace a specific operand.
219 void replaceOperandWith(unsigned i, Value *NewVal);
221 /// \brief Return specified operand.
222 Value *getOperand(unsigned i) const LLVM_READONLY;
224 /// \brief Return number of MDNode operands.
225 unsigned getNumOperands() const { return NumOperands; }
227 /// \brief Return whether MDNode is local to a function.
228 bool isFunctionLocal() const {
229 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
232 /// \brief Return the first function-local operand's function.
234 /// If this metadata is function-local and recursively has a function-local
235 /// operand, return the first such operand's parent function. Otherwise,
236 /// return null. getFunction() should not be used for performance- critical
237 /// code because it recursively visits all the MDNode's operands.
238 const Function *getFunction() const;
240 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
241 static bool classof(const Value *V) {
242 return V->getValueID() == GenericMDNodeVal ||
243 V->getValueID() == MDNodeFwdDeclVal;
246 /// \brief Check whether MDNode is a vtable access.
247 bool isTBAAVtableAccess() const;
249 /// \brief Methods for metadata merging.
250 static MDNode *concatenate(MDNode *A, MDNode *B);
251 static MDNode *intersect(MDNode *A, MDNode *B);
252 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
253 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
254 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
255 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
258 bool isNotUniqued() const {
259 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
261 void setIsNotUniqued();
263 // Shadow Value::setValueSubclassData with a private forwarding method so that
264 // any future subclasses cannot accidentally use it.
265 void setValueSubclassData(unsigned short D) {
266 Value::setValueSubclassData(D);
270 /// \brief Generic metadata node.
272 /// Generic metadata nodes, with opt-out support for uniquing.
274 /// Although nodes are uniqued by default, \a GenericMDNode has no support for
275 /// RAUW. If an operand change (due to RAUW or otherwise) causes a uniquing
276 /// collision, the uniquing bit is dropped.
278 /// TODO: Make uniquing opt-out (status: mandatory, sometimes dropped).
279 /// TODO: Drop support for RAUW.
280 class GenericMDNode : public MDNode {
282 friend class LLVMContextImpl;
286 GenericMDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
287 : MDNode(C, GenericMDNodeVal, Vals, isFunctionLocal), Hash(0) {}
290 void dropAllReferences();
293 /// \brief Get the hash, if any.
294 unsigned getHash() const { return Hash; }
296 static bool classof(const Value *V) {
297 return V->getValueID() == GenericMDNodeVal;
301 /// \brief Forward declaration of metadata.
303 /// Forward declaration of metadata, in the form of a metadata node. Unlike \a
304 /// GenericMDNode, this class has support for RAUW and is suitable for forward
306 class MDNodeFwdDecl : public MDNode {
309 MDNodeFwdDecl(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
310 : MDNode(C, MDNodeFwdDeclVal, Vals, isFunctionLocal) {}
314 static bool classof(const Value *V) {
315 return V->getValueID() == MDNodeFwdDeclVal;
319 //===----------------------------------------------------------------------===//
320 /// \brief A tuple of MDNodes.
322 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
323 /// to modules, have names, and contain lists of MDNodes.
325 /// TODO: Inherit from Metadata.
326 class NamedMDNode : public ilist_node<NamedMDNode> {
327 friend class SymbolTableListTraits<NamedMDNode, Module>;
328 friend struct ilist_traits<NamedMDNode>;
329 friend class LLVMContextImpl;
331 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
335 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
337 void setParent(Module *M) { Parent = M; }
339 explicit NamedMDNode(const Twine &N);
341 template<class T1, class T2>
342 class op_iterator_impl :
343 public std::iterator<std::bidirectional_iterator_tag, T2> {
344 const NamedMDNode *Node;
346 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
348 friend class NamedMDNode;
351 op_iterator_impl() : Node(nullptr), Idx(0) { }
353 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
354 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
355 op_iterator_impl &operator++() {
359 op_iterator_impl operator++(int) {
360 op_iterator_impl tmp(*this);
364 op_iterator_impl &operator--() {
368 op_iterator_impl operator--(int) {
369 op_iterator_impl tmp(*this);
374 T1 operator*() const { return Node->getOperand(Idx); }
378 /// \brief Drop all references and remove the node from parent module.
379 void eraseFromParent();
381 /// \brief Remove all uses and clear node vector.
382 void dropAllReferences();
386 /// \brief Get the module that holds this named metadata collection.
387 inline Module *getParent() { return Parent; }
388 inline const Module *getParent() const { return Parent; }
390 MDNode *getOperand(unsigned i) const;
391 unsigned getNumOperands() const;
392 void addOperand(MDNode *M);
393 StringRef getName() const;
394 void print(raw_ostream &ROS) const;
397 // ---------------------------------------------------------------------------
398 // Operand Iterator interface...
400 typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
401 op_iterator op_begin() { return op_iterator(this, 0); }
402 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
404 typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
405 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
406 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
408 inline iterator_range<op_iterator> operands() {
409 return iterator_range<op_iterator>(op_begin(), op_end());
411 inline iterator_range<const_op_iterator> operands() const {
412 return iterator_range<const_op_iterator>(op_begin(), op_end());
416 } // end llvm namespace