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/IRBuilder.h"
11 #include "llvm/IR/Metadata.h"
12 #include "llvm/IR/Operator.h"
13 #include "gtest/gtest.h"
19 class MDBuilderTest
: public testing::Test
{
24 TEST_F(MDBuilderTest
, createString
) {
25 MDBuilder
MDHelper(Context
);
26 MDString
*Str0
= MDHelper
.createString("");
27 MDString
*Str1
= MDHelper
.createString("string");
28 EXPECT_EQ(Str0
->getString(), StringRef(""));
29 EXPECT_EQ(Str1
->getString(), StringRef("string"));
31 TEST_F(MDBuilderTest
, createFPMath
) {
32 MDBuilder
MDHelper(Context
);
33 MDNode
*MD0
= MDHelper
.createFPMath(0.0);
34 MDNode
*MD1
= MDHelper
.createFPMath(1.0);
35 EXPECT_EQ(MD0
, (MDNode
*)nullptr);
36 EXPECT_NE(MD1
, (MDNode
*)nullptr);
37 EXPECT_EQ(MD1
->getNumOperands(), 1U);
38 Metadata
*Op
= MD1
->getOperand(0);
39 EXPECT_TRUE(mdconst::hasa
<ConstantFP
>(Op
));
40 ConstantFP
*Val
= mdconst::extract
<ConstantFP
>(Op
);
41 EXPECT_TRUE(Val
->getType()->isFloatingPointTy());
42 EXPECT_TRUE(Val
->isExactlyValue(1.0));
44 TEST_F(MDBuilderTest
, createRangeMetadata
) {
45 MDBuilder
MDHelper(Context
);
46 APInt
A(8, 1), B(8, 2);
47 MDNode
*R0
= MDHelper
.createRange(A
, A
);
48 MDNode
*R1
= MDHelper
.createRange(A
, B
);
49 EXPECT_EQ(R0
, (MDNode
*)nullptr);
50 EXPECT_NE(R1
, (MDNode
*)nullptr);
51 EXPECT_EQ(R1
->getNumOperands(), 2U);
52 EXPECT_TRUE(mdconst::hasa
<ConstantInt
>(R1
->getOperand(0)));
53 EXPECT_TRUE(mdconst::hasa
<ConstantInt
>(R1
->getOperand(1)));
54 ConstantInt
*C0
= mdconst::extract
<ConstantInt
>(R1
->getOperand(0));
55 ConstantInt
*C1
= mdconst::extract
<ConstantInt
>(R1
->getOperand(1));
56 EXPECT_EQ(C0
->getValue(), A
);
57 EXPECT_EQ(C1
->getValue(), B
);
59 TEST_F(MDBuilderTest
, createAnonymousTBAARoot
) {
60 MDBuilder
MDHelper(Context
);
61 MDNode
*R0
= MDHelper
.createAnonymousTBAARoot();
62 MDNode
*R1
= MDHelper
.createAnonymousTBAARoot();
64 EXPECT_GE(R0
->getNumOperands(), 1U);
65 EXPECT_GE(R1
->getNumOperands(), 1U);
66 EXPECT_EQ(R0
->getOperand(0), R0
);
67 EXPECT_EQ(R1
->getOperand(0), R1
);
68 EXPECT_TRUE(R0
->getNumOperands() == 1 || R0
->getOperand(1) == nullptr);
69 EXPECT_TRUE(R1
->getNumOperands() == 1 || R1
->getOperand(1) == nullptr);
71 TEST_F(MDBuilderTest
, createTBAARoot
) {
72 MDBuilder
MDHelper(Context
);
73 MDNode
*R0
= MDHelper
.createTBAARoot("Root");
74 MDNode
*R1
= MDHelper
.createTBAARoot("Root");
76 EXPECT_GE(R0
->getNumOperands(), 1U);
77 EXPECT_TRUE(isa
<MDString
>(R0
->getOperand(0)));
78 EXPECT_EQ(cast
<MDString
>(R0
->getOperand(0))->getString(), "Root");
79 EXPECT_TRUE(R0
->getNumOperands() == 1 || R0
->getOperand(1) == nullptr);
81 TEST_F(MDBuilderTest
, createTBAANode
) {
82 MDBuilder
MDHelper(Context
);
83 MDNode
*R
= MDHelper
.createTBAARoot("Root");
84 MDNode
*N0
= MDHelper
.createTBAANode("Node", R
);
85 MDNode
*N1
= MDHelper
.createTBAANode("edoN", R
);
86 MDNode
*N2
= MDHelper
.createTBAANode("Node", R
, true);
87 MDNode
*N3
= MDHelper
.createTBAANode("Node", R
);
91 EXPECT_GE(N0
->getNumOperands(), 2U);
92 EXPECT_GE(N1
->getNumOperands(), 2U);
93 EXPECT_GE(N2
->getNumOperands(), 3U);
94 EXPECT_TRUE(isa
<MDString
>(N0
->getOperand(0)));
95 EXPECT_TRUE(isa
<MDString
>(N1
->getOperand(0)));
96 EXPECT_TRUE(isa
<MDString
>(N2
->getOperand(0)));
97 EXPECT_EQ(cast
<MDString
>(N0
->getOperand(0))->getString(), "Node");
98 EXPECT_EQ(cast
<MDString
>(N1
->getOperand(0))->getString(), "edoN");
99 EXPECT_EQ(cast
<MDString
>(N2
->getOperand(0))->getString(), "Node");
100 EXPECT_EQ(N0
->getOperand(1), R
);
101 EXPECT_EQ(N1
->getOperand(1), R
);
102 EXPECT_EQ(N2
->getOperand(1), R
);
103 EXPECT_TRUE(mdconst::hasa
<ConstantInt
>(N2
->getOperand(2)));
104 EXPECT_EQ(mdconst::extract
<ConstantInt
>(N2
->getOperand(2))->getZExtValue(),