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;
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 friend class StringMapEntry<MDString>;
62 virtual void anchor();
63 MDString(const MDString &) LLVM_DELETED_FUNCTION;
65 explicit MDString(LLVMContext &C);
67 /// \brief Shadow Value::getName() to prevent its use.
68 StringRef getName() const LLVM_DELETED_FUNCTION;
71 static MDString *get(LLVMContext &Context, StringRef Str);
72 static MDString *get(LLVMContext &Context, const char *Str) {
73 return get(Context, Str ? StringRef(Str) : StringRef());
76 StringRef getString() const;
78 unsigned getLength() const { return (unsigned)getString().size(); }
80 typedef StringRef::iterator iterator;
82 /// \brief Pointer to the first byte of the string.
83 iterator begin() const { return getString().begin(); }
85 /// \brief Pointer to one byte past the end of the string.
86 iterator end() const { return getString().end(); }
88 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
89 static bool classof(const Value *V) {
90 return V->getValueID() == MDStringVal;
94 /// \brief A collection of metadata nodes that might be associated with a
95 /// memory access used by the alias-analysis infrastructure.
97 explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
99 : TBAA(T), Scope(S), NoAlias(N) {}
101 bool operator==(const AAMDNodes &A) const {
102 return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
105 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
107 LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
109 /// \brief The tag for type-based alias analysis.
112 /// \brief The tag for alias scope specification (used with noalias).
115 /// \brief The tag specifying the noalias scope.
119 // Specialize DenseMapInfo for AAMDNodes.
121 struct DenseMapInfo<AAMDNodes> {
122 static inline AAMDNodes getEmptyKey() {
123 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
125 static inline AAMDNodes getTombstoneKey() {
126 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
128 static unsigned getHashValue(const AAMDNodes &Val) {
129 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
130 DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
131 DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
133 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
140 //===----------------------------------------------------------------------===//
141 /// \brief Generic tuple of metadata.
142 class MDNode : public Metadata, public FoldingSetNode {
143 MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
144 void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
145 friend class MDNodeOperand;
146 friend class LLVMContextImpl;
147 friend struct FoldingSetTrait<MDNode>;
149 /// \brief If the MDNode is uniqued cache the hash to speed up lookup.
152 /// \brief Subclass data enums.
154 /// FunctionLocalBit - This bit is set if this MDNode is function local.
155 /// This is true when it (potentially transitively) contains a reference to
156 /// something in a function, like an argument, basicblock, or instruction.
157 FunctionLocalBit = 1 << 0,
159 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
160 /// have a null operand.
161 NotUniquedBit = 1 << 1,
163 /// DestroyFlag - This bit is set by destroy() so the destructor can assert
164 /// that the node isn't being destroyed with a plain 'delete'.
168 /// \brief FunctionLocal enums.
169 enum FunctionLocalness {
175 /// \brief Replace each instance of the given operand with a new value.
176 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
179 MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
181 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
182 FunctionLocalness FL, bool Insert = true);
184 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
185 /// \brief Construct MDNode with an explicit function-localness.
187 /// Don't analyze Vals; trust isFunctionLocal.
188 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
189 ArrayRef<Value*> Vals,
190 bool isFunctionLocal);
192 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
194 /// \brief Return a temporary MDNode
196 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
197 /// not uniqued, may be RAUW'd, and must be manually deleted with
199 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
201 /// \brief Deallocate a node created by getTemporary.
203 /// The node must not have any users.
204 static void deleteTemporary(MDNode *N);
206 /// \brief Replace a specific operand.
207 void replaceOperandWith(unsigned i, Value *NewVal);
209 /// \brief Return specified operand.
210 Value *getOperand(unsigned i) const LLVM_READONLY;
212 /// \brief Return number of MDNode operands.
213 unsigned getNumOperands() const { return NumOperands; }
215 /// \brief Return whether MDNode is local to a function.
216 bool isFunctionLocal() const {
217 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
220 /// \brief Return the first function-local operand's function.
222 /// If this metadata is function-local and recursively has a function-local
223 /// operand, return the first such operand's parent function. Otherwise,
224 /// return null. getFunction() should not be used for performance- critical
225 /// code because it recursively visits all the MDNode's operands.
226 const Function *getFunction() const;
228 /// \brief Calculate a unique identifier for this MDNode.
229 void Profile(FoldingSetNodeID &ID) 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() == MDNodeVal;
236 /// \brief Check whether MDNode is a vtable access.
237 bool isTBAAVtableAccess() const;
239 /// \brief Methods for metadata merging.
240 static MDNode *concatenate(MDNode *A, MDNode *B);
241 static MDNode *intersect(MDNode *A, MDNode *B);
242 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
243 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
244 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
245 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
247 /// \brief Delete this node. Only when there are no uses.
250 bool isNotUniqued() const {
251 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
253 void setIsNotUniqued();
255 // Shadow Value::setValueSubclassData with a private forwarding method so that
256 // any future subclasses cannot accidentally use it.
257 void setValueSubclassData(unsigned short D) {
258 Value::setValueSubclassData(D);
262 //===----------------------------------------------------------------------===//
263 /// \brief A tuple of MDNodes.
265 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
266 /// to modules, have names, and contain lists of MDNodes.
268 /// TODO: Inherit from Metadata.
269 class NamedMDNode : public ilist_node<NamedMDNode> {
270 friend class SymbolTableListTraits<NamedMDNode, Module>;
271 friend struct ilist_traits<NamedMDNode>;
272 friend class LLVMContextImpl;
274 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
278 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
280 void setParent(Module *M) { Parent = M; }
282 explicit NamedMDNode(const Twine &N);
284 template<class T1, class T2>
285 class op_iterator_impl :
286 public std::iterator<std::bidirectional_iterator_tag, T2> {
287 const NamedMDNode *Node;
289 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
291 friend class NamedMDNode;
294 op_iterator_impl() : Node(nullptr), Idx(0) { }
296 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
297 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
298 op_iterator_impl &operator++() {
302 op_iterator_impl operator++(int) {
303 op_iterator_impl tmp(*this);
307 op_iterator_impl &operator--() {
311 op_iterator_impl operator--(int) {
312 op_iterator_impl tmp(*this);
317 T1 operator*() const { return Node->getOperand(Idx); }
321 /// \brief Drop all references and remove the node from parent module.
322 void eraseFromParent();
324 /// \brief Remove all uses and clear node vector.
325 void dropAllReferences();
329 /// \brief Get the module that holds this named metadata collection.
330 inline Module *getParent() { return Parent; }
331 inline const Module *getParent() const { return Parent; }
333 MDNode *getOperand(unsigned i) const;
334 unsigned getNumOperands() const;
335 void addOperand(MDNode *M);
336 StringRef getName() const;
337 void print(raw_ostream &ROS) const;
340 // ---------------------------------------------------------------------------
341 // Operand Iterator interface...
343 typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
344 op_iterator op_begin() { return op_iterator(this, 0); }
345 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
347 typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
348 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
349 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
351 inline iterator_range<op_iterator> operands() {
352 return iterator_range<op_iterator>(op_begin(), op_end());
354 inline iterator_range<const_op_iterator> operands() const {
355 return iterator_range<const_op_iterator>(op_begin(), op_end());
359 } // end llvm namespace