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