IR: Take an LLVMContext in Metadata::Metadata()
[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 /// \brief Root of the metadata hierarchy.
38 ///
39 /// This is a root class for typeless data in the IR.
40 ///
41 /// TODO: Detach from the Value hierarchy.
42 class Metadata : public Value {
43 protected:
44   Metadata(LLVMContext &Context, unsigned ID);
45
46 public:
47   static bool classof(const Value *V) {
48     return V->getValueID() == MDNodeVal;
49   }
50 };
51
52 //===----------------------------------------------------------------------===//
53 /// \brief A single uniqued string.
54 ///
55 /// These are used to efficiently contain a byte sequence for metadata.
56 /// MDString is always unnamed.
57 ///
58 /// TODO: Inherit from Metadata.
59 class MDString : public Value {
60   friend class StringMapEntry<MDString>;
61
62   virtual void anchor();
63   MDString(const MDString &) LLVM_DELETED_FUNCTION;
64
65   explicit MDString(LLVMContext &C);
66
67   /// \brief Shadow Value::getName() to prevent its use.
68   StringRef getName() const LLVM_DELETED_FUNCTION;
69
70 public:
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());
74   }
75
76   StringRef getString() const;
77
78   unsigned getLength() const { return (unsigned)getString().size(); }
79
80   typedef StringRef::iterator iterator;
81
82   /// \brief Pointer to the first byte of the string.
83   iterator begin() const { return getString().begin(); }
84
85   /// \brief Pointer to one byte past the end of the string.
86   iterator end() const { return getString().end(); }
87
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;
91   }
92 };
93
94 /// \brief A collection of metadata nodes that might be associated with a
95 /// memory access used by the alias-analysis infrastructure.
96 struct AAMDNodes {
97   explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
98                      MDNode *N = nullptr)
99       : TBAA(T), Scope(S), NoAlias(N) {}
100
101   bool operator==(const AAMDNodes &A) const {
102     return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
103   }
104
105   bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
106
107   LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
108
109   /// \brief The tag for type-based alias analysis.
110   MDNode *TBAA;
111
112   /// \brief The tag for alias scope specification (used with noalias).
113   MDNode *Scope;
114
115   /// \brief The tag specifying the noalias scope.
116   MDNode *NoAlias;
117 };
118
119 // Specialize DenseMapInfo for AAMDNodes.
120 template<>
121 struct DenseMapInfo<AAMDNodes> {
122   static inline AAMDNodes getEmptyKey() {
123     return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
124   }
125   static inline AAMDNodes getTombstoneKey() {
126     return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
127   }
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);
132   }
133   static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
134     return LHS == RHS;
135   }
136 };
137
138 class MDNodeOperand;
139
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>;
148
149   /// \brief If the MDNode is uniqued cache the hash to speed up lookup.
150   unsigned Hash;
151
152   /// \brief Subclass data enums.
153   enum {
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,
158
159     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
160     /// have a null operand.
161     NotUniquedBit    = 1 << 1,
162
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'.
165     DestroyFlag      = 1 << 2
166   };
167
168   /// \brief FunctionLocal enums.
169   enum FunctionLocalness {
170     FL_Unknown = -1,
171     FL_No = 0,
172     FL_Yes = 1
173   };
174
175   /// \brief Replace each instance of the given operand with a new value.
176   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
177   ~MDNode();
178
179   MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
180
181   static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
182                            FunctionLocalness FL, bool Insert = true);
183 public:
184   static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
185   /// \brief Construct MDNode with an explicit function-localness.
186   ///
187   /// Don't analyze Vals; trust isFunctionLocal.
188   static MDNode *getWhenValsUnresolved(LLVMContext &Context,
189                                        ArrayRef<Value*> Vals,
190                                        bool isFunctionLocal);
191
192   static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
193
194   /// \brief Return a temporary MDNode
195   ///
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
198   /// deleteTemporary.
199   static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
200
201   /// \brief Deallocate a node created by getTemporary.
202   ///
203   /// The node must not have any users.
204   static void deleteTemporary(MDNode *N);
205
206   /// \brief Replace a specific operand.
207   void replaceOperandWith(unsigned i, Value *NewVal);
208
209   /// \brief Return specified operand.
210   Value *getOperand(unsigned i) const LLVM_READONLY;
211
212   /// \brief Return number of MDNode operands.
213   unsigned getNumOperands() const { return NumOperands; }
214
215   /// \brief Return whether MDNode is local to a function.
216   bool isFunctionLocal() const {
217     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
218   }
219
220   /// \brief Return the first function-local operand's function.
221   ///
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;
227
228   /// \brief Calculate a unique identifier for this MDNode.
229   void Profile(FoldingSetNodeID &ID) const;
230
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;
234   }
235
236   /// \brief Check whether MDNode is a vtable access.
237   bool isTBAAVtableAccess() const;
238
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);
246 private:
247   /// \brief Delete this node.  Only when there are no uses.
248   void destroy();
249
250   bool isNotUniqued() const {
251     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
252   }
253   void setIsNotUniqued();
254
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);
259   }
260 };
261
262 //===----------------------------------------------------------------------===//
263 /// \brief A tuple of MDNodes.
264 ///
265 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
266 /// to modules, have names, and contain lists of MDNodes.
267 ///
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;
273   friend class Module;
274   NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
275
276   std::string Name;
277   Module *Parent;
278   void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
279
280   void setParent(Module *M) { Parent = M; }
281
282   explicit NamedMDNode(const Twine &N);
283
284   template<class T1, class T2>
285   class op_iterator_impl :
286       public std::iterator<std::bidirectional_iterator_tag, T2> {
287     const NamedMDNode *Node;
288     unsigned Idx;
289     op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
290
291     friend class NamedMDNode;
292
293   public:
294     op_iterator_impl() : Node(nullptr), Idx(0) { }
295
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++() {
299       ++Idx;
300       return *this;
301     }
302     op_iterator_impl operator++(int) {
303       op_iterator_impl tmp(*this);
304       operator++();
305       return tmp;
306     }
307     op_iterator_impl &operator--() {
308       --Idx;
309       return *this;
310     }
311     op_iterator_impl operator--(int) {
312       op_iterator_impl tmp(*this);
313       operator--();
314       return tmp;
315     }
316
317     T1 operator*() const { return Node->getOperand(Idx); }
318   };
319
320 public:
321   /// \brief Drop all references and remove the node from parent module.
322   void eraseFromParent();
323
324   /// \brief Remove all uses and clear node vector.
325   void dropAllReferences();
326
327   ~NamedMDNode();
328
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; }
332
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;
338   void dump() const;
339
340   // ---------------------------------------------------------------------------
341   // Operand Iterator interface...
342   //
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()); }
346
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()); }
350
351   inline iterator_range<op_iterator>  operands() {
352     return iterator_range<op_iterator>(op_begin(), op_end());
353   }
354   inline iterator_range<const_op_iterator> operands() const {
355     return iterator_range<const_op_iterator>(op_begin(), op_end());
356   }
357 };
358
359 } // end llvm namespace
360
361 #endif