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