1 //===- llvm/unittest/CodeGen/GlobalISel/LowLevelTypeTest.cpp --------------===//
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/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 "llvm/Support/TypeSize.h"
15 #include "gtest/gtest.h"
21 TEST(LowLevelTypeTest
, Scalar
) {
25 for (unsigned S
: {0U, 1U, 17U, 32U, 64U, 0xfffffU
}) {
26 const LLT Ty
= LLT::scalar(S
);
29 ASSERT_TRUE(Ty
.isValid());
30 ASSERT_TRUE(Ty
.isScalar());
32 ASSERT_FALSE(Ty
.isPointer());
33 ASSERT_FALSE(Ty
.isVector());
36 EXPECT_EQ(S
, Ty
.getSizeInBits());
37 EXPECT_EQ(S
, Ty
.getScalarSizeInBits());
39 // Test equality operators.
40 EXPECT_TRUE(Ty
== Ty
);
41 EXPECT_FALSE(Ty
!= Ty
);
43 // Test Type->LLT conversion.
45 Type
*IRTy
= IntegerType::get(C
, S
);
46 EXPECT_EQ(Ty
, getLLTForType(*IRTy
, DL
));
51 TEST(LowLevelTypeTest
, Vector
) {
55 for (unsigned S
: {0U, 1U, 17U, 32U, 64U, 0xfffU
}) {
57 {ElementCount::getFixed(2), ElementCount::getFixed(3),
58 ElementCount::getFixed(4), ElementCount::getFixed(32),
59 ElementCount::getFixed(0xff), ElementCount::getScalable(2),
60 ElementCount::getScalable(3), ElementCount::getScalable(4),
61 ElementCount::getScalable(32), ElementCount::getScalable(0xff)}) {
62 const LLT STy
= LLT::scalar(S
);
63 const LLT VTy
= LLT::vector(EC
, S
);
65 // Test the alternative vector().
67 const LLT VSTy
= LLT::vector(EC
, STy
);
71 // Test getElementType().
72 EXPECT_EQ(STy
, VTy
.getElementType());
75 ASSERT_TRUE(VTy
.isValid());
76 ASSERT_TRUE(VTy
.isVector());
78 ASSERT_FALSE(VTy
.isScalar());
79 ASSERT_FALSE(VTy
.isPointer());
82 EXPECT_EQ(S
, VTy
.getScalarSizeInBits());
83 EXPECT_EQ(EC
, VTy
.getElementCount());
85 EXPECT_EQ(S
* EC
.getFixedValue(), VTy
.getSizeInBits());
87 EXPECT_EQ(TypeSize::Scalable(S
* EC
.getKnownMinValue()),
90 // Test equality operators.
91 EXPECT_TRUE(VTy
== VTy
);
92 EXPECT_FALSE(VTy
!= VTy
);
94 // Test inequality operators on..
98 // Test Type->LLT conversion.
100 Type
*IRSTy
= IntegerType::get(C
, S
);
101 Type
*IRTy
= VectorType::get(IRSTy
, EC
);
102 EXPECT_EQ(VTy
, getLLTForType(*IRTy
, DL
));
108 TEST(LowLevelTypeTest
, ScalarOrVector
) {
109 // Test version with number of bits for scalar type.
110 EXPECT_EQ(LLT::scalar(32),
111 LLT::scalarOrVector(ElementCount::getFixed(1), 32));
112 EXPECT_EQ(LLT::fixed_vector(2, 32),
113 LLT::scalarOrVector(ElementCount::getFixed(2), 32));
114 EXPECT_EQ(LLT::scalable_vector(1, 32),
115 LLT::scalarOrVector(ElementCount::getScalable(1), 32));
117 // Test version with LLT for scalar type.
118 EXPECT_EQ(LLT::scalar(32),
119 LLT::scalarOrVector(ElementCount::getFixed(1), LLT::scalar(32)));
120 EXPECT_EQ(LLT::fixed_vector(2, 32),
121 LLT::scalarOrVector(ElementCount::getFixed(2), LLT::scalar(32)));
123 // Test with pointer elements.
124 EXPECT_EQ(LLT::pointer(1, 32), LLT::scalarOrVector(ElementCount::getFixed(1),
125 LLT::pointer(1, 32)));
127 LLT::fixed_vector(2, LLT::pointer(1, 32)),
128 LLT::scalarOrVector(ElementCount::getFixed(2), LLT::pointer(1, 32)));
131 TEST(LowLevelTypeTest
, ChangeElementType
) {
132 const LLT P0
= LLT::pointer(0, 32);
133 const LLT P1
= LLT::pointer(1, 64);
135 const LLT S32
= LLT::scalar(32);
136 const LLT S64
= LLT::scalar(64);
138 const LLT V2S32
= LLT::fixed_vector(2, 32);
139 const LLT V2S64
= LLT::fixed_vector(2, 64);
141 const LLT V2P0
= LLT::fixed_vector(2, P0
);
142 const LLT V2P1
= LLT::fixed_vector(2, P1
);
144 EXPECT_EQ(S64
, S32
.changeElementType(S64
));
145 EXPECT_EQ(S32
, S32
.changeElementType(S32
));
147 EXPECT_EQ(S32
, S64
.changeElementSize(32));
148 EXPECT_EQ(S32
, S32
.changeElementSize(32));
150 EXPECT_EQ(V2S64
, V2S32
.changeElementType(S64
));
151 EXPECT_EQ(V2S32
, V2S64
.changeElementType(S32
));
153 EXPECT_EQ(V2S64
, V2S32
.changeElementSize(64));
154 EXPECT_EQ(V2S32
, V2S64
.changeElementSize(32));
156 EXPECT_EQ(P0
, S32
.changeElementType(P0
));
157 EXPECT_EQ(S32
, P0
.changeElementType(S32
));
159 EXPECT_EQ(V2P1
, V2P0
.changeElementType(P1
));
160 EXPECT_EQ(V2S32
, V2P0
.changeElementType(S32
));
162 // Similar tests for for scalable vectors.
163 const LLT NXV2S32
= LLT::scalable_vector(2, 32);
164 const LLT NXV2S64
= LLT::scalable_vector(2, 64);
166 const LLT NXV2P0
= LLT::scalable_vector(2, P0
);
167 const LLT NXV2P1
= LLT::scalable_vector(2, P1
);
169 EXPECT_EQ(NXV2S64
, NXV2S32
.changeElementType(S64
));
170 EXPECT_EQ(NXV2S32
, NXV2S64
.changeElementType(S32
));
172 EXPECT_EQ(NXV2S64
, NXV2S32
.changeElementSize(64));
173 EXPECT_EQ(NXV2S32
, NXV2S64
.changeElementSize(32));
175 EXPECT_EQ(NXV2P1
, NXV2P0
.changeElementType(P1
));
176 EXPECT_EQ(NXV2S32
, NXV2P0
.changeElementType(S32
));
179 TEST(LowLevelTypeTest
, ChangeNumElements
) {
180 const LLT P0
= LLT::pointer(0, 32);
181 const LLT V2P0
= LLT::fixed_vector(2, P0
);
182 const LLT V3P0
= LLT::fixed_vector(3, P0
);
184 const LLT S64
= LLT::scalar(64);
185 const LLT V2S64
= LLT::fixed_vector(2, 64);
186 const LLT V3S64
= LLT::fixed_vector(3, 64);
189 EXPECT_EQ(S64
, V2S64
.changeElementCount(ElementCount::getFixed(1)));
192 EXPECT_EQ(V3S64
, V2S64
.changeElementCount(ElementCount::getFixed(3)));
195 EXPECT_EQ(V2S64
, S64
.changeElementCount(ElementCount::getFixed(2)));
197 EXPECT_EQ(P0
, V2P0
.changeElementCount(ElementCount::getFixed(1)));
198 EXPECT_EQ(V3P0
, V2P0
.changeElementCount(ElementCount::getFixed(3)));
199 EXPECT_EQ(V2P0
, P0
.changeElementCount(ElementCount::getFixed(2)));
201 const LLT NXV2S64
= LLT::scalable_vector(2, 64);
202 const LLT NXV3S64
= LLT::scalable_vector(3, 64);
203 const LLT NXV2P0
= LLT::scalable_vector(2, P0
);
205 // Scalable vector to scalar
206 EXPECT_EQ(S64
, NXV2S64
.changeElementCount(ElementCount::getFixed(1)));
207 EXPECT_EQ(P0
, NXV2P0
.changeElementCount(ElementCount::getFixed(1)));
209 // Fixed-width vector to scalable vector
210 EXPECT_EQ(NXV3S64
, V2S64
.changeElementCount(ElementCount::getScalable(3)));
212 // Scalable vector to fixed-width vector
213 EXPECT_EQ(V3P0
, NXV2P0
.changeElementCount(ElementCount::getFixed(3)));
215 // Scalar to scalable vector
216 EXPECT_EQ(NXV2S64
, S64
.changeElementCount(ElementCount::getScalable(2)));
217 EXPECT_EQ(NXV2P0
, P0
.changeElementCount(ElementCount::getScalable(2)));
220 #ifdef GTEST_HAS_DEATH_TEST
223 // Invalid to directly change the element size for pointers.
224 TEST(LowLevelTypeTest
, ChangeElementTypeDeath
) {
225 const LLT P0
= LLT::pointer(0, 32);
226 const LLT V2P0
= LLT::fixed_vector(2, P0
);
228 EXPECT_DEATH(P0
.changeElementSize(64),
229 "invalid to directly change element size for pointers");
230 EXPECT_DEATH(V2P0
.changeElementSize(64),
231 "invalid to directly change element size for pointers");
233 // Make sure this still fails even without a change in size.
234 EXPECT_DEATH(P0
.changeElementSize(32),
235 "invalid to directly change element size for pointers");
236 EXPECT_DEATH(V2P0
.changeElementSize(32),
237 "invalid to directly change element size for pointers");
243 TEST(LowLevelTypeTest
, Pointer
) {
245 DataLayout
DL("p64:64:64-p127:512:512:512-p16777215:65528:8");
247 for (unsigned AS
: {0U, 1U, 127U, 0xffffU
,
248 static_cast<unsigned>(maxUIntN(23)),
249 static_cast<unsigned>(maxUIntN(24))}) {
250 for (ElementCount EC
:
251 {ElementCount::getFixed(2), ElementCount::getFixed(3),
252 ElementCount::getFixed(4), ElementCount::getFixed(256),
253 ElementCount::getFixed(65535), ElementCount::getScalable(2),
254 ElementCount::getScalable(3), ElementCount::getScalable(4),
255 ElementCount::getScalable(256), ElementCount::getScalable(65535)}) {
256 const LLT Ty
= LLT::pointer(AS
, DL
.getPointerSizeInBits(AS
));
257 const LLT VTy
= LLT::vector(EC
, Ty
);
260 ASSERT_TRUE(Ty
.isValid());
261 ASSERT_TRUE(Ty
.isPointer());
263 ASSERT_FALSE(Ty
.isScalar());
264 ASSERT_FALSE(Ty
.isVector());
266 ASSERT_TRUE(VTy
.isValid());
267 ASSERT_TRUE(VTy
.isVector());
268 ASSERT_TRUE(VTy
.getElementType().isPointer());
270 EXPECT_EQ(Ty
, VTy
.getElementType());
271 EXPECT_EQ(Ty
.getSizeInBits(), VTy
.getScalarSizeInBits());
273 // Test address space.
274 EXPECT_EQ(AS
, Ty
.getAddressSpace());
275 EXPECT_EQ(AS
, VTy
.getElementType().getAddressSpace());
277 // Test equality operators.
278 EXPECT_TRUE(Ty
== Ty
);
279 EXPECT_FALSE(Ty
!= Ty
);
280 EXPECT_TRUE(VTy
== VTy
);
281 EXPECT_FALSE(VTy
!= VTy
);
283 // Test Type->LLT conversion.
284 Type
*IRTy
= PointerType::get(IntegerType::get(C
, 8), AS
);
285 EXPECT_EQ(Ty
, getLLTForType(*IRTy
, DL
));
287 VectorType::get(PointerType::get(IntegerType::get(C
, 8), AS
), EC
);
288 EXPECT_EQ(VTy
, getLLTForType(*IRVTy
, DL
));
293 TEST(LowLevelTypeTest
, Invalid
) {
296 ASSERT_FALSE(Ty
.isValid());
297 ASSERT_FALSE(Ty
.isScalar());
298 ASSERT_FALSE(Ty
.isPointer());
299 ASSERT_FALSE(Ty
.isVector());
302 TEST(LowLevelTypeTest
, Divide
) {
303 // Test basic scalar->scalar cases.
304 EXPECT_EQ(LLT::scalar(16), LLT::scalar(32).divide(2));
305 EXPECT_EQ(LLT::scalar(8), LLT::scalar(32).divide(4));
306 EXPECT_EQ(LLT::scalar(8), LLT::scalar(32).divide(4));
308 // Test pointer->scalar
309 EXPECT_EQ(LLT::scalar(32), LLT::pointer(0, 64).divide(2));
311 // Test dividing vectors.
312 EXPECT_EQ(LLT::scalar(32), LLT::fixed_vector(2, 32).divide(2));
313 EXPECT_EQ(LLT::fixed_vector(2, 32), LLT::fixed_vector(4, 32).divide(2));
315 // Test vector of pointers
316 EXPECT_EQ(LLT::pointer(1, 64),
317 LLT::fixed_vector(4, LLT::pointer(1, 64)).divide(4));
318 EXPECT_EQ(LLT::fixed_vector(2, LLT::pointer(1, 64)),
319 LLT::fixed_vector(4, LLT::pointer(1, 64)).divide(2));
322 TEST(LowLevelTypeTest
, MultiplyElements
) {
323 // Basic scalar->vector cases
324 EXPECT_EQ(LLT::fixed_vector(2, 16), LLT::scalar(16).multiplyElements(2));
325 EXPECT_EQ(LLT::fixed_vector(3, 16), LLT::scalar(16).multiplyElements(3));
326 EXPECT_EQ(LLT::fixed_vector(4, 32), LLT::scalar(32).multiplyElements(4));
327 EXPECT_EQ(LLT::fixed_vector(4, 7), LLT::scalar(7).multiplyElements(4));
329 // Basic vector to vector cases
330 EXPECT_EQ(LLT::fixed_vector(4, 32),
331 LLT::fixed_vector(2, 32).multiplyElements(2));
332 EXPECT_EQ(LLT::fixed_vector(9, 32),
333 LLT::fixed_vector(3, 32).multiplyElements(3));
335 // Pointer to vector of pointers
336 EXPECT_EQ(LLT::fixed_vector(2, LLT::pointer(0, 32)),
337 LLT::pointer(0, 32).multiplyElements(2));
338 EXPECT_EQ(LLT::fixed_vector(3, LLT::pointer(1, 32)),
339 LLT::pointer(1, 32).multiplyElements(3));
340 EXPECT_EQ(LLT::fixed_vector(4, LLT::pointer(1, 64)),
341 LLT::pointer(1, 64).multiplyElements(4));
343 // Vector of pointers to vector of pointers
344 EXPECT_EQ(LLT::fixed_vector(8, LLT::pointer(1, 64)),
345 LLT::fixed_vector(2, LLT::pointer(1, 64)).multiplyElements(4));
346 EXPECT_EQ(LLT::fixed_vector(9, LLT::pointer(1, 32)),
347 LLT::fixed_vector(3, LLT::pointer(1, 32)).multiplyElements(3));
350 EXPECT_EQ(LLT::scalable_vector(4, 16),
351 LLT::scalable_vector(2, 16).multiplyElements(2));
352 EXPECT_EQ(LLT::scalable_vector(6, 16),
353 LLT::scalable_vector(2, 16).multiplyElements(3));
354 EXPECT_EQ(LLT::scalable_vector(9, 16),
355 LLT::scalable_vector(3, 16).multiplyElements(3));
356 EXPECT_EQ(LLT::scalable_vector(4, 32),
357 LLT::scalable_vector(2, 32).multiplyElements(2));
358 EXPECT_EQ(LLT::scalable_vector(256, 32),
359 LLT::scalable_vector(8, 32).multiplyElements(32));
361 // Scalable vectors of pointers
362 EXPECT_EQ(LLT::scalable_vector(4, LLT::pointer(0, 32)),
363 LLT::scalable_vector(2, LLT::pointer(0, 32)).multiplyElements(2));
364 EXPECT_EQ(LLT::scalable_vector(32, LLT::pointer(1, 64)),
365 LLT::scalable_vector(8, LLT::pointer(1, 64)).multiplyElements(4));