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 // TODO: Sink this into GenericMDNode. Can't do this until operands are
168 // allocated at the front (currently they're at the back).
171 /// \brief Subclass data enums.
173 /// FunctionLocalBit - This bit is set if this MDNode is function local.
174 /// This is true when it (potentially transitively) contains a reference to
175 /// something in a function, like an argument, basicblock, or instruction.
176 FunctionLocalBit = 1 << 0,
178 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
179 /// have a null operand.
180 NotUniquedBit = 1 << 1
183 /// \brief FunctionLocal enums.
184 enum FunctionLocalness {
190 /// \brief Replace each instance of the given operand with a new value.
191 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
193 MDNode(LLVMContext &C, unsigned ID, ArrayRef<Value *> Vals,
194 bool isFunctionLocal);
197 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
198 FunctionLocalness FL, bool Insert = true);
200 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
201 /// \brief Construct MDNode with an explicit function-localness.
203 /// Don't analyze Vals; trust isFunctionLocal.
204 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
205 ArrayRef<Value*> Vals,
206 bool isFunctionLocal);
208 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
210 /// \brief Return a temporary MDNode
212 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
213 /// not uniqued, may be RAUW'd, and must be manually deleted with
215 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
217 /// \brief Deallocate a node created by getTemporary.
219 /// The node must not have any users.
220 static void deleteTemporary(MDNode *N);
222 /// \brief Replace a specific operand.
223 void replaceOperandWith(unsigned i, Value *NewVal);
225 /// \brief Return specified operand.
226 Value *getOperand(unsigned i) const LLVM_READONLY;
228 /// \brief Return number of MDNode operands.
229 unsigned getNumOperands() const { return NumOperands; }
231 /// \brief Return whether MDNode is local to a function.
232 bool isFunctionLocal() const {
233 return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
236 /// \brief Return the first function-local operand's function.
238 /// If this metadata is function-local and recursively has a function-local
239 /// operand, return the first such operand's parent function. Otherwise,
240 /// return null. getFunction() should not be used for performance- critical
241 /// code because it recursively visits all the MDNode's operands.
242 const Function *getFunction() const;
244 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
245 static bool classof(const Value *V) {
246 return V->getValueID() == GenericMDNodeVal ||
247 V->getValueID() == MDNodeFwdDeclVal;
250 /// \brief Check whether MDNode is a vtable access.
251 bool isTBAAVtableAccess() const;
253 /// \brief Methods for metadata merging.
254 static MDNode *concatenate(MDNode *A, MDNode *B);
255 static MDNode *intersect(MDNode *A, MDNode *B);
256 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
257 static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
258 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
259 static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
262 bool isNotUniqued() const {
263 return (getSubclassDataFromValue() & NotUniquedBit) != 0;
265 void setIsNotUniqued();
267 // Shadow Value::setValueSubclassData with a private forwarding method so that
268 // any future subclasses cannot accidentally use it.
269 void setValueSubclassData(unsigned short D) {
270 Value::setValueSubclassData(D);
274 /// \brief Generic metadata node.
276 /// Generic metadata nodes, with opt-out support for uniquing.
278 /// Although nodes are uniqued by default, \a GenericMDNode has no support for
279 /// RAUW. If an operand change (due to RAUW or otherwise) causes a uniquing
280 /// collision, the uniquing bit is dropped.
282 /// TODO: Make uniquing opt-out (status: mandatory, sometimes dropped).
283 /// TODO: Drop support for RAUW.
284 class GenericMDNode : public MDNode {
286 friend class LLVMContextImpl;
288 GenericMDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
289 : MDNode(C, GenericMDNodeVal, Vals, isFunctionLocal) {}
292 void dropAllReferences();
295 /// \brief Get the hash, if any.
296 unsigned getHash() const { return Hash; }
298 static bool classof(const Value *V) {
299 return V->getValueID() == GenericMDNodeVal;
303 /// \brief Forward declaration of metadata.
305 /// Forward declaration of metadata, in the form of a metadata node. Unlike \a
306 /// GenericMDNode, this class has support for RAUW and is suitable for forward
308 class MDNodeFwdDecl : public MDNode {
311 MDNodeFwdDecl(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
312 : MDNode(C, MDNodeFwdDeclVal, Vals, isFunctionLocal) {}
316 static bool classof(const Value *V) {
317 return V->getValueID() == MDNodeFwdDeclVal;
321 //===----------------------------------------------------------------------===//
322 /// \brief A tuple of MDNodes.
324 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
325 /// to modules, have names, and contain lists of MDNodes.
327 /// TODO: Inherit from Metadata.
328 class NamedMDNode : public ilist_node<NamedMDNode> {
329 friend class SymbolTableListTraits<NamedMDNode, Module>;
330 friend struct ilist_traits<NamedMDNode>;
331 friend class LLVMContextImpl;
333 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
337 void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
339 void setParent(Module *M) { Parent = M; }
341 explicit NamedMDNode(const Twine &N);
343 template<class T1, class T2>
344 class op_iterator_impl :
345 public std::iterator<std::bidirectional_iterator_tag, T2> {
346 const NamedMDNode *Node;
348 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
350 friend class NamedMDNode;
353 op_iterator_impl() : Node(nullptr), Idx(0) { }
355 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
356 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
357 op_iterator_impl &operator++() {
361 op_iterator_impl operator++(int) {
362 op_iterator_impl tmp(*this);
366 op_iterator_impl &operator--() {
370 op_iterator_impl operator--(int) {
371 op_iterator_impl tmp(*this);
376 T1 operator*() const { return Node->getOperand(Idx); }
380 /// \brief Drop all references and remove the node from parent module.
381 void eraseFromParent();
383 /// \brief Remove all uses and clear node vector.
384 void dropAllReferences();
388 /// \brief Get the module that holds this named metadata collection.
389 inline Module *getParent() { return Parent; }
390 inline const Module *getParent() const { return Parent; }
392 MDNode *getOperand(unsigned i) const;
393 unsigned getNumOperands() const;
394 void addOperand(MDNode *M);
395 StringRef getName() const;
396 void print(raw_ostream &ROS) const;
399 // ---------------------------------------------------------------------------
400 // Operand Iterator interface...
402 typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
403 op_iterator op_begin() { return op_iterator(this, 0); }
404 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
406 typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
407 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
408 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
410 inline iterator_range<op_iterator> operands() {
411 return iterator_range<op_iterator>(op_begin(), op_end());
413 inline iterator_range<const_op_iterator> operands() const {
414 return iterator_range<const_op_iterator>(op_begin(), op_end());
418 } // end llvm namespace