1 //===- llvm/unittests/MDBuilderTest.cpp - MDBuilder unit tests ------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/MDBuilder.h"
10 #include "llvm/IR/Constants.h"
11 #include "llvm/IR/IRBuilder.h"
12 #include "llvm/IR/Metadata.h"
13 #include "llvm/IR/Operator.h"
14 #include "gtest/gtest.h"
20 class MDBuilderTest
: public testing::Test
{
25 TEST_F(MDBuilderTest
, createString
) {
26 MDBuilder
MDHelper(Context
);
27 MDString
*Str0
= MDHelper
.createString("");
28 MDString
*Str1
= MDHelper
.createString("string");
29 EXPECT_EQ(Str0
->getString(), StringRef(""));
30 EXPECT_EQ(Str1
->getString(), StringRef("string"));
32 TEST_F(MDBuilderTest
, createFPMath
) {
33 MDBuilder
MDHelper(Context
);
34 MDNode
*MD0
= MDHelper
.createFPMath(0.0);
35 MDNode
*MD1
= MDHelper
.createFPMath(1.0);
36 EXPECT_EQ(MD0
, (MDNode
*)nullptr);
37 EXPECT_NE(MD1
, (MDNode
*)nullptr);
38 EXPECT_EQ(MD1
->getNumOperands(), 1U);
39 Metadata
*Op
= MD1
->getOperand(0);
40 EXPECT_TRUE(mdconst::hasa
<ConstantFP
>(Op
));
41 ConstantFP
*Val
= mdconst::extract
<ConstantFP
>(Op
);
42 EXPECT_TRUE(Val
->getType()->isFloatingPointTy());
43 EXPECT_TRUE(Val
->isExactlyValue(1.0));
45 TEST_F(MDBuilderTest
, createRangeMetadata
) {
46 MDBuilder
MDHelper(Context
);
47 APInt
A(8, 1), B(8, 2);
48 MDNode
*R0
= MDHelper
.createRange(A
, A
);
49 MDNode
*R1
= MDHelper
.createRange(A
, B
);
50 EXPECT_EQ(R0
, (MDNode
*)nullptr);
51 EXPECT_NE(R1
, (MDNode
*)nullptr);
52 EXPECT_EQ(R1
->getNumOperands(), 2U);
53 EXPECT_TRUE(mdconst::hasa
<ConstantInt
>(R1
->getOperand(0)));
54 EXPECT_TRUE(mdconst::hasa
<ConstantInt
>(R1
->getOperand(1)));
55 ConstantInt
*C0
= mdconst::extract
<ConstantInt
>(R1
->getOperand(0));
56 ConstantInt
*C1
= mdconst::extract
<ConstantInt
>(R1
->getOperand(1));
57 EXPECT_EQ(C0
->getValue(), A
);
58 EXPECT_EQ(C1
->getValue(), B
);
60 TEST_F(MDBuilderTest
, createAnonymousTBAARoot
) {
61 MDBuilder
MDHelper(Context
);
62 MDNode
*R0
= MDHelper
.createAnonymousTBAARoot();
63 MDNode
*R1
= MDHelper
.createAnonymousTBAARoot();
65 EXPECT_GE(R0
->getNumOperands(), 1U);
66 EXPECT_GE(R1
->getNumOperands(), 1U);
67 EXPECT_EQ(R0
->getOperand(0), R0
);
68 EXPECT_EQ(R1
->getOperand(0), R1
);
69 EXPECT_TRUE(R0
->getNumOperands() == 1 || R0
->getOperand(1) == nullptr);
70 EXPECT_TRUE(R1
->getNumOperands() == 1 || R1
->getOperand(1) == nullptr);
72 TEST_F(MDBuilderTest
, createTBAARoot
) {
73 MDBuilder
MDHelper(Context
);
74 MDNode
*R0
= MDHelper
.createTBAARoot("Root");
75 MDNode
*R1
= MDHelper
.createTBAARoot("Root");
77 EXPECT_GE(R0
->getNumOperands(), 1U);
78 EXPECT_TRUE(isa
<MDString
>(R0
->getOperand(0)));
79 EXPECT_EQ(cast
<MDString
>(R0
->getOperand(0))->getString(), "Root");
80 EXPECT_TRUE(R0
->getNumOperands() == 1 || R0
->getOperand(1) == nullptr);
82 TEST_F(MDBuilderTest
, createTBAANode
) {
83 MDBuilder
MDHelper(Context
);
84 MDNode
*R
= MDHelper
.createTBAARoot("Root");
85 MDNode
*N0
= MDHelper
.createTBAANode("Node", R
);
86 MDNode
*N1
= MDHelper
.createTBAANode("edoN", R
);
87 MDNode
*N2
= MDHelper
.createTBAANode("Node", R
, true);
88 MDNode
*N3
= MDHelper
.createTBAANode("Node", R
);
92 EXPECT_GE(N0
->getNumOperands(), 2U);
93 EXPECT_GE(N1
->getNumOperands(), 2U);
94 EXPECT_GE(N2
->getNumOperands(), 3U);
95 EXPECT_TRUE(isa
<MDString
>(N0
->getOperand(0)));
96 EXPECT_TRUE(isa
<MDString
>(N1
->getOperand(0)));
97 EXPECT_TRUE(isa
<MDString
>(N2
->getOperand(0)));
98 EXPECT_EQ(cast
<MDString
>(N0
->getOperand(0))->getString(), "Node");
99 EXPECT_EQ(cast
<MDString
>(N1
->getOperand(0))->getString(), "edoN");
100 EXPECT_EQ(cast
<MDString
>(N2
->getOperand(0))->getString(), "Node");
101 EXPECT_EQ(N0
->getOperand(1), R
);
102 EXPECT_EQ(N1
->getOperand(1), R
);
103 EXPECT_EQ(N2
->getOperand(1), R
);
104 EXPECT_TRUE(mdconst::hasa
<ConstantInt
>(N2
->getOperand(2)));
105 EXPECT_EQ(mdconst::extract
<ConstantInt
>(N2
->getOperand(2))->getZExtValue(),
108 TEST_F(MDBuilderTest
, createPCSections
) {
109 MDBuilder
MDHelper(Context
);
110 ConstantInt
*C1
= ConstantInt::get(Context
, APInt(8, 1));
111 ConstantInt
*C2
= ConstantInt::get(Context
, APInt(8, 2));
112 MDNode
*PCS
= MDHelper
.createPCSections({{"s1", {C1
, C2
}}, {"s2", {}}});
113 ASSERT_EQ(PCS
->getNumOperands(), 3U);
114 const auto *S1
= dyn_cast
<MDString
>(PCS
->getOperand(0));
115 const auto *Aux
= dyn_cast
<MDNode
>(PCS
->getOperand(1));
116 const auto *S2
= dyn_cast
<MDString
>(PCS
->getOperand(2));
117 ASSERT_NE(S1
, nullptr);
118 ASSERT_NE(Aux
, nullptr);
119 ASSERT_NE(S2
, nullptr);
120 EXPECT_EQ(S1
->getString(), "s1");
121 EXPECT_EQ(S2
->getString(), "s2");
122 ASSERT_EQ(Aux
->getNumOperands(), 2U);
123 ASSERT_TRUE(isa
<ConstantAsMetadata
>(Aux
->getOperand(0)));
124 ASSERT_TRUE(isa
<ConstantAsMetadata
>(Aux
->getOperand(1)));
125 EXPECT_EQ(mdconst::extract
<ConstantInt
>(Aux
->getOperand(0))->getValue(),
127 EXPECT_EQ(mdconst::extract
<ConstantInt
>(Aux
->getOperand(1))->getValue(),
130 TEST_F(MDBuilderTest
, createCallbackAndMerge
) {
131 MDBuilder
MDHelper(Context
);
132 auto *CB1
= MDHelper
.createCallbackEncoding(0, {1, -1}, false);
133 auto *CB2
= MDHelper
.createCallbackEncoding(2, {-1}, false);
134 ASSERT_EQ(CB1
->getNumOperands(), 4U);
135 ASSERT_TRUE(isa
<ConstantAsMetadata
>(CB1
->getOperand(0)));
136 ASSERT_TRUE(isa
<ConstantAsMetadata
>(CB1
->getOperand(1)));
137 ASSERT_TRUE(isa
<ConstantAsMetadata
>(CB1
->getOperand(2)));
138 ASSERT_TRUE(isa
<ConstantAsMetadata
>(CB1
->getOperand(3)));
139 EXPECT_EQ(mdconst::extract
<ConstantInt
>(CB1
->getOperand(0))->getValue(), 0);
140 EXPECT_EQ(mdconst::extract
<ConstantInt
>(CB1
->getOperand(1))->getValue(), 1);
141 EXPECT_EQ(mdconst::extract
<ConstantInt
>(CB1
->getOperand(2))->getValue(), -1);
142 EXPECT_EQ(mdconst::extract
<ConstantInt
>(CB1
->getOperand(3))->getValue(),
144 ASSERT_EQ(CB2
->getNumOperands(), 3U);
145 ASSERT_TRUE(isa
<ConstantAsMetadata
>(CB2
->getOperand(0)));
146 ASSERT_TRUE(isa
<ConstantAsMetadata
>(CB2
->getOperand(1)));
147 ASSERT_TRUE(isa
<ConstantAsMetadata
>(CB2
->getOperand(2)));
148 EXPECT_EQ(mdconst::extract
<ConstantInt
>(CB2
->getOperand(0))->getValue(), 2);
149 EXPECT_EQ(mdconst::extract
<ConstantInt
>(CB2
->getOperand(1))->getValue(), -1);
150 EXPECT_EQ(mdconst::extract
<ConstantInt
>(CB2
->getOperand(2))->getValue(),
152 auto *CBList
= MDNode::get(Context
, {CB1
, CB2
});
153 auto *CB3
= MDHelper
.createCallbackEncoding(4, {5}, false);
154 auto *NewCBList
= MDHelper
.mergeCallbackEncodings(CBList
, CB3
);
155 ASSERT_EQ(NewCBList
->getNumOperands(), 3U);
156 EXPECT_TRUE(NewCBList
->getOperand(0) == CB1
);
157 EXPECT_TRUE(NewCBList
->getOperand(1) == CB2
);
158 EXPECT_TRUE(NewCBList
->getOperand(2) == CB3
);
160 ASSERT_EQ(CB3
->getNumOperands(), 3U);
161 ASSERT_TRUE(isa
<ConstantAsMetadata
>(CB3
->getOperand(0)));
162 ASSERT_TRUE(isa
<ConstantAsMetadata
>(CB3
->getOperand(1)));
163 ASSERT_TRUE(isa
<ConstantAsMetadata
>(CB3
->getOperand(2)));
164 EXPECT_EQ(mdconst::extract
<ConstantInt
>(CB3
->getOperand(0))->getValue(), 4);
165 EXPECT_EQ(mdconst::extract
<ConstantInt
>(CB3
->getOperand(1))->getValue(), 5);
166 EXPECT_EQ(mdconst::extract
<ConstantInt
>(CB3
->getOperand(2))->getValue(),