LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MDBuilder.h
Go to the documentation of this file.
1 //===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- 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 // This file defines the MDBuilder class, which is used as a convenient way to
11 // create LLVM metadata with a consistent and simplified interface.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_MDBUILDER_H
16 #define LLVM_IR_MDBUILDER_H
17 
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/Metadata.h"
21 
22 namespace llvm {
23 
24 class APInt;
25 class LLVMContext;
26 
27 class MDBuilder {
28  LLVMContext &Context;
29 
30 public:
31  MDBuilder(LLVMContext &context) : Context(context) {}
32 
33  /// \brief Return the given string as metadata.
35  return MDString::get(Context, Str);
36  }
37 
38  //===------------------------------------------------------------------===//
39  // FPMath metadata.
40  //===------------------------------------------------------------------===//
41 
42  /// \brief Return metadata with the given settings. The special value 0.0
43  /// for the Accuracy parameter indicates the default (maximal precision)
44  /// setting.
45  MDNode *createFPMath(float Accuracy) {
46  if (Accuracy == 0.0)
47  return 0;
48  assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
49  Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
50  return MDNode::get(Context, Op);
51  }
52 
53  //===------------------------------------------------------------------===//
54  // Prof metadata.
55  //===------------------------------------------------------------------===//
56 
57  /// \brief Return metadata containing two branch weights.
58  MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight) {
59  uint32_t Weights[] = { TrueWeight, FalseWeight };
60  return createBranchWeights(Weights);
61  }
62 
63  /// \brief Return metadata containing a number of branch weights.
65  assert(Weights.size() >= 2 && "Need at least two branch weights!");
66 
67  SmallVector<Value *, 4> Vals(Weights.size()+1);
68  Vals[0] = createString("branch_weights");
69 
70  Type *Int32Ty = Type::getInt32Ty(Context);
71  for (unsigned i = 0, e = Weights.size(); i != e; ++i)
72  Vals[i+1] = ConstantInt::get(Int32Ty, Weights[i]);
73 
74  return MDNode::get(Context, Vals);
75  }
76 
77  //===------------------------------------------------------------------===//
78  // Range metadata.
79  //===------------------------------------------------------------------===//
80 
81  /// \brief Return metadata describing the range [Lo, Hi).
82  MDNode *createRange(const APInt &Lo, const APInt &Hi) {
83  assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
84  // If the range is everything then it is useless.
85  if (Hi == Lo)
86  return 0;
87 
88  // Return the range [Lo, Hi).
89  Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
90  Value *Range[2] = { ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi) };
91  return MDNode::get(Context, Range);
92  }
93 
94 
95  //===------------------------------------------------------------------===//
96  // TBAA metadata.
97  //===------------------------------------------------------------------===//
98 
99  /// \brief Return metadata appropriate for a TBAA root node. Each returned
100  /// node is distinct from all other metadata and will never be identified
101  /// (uniqued) with anything else.
103  // To ensure uniqueness the root node is self-referential.
105  MDNode *Root = MDNode::get(Context, Dummy);
106  // At this point we have
107  // !0 = metadata !{} <- dummy
108  // !1 = metadata !{metadata !0} <- root
109  // Replace the dummy operand with the root node itself and delete the dummy.
110  Root->replaceOperandWith(0, Root);
112  // We now have
113  // !1 = metadata !{metadata !1} <- self-referential root
114  return Root;
115  }
116 
117  /// \brief Return metadata appropriate for a TBAA root node with the given
118  /// name. This may be identified (uniqued) with other roots with the same
119  /// name.
121  return MDNode::get(Context, createString(Name));
122  }
123 
124  /// \brief Return metadata for a non-root TBAA node with the given name,
125  /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
127  bool isConstant = false) {
128  if (isConstant) {
130  Value *Ops[3] = { createString(Name), Parent, Flags };
131  return MDNode::get(Context, Ops);
132  } else {
133  Value *Ops[2] = { createString(Name), Parent };
134  return MDNode::get(Context, Ops);
135  }
136  }
137 
139  uint64_t Offset;
140  uint64_t Size;
142  TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
143  Offset(Offset), Size(Size), TBAA(TBAA) {}
144  };
145 
146  /// \brief Return metadata for a tbaa.struct node with the given
147  /// struct field descriptions.
149  SmallVector<Value *, 4> Vals(Fields.size() * 3);
150  Type *Int64 = IntegerType::get(Context, 64);
151  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
152  Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
153  Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
154  Vals[i * 3 + 2] = Fields[i].TBAA;
155  }
156  return MDNode::get(Context, Vals);
157  }
158 
159  /// \brief Return metadata for a TBAA struct node in the type DAG
160  /// with the given name, a list of pairs (offset, field type in the type DAG).
162  ArrayRef<std::pair<MDNode*, uint64_t> > Fields) {
163  SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1);
164  Type *Int64 = IntegerType::get(Context, 64);
165  Ops[0] = createString(Name);
166  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
167  Ops[i * 2 + 1] = Fields[i].first;
168  Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
169  }
170  return MDNode::get(Context, Ops);
171  }
172 
173  /// \brief Return metadata for a TBAA scalar type node with the
174  /// given name, an offset and a parent in the TBAA type DAG.
176  uint64_t Offset = 0) {
178  Type *Int64 = IntegerType::get(Context, 64);
179  Ops[0] = createString(Name);
180  Ops[1] = Parent;
181  Ops[2] = ConstantInt::get(Int64, Offset);
182  return MDNode::get(Context, Ops);
183  }
184 
185  /// \brief Return metadata for a TBAA tag node with the given
186  /// base type, access type and offset relative to the base type.
187  MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
188  uint64_t Offset) {
189  Type *Int64 = IntegerType::get(Context, 64);
190  Value *Ops[3] = { BaseType, AccessType, ConstantInt::get(Int64, Offset) };
191  return MDNode::get(Context, Ops);
192  }
193 
194 };
195 
196 } // end namespace llvm
197 
198 #endif
MDNode * createTBAANode(StringRef Name, MDNode *Parent, bool isConstant=false)
Return metadata for a non-root TBAA node with the given name, parent in the TBAA tree, and value for 'pointsToConstantMemory'.
Definition: MDBuilder.h:126
void replaceOperandWith(unsigned i, Value *NewVal)
replaceOperandWith - Replace a specific operand.
Definition: Metadata.cpp:108
static void deleteTemporary(MDNode *N)
Definition: Metadata.cpp:292
static MDNode * getTemporary(LLVMContext &Context, ArrayRef< Value * > Vals)
Definition: Metadata.cpp:282
MDNode * createBranchWeights(ArrayRef< uint32_t > Weights)
Return metadata containing a number of branch weights.
Definition: MDBuilder.h:64
MDNode * createAnonymousTBAARoot()
Return metadata appropriate for a TBAA root node. Each returned node is distinct from all other metad...
Definition: MDBuilder.h:102
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:38
MDBuilder(LLVMContext &context)
Definition: MDBuilder.h:31
TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA)
Definition: MDBuilder.h:142
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.h:34
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.h:148
MDNode - a tuple of other values.
Definition: Metadata.h:69
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:242
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.h:58
static MDNode * get(LLVMContext &Context, ArrayRef< Value * > Vals)
Definition: Metadata.cpp:268
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:230
MDNode * createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, uint64_t Offset=0)
Return metadata for a TBAA scalar type node with the given name, an offset and a parent in the TBAA t...
Definition: MDBuilder.h:175
MDNode * createTBAAStructTypeNode(StringRef Name, ArrayRef< std::pair< MDNode *, uint64_t > > Fields)
Return metadata for a TBAA struct node in the type DAG with the given name, a list of pairs (offset...
Definition: MDBuilder.h:161
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings. The special value 0.0 for the Accuracy parameter indicates t...
Definition: MDBuilder.h:45
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:109
Type * Int32Ty
LLVM Constant Representation.
Definition: Constant.h:41
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.h:82
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1252
static IntegerType * get(LLVMContext &C, unsigned NumBits)
Get or create an IntegerType instance.
Definition: Type.cpp:305
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:492
static Constant * get(Type *Ty, double V)
Definition: Constants.cpp:557
Class for arbitrary precision integers.
Definition: APInt.h:75
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name. This may be identified (uniqued...
Definition: MDBuilder.h:120
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:241
MDNode * createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, uint64_t Offset)
Return metadata for a TBAA tag node with the given base type, access type and offset relative to the ...
Definition: MDBuilder.h:187
LLVM Value Representation.
Definition: Value.h:66