[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / unittests / CodeGen / LowLevelTypeTest.cpp
blobbf4277d82fd2ff03e3856e00933ed751d91d9813
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 //===----------------------------------------------------------------------===//
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"
16 using namespace llvm;
18 namespace {
20 TEST(LowLevelTypeTest, Scalar) {
21 LLVMContext C;
22 DataLayout DL("");
24 for (unsigned S : {1U, 17U, 32U, 64U, 0xfffffU}) {
25 const LLT Ty = LLT::scalar(S);
27 // Test kind.
28 ASSERT_TRUE(Ty.isValid());
29 ASSERT_TRUE(Ty.isScalar());
31 ASSERT_FALSE(Ty.isPointer());
32 ASSERT_FALSE(Ty.isVector());
34 // Test sizes.
35 EXPECT_EQ(S, Ty.getSizeInBits());
36 EXPECT_EQ(S, Ty.getScalarSizeInBits());
38 // Test equality operators.
39 EXPECT_TRUE(Ty == Ty);
40 EXPECT_FALSE(Ty != Ty);
42 // Test Type->LLT conversion.
43 Type *IRTy = IntegerType::get(C, S);
44 EXPECT_EQ(Ty, getLLTForType(*IRTy, DL));
48 TEST(LowLevelTypeTest, Vector) {
49 LLVMContext C;
50 DataLayout DL("");
52 for (unsigned S : {1U, 17U, 32U, 64U, 0xfffU}) {
53 for (uint16_t Elts : {2U, 3U, 4U, 32U, 0xffU}) {
54 const LLT STy = LLT::scalar(S);
55 const LLT VTy = LLT::vector(Elts, S);
57 // Test the alternative vector().
59 const LLT VSTy = LLT::vector(Elts, STy);
60 EXPECT_EQ(VTy, VSTy);
63 // Test getElementType().
64 EXPECT_EQ(STy, VTy.getElementType());
66 // Test kind.
67 ASSERT_TRUE(VTy.isValid());
68 ASSERT_TRUE(VTy.isVector());
70 ASSERT_FALSE(VTy.isScalar());
71 ASSERT_FALSE(VTy.isPointer());
73 // Test sizes.
74 EXPECT_EQ(S * Elts, VTy.getSizeInBits());
75 EXPECT_EQ(S, VTy.getScalarSizeInBits());
76 EXPECT_EQ(Elts, VTy.getNumElements());
78 // Test equality operators.
79 EXPECT_TRUE(VTy == VTy);
80 EXPECT_FALSE(VTy != VTy);
82 // Test inequality operators on..
83 // ..different kind.
84 EXPECT_NE(VTy, STy);
86 // Test Type->LLT conversion.
87 Type *IRSTy = IntegerType::get(C, S);
88 Type *IRTy = VectorType::get(IRSTy, Elts);
89 EXPECT_EQ(VTy, getLLTForType(*IRTy, DL));
94 TEST(LowLevelTypeTest, ScalarOrVector) {
95 // Test version with number of bits for scalar type.
96 EXPECT_EQ(LLT::scalar(32), LLT::scalarOrVector(1, 32));
97 EXPECT_EQ(LLT::vector(2, 32), LLT::scalarOrVector(2, 32));
99 // Test version with LLT for scalar type.
100 EXPECT_EQ(LLT::scalar(32), LLT::scalarOrVector(1, LLT::scalar(32)));
101 EXPECT_EQ(LLT::vector(2, 32), LLT::scalarOrVector(2, LLT::scalar(32)));
103 // Test with pointer elements.
104 EXPECT_EQ(LLT::pointer(1, 32), LLT::scalarOrVector(1, LLT::pointer(1, 32)));
105 EXPECT_EQ(LLT::vector(2, LLT::pointer(1, 32)),
106 LLT::scalarOrVector(2, LLT::pointer(1, 32)));
109 TEST(LowLevelTypeTest, ChangeElementType) {
110 const LLT P0 = LLT::pointer(0, 32);
111 const LLT P1 = LLT::pointer(1, 64);
113 const LLT S32 = LLT::scalar(32);
114 const LLT S64 = LLT::scalar(64);
116 const LLT V2S32 = LLT::vector(2, 32);
117 const LLT V2S64 = LLT::vector(2, 64);
119 const LLT V2P0 = LLT::vector(2, P0);
120 const LLT V2P1 = LLT::vector(2, P1);
122 EXPECT_EQ(S64, S32.changeElementType(S64));
123 EXPECT_EQ(S32, S32.changeElementType(S32));
125 EXPECT_EQ(S32, S64.changeElementSize(32));
126 EXPECT_EQ(S32, S32.changeElementSize(32));
128 EXPECT_EQ(V2S64, V2S32.changeElementType(S64));
129 EXPECT_EQ(V2S32, V2S64.changeElementType(S32));
131 EXPECT_EQ(V2S64, V2S32.changeElementSize(64));
132 EXPECT_EQ(V2S32, V2S64.changeElementSize(32));
134 EXPECT_EQ(P0, S32.changeElementType(P0));
135 EXPECT_EQ(S32, P0.changeElementType(S32));
137 EXPECT_EQ(V2P1, V2P0.changeElementType(P1));
138 EXPECT_EQ(V2S32, V2P0.changeElementType(S32));
141 #ifdef GTEST_HAS_DEATH_TEST
142 #ifndef NDEBUG
144 // Invalid to directly change the element size for pointers.
145 TEST(LowLevelTypeTest, ChangeElementTypeDeath) {
146 const LLT P0 = LLT::pointer(0, 32);
147 const LLT V2P0 = LLT::vector(2, P0);
149 EXPECT_DEATH(P0.changeElementSize(64),
150 "invalid to directly change element size for pointers");
151 EXPECT_DEATH(V2P0.changeElementSize(64),
152 "invalid to directly change element size for pointers");
154 // Make sure this still fails even without a change in size.
155 EXPECT_DEATH(P0.changeElementSize(32),
156 "invalid to directly change element size for pointers");
157 EXPECT_DEATH(V2P0.changeElementSize(32),
158 "invalid to directly change element size for pointers");
161 #endif
162 #endif
164 TEST(LowLevelTypeTest, Pointer) {
165 LLVMContext C;
166 DataLayout DL("p64:64:64-p127:512:512:512-p16777215:65528:8");
168 for (unsigned AS : {0U, 1U, 127U, 0xffffU,
169 static_cast<unsigned>(maxUIntN(23)),
170 static_cast<unsigned>(maxUIntN(24))}) {
171 for (unsigned NumElts : {2, 3, 4, 256, 65535}) {
172 const LLT Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
173 const LLT VTy = LLT::vector(NumElts, Ty);
175 // Test kind.
176 ASSERT_TRUE(Ty.isValid());
177 ASSERT_TRUE(Ty.isPointer());
179 ASSERT_FALSE(Ty.isScalar());
180 ASSERT_FALSE(Ty.isVector());
182 ASSERT_TRUE(VTy.isValid());
183 ASSERT_TRUE(VTy.isVector());
184 ASSERT_TRUE(VTy.getElementType().isPointer());
186 EXPECT_EQ(Ty, VTy.getElementType());
187 EXPECT_EQ(Ty.getSizeInBits(), VTy.getScalarSizeInBits());
189 // Test address space.
190 EXPECT_EQ(AS, Ty.getAddressSpace());
191 EXPECT_EQ(AS, VTy.getElementType().getAddressSpace());
193 // Test equality operators.
194 EXPECT_TRUE(Ty == Ty);
195 EXPECT_FALSE(Ty != Ty);
196 EXPECT_TRUE(VTy == VTy);
197 EXPECT_FALSE(VTy != VTy);
199 // Test Type->LLT conversion.
200 Type *IRTy = PointerType::get(IntegerType::get(C, 8), AS);
201 EXPECT_EQ(Ty, getLLTForType(*IRTy, DL));
202 Type *IRVTy =
203 VectorType::get(PointerType::get(IntegerType::get(C, 8), AS), NumElts);
204 EXPECT_EQ(VTy, getLLTForType(*IRVTy, DL));
209 TEST(LowLevelTypeTest, Invalid) {
210 const LLT Ty;
212 ASSERT_FALSE(Ty.isValid());
213 ASSERT_FALSE(Ty.isScalar());
214 ASSERT_FALSE(Ty.isPointer());
215 ASSERT_FALSE(Ty.isVector());