• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/unittests/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 "llvm/IR/MDBuilder.h"
11 #include "llvm/IR/IRBuilder.h"
12 #include "llvm/IR/Metadata.h"
13 #include "llvm/IR/Operator.h"
14 #include "gtest/gtest.h"
15 
16 using namespace llvm;
17 
18 namespace {
19 
20 class MDBuilderTest : public testing::Test {
21 protected:
22   LLVMContext Context;
23 };
24 
TEST_F(MDBuilderTest,createString)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"));
31 }
TEST_F(MDBuilderTest,createFPMath)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));
44 }
TEST_F(MDBuilderTest,createRangeMetadata)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);
59 }
TEST_F(MDBuilderTest,createAnonymousTBAARoot)60 TEST_F(MDBuilderTest, createAnonymousTBAARoot) {
61   MDBuilder MDHelper(Context);
62   MDNode *R0 = MDHelper.createAnonymousTBAARoot();
63   MDNode *R1 = MDHelper.createAnonymousTBAARoot();
64   EXPECT_NE(R0, R1);
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);
71 }
TEST_F(MDBuilderTest,createTBAARoot)72 TEST_F(MDBuilderTest, createTBAARoot) {
73   MDBuilder MDHelper(Context);
74   MDNode *R0 = MDHelper.createTBAARoot("Root");
75   MDNode *R1 = MDHelper.createTBAARoot("Root");
76   EXPECT_EQ(R0, R1);
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);
81 }
TEST_F(MDBuilderTest,createTBAANode)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);
89   EXPECT_EQ(N0, N3);
90   EXPECT_NE(N0, N1);
91   EXPECT_NE(N0, N2);
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(),
106             1U);
107 }
108 }
109