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/FoldingSet.h"
21#include "llvm/ADT/ilist_node.h"
22#include "llvm/ADT/iterator_range.h"
23#include "llvm/IR/Value.h"
24
25namespace llvm {
26class LLVMContext;
27class Module;
28template<typename ValueSubClass, typename ItemParentClass>
29  class SymbolTableListTraits;
30
31
32enum LLVMConstants : uint32_t {
33  DEBUG_METADATA_VERSION = 1  // Current debug info version number.
34};
35
36//===----------------------------------------------------------------------===//
37/// MDString - a single uniqued string.
38/// These are used to efficiently contain a byte sequence for metadata.
39/// MDString is always unnamed.
40class MDString : public Value {
41  virtual void anchor();
42  MDString(const MDString &) LLVM_DELETED_FUNCTION;
43
44  explicit MDString(LLVMContext &C);
45public:
46  static MDString *get(LLVMContext &Context, StringRef Str);
47  static MDString *get(LLVMContext &Context, const char *Str) {
48    return get(Context, Str ? StringRef(Str) : StringRef());
49  }
50
51  StringRef getString() const { return getName(); }
52
53  unsigned getLength() const { return (unsigned)getName().size(); }
54
55  typedef StringRef::iterator iterator;
56
57  /// begin() - Pointer to the first byte of the string.
58  iterator begin() const { return getName().begin(); }
59
60  /// end() - Pointer to one byte past the end of the string.
61  iterator end() const { return getName().end(); }
62
63  /// Methods for support type inquiry through isa, cast, and dyn_cast:
64  static bool classof(const Value *V) {
65    return V->getValueID() == MDStringVal;
66  }
67};
68
69
70class MDNodeOperand;
71
72//===----------------------------------------------------------------------===//
73/// MDNode - a tuple of other values.
74class MDNode : public Value, public FoldingSetNode {
75  MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
76  void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
77  friend class MDNodeOperand;
78  friend class LLVMContextImpl;
79  friend struct FoldingSetTrait<MDNode>;
80
81  /// Hash - If the MDNode is uniqued cache the hash to speed up lookup.
82  unsigned Hash;
83
84  /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
85  /// end of this MDNode.
86  unsigned NumOperands;
87
88  // Subclass data enums.
89  enum {
90    /// FunctionLocalBit - This bit is set if this MDNode is function local.
91    /// This is true when it (potentially transitively) contains a reference to
92    /// something in a function, like an argument, basicblock, or instruction.
93    FunctionLocalBit = 1 << 0,
94
95    /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
96    /// have a null operand.
97    NotUniquedBit    = 1 << 1,
98
99    /// DestroyFlag - This bit is set by destroy() so the destructor can assert
100    /// that the node isn't being destroyed with a plain 'delete'.
101    DestroyFlag      = 1 << 2
102  };
103
104  // FunctionLocal enums.
105  enum FunctionLocalness {
106    FL_Unknown = -1,
107    FL_No = 0,
108    FL_Yes = 1
109  };
110
111  /// replaceOperand - Replace each instance of F from the operand list of this
112  /// node with T.
113  void replaceOperand(MDNodeOperand *Op, Value *NewVal);
114  ~MDNode();
115
116  MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
117
118  static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
119                           FunctionLocalness FL, bool Insert = true);
120public:
121  // Constructors and destructors.
122  static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
123  // getWhenValsUnresolved - Construct MDNode determining function-localness
124  // from isFunctionLocal argument, not by analyzing Vals.
125  static MDNode *getWhenValsUnresolved(LLVMContext &Context,
126                                       ArrayRef<Value*> Vals,
127                                       bool isFunctionLocal);
128
129  static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
130
131  /// getTemporary - Return a temporary MDNode, for use in constructing
132  /// cyclic MDNode structures. A temporary MDNode is not uniqued,
133  /// may be RAUW'd, and must be manually deleted with deleteTemporary.
134  static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
135
136  /// deleteTemporary - Deallocate a node created by getTemporary. The
137  /// node must not have any users.
138  static void deleteTemporary(MDNode *N);
139
140  /// replaceOperandWith - Replace a specific operand.
141  void replaceOperandWith(unsigned i, Value *NewVal);
142
143  /// getOperand - Return specified operand.
144  Value *getOperand(unsigned i) const LLVM_READONLY;
145
146  /// getNumOperands - Return number of MDNode operands.
147  unsigned getNumOperands() const { return NumOperands; }
148
149  /// isFunctionLocal - Return whether MDNode is local to a function.
150  bool isFunctionLocal() const {
151    return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
152  }
153
154  // getFunction - If this metadata is function-local and recursively has a
155  // function-local operand, return the first such operand's parent function.
156  // Otherwise, return null. getFunction() should not be used for performance-
157  // critical code because it recursively visits all the MDNode's operands.
158  const Function *getFunction() const;
159
160  /// Profile - calculate a unique identifier for this MDNode to collapse
161  /// duplicates
162  void Profile(FoldingSetNodeID &ID) const;
163
164  /// Methods for support type inquiry through isa, cast, and dyn_cast:
165  static bool classof(const Value *V) {
166    return V->getValueID() == MDNodeVal;
167  }
168
169  /// Check whether MDNode is a vtable access.
170  bool isTBAAVtableAccess() const;
171
172  /// Methods for metadata merging.
173  static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
174  static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
175  static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
176private:
177  // destroy - Delete this node.  Only when there are no uses.
178  void destroy();
179
180  bool isNotUniqued() const {
181    return (getSubclassDataFromValue() & NotUniquedBit) != 0;
182  }
183  void setIsNotUniqued();
184
185  // Shadow Value::setValueSubclassData with a private forwarding method so that
186  // any future subclasses cannot accidentally use it.
187  void setValueSubclassData(unsigned short D) {
188    Value::setValueSubclassData(D);
189  }
190};
191
192//===----------------------------------------------------------------------===//
193/// NamedMDNode - a tuple of MDNodes. Despite its name, a NamedMDNode isn't
194/// itself an MDNode. NamedMDNodes belong to modules, have names, and contain
195/// lists of MDNodes.
196class NamedMDNode : public ilist_node<NamedMDNode> {
197  friend class SymbolTableListTraits<NamedMDNode, Module>;
198  friend struct ilist_traits<NamedMDNode>;
199  friend class LLVMContextImpl;
200  friend class Module;
201  NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
202
203  std::string Name;
204  Module *Parent;
205  void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
206
207  void setParent(Module *M) { Parent = M; }
208
209  explicit NamedMDNode(const Twine &N);
210
211  template<class T1, class T2>
212  class op_iterator_impl :
213      public std::iterator<std::bidirectional_iterator_tag, T2> {
214    const NamedMDNode *Node;
215    unsigned Idx;
216    op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
217
218    friend class NamedMDNode;
219
220  public:
221    op_iterator_impl() : Node(nullptr), Idx(0) { }
222
223    bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
224    bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
225    op_iterator_impl &operator++() {
226      ++Idx;
227      return *this;
228    }
229    op_iterator_impl operator++(int) {
230      op_iterator_impl tmp(*this);
231      operator++();
232      return tmp;
233    }
234    op_iterator_impl &operator--() {
235      --Idx;
236      return *this;
237    }
238    op_iterator_impl operator--(int) {
239      op_iterator_impl tmp(*this);
240      operator--();
241      return tmp;
242    }
243
244    T1 operator*() const { return Node->getOperand(Idx); }
245  };
246
247public:
248  /// eraseFromParent - Drop all references and remove the node from parent
249  /// module.
250  void eraseFromParent();
251
252  /// dropAllReferences - Remove all uses and clear node vector.
253  void dropAllReferences();
254
255  /// ~NamedMDNode - Destroy NamedMDNode.
256  ~NamedMDNode();
257
258  /// getParent - Get the module that holds this named metadata collection.
259  inline Module *getParent() { return Parent; }
260  inline const Module *getParent() const { return Parent; }
261
262  /// getOperand - Return specified operand.
263  MDNode *getOperand(unsigned i) const;
264
265  /// getNumOperands - Return the number of NamedMDNode operands.
266  unsigned getNumOperands() const;
267
268  /// addOperand - Add metadata operand.
269  void addOperand(MDNode *M);
270
271  /// getName - Return a constant reference to this named metadata's name.
272  StringRef getName() const;
273
274  /// print - Implement operator<< on NamedMDNode.
275  void print(raw_ostream &ROS) const;
276
277  /// dump() - Allow printing of NamedMDNodes from the debugger.
278  void dump() const;
279
280  // ---------------------------------------------------------------------------
281  // Operand Iterator interface...
282  //
283  typedef op_iterator_impl<MDNode*, MDNode> op_iterator;
284  op_iterator op_begin() { return op_iterator(this, 0); }
285  op_iterator op_end()   { return op_iterator(this, getNumOperands()); }
286
287  typedef op_iterator_impl<const MDNode*, MDNode> const_op_iterator;
288  const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
289  const_op_iterator op_end()   const { return const_op_iterator(this, getNumOperands()); }
290
291  inline iterator_range<op_iterator>  operands() {
292    return iterator_range<op_iterator>(op_begin(), op_end());
293  }
294  inline iterator_range<const_op_iterator> operands() const {
295    return iterator_range<const_op_iterator>(op_begin(), op_end());
296  }
297};
298
299} // end llvm namespace
300
301#endif
302