LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Metadata.h
Go to the documentation of this file.
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 
24 namespace llvm {
25 class LLVMContext;
26 class Module;
27 template<typename ValueSubClass, typename ItemParentClass>
28  class SymbolTableListTraits;
29 
30 
31 //===----------------------------------------------------------------------===//
32 /// MDString - a single uniqued string.
33 /// These are used to efficiently contain a byte sequence for metadata.
34 /// MDString is always unnamed.
35 class MDString : public Value {
36  virtual void anchor();
38 
39  explicit MDString(LLVMContext &C);
40 public:
41  static MDString *get(LLVMContext &Context, StringRef Str);
42  static MDString *get(LLVMContext &Context, const char *Str) {
43  return get(Context, Str ? StringRef(Str) : StringRef());
44  }
45 
46  StringRef getString() const { return getName(); }
47 
48  unsigned getLength() const { return (unsigned)getName().size(); }
49 
51 
52  /// begin() - Pointer to the first byte of the string.
53  iterator begin() const { return getName().begin(); }
54 
55  /// end() - Pointer to one byte past the end of the string.
56  iterator end() const { return getName().end(); }
57 
58  /// Methods for support type inquiry through isa, cast, and dyn_cast:
59  static bool classof(const Value *V) {
60  return V->getValueID() == MDStringVal;
61  }
62 };
63 
64 
65 class MDNodeOperand;
66 
67 //===----------------------------------------------------------------------===//
68 /// MDNode - a tuple of other values.
69 class MDNode : public Value, public FoldingSetNode {
71  void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
72  friend class MDNodeOperand;
73  friend class LLVMContextImpl;
74  friend struct FoldingSetTrait<MDNode>;
75 
76  /// Hash - If the MDNode is uniqued cache the hash to speed up lookup.
77  unsigned Hash;
78 
79  /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
80  /// end of this MDNode.
81  unsigned NumOperands;
82 
83  // Subclass data enums.
84  enum {
85  /// FunctionLocalBit - This bit is set if this MDNode is function local.
86  /// This is true when it (potentially transitively) contains a reference to
87  /// something in a function, like an argument, basicblock, or instruction.
88  FunctionLocalBit = 1 << 0,
89 
90  /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
91  /// have a null operand.
92  NotUniquedBit = 1 << 1,
93 
94  /// DestroyFlag - This bit is set by destroy() so the destructor can assert
95  /// that the node isn't being destroyed with a plain 'delete'.
96  DestroyFlag = 1 << 2
97  };
98 
99  // FunctionLocal enums.
100  enum FunctionLocalness {
101  FL_Unknown = -1,
102  FL_No = 0,
103  FL_Yes = 1
104  };
105 
106  /// replaceOperand - Replace each instance of F from the operand list of this
107  /// node with T.
108  void replaceOperand(MDNodeOperand *Op, Value *NewVal);
109  ~MDNode();
110 
111  MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
112 
113  static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
114  FunctionLocalness FL, bool Insert = true);
115 public:
116  // Constructors and destructors.
117  static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
118  // getWhenValsUnresolved - Construct MDNode determining function-localness
119  // from isFunctionLocal argument, not by analyzing Vals.
120  static MDNode *getWhenValsUnresolved(LLVMContext &Context,
121  ArrayRef<Value*> Vals,
122  bool isFunctionLocal);
123 
124  static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
125 
126  /// getTemporary - Return a temporary MDNode, for use in constructing
127  /// cyclic MDNode structures. A temporary MDNode is not uniqued,
128  /// may be RAUW'd, and must be manually deleted with deleteTemporary.
129  static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
130 
131  /// deleteTemporary - Deallocate a node created by getTemporary. The
132  /// node must not have any users.
133  static void deleteTemporary(MDNode *N);
134 
135  /// replaceOperandWith - Replace a specific operand.
136  void replaceOperandWith(unsigned i, Value *NewVal);
137 
138  /// getOperand - Return specified operand.
139  Value *getOperand(unsigned i) const LLVM_READONLY;
140 
141  /// getNumOperands - Return number of MDNode operands.
142  unsigned getNumOperands() const { return NumOperands; }
143 
144  /// isFunctionLocal - Return whether MDNode is local to a function.
145  bool isFunctionLocal() const {
146  return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
147  }
148 
149  // getFunction - If this metadata is function-local and recursively has a
150  // function-local operand, return the first such operand's parent function.
151  // Otherwise, return null. getFunction() should not be used for performance-
152  // critical code because it recursively visits all the MDNode's operands.
153  const Function *getFunction() const;
154 
155  /// Profile - calculate a unique identifier for this MDNode to collapse
156  /// duplicates
157  void Profile(FoldingSetNodeID &ID) const;
158 
159  /// Methods for support type inquiry through isa, cast, and dyn_cast:
160  static bool classof(const Value *V) {
161  return V->getValueID() == MDNodeVal;
162  }
163 
164  /// Check whether MDNode is a vtable access.
165  bool isTBAAVtableAccess() const;
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);
171 private:
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) {
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.
191 class NamedMDNode : public ilist_node<NamedMDNode> {
193  friend struct ilist_traits<NamedMDNode>;
194  friend class LLVMContextImpl;
195  friend class Module;
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 
206 public:
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
StringRef getName() const
getName - Return a constant reference to this named metadata's name.
Definition: Metadata.cpp:569
void replaceOperandWith(unsigned i, Value *NewVal)
replaceOperandWith - Replace a specific operand.
Definition: Metadata.cpp:108
static void deleteTemporary(MDNode *N)
Definition: Metadata.cpp:292
unsigned getLength() const
Definition: Metadata.h:48
StringRef getString() const
Definition: Metadata.h:46
static MDNode * getTemporary(LLVMContext &Context, ArrayRef< Value * > Vals)
Definition: Metadata.cpp:282
const Module * getParent() const
Definition: Metadata.h:219
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
unsigned getNumOperands() const
getNumOperands - Return number of MDNode operands.
Definition: Metadata.h:142
void addOperand(MDNode *M)
addOperand - Add metadata operand.
Definition: Metadata.cpp:551
MDNode - a tuple of other values.
Definition: Metadata.h:69
StringRef getName() const
Definition: Value.cpp:167
Value * getOperand(unsigned i) const LLVM_READONLY
getOperand - Return specified operand.
Definition: Metadata.cpp:307
iterator end() const
end() - Pointer to one byte past the end of the string.
Definition: Metadata.h:56
void eraseFromParent()
Definition: Metadata.cpp:559
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
bool isTBAAVtableAccess() const
Check whether MDNode is a vtable access.
StringRef::iterator iterator
Definition: Metadata.h:50
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Metadata.h:59
iterator begin() const
Definition: StringRef.h:97
static MDNode * getMostGenericRange(MDNode *A, MDNode *B)
Definition: Metadata.cpp:454
void Profile(FoldingSetNodeID &ID) const
Definition: Metadata.cpp:312
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Metadata.h:160
MDNode * getOperand(unsigned i) const
getOperand - Return specified operand.
Definition: Metadata.cpp:545
unsigned getValueID() const
Definition: Value.h:233
static MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)
Methods for metadata merging.
const char * iterator
Definition: StringRef.h:43
static MDNode * getWhenValsUnresolved(LLVMContext &Context, ArrayRef< Value * > Vals, bool isFunctionLocal)
Definition: Metadata.cpp:272
void print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW=0) const
print - Implement operator<< on NamedMDNode.
Definition: AsmWriter.cpp:2139
static MDNode * getIfExists(LLVMContext &Context, ArrayRef< Value * > Vals)
Definition: Metadata.cpp:278
void setValueSubclassData(unsigned short D)
Definition: Value.h:348
#define LLVM_DELETED_FUNCTION
Definition: Compiler.h:137
friend class MDNodeOperand
Definition: Metadata.h:72
bool isFunctionLocal() const
isFunctionLocal - Return whether MDNode is local to a function.
Definition: Metadata.h:145
unsigned short getSubclassDataFromValue() const
Definition: Value.h:347
iterator begin() const
begin() - Pointer to the first byte of the string.
Definition: Metadata.h:53
#define N
#define LLVM_READONLY
Definition: Compiler.h:223
~NamedMDNode()
~NamedMDNode - Destroy NamedMDNode.
Definition: Metadata.cpp:534
LLVM Value Representation.
Definition: Value.h:66
iterator end() const
Definition: StringRef.h:99
unsigned getNumOperands() const
getNumOperands - Return the number of NamedMDNode operands.
Definition: Metadata.cpp:540
void dropAllReferences()
dropAllReferences - Remove all uses and clear node vector.
Definition: Metadata.cpp:564
static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
Definition: Metadata.cpp:408
void dump() const
dump() - Allow printing of NamedMDNodes from the debugger.
Definition: AsmWriter.cpp:2221
const Function * getFunction() const
Definition: Metadata.cpp:190
Module * getParent()
getParent - Get the module that holds this named metadata collection.
Definition: Metadata.h:218