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