1 //===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_MDBUILDER_H
16 #define LLVM_IR_MDBUILDER_H
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/Metadata.h"
31 MDBuilder(LLVMContext &context) : Context(context) {}
33 /// \brief Return the given string as metadata.
34 MDString *createString(StringRef Str) {
35 return MDString::get(Context, Str);
38 //===------------------------------------------------------------------===//
40 //===------------------------------------------------------------------===//
42 /// \brief Return metadata with the given settings. The special value 0.0
43 /// for the Accuracy parameter indicates the default (maximal precision)
45 MDNode *createFPMath(float Accuracy) {
48 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
49 Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
50 return MDNode::get(Context, Op);
53 //===------------------------------------------------------------------===//
55 //===------------------------------------------------------------------===//
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);
63 /// \brief Return metadata containing a number of branch weights.
64 MDNode *createBranchWeights(ArrayRef<uint32_t> Weights) {
65 assert(Weights.size() >= 2 && "Need at least two branch weights!");
67 SmallVector<Value *, 4> Vals(Weights.size()+1);
68 Vals[0] = createString("branch_weights");
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]);
74 return MDNode::get(Context, Vals);
77 //===------------------------------------------------------------------===//
79 //===------------------------------------------------------------------===//
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.
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);
95 //===------------------------------------------------------------------===//
97 //===------------------------------------------------------------------===//
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.
102 MDNode *createAnonymousTBAARoot() {
103 // To ensure uniqueness the root node is self-referential.
104 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>());
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);
111 MDNode::deleteTemporary(Dummy);
113 // !1 = metadata !{metadata !1} <- self-referential root
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
120 MDNode *createTBAARoot(StringRef Name) {
121 return MDNode::get(Context, createString(Name));
124 /// \brief Return metadata for a non-root TBAA node with the given name,
125 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
126 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
127 bool isConstant = false) {
129 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
130 Value *Ops[3] = { createString(Name), Parent, Flags };
131 return MDNode::get(Context, Ops);
133 Value *Ops[2] = { createString(Name), Parent };
134 return MDNode::get(Context, Ops);
138 struct TBAAStructField {
142 TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
143 Offset(Offset), Size(Size), TBAA(TBAA) {}
146 /// \brief Return metadata for a tbaa.struct node with the given
147 /// struct field descriptions.
148 MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
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;
156 return MDNode::get(Context, Vals);
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).
161 MDNode *createTBAAStructTypeNode(StringRef Name,
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);
170 return MDNode::get(Context, Ops);
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.
175 MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
176 uint64_t Offset = 0) {
177 SmallVector<Value *, 4> Ops(3);
178 Type *Int64 = IntegerType::get(Context, 64);
179 Ops[0] = createString(Name);
181 Ops[2] = ConstantInt::get(Int64, Offset);
182 return MDNode::get(Context, Ops);
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,
189 Type *Int64 = IntegerType::get(Context, 64);
190 Value *Ops[3] = { BaseType, AccessType, ConstantInt::get(Int64, Offset) };
191 return MDNode::get(Context, Ops);
196 } // end namespace llvm