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, public FoldingSetNode {
142 MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
143 void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
144 friend class MDNodeOperand;
145 friend class LLVMContextImpl;
146 friend struct FoldingSetTrait<MDNode>;
148 /// \brief If the MDNode is uniqued cache the hash to speed up lookup.
151 /// \brief Subclass data enums.
153 /// FunctionLocalBit - This bit is set if this MDNode is function local.
154 /// This is true when it (potentially transitively) contains a reference to
155 /// something in a function, like an argument, basicblock, or instruction.
156 FunctionLocalBit = 1 << 0,
158 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
159 /// have a null operand.
160 NotUniquedBit = 1 << 1,
162 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
163 /// that the node isn't being destroyed with a plain 'delete'.
167 /// \brief FunctionLocal enums.
168 enum FunctionLocalness {
174 /// \brief Replace each instance of the given operand with a new value.
175 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
178 MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
180 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
181 FunctionLocalness FL, bool Insert = true);
183 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
184 /// \brief Construct MDNode with an explicit function-localness.
186 /// Don't analyze Vals; trust isFunctionLocal.
187 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
188 ArrayRef<Value*> Vals,
189 bool isFunctionLocal);
191 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
193 /// \brief Return a temporary MDNode
195 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
196 /// not uniqued, may be RAUW'd, and must be manually deleted with
198 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
200 /// \brief Deallocate a node created by getTemporary.
202 /// The node must not have any users.
203 static void deleteTemporary(MDNode *N);
205 /// \brief Replace a specific operand.
206 void replaceOperandWith(unsigned i, Value *NewVal);
208 /// \brief Return specified operand.
209 Value *getOperand(unsigned i) const LLVM_READONLY;
211 /// \brief Return number of MDNode operands.
212 unsigned getNumOperands() const { return NumOperands; }
214 /// \brief Return whether MDNode is local to a function.
215 bool isFunctionLocal() const {
216 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
219 /// \brief Return the first function-local operand's function.
221 /// If this metadata is function-local and recursively has a function-local
222 /// operand, return the first such operand's parent function. Otherwise,
223 /// return null. getFunction() should not be used for performance- critical
224 /// code because it recursively visits all the MDNode's operands.
225 const Function *getFunction() const;
227 /// \brief Calculate a unique identifier for this MDNode.
228 void Profile(FoldingSetNodeID &ID) const;
230 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
231 static bool classof(const Value *V) {
232 return V->getValueID() == MDNodeVal;
235 /// \brief Check whether MDNode is a vtable access.
236 bool isTBAAVtableAccess() const;
238 /// \brief Methods for metadata merging.
239 static MDNode *concatenate(MDNode *A, MDNode *B);
240 static MDNode *intersect(MDNode *A, MDNode *B);
241 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
242 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
243 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
244 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
246 /// \brief Delete this node. Only when there are no uses.
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 //===----------------------------------------------------------------------===//
262 /// \brief A tuple of MDNodes.
264 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
265 /// to modules, have names, and contain lists of MDNodes.
267 /// TODO: Inherit from Metadata.
268 class NamedMDNode : public ilist_node<NamedMDNode> {
269 friend class SymbolTableListTraits<NamedMDNode, Module>;
270 friend struct ilist_traits<NamedMDNode>;
271 friend class LLVMContextImpl;
273 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
277 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
279 void setParent(Module *M) { Parent = M; }
281 explicit NamedMDNode(const Twine &N);
283 template<class T1, class T2>
284 class op_iterator_impl :
285 public std::iterator<std::bidirectional_iterator_tag, T2> {
286 const NamedMDNode *Node;
288 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
290 friend class NamedMDNode;
293 op_iterator_impl() : Node(nullptr), Idx(0) { }
295 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
296 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
297 op_iterator_impl &operator++() {
301 op_iterator_impl operator++(int) {
302 op_iterator_impl tmp(*this);
306 op_iterator_impl &operator--() {
310 op_iterator_impl operator--(int) {
311 op_iterator_impl tmp(*this);
316 T1 operator*() const { return Node->getOperand(Idx); }
320 /// \brief Drop all references and remove the node from parent module.
321 void eraseFromParent();
323 /// \brief Remove all uses and clear node vector.
324 void dropAllReferences();
328 /// \brief Get the module that holds this named metadata collection.
329 inline Module *getParent() { return Parent; }
330 inline const Module *getParent() const { return Parent; }
332 MDNode *getOperand(unsigned i) const;
333 unsigned getNumOperands() const;
334 void addOperand(MDNode *M);
335 StringRef getName() const;
336 void print(raw_ostream &ROS) const;
339 // ---------------------------------------------------------------------------
340 // Operand Iterator interface...
342 typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
343 op_iterator op_begin() { return op_iterator(this, 0); }
344 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
346 typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
347 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
348 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
350 inline iterator_range<op_iterator> operands() {
351 return iterator_range<op_iterator>(op_begin(), op_end());
353 inline iterator_range<const_op_iterator> operands() const {
354 return iterator_range<const_op_iterator>(op_begin(), op_end());
358 } // end llvm namespace