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 StringMapEntry<MDString> *Entry;
67 explicit MDString(LLVMContext &Context)
68 : Metadata(Context, Value::MDStringVal), Entry(nullptr) {}
70 /// \brief Shadow Value::getName() to prevent its use.
71 StringRef getName() const LLVM_DELETED_FUNCTION;
74 static MDString *get(LLVMContext &Context, StringRef Str);
75 static MDString *get(LLVMContext &Context, const char *Str) {
76 return get(Context, Str ? StringRef(Str) : StringRef());
79 StringRef getString() const;
81 unsigned getLength() const { return (unsigned)getString().size(); }
83 typedef StringRef::iterator iterator;
85 /// \brief Pointer to the first byte of the string.
86 iterator begin() const { return getString().begin(); }
88 /// \brief Pointer to one byte past the end of the string.
89 iterator end() const { return getString().end(); }
91 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
92 static bool classof(const Value *V) {
93 return V->getValueID() == MDStringVal;
97 /// \brief A collection of metadata nodes that might be associated with a
98 /// memory access used by the alias-analysis infrastructure.
100 explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
102 : TBAA(T), Scope(S), NoAlias(N) {}
104 bool operator==(const AAMDNodes &A) const {
105 return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
108 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
110 LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
112 /// \brief The tag for type-based alias analysis.
115 /// \brief The tag for alias scope specification (used with noalias).
118 /// \brief The tag specifying the noalias scope.
122 // Specialize DenseMapInfo for AAMDNodes.
124 struct DenseMapInfo<AAMDNodes> {
125 static inline AAMDNodes getEmptyKey() {
126 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
128 static inline AAMDNodes getTombstoneKey() {
129 return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
131 static unsigned getHashValue(const AAMDNodes &Val) {
132 return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
133 DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
134 DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
136 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
143 //===----------------------------------------------------------------------===//
144 /// \brief Tuple of metadata.
145 class MDNode : public Metadata {
146 MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
147 void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
148 friend class MDNodeOperand;
149 friend class LLVMContextImpl;
150 void *operator new(size_t) LLVM_DELETED_FUNCTION;
153 void *operator new(size_t Size, unsigned NumOps);
155 /// \brief Required by std, but never called.
156 void operator delete(void *Mem);
158 /// \brief Required by std, but never called.
159 void operator delete(void *, unsigned) {
160 llvm_unreachable("Constructor throws?");
163 /// \brief Required by std, but never called.
164 void operator delete(void *, unsigned, bool) {
165 llvm_unreachable("Constructor throws?");
168 /// \brief Subclass data enums.
170 /// FunctionLocalBit - This bit is set if this MDNode is function local.
171 /// This is true when it (potentially transitively) contains a reference to
172 /// something in a function, like an argument, basicblock, or instruction.
173 FunctionLocalBit = 1 << 0,
175 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
176 /// have a null operand.
177 NotUniquedBit = 1 << 1
180 /// \brief FunctionLocal enums.
181 enum FunctionLocalness {
187 /// \brief Replace each instance of the given operand with a new value.
188 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
190 MDNode(LLVMContext &C, unsigned ID, ArrayRef<Value *> Vals,
191 bool isFunctionLocal);
194 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
195 FunctionLocalness FL, bool Insert = true);
197 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
198 /// \brief Construct MDNode with an explicit function-localness.
200 /// Don't analyze Vals; trust isFunctionLocal.
201 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
202 ArrayRef<Value*> Vals,
203 bool isFunctionLocal);
205 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
207 /// \brief Return a temporary MDNode
209 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
210 /// not uniqued, may be RAUW'd, and must be manually deleted with
212 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
214 /// \brief Deallocate a node created by getTemporary.
216 /// The node must not have any users.
217 static void deleteTemporary(MDNode *N);
219 /// \brief Replace a specific operand.
220 void replaceOperandWith(unsigned i, Value *NewVal);
222 /// \brief Return specified operand.
223 Value *getOperand(unsigned i) const LLVM_READONLY;
225 /// \brief Return number of MDNode operands.
226 unsigned getNumOperands() const { return NumOperands; }
228 /// \brief Return whether MDNode is local to a function.
229 bool isFunctionLocal() const {
230 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
233 /// \brief Return the first function-local operand's function.
235 /// If this metadata is function-local and recursively has a function-local
236 /// operand, return the first such operand's parent function. Otherwise,
237 /// return null. getFunction() should not be used for performance- critical
238 /// code because it recursively visits all the MDNode's operands.
239 const Function *getFunction() const;
241 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
242 static bool classof(const Value *V) {
243 return V->getValueID() == GenericMDNodeVal ||
244 V->getValueID() == MDNodeFwdDeclVal;
247 /// \brief Check whether MDNode is a vtable access.
248 bool isTBAAVtableAccess() const;
250 /// \brief Methods for metadata merging.
251 static MDNode *concatenate(MDNode *A, MDNode *B);
252 static MDNode *intersect(MDNode *A, MDNode *B);
253 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
254 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
255 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
256 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
259 bool isNotUniqued() const {
260 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
262 void setIsNotUniqued();
264 // Shadow Value::setValueSubclassData with a private forwarding method so that
265 // any future subclasses cannot accidentally use it.
266 void setValueSubclassData(unsigned short D) {
267 Value::setValueSubclassData(D);
271 /// \brief Generic metadata node.
273 /// Generic metadata nodes, with opt-out support for uniquing.
275 /// Although nodes are uniqued by default, \a GenericMDNode has no support for
276 /// RAUW. If an operand change (due to RAUW or otherwise) causes a uniquing
277 /// collision, the uniquing bit is dropped.
279 /// TODO: Make uniquing opt-out (status: mandatory, sometimes dropped).
280 /// TODO: Drop support for RAUW.
281 class GenericMDNode : public MDNode {
283 friend class LLVMContextImpl;
287 GenericMDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
288 : MDNode(C, GenericMDNodeVal, Vals, isFunctionLocal), Hash(0) {}
291 void dropAllReferences();
294 /// \brief Get the hash, if any.
295 unsigned getHash() const { return Hash; }
297 static bool classof(const Value *V) {
298 return V->getValueID() == GenericMDNodeVal;
302 /// \brief Forward declaration of metadata.
304 /// Forward declaration of metadata, in the form of a metadata node. Unlike \a
305 /// GenericMDNode, this class has support for RAUW and is suitable for forward
307 class MDNodeFwdDecl : public MDNode {
310 MDNodeFwdDecl(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
311 : MDNode(C, MDNodeFwdDeclVal, Vals, isFunctionLocal) {}
315 static bool classof(const Value *V) {
316 return V->getValueID() == MDNodeFwdDeclVal;
320 //===----------------------------------------------------------------------===//
321 /// \brief A tuple of MDNodes.
323 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
324 /// to modules, have names, and contain lists of MDNodes.
326 /// TODO: Inherit from Metadata.
327 class NamedMDNode : public ilist_node<NamedMDNode> {
328 friend class SymbolTableListTraits<NamedMDNode, Module>;
329 friend struct ilist_traits<NamedMDNode>;
330 friend class LLVMContextImpl;
332 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
336 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
338 void setParent(Module *M) { Parent = M; }
340 explicit NamedMDNode(const Twine &N);
342 template<class T1, class T2>
343 class op_iterator_impl :
344 public std::iterator<std::bidirectional_iterator_tag, T2> {
345 const NamedMDNode *Node;
347 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
349 friend class NamedMDNode;
352 op_iterator_impl() : Node(nullptr), Idx(0) { }
354 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
355 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
356 op_iterator_impl &operator++() {
360 op_iterator_impl operator++(int) {
361 op_iterator_impl tmp(*this);
365 op_iterator_impl &operator--() {
369 op_iterator_impl operator--(int) {
370 op_iterator_impl tmp(*this);
375 T1 operator*() const { return Node->getOperand(Idx); }
379 /// \brief Drop all references and remove the node from parent module.
380 void eraseFromParent();
382 /// \brief Remove all uses and clear node vector.
383 void dropAllReferences();
387 /// \brief Get the module that holds this named metadata collection.
388 inline Module *getParent() { return Parent; }
389 inline const Module *getParent() const { return Parent; }
391 MDNode *getOperand(unsigned i) const;
392 unsigned getNumOperands() const;
393 void addOperand(MDNode *M);
394 StringRef getName() const;
395 void print(raw_ostream &ROS) const;
398 // ---------------------------------------------------------------------------
399 // Operand Iterator interface...
401 typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
402 op_iterator op_begin() { return op_iterator(this, 0); }
403 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
405 typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
406 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
407 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
409 inline iterator_range<op_iterator> operands() {
410 return iterator_range<op_iterator>(op_begin(), op_end());
412 inline iterator_range<const_op_iterator> operands() const {
413 return iterator_range<const_op_iterator>(op_begin(), op_end());
417 } // end llvm namespace