IR: Move NumOperands from User to Value, NFC
[oota-llvm.git] / include / llvm / IR / Metadata.h
1 //===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file
11 /// This file contains the declarations for metadata subclasses.
12 /// They represent the different flavors of metadata that live in LLVM.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_IR_METADATA_H
17 #define LLVM_IR_METADATA_H
18
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
26 namespace llvm {
27 class LLVMContext;
28 class Module;
29 template<typename ValueSubClass, typename ItemParentClass>
30   class SymbolTableListTraits;
31
32
33 enum LLVMConstants : uint32_t {
34   DEBUG_METADATA_VERSION = 2  // Current debug info version number.
35 };
36
37 //===----------------------------------------------------------------------===//
38 /// \brief A single uniqued string.
39 ///
40 /// These are used to efficiently contain a byte sequence for metadata.
41 /// MDString is always unnamed.
42 class MDString : public Value {
43   virtual void anchor();
44   MDString(const MDString &) LLVM_DELETED_FUNCTION;
45
46   explicit MDString(LLVMContext &C);
47 public:
48   static MDString *get(LLVMContext &Context, StringRef Str);
49   static MDString *get(LLVMContext &Context, const char *Str) {
50     return get(Context, Str ? StringRef(Str) : StringRef());
51   }
52
53   StringRef getString() const { return getName(); }
54
55   unsigned getLength() const { return (unsigned)getName().size(); }
56
57   typedef StringRef::iterator iterator;
58
59   /// \brief Pointer to the first byte of the string.
60   iterator begin() const { return getName().begin(); }
61
62   /// \brief Pointer to one byte past the end of the string.
63   iterator end() const { return getName().end(); }
64
65   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
66   static bool classof(const Value *V) {
67     return V->getValueID() == MDStringVal;
68   }
69 };
70
71 /// \brief A collection of metadata nodes that might be associated with a
72 /// memory access used by the alias-analysis infrastructure.
73 struct AAMDNodes {
74   explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
75                      MDNode *N = nullptr)
76       : TBAA(T), Scope(S), NoAlias(N) {}
77
78   bool operator==(const AAMDNodes &A) const {
79     return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
80   }
81
82   bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
83
84   LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
85
86   /// \brief The tag for type-based alias analysis.
87   MDNode *TBAA;
88
89   /// \brief The tag for alias scope specification (used with noalias).
90   MDNode *Scope;
91
92   /// \brief The tag specifying the noalias scope.
93   MDNode *NoAlias;
94 };
95
96 // Specialize DenseMapInfo for AAMDNodes.
97 template<>
98 struct DenseMapInfo<AAMDNodes> {
99   static inline AAMDNodes getEmptyKey() {
100     return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
101   }
102   static inline AAMDNodes getTombstoneKey() {
103     return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
104   }
105   static unsigned getHashValue(const AAMDNodes &Val) {
106     return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
107            DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
108            DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
109   }
110   static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
111     return LHS == RHS;
112   }
113 };
114
115 class MDNodeOperand;
116
117 //===----------------------------------------------------------------------===//
118 /// \brief A tuple of other values.
119 class MDNode : public Value, public FoldingSetNode {
120   MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
121   void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
122   friend class MDNodeOperand;
123   friend class LLVMContextImpl;
124   friend struct FoldingSetTrait<MDNode>;
125
126   /// \brief If the MDNode is uniqued cache the hash to speed up lookup.
127   unsigned Hash;
128
129   /// \brief Subclass data enums.
130   enum {
131     /// FunctionLocalBit - This bit is set if this MDNode is function local.
132     /// This is true when it (potentially transitively) contains a reference to
133     /// something in a function, like an argument, basicblock, or instruction.
134     FunctionLocalBit = 1 << 0,
135
136     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
137     /// have a null operand.
138     NotUniquedBit    = 1 << 1,
139
140     /// DestroyFlag - This bit is set by destroy() so the destructor can assert
141     /// that the node isn't being destroyed with a plain 'delete'.
142     DestroyFlag      = 1 << 2
143   };
144
145   /// \brief FunctionLocal enums.
146   enum FunctionLocalness {
147     FL_Unknown = -1,
148     FL_No = 0,
149     FL_Yes = 1
150   };
151
152   /// \brief Replace each instance of the given operand with a new value.
153   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
154   ~MDNode();
155
156   MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
157
158   static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
159                            FunctionLocalness FL, bool Insert = true);
160 public:
161   static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
162   /// \brief Construct MDNode with an explicit function-localness.
163   ///
164   /// Don't analyze Vals; trust isFunctionLocal.
165   static MDNode *getWhenValsUnresolved(LLVMContext &Context,
166                                        ArrayRef<Value*> Vals,
167                                        bool isFunctionLocal);
168
169   static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
170
171   /// \brief Return a temporary MDNode
172   ///
173   /// For use in constructing cyclic MDNode structures. A temporary MDNode is
174   /// not uniqued, may be RAUW'd, and must be manually deleted with
175   /// deleteTemporary.
176   static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
177
178   /// \brief Deallocate a node created by getTemporary.
179   ///
180   /// The node must not have any users.
181   static void deleteTemporary(MDNode *N);
182
183   /// \brief Replace a specific operand.
184   void replaceOperandWith(unsigned i, Value *NewVal);
185
186   /// \brief Return specified operand.
187   Value *getOperand(unsigned i) const LLVM_READONLY;
188
189   /// \brief Return number of MDNode operands.
190   unsigned getNumOperands() const { return NumOperands; }
191
192   /// \brief Return whether MDNode is local to a function.
193   bool isFunctionLocal() const {
194     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
195   }
196
197   /// \brief Return the first function-local operand's function.
198   ///
199   /// If this metadata is function-local and recursively has a function-local
200   /// operand, return the first such operand's parent function.  Otherwise,
201   /// return null. getFunction() should not be used for performance- critical
202   /// code because it recursively visits all the MDNode's operands.
203   const Function *getFunction() const;
204
205   /// \brief Calculate a unique identifier for this MDNode.
206   void Profile(FoldingSetNodeID &ID) const;
207
208   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
209   static bool classof(const Value *V) {
210     return V->getValueID() == MDNodeVal;
211   }
212
213   /// \brief Check whether MDNode is a vtable access.
214   bool isTBAAVtableAccess() const;
215
216   /// \brief Methods for metadata merging.
217   static MDNode *concatenate(MDNode *A, MDNode *B);
218   static MDNode *intersect(MDNode *A, MDNode *B);
219   static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
220   static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
221   static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
222   static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
223 private:
224   /// \brief Delete this node.  Only when there are no uses.
225   void destroy();
226
227   bool isNotUniqued() const {
228     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
229   }
230   void setIsNotUniqued();
231
232   // Shadow Value::setValueSubclassData with a private forwarding method so that
233   // any future subclasses cannot accidentally use it.
234   void setValueSubclassData(unsigned short D) {
235     Value::setValueSubclassData(D);
236   }
237 };
238
239 //===----------------------------------------------------------------------===//
240 /// \brief A tuple of MDNodes.
241 ///
242 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
243 /// to modules, have names, and contain lists of MDNodes.
244 class NamedMDNode : public ilist_node<NamedMDNode> {
245   friend class SymbolTableListTraits<NamedMDNode, Module>;
246   friend struct ilist_traits<NamedMDNode>;
247   friend class LLVMContextImpl;
248   friend class Module;
249   NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
250
251   std::string Name;
252   Module *Parent;
253   void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
254
255   void setParent(Module *M) { Parent = M; }
256
257   explicit NamedMDNode(const Twine &N);
258
259   template<class T1, class T2>
260   class op_iterator_impl :
261       public std::iterator<std::bidirectional_iterator_tag, T2> {
262     const NamedMDNode *Node;
263     unsigned Idx;
264     op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
265
266     friend class NamedMDNode;
267
268   public:
269     op_iterator_impl() : Node(nullptr), Idx(0) { }
270
271     bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
272     bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
273     op_iterator_impl &operator++() {
274       ++Idx;
275       return *this;
276     }
277     op_iterator_impl operator++(int) {
278       op_iterator_impl tmp(*this);
279       operator++();
280       return tmp;
281     }
282     op_iterator_impl &operator--() {
283       --Idx;
284       return *this;
285     }
286     op_iterator_impl operator--(int) {
287       op_iterator_impl tmp(*this);
288       operator--();
289       return tmp;
290     }
291
292     T1 operator*() const { return Node->getOperand(Idx); }
293   };
294
295 public:
296   /// \brief Drop all references and remove the node from parent module.
297   void eraseFromParent();
298
299   /// \brief Remove all uses and clear node vector.
300   void dropAllReferences();
301
302   ~NamedMDNode();
303
304   /// \brief Get the module that holds this named metadata collection.
305   inline Module *getParent() { return Parent; }
306   inline const Module *getParent() const { return Parent; }
307
308   MDNode *getOperand(unsigned i) const;
309   unsigned getNumOperands() const;
310   void addOperand(MDNode *M);
311   StringRef getName() const;
312   void print(raw_ostream &ROS) const;
313   void dump() const;
314
315   // ---------------------------------------------------------------------------
316   // Operand Iterator interface...
317   //
318   typedef op_iterator_impl<MDNode*, MDNode> op_iterator;
319   op_iterator op_begin() { return op_iterator(this, 0); }
320   op_iterator op_end()   { return op_iterator(this, getNumOperands()); }
321
322   typedef op_iterator_impl<const MDNode*, MDNode> const_op_iterator;
323   const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
324   const_op_iterator op_end()   const { return const_op_iterator(this, getNumOperands()); }
325
326   inline iterator_range<op_iterator>  operands() {
327     return iterator_range<op_iterator>(op_begin(), op_end());
328   }
329   inline iterator_range<const_op_iterator> operands() const {
330     return iterator_range<const_op_iterator>(op_begin(), op_end());
331   }
332 };
333
334 } // end llvm namespace
335
336 #endif