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