IR: Make MDString inherit from 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 || 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, public FoldingSetNode {
142   MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
143   void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
144   friend class MDNodeOperand;
145   friend class LLVMContextImpl;
146   friend struct FoldingSetTrait<MDNode>;
147
148   /// \brief If the MDNode is uniqued cache the hash to speed up lookup.
149   unsigned Hash;
150
151   /// \brief Subclass data enums.
152   enum {
153     /// FunctionLocalBit - This bit is set if this MDNode is function local.
154     /// This is true when it (potentially transitively) contains a reference to
155     /// something in a function, like an argument, basicblock, or instruction.
156     FunctionLocalBit = 1 << 0,
157
158     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
159     /// have a null operand.
160     NotUniquedBit    = 1 << 1,
161
162     /// DestroyFlag - This bit is set by destroy() so the destructor can assert
163     /// that the node isn't being destroyed with a plain 'delete'.
164     DestroyFlag      = 1 << 2
165   };
166
167   /// \brief FunctionLocal enums.
168   enum FunctionLocalness {
169     FL_Unknown = -1,
170     FL_No = 0,
171     FL_Yes = 1
172   };
173
174   /// \brief Replace each instance of the given operand with a new value.
175   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
176   ~MDNode();
177
178   MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
179
180   static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
181                            FunctionLocalness FL, bool Insert = true);
182 public:
183   static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
184   /// \brief Construct MDNode with an explicit function-localness.
185   ///
186   /// Don't analyze Vals; trust isFunctionLocal.
187   static MDNode *getWhenValsUnresolved(LLVMContext &Context,
188                                        ArrayRef<Value*> Vals,
189                                        bool isFunctionLocal);
190
191   static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
192
193   /// \brief Return a temporary MDNode
194   ///
195   /// For use in constructing cyclic MDNode structures. A temporary MDNode is
196   /// not uniqued, may be RAUW'd, and must be manually deleted with
197   /// deleteTemporary.
198   static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
199
200   /// \brief Deallocate a node created by getTemporary.
201   ///
202   /// The node must not have any users.
203   static void deleteTemporary(MDNode *N);
204
205   /// \brief Replace a specific operand.
206   void replaceOperandWith(unsigned i, Value *NewVal);
207
208   /// \brief Return specified operand.
209   Value *getOperand(unsigned i) const LLVM_READONLY;
210
211   /// \brief Return number of MDNode operands.
212   unsigned getNumOperands() const { return NumOperands; }
213
214   /// \brief Return whether MDNode is local to a function.
215   bool isFunctionLocal() const {
216     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
217   }
218
219   /// \brief Return the first function-local operand's function.
220   ///
221   /// If this metadata is function-local and recursively has a function-local
222   /// operand, return the first such operand's parent function.  Otherwise,
223   /// return null. getFunction() should not be used for performance- critical
224   /// code because it recursively visits all the MDNode's operands.
225   const Function *getFunction() const;
226
227   /// \brief Calculate a unique identifier for this MDNode.
228   void Profile(FoldingSetNodeID &ID) const;
229
230   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
231   static bool classof(const Value *V) {
232     return V->getValueID() == MDNodeVal;
233   }
234
235   /// \brief Check whether MDNode is a vtable access.
236   bool isTBAAVtableAccess() const;
237
238   /// \brief Methods for metadata merging.
239   static MDNode *concatenate(MDNode *A, MDNode *B);
240   static MDNode *intersect(MDNode *A, MDNode *B);
241   static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
242   static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
243   static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
244   static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
245 private:
246   /// \brief Delete this node.  Only when there are no uses.
247   void destroy();
248
249   bool isNotUniqued() const {
250     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
251   }
252   void setIsNotUniqued();
253
254   // Shadow Value::setValueSubclassData with a private forwarding method so that
255   // any future subclasses cannot accidentally use it.
256   void setValueSubclassData(unsigned short D) {
257     Value::setValueSubclassData(D);
258   }
259 };
260
261 //===----------------------------------------------------------------------===//
262 /// \brief A tuple of MDNodes.
263 ///
264 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
265 /// to modules, have names, and contain lists of MDNodes.
266 ///
267 /// TODO: Inherit from Metadata.
268 class NamedMDNode : public ilist_node<NamedMDNode> {
269   friend class SymbolTableListTraits<NamedMDNode, Module>;
270   friend struct ilist_traits<NamedMDNode>;
271   friend class LLVMContextImpl;
272   friend class Module;
273   NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
274
275   std::string Name;
276   Module *Parent;
277   void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
278
279   void setParent(Module *M) { Parent = M; }
280
281   explicit NamedMDNode(const Twine &N);
282
283   template<class T1, class T2>
284   class op_iterator_impl :
285       public std::iterator<std::bidirectional_iterator_tag, T2> {
286     const NamedMDNode *Node;
287     unsigned Idx;
288     op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
289
290     friend class NamedMDNode;
291
292   public:
293     op_iterator_impl() : Node(nullptr), Idx(0) { }
294
295     bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
296     bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
297     op_iterator_impl &operator++() {
298       ++Idx;
299       return *this;
300     }
301     op_iterator_impl operator++(int) {
302       op_iterator_impl tmp(*this);
303       operator++();
304       return tmp;
305     }
306     op_iterator_impl &operator--() {
307       --Idx;
308       return *this;
309     }
310     op_iterator_impl operator--(int) {
311       op_iterator_impl tmp(*this);
312       operator--();
313       return tmp;
314     }
315
316     T1 operator*() const { return Node->getOperand(Idx); }
317   };
318
319 public:
320   /// \brief Drop all references and remove the node from parent module.
321   void eraseFromParent();
322
323   /// \brief Remove all uses and clear node vector.
324   void dropAllReferences();
325
326   ~NamedMDNode();
327
328   /// \brief Get the module that holds this named metadata collection.
329   inline Module *getParent() { return Parent; }
330   inline const Module *getParent() const { return Parent; }
331
332   MDNode *getOperand(unsigned i) const;
333   unsigned getNumOperands() const;
334   void addOperand(MDNode *M);
335   StringRef getName() const;
336   void print(raw_ostream &ROS) const;
337   void dump() const;
338
339   // ---------------------------------------------------------------------------
340   // Operand Iterator interface...
341   //
342   typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
343   op_iterator op_begin() { return op_iterator(this, 0); }
344   op_iterator op_end()   { return op_iterator(this, getNumOperands()); }
345
346   typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
347   const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
348   const_op_iterator op_end()   const { return const_op_iterator(this, getNumOperands()); }
349
350   inline iterator_range<op_iterator>  operands() {
351     return iterator_range<op_iterator>(op_begin(), op_end());
352   }
353   inline iterator_range<const_op_iterator> operands() const {
354     return iterator_range<const_op_iterator>(op_begin(), op_end());
355   }
356 };
357
358 } // end llvm namespace
359
360 #endif