1 //===- llvm/unittest/CodeGen/GlobalISel/LowLevelTypeTest.cpp --------------===//
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/CodeGen/LowLevelType.h"
11 #include "llvm/IR/DataLayout.h"
12 #include "llvm/IR/DerivedTypes.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Type.h"
15 #include "gtest/gtest.h"
16
17 using namespace llvm;
18
19 // Define a pretty printer to help debugging when things go wrong.
20 namespace llvm {
21 std::ostream &
operator <<(std::ostream & OS,const llvm::LLT Ty)22 operator<<(std::ostream &OS, const llvm::LLT Ty) {
23 std::string Repr;
24 raw_string_ostream SS{Repr};
25 Ty.print(SS);
26 OS << SS.str();
27 return OS;
28 }
29 }
30
31 namespace {
32
TEST(LowLevelTypeTest,Scalar)33 TEST(LowLevelTypeTest, Scalar) {
34 LLVMContext C;
35 DataLayout DL("");
36
37 for (unsigned S : {1U, 17U, 32U, 64U, 0xfffffU}) {
38 const LLT Ty = LLT::scalar(S);
39
40 // Test kind.
41 ASSERT_TRUE(Ty.isValid());
42 ASSERT_TRUE(Ty.isScalar());
43
44 ASSERT_FALSE(Ty.isPointer());
45 ASSERT_FALSE(Ty.isVector());
46
47 // Test sizes.
48 EXPECT_EQ(S, Ty.getSizeInBits());
49 EXPECT_EQ(S, Ty.getScalarSizeInBits());
50
51 // Test equality operators.
52 EXPECT_TRUE(Ty == Ty);
53 EXPECT_FALSE(Ty != Ty);
54
55 // Test Type->LLT conversion.
56 Type *IRTy = IntegerType::get(C, S);
57 EXPECT_EQ(Ty, getLLTForType(*IRTy, DL));
58 }
59 }
60
TEST(LowLevelTypeTest,Vector)61 TEST(LowLevelTypeTest, Vector) {
62 LLVMContext C;
63 DataLayout DL("");
64
65 for (unsigned S : {1U, 17U, 32U, 64U, 0xfffU}) {
66 for (uint16_t Elts : {2U, 3U, 4U, 32U, 0xffU}) {
67 const LLT STy = LLT::scalar(S);
68 const LLT VTy = LLT::vector(Elts, S);
69
70 // Test the alternative vector().
71 {
72 const LLT VSTy = LLT::vector(Elts, STy);
73 EXPECT_EQ(VTy, VSTy);
74 }
75
76 // Test getElementType().
77 EXPECT_EQ(STy, VTy.getElementType());
78
79 // Test kind.
80 ASSERT_TRUE(VTy.isValid());
81 ASSERT_TRUE(VTy.isVector());
82
83 ASSERT_FALSE(VTy.isScalar());
84 ASSERT_FALSE(VTy.isPointer());
85
86 // Test sizes.
87 EXPECT_EQ(S * Elts, VTy.getSizeInBits());
88 EXPECT_EQ(S, VTy.getScalarSizeInBits());
89 EXPECT_EQ(Elts, VTy.getNumElements());
90
91 // Test equality operators.
92 EXPECT_TRUE(VTy == VTy);
93 EXPECT_FALSE(VTy != VTy);
94
95 // Test inequality operators on..
96 // ..different kind.
97 EXPECT_NE(VTy, STy);
98
99 // Test Type->LLT conversion.
100 Type *IRSTy = IntegerType::get(C, S);
101 Type *IRTy = VectorType::get(IRSTy, Elts);
102 EXPECT_EQ(VTy, getLLTForType(*IRTy, DL));
103 }
104 }
105 }
106
TEST(LowLevelTypeTest,Pointer)107 TEST(LowLevelTypeTest, Pointer) {
108 LLVMContext C;
109 DataLayout DL("");
110
111 for (unsigned AS : {0U, 1U, 127U, 0xffffU}) {
112 const LLT Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
113 const LLT VTy = LLT::vector(4, Ty);
114
115 // Test kind.
116 ASSERT_TRUE(Ty.isValid());
117 ASSERT_TRUE(Ty.isPointer());
118
119 ASSERT_FALSE(Ty.isScalar());
120 ASSERT_FALSE(Ty.isVector());
121
122 ASSERT_TRUE(VTy.isValid());
123 ASSERT_TRUE(VTy.isVector());
124 ASSERT_TRUE(VTy.getElementType().isPointer());
125
126 // Test addressspace.
127 EXPECT_EQ(AS, Ty.getAddressSpace());
128 EXPECT_EQ(AS, VTy.getElementType().getAddressSpace());
129
130 // Test equality operators.
131 EXPECT_TRUE(Ty == Ty);
132 EXPECT_FALSE(Ty != Ty);
133 EXPECT_TRUE(VTy == VTy);
134 EXPECT_FALSE(VTy != VTy);
135
136 // Test Type->LLT conversion.
137 Type *IRTy = PointerType::get(IntegerType::get(C, 8), AS);
138 EXPECT_EQ(Ty, getLLTForType(*IRTy, DL));
139 Type *IRVTy =
140 VectorType::get(PointerType::get(IntegerType::get(C, 8), AS), 4);
141 EXPECT_EQ(VTy, getLLTForType(*IRVTy, DL));
142 }
143 }
144
TEST(LowLevelTypeTest,Invalid)145 TEST(LowLevelTypeTest, Invalid) {
146 const LLT Ty;
147
148 ASSERT_FALSE(Ty.isValid());
149 ASSERT_FALSE(Ty.isScalar());
150 ASSERT_FALSE(Ty.isPointer());
151 ASSERT_FALSE(Ty.isVector());
152 }
153
154 }
155