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