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