IR: Move MDNode operands from the back to the front
[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 #include "llvm/Support/ErrorHandling.h"
26
27 namespace llvm {
28 class LLVMContext;
29 class Module;
30 template<typename ValueSubClass, typename ItemParentClass>
31   class SymbolTableListTraits;
32
33
34 enum LLVMConstants : uint32_t {
35   DEBUG_METADATA_VERSION = 2  // Current debug info version number.
36 };
37
38 /// \brief Root of the metadata hierarchy.
39 ///
40 /// This is a root class for typeless data in the IR.
41 ///
42 /// TODO: Detach from the Value hierarchy.
43 class Metadata : public Value {
44 protected:
45   Metadata(LLVMContext &Context, unsigned ID);
46
47 public:
48   static bool classof(const Value *V) {
49     return V->getValueID() == GenericMDNodeVal ||
50            V->getValueID() == MDNodeFwdDeclVal ||
51            V->getValueID() == MDStringVal;
52   }
53 };
54
55 //===----------------------------------------------------------------------===//
56 /// \brief A single uniqued string.
57 ///
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>;
62
63   virtual void anchor();
64   MDString(const MDString &) LLVM_DELETED_FUNCTION;
65
66   explicit MDString(LLVMContext &Context)
67       : Metadata(Context, Value::MDStringVal) {}
68
69   /// \brief Shadow Value::getName() to prevent its use.
70   StringRef getName() const LLVM_DELETED_FUNCTION;
71
72 public:
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());
76   }
77
78   StringRef getString() const;
79
80   unsigned getLength() const { return (unsigned)getString().size(); }
81
82   typedef StringRef::iterator iterator;
83
84   /// \brief Pointer to the first byte of the string.
85   iterator begin() const { return getString().begin(); }
86
87   /// \brief Pointer to one byte past the end of the string.
88   iterator end() const { return getString().end(); }
89
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;
93   }
94 };
95
96 /// \brief A collection of metadata nodes that might be associated with a
97 /// memory access used by the alias-analysis infrastructure.
98 struct AAMDNodes {
99   explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
100                      MDNode *N = nullptr)
101       : TBAA(T), Scope(S), NoAlias(N) {}
102
103   bool operator==(const AAMDNodes &A) const {
104     return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
105   }
106
107   bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
108
109   LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
110
111   /// \brief The tag for type-based alias analysis.
112   MDNode *TBAA;
113
114   /// \brief The tag for alias scope specification (used with noalias).
115   MDNode *Scope;
116
117   /// \brief The tag specifying the noalias scope.
118   MDNode *NoAlias;
119 };
120
121 // Specialize DenseMapInfo for AAMDNodes.
122 template<>
123 struct DenseMapInfo<AAMDNodes> {
124   static inline AAMDNodes getEmptyKey() {
125     return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
126   }
127   static inline AAMDNodes getTombstoneKey() {
128     return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
129   }
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);
134   }
135   static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
136     return LHS == RHS;
137   }
138 };
139
140 class MDNodeOperand;
141
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;
150
151 protected:
152   void *operator new(size_t Size, unsigned NumOps);
153
154   /// \brief Required by std, but never called.
155   void operator delete(void *Mem);
156
157   /// \brief Required by std, but never called.
158   void operator delete(void *, unsigned) {
159     llvm_unreachable("Constructor throws?");
160   }
161
162   /// \brief Required by std, but never called.
163   void operator delete(void *, unsigned, bool) {
164     llvm_unreachable("Constructor throws?");
165   }
166
167   // TODO: Sink this into GenericMDNode.  Can't do this until operands are
168   // allocated at the front (currently they're at the back).
169   unsigned Hash;
170
171   /// \brief Subclass data enums.
172   enum {
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,
177
178     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
179     /// have a null operand.
180     NotUniquedBit    = 1 << 1
181   };
182
183   /// \brief FunctionLocal enums.
184   enum FunctionLocalness {
185     FL_Unknown = -1,
186     FL_No = 0,
187     FL_Yes = 1
188   };
189
190   /// \brief Replace each instance of the given operand with a new value.
191   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
192
193   MDNode(LLVMContext &C, unsigned ID, ArrayRef<Value *> Vals,
194          bool isFunctionLocal);
195   ~MDNode() {}
196
197   static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
198                            FunctionLocalness FL, bool Insert = true);
199 public:
200   static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
201   /// \brief Construct MDNode with an explicit function-localness.
202   ///
203   /// Don't analyze Vals; trust isFunctionLocal.
204   static MDNode *getWhenValsUnresolved(LLVMContext &Context,
205                                        ArrayRef<Value*> Vals,
206                                        bool isFunctionLocal);
207
208   static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
209
210   /// \brief Return a temporary MDNode
211   ///
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
214   /// deleteTemporary.
215   static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
216
217   /// \brief Deallocate a node created by getTemporary.
218   ///
219   /// The node must not have any users.
220   static void deleteTemporary(MDNode *N);
221
222   /// \brief Replace a specific operand.
223   void replaceOperandWith(unsigned i, Value *NewVal);
224
225   /// \brief Return specified operand.
226   Value *getOperand(unsigned i) const LLVM_READONLY;
227
228   /// \brief Return number of MDNode operands.
229   unsigned getNumOperands() const { return NumOperands; }
230
231   /// \brief Return whether MDNode is local to a function.
232   bool isFunctionLocal() const {
233     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
234   }
235
236   /// \brief Return the first function-local operand's function.
237   ///
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;
243
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;
248   }
249
250   /// \brief Check whether MDNode is a vtable access.
251   bool isTBAAVtableAccess() const;
252
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);
260
261 protected:
262   bool isNotUniqued() const {
263     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
264   }
265   void setIsNotUniqued();
266
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);
271   }
272 };
273
274 /// \brief Generic metadata node.
275 ///
276 /// Generic metadata nodes, with opt-out support for uniquing.
277 ///
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.
281 ///
282 /// TODO: Make uniquing opt-out (status: mandatory, sometimes dropped).
283 /// TODO: Drop support for RAUW.
284 class GenericMDNode : public MDNode {
285   friend class MDNode;
286   friend class LLVMContextImpl;
287
288   GenericMDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
289       : MDNode(C, GenericMDNodeVal, Vals, isFunctionLocal) {}
290   ~GenericMDNode();
291
292   void dropAllReferences();
293
294 public:
295   /// \brief Get the hash, if any.
296   unsigned getHash() const { return Hash; }
297
298   static bool classof(const Value *V) {
299     return V->getValueID() == GenericMDNodeVal;
300   }
301 };
302
303 /// \brief Forward declaration of metadata.
304 ///
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
307 /// references.
308 class MDNodeFwdDecl : public MDNode {
309   friend class MDNode;
310
311   MDNodeFwdDecl(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
312       : MDNode(C, MDNodeFwdDeclVal, Vals, isFunctionLocal) {}
313   ~MDNodeFwdDecl() {}
314
315 public:
316   static bool classof(const Value *V) {
317     return V->getValueID() == MDNodeFwdDeclVal;
318   }
319 };
320
321 //===----------------------------------------------------------------------===//
322 /// \brief A tuple of MDNodes.
323 ///
324 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
325 /// to modules, have names, and contain lists of MDNodes.
326 ///
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;
332   friend class Module;
333   NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
334
335   std::string Name;
336   Module *Parent;
337   void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
338
339   void setParent(Module *M) { Parent = M; }
340
341   explicit NamedMDNode(const Twine &N);
342
343   template<class T1, class T2>
344   class op_iterator_impl :
345       public std::iterator<std::bidirectional_iterator_tag, T2> {
346     const NamedMDNode *Node;
347     unsigned Idx;
348     op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
349
350     friend class NamedMDNode;
351
352   public:
353     op_iterator_impl() : Node(nullptr), Idx(0) { }
354
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++() {
358       ++Idx;
359       return *this;
360     }
361     op_iterator_impl operator++(int) {
362       op_iterator_impl tmp(*this);
363       operator++();
364       return tmp;
365     }
366     op_iterator_impl &operator--() {
367       --Idx;
368       return *this;
369     }
370     op_iterator_impl operator--(int) {
371       op_iterator_impl tmp(*this);
372       operator--();
373       return tmp;
374     }
375
376     T1 operator*() const { return Node->getOperand(Idx); }
377   };
378
379 public:
380   /// \brief Drop all references and remove the node from parent module.
381   void eraseFromParent();
382
383   /// \brief Remove all uses and clear node vector.
384   void dropAllReferences();
385
386   ~NamedMDNode();
387
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; }
391
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;
397   void dump() const;
398
399   // ---------------------------------------------------------------------------
400   // Operand Iterator interface...
401   //
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()); }
405
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()); }
409
410   inline iterator_range<op_iterator>  operands() {
411     return iterator_range<op_iterator>(op_begin(), op_end());
412   }
413   inline iterator_range<const_op_iterator> operands() const {
414     return iterator_range<const_op_iterator>(op_begin(), op_end());
415   }
416 };
417
418 } // end llvm namespace
419
420 #endif