llvm.org GIT mirror llvm / 479151a
Move MDBuilder's methods out of line. Making them inline was a historical accident, they're neither hot nor templated. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206109 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 6 years ago
4 changed file(s) with 163 addition(s) and 102 deletion(s). Raw diff Collapse all Expand all
1414 #ifndef LLVM_IR_MDBUILDER_H
1515 #define LLVM_IR_MDBUILDER_H
1616
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Metadata.h"
17 #include "llvm/Support/DataTypes.h"
18 #include
2019
2120 namespace llvm {
2221
2322 class APInt;
23 template class ArrayRef;
2424 class LLVMContext;
25 class MDNode;
26 class MDString;
27 class StringRef;
2528
2629 class MDBuilder {
2730 LLVMContext &Context;
3033 MDBuilder(LLVMContext &context) : Context(context) {}
3134
3235 /// \brief Return the given string as metadata.
33 MDString *createString(StringRef Str) {
34 return MDString::get(Context, Str);
35 }
36 MDString *createString(StringRef Str);
3637
3738 //===------------------------------------------------------------------===//
3839 // FPMath metadata.
4142 /// \brief Return metadata with the given settings. The special value 0.0
4243 /// for the Accuracy parameter indicates the default (maximal precision)
4344 /// setting.
44 MDNode *createFPMath(float Accuracy) {
45 if (Accuracy == 0.0)
46 return 0;
47 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
48 Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
49 return MDNode::get(Context, Op);
50 }
45 MDNode *createFPMath(float Accuracy);
5146
5247 //===------------------------------------------------------------------===//
5348 // Prof metadata.
5449 //===------------------------------------------------------------------===//
5550
5651 /// \brief Return metadata containing two branch weights.
57 MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight) {
58 uint32_t Weights[] = { TrueWeight, FalseWeight };
59 return createBranchWeights(Weights);
60 }
52 MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
6153
6254 /// \brief Return metadata containing a number of branch weights.
63 MDNode *createBranchWeights(ArrayRef Weights) {
64 assert(Weights.size() >= 2 && "Need at least two branch weights!");
65
66 SmallVector Vals(Weights.size()+1);
67 Vals[0] = createString("branch_weights");
68
69 Type *Int32Ty = Type::getInt32Ty(Context);
70 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
71 Vals[i+1] = ConstantInt::get(Int32Ty, Weights[i]);
72
73 return MDNode::get(Context, Vals);
74 }
55 MDNode *createBranchWeights(ArrayRef Weights);
7556
7657 //===------------------------------------------------------------------===//
7758 // Range metadata.
7859 //===------------------------------------------------------------------===//
7960
8061 /// \brief Return metadata describing the range [Lo, Hi).
81 MDNode *createRange(const APInt &Lo, const APInt &Hi) {
82 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
83 // If the range is everything then it is useless.
84 if (Hi == Lo)
85 return 0;
86
87 // Return the range [Lo, Hi).
88 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
89 Value *Range[2] = { ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi) };
90 return MDNode::get(Context, Range);
91 }
92
62 MDNode *createRange(const APInt &Lo, const APInt &Hi);
9363
9464 //===------------------------------------------------------------------===//
9565 // TBAA metadata.
9868 /// \brief Return metadata appropriate for a TBAA root node. Each returned
9969 /// node is distinct from all other metadata and will never be identified
10070 /// (uniqued) with anything else.
101 MDNode *createAnonymousTBAARoot() {
102 // To ensure uniqueness the root node is self-referential.
103 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef());
104 MDNode *Root = MDNode::get(Context, Dummy);
105 // At this point we have
106 // !0 = metadata !{} <- dummy
107 // !1 = metadata !{metadata !0} <- root
108 // Replace the dummy operand with the root node itself and delete the dummy.
109 Root->replaceOperandWith(0, Root);
110 MDNode::deleteTemporary(Dummy);
111 // We now have
112 // !1 = metadata !{metadata !1} <- self-referential root
113 return Root;
114 }
71 MDNode *createAnonymousTBAARoot();
11572
11673 /// \brief Return metadata appropriate for a TBAA root node with the given
11774 /// name. This may be identified (uniqued) with other roots with the same
11875 /// name.
119 MDNode *createTBAARoot(StringRef Name) {
120 return MDNode::get(Context, createString(Name));
121 }
76 MDNode *createTBAARoot(StringRef Name);
12277
12378 /// \brief Return metadata for a non-root TBAA node with the given name,
12479 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
12580 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
126 bool isConstant = false) {
127 if (isConstant) {
128 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
129 Value *Ops[3] = { createString(Name), Parent, Flags };
130 return MDNode::get(Context, Ops);
131 } else {
132 Value *Ops[2] = { createString(Name), Parent };
133 return MDNode::get(Context, Ops);
134 }
135 }
81 bool isConstant = false);
13682
13783 struct TBAAStructField {
13884 uint64_t Offset;
14490
14591 /// \brief Return metadata for a tbaa.struct node with the given
14692 /// struct field descriptions.
147 MDNode *createTBAAStructNode(ArrayRef Fields) {
148 SmallVector Vals(Fields.size() * 3);
149 Type *Int64 = IntegerType::get(Context, 64);
150 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
151 Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
152 Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
153 Vals[i * 3 + 2] = Fields[i].TBAA;
154 }
155 return MDNode::get(Context, Vals);
156 }
93 MDNode *createTBAAStructNode(ArrayRef Fields);
15794
15895 /// \brief Return metadata for a TBAA struct node in the type DAG
15996 /// with the given name, a list of pairs (offset, field type in the type DAG).
160 MDNode *createTBAAStructTypeNode(StringRef Name,
161 ArrayRef > Fields) {
162 SmallVector Ops(Fields.size() * 2 + 1);
163 Type *Int64 = IntegerType::get(Context, 64);
164 Ops[0] = createString(Name);
165 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
166 Ops[i * 2 + 1] = Fields[i].first;
167 Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
168 }
169 return MDNode::get(Context, Ops);
170 }
97 MDNode *
98 createTBAAStructTypeNode(StringRef Name,
99 ArrayRef> Fields);
171100
172101 /// \brief Return metadata for a TBAA scalar type node with the
173102 /// given name, an offset and a parent in the TBAA type DAG.
174103 MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
175 uint64_t Offset = 0) {
176 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
177 Value *Ops[3] = { createString(Name), Parent, Off };
178 return MDNode::get(Context, Ops);
179 }
104 uint64_t Offset = 0);
180105
181106 /// \brief Return metadata for a TBAA tag node with the given
182107 /// base type, access type and offset relative to the base type.
183108 MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
184 uint64_t Offset) {
185 Type *Int64 = IntegerType::get(Context, 64);
186 Value *Ops[3] = { BaseType, AccessType, ConstantInt::get(Int64, Offset) };
187 return MDNode::get(Context, Ops);
188 }
189
109 uint64_t Offset);
190110 };
191111
192112 } // end namespace llvm
66 ConstantRange.cpp
77 Constants.cpp
88 Core.cpp
9 DiagnosticInfo.cpp
10 DiagnosticPrinter.cpp
119 DIBuilder.cpp
1210 DataLayout.cpp
1311 DebugInfo.cpp
1412 DebugLoc.cpp
13 DiagnosticInfo.cpp
14 DiagnosticPrinter.cpp
1515 Dominators.cpp
1616 Function.cpp
1717 GCOV.cpp
2727 LLVMContextImpl.cpp
2828 LeakDetector.cpp
2929 LegacyPassManager.cpp
30 MDBuilder.cpp
3031 Mangler.cpp
3132 Metadata.cpp
3233 Module.cpp
0 //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the MDBuilder class, which is used as a convenient way to
10 // create LLVM metadata with a consistent and simplified interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/IR/MDBuilder.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Metadata.h"
17 using namespace llvm;
18
19 MDString *MDBuilder::createString(StringRef Str) {
20 return MDString::get(Context, Str);
21 }
22
23 MDNode *MDBuilder::createFPMath(float Accuracy) {
24 if (Accuracy == 0.0)
25 return 0;
26 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
27 Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
28 return MDNode::get(Context, Op);
29 }
30
31 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
32 uint32_t FalseWeight) {
33 uint32_t Weights[] = {TrueWeight, FalseWeight};
34 return createBranchWeights(Weights);
35 }
36
37 MDNode *MDBuilder::createBranchWeights(ArrayRef Weights) {
38 assert(Weights.size() >= 2 && "Need at least two branch weights!");
39
40 SmallVector Vals(Weights.size() + 1);
41 Vals[0] = createString("branch_weights");
42
43 Type *Int32Ty = Type::getInt32Ty(Context);
44 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
45 Vals[i + 1] = ConstantInt::get(Int32Ty, Weights[i]);
46
47 return MDNode::get(Context, Vals);
48 }
49
50 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
51 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
52 // If the range is everything then it is useless.
53 if (Hi == Lo)
54 return nullptr;
55
56 // Return the range [Lo, Hi).
57 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
58 Value *Range[2] = {ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)};
59 return MDNode::get(Context, Range);
60 }
61
62 MDNode *MDBuilder::createAnonymousTBAARoot() {
63 // To ensure uniqueness the root node is self-referential.
64 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef());
65 MDNode *Root = MDNode::get(Context, Dummy);
66 // At this point we have
67 // !0 = metadata !{} <- dummy
68 // !1 = metadata !{metadata !0} <- root
69 // Replace the dummy operand with the root node itself and delete the dummy.
70 Root->replaceOperandWith(0, Root);
71 MDNode::deleteTemporary(Dummy);
72 // We now have
73 // !1 = metadata !{metadata !1} <- self-referential root
74 return Root;
75 }
76
77 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
78 return MDNode::get(Context, createString(Name));
79 }
80
81 /// \brief Return metadata for a non-root TBAA node with the given name,
82 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
83 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
84 bool isConstant) {
85 if (isConstant) {
86 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
87 Value *Ops[3] = {createString(Name), Parent, Flags};
88 return MDNode::get(Context, Ops);
89 } else {
90 Value *Ops[2] = {createString(Name), Parent};
91 return MDNode::get(Context, Ops);
92 }
93 }
94
95 /// \brief Return metadata for a tbaa.struct node with the given
96 /// struct field descriptions.
97 MDNode *MDBuilder::createTBAAStructNode(ArrayRef Fields) {
98 SmallVector Vals(Fields.size() * 3);
99 Type *Int64 = Type::getInt64Ty(Context);
100 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
101 Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
102 Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
103 Vals[i * 3 + 2] = Fields[i].TBAA;
104 }
105 return MDNode::get(Context, Vals);
106 }
107
108 /// \brief Return metadata for a TBAA struct node in the type DAG
109 /// with the given name, a list of pairs (offset, field type in the type DAG).
110 MDNode *MDBuilder::createTBAAStructTypeNode(
111 StringRef Name, ArrayRef> Fields) {
112 SmallVector Ops(Fields.size() * 2 + 1);
113 Type *Int64 = Type::getInt64Ty(Context);
114 Ops[0] = createString(Name);
115 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
116 Ops[i * 2 + 1] = Fields[i].first;
117 Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
118 }
119 return MDNode::get(Context, Ops);
120 }
121
122 /// \brief Return metadata for a TBAA scalar type node with the
123 /// given name, an offset and a parent in the TBAA type DAG.
124 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
125 uint64_t Offset) {
126 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
127 Value *Ops[3] = {createString(Name), Parent, Off};
128 return MDNode::get(Context, Ops);
129 }
130
131 /// \brief Return metadata for a TBAA tag node with the given
132 /// base type, access type and offset relative to the base type.
133 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
134 uint64_t Offset) {
135 Type *Int64 = Type::getInt64Ty(Context);
136 Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)};
137 return MDNode::get(Context, Ops);
138 }
88
99 #include "llvm/IR/MDBuilder.h"
1010 #include "llvm/IR/IRBuilder.h"
11 #include "llvm/IR/Metadata.h"
1112 #include "llvm/IR/Operator.h"
1213 #include "gtest/gtest.h"
1314