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(Type *Type, unsigned ID) : Value(Type, ID) {}
47 static bool classof(const Value *V) {
48 return V->getValueID() == MDNodeVal;
52 //===----------------------------------------------------------------------===//
53 /// \brief A single uniqued string.
55 /// These are used to efficiently contain a byte sequence for metadata.
56 /// MDString is always unnamed.
58 /// TODO: Inherit from Metadata.
59 class MDString : public Value {
60 virtual void anchor();
61 MDString(const MDString &) LLVM_DELETED_FUNCTION;
63 explicit MDString(LLVMContext &C);
65 static MDString *get(LLVMContext &Context, StringRef Str);
66 static MDString *get(LLVMContext &Context, const char *Str) {
67 return get(Context, Str ? StringRef(Str) : StringRef());
70 StringRef getString() const { return getName(); }
72 unsigned getLength() const { return (unsigned)getName().size(); }
74 typedef StringRef::iterator iterator;
76 /// \brief Pointer to the first byte of the string.
77 iterator begin() const { return getName().begin(); }
79 /// \brief Pointer to one byte past the end of the string.
80 iterator end() const { return getName().end(); }
82 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
83 static bool classof(const Value *V) {
84 return V->getValueID() == MDStringVal;
88 /// \brief A collection of metadata nodes that might be associated with a
89 /// memory access used by the alias-analysis infrastructure.
91 explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
93 : TBAA(T), Scope(S), NoAlias(N) {}
95 bool operator==(const AAMDNodes &A) const {
96 return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
99 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
101 LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
103 /// \brief The tag for type-based alias analysis.
106 /// \brief The tag for alias scope specification (used with noalias).
109 /// \brief The tag specifying the noalias scope.
113 // Specialize DenseMapInfo for AAMDNodes.
115 struct DenseMapInfo<AAMDNodes> {
116 static inline AAMDNodes getEmptyKey() {
117 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
119 static inline AAMDNodes getTombstoneKey() {
120 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
122 static unsigned getHashValue(const AAMDNodes &Val) {
123 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
124 DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
125 DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
127 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
134 //===----------------------------------------------------------------------===//
135 /// \brief Generic tuple of metadata.
136 class MDNode : public Metadata, public FoldingSetNode {
137 MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
138 void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
139 friend class MDNodeOperand;
140 friend class LLVMContextImpl;
141 friend struct FoldingSetTrait<MDNode>;
143 /// \brief If the MDNode is uniqued cache the hash to speed up lookup.
146 /// \brief Subclass data enums.
148 /// FunctionLocalBit - This bit is set if this MDNode is function local.
149 /// This is true when it (potentially transitively) contains a reference to
150 /// something in a function, like an argument, basicblock, or instruction.
151 FunctionLocalBit = 1 << 0,
153 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
154 /// have a null operand.
155 NotUniquedBit = 1 << 1,
157 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
158 /// that the node isn't being destroyed with a plain 'delete'.
162 /// \brief FunctionLocal enums.
163 enum FunctionLocalness {
169 /// \brief Replace each instance of the given operand with a new value.
170 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
173 MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
175 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
176 FunctionLocalness FL, bool Insert = true);
178 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
179 /// \brief Construct MDNode with an explicit function-localness.
181 /// Don't analyze Vals; trust isFunctionLocal.
182 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
183 ArrayRef<Value*> Vals,
184 bool isFunctionLocal);
186 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
188 /// \brief Return a temporary MDNode
190 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
191 /// not uniqued, may be RAUW'd, and must be manually deleted with
193 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
195 /// \brief Deallocate a node created by getTemporary.
197 /// The node must not have any users.
198 static void deleteTemporary(MDNode *N);
200 /// \brief Replace a specific operand.
201 void replaceOperandWith(unsigned i, Value *NewVal);
203 /// \brief Return specified operand.
204 Value *getOperand(unsigned i) const LLVM_READONLY;
206 /// \brief Return number of MDNode operands.
207 unsigned getNumOperands() const { return NumOperands; }
209 /// \brief Return whether MDNode is local to a function.
210 bool isFunctionLocal() const {
211 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
214 /// \brief Return the first function-local operand's function.
216 /// If this metadata is function-local and recursively has a function-local
217 /// operand, return the first such operand's parent function. Otherwise,
218 /// return null. getFunction() should not be used for performance- critical
219 /// code because it recursively visits all the MDNode's operands.
220 const Function *getFunction() const;
222 /// \brief Calculate a unique identifier for this MDNode.
223 void Profile(FoldingSetNodeID &ID) const;
225 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
226 static bool classof(const Value *V) {
227 return V->getValueID() == MDNodeVal;
230 /// \brief Check whether MDNode is a vtable access.
231 bool isTBAAVtableAccess() const;
233 /// \brief Methods for metadata merging.
234 static MDNode *concatenate(MDNode *A, MDNode *B);
235 static MDNode *intersect(MDNode *A, MDNode *B);
236 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
237 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
238 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
239 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
241 /// \brief Delete this node. Only when there are no uses.
244 bool isNotUniqued() const {
245 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
247 void setIsNotUniqued();
249 // Shadow Value::setValueSubclassData with a private forwarding method so that
250 // any future subclasses cannot accidentally use it.
251 void setValueSubclassData(unsigned short D) {
252 Value::setValueSubclassData(D);
256 //===----------------------------------------------------------------------===//
257 /// \brief A tuple of MDNodes.
259 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
260 /// to modules, have names, and contain lists of MDNodes.
262 /// TODO: Inherit from Metadata.
263 class NamedMDNode : public ilist_node<NamedMDNode> {
264 friend class SymbolTableListTraits<NamedMDNode, Module>;
265 friend struct ilist_traits<NamedMDNode>;
266 friend class LLVMContextImpl;
268 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
272 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
274 void setParent(Module *M) { Parent = M; }
276 explicit NamedMDNode(const Twine &N);
278 template<class T1, class T2>
279 class op_iterator_impl :
280 public std::iterator<std::bidirectional_iterator_tag, T2> {
281 const NamedMDNode *Node;
283 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
285 friend class NamedMDNode;
288 op_iterator_impl() : Node(nullptr), Idx(0) { }
290 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
291 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
292 op_iterator_impl &operator++() {
296 op_iterator_impl operator++(int) {
297 op_iterator_impl tmp(*this);
301 op_iterator_impl &operator--() {
305 op_iterator_impl operator--(int) {
306 op_iterator_impl tmp(*this);
311 T1 operator*() const { return Node->getOperand(Idx); }
315 /// \brief Drop all references and remove the node from parent module.
316 void eraseFromParent();
318 /// \brief Remove all uses and clear node vector.
319 void dropAllReferences();
323 /// \brief Get the module that holds this named metadata collection.
324 inline Module *getParent() { return Parent; }
325 inline const Module *getParent() const { return Parent; }
327 MDNode *getOperand(unsigned i) const;
328 unsigned getNumOperands() const;
329 void addOperand(MDNode *M);
330 StringRef getName() const;
331 void print(raw_ostream &ROS) const;
334 // ---------------------------------------------------------------------------
335 // Operand Iterator interface...
337 typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
338 op_iterator op_begin() { return op_iterator(this, 0); }
339 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
341 typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
342 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
343 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
345 inline iterator_range<op_iterator> operands() {
346 return iterator_range<op_iterator>(op_begin(), op_end());
348 inline iterator_range<const_op_iterator> operands() const {
349 return iterator_range<const_op_iterator>(op_begin(), op_end());
353 } // end llvm namespace