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